[WebAssembly] Add SIMD integer min/max builtins

Summary:
Although SIMD integer min/max operations can be expressed using the ?:
operator in C++, that operator is disallowed for vectors in C. As a
workaround, this change introduces new WebAssembly-specific builtin
functions that lower to the desired vector icmp/select sequences.

Reviewers: aheejin, dschuff, kripken

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

Tags: #clang

Differential Revision: https://reviews.llvm.org/D75770
This commit is contained in:
Thomas Lively 2020-03-06 13:00:16 -08:00
parent 20dca52288
commit d43fcd0c04
3 changed files with 138 additions and 0 deletions

View File

@ -98,6 +98,19 @@ TARGET_BUILTIN(__builtin_wasm_sub_saturate_u_i8x16, "V16cV16cV16c", "nc", "simd1
TARGET_BUILTIN(__builtin_wasm_sub_saturate_s_i16x8, "V8sV8sV8s", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_sub_saturate_u_i16x8, "V8sV8sV8s", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_min_s_i8x16, "V16cV16cV16c", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_min_u_i8x16, "V16cV16cV16c", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_max_s_i8x16, "V16cV16cV16c", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_max_u_i8x16, "V16cV16cV16c", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_min_s_i16x8, "V8sV8sV8s", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_min_u_i16x8, "V8sV8sV8s", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_max_s_i16x8, "V8sV8sV8s", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_max_u_i16x8, "V8sV8sV8s", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_min_s_i32x4, "V4iV4iV4i", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_min_u_i32x4, "V4iV4iV4i", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_max_s_i32x4, "V4iV4iV4i", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_max_u_i32x4, "V4iV4iV4i", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_avgr_u_i8x16, "V16cV16cV16c", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_avgr_u_i16x8, "V8sV8sV8s", "nc", "simd128")

View File

@ -14900,6 +14900,47 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
Function *Callee = CGM.getIntrinsic(IntNo, ConvertType(E->getType()));
return Builder.CreateCall(Callee, {LHS, RHS});
}
case WebAssembly::BI__builtin_wasm_min_s_i8x16:
case WebAssembly::BI__builtin_wasm_min_u_i8x16:
case WebAssembly::BI__builtin_wasm_max_s_i8x16:
case WebAssembly::BI__builtin_wasm_max_u_i8x16:
case WebAssembly::BI__builtin_wasm_min_s_i16x8:
case WebAssembly::BI__builtin_wasm_min_u_i16x8:
case WebAssembly::BI__builtin_wasm_max_s_i16x8:
case WebAssembly::BI__builtin_wasm_max_u_i16x8:
case WebAssembly::BI__builtin_wasm_min_s_i32x4:
case WebAssembly::BI__builtin_wasm_min_u_i32x4:
case WebAssembly::BI__builtin_wasm_max_s_i32x4:
case WebAssembly::BI__builtin_wasm_max_u_i32x4: {
Value *LHS = EmitScalarExpr(E->getArg(0));
Value *RHS = EmitScalarExpr(E->getArg(1));
Value *ICmp;
switch (BuiltinID) {
case WebAssembly::BI__builtin_wasm_min_s_i8x16:
case WebAssembly::BI__builtin_wasm_min_s_i16x8:
case WebAssembly::BI__builtin_wasm_min_s_i32x4:
ICmp = Builder.CreateICmpSLT(LHS, RHS);
break;
case WebAssembly::BI__builtin_wasm_min_u_i8x16:
case WebAssembly::BI__builtin_wasm_min_u_i16x8:
case WebAssembly::BI__builtin_wasm_min_u_i32x4:
ICmp = Builder.CreateICmpULT(LHS, RHS);
break;
case WebAssembly::BI__builtin_wasm_max_s_i8x16:
case WebAssembly::BI__builtin_wasm_max_s_i16x8:
case WebAssembly::BI__builtin_wasm_max_s_i32x4:
ICmp = Builder.CreateICmpSGT(LHS, RHS);
break;
case WebAssembly::BI__builtin_wasm_max_u_i8x16:
case WebAssembly::BI__builtin_wasm_max_u_i16x8:
case WebAssembly::BI__builtin_wasm_max_u_i32x4:
ICmp = Builder.CreateICmpUGT(LHS, RHS);
break;
default:
llvm_unreachable("unexpected builtin ID");
}
return Builder.CreateSelect(ICmp, LHS, RHS);
}
case WebAssembly::BI__builtin_wasm_avgr_u_i8x16:
case WebAssembly::BI__builtin_wasm_avgr_u_i16x8: {
Value *LHS = EmitScalarExpr(E->getArg(0));

View File

@ -338,6 +338,90 @@ i8x16 sub_saturate_u_i8x16(i8x16 x, i8x16 y) {
// WEBASSEMBLY-NEXT: ret
}
i8x16 min_s_i8x16(i8x16 x, i8x16 y) {
return __builtin_wasm_min_s_i8x16(x, y);
// WEBASSEMBLY: %0 = icmp slt <16 x i8> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <16 x i1> %0, <16 x i8> %x, <16 x i8> %y
// WEBASSEMBLY-NEXT: ret <16 x i8> %1
}
i8x16 min_u_i8x16(i8x16 x, i8x16 y) {
return __builtin_wasm_min_u_i8x16(x, y);
// WEBASSEMBLY: %0 = icmp ult <16 x i8> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <16 x i1> %0, <16 x i8> %x, <16 x i8> %y
// WEBASSEMBLY-NEXT: ret <16 x i8> %1
}
i8x16 max_s_i8x16(i8x16 x, i8x16 y) {
return __builtin_wasm_max_s_i8x16(x, y);
// WEBASSEMBLY: %0 = icmp sgt <16 x i8> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <16 x i1> %0, <16 x i8> %x, <16 x i8> %y
// WEBASSEMBLY-NEXT: ret <16 x i8> %1
}
i8x16 max_u_i8x16(i8x16 x, i8x16 y) {
return __builtin_wasm_max_u_i8x16(x, y);
// WEBASSEMBLY: %0 = icmp ugt <16 x i8> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <16 x i1> %0, <16 x i8> %x, <16 x i8> %y
// WEBASSEMBLY-NEXT: ret <16 x i8> %1
}
i16x8 min_s_i16x8(i16x8 x, i16x8 y) {
return __builtin_wasm_min_s_i16x8(x, y);
// WEBASSEMBLY: %0 = icmp slt <8 x i16> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <8 x i1> %0, <8 x i16> %x, <8 x i16> %y
// WEBASSEMBLY-NEXT: ret <8 x i16> %1
}
i16x8 min_u_i16x8(i16x8 x, i16x8 y) {
return __builtin_wasm_min_u_i16x8(x, y);
// WEBASSEMBLY: %0 = icmp ult <8 x i16> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <8 x i1> %0, <8 x i16> %x, <8 x i16> %y
// WEBASSEMBLY-NEXT: ret <8 x i16> %1
}
i16x8 max_s_i16x8(i16x8 x, i16x8 y) {
return __builtin_wasm_max_s_i16x8(x, y);
// WEBASSEMBLY: %0 = icmp sgt <8 x i16> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <8 x i1> %0, <8 x i16> %x, <8 x i16> %y
// WEBASSEMBLY-NEXT: ret <8 x i16> %1
}
i16x8 max_u_i16x8(i16x8 x, i16x8 y) {
return __builtin_wasm_max_u_i16x8(x, y);
// WEBASSEMBLY: %0 = icmp ugt <8 x i16> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <8 x i1> %0, <8 x i16> %x, <8 x i16> %y
// WEBASSEMBLY-NEXT: ret <8 x i16> %1
}
i32x4 min_s_i32x4(i32x4 x, i32x4 y) {
return __builtin_wasm_min_s_i32x4(x, y);
// WEBASSEMBLY: %0 = icmp slt <4 x i32> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <4 x i1> %0, <4 x i32> %x, <4 x i32> %y
// WEBASSEMBLY-NEXT: ret <4 x i32> %1
}
i32x4 min_u_i32x4(i32x4 x, i32x4 y) {
return __builtin_wasm_min_u_i32x4(x, y);
// WEBASSEMBLY: %0 = icmp ult <4 x i32> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <4 x i1> %0, <4 x i32> %x, <4 x i32> %y
// WEBASSEMBLY-NEXT: ret <4 x i32> %1
}
i32x4 max_s_i32x4(i32x4 x, i32x4 y) {
return __builtin_wasm_max_s_i32x4(x, y);
// WEBASSEMBLY: %0 = icmp sgt <4 x i32> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <4 x i1> %0, <4 x i32> %x, <4 x i32> %y
// WEBASSEMBLY-NEXT: ret <4 x i32> %1
}
i32x4 max_u_i32x4(i32x4 x, i32x4 y) {
return __builtin_wasm_max_u_i32x4(x, y);
// WEBASSEMBLY: %0 = icmp ugt <4 x i32> %x, %y
// WEBASSEMBLY-NEXT: %1 = select <4 x i1> %0, <4 x i32> %x, <4 x i32> %y
// WEBASSEMBLY-NEXT: ret <4 x i32> %1
}
i16x8 sub_saturate_s_i16x8(i16x8 x, i16x8 y) {
return __builtin_wasm_sub_saturate_s_i16x8(x, y);
// WEBASSEMBLY: call <8 x i16> @llvm.wasm.sub.saturate.signed.v8i16(