[clang] de6cd3e - [WebAssembly] Add SIMD integer abs builtins

Thomas Lively via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 21 00:27:24 PDT 2020


Author: Thomas Lively
Date: 2020-03-21T00:21:24-07:00
New Revision: de6cd3e836e10ca1b893459d38e7052b7513d55e

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

LOG: [WebAssembly] Add SIMD integer abs builtins

Summary:
Since the conditional operator cannot be used with vector conditions
in C, we need a builtin to be able to express this operation in C
source.

Reviewers: aheejin

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

Tags: #clang

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

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 86d17fc952ec..5fb7a603fe17 100644
--- a/clang/include/clang/Basic/BuiltinsWebAssembly.def
+++ b/clang/include/clang/Basic/BuiltinsWebAssembly.def
@@ -98,6 +98,10 @@ 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_abs_i8x16, "V16cV16c", "nc", "simd128")
+TARGET_BUILTIN(__builtin_wasm_abs_i16x8, "V8sV8s", "nc", "simd128")
+TARGET_BUILTIN(__builtin_wasm_abs_i32x4, "V4iV4i", "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")

diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 413dbf95f1b9..8d86424c60a9 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -15050,6 +15050,15 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
     Function *Callee = CGM.getIntrinsic(IntNo, ConvertType(E->getType()));
     return Builder.CreateCall(Callee, {LHS, RHS});
   }
+  case WebAssembly::BI__builtin_wasm_abs_i8x16:
+  case WebAssembly::BI__builtin_wasm_abs_i16x8:
+  case WebAssembly::BI__builtin_wasm_abs_i32x4: {
+    Value *Vec = EmitScalarExpr(E->getArg(0));
+    Value *Neg = Builder.CreateNeg(Vec, "neg");
+    Constant *Zero = llvm::Constant::getNullValue(Vec->getType());
+    Value *ICmp = Builder.CreateICmpSLT(Vec, Zero, "abscond");
+    return Builder.CreateSelect(ICmp, Neg, Vec, "abs");
+  }
   case WebAssembly::BI__builtin_wasm_min_s_i8x16:
   case WebAssembly::BI__builtin_wasm_min_u_i8x16:
   case WebAssembly::BI__builtin_wasm_max_s_i8x16:

diff  --git a/clang/test/CodeGen/builtins-wasm.c b/clang/test/CodeGen/builtins-wasm.c
index 0d70ea601d79..8b497d95298c 100644
--- a/clang/test/CodeGen/builtins-wasm.c
+++ b/clang/test/CodeGen/builtins-wasm.c
@@ -338,6 +338,30 @@ i8x16 sub_saturate_u_i8x16(i8x16 x, i8x16 y) {
   // WEBASSEMBLY-NEXT: ret
 }
 
+i8x16 abs_i8x16(i8x16 v) {
+  return __builtin_wasm_abs_i8x16(v);
+  // WEBASSEMBLY: %neg = sub <16 x i8> zeroinitializer, %v
+  // WEBASSEMBLY: %abscond = icmp slt <16 x i8> %v, zeroinitializer
+  // WEBASSEMBLY: %abs = select <16 x i1> %abscond, <16 x i8> %neg, <16 x i8> %v
+  // WEBASSEMBLY: ret <16 x i8> %abs
+}
+
+i16x8 abs_i16x8(i16x8 v) {
+  return __builtin_wasm_abs_i16x8(v);
+  // WEBASSEMBLY: %neg = sub <8 x i16> zeroinitializer, %v
+  // WEBASSEMBLY: %abscond = icmp slt <8 x i16> %v, zeroinitializer
+  // WEBASSEMBLY: %abs = select <8 x i1> %abscond, <8 x i16> %neg, <8 x i16> %v
+  // WEBASSEMBLY: ret <8 x i16> %abs
+}
+
+i32x4 abs_i32x4(i32x4 v) {
+  return __builtin_wasm_abs_i32x4(v);
+  // WEBASSEMBLY: %neg = sub <4 x i32> zeroinitializer, %v
+  // WEBASSEMBLY: %abscond = icmp slt <4 x i32> %v, zeroinitializer
+  // WEBASSEMBLY: %abs = select <4 x i1> %abscond, <4 x i32> %neg, <4 x i32> %v
+  // WEBASSEMBLY: ret <4 x i32> %abs
+}
+
 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


        


More information about the cfe-commits mailing list