Skip to content

Commit

Permalink
Revert "[llvm][NFC] Cleanup uses of std::function in Inlining-related…
Browse files Browse the repository at this point in the history
… APIs"

This reverts commit 767db5b.
  • Loading branch information
mtrofin committed May 15, 2020
1 parent 767db5b commit 454de99
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 112 deletions.
28 changes: 12 additions & 16 deletions llvm/include/llvm/Analysis/InlineCost.h
Expand Up @@ -216,14 +216,12 @@ int getCallsiteCost(CallBase &Call, const DataLayout &DL);
///
/// Also note that calling this function *dynamically* computes the cost of
/// inlining the callsite. It is an expensive, heavyweight call.
InlineCost
getInlineCost(CallBase &Call, const InlineParams &Params,
TargetTransformInfo &CalleeTTI,
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
ProfileSummaryInfo *PSI = nullptr,
OptimizationRemarkEmitter *ORE = nullptr);
InlineCost getInlineCost(
CallBase &Call, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE = nullptr);

/// Get an InlineCost with the callee explicitly specified.
/// This allows you to calculate the cost of inlining a function via a
Expand All @@ -233,11 +231,10 @@ getInlineCost(CallBase &Call, const InlineParams &Params,
InlineCost
getInlineCost(CallBase &Call, Function *Callee, const InlineParams &Params,
TargetTransformInfo &CalleeTTI,
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
ProfileSummaryInfo *PSI = nullptr,
OptimizationRemarkEmitter *ORE = nullptr);
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE);

/// Returns InlineResult::success() if the call site should be always inlined
/// because of user directives, and the inlining is viable. Returns
Expand All @@ -259,10 +256,9 @@ Optional<InlineResult> getAttributeBasedInliningDecision(
/// - an integer, representing the cost.
Optional<int> getInliningCostEstimate(
CallBase &Call, TargetTransformInfo &CalleeTTI,
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
ProfileSummaryInfo *PSI = nullptr,
OptimizationRemarkEmitter *ORE = nullptr);
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);
Expand Down
14 changes: 7 additions & 7 deletions llvm/include/llvm/Transforms/Utils/Cloning.h
Expand Up @@ -171,19 +171,19 @@ void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
/// the auxiliary results produced by it.
class InlineFunctionInfo {
public:
explicit InlineFunctionInfo(
CallGraph *cg = nullptr,
function_ref<AssumptionCache &(Function &)> GetAssumptionCache = nullptr,
ProfileSummaryInfo *PSI = nullptr,
BlockFrequencyInfo *CallerBFI = nullptr,
BlockFrequencyInfo *CalleeBFI = nullptr)
explicit InlineFunctionInfo(CallGraph *cg = nullptr,
std::function<AssumptionCache &(Function &)>
*GetAssumptionCache = nullptr,
ProfileSummaryInfo *PSI = nullptr,
BlockFrequencyInfo *CallerBFI = nullptr,
BlockFrequencyInfo *CalleeBFI = nullptr)
: CG(cg), GetAssumptionCache(GetAssumptionCache), PSI(PSI),
CallerBFI(CallerBFI), CalleeBFI(CalleeBFI) {}

/// If non-null, InlineFunction will update the callgraph to reflect the
/// changes it makes.
CallGraph *CG;
function_ref<AssumptionCache &(Function &)> GetAssumptionCache;
std::function<AssumptionCache &(Function &)> *GetAssumptionCache;
ProfileSummaryInfo *PSI;
BlockFrequencyInfo *CallerBFI, *CalleeBFI;

Expand Down
10 changes: 7 additions & 3 deletions llvm/lib/Analysis/InlineAdvisor.cpp
Expand Up @@ -99,7 +99,11 @@ DefaultInlineAdvisor::getAdvice(CallBase &CB, FunctionAnalysisManager &FAM) {
*CB.getParent()->getParent()->getParent());

auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
// FIXME: make GetAssumptionCache's decl similar to the other 2 below. May
// need changing the type of getInlineCost parameters? Also see similar case
// in Inliner.cpp
std::function<AssumptionCache &(Function &)> GetAssumptionCache =
[&](Function &F) -> AssumptionCache & {
return FAM.getResult<AssumptionAnalysis>(F);
};
auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {
Expand All @@ -115,8 +119,8 @@ DefaultInlineAdvisor::getAdvice(CallBase &CB, FunctionAnalysisManager &FAM) {
bool RemarksEnabled =
Callee.getContext().getDiagHandlerPtr()->isMissedOptRemarkEnabled(
DEBUG_TYPE);
return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, GetTLI,
GetBFI, PSI, RemarksEnabled ? &ORE : nullptr);
return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, {GetBFI},
GetTLI, PSI, RemarksEnabled ? &ORE : nullptr);
};
auto OIC = llvm::shouldInline(CB, GetInlineCost, ORE);
return std::make_unique<DefaultInlineAdvice>(this, CB, OIC, ORE);
Expand Down
56 changes: 27 additions & 29 deletions llvm/lib/Analysis/InlineCost.cpp
Expand Up @@ -148,10 +148,10 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
const TargetTransformInfo &TTI;

