Skip to content

Commit 751f953

Browse files
committed
[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 a WaveActiveOp dxil op in DXILOpLowering.cpp - add test cases to illustrate passes
1 parent b68a561 commit 751f953

File tree

16 files changed

+476
-0
lines changed

16 files changed

+476
-0
lines changed

clang/include/clang/Basic/Builtins.td

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

4752+
def HLSLWaveActiveSum : LangBuiltin<"HLSL_LANG"> {
4753+
let Spellings = ["__builtin_hlsl_wave_active_sum"];
4754+
let Attributes = [NoThrow, Const];
4755+
let Prototype = "void (...)";
4756+
}
4757+
47524758
def HLSLWaveGetLaneIndex : LangBuiltin<"HLSL_LANG"> {
47534759
let Spellings = ["__builtin_hlsl_wave_get_lane_index"];
47544760
let Attributes = [NoThrow, Const];

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9232,6 +9232,9 @@ def err_typecheck_expect_scalar_or_vector : Error<
92329232
"a vector of such type is required">;
92339233
def err_typecheck_expect_any_scalar_or_vector : Error<
92349234
"invalid operand of type %0 where a scalar or vector is required">;
9235+
def err_typecheck_expect_scalar_or_vector_not_type : Error<
9236+
"invalid operand of type %0 where %1 or "
9237+
"a vector of such type is not allowed">;
92359238
def err_typecheck_expect_flt_or_vector : Error<
92369239
"invalid operand of type %0 where floating, complex or "
92379240
"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
@@ -18638,6 +18638,23 @@ static Intrinsic::ID getDotProductIntrinsic(CGHLSLRuntime &RT, QualType QT) {
1863818638
return RT.getUDotIntrinsic();
1863918639
}
1864018640

18641+
// Return wave active sum that corresponds to the QT scalar type
18642+
static Intrinsic::ID getWaveActiveSumIntrinsic(llvm::Triple::ArchType Arch,
18643+
CGHLSLRuntime &RT, QualType QT) {
18644+
switch (Arch) {
18645+
case llvm::Triple::spirv:
18646+
return llvm::Intrinsic::spv_wave_active_sum;
18647+
case llvm::Triple::dxil: {
18648+
if (QT->isUnsignedIntegerType())
18649+
return llvm::Intrinsic::dx_wave_active_usum;
18650+
return llvm::Intrinsic::dx_wave_active_sum;
18651+
}
18652+
default:
18653+
llvm_unreachable("Intrinsic WaveActiveSum"
18654+
" not supported by target architecture");
18655+
}
18656+
}
18657+
1864118658
Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1864218659
const CallExpr *E,
1864318660
ReturnValueSlot ReturnValue) {
@@ -18883,6 +18900,23 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1888318900
/*ReturnType=*/Op0->getType(), CGM.getHLSLRuntime().getStepIntrinsic(),
1888418901
ArrayRef<Value *>{Op0, Op1}, nullptr, "hlsl.step");
1888518902
}
18903+
case Builtin::BI__builtin_hlsl_wave_active_sum: {
18904+
// Due to the use of variadic arguments, explicitly retreive argument
18905+
Value *OpExpr = EmitScalarExpr(E->getArg(0));
18906+
llvm::FunctionType *FT = llvm::FunctionType::get(
18907+
OpExpr->getType(), ArrayRef{OpExpr->getType()}, false);
18908+
Intrinsic::ID IID = getWaveActiveSumIntrinsic(
18909+
getTarget().getTriple().getArch(), CGM.getHLSLRuntime(),
18910+
E->getArg(0)->getType());
18911+
18912+
// Get overloaded name
18913+
std::string Name =
18914+
Intrinsic::getName(IID, ArrayRef{OpExpr->getType()}, &CGM.getModule());
18915+
return EmitRuntimeCall(CGM.CreateRuntimeFunction(FT, Name, {},
18916+
/*Local=*/false,
18917+
/*AssumeConvergent=*/true),
18918+
ArrayRef{OpExpr}, "hlsl.wave.active.sum");
18919+
}
1888618920
case Builtin::BI__builtin_hlsl_wave_get_lane_index: {
1888718921
// We don't define a SPIR-V intrinsic, instead it is a SPIR-V built-in
1888818922
// 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
@@ -2177,6 +2177,105 @@ __attribute__((convergent)) double3 WaveReadLaneAt(double3, int32_t);
21772177
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_read_lane_at)
21782178
__attribute__((convergent)) double4 WaveReadLaneAt(double4, int32_t);
21792179

