[llvm] [AMDGPU] Increase inline threshold when the callee only has one live use (PR #111311)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 7 08:55:35 PDT 2024
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/111311
>From e4c116094da62bea90ee466f25269090311e199e Mon Sep 17 00:00:00 2001
From: Shilei Tian <shilei.tian at amd.com>
Date: Sun, 6 Oct 2024 16:16:47 -0400
Subject: [PATCH] [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.
---
.../AMDGPU/AMDGPUTargetTransformInfo.cpp | 10 +++++++
.../AMDGPU/amdgpu-inline-only-one-live-use.ll | 26 +++++++++++++++++++
2 files changed, 36 insertions(+)
create mode 100644 llvm/test/Transforms/Inline/AMDGPU/amdgpu-inline-only-one-live-use.ll
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
index d348166c2d9a04..608ca6ded43e53 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
@@ -75,6 +75,10 @@ static cl::opt<size_t> InlineMaxBB(
cl::desc("Maximum number of BBs allowed in a function after inlining"
" (compile time constraint)"));
+static cl::opt<unsigned> InlineThresholdOneLiveUse(
+ "amdgpu-inline-threshold-one-live-use", cl::Hidden, cl::init(15000),
+ cl::desc("Threshold added when the callee only has one live use"));
+
static bool dependsOnLocalPhi(const Loop *L, const Value *Cond,
unsigned Depth = 0) {
const Instruction *I = dyn_cast<Instruction>(Cond);
@@ -1307,6 +1311,12 @@ unsigned GCNTTIImpl::adjustInliningThreshold(const CallBase *CB) const {
unsigned AllocaSize = getCallArgsTotalAllocaSize(CB, DL);
if (AllocaSize > 0)
Threshold += ArgAllocaCost;
+
+ // Increase the threshold if it is the only call to a local function.
+ Function *Callee = CB->getCalledFunction();
+ if (Callee && Callee->hasLocalLinkage() && Callee->hasOneLiveUse())
+ Threshold += InlineThresholdOneLiveUse;
+
return Threshold;
}
diff --git a/llvm/test/Transforms/Inline/AMDGPU/amdgpu-inline-only-one-live-use.ll b/llvm/test/Transforms/Inline/AMDGPU/amdgpu-inline-only-one-live-use.ll
new file mode 100644
index 00000000000000..872ee410e3f973
--- /dev/null
+++ b/llvm/test/Transforms/Inline/AMDGPU/amdgpu-inline-only-one-live-use.ll
@@ -0,0 +1,26 @@
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -S -passes=inline -inline-threshold=0 -debug-only=inline-cost %s -o - 2>&1 | FileCheck --check-prefixes=CHECK,CHECK-DEFAULT %s
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -S -passes=inline -inline-threshold=0 -debug-only=inline-cost %s -amdgpu-inline-threshold-one-live-use=1024 -o - 2>&1 | FileCheck --check-prefixes=CHECK,CHECK-USER %s
+; REQUIRES: asserts
+
+; CHECK: Analyzing call of callee_not_only_one_live_use... (caller:caller)
+; CHECK: Cost: -30
+; CHECK: Threshold: 0
+; CHECK: Analyzing call of callee_only_one_live_use... (caller:caller)
+; CHECK: Cost: -15030
+; CHECK-DEFAULT: Threshold: 247500
+; CHECK-USER: Threshold: 16896
+
+define internal void @callee_not_only_one_live_use() {
+ ret void
+}
+
+define internal void @callee_only_one_live_use() {
+ ret void
+}
+
+define void @caller() {
+ call void @callee_not_only_one_live_use()
+ call void @callee_not_only_one_live_use()
+ call void @callee_only_one_live_use()
+ ret void
+}
More information about the llvm-commits
mailing list