Skip to content

Commit 9a1ad54

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 b0f11df commit 9a1ad54

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
@@ -4793,6 +4793,12 @@ def HLSLWaveActiveCountBits : LangBuiltin<"HLSL_LANG"> {
47934793
let Prototype = "unsigned int(bool)";
47944794
}
47954795

4796+
def HLSLWaveActiveSum : LangBuiltin<"HLSL_LANG"> {
4797+
let Spellings = ["__builtin_hlsl_wave_active_sum"];
4798+
let Attributes = [NoThrow, Const];
4799+
let Prototype = "void (...)";
4800+
}
4801+
47964802
def HLSLWaveGetLaneIndex : LangBuiltin<"HLSL_LANG"> {
47974803
let Spellings = ["__builtin_hlsl_wave_get_lane_index"];
47984804
let Attributes = [NoThrow, Const];

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9287,6 +9287,9 @@ def err_typecheck_expect_scalar_or_vector : Error<
92879287
"a vector of such type is required">;
92889288
def err_typecheck_expect_any_scalar_or_vector : Error<
92899289
"invalid operand of type %0 where a scalar or vector is required">;
9290+
def err_typecheck_expect_scalar_or_vector_not_type : Error<
9291+
"invalid operand of type %0 where %1 or "
9292+
"a vector of such type is not allowed">;
92909293
def err_typecheck_expect_flt_or_vector : Error<
92919294
"invalid operand of type %0 where floating, complex or "
92929295
"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
@@ -19198,6 +19198,23 @@ static Intrinsic::ID getFirstBitHighIntrinsic(CGHLSLRuntime &RT, QualType QT) {
1919819198
return RT.getFirstBitUHighIntrinsic();
1919919199
}
1920019200

19201+
// Return wave active sum that corresponds to the QT scalar type
19202+
static Intrinsic::ID getWaveActiveSumIntrinsic(llvm::Triple::ArchType Arch,
19203+
CGHLSLRuntime &RT, QualType QT) {
19204+
switch (Arch) {
19205+
case llvm::Triple::spirv:
19206+
return llvm::Intrinsic::spv_wave_active_sum;
19207+
case llvm::Triple::dxil: {
19208+
if (QT->isUnsignedIntegerType())
19209+
return llvm::Intrinsic::dx_wave_active_usum;
19210+
return llvm::Intrinsic::dx_wave_active_sum;
19211+
}
19212+
default:
19213+
llvm_unreachable("Intrinsic WaveActiveSum"
19214+
" not supported by target architecture");
19215+
}
19216+
}
19217+
1920119218
Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1920219219
const CallExpr *E,
1920319220
ReturnValueSlot ReturnValue) {
@@ -19517,6 +19534,23 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1951719534
Intrinsic::getOrInsertDeclaration(&CGM.getModule(), ID),
1951819535
ArrayRef{OpExpr});
1951919536
}
19537+
case Builtin::BI__builtin_hlsl_wave_active_sum: {
19538+
// Due to the use of variadic arguments, explicitly retreive argument
19539+
Value *OpExpr = EmitScalarExpr(E->getArg(0));
19540+
llvm::FunctionType *FT = llvm::FunctionType::get(
19541+
OpExpr->getType(), ArrayRef{OpExpr->getType()}, false);
19542+
Intrinsic::ID IID = getWaveActiveSumIntrinsic(
19543+
getTarget().getTriple().getArch(), CGM.getHLSLRuntime(),
19544+
E->getArg(0)->getType());
19545+
19546+
// Get overloaded name
19547+
std::string Name =
19548+
Intrinsic::getName(IID, ArrayRef{OpExpr->getType()}, &CGM.getModule());
19549+
return EmitRuntimeCall(CGM.CreateRuntimeFunction(FT, Name, {},
19550+
/*Local=*/false,
19551+
/*AssumeConvergent=*/true),
19552+
ArrayRef{OpExpr}, "hlsl.wave.active.sum");
19553+
}
1952019554
case Builtin::BI__builtin_hlsl_wave_get_lane_index: {
1952119555
// We don't define a SPIR-V intrinsic, instead it is a SPIR-V built-in
1952219556
// 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
@@ -2358,6 +2358,105 @@ __attribute__((convergent)) double3 WaveReadLaneAt(double3, int32_t);
23582358
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_read_lane_at)
23592359
__attribute__((convergent)) double4 WaveReadLaneAt(double4, int32_t);
23602360

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

clang/lib/Sema/SemaHLSL.cpp

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

