[PATCH] D79138: [Inlining] Teach shouldBeDeferred to take the total cost into account
Kazu Hirata via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 29 16:12:52 PDT 2020
kazu created this revision.
kazu added a reviewer: davidxl.
Herald added subscribers: hiraditya, eraman.
Herald added a project: LLVM.
This patch teaches shouldBeDeferred to take into account the total
cost of inlining.
Suppose we have a call hierarchy {A1,A2,A3,...}->B->C. (Each of A1,
A2, A3, ... calls B, which in turn calls C.)
Without this patch, shouldBeDeferred essentially returns true if
TotalSecondaryCost < IC.getCost()
where TotalSecondaryCost is the total cost of inlining B into As.
This means that if B is a small wraper function, for example, it would
get inlined into all of As. In turn, C gets inlined into all of As.
In other words, shouldBeDeferred ignores the cost of inlining C into
each of As.
This patch replaces the expression above with:
TotalCost < Allowance
where
- TotalCost is TotalSecondaryCost + IC.getCost() * # of As, and
- Allowance is IC.getCost() * Scale
For now, Scale defaults to 2, which essentially limits the number of
As to 1 for shouldBeDeferred to return true.
With this patch, Clang PGO bootstrap results in a 0.33% smaller .text*
sections. Compiling the 10 largest preprocessed files of Clang with
the PGO bootstrapped clang takes:
- 69.677 seconds on average of five runs without the patch, and
- 68.939 seconds on average of five runs with the patch.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D79138
Files:
llvm/lib/Transforms/IPO/Inliner.cpp
Index: llvm/lib/Transforms/IPO/Inliner.cpp
===================================================================
--- llvm/lib/Transforms/IPO/Inliner.cpp
+++ llvm/lib/Transforms/IPO/Inliner.cpp
@@ -93,6 +93,11 @@
DisableInlinedAllocaMerging("disable-inlined-alloca-merging",
cl::init(false), cl::Hidden);
+static cl::opt<int>
+ InlineDeferralScale("inline-deferral-scale",
+ cl::desc("Scale to limit the cost of inline deferral"),
+ cl::init(2), cl::Hidden);
+
namespace {
enum class InlinerFunctionImportStatsOpts {
@@ -338,12 +343,8 @@
bool ApplyLastCallBonus = Caller->hasLocalLinkage() && !Caller->hasOneUse();
// This bool tracks what happens if we DO inline C into B.
bool InliningPreventsSomeOuterInline = false;
+ unsigned SecondaryUsers = 0;
for (User *U : Caller->users()) {
- // If the caller will not be removed (either because it does not have a
- // local linkage or because the LastCallToStaticBonus has been already
- // applied), then we can exit the loop early.
- if (!ApplyLastCallBonus && TotalSecondaryCost >= IC.getCost())
- return false;
CallBase *CS2 = dyn_cast<CallBase>(U);
// If this isn't a call to Caller (it could be some other sort
@@ -369,8 +370,13 @@
if (IC2.getCostDelta() <= CandidateCost) {
InliningPreventsSomeOuterInline = true;
TotalSecondaryCost += IC2.getCost();
+ SecondaryUsers++;
}
}
+
+ if (!InliningPreventsSomeOuterInline)
+ return false;
+
// If all outer calls to Caller would get inlined, the cost for the last
// one is set very low by getInlineCost, in anticipation that Caller will
// be removed entirely. We did not account for this above unless there
@@ -378,7 +384,9 @@
if (ApplyLastCallBonus)
TotalSecondaryCost -= InlineConstants::LastCallToStaticBonus;
- return InliningPreventsSomeOuterInline && TotalSecondaryCost < IC.getCost();
+ int TotalCost = TotalSecondaryCost + IC.getCost() * SecondaryUsers;
+ int Allowance = IC.getCost() * InlineDeferralScale;
+ return TotalCost < Allowance;
}
static std::basic_ostream<char> &operator<<(std::basic_ostream<char> &R,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79138.261073.patch
Type: text/x-patch
Size: 2218 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200429/6885a3f5/attachment.bin>
More information about the llvm-commits
mailing list