Skip to content

Commit 1a626a6

Browse files
committed
Rebased and addressed review comments
1 parent 338a47e commit 1a626a6

File tree

6 files changed

+97
-134
lines changed

6 files changed

+97
-134
lines changed

llvm/lib/Transforms/IPO/FunctionAttrs.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,13 +2285,10 @@ deriveAttrsInPostOrder(ArrayRef<Function *> Functions, AARGetterT &&AARGetter,
22852285
addColdAttrs(Nodes.SCCNodes, Changed);
22862286
addWillReturn(Nodes.SCCNodes, Changed);
22872287
addNoUndefAttrs(Nodes.SCCNodes, Changed);
2288-
2289-
// If we have no external nodes participating in the SCC, we can deduce some
2290-
// more precise attributes as well.
2291-
addNoAliasAttrs(Nodes.SCCNodes, Changed);
2292-
addNonNullAttrs(Nodes.SCCNodes, Changed);
2293-
inferAttrsFromFunctionBodies(Nodes.SCCNodes, Changed);
2294-
addNoRecurseAttrs(Nodes.SCCNodes, Changed, NoFunctionAddressIsTaken);
2288+
addNoAliasAttrs(Nodes.SCCNodes, Changed);
2289+
addNonNullAttrs(Nodes.SCCNodes, Changed);
2290+
inferAttrsFromFunctionBodies(Nodes.SCCNodes, Changed);
2291+
addNoRecurseAttrs(Nodes.SCCNodes, Changed, NoFunctionAddressIsTaken);
22952292

22962293
// Finally, infer the maximal set of attributes from the ones we've inferred
22972294
// above. This is handling the cases where one attribute on a signature
@@ -2333,38 +2330,37 @@ PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
23332330
Functions.push_back(&N.getFunction());
23342331
}
23352332