1856+
static bool CheckNotScalarType(Sema *S, CallExpr *TheCall, QualType Scalar,
1857+
unsigned ArgIndex) {
1858+
assert(TheCall->getNumArgs() >= ArgIndex);
1859+
QualType ArgType = TheCall->getArg(ArgIndex)->getType();
1860+
auto *VTy = ArgType->getAs<VectorType>();
1861+
// is the scalar or vector<scalar>
1862+
if (S->Context.hasSameUnqualifiedType(ArgType, Scalar) ||
1863+
(VTy &&
1864+
S->Context.hasSameUnqualifiedType(VTy->getElementType(), Scalar))) {
1865+
S->Diag(TheCall->getArg(0)->getBeginLoc(),
1866+
diag::err_typecheck_expect_scalar_or_vector_not_type)
1867+
<< ArgType << Scalar;
1868+
return true;
1869+
}
1870+
return false;
1871+
}
1872+
18561873
static bool CheckBoolSelect(Sema *S, CallExpr *TheCall) {
18571874
assert(TheCall->getNumArgs() == 3);
18581875
Expr *Arg1 = TheCall->getArg(1);
@@ -2161,6 +2178,20 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
21612178
TheCall->setType(ArgTyA);
21622179
break;
21632180
}
2181+
case Builtin::BI__builtin_hlsl_wave_active_sum: {
2182+
if (SemaRef.checkArgCount(TheCall, 1))
2183+
return true;
2184+
2185+
// Ensure input expr type is a scalar/vector and the same as the return type
2186+
if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
2187+
return true;
2188+
if (CheckNotScalarType(&SemaRef, TheCall, getASTContext().BoolTy, 0))
2189+
return true;
2190+
ExprResult Expr = TheCall->getArg(0);
2191+
QualType ArgTyExpr = Expr.get()->getType();
2192+
TheCall->setType(ArgTyExpr);
2193+
break;
2194+
}
21642195
// Note these are llvm builtins that we want to catch invalid intrinsic
21652196
// generation. Normal handling of these builitns will occur elsewhere.
21662197
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
@@ -98,6 +98,8 @@ def int_dx_wave_active_countbits : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i1
9898
def int_dx_wave_all : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_i1_ty], [IntrConvergent, IntrNoMem]>;
9999
def int_dx_wave_any : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_i1_ty], [IntrConvergent, IntrNoMem]>;
100100
def int_dx_wave_getlaneindex : DefaultAttrsIntrinsic<[llvm_i32_ty], [], [IntrConvergent, IntrNoMem]>;
101+
def int_dx_wave_active_sum : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
102+
def int_dx_wave_active_usum : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
101103
def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
102104
def int_dx_wave_readlane : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>, llvm_i32_ty], [IntrConvergent, IntrNoMem]>;
103105
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
@@ -91,6 +91,7 @@ let TargetPrefix = "spv" in {
9191
def int_spv_wave_active_countbits : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i1_ty], [IntrConvergent, IntrNoMem]>;
9292
def int_spv_wave_all : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_i1_ty], [IntrConvergent, IntrNoMem]>;
9393
def int_spv_wave_any : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_i1_ty], [IntrConvergent, IntrNoMem]>;
94+
def int_spv_wave_active_sum : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
9495
def int_spv_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
9596
def int_spv_wave_readlane : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>, llvm_i32_ty], [IntrConvergent, IntrNoMem]>;
9697
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;
@@ -940,6 +948,24 @@ def WaveActiveAnyTrue : DXILOp<113, waveAnyTrue> {
940948
let stages = [Stages<DXIL1_0, [all_stages]>];
941949
}
942950

951+
def WaveActiveOp : DXILOp<119, waveActiveOp> {
952+
let Doc = "returns the result of the operation across waves";
953+
let intrinsics = [
954+
IntrinSelect<
955+
int_dx_wave_active_sum,
956+
[ IntrinArgIndex<0>, IntrinArgI8<WaveOpKind_Sum>, IntrinArgI8<SignedOpKind_Signed> ]>,
957+
IntrinSelect<
958+
int_dx_wave_active_usum,
959+
[ IntrinArgIndex<0>, IntrinArgI8<WaveOpKind_Sum>, IntrinArgI8<SignedOpKind_Unsigned> ]>,
960+
];
961+
962+
let arguments = [OverloadTy, Int8Ty, Int8Ty];
963+
let result = OverloadTy;
964+
let overloads = [Overloads<DXIL1_0, [HalfTy, FloatTy, DoubleTy, Int16Ty, Int32Ty, Int64Ty]>];
965+
let stages = [Stages<DXIL1_0, [all_stages]>];
966+
let attributes = [Attributes<DXIL1_0, [ReadNone]>];
967+
}
968+
943969
def WaveIsFirstLane : DXILOp<110, waveIsFirstLane> {
944970
let Doc = "returns 1 for the first lane in the wave";
945971
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)