Skip to content

Commit aa2c7ed

Browse files
inbelicadam-yang
authored andcommitted
[HLSL][SPIRV][DXIL] Implement WaveActiveSum intrinsic
- add clang builtin to Builtins.td - link builtin in hlsl_intrinsics - add codegen for spirv intrinsic and two directx intrinsics to retain signedness information of the operands in CGBuiltin.cpp - add semantic analysis in SemaHLSL.cpp - add lowering of spirv intrinsic to spirv backend in SPIRVInstructionSelector.cpp - add lowering of directx intrinsics to WaveActiveOp dxil op in DXIL.td - add test cases to illustrate passes
1 parent a2fc276 commit aa2c7ed

File tree

14 files changed

+491
-0
lines changed

14 files changed

+491
-0
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4774,6 +4774,12 @@ def HLSLWaveActiveCountBits : LangBuiltin<"HLSL_LANG"> {
47744774
let Prototype = "unsigned int(bool)";
47754775
}
47764776

4777+
def HLSLWaveActiveSum : LangBuiltin<"HLSL_LANG"> {
4778+
let Spellings = ["__builtin_hlsl_wave_active_sum"];
4779+
let Attributes = [NoThrow, Const];
4780+
let Prototype = "void (...)";
4781+
}
4782+
47774783
def HLSLWaveGetLaneIndex : LangBuiltin<"HLSL_LANG"> {
47784784
let Spellings = ["__builtin_hlsl_wave_get_lane_index"];
47794785
let Attributes = [NoThrow, Const];

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9272,6 +9272,9 @@ def err_typecheck_expect_scalar_or_vector : Error<
92729272
"a vector of such type is required">;
92739273
def err_typecheck_expect_any_scalar_or_vector : Error<
92749274
"invalid operand of type %0 where a scalar or vector is required">;
9275+
def err_typecheck_expect_scalar_or_vector_not_type : Error<
9276+
"invalid operand of type %0 where %1 or "
9277+
"a vector of such type is not allowed">;
92759278
def err_typecheck_expect_flt_or_vector : Error<
92769279
"invalid operand of type %0 where floating, complex or "
92779280
"a vector of such types is required">;

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19117,6 +19117,23 @@ static Intrinsic::ID getFirstBitHighIntrinsic(CGHLSLRuntime &RT, QualType QT) {
1911719117
return RT.getFirstBitUHighIntrinsic();
1911819118
}
1911919119

19120+
// Return wave active sum that corresponds to the QT scalar type
19121+
static Intrinsic::ID getWaveActiveSumIntrinsic(llvm::Triple::ArchType Arch,
19122+
CGHLSLRuntime &RT, QualType QT) {
19123+
switch (Arch) {
19124+
case llvm::Triple::spirv:
19125+
return llvm::Intrinsic::spv_wave_active_sum;
19126+
case llvm::Triple::dxil: {
19127+
if (QT->isUnsignedIntegerType())
19128+
return llvm::Intrinsic::dx_wave_active_usum;
19129+
return llvm::Intrinsic::dx_wave_active_sum;
19130+
}
19131+
default:
19132+
llvm_unreachable("Intrinsic WaveActiveSum"
19133+
" not supported by target architecture");
19134+
}
19135+
}
19136+
1912019137
Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1912119138
const CallExpr *E,
1912219139
ReturnValueSlot ReturnValue) {
@@ -19426,6 +19443,23 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1942619443
Intrinsic::getOrInsertDeclaration(&CGM.getModule(), ID),
1942719444
ArrayRef{OpExpr});
1942819445
}
19446+
case Builtin::BI__builtin_hlsl_wave_active_sum: {
19447+
// Due to the use of variadic arguments, explicitly retreive argument
19448+
Value *OpExpr = EmitScalarExpr(E->getArg(0));
19449+
llvm::FunctionType *FT = llvm::FunctionType::get(
19450+
OpExpr->getType(), ArrayRef{OpExpr->getType()}, false);
19451+
Intrinsic::ID IID = getWaveActiveSumIntrinsic(
19452+
getTarget().getTriple().getArch(), CGM.getHLSLRuntime(),
19453+
E->getArg(0)->getType());
19454+
19455+
// Get overloaded name
19456+
std::string Name =
19457+
Intrinsic::getName(IID, ArrayRef{OpExpr->getType()}, &CGM.getModule());
19458+
return EmitRuntimeCall(CGM.CreateRuntimeFunction(FT, Name, {},
19459+
/*Local=*/false,
19460+
/*AssumeConvergent=*/true),
19461+
ArrayRef{OpExpr}, "hlsl.wave.active.sum");
19462+
}
1942919463
case Builtin::BI__builtin_hlsl_wave_get_lane_index: {
1943019464
// We don't define a SPIR-V intrinsic, instead it is a SPIR-V built-in
1943119465
// defined in SPIRVBuiltins.td. So instead we manually get the matching name

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,105 @@ __attribute__((convergent)) double3 WaveReadLaneAt(double3, int32_t);
23492349
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_read_lane_at)
23502350
__attribute__((convergent)) double4 WaveReadLaneAt(double4, int32_t);
23512351

