Skip to content

Commit bf30b6c

Browse files
authored
[HLSL][SPIRV][DXIL] Implement dot4add_u8packed intrinsic (#115068)
```- create a clang built-in in Builtins.td - link dot4add_u8packed in hlsl_intrinsics.h - add lowering to spirv backend through expansion of operation as OpUDot is missing up to SPIRV 1.6 in SPIRVInstructionSelector.cpp - add lowering to spirv backend using OpUDot if applicable SPIRV version or SPV_KHR_integer_dot_product is enabled - add dot4add_u8packed intrinsic to IntrinsicsDirectX.td and mapping to DXIL.td op Dot4AddU8Packed - add tests for HLSL intrinsic lowering to dx/spv intrinsic in dot4add_u8packed.hlsl - add tests for sema checks in dot4add_u8packed-errors.hlsl - add test of spir-v lowering in SPIRV/dot4add_u8packed.ll - add test to dxil lowering in DirectX/dot4add_u8packed.ll ``` Resolves #99219
1 parent 7760ae7 commit bf30b6c

File tree

12 files changed

+163
-2
lines changed

12 files changed

+163
-2
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4798,6 +4798,12 @@ def HLSLDot4AddI8Packed : LangBuiltin<"HLSL_LANG"> {
47984798
let Prototype = "int(unsigned int, unsigned int, int)";
47994799
}
48004800

4801+
def HLSLDot4AddU8Packed : LangBuiltin<"HLSL_LANG"> {
4802+
let Spellings = ["__builtin_hlsl_dot4add_u8packed"];
4803+
let Attributes = [NoThrow, Const];
4804+
let Prototype = "unsigned int(unsigned int, unsigned int, unsigned int)";
4805+
}
4806+
48014807
def HLSLFirstBitHigh : LangBuiltin<"HLSL_LANG"> {
48024808
let Spellings = ["__builtin_hlsl_elementwise_firstbithigh"];
48034809
let Attributes = [NoThrow, Const];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18881,6 +18881,16 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1888118881
/*ReturnType=*/C->getType(), ID, ArrayRef<Value *>{A, B, C}, nullptr,
1888218882
"hlsl.dot4add.i8packed");
1888318883
}
18884+
case Builtin::BI__builtin_hlsl_dot4add_u8packed: {
18885+
Value *A = EmitScalarExpr(E->getArg(0));
18886+
Value *B = EmitScalarExpr(E->getArg(1));
18887+
Value *C = EmitScalarExpr(E->getArg(2));
18888+
18889+
Intrinsic::ID ID = CGM.getHLSLRuntime().getDot4AddU8PackedIntrinsic();
18890+
return Builder.CreateIntrinsic(
18891+
/*ReturnType=*/C->getType(), ID, ArrayRef<Value *>{A, B, C}, nullptr,
18892+
"hlsl.dot4add.u8packed");
18893+
}
1888418894
case Builtin::BI__builtin_hlsl_elementwise_firstbithigh: {
1888518895

1888618896
Value *X = EmitScalarExpr(E->getArg(0));

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class CGHLSLRuntime {
9090
GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)
9191
GENERATE_HLSL_INTRINSIC_FUNCTION(UDot, udot)
9292
GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddI8Packed, dot4add_i8packed)
93+
GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddU8Packed, dot4add_u8packed)
9394
GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
9495
GENERATE_HLSL_INTRINSIC_FUNCTION(WaveReadLaneAt, wave_readlane)
9596
GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitUHigh, firstbituhigh)

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,13 @@ uint64_t dot(uint64_t4, uint64_t4);
942942

943943
_HLSL_AVAILABILITY(shadermodel, 6.4)
944944
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_dot4add_i8packed)
945-
int dot4add_i8packed(unsigned int, unsigned int, int);
945+
int dot4add_i8packed(uint, uint, int);
946+
947+
/// \fn uint dot4add_u8packed(uint A, uint B, uint C)
948+
949+
_HLSL_AVAILABILITY(shadermodel, 6.4)
950+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_dot4add_u8packed)
951+
uint dot4add_u8packed(uint, uint, uint);
946952

