Skip to content

[clang][HLSL] Add WaveIsFirstLane() intrinsic #103299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions clang/include/clang/Basic/Builtins.td
Original file line number Diff line number Diff line change
Expand Up @@ -4679,6 +4679,12 @@ def HLSLWaveGetLaneIndex : LangBuiltin<"HLSL_LANG"> {
let Prototype = "unsigned int()";
}

def HLSLWaveIsFirstLane : LangBuiltin<"HLSL_LANG"> {
let Spellings = ["__builtin_hlsl_wave_is_first_lane"];
let Attributes = [NoThrow, Const];
let Prototype = "bool()";
}

def HLSLClamp : LangBuiltin<"HLSL_LANG"> {
let Spellings = ["__builtin_hlsl_elementwise_clamp"];
let Attributes = [NoThrow, Const];
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18700,6 +18700,10 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
llvm::FunctionType::get(IntTy, {}, false), "__hlsl_wave_get_lane_index",
{}, false, true));
}
case Builtin::BI__builtin_hlsl_wave_is_first_lane: {
Intrinsic::ID ID = CGM.getHLSLRuntime().getWaveIsFirstLaneIntrinsic();
return EmitRuntimeCall(Intrinsic::getDeclaration(&CGM.getModule(), ID));
}
}
return nullptr;
}
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CGHLSLRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class CGHLSLRuntime {
GENERATE_HLSL_INTRINSIC_FUNCTION(FDot, fdot)
GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)
GENERATE_HLSL_INTRINSIC_FUNCTION(UDot, udot)
GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)

//===----------------------------------------------------------------------===//
// End of reserved area for HLSL intrinsic getters.
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Headers/hlsl/hlsl_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -1796,5 +1796,9 @@ _HLSL_AVAILABILITY(shadermodel, 6.0)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_get_lane_index)
__attribute__((convergent)) uint WaveGetLaneIndex();

_HLSL_AVAILABILITY(shadermodel, 6.0)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_is_first_lane)
__attribute__((convergent)) bool WaveIsFirstLane();

} // namespace hlsl
#endif //_HLSL_HLSL_INTRINSICS_H_
34 changes: 34 additions & 0 deletions clang/test/CodeGenHLSL/builtins/wave_is_first_lane.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
// RUN: spirv-pc-vulkan-compute %s -emit-llvm -disable-llvm-passes -o - | \
// RUN: FileCheck %s --check-prefixes=CHECK,CHECK-SPIRV
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
// RUN: dxil-pc-shadermodel6.3-compute %s -emit-llvm -disable-llvm-passes -o - | \
// RUN: FileCheck %s --check-prefixes=CHECK,CHECK-DXIL

[numthreads(1, 1, 1)]
void main() {
// CHECK-SPIRV: %[[#entry_tok:]] = call token @llvm.experimental.convergence.entry()

// CHECK-SPIRV: %[[#loop_tok:]] = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %[[#entry_tok]]) ]
while (true) {

// CHECK-DXIL: %[[#]] = call i1 @llvm.dx.wave.is.first.lane()
// CHECK-SPIRV: %[[#]] = call i1 @llvm.spv.wave.is.first.lane()
// CHECK-SPIRV-SAME: [ "convergencectrl"(token %[[#loop_tok]]) ]
if (WaveIsFirstLane()) {
break;
}
}

// CHECK-DXIL: %[[#]] = call i1 @llvm.dx.wave.is.first.lane()
// CHECK-SPIRV: %[[#]] = call i1 @llvm.spv.wave.is.first.lane()
// CHECK-SPIRV-SAME: [ "convergencectrl"(token %[[#entry_tok]]) ]
if (WaveIsFirstLane()) {
return;
}
}

// CHECK-DXIL: i1 @llvm.dx.wave.is.first.lane() #[[#attr:]]
// CHECK-SPIRV: i1 @llvm.spv.wave.is.first.lane() #[[#attr:]]

// CHECK: attributes #[[#attr]] = {{{.*}} convergent {{.*}}}
2 changes: 2 additions & 0 deletions llvm/include/llvm/IR/IntrinsicsDirectX.td
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ def int_dx_umad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLV
def int_dx_normalize : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty]>;
def int_dx_rcp : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;
def int_dx_rsqrt : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;

