Skip to content

Commit

Permalink
[WebAssembly] SIMD bitmask intrinsics and builtin functions
Browse files Browse the repository at this point in the history
Summary:
These experimental new instructions are proposed in
WebAssembly/simd#201.

Reviewers: aheejin

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

Tags: #clang

Differential Revision: https://reviews.llvm.org/D76397
  • Loading branch information
tlively committed Mar 20, 2020
1 parent 678da7b commit a3f974f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/BuiltinsWebAssembly.def
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ TARGET_BUILTIN(__builtin_wasm_all_true_i16x8, "iV8s", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_all_true_i32x4, "iV4i", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_all_true_i64x2, "iV2LLi", "nc", "unimplemented-simd128")

TARGET_BUILTIN(__builtin_wasm_bitmask_i8x16, "iV16c", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_bitmask_i16x8, "iV8s", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_bitmask_i32x4, "iV4i", "nc", "simd128")

TARGET_BUILTIN(__builtin_wasm_abs_f32x4, "V4fV4f", "nc", "simd128")
TARGET_BUILTIN(__builtin_wasm_abs_f64x2, "V2dV2d", "nc", "simd128")

Expand Down
8 changes: 8 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15142,6 +15142,14 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
Function *Callee = CGM.getIntrinsic(IntNo, Vec->getType());
return Builder.CreateCall(Callee, {Vec});
}
case WebAssembly::BI__builtin_wasm_bitmask_i8x16:
case WebAssembly::BI__builtin_wasm_bitmask_i16x8:
case WebAssembly::BI__builtin_wasm_bitmask_i32x4: {
Value *Vec = EmitScalarExpr(E->getArg(0));
Function *Callee =
CGM.getIntrinsic(Intrinsic::wasm_bitmask, Vec->getType());
return Builder.CreateCall(Callee, {Vec});
}
case WebAssembly::BI__builtin_wasm_abs_f32x4:
case WebAssembly::BI__builtin_wasm_abs_f64x2: {
Value *Vec = EmitScalarExpr(E->getArg(0));
Expand Down
18 changes: 18 additions & 0 deletions clang/test/CodeGen/builtins-wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,24 @@ int all_true_i64x2(i64x2 x) {
// WEBASSEMBLY: ret
}

int bitmask_i8x16(i8x16 x) {
return __builtin_wasm_bitmask_i8x16(x);
// WEBASSEMBLY: call i32 @llvm.wasm.bitmask.v16i8(<16 x i8> %x)
// WEBASSEMBLY: ret
}

int bitmask_i16x8(i16x8 x) {
return __builtin_wasm_bitmask_i16x8(x);
// WEBASSEMBLY: call i32 @llvm.wasm.bitmask.v8i16(<8 x i16> %x)
// WEBASSEMBLY: ret
}

int bitmask_i32x4(i32x4 x) {
return __builtin_wasm_bitmask_i32x4(x);
// WEBASSEMBLY: call i32 @llvm.wasm.bitmask.v4i32(<4 x i32> %x)
// WEBASSEMBLY: ret
}

f32x4 abs_f32x4(f32x4 x) {
return __builtin_wasm_abs_f32x4(x);
// WEBASSEMBLY: call <4 x float> @llvm.fabs.v4f32(<4 x float> %x)
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/IR/IntrinsicsWebAssembly.td
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ def int_wasm_alltrue :
Intrinsic<[llvm_i32_ty],
[llvm_anyvector_ty],
[IntrNoMem, IntrSpeculatable]>;
def int_wasm_bitmask :
Intrinsic<[llvm_i32_ty],
[llvm_anyvector_ty],
[IntrNoMem, IntrSpeculatable]>;
def int_wasm_qfma :
Intrinsic<[llvm_anyvector_ty],
[LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>],
Expand Down
12 changes: 12 additions & 0 deletions llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,18 @@ def : Pat<(i32 (seteq
(i32 (!cast<NI>(reduction[1]#"_"#ty) (ty V128:$x)))>;
}

multiclass SIMDBitmask<ValueType vec_t, string vec, bits<32> simdop> {
defm _#vec_t : SIMD_I<(outs I32:$dst), (ins V128:$vec), (outs), (ins),
[(set I32:$dst,
(i32 (int_wasm_bitmask (vec_t V128:$vec)))
)],
vec#".bitmask\t$dst, $vec", vec#".bitmask", simdop>;
}

defm BITMASK : SIMDBitmask<v16i8, "i8x16", 228>;
defm BITMASK : SIMDBitmask<v8i16, "i16x8", 229>;
defm BITMASK : SIMDBitmask<v4i32, "i32x4", 230>;

//===----------------------------------------------------------------------===//
// Bit shifts
//===----------------------------------------------------------------------===//
Expand Down
30 changes: 30 additions & 0 deletions llvm/test/CodeGen/WebAssembly/simd-intrinsics.ll
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ define i32 @all_v16i8(<16 x i8> %x) {
ret i32 %a
}

; CHECK-LABEL: bitmask_v16i8:
; SIMD128-NEXT: .functype bitmask_v16i8 (v128) -> (i32){{$}}
; SIMD128-NEXT: i8x16.bitmask $push[[R:[0-9]+]]=, $0{{$}}
; SIMD128-NEXT: return $pop[[R]]{{$}}
declare i32 @llvm.wasm.bitmask.v16i8(<16 x i8>)
define i32 @bitmask_v16i8(<16 x i8> %x) {
%a = call i32 @llvm.wasm.bitmask.v16i8(<16 x i8> %x)
ret i32 %a
}

; CHECK-LABEL: bitselect_v16i8:
; SIMD128-NEXT: .functype bitselect_v16i8 (v128, v128, v128) -> (v128){{$}}
; SIMD128-NEXT: v128.bitselect $push[[R:[0-9]+]]=, $0, $1, $2{{$}}
Expand Down Expand Up @@ -208,6 +218,16 @@ define i32 @all_v8i16(<8 x i16> %x) {
ret i32 %a
}

; CHECK-LABEL: bitmask_v8i16:
; SIMD128-NEXT: .functype bitmask_v8i16 (v128) -> (i32){{$}}
; SIMD128-NEXT: i16x8.bitmask $push[[R:[0-9]+]]=, $0{{$}}
; SIMD128-NEXT: return $pop[[R]]{{$}}
declare i32 @llvm.wasm.bitmask.v8i16(<8 x i16>)
define i32 @bitmask_v8i16(<8 x i16> %x) {
%a = call i32 @llvm.wasm.bitmask.v8i16(<8 x i16> %x)
ret i32 %a
}

; CHECK-LABEL: bitselect_v8i16:
; SIMD128-NEXT: .functype bitselect_v8i16 (v128, v128, v128) -> (v128){{$}}
; SIMD128-NEXT: v128.bitselect $push[[R:[0-9]+]]=, $0, $1, $2{{$}}
Expand Down Expand Up @@ -317,6 +337,16 @@ define i32 @all_v4i32(<4 x i32> %x) {
ret i32 %a
}

; CHECK-LABEL: bitmask_v4i32:
; SIMD128-NEXT: .functype bitmask_v4i32 (v128) -> (i32){{$}}
; SIMD128-NEXT: i32x4.bitmask $push[[R:[0-9]+]]=, $0{{$}}
; SIMD128-NEXT: return $pop[[R]]{{$}}
declare i32 @llvm.wasm.bitmask.v4i32(<4 x i32>)
define i32 @bitmask_v4i32(<4 x i32> %x) {
%a = call i32 @llvm.wasm.bitmask.v4i32(<4 x i32> %x)
ret i32 %a
}

; CHECK-LABEL: bitselect_v4i32:
; SIMD128-NEXT: .functype bitselect_v4i32 (v128, v128, v128) -> (v128){{$}}
; SIMD128-NEXT: v128.bitselect $push[[R:[0-9]+]]=, $0, $1, $2{{$}}
Expand Down
9 changes: 9 additions & 0 deletions llvm/test/MC/WebAssembly/simd-encodings.s
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,13 @@ main:
# CHECK: i32x4.dot_i16x8_s # encoding: [0xfd,0xdb,0x01]
i32x4.dot_i16x8_s

# CHECK: i8x16.bitmask # encoding: [0xfd,0xe4,0x01]
i8x16.bitmask

# CHECK: i16x8.bitmask # encoding: [0xfd,0xe5,0x01]
i16x8.bitmask

# CHECK: i32x4.bitmask # encoding: [0xfd,0xe6,0x01]
i32x4.bitmask

end_function

0 comments on commit a3f974f

Please sign in to comment.