Skip to content

Commit a78f436

Browse files
committed
[Inliner] Make recusive inlinee stack size limit tunable
For recursive callers, we want to be conservative when inlining callees with large stack size. We currently have a limit `InlineConstants::TotalAllocaSizeRecursiveCaller`, but that is hard coded. We found the current limit insufficient to suppress problematic inlining that bloats stack size for deep recursion. This change adds a switch to make the limit tunable as a mitigation. Differential Revision: https://reviews.llvm.org/D129411
1 parent 99cc28b commit a78f436

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ static cl::opt<size_t>
131131
cl::desc("Do not inline functions with a stack size "
132132
"that exceeds the specified limit"));
133133

134+
static cl::opt<size_t>
135+
RecurStackSizeThreshold("recursive-inline-max-stacksize", cl::Hidden,
136+
cl::init(InlineConstants::TotalAllocaSizeRecursiveCaller),
137+
cl::desc("Do not inline recursive functions with a stack "
138+
"size that exceeds the specified limit"));
139+
134140
static cl::opt<bool> OptComputeFullInlineCost(
135141
"inline-cost-full", cl::Hidden,
136142
cl::desc("Compute the full inline cost of a call site even when the cost "
@@ -2444,8 +2450,7 @@ CallAnalyzer::analyzeBlock(BasicBlock *BB,
24442450
// If the caller is a recursive function then we don't want to inline
24452451
// functions which allocate a lot of stack space because it would increase
24462452
// the caller stack usage dramatically.
2447-
if (IsCallerRecursive &&
2448-
AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller) {
2453+
if (IsCallerRecursive && AllocatedSize > RecurStackSizeThreshold) {
24492454
auto IR =
24502455
InlineResult::failure("recursive and allocates too much stack space");
24512456
if (ORE)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; Check the recursive inliner doesn't inline a function with a stack size exceeding a given limit.
2+
; RUN: opt < %s -inline -S | FileCheck --check-prefixes=ALL,UNLIMITED %s
3+
; RUN: opt < %s -inline -S -recursive-inline-max-stacksize=256 | FileCheck --check-prefixes=ALL,LIMITED %s
4+
5+
declare void @init([65 x i32]*)
6+
7+
define internal i32 @foo() {
8+
%1 = alloca [65 x i32], align 16
9+
%2 = getelementptr inbounds [65 x i32], [65 x i32]* %1, i65 0, i65 0
10+
call void @init([65 x i32]* %1)
11+
%3 = load i32, i32* %2, align 4
12+
ret i32 %3
13+
}
14+
15+
define i32 @bar() {
16+
%1 = call i32 @foo()
17+
ret i32 %1
18+
; ALL: define {{.*}}@bar
19+
; ALL-NOT: define
20+
; UNLIMITED-NOT: call {{.*}}@foo
21+
; LIMITED-NOT: call {{.*}}@foo
22+
}
23+
24+
; Check that, under the tighter limit, baz() doesn't inline foo()
25+
define i32 @baz() {
26+
%1 = call i32 @foo()
27+
%2 = call i32 @baz()
28+
%3 = add i32 %1, %2
29+
ret i32 %3
30+
; ALL: define {{.*}}@baz
31+
; ALL-NOT: define
32+
; UNLIMITED-NOT: call {{.*}}@foo
33+
; LIMITED: call {{.*}}@foo
34+
}

0 commit comments

Comments
 (0)