InlineOrder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //===- InlineOrder.cpp - Inlining order abstraction -*- C++ ---*-----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Analysis/InlineOrder.h"
  9. #include "llvm/Analysis/AssumptionCache.h"
  10. #include "llvm/Analysis/BlockFrequencyInfo.h"
  11. #include "llvm/Analysis/GlobalsModRef.h"
  12. #include "llvm/Analysis/InlineAdvisor.h"
  13. #include "llvm/Analysis/InlineCost.h"
  14. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  15. #include "llvm/Analysis/ProfileSummaryInfo.h"
  16. #include "llvm/Analysis/TargetLibraryInfo.h"
  17. #include "llvm/Analysis/TargetTransformInfo.h"
  18. #include "llvm/Support/CommandLine.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "inline-order"
  21. enum class InlinePriorityMode : int { Size, Cost, CostBenefit, ML };
  22. static cl::opt<InlinePriorityMode> UseInlinePriority(
  23. "inline-priority-mode", cl::init(InlinePriorityMode::Size), cl::Hidden,
  24. cl::desc("Choose the priority mode to use in module inline"),
  25. cl::values(clEnumValN(InlinePriorityMode::Size, "size",
  26. "Use callee size priority."),
  27. clEnumValN(InlinePriorityMode::Cost, "cost",
  28. "Use inline cost priority."),
  29. clEnumValN(InlinePriorityMode::CostBenefit, "cost-benefit",
  30. "Use cost-benefit ratio."),
  31. clEnumValN(InlinePriorityMode::ML, "ml",
  32. "Use ML.")));
  33. static cl::opt<int> ModuleInlinerTopPriorityThreshold(
  34. "moudle-inliner-top-priority-threshold", cl::Hidden, cl::init(0),
  35. cl::desc("The cost threshold for call sites that get inlined without the "
  36. "cost-benefit analysis"));
  37. namespace {
  38. llvm::InlineCost getInlineCostWrapper(CallBase &CB,
  39. FunctionAnalysisManager &FAM,
  40. const InlineParams &Params) {
  41. Function &Caller = *CB.getCaller();
  42. ProfileSummaryInfo *PSI =
  43. FAM.getResult<ModuleAnalysisManagerFunctionProxy>(Caller)
  44. .getCachedResult<ProfileSummaryAnalysis>(
  45. *CB.getParent()->getParent()->getParent());
  46. auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
  47. auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
  48. return FAM.getResult<AssumptionAnalysis>(F);
  49. };
  50. auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {
  51. return FAM.getResult<BlockFrequencyAnalysis>(F);
  52. };
  53. auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
  54. return FAM.getResult<TargetLibraryAnalysis>(F);
  55. };
  56. Function &Callee = *CB.getCalledFunction();
  57. auto &CalleeTTI = FAM.getResult<TargetIRAnalysis>(Callee);
  58. bool RemarksEnabled =
  59. Callee.getContext().getDiagHandlerPtr()->isMissedOptRemarkEnabled(
  60. DEBUG_TYPE);
  61. return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, GetTLI,
  62. GetBFI, PSI, RemarksEnabled ? &ORE : nullptr);
  63. }
  64. class SizePriority {
  65. public:
  66. SizePriority() = default;
  67. SizePriority(const CallBase *CB, FunctionAnalysisManager &,
  68. const InlineParams &) {
  69. Function *Callee = CB->getCalledFunction();
  70. Size = Callee->getInstructionCount();
  71. }
  72. static bool isMoreDesirable(const SizePriority &P1, const SizePriority &P2) {
  73. return P1.Size < P2.Size;
  74. }
  75. private:
  76. unsigned Size = UINT_MAX;
  77. };
  78. class CostPriority {
  79. public:
  80. CostPriority() = default;
  81. CostPriority(const CallBase *CB, FunctionAnalysisManager &FAM,
  82. const InlineParams &Params) {
  83. auto IC = getInlineCostWrapper(const_cast<CallBase &>(*CB), FAM, Params);
  84. if (IC.isVariable())
  85. Cost = IC.getCost();
  86. else
  87. Cost = IC.isNever() ? INT_MAX : INT_MIN;
  88. }
  89. static bool isMoreDesirable(const CostPriority &P1, const CostPriority &P2) {
  90. return P1.Cost < P2.Cost;
  91. }
  92. private:
  93. int Cost = INT_MAX;
  94. };
  95. class CostBenefitPriority {
  96. public:
  97. CostBenefitPriority() = default;
  98. CostBenefitPriority(const CallBase *CB, FunctionAnalysisManager &FAM,
  99. const InlineParams &Params) {
  100. auto IC = getInlineCostWrapper(const_cast<CallBase &>(*CB), FAM, Params);
  101. Cost = IC.getCost();
  102. StaticBonusApplied = IC.getStaticBonusApplied();
  103. CostBenefit = IC.getCostBenefit();
  104. }
  105. static bool isMoreDesirable(const CostBenefitPriority &P1,
  106. const CostBenefitPriority &P2) {
  107. // We prioritize call sites in the dictionary order of the following
  108. // priorities:
  109. //
  110. // 1. Those call sites that are expected to reduce the caller size when
  111. // inlined. Within them, we prioritize those call sites with bigger
  112. // reduction.
  113. //
  114. // 2. Those call sites that have gone through the cost-benefit analysis.
  115. // Currently, they are limited to hot call sites. Within them, we
  116. // prioritize those call sites with higher benefit-to-cost ratios.
  117. //
  118. // 3. Remaining call sites are prioritized according to their costs.
  119. // We add back StaticBonusApplied to determine whether we expect the caller
  120. // to shrink (even if we don't delete the callee).
  121. bool P1ReducesCallerSize =
  122. P1.Cost + P1.StaticBonusApplied < ModuleInlinerTopPriorityThreshold;
  123. bool P2ReducesCallerSize =
  124. P2.Cost + P2.StaticBonusApplied < ModuleInlinerTopPriorityThreshold;
  125. if (P1ReducesCallerSize || P2ReducesCallerSize) {
  126. // If one reduces the caller size while the other doesn't, then return
  127. // true iff P1 reduces the caller size.
  128. if (P1ReducesCallerSize != P2ReducesCallerSize)
  129. return P1ReducesCallerSize;
  130. // If they both reduce the caller size, pick the one with the smaller
  131. // cost.
  132. return P1.Cost < P2.Cost;
  133. }
  134. bool P1HasCB = P1.CostBenefit.has_value();
  135. bool P2HasCB = P2.CostBenefit.has_value();
  136. if (P1HasCB || P2HasCB) {
  137. // If one has undergone the cost-benefit analysis while the other hasn't,
  138. // then return true iff P1 has.
  139. if (P1HasCB != P2HasCB)
  140. return P1HasCB;
  141. // If they have undergone the cost-benefit analysis, then pick the one
  142. // with a higher benefit-to-cost ratio.
  143. APInt LHS = P1.CostBenefit->getBenefit() * P2.CostBenefit->getCost();
  144. APInt RHS = P2.CostBenefit->getBenefit() * P1.CostBenefit->getCost();
  145. return LHS.ugt(RHS);
  146. }
  147. // Remaining call sites are ordered according to their costs.
  148. return P1.Cost < P2.Cost;
  149. }
  150. private:
  151. int Cost = INT_MAX;
  152. int StaticBonusApplied = 0;
  153. std::optional<CostBenefitPair> CostBenefit;
  154. };
  155. class MLPriority {
  156. public:
  157. MLPriority() = default;
  158. MLPriority(const CallBase *CB, FunctionAnalysisManager &FAM,
  159. const InlineParams &Params) {
  160. auto IC = getInlineCostWrapper(const_cast<CallBase &>(*CB), FAM, Params);
  161. if (IC.isVariable())
  162. Cost = IC.getCost();
  163. else
  164. Cost = IC.isNever() ? INT_MAX : INT_MIN;
  165. }
  166. static bool isMoreDesirable(const MLPriority &P1, const MLPriority &P2) {
  167. return P1.Cost < P2.Cost;
  168. }
  169. private:
  170. int Cost = INT_MAX;
  171. };
  172. template <typename PriorityT>
  173. class PriorityInlineOrder : public InlineOrder<std::pair<CallBase *, int>> {
  174. using T = std::pair<CallBase *, int>;
  175. bool hasLowerPriority(const CallBase *L, const CallBase *R) const {
  176. const auto I1 = Priorities.find(L);
  177. const auto I2 = Priorities.find(R);
  178. assert(I1 != Priorities.end() && I2 != Priorities.end());
  179. return PriorityT::isMoreDesirable(I2->second, I1->second);
  180. }
  181. bool updateAndCheckDecreased(const CallBase *CB) {
  182. auto It = Priorities.find(CB);
  183. const auto OldPriority = It->second;
  184. It->second = PriorityT(CB, FAM, Params);
  185. const auto NewPriority = It->second;
  186. return PriorityT::isMoreDesirable(OldPriority, NewPriority);
  187. }
  188. // A call site could become less desirable for inlining because of the size
  189. // growth from prior inlining into the callee. This method is used to lazily
  190. // update the desirability of a call site if it's decreasing. It is only
  191. // called on pop() or front(), not every time the desirability changes. When
  192. // the desirability of the front call site decreases, an updated one would be
  193. // pushed right back into the heap. For simplicity, those cases where
  194. // the desirability of a call site increases are ignored here.
  195. void adjust() {
  196. while (updateAndCheckDecreased(Heap.front())) {
  197. std::pop_heap(Heap.begin(), Heap.end(), isLess);
  198. std::push_heap(Heap.begin(), Heap.end(), isLess);
  199. }
  200. }
  201. public:
  202. PriorityInlineOrder(FunctionAnalysisManager &FAM, const InlineParams &Params)
  203. : FAM(FAM), Params(Params) {
  204. isLess = [&](const CallBase *L, const CallBase *R) {
  205. return hasLowerPriority(L, R);
  206. };
  207. }
  208. size_t size() override { return Heap.size(); }
  209. void push(const T &Elt) override {
  210. CallBase *CB = Elt.first;
  211. const int InlineHistoryID = Elt.second;
  212. Heap.push_back(CB);
  213. Priorities[CB] = PriorityT(CB, FAM, Params);
  214. std::push_heap(Heap.begin(), Heap.end(), isLess);
  215. InlineHistoryMap[CB] = InlineHistoryID;
  216. }
  217. T pop() override {
  218. assert(size() > 0);
  219. adjust();
  220. CallBase *CB = Heap.front();
  221. T Result = std::make_pair(CB, InlineHistoryMap[CB]);
  222. InlineHistoryMap.erase(CB);
  223. std::pop_heap(Heap.begin(), Heap.end(), isLess);
  224. Heap.pop_back();
  225. return Result;
  226. }
  227. void erase_if(function_ref<bool(T)> Pred) override {
  228. auto PredWrapper = [=](CallBase *CB) -> bool {
  229. return Pred(std::make_pair(CB, 0));
  230. };
  231. llvm::erase_if(Heap, PredWrapper);
  232. std::make_heap(Heap.begin(), Heap.end(), isLess);
  233. }
  234. private:
  235. SmallVector<CallBase *, 16> Heap;
  236. std::function<bool(const CallBase *L, const CallBase *R)> isLess;
  237. DenseMap<CallBase *, int> InlineHistoryMap;
  238. DenseMap<const CallBase *, PriorityT> Priorities;
  239. FunctionAnalysisManager &FAM;
  240. const InlineParams &Params;
  241. };
  242. } // namespace
  243. std::unique_ptr<InlineOrder<std::pair<CallBase *, int>>>
  244. llvm::getInlineOrder(FunctionAnalysisManager &FAM, const InlineParams &Params) {
  245. switch (UseInlinePriority) {
  246. case InlinePriorityMode::Size:
  247. LLVM_DEBUG(dbgs() << " Current used priority: Size priority ---- \n");
  248. return std::make_unique<PriorityInlineOrder<SizePriority>>(FAM, Params);
  249. case InlinePriorityMode::Cost:
  250. LLVM_DEBUG(dbgs() << " Current used priority: Cost priority ---- \n");
  251. return std::make_unique<PriorityInlineOrder<CostPriority>>(FAM, Params);
  252. case InlinePriorityMode::CostBenefit:
  253. LLVM_DEBUG(
  254. dbgs() << " Current used priority: cost-benefit priority ---- \n");
  255. return std::make_unique<PriorityInlineOrder<CostBenefitPriority>>(FAM, Params);
  256. case InlinePriorityMode::ML:
  257. LLVM_DEBUG(
  258. dbgs() << " Current used priority: ML priority ---- \n");
  259. return std::make_unique<PriorityInlineOrder<MLPriority>>(FAM, Params);
  260. }
  261. return nullptr;
  262. }