Skip to content

Commit 1992e30

Browse files
committed
[WebAssembly] Prototype i8x16.popcnt
As proposed at WebAssembly/simd#379. Use a target builtin and intrinsic rather than normal codegen patterns to make the instruction opt-in until it is merged to the proposal and stabilized in engines. Differential Revision: https://reviews.llvm.org/D89446
1 parent 122d92d commit 1992e30

File tree

7 files changed

+34
-0
lines changed

7 files changed

+34
-0
lines changed

clang/include/clang/Basic/BuiltinsWebAssembly.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ TARGET_BUILTIN(__builtin_wasm_max_u_i32x4, "V4UiV4UiV4Ui", "nc", "simd128")
114114
TARGET_BUILTIN(__builtin_wasm_avgr_u_i8x16, "V16UcV16UcV16Uc", "nc", "simd128")
115115
TARGET_BUILTIN(__builtin_wasm_avgr_u_i16x8, "V8UsV8UsV8Us", "nc", "simd128")
116116

117+
TARGET_BUILTIN(__builtin_wasm_popcnt_i8x16, "V16ScV16Sc", "nc", "simd128")
118+
117119
TARGET_BUILTIN(__builtin_wasm_q15mulr_saturate_s_i8x16, "V8sV8sV8s", "nc", "simd128")
118120

119121
TARGET_BUILTIN(__builtin_wasm_bitselect, "V4iV4iV4iV4i", "nc", "simd128")

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16606,6 +16606,11 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
1660616606
Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_dot);
1660716607
return Builder.CreateCall(Callee, {LHS, RHS});
1660816608
}
16609+
case WebAssembly::BI__builtin_wasm_popcnt_i8x16: {
16610+
Value *Vec = EmitScalarExpr(E->getArg(0));
16611+
Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_popcnt);
16612+
return Builder.CreateCall(Callee, {Vec});
16613+
}
1660916614
case WebAssembly::BI__builtin_wasm_any_true_i8x16:
1661016615
case WebAssembly::BI__builtin_wasm_any_true_i16x8:
1661116616
case WebAssembly::BI__builtin_wasm_any_true_i32x4:

clang/test/CodeGen/builtins-wasm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,12 @@ i32x4 bitselect(i32x4 x, i32x4 y, i32x4 c) {
538538
// WEBASSEMBLY-NEXT: ret
539539
}
540540

541+
i8x16 popcnt(i8x16 x) {
542+
return __builtin_wasm_popcnt_i8x16(x);
543+
// WEBASSEMBLY: call <16 x i8> @llvm.wasm.popcnt(<16 x i8> %x)
544+
// WEBASSEMBLY-NEXT: ret
545+
}
546+
541547
int any_true_i8x16(i8x16 x) {
542548
return __builtin_wasm_any_true_i8x16(x);
543549
// WEBASSEMBLY: call i32 @llvm.wasm.anytrue.v16i8(<16 x i8> %x)

llvm/include/llvm/IR/IntrinsicsWebAssembly.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ def int_wasm_store64_lane :
254254
[IntrWriteMem, IntrArgMemOnly],
255255
"", [SDNPMemOperand]>;
256256

257+
// TODO: Replace this intrinsic with normal ISel patterns once popcnt is merged
258+
// to the proposal.
259+
def int_wasm_popcnt :
260+
Intrinsic<[llvm_v16i8_ty], [llvm_v16i8_ty], [IntrNoMem, IntrSpeculatable]>;
261+
257262
//===----------------------------------------------------------------------===//
258263
// Thread-local storage intrinsics
259264
//===----------------------------------------------------------------------===//

llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,9 @@ defm ANYTRUE : SIMDReduce<int_wasm_anytrue, "any_true", 98>;
785785
// All lanes true: all_true
786786
defm ALLTRUE : SIMDReduce<int_wasm_alltrue, "all_true", 99>;
787787

788+
// Population count: popcnt
789+
defm POPCNT : SIMDUnary<v16i8, "i8x16", int_wasm_popcnt, "popcnt", 124>;
790+
788791
// Reductions already return 0 or 1, so and 1, setne 0, and seteq 1
789792
// can be folded out
790793
foreach reduction =

llvm/test/CodeGen/WebAssembly/simd-intrinsics.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ define <16 x i8> @avgr_u_v16i8(<16 x i8> %x, <16 x i8> %y) {
7575
ret <16 x i8> %a
7676
}
7777

78+
; CHECK-LABEL: popcnt_v16i8:
79+
; SIMD128-NEXT: .functype popcnt_v16i8 (v128) -> (v128){{$}}
80+
; SIMD128-NEXT: i8x16.popcnt $push[[R:[0-9]+]]=, $0{{$}}
81+
; SIMD128-NEXT: return $pop[[R]]{{$}}
82+
declare <16 x i8> @llvm.wasm.popcnt(<16 x i8>)
83+
define <16 x i8> @popcnt_v16i8(<16 x i8> %x) {
84+
%a = call <16 x i8> @llvm.wasm.popcnt(<16 x i8> %x)
85+
ret <16 x i8> %a
86+
}
87+
7888
; CHECK-LABEL: any_v16i8:
7989
; SIMD128-NEXT: .functype any_v16i8 (v128) -> (i32){{$}}
8090
; SIMD128-NEXT: i8x16.any_true $push[[R:[0-9]+]]=, $0{{$}}

llvm/test/MC/WebAssembly/simd-encodings.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ main:
367367
# CHECK: i8x16.avgr_u # encoding: [0xfd,0x7b]
368368
i8x16.avgr_u
369369

370+
# CHECK: i8x16.popcnt # encoding: [0xfd,0x7c]
371+
i8x16.popcnt
372+
370373
# CHECK: i16x8.abs # encoding: [0xfd,0x80,0x01]
371374
i16x8.abs
372375

0 commit comments

Comments
 (0)