2336-
bool NoFunctionsAddressIsTaken = false;
23372333
// Check if any function in the whole program has its address taken or has
23382334
// potentially external linkage.
23392335
// We use this information when inferring norecurse attribute: If there is
23402336
// no function whose address is taken and all functions have internal
23412337
// linkage, there is no path for a callback to any user function.
2342-
if (IsLTOPostLink || ForceLTOFuncAttrs) {
2343-
bool AnyFunctionsAddressIsTaken = false;
2344-
// Get the parent Module of the Function
2345-
Module &M = *C.begin()->getFunction().getParent();
2346-
for (Function &F : M) {
2347-
// We only care about functions defined in user program whose addresses
2348-
// escape, making them potential callback targets.
2349-
if (F.isDeclaration())
2350-
continue;
2338+
bool AnyFunctionsAddressIsTaken = false;
2339+
// Get the parent Module of the Function
2340+
Module &M = *C.begin()->getFunction().getParent();
2341+
for (Function &F : M) {
2342+
// We only care about functions defined in user program whose addresses
2343+
// escape, making them potential callback targets.
2344+
if (F.isDeclaration())
2345+
continue;
23512346

2352-
// If the function is already marked as norecurse, this should not block
2353-
// norecurse inference even though it may have external linkage.
2354-
// For ex: main() in C++.
2355-
if (F.doesNotRecurse())
2356-
continue;
2347+
// If the function is already marked as norecurse, this should not block
2348+
// norecurse inference even though it may have external linkage.
2349+
// For ex: main() in C++.
2350+
if (F.doesNotRecurse())
2351+
continue;
23572352

2358-
if (!F.hasLocalLinkage() || F.hasAddressTaken()) {
2359-
AnyFunctionsAddressIsTaken = true;
2360-
break; // break if we found one
2361-
}
2353+
if (!F.hasLocalLinkage() || F.hasAddressTaken()) {
2354+
AnyFunctionsAddressIsTaken = true;
2355+
break; // break if we found one
23622356
}
2363-
NoFunctionsAddressIsTaken = !AnyFunctionsAddressIsTaken;
23642357
}
23652358

2359+
if (ForceLTOFuncAttrs)
2360+
IsLTOPostLink = true; // For testing purposes only
23662361
auto ChangedFunctions = deriveAttrsInPostOrder(
2367-
Functions, AARGetter, ArgAttrsOnly, NoFunctionsAddressIsTaken);
2362+
Functions, AARGetter, ArgAttrsOnly,
2363+
IsLTOPostLink ? !AnyFunctionsAddressIsTaken : false);
23682364

23692365
if (ChangedFunctions.empty())
23702366
return PreservedAnalyses::all();

llvm/test/Transforms/FunctionAttrs/norecurse_libfunc_address_taken.ll

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77

88
@.str = private unnamed_addr constant [12 x i8] c"Hello World\00", align 1
99

10-
; Function Attrs: nofree noinline nounwind uwtable
11-
define dso_local void @bob() #0 {
12-
; CHECK: Function Attrs: nofree noinline nounwind uwtable
13-
; CHECK-LABEL: define dso_local void @bob(
14-
; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
10+
define dso_local void @bob() {
11+
; CHECK-LABEL: define dso_local void @bob() {
1512
; CHECK-NEXT: [[ENTRY:.*:]]
1613
; CHECK-NEXT: [[CALL:%.*]] = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
1714
; CHECK-NEXT: ret void
@@ -21,14 +18,12 @@ entry:
2118
ret void
2219
}
2320

24-
; Function Attrs: nofree nounwind
25-
declare noundef i32 @printf(ptr noundef readonly captures(none), ...) local_unnamed_addr #1
21+
declare noundef i32 @printf(ptr noundef readonly captures(none), ...)
2622

27-
; Function Attrs: nofree norecurse nounwind uwtable
28-
define dso_local noundef i32 @main() #2 {
29-
; CHECK: Function Attrs: nofree norecurse nounwind uwtable
23+
define dso_local noundef i32 @main() norecurse {
24+
; CHECK: Function Attrs: norecurse
3025
; CHECK-LABEL: define dso_local noundef i32 @main(
31-
; CHECK-SAME: ) #[[ATTR2:[0-9]+]] {
26+
; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
3227
; CHECK-NEXT: [[ENTRY:.*:]]
3328
; CHECK-NEXT: tail call void @bob()
3429
; CHECK-NEXT: ret i32 0
@@ -37,7 +32,3 @@ entry:
3732
tail call void @bob()
3833
ret i32 0
3934
}
40-
41-
attributes #0 = { nofree noinline nounwind uwtable }
42-
attributes #1 = { nofree nounwind }
43-
attributes #2 = { nofree norecurse nounwind uwtable }

llvm/test/Transforms/FunctionAttrs/norecurse_libfunc_no_address_taken.ll

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
@.str = private unnamed_addr constant [12 x i8] c"Hello World\00", align 1
99

1010
; Function Attrs: nofree noinline nounwind uwtable
11-
define internal void @bob() #0 {
12-
; CHECK: Function Attrs: nofree noinline norecurse nounwind uwtable
11+
define internal void @bob() {
12+
; CHECK: Function Attrs: norecurse
1313
; CHECK-LABEL: define internal void @bob(
1414
; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
1515
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -22,13 +22,13 @@ entry:
2222
}
2323

2424
; Function Attrs: nofree nounwind
25-
declare noundef i32 @printf(ptr noundef readonly captures(none), ...) local_unnamed_addr #1
25+
declare noundef i32 @printf(ptr noundef readonly captures(none), ...)
2626

2727
; Function Attrs: nofree norecurse nounwind uwtable
28-
define dso_local noundef i32 @main() #2 {
29-
; CHECK: Function Attrs: nofree norecurse nounwind uwtable
28+
define dso_local noundef i32 @main() norecurse {
29+
; CHECK: Function Attrs: norecurse
3030
; CHECK-LABEL: define dso_local noundef i32 @main(
31-
; CHECK-SAME: ) #[[ATTR2:[0-9]+]] {
31+
; CHECK-SAME: ) #[[ATTR0]] {
3232
; CHECK-NEXT: [[ENTRY:.*:]]
3333
; CHECK-NEXT: tail call void @bob()
3434
; CHECK-NEXT: ret i32 0
@@ -37,7 +37,3 @@ entry:
3737
tail call void @bob()
3838
ret i32 0
3939
}
40-
41-
attributes #0 = { nofree noinline nounwind uwtable }
42-
attributes #1 = { nofree nounwind }
43-
attributes #2 = { nofree norecurse nounwind uwtable }

llvm/test/Transforms/FunctionAttrs/norecurse_multi_scc_indirect_recursion.ll

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-attributes --version 5
2-
; RUN: opt < %s -passes=function-attrs -force-lto-funcattrs -S | FileCheck %s
2+
; RUN: opt < %s -passes=function-attrs -force-lto-funcattrs -S | FileCheck %s
33

44
; This test includes a call graph with multiple SCCs. The purpose of this is
55
; to check that norecurse is not added when a function is part of non-singular
@@ -11,8 +11,8 @@
1111
; None of these functions should be marked as norecurse
1212

1313
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
14-
define internal void @bar1() #0 {
15-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
14+
define internal void @bar1() {
15+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
1616
; CHECK-LABEL: define internal void @bar1(
1717
; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
1818
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -25,8 +25,8 @@ entry:
2525
}
2626

2727
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
28-
define internal void @f1() #0 {
29-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
28+
define internal void @f1() {
29+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
3030
; CHECK-LABEL: define internal void @f1(
3131
; CHECK-SAME: ) #[[ATTR0]] {
3232
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -43,8 +43,8 @@ entry:
4343
}
4444

4545
; Function Attrs: nofree noinline norecurse nosync nounwind memory(none) uwtable
46-
define dso_local noundef i32 @main() #1 {
47-
; CHECK: Function Attrs: nofree noinline norecurse nosync nounwind memory(none) uwtable
46+
define dso_local noundef i32 @main() norecurse {
47+
; CHECK: Function Attrs: nofree norecurse nosync nounwind memory(none)
4848
; CHECK-LABEL: define dso_local noundef i32 @main(
4949
; CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
5050
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -57,8 +57,8 @@ entry:
5757
}
5858

5959
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
60-
define internal void @foo1() #0 {
61-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
60+
define internal void @foo1() {
61+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
6262
; CHECK-LABEL: define internal void @foo1(
6363
; CHECK-SAME: ) #[[ATTR0]] {
6464
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -71,8 +71,8 @@ entry:
7171
}
7272

7373
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
74-
define internal void @bar() #0 {
75-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
74+
define internal void @bar() {
75+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
7676
; CHECK-LABEL: define internal void @bar(
7777
; CHECK-SAME: ) #[[ATTR0]] {
7878
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -85,8 +85,8 @@ entry:
8585
}
8686

8787
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
88-
define internal void @foo() #0 {
89-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
88+
define internal void @foo() {
89+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
9090
; CHECK-LABEL: define internal void @foo(
9191
; CHECK-SAME: ) #[[ATTR0]] {
9292
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -99,8 +99,8 @@ entry:
9999
}
100100

101101
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
102-
define internal void @bar4() #0 {
103-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
102+
define internal void @bar4() {
103+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
104104
; CHECK-LABEL: define internal void @bar4(
105105
; CHECK-SAME: ) #[[ATTR0]] {
106106
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -113,8 +113,8 @@ entry:
113113
}
114114

115115
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
116-
define internal void @bar2() #0 {
117-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
116+
define internal void @bar2() {
117+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
118118
; CHECK-LABEL: define internal void @bar2(
119119
; CHECK-SAME: ) #[[ATTR0]] {
120120
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -127,8 +127,8 @@ entry:
127127
}
128128

129129
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
130-
define internal void @bar3() #0 {
131-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
130+
define internal void @bar3() {
131+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
132132
; CHECK-LABEL: define internal void @bar3(
133133
; CHECK-SAME: ) #[[ATTR0]] {
134134
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -141,8 +141,8 @@ entry:
141141
}
142142

143143
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
144-
define internal void @fun() #0 {
145-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
144+
define internal void @fun() {
145+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
146146
; CHECK-LABEL: define internal void @fun(
147147
; CHECK-SAME: ) #[[ATTR0]] {
148148
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -154,9 +154,8 @@ entry:
154154
ret void
155155
}
156156

157-
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
158-
define internal void @baz() #0 {
159-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
157+
define internal void @baz() {
158+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
160159
; CHECK-LABEL: define internal void @baz(
161160
; CHECK-SAME: ) #[[ATTR0]] {
162161
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -167,6 +166,3 @@ entry:
167166
tail call void @fun()
168167
ret void
169168
}
170-
171-
attributes #0 = { nofree noinline nosync nounwind memory(none) uwtable }
172-
attributes #1 = { nofree noinline norecurse nosync nounwind memory(none) uwtable }

llvm/test/Transforms/FunctionAttrs/norecurse_multi_scc_indirect_recursion1.ll

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
; norecurse.
1313

1414
; Function Attrs: nofree noinline norecurse nosync nounwind memory(none) uwtable
15-
define dso_local noundef i32 @main() #1 {
16-
; CHECK: Function Attrs: nofree noinline norecurse nosync nounwind memory(none) uwtable
15+
define dso_local noundef i32 @main() norecurse {
16+
; CHECK: Function Attrs: nofree norecurse nosync nounwind memory(none)
1717
; CHECK-LABEL: define dso_local noundef i32 @main(
1818
; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
1919
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -26,8 +26,8 @@ entry:
2626
}
2727

2828
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
29-
define internal void @f1() #0 {
30-
; CHECK: Function Attrs: nofree noinline norecurse nosync nounwind memory(none) uwtable
29+
define internal void @f1() {
30+
; CHECK: Function Attrs: nofree norecurse nosync nounwind memory(none)
3131
; CHECK-LABEL: define internal void @f1(
3232
; CHECK-SAME: ) #[[ATTR0]] {
3333
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -42,8 +42,8 @@ entry:
4242
}
4343

4444
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
45-
define internal void @bar4() #0 {
46-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
45+
define internal void @bar4() {
46+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
4747
; CHECK-LABEL: define internal void @bar4(
4848
; CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
4949
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -56,8 +56,8 @@ entry:
5656
}
5757

5858
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
59-
define internal void @bar2() #0 {
60-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
59+
define internal void @bar2() {
60+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
6161
; CHECK-LABEL: define internal void @bar2(
6262
; CHECK-SAME: ) #[[ATTR1]] {
6363
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -70,8 +70,8 @@ entry:
7070
}
7171

7272
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
73-
define internal void @bar3() #0 {
74-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
73+
define internal void @bar3() {
74+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
7575
; CHECK-LABEL: define internal void @bar3(
7676
; CHECK-SAME: ) #[[ATTR1]] {
7777
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -84,8 +84,8 @@ entry:
8484
}
8585

8686
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
87-
define internal void @fun() #0 {
88-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
87+
define internal void @fun() {
88+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
8989
; CHECK-LABEL: define internal void @fun(
9090
; CHECK-SAME: ) #[[ATTR1]] {
9191
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -98,8 +98,8 @@ entry:
9898
}
9999

100100
; Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
101-
define internal void @baz() #0 {
102-
; CHECK: Function Attrs: nofree noinline nosync nounwind memory(none) uwtable
101+
define internal void @baz() {
102+
; CHECK: Function Attrs: nofree nosync nounwind memory(none)
103103
; CHECK-LABEL: define internal void @baz(
104104
; CHECK-SAME: ) #[[ATTR1]] {
105105
; CHECK-NEXT: [[ENTRY:.*:]]
@@ -110,6 +110,3 @@ entry:
110110
tail call void @fun()
111111
ret void
112112
}
113-
114-
attributes #0 = { nofree noinline nosync nounwind memory(none) uwtable }
115-
attributes #1 = { nofree noinline norecurse nosync nounwind memory(none) uwtable }

0 commit comments

Comments
 (0)