diff options
author | Mircea Trofin <mtrofin@google.com> | 2020-05-14 22:38:41 -0700 |
---|---|---|
committer | Mircea Trofin <mtrofin@google.com> | 2020-05-15 12:29:16 -0700 |
commit | 08e2386dee3f51c0085f87ca909f533681609bd0 (patch) | |
tree | 178bdbb195cf2dac2a41f8b6ab4a139778b5b31a /llvm/lib/Analysis/InlineCost.cpp | |
parent | 11aa3707e30fe2dae214e1299b951fe908def14c (diff) | |
download | llvm-08e2386dee3f51c0085f87ca909f533681609bd0.zip llvm-08e2386dee3f51c0085f87ca909f533681609bd0.tar.gz llvm-08e2386dee3f51c0085f87ca909f533681609bd0.tar.bz2 |
Revert "Revert "[llvm][NFC] Cleanup uses of std::function in Inlining-related APIs""
This reverts commit 454de99a6fec705e76ed7743bf538f7a77296f59.
The problem was that one of the ctor arguments of CallAnalyzer was left
to be const std::function<>&. A function_ref was passed for it, and then
the ctor stored the value in a function_ref field. So a std::function<>
would be created as a temporary, and not survive past the ctor
invocation, while the field would.
Tested locally by following https://github.com/google/sanitizers/wiki/SanitizerBotReproduceBuild
Original Differential Revision: https://reviews.llvm.org/D79917
Diffstat (limited to 'llvm/lib/Analysis/InlineCost.cpp')
-rw-r--r-- | llvm/lib/Analysis/InlineCost.cpp | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp index ce9f030..7377b088 100644 --- a/llvm/lib/Analysis/InlineCost.cpp +++ b/llvm/lib/Analysis/InlineCost.cpp @@ -138,6 +138,14 @@ public: formatted_raw_ostream &OS); }; +/// Carry out call site analysis, in order to evaluate inlinability. +/// NOTE: the type is currently used as implementation detail of functions such +/// as llvm::getInlineCost. Note the function_ref constructor parameters - the +/// expectation is that they come from the outer scope, from the wrapper +/// functions. If we want to support constructing CallAnalyzer objects where +/// lambdas are provided inline at construction, or where the object needs to +/// otherwise survive past the scope of the provided functions, we need to +/// revisit the argument types. class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> { typedef InstVisitor<CallAnalyzer, bool> Base; friend class InstVisitor<CallAnalyzer, bool>; @@ -148,10 +156,10 @@ protected: const TargetTransformInfo &TTI; /// Getter for the cache of @llvm.assume intrinsics. - std::function<AssumptionCache &(Function &)> &GetAssumptionCache; + function_ref<AssumptionCache &(Function &)> GetAssumptionCache; /// Getter for BlockFrequencyInfo - Optional<function_ref<BlockFrequencyInfo &(Function &)>> &GetBFI; + function_ref<BlockFrequencyInfo &(Function &)> GetBFI; /// Profile summary information. ProfileSummaryInfo *PSI; @@ -382,11 +390,12 @@ protected: bool visitUnreachableInst(UnreachableInst &I); public: - CallAnalyzer(const TargetTransformInfo &TTI, - std::function<AssumptionCache &(Function &)> &GetAssumptionCache, - Optional<function_ref<BlockFrequencyInfo &(Function &)>> &GetBFI, - ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE, - Function &Callee, CallBase &Call) + CallAnalyzer( + Function &Callee, CallBase &Call, const TargetTransformInfo &TTI, + function_ref<AssumptionCache &(Function &)> GetAssumptionCache, + function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr, + ProfileSummaryInfo *PSI = nullptr, + OptimizationRemarkEmitter *ORE = nullptr) : TTI(TTI), GetAssumptionCache(GetAssumptionCache), GetBFI(GetBFI), PSI(PSI), F(Callee), DL(F.getParent()->getDataLayout()), ORE(ORE), CandidateCall(Call), EnableLoadElimination(true) {} @@ -504,8 +513,8 @@ class InlineCostCallAnalyzer final : public CallAnalyzer { InlineConstants::IndirectCallThreshold; /// FIXME: if InlineCostCallAnalyzer is derived from, this may need /// to instantiate the derived class. - InlineCostCallAnalyzer CA(TTI, GetAssumptionCache, GetBFI, PSI, ORE, *F, - Call, IndirectCallParams, false); + InlineCostCallAnalyzer CA(*F, Call, IndirectCallParams, TTI, + GetAssumptionCache, GetBFI, PSI, ORE, 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. @@ -693,13 +702,14 @@ class InlineCostCallAnalyzer final : public CallAnalyzer { public: InlineCostCallAnalyzer( + Function &Callee, CallBase &Call, const InlineParams &Params, 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, bool BoostIndirect = true, + function_ref<AssumptionCache &(Function &)> GetAssumptionCache, + function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr, + ProfileSummaryInfo *PSI = nullptr, + OptimizationRemarkEmitter *ORE = nullptr, bool BoostIndirect = true, bool IgnoreThreshold = false) - : CallAnalyzer(TTI, GetAssumptionCache, GetBFI, PSI, ORE, Callee, Call), + : CallAnalyzer(Callee, Call, TTI, GetAssumptionCache, GetBFI, PSI, ORE), ComputeFullInlineCost(OptComputeFullInlineCost || Params.ComputeFullInlineCost || ORE), Params(Params), Threshold(Params.DefaultThreshold), @@ -1298,7 +1308,7 @@ void InlineCostCallAnalyzer::updateThreshold(CallBase &Call, Function &Callee) { // Callsite hotness and coldness can be determined if sample profile is // used (which adds hotness metadata to calls) or if caller's // BlockFrequencyInfo is available. - BlockFrequencyInfo *CallerBFI = GetBFI ? &((*GetBFI)(*Caller)) : nullptr; + BlockFrequencyInfo *CallerBFI = GetBFI ? &(GetBFI(*Caller)) : nullptr; auto HotCallSiteThreshold = getHotCallSiteThreshold(Call, CallerBFI); if (!Caller->hasOptSize() && HotCallSiteThreshold) { LLVM_DEBUG(dbgs() << "Hot callsite.\n"); @@ -1765,7 +1775,7 @@ bool CallAnalyzer::visitSwitchInst(SwitchInst &SI) { // does not (yet) fire. unsigned JumpTableSize = 0; - BlockFrequencyInfo *BFI = GetBFI ? &((*GetBFI)(F)) : nullptr; + BlockFrequencyInfo *BFI = GetBFI ? &(GetBFI(F)) : nullptr; unsigned NumCaseCluster = TTI.getEstimatedNumberOfCaseClusters(SI, JumpTableSize, PSI, BFI); @@ -2219,18 +2229,18 @@ int llvm::getCallsiteCost(CallBase &Call, const DataLayout &DL) { InlineCost llvm::getInlineCost( CallBase &Call, const InlineParams &Params, TargetTransformInfo &CalleeTTI, - std::function<AssumptionCache &(Function &)> &GetAssumptionCache, - Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI, + function_ref<AssumptionCache &(Function &)> GetAssumptionCache, function_ref<const TargetLibraryInfo &(Function &)> GetTLI, + function_ref<BlockFrequencyInfo &(Function &)> GetBFI, ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) { return getInlineCost(Call, Call.getCalledFunction(), Params, CalleeTTI, - GetAssumptionCache, GetBFI, GetTLI, PSI, ORE); + GetAssumptionCache, GetTLI, GetBFI, PSI, ORE); } Optional<int> llvm::getInliningCostEstimate( CallBase &Call, TargetTransformInfo &CalleeTTI, - std::function<AssumptionCache &(Function &)> &GetAssumptionCache, - Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI, + function_ref<AssumptionCache &(Function &)> GetAssumptionCache, + function_ref<BlockFrequencyInfo &(Function &)> GetBFI, ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) { const InlineParams Params = {/* DefaultThreshold*/ 0, /*HintThreshold*/ {}, @@ -2242,8 +2252,8 @@ Optional<int> llvm::getInliningCostEstimate( /*ColdCallSiteThreshold*/ {}, /* ComputeFullInlineCost*/ true}; - InlineCostCallAnalyzer CA(CalleeTTI, GetAssumptionCache, GetBFI, PSI, ORE, - *Call.getCalledFunction(), Call, Params, true, + InlineCostCallAnalyzer CA(*Call.getCalledFunction(), Call, Params, CalleeTTI, + GetAssumptionCache, GetBFI, PSI, ORE, true, /*IgnoreThreshold*/ true); auto R = CA.analyze(); if (!R.isSuccess()) @@ -2315,9 +2325,9 @@ Optional<InlineResult> llvm::getAttributeBasedInliningDecision( InlineCost llvm::getInlineCost( CallBase &Call, Function *Callee, const InlineParams &Params, TargetTransformInfo &CalleeTTI, - std::function<AssumptionCache &(Function &)> &GetAssumptionCache, - Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI, + function_ref<AssumptionCache &(Function &)> GetAssumptionCache, function_ref<const TargetLibraryInfo &(Function &)> GetTLI, + function_ref<BlockFrequencyInfo &(Function &)> GetBFI, ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) { auto UserDecision = @@ -2333,8 +2343,8 @@ InlineCost llvm::getInlineCost( << "... (caller:" << Call.getCaller()->getName() << ")\n"); - InlineCostCallAnalyzer CA(CalleeTTI, GetAssumptionCache, GetBFI, PSI, ORE, - *Callee, Call, Params); + InlineCostCallAnalyzer CA(*Callee, Call, Params, CalleeTTI, + GetAssumptionCache, GetBFI, PSI, ORE); InlineResult ShouldInline = CA.analyze(); LLVM_DEBUG(CA.dump()); |