[clang] d43fcd0 - [WebAssembly] Add SIMD integer min/max builtins

Thomas Lively via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 6 14:30:54 PST 2020


Author: Thomas Lively
Date: 2020-03-06T14:28:52-08:00
New Revision: d43fcd0c0410d474a947cf51a3c67aa2e8d5a711

URL: https://github.com/llvm/llvm-project/commit/d43fcd0c0410d474a947cf51a3c67aa2e8d5a711
DIFF: https://github.com/llvm/llvm-project/commit/d43fcd0c0410d474a947cf51a3c67aa2e8d5a711.diff

LOG: [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

Added: 
    

Modified: 
    clang/include/clang/Basic/BuiltinsWebAssembly.def
    clang/lib/CodeGen/CGBuiltin.cpp
    clang/test/CodeGen/builtins-wasm.c

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/BuiltinsWebAssembly.def b/clang/include/clang/Basic/BuiltinsWebAssembly.def
index 0574af395f7b..b544e3b42137 100644
--- a/clang/include/clang/Basic/BuiltinsWebAssembly.def
+++ b/clang/include/clang/Basic/BuiltinsWebAssembly.def
@@ -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")
 

diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index fbb397ba3cfe..417b308d7a22 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -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));

diff  --git a/clang/test/CodeGen/builtins-wasm.c b/clang/test/CodeGen/builtins-wasm.c
index e8e5ba3fbe09..b27bf75248ca 100644
--- a/clang/test/CodeGen/builtins-wasm.c
+++ b/clang/test/CodeGen/builtins-wasm.c
@@ -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(


        


More information about the cfe-commits mailing list