/// Getter for the cache of @llvm.assume intrinsics.
function_ref<AssumptionCache &(Function &)> GetAssumptionCache;
std::function<AssumptionCache &(Function &)> &GetAssumptionCache;

/// Getter for BlockFrequencyInfo
function_ref<BlockFrequencyInfo &(Function &)> GetBFI;
Optional<function_ref<BlockFrequencyInfo &(Function &)>> &GetBFI;

/// Profile summary information.
ProfileSummaryInfo *PSI;
Expand Down Expand Up @@ -382,12 +382,11 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
bool visitUnreachableInst(UnreachableInst &I);

public:
CallAnalyzer(
Function &Callee, CallBase &Call, const TargetTransformInfo &TTI,
const std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
ProfileSummaryInfo *PSI = nullptr,
OptimizationRemarkEmitter *ORE = nullptr)
CallAnalyzer(const TargetTransformInfo &TTI,
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
Optional<function_ref<BlockFrequencyInfo &(Function &)>> &GetBFI,
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE,
Function &Callee, CallBase &Call)
: TTI(TTI), GetAssumptionCache(GetAssumptionCache), GetBFI(GetBFI),
PSI(PSI), F(Callee), DL(F.getParent()->getDataLayout()), ORE(ORE),
CandidateCall(Call), EnableLoadElimination(true) {}
Expand Down Expand Up @@ -505,8 +504,8 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
InlineConstants::IndirectCallThreshold;
/// FIXME: if InlineCostCallAnalyzer is derived from, this may need
/// to instantiate the derived class.
InlineCostCallAnalyzer CA(*F, Call, IndirectCallParams, TTI,
GetAssumptionCache, GetBFI, PSI, ORE, false);
InlineCostCallAnalyzer 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.
Expand Down Expand Up @@ -694,14 +693,13 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {

public:
InlineCostCallAnalyzer(
Function &Callee, CallBase &Call, const InlineParams &Params,
const TargetTransformInfo &TTI,
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
ProfileSummaryInfo *PSI = nullptr,
OptimizationRemarkEmitter *ORE = nullptr, bool BoostIndirect = true,
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,
bool IgnoreThreshold = false)
: CallAnalyzer(Callee, Call, TTI, GetAssumptionCache, GetBFI, PSI, ORE),
: CallAnalyzer(TTI, GetAssumptionCache, GetBFI, PSI, ORE, Callee, Call),
ComputeFullInlineCost(OptComputeFullInlineCost ||
Params.ComputeFullInlineCost || ORE),
Params(Params), Threshold(Params.DefaultThreshold),
Expand Down Expand Up @@ -1300,7 +1298,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");
Expand Down Expand Up @@ -1767,7 +1765,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);

Expand Down Expand Up @@ -2221,18 +2219,18 @@ int llvm::getCallsiteCost(CallBase &Call, const DataLayout &DL) {

InlineCost llvm::getInlineCost(
CallBase &Call, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
return getInlineCost(Call, Call.getCalledFunction(), Params, CalleeTTI,
GetAssumptionCache, GetTLI, GetBFI, PSI, ORE);
GetAssumptionCache, GetBFI, GetTLI, PSI, ORE);
}

