[PATCH] D104972: templatize priorityinlineorder

Liqiang Tao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 26 01:47:03 PDT 2021


taolq created this revision.
Herald added subscribers: ChuanqiXu, ormris, hiraditya, eraman.
taolq requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104972

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
@@ -724,23 +724,12 @@
   size_t FirstIndex = 0;
 };
 
+template <typename Priority>
 class PriorityInlineOrder : public InlineOrder<std::pair<CallBase *, int>> {
-  using T = std::pair<CallBase *, int>;
+  using T = std::pair<CallBase *, Priority>;
   using reference = T &;
   using const_reference = const T &;
 
-  // Return true if S1 is more desirable than S2.
-  static bool isMoreDesirable(int S1, int S2) { return S1 < S2; }
-
-  static bool cmp(const T &P1, const T &P2) {
-    return isMoreDesirable(P2.second, P1.second);
-  }
-
-  int evaluate(CallBase *CB) {
-    Function *Callee = CB->getCalledFunction();
-    return (int)Callee->getInstructionCount();
-  }
-
   // A call site could become less desirable for inlining because of the size
   // growth from prior inlining into the callee. This method is used to lazily
   // update the desirability of a call site if it's decreasing. It is only
@@ -753,8 +742,8 @@
     do {
       CallBase *CB = Heap.front().first;
       const int PreviousGoodness = Heap.front().second;
-      const int CurrentGoodness = evaluate(CB);
-      Changed = isMoreDesirable(PreviousGoodness, CurrentGoodness);
+      const int CurrentGoodness = Evaluate(CB);
+      Changed = IsMoreDesirable(PreviousGoodness, CurrentGoodness);
       if (Changed) {
         std::pop_heap(Heap.begin(), Heap.end(), cmp);
         Heap.pop_back();
@@ -765,12 +754,20 @@
   }
 
 public:
+  PriorityInlineOrder(std::function<bool(Priority, Priority)> IsMoreDesirable,
+                      std::function<Priority(CallBase *)> Evaluate)
+      : IsMoreDesirable(IsMoreDesirable), Evaluate(Evaluate) {
+    cmp = [=](const T &P1, const T &P2) -> bool {
+      return IsMoreDesirable(P2.second, P1.second);
+    };
+  }
+
   size_t size() override { return Heap.size(); }
 
   void push(const T &Elt) override {
     CallBase *CB = Elt.first;
     const int InlineHistoryID = Elt.second;
-    const int Goodness = evaluate(CB);
+    const int Goodness = Evaluate(CB);
 
     Heap.push_back({CB, Goodness});
     std::push_heap(Heap.begin(), Heap.end(), cmp);
@@ -805,6 +802,9 @@
 private:
   SmallVector<T, 16> Heap;
   DenseMap<CallBase *, int> InlineHistoryMap;
+  std::function<bool(const T &, const T &)> cmp;
+  std::function<bool(Priority, Priority)> IsMoreDesirable;
+  std::function<Priority(CallBase *)> Evaluate;
 };
 
 PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
@@ -853,9 +853,16 @@
   // and eventually they all become too large to inline, rather than
   // incrementally maknig a single function grow in a super linear fashion.
   std::unique_ptr<InlineOrder<std::pair<CallBase *, int>>> Calls;
-  if (InlineEnablePriorityOrder)
-    Calls = std::make_unique<PriorityInlineOrder>();
-  else
+  if (InlineEnablePriorityOrder) {
+    auto IsMoreDesirable = [](int S1, int S2) { return S1 < S2; };
+    auto Evaluate = [](CallBase *CB) {
+      Function *Callee = CB->getCalledFunction();
+      return (int)Callee->getInstructionCount();
+    };
+
+    Calls =
+        std::make_unique<PriorityInlineOrder<int>>(IsMoreDesirable, Evaluate);
+  } else
     Calls = std::make_unique<DefaultInlineOrder<std::pair<CallBase *, int>>>();
   assert(Calls != nullptr && "Expected an initialized InlineOrder");
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104972.354654.patch
Type: text/x-patch
Size: 3455 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210626/455a2845/attachment.bin>


More information about the llvm-commits mailing list