Skip to content

Commit c2376ef

Browse files
committed
[AMDGPU] Increase inline threshold when the callee only has one live use
Currently we will not inline a large function even if it only has one live use. This could significantly impact the performance because CSR spill is very expensive. The goal of this PR is trying to force the inlining if there is only one live use by adjusting the inlining threshold, which is a configurable number. The default value is 15000, which borrows from `InlineConstants::LastCallToStaticBonus`. I'm not sure if this is a good number, and if this is the right way to do that. After making this change, the callee in my local test case can finally be inlined, but the cost is still very close to the threshold: `cost=14010, threshold=170775`. Speaking of the test, how are we gonna test this? Do we want to include a giant IR file? Fixes SWDEV-471398.
1 parent d905b1c commit c2376ef

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ class TargetTransformInfo {
352352
unsigned getInliningCostBenefitAnalysisSavingsMultiplier() const;
353353
unsigned getInliningCostBenefitAnalysisProfitableMultiplier() const;
354354

355+
/// \returns The bonus of inlining the last call to a static function.
356+
int getInliningLastCallToStaticBonus() const;
357+
355358
/// \returns A value to be added to the inlining threshold.
356359
unsigned adjustInliningThreshold(const CallBase *CB) const;
357360

@@ -1825,6 +1828,7 @@ class TargetTransformInfo::Concept {
18251828
virtual unsigned getInliningCostBenefitAnalysisSavingsMultiplier() const = 0;
18261829
virtual unsigned
18271830
getInliningCostBenefitAnalysisProfitableMultiplier() const = 0;
1831+
virtual int getInliningLastCallToStaticBonus() const = 0;
18281832
virtual unsigned adjustInliningThreshold(const CallBase *CB) = 0;
18291833
virtual int getInlinerVectorBonusPercent() const = 0;
18301834
virtual unsigned getCallerAllocaCost(const CallBase *CB,
@@ -2230,6 +2234,9 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
22302234
unsigned getInliningCostBenefitAnalysisProfitableMultiplier() const override {
22312235
return Impl.getInliningCostBenefitAnalysisProfitableMultiplier();
22322236
}
2237+
int getInliningLastCallToStaticBonus() const override {
2238+
return Impl.getInliningLastCallToStaticBonus();
2239+
}
22332240
int getInlinerVectorBonusPercent() const override {
22342241
return Impl.getInlinerVectorBonusPercent();
22352242
}

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class TargetTransformInfoImplBase {
7474
unsigned getInliningCostBenefitAnalysisProfitableMultiplier() const {
7575
return 8;
7676
}
77+
int getInliningLastCallToStaticBonus() const {
78+
// This is same as InlineConstants::LastCallToStaticBonus.
79+
return 15000;
80+
}
7781
unsigned adjustInliningThreshold(const CallBase *CB) const { return 0; }
7882
unsigned getCallerAllocaCost(const CallBase *CB, const AllocaInst *AI) const {
7983
return 0;

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ void InlineCostCallAnalyzer::updateThreshold(CallBase &Call, Function &Callee) {
19431943
// and the callsite.
19441944
int SingleBBBonusPercent = 50;
19451945
int VectorBonusPercent = TTI.getInlinerVectorBonusPercent();
1946-
int LastCallToStaticBonus = InlineConstants::LastCallToStaticBonus;
1946+
int LastCallToStaticBonus = TTI.getInliningLastCallToStaticBonus();
19471947

19481948
// Lambda to set all the above bonus and bonus percentages to 0.
19491949
auto DisallowAllBonuses = [&]() {

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ TargetTransformInfo::getInliningCostBenefitAnalysisProfitableMultiplier()
228228
return TTIImpl->getInliningCostBenefitAnalysisProfitableMultiplier();
229229
}
230230

231+
int TargetTransformInfo::getInliningLastCallToStaticBonus() const {
232+
return TTIImpl->getInliningLastCallToStaticBonus();
233+
}
234+
231235
unsigned
232236
TargetTransformInfo::adjustInliningThreshold(const CallBase *CB) const {
233237
return TTIImpl->adjustInliningThreshold(CB);

llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ class GCNTTIImpl final : public BasicTTIImplBase<GCNTTIImpl> {
240240
bool areInlineCompatible(const Function *Caller,
241241
const Function *Callee) const;
242242

243+
int getInliningLastCallToStaticBonus() const {
244+
// InlineConstants::LastCallToStaticBonus * 11 where 11 is AMDGPU's inlining
245+
// threshold multiplier.
246+
return 165000;
247+
}
243248
unsigned getInliningThresholdMultiplier() const { return 11; }
244249
unsigned adjustInliningThreshold(const CallBase *CB) const;
245250
unsigned getCallerAllocaCost(const CallBase *CB, const AllocaInst *AI) const;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: opt -mtriple=amdgcn-amd-amdhsa -S -passes=inline -inline-threshold=0 -debug-only=inline-cost %s -o - 2>&1 | FileCheck %s
2+
; REQUIRES: asserts
3+
4+
; CHECK: Analyzing call of callee_not_only_one_live_use... (caller:caller)
5+
; CHECK: Cost: -30
6+
; CHECK: Analyzing call of callee_only_one_live_use... (caller:caller)
7+
; CHECK: Cost: -165030
8+
9+
define internal void @callee_not_only_one_live_use() {
10+
ret void
11+
}
12+
13+
define internal void @callee_only_one_live_use() {
14+
ret void
15+
}
16+
17+
define void @caller() {
18+
call void @callee_not_only_one_live_use()
19+
call void @callee_not_only_one_live_use()
20+
call void @callee_only_one_live_use()
21+
ret void
22+
}

0 commit comments

Comments
 (0)