Optional<int> llvm::getInliningCostEstimate(
CallBase &Call, TargetTransformInfo &CalleeTTI,
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
const InlineParams Params = {/* DefaultThreshold*/ 0,
/*HintThreshold*/ {},
Expand All @@ -2244,8 +2242,8 @@ Optional<int> llvm::getInliningCostEstimate(
/*ColdCallSiteThreshold*/ {},
/* ComputeFullInlineCost*/ true};

InlineCostCallAnalyzer CA(*Call.getCalledFunction(), Call, Params, CalleeTTI,
GetAssumptionCache, GetBFI, PSI, ORE, true,
InlineCostCallAnalyzer CA(CalleeTTI, GetAssumptionCache, GetBFI, PSI, ORE,
*Call.getCalledFunction(), Call, Params, true,
/*IgnoreThreshold*/ true);
auto R = CA.analyze();
if (!R.isSuccess())
Expand Down Expand Up @@ -2317,9 +2315,9 @@ Optional<InlineResult> llvm::getAttributeBasedInliningDecision(
InlineCost llvm::getInlineCost(
CallBase &Call, Function *Callee, const InlineParams &Params,
TargetTransformInfo &CalleeTTI,
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {

auto UserDecision =
Expand All @@ -2335,8 +2333,8 @@ InlineCost llvm::getInlineCost(
<< "... (caller:" << Call.getCaller()->getName()
<< ")\n");

InlineCostCallAnalyzer CA(*Callee, Call, Params, CalleeTTI,
GetAssumptionCache, GetBFI, PSI, ORE);
InlineCostCallAnalyzer CA(CalleeTTI, GetAssumptionCache, GetBFI, PSI, ORE,
*Callee, Call, Params);
InlineResult ShouldInline = CA.analyze();

LLVM_DEBUG(CA.dump());
Expand Down
9 changes: 5 additions & 4 deletions llvm/lib/Target/AMDGPU/AMDGPUInline.cpp
Expand Up @@ -208,13 +208,14 @@ InlineCost AMDGPUInliner::getInlineCost(CallBase &CB) {
}

OptimizationRemarkEmitter ORE(Caller);
auto GetAssumptionCache = [this](Function &F) -> AssumptionCache & {
std::function<AssumptionCache &(Function &)> GetAssumptionCache =
[this](Function &F) -> AssumptionCache & {
return ACT->getAssumptionCache(F);
};

auto IC = llvm::getInlineCost(CB, Callee, LocalParams, TTI,
GetAssumptionCache, GetTLI, nullptr, PSI,
RemarksEnabled ? &ORE : nullptr);
auto IC =
llvm::getInlineCost(CB, Callee, LocalParams, TTI, GetAssumptionCache,
None, GetTLI, PSI, RemarksEnabled ? &ORE : nullptr);

if (IC && !IC.isAlways() && !Callee->hasFnAttribute(Attribute::InlineHint)) {
// Single BB does not increase total BB amount, thus subtract 1
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/IPO/AlwaysInliner.cpp
Expand Up @@ -36,10 +36,11 @@ PreservedAnalyses AlwaysInlinerPass::run(Module &M,
// Add inline assumptions during code generation.
FunctionAnalysisManager &FAM =
MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
std::function<AssumptionCache &(Function &)> GetAssumptionCache =
[&](Function &F) -> AssumptionCache & {
return FAM.getResult<AssumptionAnalysis>(F);
};
InlineFunctionInfo IFI(/*cg=*/nullptr, GetAssumptionCache);
InlineFunctionInfo IFI(/*cg=*/nullptr, &GetAssumptionCache);

SmallSetVector<CallBase *, 16> Calls;
bool Changed = false;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/IPO/InlineSimple.cpp
Expand Up @@ -68,8 +68,8 @@ class SimpleInliner : public LegacyInlinerBase {
[&](Function &F) -> AssumptionCache & {
return ACT->getAssumptionCache(F);
};
return llvm::getInlineCost(CB, Params, TTI, GetAssumptionCache, GetTLI,
/*GetBFI=*/nullptr, PSI,
return llvm::getInlineCost(CB, Params, TTI, GetAssumptionCache,
/*GetBFI=*/None, GetTLI, PSI,
RemarksEnabled ? &ORE : nullptr);
}

Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/Transforms/IPO/Inliner.cpp
Expand Up @@ -395,7 +395,7 @@ inlineCallsImpl(CallGraphSCC &SCC, CallGraph &CG,
std::swap(CallSites[I--], CallSites[--FirstCallInSCC]);

InlinedArrayAllocasTy InlinedArrayAllocas;
InlineFunctionInfo InlineInfo(&CG, GetAssumptionCache, PSI);
InlineFunctionInfo InlineInfo(&CG, &GetAssumptionCache, PSI);

// Now that we have all of the call sites, loop over them and inline them if
// it looks profitable to do so.
Expand Down Expand Up @@ -804,7 +804,8 @@ PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,

LLVM_DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n");

auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
std::function<AssumptionCache &(Function &)> GetAssumptionCache =
[&](Function &F) -> AssumptionCache & {
return FAM.getResult<AssumptionAnalysis>(F);
};

Expand Down Expand Up @@ -848,7 +849,7 @@ PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
// Setup the data structure used to plumb customization into the
// `InlineFunction` routine.
InlineFunctionInfo IFI(
/*cg=*/nullptr, GetAssumptionCache, PSI,
/*cg=*/nullptr, &GetAssumptionCache, PSI,
&FAM.getResult<BlockFrequencyAnalysis>(*(CB->getCaller())),
&FAM.getResult<BlockFrequencyAnalysis>(Callee));

Expand Down

0 comments on commit 454de99

Please sign in to comment.