Skip to content

Commit 935c84c

Browse files
committed
[WebAssembly] Add experimental SIMD dot product instruction
Summary: This instruction is not merged to the spec proposal, but we need it to be implemented in the toolchain to experiment with it. It is available only on an opt-in basis through a clang builtin. Defined in WebAssembly/simd#127. Depends on D69696. Reviewers: aheejin Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, cfe-commits, llvm-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D69697
1 parent 4592f70 commit 935c84c

File tree

7 files changed

+38
-0
lines changed

7 files changed

+38
-0
lines changed

clang/include/clang/Basic/BuiltinsWebAssembly.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ TARGET_BUILTIN(__builtin_wasm_max_f32x4, "V4fV4fV4f", "nc", "simd128")
132132
TARGET_BUILTIN(__builtin_wasm_min_f64x2, "V2dV2dV2d", "nc", "unimplemented-simd128")
133133
TARGET_BUILTIN(__builtin_wasm_max_f64x2, "V2dV2dV2d", "nc", "unimplemented-simd128")
134134

135+
TARGET_BUILTIN(__builtin_wasm_dot_s_i32x4_i16x8, "V4iV8sV8s", "nc", "simd128")
136+
135137
TARGET_BUILTIN(__builtin_wasm_sqrt_f32x4, "V4fV4f", "nc", "unimplemented-simd128")
136138
TARGET_BUILTIN(__builtin_wasm_sqrt_f64x2, "V2dV2d", "nc", "unimplemented-simd128")
137139

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14360,6 +14360,12 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
1436014360
Function *Callee = CGM.getIntrinsic(IntNo, ConvertType(E->getType()));
1436114361
return Builder.CreateCall(Callee, {LHS, RHS});
1436214362
}
14363+
case WebAssembly::BI__builtin_wasm_dot_s_i32x4_i16x8: {
14364+
Value *LHS = EmitScalarExpr(E->getArg(0));
14365+
Value *RHS = EmitScalarExpr(E->getArg(1));
14366+
Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_dot);
14367+
return Builder.CreateCall(Callee, {LHS, RHS});
14368+
}
1436314369
case WebAssembly::BI__builtin_wasm_any_true_i8x16:
1436414370
case WebAssembly::BI__builtin_wasm_any_true_i16x8:
1436514371
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
@@ -436,6 +436,12 @@ i32x4 max_u_i32x4(i32x4 x, i32x4 y) {
436436
// WEBASSEMBLY-NEXT: ret
437437
}
438438

439+
i32x4 dot_i16x8_s(i16x8 x, i16x8 y) {
440+
return __builtin_wasm_dot_s_i32x4_i16x8(x, y);
441+
// WEBASSEMBLY: call <4 x i32> @llvm.wasm.dot(<8 x i16> %x, <8 x i16> %y)
442+
// WEBASSEMBLY-NEXT: ret
443+
}
444+
439445
i32x4 bitselect(i32x4 x, i32x4 y, i32x4 c) {
440446
return __builtin_wasm_bitselect(x, y, c);
441447
// WEBASSEMBLY: call <4 x i32> @llvm.wasm.bitselect.v4i32(

llvm/include/llvm/IR/IntrinsicsWebAssembly.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ def int_wasm_qfms :
152152
Intrinsic<[llvm_anyvector_ty],
153153
[LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>],
154154
[IntrNoMem, IntrSpeculatable]>;
155+
def int_wasm_dot :
156+
Intrinsic<[llvm_v4i32_ty],
157+
[llvm_v8i16_ty, llvm_v8i16_ty],
158+
[IntrNoMem, IntrSpeculatable]>;
155159
def int_wasm_narrow_signed :
156160
Intrinsic<[llvm_anyvector_ty],
157161
[llvm_anyvector_ty, LLVMMatchType<1>],

llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,13 @@ defm MAX_S : SIMDBinaryIntNoI64x2<int_wasm_max_signed, "max_s", 96>;
738738
defm MAX_U : SIMDBinaryIntNoI64x2<int_wasm_max_unsigned, "max_u", 97>;
739739
} // isCommutable = 1
740740

741+
// Widening dot product: i32x4.dot_i16x8_s
742+
let isCommutable = 1 in
743+
defm DOT : SIMD_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs), (outs), (ins),
744+
[(set V128:$dst, (int_wasm_dot V128:$lhs, V128:$rhs))],
745+
"i32x4.dot_i16x8_s\t$dst, $lhs, $rhs", "i32x4.dot_i16x8_s",
746+
217>;
747+
741748
//===----------------------------------------------------------------------===//
742749
// Floating-point unary arithmetic
743750
//===----------------------------------------------------------------------===//

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,16 @@ define <4 x i32> @max_u_v4i32(<4 x i32> %x, <4 x i32> %y) {
387387
ret <4 x i32> %a
388388
}
389389

390+
; CHECK-LABEL: dot:
391+
; SIMD128-NEXT: .functype dot (v128, v128) -> (v128){{$}}
392+
; SIMD128-NEXT: i32x4.dot_i16x8_s $push[[R:[0-9]+]]=, $0, $1{{$}}
393+
; SIMD128-NEXT: return $pop[[R]]{{$}}
394+
declare <4 x i32> @llvm.wasm.dot(<8 x i16>, <8 x i16>)
395+
define <4 x i32> @dot(<8 x i16> %x, <8 x i16> %y) {
396+
%a = call <4 x i32> @llvm.wasm.dot(<8 x i16> %x, <8 x i16> %y)
397+
ret <4 x i32> %a
398+
}
399+
390400
; CHECK-LABEL: any_v4i32:
391401
; SIMD128-NEXT: .functype any_v4i32 (v128) -> (i32){{$}}
392402
; SIMD128-NEXT: i32x4.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
@@ -571,4 +571,7 @@ main:
571571
# CHECK: v128.andnot # encoding: [0xfd,0xd8,0x01]
572572
v128.andnot
573573

574+
# CHECK: i32x4.dot_i16x8_s # encoding: [0xfd,0xd9,0x01]
575+
i32x4.dot_i16x8_s
576+
574577
end_function

0 commit comments

Comments
 (0)