2352+
//===----------------------------------------------------------------------===//
2353+
// WaveActiveSum builtins
2354+
//===----------------------------------------------------------------------===//
2355+
2356+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2357+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2358+
__attribute((convergent)) half WaveActiveSum(half);
2359+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2360+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2361+
__attribute((convergent)) half2 WaveActiveSum(half2);
2362+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2363+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2364+
__attribute((convergent)) half3 WaveActiveSum(half3);
2365+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2366+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2367+
__attribute((convergent)) half4 WaveActiveSum(half4);
2368+
2369+
#ifdef __HLSL_ENABLE_16_BIT
2370+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2371+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2372+
__attribute((convergent)) int16_t WaveActiveSum(int16_t);
2373+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2374+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2375+
__attribute((convergent)) int16_t2 WaveActiveSum(int16_t2);
2376+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2377+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2378+
__attribute((convergent)) int16_t3 WaveActiveSum(int16_t3);
2379+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2380+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2381+
__attribute((convergent)) int16_t4 WaveActiveSum(int16_t4);
2382+
2383+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2384+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2385+
__attribute((convergent)) uint16_t WaveActiveSum(uint16_t);
2386+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2387+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2388+
__attribute((convergent)) uint16_t2 WaveActiveSum(uint16_t2);
2389+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2390+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2391+
__attribute((convergent)) uint16_t3 WaveActiveSum(uint16_t3);
2392+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2393+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2394+
__attribute((convergent)) uint16_t4 WaveActiveSum(uint16_t4);
2395+
#endif
2396+
2397+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2398+
__attribute((convergent)) int WaveActiveSum(int);
2399+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2400+
__attribute((convergent)) int2 WaveActiveSum(int2);
2401+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2402+
__attribute((convergent)) int3 WaveActiveSum(int3);
2403+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2404+
__attribute((convergent)) int4 WaveActiveSum(int4);
2405+
2406+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2407+
__attribute((convergent)) uint WaveActiveSum(uint);
2408+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2409+
__attribute((convergent)) uint2 WaveActiveSum(uint2);
2410+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2411+
__attribute((convergent)) uint3 WaveActiveSum(uint3);
2412+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2413+
__attribute((convergent)) uint4 WaveActiveSum(uint4);
2414+
2415+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2416+
__attribute((convergent)) int64_t WaveActiveSum(int64_t);
2417+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2418+
__attribute((convergent)) int64_t2 WaveActiveSum(int64_t2);
2419+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2420+
__attribute((convergent)) int64_t3 WaveActiveSum(int64_t3);
2421+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2422+
__attribute((convergent)) int64_t4 WaveActiveSum(int64_t4);
2423+
2424+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2425+
__attribute((convergent)) uint64_t WaveActiveSum(uint64_t);
2426+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2427+
__attribute((convergent)) uint64_t2 WaveActiveSum(uint64_t2);
2428+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2429+
__attribute((convergent)) uint64_t3 WaveActiveSum(uint64_t3);
2430+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2431+
__attribute((convergent)) uint64_t4 WaveActiveSum(uint64_t4);
2432+
2433+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2434+
__attribute((convergent)) float WaveActiveSum(float);
2435+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2436+
__attribute((convergent)) float2 WaveActiveSum(float2);
2437+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2438+
__attribute((convergent)) float3 WaveActiveSum(float3);
2439+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2440+
__attribute((convergent)) float4 WaveActiveSum(float4);
2441+
2442+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2443+
__attribute((convergent)) double WaveActiveSum(double);
2444+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2445+
__attribute((convergent)) double2 WaveActiveSum(double2);
2446+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2447+
__attribute((convergent)) double3 WaveActiveSum(double3);
2448+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2449+
__attribute((convergent)) double4 WaveActiveSum(double4);
2450+
23522451
//===----------------------------------------------------------------------===//
23532452
// sign builtins
23542453
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,23 @@ static bool CheckAnyScalarOrVector(Sema *S, CallExpr *TheCall,
18431843
return false;
18441844
}
18451845