2180+
//===----------------------------------------------------------------------===//
2181+
// WaveActiveSum builtins
2182+
//===----------------------------------------------------------------------===//
2183+
2184+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2185+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2186+
__attribute((convergent)) half WaveActiveSum(half);
2187+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2188+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2189+
__attribute((convergent)) half2 WaveActiveSum(half2);
2190+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2191+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2192+
__attribute((convergent)) half3 WaveActiveSum(half3);
2193+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2194+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2195+
__attribute((convergent)) half4 WaveActiveSum(half4);
2196+
2197+
#ifdef __HLSL_ENABLE_16_BIT
2198+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2199+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2200+
__attribute((convergent)) int16_t WaveActiveSum(int16_t);
2201+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2202+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2203+
__attribute((convergent)) int16_t2 WaveActiveSum(int16_t2);
2204+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2205+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2206+
__attribute((convergent)) int16_t3 WaveActiveSum(int16_t3);
2207+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2208+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2209+
__attribute((convergent)) int16_t4 WaveActiveSum(int16_t4);
2210+
2211+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2212+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2213+
__attribute((convergent)) uint16_t WaveActiveSum(uint16_t);
2214+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2215+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2216+
__attribute((convergent)) uint16_t2 WaveActiveSum(uint16_t2);
2217+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2218+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2219+
__attribute((convergent)) uint16_t3 WaveActiveSum(uint16_t3);
2220+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2221+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2222+
__attribute((convergent)) uint16_t4 WaveActiveSum(uint16_t4);
2223+
#endif
2224+
2225+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2226+
__attribute((convergent)) int WaveActiveSum(int);
2227+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2228+
__attribute((convergent)) int2 WaveActiveSum(int2);
2229+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2230+
__attribute((convergent)) int3 WaveActiveSum(int3);
2231+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2232+
__attribute((convergent)) int4 WaveActiveSum(int4);
2233+
2234+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2235+
__attribute((convergent)) uint WaveActiveSum(uint);
2236+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2237+
__attribute((convergent)) uint2 WaveActiveSum(uint2);
2238+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2239+
__attribute((convergent)) uint3 WaveActiveSum(uint3);
2240+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2241+
__attribute((convergent)) uint4 WaveActiveSum(uint4);
2242+
2243+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2244+
__attribute((convergent)) int64_t WaveActiveSum(int64_t);
2245+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2246+
__attribute((convergent)) int64_t2 WaveActiveSum(int64_t2);
2247+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2248+
__attribute((convergent)) int64_t3 WaveActiveSum(int64_t3);
2249+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2250+
__attribute((convergent)) int64_t4 WaveActiveSum(int64_t4);
2251+
2252+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2253+
__attribute((convergent)) uint64_t WaveActiveSum(uint64_t);
2254+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2255+
__attribute((convergent)) uint64_t2 WaveActiveSum(uint64_t2);
2256+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2257+
__attribute((convergent)) uint64_t3 WaveActiveSum(uint64_t3);
2258+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2259+
__attribute((convergent)) uint64_t4 WaveActiveSum(uint64_t4);
2260+
2261+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2262+
__attribute((convergent)) float WaveActiveSum(float);
2263+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2264+
__attribute((convergent)) float2 WaveActiveSum(float2);
2265+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2266+
__attribute((convergent)) float3 WaveActiveSum(float3);
2267+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2268+
__attribute((convergent)) float4 WaveActiveSum(float4);
2269+
2270+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2271+
__attribute((convergent)) double WaveActiveSum(double);
2272+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2273+
__attribute((convergent)) double2 WaveActiveSum(double2);
2274+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2275+
__attribute((convergent)) double3 WaveActiveSum(double3);
2276+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_sum)
2277+
__attribute((convergent)) double4 WaveActiveSum(double4);
2278+
21802279
//===----------------------------------------------------------------------===//
21812280
// sign builtins
21822281
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaHLSL.cpp

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

