[PATCH] D134376: [ModuleInliner] Add a cost-benefit-based priority (WORK-IN-PROGRESS)
Kazu Hirata via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 21 10:50:43 PDT 2022
kazu created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
kazu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
This is a work-in-progress patch. DO NOT COMMIT.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D134376
Files:
llvm/lib/Analysis/InlineOrder.cpp
Index: llvm/lib/Analysis/InlineOrder.cpp
===================================================================
--- llvm/lib/Analysis/InlineOrder.cpp
+++ llvm/lib/Analysis/InlineOrder.cpp
@@ -30,7 +30,9 @@
cl::values(clEnumValN(InlinePriorityMode::Size, "size",
"Use callee size priority."),
clEnumValN(InlinePriorityMode::Cost, "cost",
- "Use inline cost priority.")));
+ "Use inline cost priority."),
+ clEnumValN(InlinePriorityMode::OptRatio, "optratio",
+ "Use cost-benefit ratio.")));
namespace {
@@ -100,6 +102,70 @@
int Cost;
};
+class CostBenefitPriority {
+public:
+ CostBenefitPriority() = default;
+ CostBenefitPriority(const CallBase *CB, FunctionAnalysisManager &FAM,
+ const InlineParams &Params) {
+ auto IC = getInlineCostWrapper(const_cast<CallBase &>(*CB), FAM, Params);
+ Cost = IC.getCost();
+ CostBenefit = IC.getCostBenefit();
+ }
+
+ static bool isMoreDesirable(const CostBenefitPriority &P1,
+ const CostBenefitPriority &P2) {
+ // We prioritize call sites in the dictionary order of the following
+ // priorities:
+ //
+ // 1. Those call sites that reduce the caller size. Within them, we
+ // prioritize those call sites with bigger reduction.
+ //
+ // 2. Those call sites that have gone through the cost-benefit analysis.
+ // Currently, they are limited to hot call sites. Within them, we
+ // prioritize those call sites with higher benefit-to-cost ratios.
+ //
+ // 3. Remaining call sites are prioritized according to their costs.
+
+ // We look for costs greater than -1000 to avoid catching those call sites
+ // with LastCallToStaticBonus. This is an ugly hack. We need to get proper
+ // costs before LastCallToStaticBonus is applied.
+ bool P1ReducesCallerSize = -1000 < P1.Cost && P1.Cost < 0;
+ bool P2ReducesCallerSize = -1000 < P2.Cost && P2.Cost < 0;
+ if (P1ReducesCallerSize || P2ReducesCallerSize) {
+ // If one reduces the caller size while the other doesn't, then return
+ // true iff P1 reduces the caller size.
+ if (P1ReducesCallerSize != P2ReducesCallerSize)
+ return P1ReducesCallerSize;
+
+ // If they both reduce the caller size, pick the one with the smaller
+ // cost.
+ return P1.Cost < P2.Cost;
+ }
+
+ bool P1HasCB = P1.CostBenefit.has_value();
+ bool P2HasCB = P2.CostBenefit.has_value();
+ if (P1HasCB || P2HasCB) {
+ // If one has undergone the cost-benefit analysis while the other hasn't,
+ // then return true iff P1 has.
+ if (P1HasCB != P2HasCB)
+ return P1HasCB;
+
+ // If they have undergone the cost-benefit analysis, then pick the one
+ // with a higher benefit-to-cost ratio.
+ APInt LHS = P1.CostBenefit->getBenefit() * P2.CostBenefit->getCost();
+ APInt RHS = P2.CostBenefit->getBenefit() * P1.CostBenefit->getCost();
+ return LHS.ugt(RHS);
+ }
+
+ // Remaining call sites are ordered according to their costs.
+ return P1.Cost < P2.Cost;
+ }
+
+private:
+ int Cost;
+ Optional<CostBenefitPair> CostBenefit;
+};
+
template <typename PriorityT>
class PriorityInlineOrder : public InlineOrder<std::pair<CallBase *, int>> {
using T = std::pair<CallBase *, int>;
@@ -195,9 +261,10 @@
LLVM_DEBUG(dbgs() << " Current used priority: Cost priority ---- \n");
return std::make_unique<PriorityInlineOrder<CostPriority>>(FAM, Params);
- default:
- llvm_unreachable("Unsupported Inline Priority Mode");
- break;
+ case InlinePriorityMode::OptRatio:
+ LLVM_DEBUG(
+ dbgs() << " Current used priority: cost-benefit priority ---- \n");
+ return std::make_unique<PriorityInlineOrder<CostBenefitPriority>>(FAM, Params);
}
return nullptr;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134376.461947.patch
Type: text/x-patch
Size: 3928 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220921/6f02e133/attachment.bin>
More information about the llvm-commits
mailing list