1846+
static bool CheckNotScalarType(Sema *S, CallExpr *TheCall, QualType Scalar,
1847+
unsigned ArgIndex) {
1848+
assert(TheCall->getNumArgs() >= ArgIndex);
1849+
QualType ArgType = TheCall->getArg(ArgIndex)->getType();
1850+
auto *VTy = ArgType->getAs<VectorType>();
1851+
// is the scalar or vector<scalar>
1852+
if (S->Context.hasSameUnqualifiedType(ArgType, Scalar) ||
1853+
(VTy &&
1854+
S->Context.hasSameUnqualifiedType(VTy->getElementType(), Scalar))) {
1855+
S->Diag(TheCall->getArg(0)->getBeginLoc(),
1856+
diag::err_typecheck_expect_scalar_or_vector_not_type)
1857+
<< ArgType << Scalar;
1858+
return true;
1859+
}
1860+
return false;
1861+
}
1862+
18461863
static bool CheckBoolSelect(Sema *S, CallExpr *TheCall) {
18471864
assert(TheCall->getNumArgs() == 3);
18481865
Expr *Arg1 = TheCall->getArg(1);
@@ -2151,6 +2168,20 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
21512168
TheCall->setType(ArgTyA);
21522169
break;
21532170
}
2171+
case Builtin::BI__builtin_hlsl_wave_active_sum: {
2172+
if (SemaRef.checkArgCount(TheCall, 1))
2173+
return true;
2174+
2175+
// Ensure input expr type is a scalar/vector and the same as the return type
2176+
if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
2177+
return true;
2178+
if (CheckNotScalarType(&SemaRef, TheCall, getASTContext().BoolTy, 0))
2179+
return true;
2180+
ExprResult Expr = TheCall->getArg(0);
2181+
QualType ArgTyExpr = Expr.get()->getType();
2182+
TheCall->setType(ArgTyExpr);
2183+
break;
2184+
}
21542185
// Note these are llvm builtins that we want to catch invalid intrinsic
21552186
// generation. Normal handling of these builitns will occur elsewhere.
21562187
case Builtin::BI__builtin_elementwise_bitreverse: {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -triple \
2+
// RUN: dxil-pc-shadermodel6.3-compute %s -emit-llvm -disable-llvm-passes -o - | \
3+
// RUN: FileCheck %s --check-prefixes=CHECK,CHECK-DXIL
4+
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -triple \
5+
// RUN: spirv-pc-vulkan-compute %s -emit-llvm -disable-llvm-passes -o - | \
6+
// RUN: FileCheck %s --check-prefixes=CHECK,CHECK-SPIRV
7+
8+
// Test basic lowering to runtime function call.
9+
10+
// CHECK-LABEL: test_int
11+
int test_int(int expr) {
12+
// CHECK-SPIRV: %[[RET:.*]] = call spir_func [[TY:.*]] @llvm.spv.wave.active.sum.i32([[TY]] %[[#]])
13+
// CHECK-DXIL: %[[RET:.*]] = call [[TY:.*]] @llvm.dx.wave.active.sum.i32([[TY]] %[[#]])
14+
// CHECK: ret [[TY]] %[[RET]]
15+
return WaveActiveSum(expr);
16+
}
17+
18+
// CHECK-DXIL: declare [[TY]] @llvm.dx.wave.active.sum.i32([[TY]]) #[[#attr:]]
19+
// CHECK-SPIRV: declare spir_func [[TY]] @llvm.spv.wave.active.sum.i32([[TY]]) #[[#attr:]]
20+
21+
// CHECK-LABEL: test_uint64_t
22+
uint64_t test_uint64_t(uint64_t expr) {
23+
// CHECK-SPIRV: %[[RET:.*]] = call spir_func [[TY:.*]] @llvm.spv.wave.active.sum.i64([[TY]] %[[#]])
24+
// CHECK-DXIL: %[[RET:.*]] = call [[TY:.*]] @llvm.dx.wave.active.usum.i64([[TY]] %[[#]])
25+
// CHECK: ret [[TY]] %[[RET]]
26+
return WaveActiveSum(expr);
27+
}
28+
29+
// CHECK-DXIL: declare [[TY]] @llvm.dx.wave.active.usum.i64([[TY]]) #[[#attr:]]
30+
// CHECK-SPIRV: declare spir_func [[TY]] @llvm.spv.wave.active.sum.i64([[TY]]) #[[#attr:]]
31+
32+
// Test basic lowering to runtime function call with array and float value.
33+
34+
// CHECK-LABEL: test_floatv4
35+
float4 test_floatv4(float4 expr) {
36+
// CHECK-SPIRV: %[[RET1:.*]] = call spir_func [[TY1:.*]] @llvm.spv.wave.active.sum.v4f32([[TY1]] %[[#]]
37+
// CHECK-DXIL: %[[RET1:.*]] = call [[TY1:.*]] @llvm.dx.wave.active.sum.v4f32([[TY1]] %[[#]])
38+
// CHECK: ret [[TY1]] %[[RET1]]
39+
return WaveActiveSum(expr);
40+
}
41+
42+
// CHECK-DXIL: declare [[TY1]] @llvm.dx.wave.active.sum.v4f32([[TY1]]) #[[#attr]]
43+
// CHECK-SPIRV: declare spir_func [[TY1]] @llvm.spv.wave.active.sum.v4f32([[TY1]]) #[[#attr]]
44+
45+
// CHECK: attributes #[[#attr]] = {{{.*}} convergent {{.*}}}
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_arg() {
4+
return __builtin_hlsl_wave_active_sum();
5+
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
6+
}
7+
8+
float2 test_too_many_arg(float2 p0) {
9+
return __builtin_hlsl_wave_active_sum(p0, p0);
10+
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
11+
}
12+
13+
bool test_expr_bool_type_check(bool p0) {
14+
return __builtin_hlsl_wave_active_sum(p0);
15+
// expected-error@-1 {{invalid operand of type 'bool' where 'bool' or a vector of such type is not allowed}}
16+
}
17+
18+
bool2 test_expr_bool_vec_type_check(bool2 p0) {
19+
return __builtin_hlsl_wave_active_sum(p0);
20+
// expected-error@-1 {{invalid operand of type 'bool2' (aka 'vector<bool, 2>') where 'bool' or a vector of such type is not allowed}}
21+
}
22+
23+
struct S { float f; };
24+
25+
S test_expr_struct_type_check(S p0) {
26+
return __builtin_hlsl_wave_active_sum(p0);
27+
// expected-error@-1 {{invalid operand of type 'S' where a scalar or vector is required}}
28+
}

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def int_dx_rsqrt : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]
100100
def int_dx_wave_active_countbits : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i1_ty], [IntrConvergent, IntrNoMem]>;
101101
def int_dx_wave_any : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_i1_ty], [IntrConvergent, IntrNoMem]>;
102102
def int_dx_wave_getlaneindex : DefaultAttrsIntrinsic<[llvm_i32_ty], [], [IntrConvergent, IntrNoMem]>;
103+
def int_dx_wave_active_sum : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
104+
def int_dx_wave_active_usum : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
103105
def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
104106
def int_dx_wave_readlane : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>, llvm_i32_ty], [IntrConvergent, IntrNoMem]>;
105107
def int_dx_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;

