Skip to content

Commit b18e409

Browse files
committed
[HLSL][SPIRV] Implement WaveActiveMax 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 test cases to illustrate passes Note that this defines the dx intrinsics but does not implement the DirectX lowering to DXIL. This will be implemented in a second pr when the dependent pr merges.
1 parent 6d13cc9 commit b18e409

File tree

11 files changed

+340
-0
lines changed

11 files changed

+340
-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 HLSLWaveActiveMax : LangBuiltin<"HLSL_LANG"> {
4753+
let Spellings = ["__builtin_hlsl_wave_active_max"];
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18638,6 +18638,25 @@ static Intrinsic::ID getDotProductIntrinsic(CGHLSLRuntime &RT, QualType QT) {
1863818638
return RT.getUDotIntrinsic();
1863918639
}
1864018640

18641+
// Return wave active max that corresponds to the QT scalar type
18642+
static Intrinsic::ID getWaveActiveMaxIntrinsic(llvm::Triple::ArchType Arch,
18643+
CGHLSLRuntime &RT, QualType QT) {
18644+
switch (Arch) {
18645+
case llvm::Triple::spirv:
18646+
if (QT->isUnsignedIntegerType())
18647+
return llvm::Intrinsic::spv_wave_active_umax;
18648+
return llvm::Intrinsic::spv_wave_active_max;
18649+
case llvm::Triple::dxil: {
18650+
if (QT->isUnsignedIntegerType())
18651+
return llvm::Intrinsic::dx_wave_active_umax;
18652+
return llvm::Intrinsic::dx_wave_active_max;
18653+
}
18654+
default:
18655+
llvm_unreachable("Intrinsic WaveActiveMax"
18656+
" not supported by target architecture");
18657+
}
18658+
}
18659+
1864118660
Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1864218661
const CallExpr *E,
1864318662
ReturnValueSlot ReturnValue) {
@@ -18883,6 +18902,23 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1888318902
/*ReturnType=*/Op0->getType(), CGM.getHLSLRuntime().getStepIntrinsic(),
1888418903
ArrayRef<Value *>{Op0, Op1}, nullptr, "hlsl.step");
1888518904
}
18905+
case Builtin::BI__builtin_hlsl_wave_active_max: {
18906+
// Due to the use of variadic arguments, explicitly retreive argument
18907+
Value *OpExpr = EmitScalarExpr(E->getArg(0));
18908+
llvm::FunctionType *FT = llvm::FunctionType::get(
18909+
OpExpr->getType(), ArrayRef{OpExpr->getType()}, false);
18910+
Intrinsic::ID IID = getWaveActiveMaxIntrinsic(
18911+
getTarget().getTriple().getArch(), CGM.getHLSLRuntime(),
18912+
E->getArg(0)->getType());
18913+
18914+
// Get overloaded name
18915+
std::string Name =
18916+
Intrinsic::getName(IID, ArrayRef{OpExpr->getType()}, &CGM.getModule());
18917+
return EmitRuntimeCall(CGM.CreateRuntimeFunction(FT, Name, {},
18918+
/*Local=*/false,
18919+
/*AssumeConvergent=*/true),
18920+
ArrayRef{OpExpr}, "hlsl.wave.active.max");
18921+
}
1888618922
case Builtin::BI__builtin_hlsl_wave_get_lane_index: {
1888718923
// We don't define a SPIR-V intrinsic, instead it is a SPIR-V built-in
1888818924
// 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+
// WaveActiveMax builtins
2182+
//===----------------------------------------------------------------------===//
2183+
2184+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2185+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2186+
__attribute((convergent)) half WaveActiveMax(half);
2187+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2188+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2189+
__attribute((convergent)) half2 WaveActiveMax(half2);
2190+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2191+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2192+
__attribute((convergent)) half3 WaveActiveMax(half3);
2193+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.0)
2194+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2195+
__attribute((convergent)) half4 WaveActiveMax(half4);
2196+
2197+
#ifdef __HLSL_ENABLE_16_BIT
2198+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2199+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2200+
__attribute((convergent)) int16_t WaveActiveMax(int16_t);
2201+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2202+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2203+
__attribute((convergent)) int16_t2 WaveActiveMax(int16_t2);
2204+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2205+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2206+
__attribute((convergent)) int16_t3 WaveActiveMax(int16_t3);
2207+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2208+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2209+
__attribute((convergent)) int16_t4 WaveActiveMax(int16_t4);
2210+
2211+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2212+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2213+
__attribute((convergent)) uint16_t WaveActiveMax(uint16_t);
2214+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2215+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2216+
__attribute((convergent)) uint16_t2 WaveActiveMax(uint16_t2);
2217+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2218+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2219+
__attribute((convergent)) uint16_t3 WaveActiveMax(uint16_t3);
2220+
_HLSL_AVAILABILITY(shadermodel, 6.0)
2221+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2222+
__attribute((convergent)) uint16_t4 WaveActiveMax(uint16_t4);
2223+
#endif
2224+
2225+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2226+
__attribute((convergent)) int WaveActiveMax(int);
2227+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2228+
__attribute((convergent)) int2 WaveActiveMax(int2);
2229+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2230+
__attribute((convergent)) int3 WaveActiveMax(int3);
2231+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2232+
__attribute((convergent)) int4 WaveActiveMax(int4);
2233+
2234+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2235+
__attribute((convergent)) uint WaveActiveMax(uint);
2236+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2237+
__attribute((convergent)) uint2 WaveActiveMax(uint2);
2238+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2239+
__attribute((convergent)) uint3 WaveActiveMax(uint3);
2240+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2241+
__attribute((convergent)) uint4 WaveActiveMax(uint4);
2242+
2243+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2244+
__attribute((convergent)) int64_t WaveActiveMax(int64_t);
2245+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2246+
__attribute((convergent)) int64_t2 WaveActiveMax(int64_t2);
2247+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2248+
__attribute((convergent)) int64_t3 WaveActiveMax(int64_t3);
2249+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2250+
__attribute((convergent)) int64_t4 WaveActiveMax(int64_t4);
2251+
2252+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2253+
__attribute((convergent)) uint64_t WaveActiveMax(uint64_t);
2254+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2255+
__attribute((convergent)) uint64_t2 WaveActiveMax(uint64_t2);
2256+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2257+
__attribute((convergent)) uint64_t3 WaveActiveMax(uint64_t3);
2258+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2259+
__attribute((convergent)) uint64_t4 WaveActiveMax(uint64_t4);
2260+
2261+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2262+
__attribute((convergent)) float WaveActiveMax(float);
2263+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2264+
__attribute((convergent)) float2 WaveActiveMax(float2);
2265+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2266+
__attribute((convergent)) float3 WaveActiveMax(float3);
2267+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2268+
__attribute((convergent)) float4 WaveActiveMax(float4);
2269+
2270+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2271+
__attribute((convergent)) double WaveActiveMax(double);
2272+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2273+
__attribute((convergent)) double2 WaveActiveMax(double2);
2274+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2275+
__attribute((convergent)) double3 WaveActiveMax(double3);
2276+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_active_max)
2277+
__attribute((convergent)) double4 WaveActiveMax(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_max: {
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.max.i32([[TY]] %[[#]]) [ "convergencectrl"(token %[[#entry_tok]]) ]
14+
// CHECK-DXIL: %[[RET:.*]] = call [[TY:.*]] @llvm.dx.wave.active.max.i32([[TY]] %[[#]])
15+
// CHECK: ret [[TY]] %[[RET]]
16+
return WaveActiveMax(expr);
17+
}
18+
19+
// CHECK-DXIL: declare [[TY]] @llvm.dx.wave.active.max.i32([[TY]]) #[[#attr:]]
20+
// CHECK-SPIRV: declare [[TY]] @llvm.spv.wave.active.max.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 [[TY1:.*]] @llvm.spv.wave.active.umax.i64([[TY1]] %[[#]]) [ "convergencectrl"(token %[[#entry_tok1]]) ]
26+
// CHECK-DXIL: %[[RET:.*]] = call [[TY1:.*]] @llvm.dx.wave.active.umax.i64([[TY1]] %[[#]])
27+
// CHECK: ret [[TY1]] %[[RET]]
28+
return WaveActiveMax(expr);
29+
}
30+
31+
// CHECK-DXIL: declare [[TY1]] @llvm.dx.wave.active.umax.i64([[TY1]]) #[[#attr:]]
32+
// CHECK-SPIRV: declare [[TY1]] @llvm.spv.wave.active.umax.i64([[TY1]]) #[[#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 [[TY2:.*]] @llvm.spv.wave.active.max.v4f32([[TY2]] %[[#]]) [ "convergencectrl"(token %[[#entry_tok2]]) ]
40+
// CHECK-DXIL: %[[RET1:.*]] = call [[TY2:.*]] @llvm.dx.wave.active.max.v4f32([[TY2]] %[[#]])
41+
// CHECK: ret [[TY2]] %[[RET1]]
42+
return WaveActiveMax(expr);
43+
}
44+
45+
// CHECK-DXIL: declare [[TY2]] @llvm.dx.wave.active.max.v4f32([[TY2]]) #[[#attr]]
46+
// CHECK-SPIRV: declare [[TY2]] @llvm.spv.wave.active.max.v4f32([[TY2]]) #[[#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_max();
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_max(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_max(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_max(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_max(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_max : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
89+
def int_dx_wave_active_umax : 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ 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_max : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
87+
def int_spv_wave_active_umax : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>], [IntrConvergent, IntrNoMem]>;
8688
def int_spv_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
8789
def int_spv_wave_readlane : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>, llvm_i32_ty], [IntrConvergent, IntrNoMem]>;
8890
def int_spv_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ class SPIRVInstructionSelector : public InstructionSelector {
164164
bool selectIntegerDot(Register ResVReg, const SPIRVType *ResType,
165165
MachineInstr &I) const;
166166

167+
bool selectWaveActiveMax(Register ResVReg, const SPIRVType *ResType,
168+
MachineInstr &I, bool IsSigned) const;
169+
167170
void renderImm32(MachineInstrBuilder &MIB, const MachineInstr &I,
168171
int OpIdx) const;
169172
void renderFImm64(MachineInstrBuilder &MIB, const MachineInstr &I,
@@ -1782,6 +1785,33 @@ bool SPIRVInstructionSelector::selectWaveReadLaneAt(Register ResVReg,
17821785
.addUse(I.getOperand(3).getReg());
17831786
}
17841787

1788+
bool SPIRVInstructionSelector::selectWaveActiveMax(Register ResVReg,
1789+
const SPIRVType *ResType,
1790+
MachineInstr &I,
1791+
bool IsSigned) const {
1792+
assert(I.getNumOperands() == 3);
1793+
assert(I.getOperand(2).isReg());
1794+
MachineBasicBlock &BB = *I.getParent();
1795+
Register InputRegister = I.getOperand(2).getReg();
1796+
SPIRVType *InputType = GR.getSPIRVTypeForVReg(InputRegister);
1797+
1798+
if (!InputType)
1799+
report_fatal_error("Input Type could not be determined.");
1800+
1801+
SPIRVType *IntTy = GR.getOrCreateSPIRVIntegerType(32, I, TII);
1802+
// Retreive the operation to use based on input type
1803+
bool IsFloatTy = GR.isScalarOrVectorOfType(InputRegister, SPIRV::OpTypeFloat);
1804+
auto Opcode =
1805+
IsFloatTy ? SPIRV::OpGroupNonUniformFMax : (IsSigned ?
1806+
SPIRV::OpGroupNonUniformSMax : SPIRV::OpGroupNonUniformUMax);
1807+
return BuildMI(BB, I, I.getDebugLoc(), TII.get(Opcode))
1808+
.addDef(ResVReg)
1809+
.addUse(GR.getSPIRVTypeID(ResType))
1810+
.addUse(GR.getOrCreateConstInt(SPIRV::Scope::Subgroup, I, IntTy, TII))
1811+
.addImm(SPIRV::GroupOperation::Reduce)
1812+
.addUse(I.getOperand(2).getReg());
1813+
}
1814+
17851815
bool SPIRVInstructionSelector::selectBitreverse(Register ResVReg,
17861816
const SPIRVType *ResType,
17871817
MachineInstr &I) const {
@@ -2559,6 +2589,10 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
25592589
} break;
25602590
case Intrinsic::spv_saturate:
25612591
return selectSaturate(ResVReg, ResType, I);
2592+
case Intrinsic::spv_wave_active_umax:
2593+
return selectWaveActiveMax(ResVReg, ResType, I, false);
2594+
case Intrinsic::spv_wave_active_max:
2595+
return selectWaveActiveMax(ResVReg, ResType, I, true);
25622596
case Intrinsic::spv_wave_is_first_lane: {
25632597
SPIRVType *IntTy = GR.getOrCreateSPIRVIntegerType(32, I, TII);
25642598
return BuildMI(BB, I, I.getDebugLoc(),

0 commit comments

Comments
 (0)