1770+
static bool CheckNotScalarType(Sema *S, CallExpr *TheCall, QualType Scalar,
1771+
unsigned ArgIndex) {
1772+
assert(TheCall->getNumArgs() >= ArgIndex);
1773+
QualType ArgType = TheCall->getArg(ArgIndex)->getType();
1774+
auto *VTy = ArgType->getAs<VectorType>();
1775+
// is the scalar or vector<scalar>
1776+
if (S->Context.hasSameUnqualifiedType(ArgType, Scalar) ||
1777+
(VTy &&
1778+
S->Context.hasSameUnqualifiedType(VTy->getElementType(), Scalar))) {
1779+
S->Diag(TheCall->getArg(0)->getBeginLoc(),
1780+
diag::err_typecheck_expect_scalar_or_vector_not_type)
1781+
<< ArgType << Scalar;
1782+
return true;
1783+
}
1784+
return false;
1785+
}
1786+
17701787
static bool CheckBoolSelect(Sema *S, CallExpr *TheCall) {
17711788
assert(TheCall->getNumArgs() == 3);
17721789
Expr *Arg1 = TheCall->getArg(1);
@@ -2002,6 +2019,20 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
20022019
TheCall->setType(ArgTyA);
20032020
break;
20042021
}
2022+
case Builtin::BI__builtin_hlsl_wave_active_sum: {
2023+
if (SemaRef.checkArgCount(TheCall, 1))
2024+
return true;
2025+
2026+
// Ensure input expr type is a scalar/vector and the same as the return type
2027+
if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0))
2028+
return true;
2029+
if (CheckNotScalarType(&SemaRef, TheCall, getASTContext().BoolTy, 0))
2030+
return true;
2031+
ExprResult Expr = TheCall->getArg(0);
2032+
QualType ArgTyExpr = Expr.get()->getType();
2033+
TheCall->setType(ArgTyExpr);
2034+
break;
2035+
}
20052036
// Note these are llvm builtins that we want to catch invalid intrinsic
20062037
// generation. Normal handling of these builitns will occur elsewhere.
20072038
case Builtin::BI__builtin_elementwise_bitreverse: {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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: %[[#entry_tok:]] = call token @llvm.experimental.convergence.entry()
13+
// CHECK-SPIRV: %[[RET:.*]] = call [[TY:.*]] @llvm.spv.wave.active.sum.i32([[TY]] %[[#]]) [ "convergencectrl"(token %[[#entry_tok]]) ]
14+
// CHECK-DXIL: %[[RET:.*]] = call [[TY:.*]] @llvm.dx.wave.active.sum.i32([[TY]] %[[#]])
15+
// CHECK: ret [[TY]] %[[RET]]
16+
return WaveActiveSum(expr);
17+
}
18+
19+
// CHECK-DXIL: declare [[TY]] @llvm.dx.wave.active.sum.i32([[TY]]) #[[#attr:]]
20+
// CHECK-SPIRV: declare [[TY]] @llvm.spv.wave.active.sum.i32([[TY]]) #[[#attr:]]
21+
22+
// CHECK-LABEL: test_uint64_t
23+
uint64_t test_uint64_t(uint64_t expr) {
24+
// CHECK-SPIRV: %[[#entry_tok1:]] = call token @llvm.experimental.convergence.entry()
25+
// CHECK-SPIRV: %[[RET:.*]] = call [[TY:.*]] @llvm.spv.wave.active.sum.i64([[TY]] %[[#]]) [ "convergencectrl"(token %[[#entry_tok1]]) ]
26+
// CHECK-DXIL: %[[RET:.*]] = call [[TY:.*]] @llvm.dx.wave.active.usum.i64([[TY]] %[[#]])
27+
// CHECK: ret [[TY]] %[[RET]]
28+
return WaveActiveSum(expr);
29+
}
30+
31+
// CHECK-DXIL: declare [[TY]] @llvm.dx.wave.active.usum.i64([[TY]]) #[[#attr:]]
32+
// CHECK-SPIRV: declare [[TY]] @llvm.spv.wave.active.sum.i64([[TY]]) #[[#attr:]]
33+
34+
// Test basic lowering to runtime function call with array and float value.
35+
36+
// CHECK-LABEL: test_floatv4
37+
float4 test_floatv4(float4 expr) {
38+
// CHECK-SPIRV: %[[#entry_tok2:]] = call token @llvm.experimental.convergence.entry()
39+
// CHECK-SPIRV: %[[RET1:.*]] = call [[TY1:.*]] @llvm.spv.wave.active.sum.v4f32([[TY1]] %[[#]]) [ "convergencectrl"(token %[[#entry_tok2]]) ]
40+
// CHECK-DXIL: %[[RET1:.*]] = call [[TY1:.*]] @llvm.dx.wave.active.sum.v4f32([[TY1]] %[[#]])
41+
// CHECK: ret [[TY1]] %[[RET1]]
42+
return WaveActiveSum(expr);
43+
}
44+
45+
// CHECK-DXIL: declare [[TY1]] @llvm.dx.wave.active.sum.v4f32([[TY1]]) #[[#attr]]
46+
// CHECK-SPIRV: declare [[TY1]] @llvm.spv.wave.active.sum.v4f32([[TY1]]) #[[#attr]]
47+
48+
// 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
@@ -85,6 +85,8 @@ def int_dx_umad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLV
8585
def int_dx_normalize : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty], [IntrNoMem]>;
8686
def int_dx_rsqrt : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>], [IntrNoMem]>;
8787
def int_dx_wave_getlaneindex : DefaultAttrsIntrinsic<[llvm_i32_ty], [], [IntrConvergent, IntrNoMem]>;
88+
def int_dx_wave_active_sum : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
89+
def int_dx_wave_active_usum : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
8890
def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
8991
def int_dx_wave_readlane : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>, llvm_i32_ty], [IntrConvergent, IntrNoMem]>;
9092
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
@@ -83,6 +83,7 @@ let TargetPrefix = "spv" in {
8383
DefaultAttrsIntrinsic<[LLVMVectorElementType<0>],
8484
[llvm_anyint_ty, LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
8585
[IntrNoMem, Commutative] >;
86+
def int_spv_wave_active_sum : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
8687
def int_spv_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
8788
def int_spv_wave_readlane : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>, llvm_i32_ty], [IntrConvergent, IntrNoMem]>;
8889
def int_spv_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;

llvm/lib/Target/DirectX/DXILConstants.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ enum class OpParamType : unsigned {
3030
#include "DXILOperation.inc"
3131
};
3232

33+
enum class SignedOpKind : unsigned {
34+
Signed = 0,
35+
Unsigned = 1,
36+
};
37+
38+
enum class WaveOpKind : unsigned {
39+
Sum = 0,
40+
Product = 1,
41+
Min = 2,
42+
Max = 3,
43+
};
44+
3345
} // namespace dxil
3446
} // namespace llvm
3547

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ static bool isVectorArgExpansion(Function &F) {
4040
return false;
4141
}
4242