llvm/include/llvm/IR/IntrinsicsSPIRV.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ let TargetPrefix = "spv" in {
8787
def int_spv_dot4add_u8packed : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i32_ty, llvm_i32_ty, llvm_i32_ty], [IntrNoMem]>;
8888
def int_spv_wave_active_countbits : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i1_ty], [IntrConvergent, IntrNoMem]>;
8989
def int_spv_wave_any : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_i1_ty], [IntrConvergent, IntrNoMem]>;
90+
def int_spv_wave_active_sum : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
9091
def int_spv_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
9192
def int_spv_wave_readlane : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>, llvm_i32_ty], [IntrConvergent, IntrNoMem]>;
9293
def int_spv_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ defvar BarrierMode_GroupMemoryBarrierWithGroupSync = 9;
301301
defvar BarrierMode_AllMemoryBarrier = 10;
302302
defvar BarrierMode_AllMemoryBarrierWithGroupSync = 11;
303303

304+
defvar WaveOpKind_Sum = 0;
305+
defvar WaveOpKind_Product = 1;
306+
defvar WaveOpKind_Min = 2;
307+
defvar WaveOpKind_Max = 3;
308+
309+
defvar SignedOpKind_Signed = 0;
310+
defvar SignedOpKind_Unsigned = 1;
311+
304312
// Intrinsic arg selection
305313
class IntrinArgSelectType;
306314
def IntrinArgSelect_Index : IntrinArgSelectType;
@@ -932,6 +940,24 @@ def WaveActiveAnyTrue : DXILOp<113, waveAnyTrue> {
932940
let stages = [Stages<DXIL1_0, [all_stages]>];
933941
}
934942