def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
}
1 change: 1 addition & 0 deletions llvm/include/llvm/IR/IntrinsicsSPIRV.td
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ let TargetPrefix = "spv" in {
DefaultAttrsIntrinsic<[LLVMVectorElementType<0>],
[llvm_anyint_ty, LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
[IntrNoMem, Commutative] >;
def int_spv_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
}
9 changes: 9 additions & 0 deletions llvm/lib/Target/DirectX/DXIL.td
Original file line number Diff line number Diff line change
Expand Up @@ -746,3 +746,12 @@ def CreateHandleFromBinding : DXILOp<218, createHandleFromBinding> {
let result = HandleTy;
let stages = [Stages<DXIL1_6, [all_stages]>];
}

def WaveIsFirstLane : DXILOp<110, waveIsFirstLane> {
let Doc = "returns 1 for the first lane in the wave";
let LLVMIntrinsic = int_dx_wave_is_first_lane;
let arguments = [];
let result = Int1Ty;
let stages = [Stages<DXIL1_0, [all_stages]>];
let attributes = [Attributes<DXIL1_0, [ReadNone]>];
}
8 changes: 8 additions & 0 deletions llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,14 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
} break;
case Intrinsic::spv_saturate:
return selectSaturate(ResVReg, ResType, I);
case Intrinsic::spv_wave_is_first_lane: {
SPIRVType *IntTy = GR.getOrCreateSPIRVIntegerType(32, I, TII);
return BuildMI(BB, I, I.getDebugLoc(),
TII.get(SPIRV::OpGroupNonUniformElect))
.addDef(ResVReg)
.addUse(GR.getSPIRVTypeID(ResType))
.addUse(GR.getOrCreateConstInt(3, I, IntTy, TII));
}
default: {
std::string DiagMsg;
raw_string_ostream OS(DiagMsg);
Expand Down
53 changes: 31 additions & 22 deletions llvm/lib/Target/SPIRV/SPIRVStripConvergentIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,40 @@ class SPIRVStripConvergentIntrinsics : public FunctionPass {
virtual bool runOnFunction(Function &F) override {
DenseSet<Instruction *> ToRemove;

// Is the instruction is a convergent intrinsic, add it to kill-list and
// returns true. Returns false otherwise.
auto CleanupIntrinsic = [&](IntrinsicInst *II) {
if (II->getIntrinsicID() != Intrinsic::experimental_convergence_entry &&
II->getIntrinsicID() != Intrinsic::experimental_convergence_loop &&
II->getIntrinsicID() != Intrinsic::experimental_convergence_anchor)
return false;

II->replaceAllUsesWith(UndefValue::get(II->getType()));
ToRemove.insert(II);
return true;
};

// Replace the given CallInst by a similar CallInst with no convergencectrl
// attribute.
auto CleanupCall = [&](CallInst *CI) {
auto OB = CI->getOperandBundle(LLVMContext::OB_convergencectrl);
if (!OB.has_value())
return;

auto *NewCall = CallBase::removeOperandBundle(
CI, LLVMContext::OB_convergencectrl, CI);
NewCall->copyMetadata(*CI);
CI->replaceAllUsesWith(NewCall);
ToRemove.insert(CI);
};

for (BasicBlock &BB : F) {
for (Instruction &I : BB) {
if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
if (II->getIntrinsicID() !=
Intrinsic::experimental_convergence_entry &&
II->getIntrinsicID() !=
Intrinsic::experimental_convergence_loop &&
II->getIntrinsicID() !=
Intrinsic::experimental_convergence_anchor) {
if (auto *II = dyn_cast<IntrinsicInst>(&I))
if (CleanupIntrinsic(II))
continue;
}

II->replaceAllUsesWith(UndefValue::get(II->getType()));
ToRemove.insert(II);
} else if (auto *CI = dyn_cast<CallInst>(&I)) {
auto OB = CI->getOperandBundle(LLVMContext::OB_convergencectrl);
if (!OB.has_value())
continue;

auto *NewCall = CallBase::removeOperandBundle(
CI, LLVMContext::OB_convergencectrl, CI);
NewCall->copyMetadata(*CI);
CI->replaceAllUsesWith(NewCall);
ToRemove.insert(CI);
}
if (auto *CI = dyn_cast<CallInst>(&I))
CleanupCall(CI);
}
}

Expand Down
13 changes: 13 additions & 0 deletions llvm/test/CodeGen/DirectX/wave_is_first_lane.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; RUN: opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-compute %s | FileCheck %s

define void @main() #0 {
entry:
; CHECK: call i1 @dx.op.waveIsFirstLane(i32 110)
%0 = call i1 @llvm.dx.wave.is.first.lane()
ret void
}

declare i1 @llvm.dx.wave.is.first.lane() #1

attributes #0 = { convergent norecurse "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { convergent nocallback nofree nosync nounwind willreturn }
27 changes: 27 additions & 0 deletions llvm/test/CodeGen/SPIRV/hlsl-intrinsics/WaveIsFirstLane.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; RUN: llc -O0 -mtriple=spirv-unknown-linux %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-vulkan-unknown %s -o - -filetype=obj | spirv-val %}

target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
target triple = "spirv-unknown-vulkan-compute"

; CHECK-DAG: %[[#uint:]] = OpTypeInt 32 0
; CHECK-DAG: %[[#uint_3:]] = OpConstant %[[#uint]] 3
; CHECK-DAG: %[[#bool:]] = OpTypeBool

define spir_func void @main() #0 {
entry:
%0 = call token @llvm.experimental.convergence.entry()
; CHECK: %[[#]] = OpGroupNonUniformElect %[[#bool]] %[[#uint_3]]
%1 = call i1 @llvm.spv.wave.is.first.lane() [ "convergencectrl"(token %0) ]
ret void
}

declare i32 @__hlsl_wave_get_lane_index() #1

attributes #0 = { convergent norecurse "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { convergent }

!llvm.module.flags = !{!0, !1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 4, !"dx.disable_optimizations", i32 1}
Loading