43+
template <WaveOpKind OpVal, SignedOpKind SOpVal>
44+
static SmallVector<Value *> getWaveActiveOpArgs(Function &F,
45+
IRBuilder<> &Builder) {
46+
SmallVector<Value *, 0> Args;
47+
IntegerType *IntTy = IntegerType::get(Builder.getContext(), 8);
48+
Constant *Op = ConstantInt::get(IntTy, (unsigned)OpVal);
49+
Constant *SOp = ConstantInt::get(IntTy, (unsigned)SOpVal);
50+
Args.push_back(Op);
51+
Args.push_back(SOp);
52+
return Args;
53+
}
54+
4355
static SmallVector<Value *> populateOperands(Value *Arg, IRBuilder<> &Builder) {
4456
SmallVector<Value *> ExtractedElements;
4557
auto *VecArg = dyn_cast<FixedVectorType>(Arg->getType());
@@ -496,6 +508,18 @@ class OpLowerer {
496508
case Intrinsic::dx_typedBufferStore:
497509
HasErrors |= lowerTypedBufferStore(F);
498510
break;
511+
case Intrinsic::dx_wave_active_sum:
512+
HasErrors |= replaceFunctionWithOp(
513+
F, dxil::OpCode::WaveActiveOp,
514+
getWaveActiveOpArgs<WaveOpKind::Sum, SignedOpKind::Signed>(
515+
F, OpBuilder.getIRB()));
516+
break;
517+
case Intrinsic::dx_wave_active_usum:
518+
HasErrors |= replaceFunctionWithOp(
519+
F, dxil::OpCode::WaveActiveOp,
520+
getWaveActiveOpArgs<WaveOpKind::Sum, SignedOpKind::Unsigned>(
521+
F, OpBuilder.getIRB()));
522+
break;
499523
}
500524
Updated = true;
501525
}

llvm/lib/Target/DirectX/DirectXTargetTransformInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ bool DirectXTTIImpl::isTargetIntrinsicTriviallyScalarizable(
2828
switch (ID) {
2929
case Intrinsic::dx_frac:
3030
case Intrinsic::dx_rsqrt:
31+
case Intrinsic::dx_wave_active_sum:
32+
case Intrinsic::dx_wave_active_usum:
3133
return true;
3234
default:
3335
return false;

0 commit comments

Comments
 (0)