Skip to content

Commit

Permalink
[WebAssembly] Bitselect and min/max builtins
Browse files Browse the repository at this point in the history
Reviewers: aheejin, dschuff

Subscribers: sbc100, jgravelle-google, sunfish, cfe-commits

Differential Revision: https://reviews.llvm.org/D53685

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@345301 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
tlively committed Oct 25, 2018
1 parent e07b147 commit 4a3fc17
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/clang/Basic/BuiltinsWebAssembly.def
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ BUILTIN(__builtin_wasm_trunc_saturate_u_i64_f32, "LLif", "nc")
BUILTIN(__builtin_wasm_trunc_saturate_s_i64_f64, "LLid", "nc")
BUILTIN(__builtin_wasm_trunc_saturate_u_i64_f64, "LLid", "nc")

// Floating point min/max
BUILTIN(__builtin_wasm_min_f32, "fff", "nc")
BUILTIN(__builtin_wasm_max_f32, "fff", "nc")
BUILTIN(__builtin_wasm_min_f64, "ddd", "nc")
BUILTIN(__builtin_wasm_max_f64, "ddd", "nc")

// SIMD builtins
BUILTIN(__builtin_wasm_extract_lane_s_i8x16, "iV16cIi", "nc")
BUILTIN(__builtin_wasm_extract_lane_u_i8x16, "iV16cIi", "nc")
Expand Down Expand Up @@ -76,6 +82,8 @@ BUILTIN(__builtin_wasm_sub_saturate_u_i8x16, "V16cV16cV16c", "nc")
BUILTIN(__builtin_wasm_sub_saturate_s_i16x8, "V8sV8sV8s", "nc")
BUILTIN(__builtin_wasm_sub_saturate_u_i16x8, "V8sV8sV8s", "nc")

BUILTIN(__builtin_wasm_bitselect, "V4iV4iV4iV4i", "nc")

BUILTIN(__builtin_wasm_any_true_i8x16, "iV16c", "nc")
BUILTIN(__builtin_wasm_any_true_i16x8, "iV8s", "nc")
BUILTIN(__builtin_wasm_any_true_i32x4, "iV4i", "nc")
Expand All @@ -88,6 +96,11 @@ BUILTIN(__builtin_wasm_all_true_i64x2, "iV2LLi", "nc")
BUILTIN(__builtin_wasm_abs_f32x4, "V4fV4f", "nc")
BUILTIN(__builtin_wasm_abs_f64x2, "V2dV2d", "nc")

BUILTIN(__builtin_wasm_min_f32x4, "V4fV4fV4f", "nc")
BUILTIN(__builtin_wasm_max_f32x4, "V4fV4fV4f", "nc")
BUILTIN(__builtin_wasm_min_f64x2, "V2dV2dV2d", "nc")
BUILTIN(__builtin_wasm_max_f64x2, "V2dV2dV2d", "nc")

BUILTIN(__builtin_wasm_sqrt_f32x4, "V4fV4f", "nc")
BUILTIN(__builtin_wasm_sqrt_f64x2, "V2dV2d", "nc")

Expand Down
28 changes: 28 additions & 0 deletions lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12544,6 +12544,26 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
{ResT, Src->getType()});
return Builder.CreateCall(Callee, {Src});
}
case WebAssembly::BI__builtin_wasm_min_f32:
case WebAssembly::BI__builtin_wasm_min_f64:
case WebAssembly::BI__builtin_wasm_min_f32x4:
case WebAssembly::BI__builtin_wasm_min_f64x2: {
Value *LHS = EmitScalarExpr(E->getArg(0));
Value *RHS = EmitScalarExpr(E->getArg(1));
Value *Callee = CGM.getIntrinsic(Intrinsic::minimum,
ConvertType(E->getType()));
return Builder.CreateCall(Callee, {LHS, RHS});
}
case WebAssembly::BI__builtin_wasm_max_f32:
case WebAssembly::BI__builtin_wasm_max_f64:
case WebAssembly::BI__builtin_wasm_max_f32x4:
case WebAssembly::BI__builtin_wasm_max_f64x2: {
Value *LHS = EmitScalarExpr(E->getArg(0));
Value *RHS = EmitScalarExpr(E->getArg(1));
Value *Callee = CGM.getIntrinsic(Intrinsic::maximum,
ConvertType(E->getType()));
return Builder.CreateCall(Callee, {LHS, RHS});
}
case WebAssembly::BI__builtin_wasm_extract_lane_s_i8x16:
case WebAssembly::BI__builtin_wasm_extract_lane_u_i8x16:
case WebAssembly::BI__builtin_wasm_extract_lane_s_i16x8:
Expand Down Expand Up @@ -12636,6 +12656,14 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
Value *Callee = CGM.getIntrinsic(IntNo, ConvertType(E->getType()));
return Builder.CreateCall(Callee, {LHS, RHS});
}
case WebAssembly::BI__builtin_wasm_bitselect: {
Value *V1 = EmitScalarExpr(E->getArg(0));
Value *V2 = EmitScalarExpr(E->getArg(1));
Value *C = EmitScalarExpr(E->getArg(2));
Value *Callee = CGM.getIntrinsic(Intrinsic::wasm_bitselect,
ConvertType(E->getType()));
return Builder.CreateCall(Callee, {V1, V2, C});
}
case WebAssembly::BI__builtin_wasm_any_true_i8x16:
case WebAssembly::BI__builtin_wasm_any_true_i16x8:
case WebAssembly::BI__builtin_wasm_any_true_i32x4:
Expand Down
60 changes: 60 additions & 0 deletions test/CodeGen/builtins-wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,31 @@ long long trunc_saturate_u_i64_f64(double f) {
// WEBASSEMBLY-NEXT: ret
}

