-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][HLSL][SPRI-V] Add convergence intrinsics #80680
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dc00816
[clang][HLSL][SPRI-V] Add convergence intrinsics
Keenuts c02927f
change implemented builtin
Keenuts b54a2aa
add tests
Keenuts 9929124
review feedback
Keenuts 2aeabdb
add convergent attribute
Keenuts 62a0736
clang-format
Keenuts 2dff826
remove convergent attr
Keenuts 89f6ee6
Revert "remove convergent attr"
Keenuts cffb7d8
style fix
Keenuts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1131,8 +1131,92 @@ struct BitTest { | |
|
||
static BitTest decodeBitTestBuiltin(unsigned BuiltinID); | ||
}; | ||
|
||
// Returns the first convergence entry/loop/anchor instruction found in |BB|. | ||
// std::nullptr otherwise. | ||
llvm::IntrinsicInst *getConvergenceToken(llvm::BasicBlock *BB) { | ||
for (auto &I : *BB) { | ||
Keenuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto *II = dyn_cast<llvm::IntrinsicInst>(&I); | ||
if (II && isConvergenceControlIntrinsic(II->getIntrinsicID())) | ||
return II; | ||
} | ||
return nullptr; | ||
} | ||
|
||
} // namespace | ||
|
||
llvm::CallBase * | ||
CodeGenFunction::addConvergenceControlToken(llvm::CallBase *Input, | ||
llvm::Value *ParentToken) { | ||
llvm::Value *bundleArgs[] = {ParentToken}; | ||
Keenuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
llvm::OperandBundleDef OB("convergencectrl", bundleArgs); | ||
auto Output = llvm::CallBase::addOperandBundle( | ||
Input, llvm::LLVMContext::OB_convergencectrl, OB, Input); | ||
Input->replaceAllUsesWith(Output); | ||
Input->eraseFromParent(); | ||
return Output; | ||
} | ||
|
||
llvm::IntrinsicInst * | ||
CodeGenFunction::emitConvergenceLoopToken(llvm::BasicBlock *BB, | ||
llvm::Value *ParentToken) { | ||
CGBuilderTy::InsertPoint IP = Builder.saveIP(); | ||
Builder.SetInsertPoint(&BB->front()); | ||
auto CB = Builder.CreateIntrinsic( | ||
llvm::Intrinsic::experimental_convergence_loop, {}, {}); | ||
Builder.restoreIP(IP); | ||
|
||
auto I = addConvergenceControlToken(CB, ParentToken); | ||
return cast<llvm::IntrinsicInst>(I); | ||
} | ||
|
||
llvm::IntrinsicInst * | ||
CodeGenFunction::getOrEmitConvergenceEntryToken(llvm::Function *F) { | ||
auto *BB = &F->getEntryBlock(); | ||
auto *token = getConvergenceToken(BB); | ||
if (token) | ||
return token; | ||
|
||
// Adding a convergence token requires the function to be marked as | ||
// convergent. | ||
F->setConvergent(); | ||
|
||
CGBuilderTy::InsertPoint IP = Builder.saveIP(); | ||
Builder.SetInsertPoint(&BB->front()); | ||
auto I = Builder.CreateIntrinsic( | ||
llvm::Intrinsic::experimental_convergence_entry, {}, {}); | ||
assert(isa<llvm::IntrinsicInst>(I)); | ||
Keenuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Builder.restoreIP(IP); | ||
|
||
return cast<llvm::IntrinsicInst>(I); | ||
} | ||
|
||
llvm::IntrinsicInst * | ||
CodeGenFunction::getOrEmitConvergenceLoopToken(const LoopInfo *LI) { | ||
assert(LI != nullptr); | ||
|
||
auto *token = getConvergenceToken(LI->getHeader()); | ||
if (token) | ||
return token; | ||
|
||
llvm::IntrinsicInst *PII = | ||
LI->getParent() | ||
? emitConvergenceLoopToken( | ||
LI->getHeader(), getOrEmitConvergenceLoopToken(LI->getParent())) | ||
: getOrEmitConvergenceEntryToken(LI->getHeader()->getParent()); | ||
|
||
return emitConvergenceLoopToken(LI->getHeader(), PII); | ||
} | ||
|
||
llvm::CallBase * | ||
CodeGenFunction::addControlledConvergenceToken(llvm::CallBase *Input) { | ||
llvm::Value *ParentToken = | ||
LoopStack.hasInfo() | ||
? getOrEmitConvergenceLoopToken(&LoopStack.getInfo()) | ||
: getOrEmitConvergenceEntryToken(Input->getFunction()); | ||
return addConvergenceControlToken(Input, ParentToken); | ||
} | ||
|
||
BitTest BitTest::decodeBitTestBuiltin(unsigned BuiltinID) { | ||
switch (BuiltinID) { | ||
// Main portable variants. | ||
|
@@ -5803,6 +5887,15 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, | |
{NDRange, Kernel, Block})); | ||
} | ||
|
||
case Builtin::BI__builtin_hlsl_wave_get_lane_index: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I missed this PR. Can you move this to |
||
auto *CI = EmitRuntimeCall(CGM.CreateRuntimeFunction( | ||
llvm::FunctionType::get(IntTy, {}, false), "__hlsl_wave_get_lane_index", | ||
{}, false, true)); | ||
if (getTarget().getTriple().isSPIRVLogical()) | ||
CI = dyn_cast<CallInst>(addControlledConvergenceToken(CI)); | ||
return RValue::get(CI); | ||
} | ||
|
||
case Builtin::BI__builtin_store_half: | ||
case Builtin::BI__builtin_store_halff: { | ||
Value *Val = EmitScalarExpr(E->getArg(0)); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
clang/test/CodeGenHLSL/builtins/wave_get_lane_index_do_while.hlsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \ | ||
// RUN: spirv-pc-vulkan-library %s -emit-llvm -disable-llvm-passes -o - | FileCheck %s | ||
|
||
// CHECK: define spir_func void @main() [[A0:#[0-9]+]] { | ||
void main() { | ||
// CHECK: entry: | ||
// CHECK: %[[CT_ENTRY:[0-9]+]] = call token @llvm.experimental.convergence.entry() | ||
// CHECK: br label %[[LABEL_WHILE_COND:.+]] | ||
int cond = 0; | ||
|
||
// CHECK: [[LABEL_WHILE_COND]]: | ||
// CHECK: %[[CT_LOOP:[0-9]+]] = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %[[CT_ENTRY]]) ] | ||
// CHECK: br label %[[LABEL_WHILE_BODY:.+]] | ||
while (true) { | ||
|
||
// CHECK: [[LABEL_WHILE_BODY]]: | ||
// CHECK: br i1 {{%.+}}, label %[[LABEL_IF_THEN:.+]], label %[[LABEL_IF_END:.+]] | ||
|
||
// CHECK: [[LABEL_IF_THEN]]: | ||
// CHECK: call i32 @__hlsl_wave_get_lane_index() [ "convergencectrl"(token %[[CT_LOOP]]) ] | ||
// CHECK: br label %[[LABEL_WHILE_END:.+]] | ||
if (cond == 2) { | ||
uint index = WaveGetLaneIndex(); | ||
break; | ||
} | ||
|
||
// CHECK: [[LABEL_IF_END]]: | ||
// CHECK: br label %[[LABEL_WHILE_COND]] | ||
cond++; | ||
} | ||
|
||
// CHECK: [[LABEL_WHILE_END]]: | ||
// CHECK: ret void | ||
} | ||
|
||
// CHECK-DAG: declare i32 @__hlsl_wave_get_lane_index() [[A1:#[0-9]+]] | ||
|
||
// CHECK-DAG: attributes [[A0]] = {{{.*}}convergent{{.*}}} | ||
// CHECK-DAG: attributes [[A1]] = {{{.*}}convergent{{.*}}} | ||
|
14 changes: 14 additions & 0 deletions
14
clang/test/CodeGenHLSL/builtins/wave_get_lane_index_simple.hlsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \ | ||
// RUN: spirv-pc-vulkan-library %s -emit-llvm -disable-llvm-passes -o - | FileCheck %s | ||
|
||
// CHECK: define spir_func noundef i32 @_Z6test_1v() [[A0:#[0-9]+]] { | ||
// CHECK: %[[CI:[0-9]+]] = call token @llvm.experimental.convergence.entry() | ||
// CHECK: call i32 @__hlsl_wave_get_lane_index() [ "convergencectrl"(token %[[CI]]) ] | ||
uint test_1() { | ||
return WaveGetLaneIndex(); | ||
} | ||
|
||
// CHECK: declare i32 @__hlsl_wave_get_lane_index() [[A1:#[0-9]+]] | ||
|
||
// CHECK-DAG: attributes [[A0]] = { {{.*}}convergent{{.*}} } | ||
// CHECK-DAG: attributes [[A1]] = { {{.*}}convergent{{.*}} } |
21 changes: 21 additions & 0 deletions
21
clang/test/CodeGenHLSL/builtins/wave_get_lane_index_subcall.hlsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \ | ||
// RUN: spirv-pc-vulkan-library %s -emit-llvm -disable-llvm-passes -o - | FileCheck %s | ||
|
||
// CHECK: define spir_func noundef i32 @_Z6test_1v() [[A0:#[0-9]+]] { | ||
// CHECK: %[[C1:[0-9]+]] = call token @llvm.experimental.convergence.entry() | ||
// CHECK: call i32 @__hlsl_wave_get_lane_index() [ "convergencectrl"(token %[[C1]]) ] | ||
uint test_1() { | ||
return WaveGetLaneIndex(); | ||
} | ||
|
||
// CHECK-DAG: declare i32 @__hlsl_wave_get_lane_index() [[A1:#[0-9]+]] | ||
|
||
// CHECK: define spir_func noundef i32 @_Z6test_2v() [[A0]] { | ||
// CHECK: %[[C2:[0-9]+]] = call token @llvm.experimental.convergence.entry() | ||
// CHECK: call spir_func noundef i32 @_Z6test_1v() [ "convergencectrl"(token %[[C2]]) ] | ||
uint test_2() { | ||
return test_1(); | ||
} | ||
|
||
// CHECK-DAG: attributes [[A0]] = {{{.*}}convergent{{.*}}} | ||
// CHECK-DAG: attributes [[A1]] = {{{.*}}convergent{{.*}}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.