Skip to content

[OPT] Search whole BB for convergence token. #112728

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 6 commits into from
Oct 30, 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
38 changes: 21 additions & 17 deletions llvm/lib/Transforms/Utils/InlineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,21 @@ namespace {
}
}
};

} // end anonymous namespace

static IntrinsicInst *getConvergenceEntry(BasicBlock &BB) {
auto *I = BB.getFirstNonPHI();
while (I) {
if (auto *IntrinsicCall = dyn_cast<ConvergenceControlInst>(I)) {
if (IntrinsicCall->isEntry()) {
return IntrinsicCall;
}
}
I = I->getNextNode();
}
return nullptr;
}

/// Get or create a target for the branch from ResumeInsts.
BasicBlock *LandingPadInliningInfo::getInnerResumeDest() {
if (InnerResumeDest) return InnerResumeDest;
Expand Down Expand Up @@ -2496,15 +2508,10 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
// fully implements convergence control tokens, there is no mixing of
// controlled and uncontrolled convergent operations in the whole program.
if (CB.isConvergent()) {
auto *I = CalledFunc->getEntryBlock().getFirstNonPHI();
if (auto *IntrinsicCall = dyn_cast<IntrinsicInst>(I)) {
if (IntrinsicCall->getIntrinsicID() ==
Intrinsic::experimental_convergence_entry) {
if (!ConvergenceControlToken) {
return InlineResult::failure(
"convergent call needs convergencectrl operand");
}
}
if (!ConvergenceControlToken &&
getConvergenceEntry(CalledFunc->getEntryBlock())) {
return InlineResult::failure(
"convergent call needs convergencectrl operand");
}
}

Expand Down Expand Up @@ -2795,13 +2802,10 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
}

if (ConvergenceControlToken) {
auto *I = FirstNewBlock->getFirstNonPHI();
if (auto *IntrinsicCall = dyn_cast<IntrinsicInst>(I)) {
if (IntrinsicCall->getIntrinsicID() ==
Intrinsic::experimental_convergence_entry) {
IntrinsicCall->replaceAllUsesWith(ConvergenceControlToken);
IntrinsicCall->eraseFromParent();
}
IntrinsicInst *IntrinsicCall = getConvergenceEntry(*FirstNewBlock);
if (IntrinsicCall) {
IntrinsicCall->replaceAllUsesWith(ConvergenceControlToken);
IntrinsicCall->eraseFromParent();
}
}

Expand Down
24 changes: 24 additions & 0 deletions llvm/test/Transforms/Inline/convergence-inline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ define void @test_two_calls() convergent {
ret void
}

define i32 @token_not_first(i32 %x) convergent alwaysinline {
; CHECK-LABEL: @token_not_first(
; CHECK-NEXT: {{%.*}} = alloca ptr, align 8
; CHECK-NEXT: [[TOKEN:%.*]] = call token @llvm.experimental.convergence.entry()
; CHECK-NEXT: [[Y:%.*]] = call i32 @g(i32 [[X:%.*]]) [ "convergencectrl"(token [[TOKEN]]) ]
; CHECK-NEXT: ret i32 [[Y]]
;
%p = alloca ptr, align 8
%token = call token @llvm.experimental.convergence.entry()
%y = call i32 @g(i32 %x) [ "convergencectrl"(token %token) ]
ret i32 %y
}

define void @test_token_not_first() convergent {
; CHECK-LABEL: @test_token_not_first(
; CHECK-NEXT: [[TOKEN:%.*]] = call token @llvm.experimental.convergence.entry()
; CHECK-NEXT: {{%.*}} = call i32 @g(i32 23) [ "convergencectrl"(token [[TOKEN]]) ]
; CHECK-NEXT: ret void
;
%token = call token @llvm.experimental.convergence.entry()
%x = call i32 @token_not_first(i32 23) [ "convergencectrl"(token %token) ]
ret void
}

declare void @f(i32) convergent
declare i32 @g(i32) convergent

Expand Down
Loading