[PATCH] D76538: [WebAssembly] Add SIMD integer abs builtins
Thomas Lively via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 20 20:03:51 PDT 2020
tlively created this revision.
tlively added a reviewer: aheejin.
Herald added subscribers: cfe-commits, sunfish, jgravelle-google, sbc100, dschuff.
Herald added a project: clang.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D76538
Files:
clang/include/clang/Basic/BuiltinsWebAssembly.def
clang/lib/CodeGen/CGBuiltin.cpp
clang/test/CodeGen/builtins-wasm.c
Index: clang/test/CodeGen/builtins-wasm.c
===================================================================
--- clang/test/CodeGen/builtins-wasm.c
+++ clang/test/CodeGen/builtins-wasm.c
@@ -338,6 +338,30 @@
// 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
Index: clang/lib/CodeGen/CGBuiltin.cpp
===================================================================
--- clang/lib/CodeGen/CGBuiltin.cpp
+++ clang/lib/CodeGen/CGBuiltin.cpp
@@ -15050,6 +15050,15 @@
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:
Index: clang/include/clang/Basic/BuiltinsWebAssembly.def
===================================================================
--- clang/include/clang/Basic/BuiltinsWebAssembly.def
+++ clang/include/clang/Basic/BuiltinsWebAssembly.def
@@ -98,6 +98,10 @@
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")
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76538.251820.patch
Type: text/x-patch
Size: 3128 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200321/0e23f7f6/attachment.bin>
More information about the cfe-commits
mailing list