float min_f32(float x, float y) {
return __builtin_wasm_min_f32(x, y);
// WEBASSEMBLY: call float @llvm.minimum.f32(float %x, float %y)
// WEBASSEMBLY-NEXT: ret
}

float max_f32(float x, float y) {
return __builtin_wasm_max_f32(x, y);
// WEBASSEMBLY: call float @llvm.maximum.f32(float %x, float %y)
// WEBASSEMBLY-NEXT: ret
}

double min_f64(double x, double y) {
return __builtin_wasm_min_f64(x, y);
// WEBASSEMBLY: call double @llvm.minimum.f64(double %x, double %y)
// WEBASSEMBLY-NEXT: ret
}

double max_f64(double x, double y) {
return __builtin_wasm_max_f64(x, y);
// WEBASSEMBLY: call double @llvm.maximum.f64(double %x, double %y)
// WEBASSEMBLY-NEXT: ret
}


int extract_lane_s_i8x16(i8x16 v) {
return __builtin_wasm_extract_lane_s_i8x16(v, 13);
// WEBASSEMBLY: extractelement <16 x i8> %v, i32 13
Expand Down Expand Up @@ -277,6 +302,13 @@ i16x8 sub_saturate_u_i16x8(i16x8 x, i16x8 y) {
// WEBASSEMBLY-NEXT: ret
}

i32x4 bitselect(i32x4 x, i32x4 y, i32x4 c) {
return __builtin_wasm_bitselect(x, y, c);
// WEBASSEMBLY: call <4 x i32> @llvm.wasm.bitselect.v4i32(
// WEBASSEMBLY-SAME: <4 x i32> %x, <4 x i32> %y, <4 x i32> %c)
// WEBASSEMBLY-NEXT: ret
}

int any_true_i8x16(i8x16 x) {
return __builtin_wasm_any_true_i8x16(x);
// WEBASSEMBLY: call i32 @llvm.wasm.anytrue.v16i8(<16 x i8> %x)
Expand Down Expand Up @@ -337,6 +369,34 @@ f64x2 abs_f64x2(f64x2 x) {
// WEBASSEMBLY: ret
}

f32x4 min_f32x4(f32x4 x, f32x4 y) {
return __builtin_wasm_min_f32x4(x, y);
// WEBASSEMBLY: call <4 x float> @llvm.minimum.v4f32(
// WEBASSEMBLY-SAME: <4 x float> %x, <4 x float> %y)
// WEBASSEMBLY-NEXT: ret
}

f32x4 max_f32x4(f32x4 x, f32x4 y) {
return __builtin_wasm_max_f32x4(x, y);
// WEBASSEMBLY: call <4 x float> @llvm.maximum.v4f32(
// WEBASSEMBLY-SAME: <4 x float> %x, <4 x float> %y)
// WEBASSEMBLY-NEXT: ret
}

f64x2 min_f64x2(f64x2 x, f64x2 y) {
return __builtin_wasm_min_f64x2(x, y);
// WEBASSEMBLY: call <2 x double> @llvm.minimum.v2f64(
// WEBASSEMBLY-SAME: <2 x double> %x, <2 x double> %y)
// WEBASSEMBLY-NEXT: ret
}

f64x2 max_f64x2(f64x2 x, f64x2 y) {
return __builtin_wasm_max_f64x2(x, y);
// WEBASSEMBLY: call <2 x double> @llvm.maximum.v2f64(
// WEBASSEMBLY-SAME: <2 x double> %x, <2 x double> %y)
// WEBASSEMBLY-NEXT: ret
}

f32x4 sqrt_f32x4(f32x4 x) {
return __builtin_wasm_sqrt_f32x4(x);
// WEBASSEMBLY: call <4 x float> @llvm.sqrt.v4f32(<4 x float> %x)
Expand Down

0 comments on commit 4a3fc17

Please sign in to comment.