947953
//===----------------------------------------------------------------------===//
948954
// exp builtins
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
// RUN: %clang_cc1 -finclude-default-header -triple \
3+
// RUN: dxil-pc-shadermodel6.3-compute %s -emit-llvm -disable-llvm-passes -o - | \
4+
// RUN: FileCheck %s -DTARGET=dx
5+
// RUN: %clang_cc1 -finclude-default-header -triple \
6+
// RUN: spirv-pc-vulkan-compute %s -emit-llvm -disable-llvm-passes -o - | \
7+
// RUN: FileCheck %s -DTARGET=spv
8+
9+
// Test basic lowering to runtime function call.
10+
11+
// CHECK-LABEL: define {{.*}}test
12+
uint test(uint a, uint b, uint c) {
13+
// CHECK: %[[RET:.*]] = call [[TY:i32]] @llvm.[[TARGET]].dot4add.u8packed([[TY]] %[[#]], [[TY]] %[[#]], [[TY]] %[[#]])
14+
// CHECK: ret [[TY]] %[[RET]]
15+
return dot4add_u8packed(a, b, c);
16+
}
17+
18+
// CHECK: declare [[TY]] @llvm.[[TARGET]].dot4add.u8packed([[TY]], [[TY]], [[TY]])
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -emit-llvm-only -disable-llvm-passes -verify
2+
3+
int test_too_few_arg0() {
4+
return __builtin_hlsl_dot4add_u8packed();
5+
// expected-error@-1 {{too few arguments to function call, expected 3, have 0}}
6+
}
7+
8+
int test_too_few_arg1(int p0) {
9+
return __builtin_hlsl_dot4add_u8packed(p0);
10+
// expected-error@-1 {{too few arguments to function call, expected 3, have 1}}
11+
}
12+
13+
int test_too_few_arg2(uint p0) {
14+
return __builtin_hlsl_dot4add_u8packed(p0, p0);
15+
// expected-error@-1 {{too few arguments to function call, expected 3, have 2}}
16+
}
17+
18+
int test_too_many_arg(uint p0) {
19+
return __builtin_hlsl_dot4add_u8packed(p0, p0, p0, p0);
20+
// expected-error@-1 {{too many arguments to function call, expected 3, have 4}}
21+
}
22+
23+
struct S { float f; };
24+
25+
int test_expr_struct_type_check(S p0, uint p1) {
26+
return __builtin_hlsl_dot4add_u8packed(p1, p1, p0);
27+
// expected-error@-1 {{no viable conversion from 'S' to 'unsigned int'}}
28+
}

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def int_dx_udot :
6969
DefaultAttrsIntrinsic<[LLVMVectorElementType<0>],
7070
[llvm_anyint_ty, LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
7171
[IntrNoMem, Commutative] >;
72-
def int_dx_dot4add_i8packed : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty], [IntrNoMem]>;
72+
def int_dx_dot4add_i8packed : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty], [IntrNoMem]>;
73+
def int_dx_dot4add_u8packed : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty], [IntrNoMem]>;
7374

7475
def int_dx_frac : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>], [IntrNoMem]>;
7576
def int_dx_degrees : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty], [IntrNoMem]>;

llvm/include/llvm/IR/IntrinsicsSPIRV.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ let TargetPrefix = "spv" in {
8484
[llvm_anyint_ty, LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
8585
[IntrNoMem, Commutative] >;
8686
def int_spv_dot4add_i8packed : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty], [IntrNoMem]>;
87+
def int_spv_dot4add_u8packed : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty], [IntrNoMem]>;
8788
def int_spv_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
8889
def int_spv_wave_readlane : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>, llvm_i32_ty], [IntrConvergent, IntrNoMem]>;
8990
def int_spv_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,16 @@ def Dot4AddI8Packed : DXILOp<163, dot4AddPacked> {
822822
let stages = [Stages<DXIL1_0, [all_stages]>];
823823
}
824824

825+
def Dot4AddU8Packed : DXILOp<164, dot4AddPacked> {
826+
let Doc = "unsigned dot product of 4 x i8 vectors packed into i32, with "
827+
"accumulate to i32";
828+
let LLVMIntrinsic = int_dx_dot4add_u8packed;
829+
let arguments = [Int32Ty, Int32Ty, Int32Ty];
830+
let result = Int32Ty;
831+
let attributes = [Attributes<DXIL1_0, [ReadNone]>];
832+
let stages = [Stages<DXIL1_0, [all_stages]>];
833+
}
834+
825835
def AnnotateHandle : DXILOp<216, annotateHandle> {
826836
let Doc = "annotate handle with resource properties";
827837
let arguments = [HandleTy, ResPropsTy];

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,11 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
26872687
STI.isAtLeastSPIRVVer(VersionTuple(1, 6)))
26882688
return selectDot4AddPacked<true>(ResVReg, ResType, I);
26892689
return selectDot4AddPackedExpansion<true>(ResVReg, ResType, I);
2690+
case Intrinsic::spv_dot4add_u8packed:
2691+
if (STI.canUseExtension(SPIRV::Extension::SPV_KHR_integer_dot_product) ||
2692+
STI.isAtLeastSPIRVVer(VersionTuple(1, 6)))
2693+
return selectDot4AddPacked<false>(ResVReg, ResType, I);
2694+
return selectDot4AddPackedExpansion<false>(ResVReg, ResType, I);
26902695
case Intrinsic::spv_all:
26912696
return selectAll(ResVReg, ResType, I);
26922697
case Intrinsic::spv_any:

0 commit comments

Comments
 (0)