[llvm] [IPO] Prevent removal of some convergent attr (PR #134863)
Nathan Gauër via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 9 06:29:33 PDT 2025
https://github.com/Keenuts updated https://github.com/llvm/llvm-project/pull/134863
>From b7021ef1669dd9a156892526801400d92d90f58c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Mon, 7 Apr 2025 17:39:10 +0200
Subject: [PATCH 1/4] [IPO] Prevent removal of some convergent attr
When a callee is marked as `convergent`, some targets like HLSL/SPIR-V
add a convergent token to the call.
This is valid if both functions are marked as `convergent`.
ADCE/BDCE and other DCE passes were allowed to remove convergence
intrinsics when their token were unused.
This meant a leaf function could lose all its convergence intrinsics.
This would allow further optimization to remove the `convergent`
attribute from the callee.
Issue was the caller was not updated, and we now had a convergence token
attached to a call function calling a non-convergent function.
This commit limits the removal of the `convergent` attribute when only
users are calls without convergence intrinsics.
If any other use is found (either a call to a convergent, or something
else like taking the function address), the removal is prevented.
---
llvm/lib/Transforms/IPO/FunctionAttrs.cpp | 17 ++++++-
llvm/test/Transforms/ADCE/convergence.ll | 46 ++++++++++++++++++
llvm/test/Transforms/BDCE/convergence.ll | 47 +++++++++++++++++++
.../Transforms/FunctionAttrs/convergent.ll | 32 +++++++++++++
4 files changed, 141 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/Transforms/ADCE/convergence.ll
create mode 100644 llvm/test/Transforms/BDCE/convergence.ll
diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
index ef7989507c89f..104668ce2849e 100644
--- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -1864,6 +1864,19 @@ static bool InstrBreaksNonConvergent(Instruction &I,
!SCCNodes.contains(CB->getCalledFunction());
}
+static bool FunctionRequiresConvergence(const Function &F) {
+ for (auto &Use : F.uses()) {
+ CallBase *CB = dyn_cast<CallBase>(Use.getUser());
+ if (!CB)
+ return true;
+
+ if (CB->getConvergenceControlToken())
+ return true;
+ }
+
+ return false;
+}
+
/// Helper for NoUnwind inference predicate InstrBreaksAttribute.
static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
if (!I.mayThrow(/* IncludePhaseOneUnwind */ true))
@@ -1967,7 +1980,9 @@ static void inferConvergent(const SCCNodeSet &SCCNodes,
AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
Attribute::Convergent,
// Skip non-convergent functions.
- [](const Function &F) { return !F.isConvergent(); },
+ [](const Function &F) {
+ return !F.isConvergent() || FunctionRequiresConvergence(F);
+ },
// Instructions that break non-convergent assumption.
[SCCNodes](Instruction &I) {
return InstrBreaksNonConvergent(I, SCCNodes);
diff --git a/llvm/test/Transforms/ADCE/convergence.ll b/llvm/test/Transforms/ADCE/convergence.ll
new file mode 100644
index 0000000000000..0bc317e898bf3
--- /dev/null
+++ b/llvm/test/Transforms/ADCE/convergence.ll
@@ -0,0 +1,46 @@
+; RUN: opt %s -passes=adce -S | FileCheck %s
+
+; CHECK: Function Attrs: convergent
+; CHECK-NEXT: define i32 @foo(i32 %a) #0 {
+define i32 @foo(i32 %a) #0 {
+entry:
+; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
+ %0 = call token @llvm.experimental.convergence.entry()
+ ret i32 %a
+}
+
+; CHECK: Function Attrs: convergent
+; CHECK-NEXT: define void @bar() #0 {
+define void @bar() #0 {
+entry:
+; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
+ %0 = call token @llvm.experimental.convergence.anchor()
+ ret void
+}
+
+; CHECK: Function Attrs: convergent
+; CHECK-NEXT: define void @baz() #0 {
+define void @baz() #0 {
+entry:
+; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
+ %0 = call token @llvm.experimental.convergence.entry()
+ br label %header
+
+header:
+; CHECK-NOT: %1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
+ %1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
+ br i1 true, label %body, label %exit
+
+body:
+ br label %header
+
+exit:
+ ret void
+}
+
+declare token @llvm.experimental.convergence.entry() #1
+declare token @llvm.experimental.convergence.anchor() #1
+declare token @llvm.experimental.convergence.loop() #1
+
+attributes #0 = { convergent }
+attributes #1 = { convergent nocallback nofree nosync nounwind willreturn memory(none) }
diff --git a/llvm/test/Transforms/BDCE/convergence.ll b/llvm/test/Transforms/BDCE/convergence.ll
new file mode 100644
index 0000000000000..417d6cc809fb1
--- /dev/null
+++ b/llvm/test/Transforms/BDCE/convergence.ll
@@ -0,0 +1,47 @@
+; RUN: opt %s -passes=bdce -S | FileCheck %s
+
+; CHECK: Function Attrs: convergent
+; CHECK-NEXT: define i32 @foo(i32 %a) #0 {
+define i32 @foo(i32 %a) #0 {
+entry:
+; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
+ %0 = call token @llvm.experimental.convergence.entry()
+ ret i32 %a
+}
+
+; CHECK: Function Attrs: convergent
+; CHECK-NEXT: define void @bar() #0 {
+define void @bar() #0 {
+entry:
+; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
+ %0 = call token @llvm.experimental.convergence.anchor()
+ ret void
+}
+
+; CHECK: Function Attrs: convergent
+; CHECK-NEXT: define void @baz() #0 {
+define void @baz() #0 {
+entry:
+; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
+ %0 = call token @llvm.experimental.convergence.entry()
+ br label %header
+
+header:
+; CHECK-NOT: %1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
+ %1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
+ br i1 true, label %body, label %exit
+
+body:
+ br label %header
+
+exit:
+ ret void
+}
+
+declare token @llvm.experimental.convergence.entry() #1
+declare token @llvm.experimental.convergence.anchor() #1
+declare token @llvm.experimental.convergence.loop() #1
+
+attributes #0 = { convergent }
+attributes #1 = { convergent nocallback nofree nosync nounwind willreturn memory(none) }
+
diff --git a/llvm/test/Transforms/FunctionAttrs/convergent.ll b/llvm/test/Transforms/FunctionAttrs/convergent.ll
index fe8029d39d924..62e1183e58688 100644
--- a/llvm/test/Transforms/FunctionAttrs/convergent.ll
+++ b/llvm/test/Transforms/FunctionAttrs/convergent.ll
@@ -129,3 +129,35 @@ define i32 @noopt_friend() convergent {
%a = call i32 @noopt()
ret i32 0
}
+
+; A function which should normally be stripped of its convergent attribute,
+; but because it's used in a controlled convergence call, the attribute
+; remains.
+; This could be improved by propagating the non-convergence outside of the
+; function, but for the time being, we stop when we encounter this scenario.
+define i32 @leaf_noconvergent_used() convergent {
+; CHECK: Function Attrs: convergent mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+; CHECK-LABEL: define {{[^@]+}}@leaf_noconvergent_used
+; CHECK-SAME: () #[[ATTR7:[0-9]+]] {
+; CHECK-NEXT: ret i32 0
+;
+ ret i32 0
+}
+
+define i32 @nonleaf_convergent() convergent {
+; CHECK: Function Attrs: convergent mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+; CHECK-LABEL: define {{[^@]+}}@nonleaf_convergent
+; CHECK-SAME: () #[[ATTR7]] {
+; CHECK-NEXT: [[TMP1:%.*]] = call token @llvm.experimental.convergence.entry()
+; CHECK-NEXT: [[TMP2:%.*]] = call i32 @leaf_noconvergent_used() [ "convergencectrl"(token [[TMP1]]) ]
+; CHECK-NEXT: ret i32 0
+;
+ %1 = call token @llvm.experimental.convergence.entry()
+ %2 = call i32 @leaf_noconvergent_used() [ "convergencectrl"(token %1) ]
+ ret i32 0
+}
+
+
+declare token @llvm.experimental.convergence.entry() #1
+
+attributes #1 = { convergent nocallback nofree nosync nounwind willreturn memory(none) }
>From 73601b7af9ac69c74dfa210a766fdf0778e2d267 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Tue, 8 Apr 2025 17:56:28 +0200
Subject: [PATCH 2/4] replace vreg with named values
---
llvm/test/Transforms/ADCE/convergence.ll | 16 ++++++++--------
llvm/test/Transforms/BDCE/convergence.ll | 16 ++++++++--------
2 files changed, 16 insertions(+), 16 deletions(-)
diff --git a/llvm/test/Transforms/ADCE/convergence.ll b/llvm/test/Transforms/ADCE/convergence.ll
index 0bc317e898bf3..d4f1497aae497 100644
--- a/llvm/test/Transforms/ADCE/convergence.ll
+++ b/llvm/test/Transforms/ADCE/convergence.ll
@@ -4,8 +4,8 @@
; CHECK-NEXT: define i32 @foo(i32 %a) #0 {
define i32 @foo(i32 %a) #0 {
entry:
-; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
- %0 = call token @llvm.experimental.convergence.entry()
+; CHECK-NOT: %tk = call token @llvm.experimental.convergence.entry()
+ %tk = call token @llvm.experimental.convergence.entry()
ret i32 %a
}
@@ -13,8 +13,8 @@ entry:
; CHECK-NEXT: define void @bar() #0 {
define void @bar() #0 {
entry:
-; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
- %0 = call token @llvm.experimental.convergence.anchor()
+; CHECK-NOT: %tk = call token @llvm.experimental.convergence.entry()
+ %tk = call token @llvm.experimental.convergence.anchor()
ret void
}
@@ -22,13 +22,13 @@ entry:
; CHECK-NEXT: define void @baz() #0 {
define void @baz() #0 {
entry:
-; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
- %0 = call token @llvm.experimental.convergence.entry()
+; CHECK-NOT: %tk = call token @llvm.experimental.convergence.entry()
+ %tk0 = call token @llvm.experimental.convergence.entry()
br label %header
header:
-; CHECK-NOT: %1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
- %1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
+; CHECK-NOT: %tk1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tk0) ]
+ %tk1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tk0) ]
br i1 true, label %body, label %exit
body:
diff --git a/llvm/test/Transforms/BDCE/convergence.ll b/llvm/test/Transforms/BDCE/convergence.ll
index 417d6cc809fb1..043dc9f8d18dd 100644
--- a/llvm/test/Transforms/BDCE/convergence.ll
+++ b/llvm/test/Transforms/BDCE/convergence.ll
@@ -4,8 +4,8 @@
; CHECK-NEXT: define i32 @foo(i32 %a) #0 {
define i32 @foo(i32 %a) #0 {
entry:
-; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
- %0 = call token @llvm.experimental.convergence.entry()
+; CHECK-NOT: %tk0 = call token @llvm.experimental.convergence.entry()
+ %tk0 = call token @llvm.experimental.convergence.entry()
ret i32 %a
}
@@ -13,8 +13,8 @@ entry:
; CHECK-NEXT: define void @bar() #0 {
define void @bar() #0 {
entry:
-; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
- %0 = call token @llvm.experimental.convergence.anchor()
+; CHECK-NOT: %tk0 = call token @llvm.experimental.convergence.entry()
+ %tk0 = call token @llvm.experimental.convergence.anchor()
ret void
}
@@ -22,13 +22,13 @@ entry:
; CHECK-NEXT: define void @baz() #0 {
define void @baz() #0 {
entry:
-; CHECK-NOT: %0 = call token @llvm.experimental.convergence.entry()
- %0 = call token @llvm.experimental.convergence.entry()
+; CHECK-NOT: %tk0 = call token @llvm.experimental.convergence.entry()
+ %tk0 = call token @llvm.experimental.convergence.entry()
br label %header
header:
-; CHECK-NOT: %1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
- %1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
+; CHECK-NOT: %tk1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tk0) ]
+ %tk1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tk0) ]
br i1 true, label %body, label %exit
body:
>From 05f601dc918a3e3bd192b17e7fbe21c6199c84dc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Tue, 8 Apr 2025 17:59:00 +0200
Subject: [PATCH 3/4] generate full checks instead of check-not
---
llvm/test/Transforms/ADCE/convergence.ll | 29 ++++++++++++++++++------
llvm/test/Transforms/BDCE/convergence.ll | 29 ++++++++++++++++++------
2 files changed, 44 insertions(+), 14 deletions(-)
diff --git a/llvm/test/Transforms/ADCE/convergence.ll b/llvm/test/Transforms/ADCE/convergence.ll
index d4f1497aae497..819389c505372 100644
--- a/llvm/test/Transforms/ADCE/convergence.ll
+++ b/llvm/test/Transforms/ADCE/convergence.ll
@@ -1,33 +1,48 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt %s -passes=adce -S | FileCheck %s
; CHECK: Function Attrs: convergent
-; CHECK-NEXT: define i32 @foo(i32 %a) #0 {
define i32 @foo(i32 %a) #0 {
+; CHECK-LABEL: define i32 @foo(
+; CHECK-SAME: i32 [[A:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: ret i32 [[A]]
+;
entry:
-; CHECK-NOT: %tk = call token @llvm.experimental.convergence.entry()
%tk = call token @llvm.experimental.convergence.entry()
ret i32 %a
}
; CHECK: Function Attrs: convergent
-; CHECK-NEXT: define void @bar() #0 {
define void @bar() #0 {
+; CHECK-LABEL: define void @bar(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: ret void
+;
entry:
-; CHECK-NOT: %tk = call token @llvm.experimental.convergence.entry()
%tk = call token @llvm.experimental.convergence.anchor()
ret void
}
; CHECK: Function Attrs: convergent
-; CHECK-NEXT: define void @baz() #0 {
define void @baz() #0 {
+; CHECK-LABEL: define void @baz(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[HEADER:.*]]
+; CHECK: [[HEADER]]:
+; CHECK-NEXT: br i1 true, label %[[BODY:.*]], label %[[EXIT:.*]]
+; CHECK: [[BODY]]:
+; CHECK-NEXT: br label %[[HEADER]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
entry:
-; CHECK-NOT: %tk = call token @llvm.experimental.convergence.entry()
%tk0 = call token @llvm.experimental.convergence.entry()
br label %header
header:
-; CHECK-NOT: %tk1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tk0) ]
%tk1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tk0) ]
br i1 true, label %body, label %exit
diff --git a/llvm/test/Transforms/BDCE/convergence.ll b/llvm/test/Transforms/BDCE/convergence.ll
index 043dc9f8d18dd..51f279925b9f7 100644
--- a/llvm/test/Transforms/BDCE/convergence.ll
+++ b/llvm/test/Transforms/BDCE/convergence.ll
@@ -1,33 +1,48 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt %s -passes=bdce -S | FileCheck %s
; CHECK: Function Attrs: convergent
-; CHECK-NEXT: define i32 @foo(i32 %a) #0 {
define i32 @foo(i32 %a) #0 {
+; CHECK-LABEL: define i32 @foo(
+; CHECK-SAME: i32 [[A:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: ret i32 [[A]]
+;
entry:
-; CHECK-NOT: %tk0 = call token @llvm.experimental.convergence.entry()
%tk0 = call token @llvm.experimental.convergence.entry()
ret i32 %a
}
; CHECK: Function Attrs: convergent
-; CHECK-NEXT: define void @bar() #0 {
define void @bar() #0 {
+; CHECK-LABEL: define void @bar(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: ret void
+;
entry:
-; CHECK-NOT: %tk0 = call token @llvm.experimental.convergence.entry()
%tk0 = call token @llvm.experimental.convergence.anchor()
ret void
}
; CHECK: Function Attrs: convergent
-; CHECK-NEXT: define void @baz() #0 {
define void @baz() #0 {
+; CHECK-LABEL: define void @baz(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[HEADER:.*]]
+; CHECK: [[HEADER]]:
+; CHECK-NEXT: br i1 true, label %[[BODY:.*]], label %[[EXIT:.*]]
+; CHECK: [[BODY]]:
+; CHECK-NEXT: br label %[[HEADER]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
entry:
-; CHECK-NOT: %tk0 = call token @llvm.experimental.convergence.entry()
%tk0 = call token @llvm.experimental.convergence.entry()
br label %header
header:
-; CHECK-NOT: %tk1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tk0) ]
%tk1 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tk0) ]
br i1 true, label %body, label %exit
>From 486720af82c25410537c87eaf65cd5652918b67e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Wed, 9 Apr 2025 14:07:06 +0200
Subject: [PATCH 4/4] fix attributor
---
.../Transforms/IPO/AttributorAttributes.cpp | 21 ++
llvm/lib/Transforms/IPO/FunctionAttrs.cpp | 10 +-
llvm/test/Transforms/ADCE/convergence.ll | 31 +++
llvm/test/Transforms/Attributor/convergent.ll | 186 ++++++++++++++----
.../Transforms/FunctionAttrs/convergent.ll | 2 +
5 files changed, 211 insertions(+), 39 deletions(-)
diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
index a477c90bb4f45..a8f37448f0c38 100644
--- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
+++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
@@ -2902,6 +2902,22 @@ struct AANonConvergentImpl : public AANonConvergent {
}
};
+static bool FunctionCalledWithConvergenceToken(const Function *F) {
+ for (auto &Use : F->uses()) {
+ CallBase *CB = dyn_cast<CallBase>(Use.getUser());
+ if (!CB)
+ continue;
+
+ // We are not called, just used as an argument.
+ if (CB->getCalledFunction() != F)
+ continue;
+
+ if (CB->getConvergenceControlToken())
+ return true;
+ }
+ return false;
+}
+
struct AANonConvergentFunction final : AANonConvergentImpl {
AANonConvergentFunction(const IRPosition &IRP, Attributor &A)
: AANonConvergentImpl(IRP, A) {}
@@ -2929,6 +2945,11 @@ struct AANonConvergentFunction final : AANonConvergentImpl {
UsedAssumedInformation)) {
return indicatePessimisticFixpoint();
}
+
+ const Function *F = this->getIRPosition().getAssociatedFunction();
+ if (FunctionCalledWithConvergenceToken(F))
+ return indicatePessimisticFixpoint();
+
return ChangeStatus::UNCHANGED;
}
diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
index 104668ce2849e..dc980a4a997b5 100644
--- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -1864,12 +1864,16 @@ static bool InstrBreaksNonConvergent(Instruction &I,
!SCCNodes.contains(CB->getCalledFunction());
}
-static bool FunctionRequiresConvergence(const Function &F) {
- for (auto &Use : F.uses()) {
+static bool FunctionRequiresConvergence(const Function *F) {
+ for (auto &Use : F->uses()) {
CallBase *CB = dyn_cast<CallBase>(Use.getUser());
if (!CB)
return true;
+ // We are not called, just used as an argument.
+ if (CB->getCalledFunction() != F)
+ continue;
+
if (CB->getConvergenceControlToken())
return true;
}
@@ -1981,7 +1985,7 @@ static void inferConvergent(const SCCNodeSet &SCCNodes,
Attribute::Convergent,
// Skip non-convergent functions.
[](const Function &F) {
- return !F.isConvergent() || FunctionRequiresConvergence(F);
+ return !F.isConvergent() || FunctionRequiresConvergence(&F);
},
// Instructions that break non-convergent assumption.
[SCCNodes](Instruction &I) {
diff --git a/llvm/test/Transforms/ADCE/convergence.ll b/llvm/test/Transforms/ADCE/convergence.ll
index 819389c505372..5d7b9bd65c821 100644
--- a/llvm/test/Transforms/ADCE/convergence.ll
+++ b/llvm/test/Transforms/ADCE/convergence.ll
@@ -53,6 +53,37 @@ exit:
ret void
}
+define void @indirect_inner() #0 {
+; CHECK-LABEL: define void @indirect_inner(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: ret void
+;
+entry:
+ %tk0 = call token @llvm.experimental.convergence.entry()
+ ret void
+}
+
+define void @indirect() #0 {
+; CHECK-LABEL: define void @indirect(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TK0:%.*]] = call token @llvm.experimental.convergence.entry()
+; CHECK-NEXT: [[VAR:%.*]] = alloca ptr, align 8
+; CHECK-NEXT: store ptr @indirect_inner, ptr [[VAR]], align 8
+; CHECK-NEXT: [[PTR:%.*]] = load ptr, ptr [[VAR]], align 8
+; CHECK-NEXT: call void [[PTR]]() #[[ATTR0]] [ "convergencectrl"(token [[TK0]]) ]
+; CHECK-NEXT: ret void
+;
+entry:
+ %tk0 = call token @llvm.experimental.convergence.entry()
+ %var = alloca ptr, align 8
+ store ptr @indirect_inner, ptr %var, align 8
+ %ptr = load ptr, ptr %var, align 8
+ call void %ptr() convergent [ "convergencectrl"(token %tk0) ]
+ ret void
+}
+
declare token @llvm.experimental.convergence.entry() #1
declare token @llvm.experimental.convergence.anchor() #1
declare token @llvm.experimental.convergence.loop() #1
diff --git a/llvm/test/Transforms/Attributor/convergent.ll b/llvm/test/Transforms/Attributor/convergent.ll
index 702d0bb0a9e63..f4ae905f4bd14 100644
--- a/llvm/test/Transforms/Attributor/convergent.ll
+++ b/llvm/test/Transforms/Attributor/convergent.ll
@@ -1,11 +1,11 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals
-; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,TUNIT
-; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,CGSCC
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-attributes --check-globals all --version 5
+; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-annotate-decl-cs -S %s | FileCheck %s --check-prefixes=CHECK,TUNIT
+; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S %s | FileCheck %s --check-prefixes=CHECK,CGSCC
define i32 @defined() convergent {
; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
-; CHECK-LABEL: define {{[^@]+}}@defined
-; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
+; CHECK-LABEL: define noundef i32 @defined(
+; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: ret i32 1
;
ret i32 1
@@ -13,14 +13,14 @@ define i32 @defined() convergent {
define i32 @calls_defined() convergent {
; TUNIT: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
-; TUNIT-LABEL: define {{[^@]+}}@calls_defined
-; TUNIT-SAME: () #[[ATTR0]] {
+; TUNIT-LABEL: define noundef i32 @calls_defined(
+; TUNIT-SAME: ) #[[ATTR0]] {
; TUNIT-NEXT: ret i32 1
;
; CGSCC: Function Attrs: convergent mustprogress nofree nosync nounwind willreturn memory(none)
-; CGSCC-LABEL: define {{[^@]+}}@calls_defined
-; CGSCC-SAME: () #[[ATTR1:[0-9]+]] {
-; CGSCC-NEXT: [[A:%.*]] = call noundef i32 @defined() #[[ATTR6:[0-9]+]]
+; CGSCC-LABEL: define noundef i32 @calls_defined(
+; CGSCC-SAME: ) #[[ATTR1:[0-9]+]] {
+; CGSCC-NEXT: [[A:%.*]] = call noundef i32 @defined() #[[ATTR7:[0-9]+]]
; CGSCC-NEXT: ret i32 [[A]]
;
%a = call i32 @defined()
@@ -30,7 +30,7 @@ define i32 @calls_defined() convergent {
declare void @declared_non_convergent()
define void @calls_declared_non_convergent() convergent {
-; CHECK-LABEL: define {{[^@]+}}@calls_declared_non_convergent() {
+; CHECK-LABEL: define void @calls_declared_non_convergent() {
; CHECK-NEXT: call void @declared_non_convergent()
; CHECK-NEXT: ret void
;
@@ -43,14 +43,14 @@ declare i32 @declared_convergent() convergent
define i32 @calls_declared_convergent() convergent {
; TUNIT: Function Attrs: convergent
-; TUNIT-LABEL: define {{[^@]+}}@calls_declared_convergent
-; TUNIT-SAME: () #[[ATTR1:[0-9]+]] {
+; TUNIT-LABEL: define i32 @calls_declared_convergent(
+; TUNIT-SAME: ) #[[ATTR1:[0-9]+]] {
; TUNIT-NEXT: [[A:%.*]] = call i32 @declared_convergent()
; TUNIT-NEXT: ret i32 [[A]]
;
; CGSCC: Function Attrs: convergent
-; CGSCC-LABEL: define {{[^@]+}}@calls_declared_convergent
-; CGSCC-SAME: () #[[ATTR2:[0-9]+]] {
+; CGSCC-LABEL: define i32 @calls_declared_convergent(
+; CGSCC-SAME: ) #[[ATTR2:[0-9]+]] {
; CGSCC-NEXT: [[A:%.*]] = call i32 @declared_convergent()
; CGSCC-NEXT: ret i32 [[A]]
;
@@ -58,9 +58,120 @@ define i32 @calls_declared_convergent() convergent {
ret i32 %a
}
+; Function declared convergent. Body not provided to prevent inlining
+; of the functions below.
+declare i32 @direct_call_convergent_attribute_with_token_declared(i32 %a)
+
+; This function could be declared non-convergent as it only calls
+; non-convergent functions, but it is being called with a convergence token,
+; hence it is not allowed to drop the convergence attribute.
+define i32 @direct_call_convergent_attribute_with_token_inner(i32 %a) convergent {
+; TUNIT: Function Attrs: convergent
+; TUNIT-LABEL: define i32 @direct_call_convergent_attribute_with_token_inner(
+; TUNIT-SAME: i32 [[A:%.*]]) #[[ATTR1]] {
+; TUNIT-NEXT: [[RS:%.*]] = call i32 @direct_call_convergent_attribute_with_token_declared(i32 [[A]])
+; TUNIT-NEXT: ret i32 [[RS]]
+;
+; CGSCC: Function Attrs: convergent
+; CGSCC-LABEL: define i32 @direct_call_convergent_attribute_with_token_inner(
+; CGSCC-SAME: i32 [[A:%.*]]) #[[ATTR2]] {
+; CGSCC-NEXT: [[RS:%.*]] = call i32 @direct_call_convergent_attribute_with_token_declared(i32 [[A]])
+; CGSCC-NEXT: ret i32 [[RS]]
+;
+ %rs = call i32 @direct_call_convergent_attribute_with_token_declared(i32 %a)
+ ret i32 %rs
+}
+
+define i32 @direct_call_convergent_attribute_with_token(i32 %a) convergent {
+; TUNIT: Function Attrs: convergent
+; TUNIT-LABEL: define i32 @direct_call_convergent_attribute_with_token(
+; TUNIT-SAME: i32 [[A:%.*]]) #[[ATTR1]] {
+; TUNIT-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR6:[0-9]+]]
+; TUNIT-NEXT: [[RS:%.*]] = call i32 @direct_call_convergent_attribute_with_token_inner(i32 [[A]]) [ "convergencectrl"(token [[TK]]) ]
+; TUNIT-NEXT: ret i32 [[RS]]
+;
+; CGSCC: Function Attrs: convergent
+; CGSCC-LABEL: define i32 @direct_call_convergent_attribute_with_token(
+; CGSCC-SAME: i32 [[A:%.*]]) #[[ATTR2]] {
+; CGSCC-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR8:[0-9]+]]
+; CGSCC-NEXT: [[RS:%.*]] = call i32 @direct_call_convergent_attribute_with_token_inner(i32 [[A]]) [ "convergencectrl"(token [[TK]]) ]
+; CGSCC-NEXT: ret i32 [[RS]]
+;
+ %tk = call token @llvm.experimental.convergence.entry()
+ %rs = call i32 @direct_call_convergent_attribute_with_token_inner(i32 %a) [ "convergencectrl"(token %tk) ]
+ ret i32 %rs
+}
+
+; Function declared non-convergent.
+declare i32 @indirect_call_declared(i32 %a)
+
+; Function declared convergent, but which is not required to be
+; marked convergent: the only used is through a call which is marked
+; as convergent, hence the function attribute can be dropped.
+define i32 @indirect_call_inner(i32 %a) convergent {
+; CHECK-LABEL: define i32 @indirect_call_inner(
+; CHECK-SAME: i32 [[A:%.*]]) {
+; CHECK-NEXT: [[RS:%.*]] = call i32 @indirect_call_declared(i32 [[A]])
+; CHECK-NEXT: ret i32 [[RS]]
+;
+ %rs = call i32 @indirect_call_declared(i32 %a)
+ ret i32 %rs
+}
+
+; Convergent function, calling a function and marking the call as convergent.
+define i32 @indirect_call(i32 %a) convergent {
+; TUNIT: Function Attrs: convergent
+; TUNIT-LABEL: define i32 @indirect_call(
+; TUNIT-SAME: i32 [[A:%.*]]) #[[ATTR1]] {
+; TUNIT-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR7:[0-9]+]]
+; TUNIT-NEXT: [[RS:%.*]] = call i32 @indirect_call_inner(i32 [[A]]) #[[ATTR1]] [ "convergencectrl"(token [[TK]]) ]
+; TUNIT-NEXT: ret i32 [[RS]]
+;
+; CGSCC: Function Attrs: convergent
+; CGSCC-LABEL: define i32 @indirect_call(
+; CGSCC-SAME: i32 [[A:%.*]]) #[[ATTR2]] {
+; CGSCC-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR7]]
+; CGSCC-NEXT: [[RS:%.*]] = call i32 @indirect_call_inner(i32 [[A]]) #[[ATTR2]] [ "convergencectrl"(token [[TK]]) ]
+; CGSCC-NEXT: ret i32 [[RS]]
+;
+ %tk = call token @llvm.experimental.convergence.entry()
+ %fptr = alloca ptr
+ store ptr @indirect_call_inner, ptr %fptr
+ %ic = load ptr, ptr %fptr
+ %rs = call i32 %ic(i32 %a) convergent [ "convergencectrl"(token %tk) ]
+ ret i32 %rs
+}
+
+; A non-convergent declaration.
+declare i32 @leaf_convergent_indirect_declared(i32 %a)
+
+; A function calling indirectly a non-convergent function.
+define i32 @leaf_convergent_indirect(i32 %a) convergent {
+; TUNIT: Function Attrs: convergent
+; TUNIT-LABEL: define i32 @leaf_convergent_indirect(
+; TUNIT-SAME: i32 [[A:%.*]]) #[[ATTR1]] {
+; TUNIT-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR7]]
+; TUNIT-NEXT: [[RS:%.*]] = call i32 @leaf_convergent_indirect_declared(i32 [[A]]) #[[ATTR1]] [ "convergencectrl"(token [[TK]]) ]
+; TUNIT-NEXT: ret i32 [[RS]]
+;
+; CGSCC: Function Attrs: convergent
+; CGSCC-LABEL: define i32 @leaf_convergent_indirect(
+; CGSCC-SAME: i32 [[A:%.*]]) #[[ATTR2]] {
+; CGSCC-NEXT: [[TK:%.*]] = call token @llvm.experimental.convergence.entry() #[[ATTR7]]
+; CGSCC-NEXT: [[RS:%.*]] = call i32 @leaf_convergent_indirect_declared(i32 [[A]]) #[[ATTR2]] [ "convergencectrl"(token [[TK]]) ]
+; CGSCC-NEXT: ret i32 [[RS]]
+;
+ %tk = call token @llvm.experimental.convergence.entry()
+ %fptr = alloca ptr
+ store ptr @leaf_convergent_indirect_declared, ptr %fptr
+ %ic = load ptr, ptr %fptr
+ %rs = call i32 %ic(i32 %a) convergent [ "convergencectrl"(token %tk) ]
+ ret i32 %rs
+}
+
define i32 @defined_with_asm(i32 %a, i32 %b) {
-; CHECK-LABEL: define {{[^@]+}}@defined_with_asm
-; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-LABEL: define i32 @defined_with_asm(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
; CHECK-NEXT: [[RESULT:%.*]] = add i32 [[A]], [[B]]
; CHECK-NEXT: [[ASM_RESULT:%.*]] = call i32 asm sideeffect "addl $1, $0", "=r,r"(i32 [[RESULT]])
; CHECK-NEXT: ret i32 [[ASM_RESULT]]
@@ -72,14 +183,14 @@ define i32 @defined_with_asm(i32 %a, i32 %b) {
define i32 @calls_defined_with_asm(i32 %a, i32 %b) convergent {
; TUNIT: Function Attrs: convergent
-; TUNIT-LABEL: define {{[^@]+}}@calls_defined_with_asm
-; TUNIT-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR1]] {
+; TUNIT-LABEL: define i32 @calls_defined_with_asm(
+; TUNIT-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR1]] {
; TUNIT-NEXT: [[C:%.*]] = call i32 @defined_with_asm(i32 [[A]], i32 [[B]])
; TUNIT-NEXT: ret i32 [[C]]
;
; CGSCC: Function Attrs: convergent
-; CGSCC-LABEL: define {{[^@]+}}@calls_defined_with_asm
-; CGSCC-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR2]] {
+; CGSCC-LABEL: define i32 @calls_defined_with_asm(
+; CGSCC-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR2]] {
; CGSCC-NEXT: [[C:%.*]] = call i32 @defined_with_asm(i32 [[A]], i32 [[B]])
; CGSCC-NEXT: ret i32 [[C]]
;
@@ -91,15 +202,15 @@ declare void @llvm.convergent.copy.p0.p0.i64(ptr %dest, ptr %src, i64 %size, i1
define void @calls_convergent_intrinsic(ptr %dest, ptr %src, i64 %size) convergent {
; TUNIT: Function Attrs: convergent mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite)
-; TUNIT-LABEL: define {{[^@]+}}@calls_convergent_intrinsic
-; TUNIT-SAME: (ptr nofree [[DEST:%.*]], ptr nofree [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR3:[0-9]+]] {
-; TUNIT-NEXT: call void @llvm.convergent.copy.p0.p0.i64(ptr nofree [[DEST]], ptr nofree [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR5:[0-9]+]]
+; TUNIT-LABEL: define void @calls_convergent_intrinsic(
+; TUNIT-SAME: ptr nofree [[DEST:%.*]], ptr nofree [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR3:[0-9]+]] {
+; TUNIT-NEXT: call void @llvm.convergent.copy.p0.p0.i64(ptr nofree [[DEST]], ptr nofree [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR6]]
; TUNIT-NEXT: ret void
;
; CGSCC: Function Attrs: convergent mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite)
-; CGSCC-LABEL: define {{[^@]+}}@calls_convergent_intrinsic
-; CGSCC-SAME: (ptr nofree [[DEST:%.*]], ptr nofree [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR4:[0-9]+]] {
-; CGSCC-NEXT: call void @llvm.convergent.copy.p0.p0.i64(ptr nofree [[DEST]], ptr nofree [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR7:[0-9]+]]
+; CGSCC-LABEL: define void @calls_convergent_intrinsic(
+; CGSCC-SAME: ptr nofree [[DEST:%.*]], ptr nofree [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR4:[0-9]+]] {
+; CGSCC-NEXT: call void @llvm.convergent.copy.p0.p0.i64(ptr nofree [[DEST]], ptr nofree [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR8]]
; CGSCC-NEXT: ret void
;
call void @llvm.convergent.copy.p0.p0.i64(ptr %dest, ptr %src, i64 %size, i1 false)
@@ -110,15 +221,15 @@ declare void @llvm.memcpy.p0.p0.i64(ptr %dest, ptr %src, i64 %size, i1 %isVolati
define void @calls_intrinsic(ptr %dest, ptr %src, i64 %size) convergent {
; TUNIT: Function Attrs: convergent mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite)
-; TUNIT-LABEL: define {{[^@]+}}@calls_intrinsic
-; TUNIT-SAME: (ptr nofree writeonly captures(none) [[DEST:%.*]], ptr nofree readonly captures(none) [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR2:[0-9]+]] {
-; TUNIT-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr nofree writeonly captures(none) [[DEST]], ptr nofree readonly captures(none) [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR5]]
+; TUNIT-LABEL: define void @calls_intrinsic(
+; TUNIT-SAME: ptr nofree writeonly captures(none) [[DEST:%.*]], ptr nofree readonly captures(none) [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR2:[0-9]+]] {
+; TUNIT-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr nofree writeonly captures(none) [[DEST]], ptr nofree readonly captures(none) [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR6]]
; TUNIT-NEXT: ret void
;
; CGSCC: Function Attrs: convergent mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite)
-; CGSCC-LABEL: define {{[^@]+}}@calls_intrinsic
-; CGSCC-SAME: (ptr nofree writeonly captures(none) [[DEST:%.*]], ptr nofree readonly captures(none) [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR3:[0-9]+]] {
-; CGSCC-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr nofree writeonly captures(none) [[DEST]], ptr nofree readonly captures(none) [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR7]]
+; CGSCC-LABEL: define void @calls_intrinsic(
+; CGSCC-SAME: ptr nofree writeonly captures(none) [[DEST:%.*]], ptr nofree readonly captures(none) [[SRC:%.*]], i64 [[SIZE:%.*]]) #[[ATTR3:[0-9]+]] {
+; CGSCC-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr nofree writeonly captures(none) [[DEST]], ptr nofree readonly captures(none) [[SRC]], i64 [[SIZE]], i1 noundef false) #[[ATTR8]]
; CGSCC-NEXT: ret void
;
call void @llvm.memcpy.p0.p0.i64(ptr %dest, ptr %src, i64 %size, i1 false)
@@ -133,7 +244,9 @@ attributes #0 = { convergent mustprogress nofree norecurse nosync nounwind willr
; TUNIT: attributes #[[ATTR2]] = { convergent mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) }
; TUNIT: attributes #[[ATTR3]] = { convergent mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite) }
; TUNIT: attributes #[[ATTR4:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
-; TUNIT: attributes #[[ATTR5]] = { nofree willreturn }
+; TUNIT: attributes #[[ATTR5:[0-9]+]] = { convergent nocallback nofree nosync nounwind willreturn memory(none) }
+; TUNIT: attributes #[[ATTR6]] = { nofree willreturn }
+; TUNIT: attributes #[[ATTR7]] = { nofree nosync willreturn }
;.
; CGSCC: attributes #[[ATTR0]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) }
; CGSCC: attributes #[[ATTR1]] = { convergent mustprogress nofree nosync nounwind willreturn memory(none) }
@@ -141,6 +254,7 @@ attributes #0 = { convergent mustprogress nofree norecurse nosync nounwind willr
; CGSCC: attributes #[[ATTR3]] = { convergent mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) }
; CGSCC: attributes #[[ATTR4]] = { convergent mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite) }
; CGSCC: attributes #[[ATTR5:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
-; CGSCC: attributes #[[ATTR6]] = { nofree nosync willreturn }
-; CGSCC: attributes #[[ATTR7]] = { nofree willreturn }
+; CGSCC: attributes #[[ATTR6:[0-9]+]] = { convergent nocallback nofree nosync nounwind willreturn memory(none) }
+; CGSCC: attributes #[[ATTR7]] = { nofree nosync willreturn }
+; CGSCC: attributes #[[ATTR8]] = { nofree willreturn }
;.
diff --git a/llvm/test/Transforms/FunctionAttrs/convergent.ll b/llvm/test/Transforms/FunctionAttrs/convergent.ll
index 62e1183e58688..20292a01febae 100644
--- a/llvm/test/Transforms/FunctionAttrs/convergent.ll
+++ b/llvm/test/Transforms/FunctionAttrs/convergent.ll
@@ -1,5 +1,7 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes
; RUN: opt -passes=function-attrs -S < %s | FileCheck %s
+; RUN: opt -passes=function-attrs -S < %s | FileCheck %s
+; RUN: opt -passes=function-attrs --aa-pipeline=basic-aa -S %s | FileCheck %s
define i32 @nonleaf() convergent {
; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
More information about the llvm-commits
mailing list