[PATCH] D78730: [llvm][NFC] Add an explicit 'ComputeFullInlineCost' API

Mircea Trofin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 24 12:26:45 PDT 2020


mtrofin updated this revision to Diff 259958.
mtrofin marked an inline comment as done.
mtrofin added a comment.

feedback


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D78730/new/

https://reviews.llvm.org/D78730

Files:
  llvm/include/llvm/Analysis/InlineCost.h
  llvm/lib/Analysis/InlineCost.cpp


Index: llvm/lib/Analysis/InlineCost.cpp
===================================================================
--- llvm/lib/Analysis/InlineCost.cpp
+++ llvm/lib/Analysis/InlineCost.cpp
@@ -407,7 +407,8 @@
 
 /// FIXME: if it is necessary to derive from InlineCostCallAnalyzer, note
 /// the FIXME in onLoweredCall, when instantiating an InlineCostCallAnalyzer
-class InlineCostCallAnalyzer final : public CallAnalyzer {
+class InlineCostCallAnalyzer : public CallAnalyzer {
+protected:
   const int CostUpperBound = INT_MAX - InlineConstants::InstrCost - 1;
   const bool ComputeFullInlineCost;
   int LoadEliminationCost = 0;
@@ -710,6 +711,60 @@
   int getThreshold() { return Threshold; }
   int getCost() { return Cost; }
 };
+
+/// Calculate inline cost irrespective of thresholds.
+class CompleteInlineCostAnalyzer final : public InlineCostCallAnalyzer {
+public:
+  CompleteInlineCostAnalyzer(
+      const TargetTransformInfo &TTI,
+      std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
+      Optional<function_ref<BlockFrequencyInfo &(Function &)>> &GetBFI,
+      ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE, Function &Callee,
+      CallBase &Call,
+      const InlineParams Params = {/* DefaultThreshold*/ 0,
+                                   /*HintThreshold*/ {},
+                                   /*ColdThreshold*/ {},
+                                   /*OptSizeThreshold*/ {},
+                                   /*OptMinSizeThreshold*/ {},
+                                   /*HotCallSiteThreshold*/ {},
+                                   /*LocallyHotCallSiteThreshold*/ {},
+                                   /*ColdCallSiteThreshold*/ {},
+                                   /* ComputeFullInlineCost*/ true},
+      bool BoostIndirect = true)
+      : InlineCostCallAnalyzer(TTI, GetAssumptionCache, GetBFI, PSI, ORE,
+                               Callee, Call, Params, BoostIndirect) {}
+
+  // Ignore parent's result, and just return success.
+  InlineResult finalizeAnalysis() override {
+    InlineCostCallAnalyzer::finalizeAnalysis();
+    return InlineResult::success();
+  }
+
+  bool shouldStop() override { return false; }
+
+  // Same as parent's onLoweredCall, just that it uses itself internally. Not
+  // worth doing something more elegant, because this implementation will go
+  // away.
+  void onLoweredCall(Function *F, CallBase &Call,
+                     bool IsIndirectCall) override {
+    addCost(Call.arg_size() * InlineConstants::InstrCost);
+
+    if (IsIndirectCall && BoostIndirectCalls) {
+      auto IndirectCallParams = Params;
+      IndirectCallParams.DefaultThreshold =
+          InlineConstants::IndirectCallThreshold;
+      CompleteInlineCostAnalyzer CA(TTI, GetAssumptionCache, GetBFI, PSI, ORE,
+                                    *F, Call, IndirectCallParams, false);
+      if (CA.analyze().isSuccess()) {
+        // We were able to inline the indirect call! Subtract the cost from the
+        // threshold to get the bonus we want to apply, but don't go below zero.
+        Cost -= std::max(0, CA.getThreshold() - CA.getCost());
+      }
+    } else
+      // Otherwise simply add the cost for merely making the call.
+      addCost(InlineConstants::CallPenalty);
+  }
+};
 } // namespace
 
 /// Test whether the given value is an Alloca-derived function argument.
Index: llvm/include/llvm/Analysis/InlineCost.h
===================================================================
--- llvm/include/llvm/Analysis/InlineCost.h
+++ llvm/include/llvm/Analysis/InlineCost.h
@@ -246,6 +246,20 @@
     CallBase &Call, Function *Callee, TargetTransformInfo &CalleeTTI,
     function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
 
+/// Get the cost estimate ignoring thresholds. This is similar to getInlineCost
+/// when passed InlineParams::ComputeFullInlineCost, or a non-null ORE. It
+/// uses default InlineParams otherwise.
+/// Contrary to getInlineCost, which makes a threshold-based final evaluation of
+/// should/shouldn't inline, captured in InlineResult, getInliningCostEstimate 
+/// returns:
+/// - None, if the inlining cannot happen (is illegal)
+/// - an integer, representing the cost.
+Optional<int> getInliningCostEstimate(
+    CallBase &Call, TargetTransformInfo &CalleeTTI,
+    std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
+    Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
+    ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE);
+
 /// Minimal filter to detect invalid constructs for inlining.
 InlineResult isInlineViable(Function &Callee);
 } // namespace llvm


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78730.259958.patch
Type: text/x-patch
Size: 4637 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200424/981ba75e/attachment-0001.bin>


More information about the llvm-commits mailing list