943+
def WaveActiveOp : DXILOp<119, waveActiveOp> {
944+
let Doc = "returns the result of the operation across waves";
945+
let intrinsics = [
946+
IntrinSelect<
947+
int_dx_wave_active_sum,
948+
[ IntrinArgIndex<0>, IntrinArgI8<WaveOpKind_Sum>, IntrinArgI8<SignedOpKind_Signed> ]>,
949+
IntrinSelect<
950+
int_dx_wave_active_usum,
951+
[ IntrinArgIndex<0>, IntrinArgI8<WaveOpKind_Sum>, IntrinArgI8<SignedOpKind_Unsigned> ]>,
952+
];
953+
954+
let arguments = [OverloadTy, Int8Ty, Int8Ty];
955+
let result = OverloadTy;
956+
let overloads = [Overloads<DXIL1_0, [HalfTy, FloatTy, DoubleTy, Int16Ty, Int32Ty, Int64Ty]>];
957+
let stages = [Stages<DXIL1_0, [all_stages]>];
958+
let attributes = [Attributes<DXIL1_0, [ReadNone]>];
959+
}
960+
935961
def WaveIsFirstLane : DXILOp<110, waveIsFirstLane> {
936962
let Doc = "returns 1 for the first lane in the wave";
937963
let intrinsics = [ IntrinSelect<int_dx_wave_is_first_lane> ];

llvm/lib/Target/DirectX/DirectXTargetTransformInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ bool DirectXTTIImpl::isTargetIntrinsicTriviallyScalarizable(
4040
switch (ID) {
4141
case Intrinsic::dx_frac:
4242
case Intrinsic::dx_rsqrt:
43+
case Intrinsic::dx_wave_active_sum:
44+
case Intrinsic::dx_wave_active_usum:
4345
case Intrinsic::dx_wave_readlane:
4446
case Intrinsic::dx_asdouble:
4547
case Intrinsic::dx_splitdouble:

0 commit comments

Comments
 (0)