Skip to content

Commit 454de99

Browse files
committed
Revert "[llvm][NFC] Cleanup uses of std::function in Inlining-related APIs"
This reverts commit 767db5b.
1 parent 767db5b commit 454de99

File tree

11 files changed

+121
-112
lines changed

11 files changed

+121
-112
lines changed

llvm/include/llvm/Analysis/InlineCost.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,12 @@ int getCallsiteCost(CallBase &Call, const DataLayout &DL);
216216
///
217217
/// Also note that calling this function *dynamically* computes the cost of
218218
/// inlining the callsite. It is an expensive, heavyweight call.
219-
InlineCost
220-
getInlineCost(CallBase &Call, const InlineParams &Params,
221-
TargetTransformInfo &CalleeTTI,
222-
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
223-
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
224-
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
225-
ProfileSummaryInfo *PSI = nullptr,
226-
OptimizationRemarkEmitter *ORE = nullptr);
219+
InlineCost getInlineCost(
220+
CallBase &Call, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
221+
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
222+
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
223+
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
224+
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE = nullptr);
227225

228226
/// Get an InlineCost with the callee explicitly specified.
229227
/// This allows you to calculate the cost of inlining a function via a
@@ -233,11 +231,10 @@ getInlineCost(CallBase &Call, const InlineParams &Params,
233231
InlineCost
234232
getInlineCost(CallBase &Call, Function *Callee, const InlineParams &Params,
235233
TargetTransformInfo &CalleeTTI,
236-
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
234+
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
235+
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
237236
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
238-
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
239-
ProfileSummaryInfo *PSI = nullptr,
240-
OptimizationRemarkEmitter *ORE = nullptr);
237+
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE);
241238

242239
/// Returns InlineResult::success() if the call site should be always inlined
243240
/// because of user directives, and the inlining is viable. Returns
@@ -259,10 +256,9 @@ Optional<InlineResult> getAttributeBasedInliningDecision(
259256
/// - an integer, representing the cost.
260257
Optional<int> getInliningCostEstimate(
261258
CallBase &Call, TargetTransformInfo &CalleeTTI,
262-
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
263-
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
264-
ProfileSummaryInfo *PSI = nullptr,
265-
OptimizationRemarkEmitter *ORE = nullptr);
259+
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
260+
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
261+
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE);
266262

267263
/// Minimal filter to detect invalid constructs for inlining.
268264
InlineResult isInlineViable(Function &Callee);

llvm/include/llvm/Transforms/Utils/Cloning.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,19 @@ void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
171171
/// the auxiliary results produced by it.
172172
class InlineFunctionInfo {
173173
public:
174-
explicit InlineFunctionInfo(
175-
CallGraph *cg = nullptr,
176-
function_ref<AssumptionCache &(Function &)> GetAssumptionCache = nullptr,
177-
ProfileSummaryInfo *PSI = nullptr,
178-
BlockFrequencyInfo *CallerBFI = nullptr,
179-
BlockFrequencyInfo *CalleeBFI = nullptr)
174+
explicit InlineFunctionInfo(CallGraph *cg = nullptr,
175+
std::function<AssumptionCache &(Function &)>
176+
*GetAssumptionCache = nullptr,
177+
ProfileSummaryInfo *PSI = nullptr,
178+
BlockFrequencyInfo *CallerBFI = nullptr,
179+
BlockFrequencyInfo *CalleeBFI = nullptr)
180180
: CG(cg), GetAssumptionCache(GetAssumptionCache), PSI(PSI),
181181
CallerBFI(CallerBFI), CalleeBFI(CalleeBFI) {}
182182

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

llvm/lib/Analysis/InlineAdvisor.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ DefaultInlineAdvisor::getAdvice(CallBase &CB, FunctionAnalysisManager &FAM) {
9999
*CB.getParent()->getParent()->getParent());
100100

101101
auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
102-
auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
102+
// FIXME: make GetAssumptionCache's decl similar to the other 2 below. May
103+
// need changing the type of getInlineCost parameters? Also see similar case
104+
// in Inliner.cpp
105+
std::function<AssumptionCache &(Function &)> GetAssumptionCache =
106+
[&](Function &F) -> AssumptionCache & {
103107
return FAM.getResult<AssumptionAnalysis>(F);
104108
};
105109
auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {
@@ -115,8 +119,8 @@ DefaultInlineAdvisor::getAdvice(CallBase &CB, FunctionAnalysisManager &FAM) {
115119
bool RemarksEnabled =
116120
Callee.getContext().getDiagHandlerPtr()->isMissedOptRemarkEnabled(
117121
DEBUG_TYPE);
118-
return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, GetTLI,
119-
GetBFI, PSI, RemarksEnabled ? &ORE : nullptr);
122+
return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, {GetBFI},
123+
GetTLI, PSI, RemarksEnabled ? &ORE : nullptr);
120124
};
121125
auto OIC = llvm::shouldInline(CB, GetInlineCost, ORE);
122126
return std::make_unique<DefaultInlineAdvice>(this, CB, OIC, ORE);

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
148148
const TargetTransformInfo &TTI;
149149

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

153153
/// Getter for BlockFrequencyInfo
154-
function_ref<BlockFrequencyInfo &(Function &)> GetBFI;
154+
Optional<function_ref<BlockFrequencyInfo &(Function &)>> &GetBFI;
155155

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

384384
public:
385-
CallAnalyzer(
386-
Function &Callee, CallBase &Call, const TargetTransformInfo &TTI,
387-
const std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
388-
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
389-
ProfileSummaryInfo *PSI = nullptr,
390-
OptimizationRemarkEmitter *ORE = nullptr)
385+
CallAnalyzer(const TargetTransformInfo &TTI,
386+
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
387+
Optional<function_ref<BlockFrequencyInfo &(Function &)>> &GetBFI,
388+
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE,
389+
Function &Callee, CallBase &Call)
391390
: TTI(TTI), GetAssumptionCache(GetAssumptionCache), GetBFI(GetBFI),
392391
PSI(PSI), F(Callee), DL(F.getParent()->getDataLayout()), ORE(ORE),
393392
CandidateCall(Call), EnableLoadElimination(true) {}
@@ -505,8 +504,8 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
505504
InlineConstants::IndirectCallThreshold;
506505
/// FIXME: if InlineCostCallAnalyzer is derived from, this may need
507506
/// to instantiate the derived class.
508-
InlineCostCallAnalyzer CA(*F, Call, IndirectCallParams, TTI,
509-
GetAssumptionCache, GetBFI, PSI, ORE, false);
507+
InlineCostCallAnalyzer CA(TTI, GetAssumptionCache, GetBFI, PSI, ORE, *F,
508+
Call, IndirectCallParams, false);
510509
if (CA.analyze().isSuccess()) {
511510
// We were able to inline the indirect call! Subtract the cost from the
512511
// threshold to get the bonus we want to apply, but don't go below zero.
@@ -694,14 +693,13 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
694693

695694
public:
696695
InlineCostCallAnalyzer(
697-
Function &Callee, CallBase &Call, const InlineParams &Params,
698696
const TargetTransformInfo &TTI,
699-
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
700-
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
701-
ProfileSummaryInfo *PSI = nullptr,
702-
OptimizationRemarkEmitter *ORE = nullptr, bool BoostIndirect = true,
697+
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
698+
Optional<function_ref<BlockFrequencyInfo &(Function &)>> &GetBFI,
699+
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE, Function &Callee,
700+
CallBase &Call, const InlineParams &Params, bool BoostIndirect = true,
703701
bool IgnoreThreshold = false)
704-
: CallAnalyzer(Callee, Call, TTI, GetAssumptionCache, GetBFI, PSI, ORE),
702+
: CallAnalyzer(TTI, GetAssumptionCache, GetBFI, PSI, ORE, Callee, Call),
705703
ComputeFullInlineCost(OptComputeFullInlineCost ||
706704
Params.ComputeFullInlineCost || ORE),
707705
Params(Params), Threshold(Params.DefaultThreshold),
@@ -1300,7 +1298,7 @@ void InlineCostCallAnalyzer::updateThreshold(CallBase &Call, Function &Callee) {
13001298
// Callsite hotness and coldness can be determined if sample profile is
13011299
// used (which adds hotness metadata to calls) or if caller's
13021300
// BlockFrequencyInfo is available.
1303-
BlockFrequencyInfo *CallerBFI = GetBFI ? &(GetBFI(*Caller)) : nullptr;
1301+
BlockFrequencyInfo *CallerBFI = GetBFI ? &((*GetBFI)(*Caller)) : nullptr;
13041302
auto HotCallSiteThreshold = getHotCallSiteThreshold(Call, CallerBFI);
13051303
if (!Caller->hasOptSize() && HotCallSiteThreshold) {
13061304
LLVM_DEBUG(dbgs() << "Hot callsite.\n");
@@ -1767,7 +1765,7 @@ bool CallAnalyzer::visitSwitchInst(SwitchInst &SI) {
17671765
// does not (yet) fire.
17681766

17691767
unsigned JumpTableSize = 0;
1770-
BlockFrequencyInfo *BFI = GetBFI ? &(GetBFI(F)) : nullptr;
1768+
BlockFrequencyInfo *BFI = GetBFI ? &((*GetBFI)(F)) : nullptr;
17711769
unsigned NumCaseCluster =
17721770
TTI.getEstimatedNumberOfCaseClusters(SI, JumpTableSize, PSI, BFI);
17731771

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

22222220
InlineCost llvm::getInlineCost(
22232221
CallBase &Call, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
2224-
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
2222+
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
2223+
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
22252224
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
2226-
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
22272225
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
22282226
return getInlineCost(Call, Call.getCalledFunction(), Params, CalleeTTI,
2229-
GetAssumptionCache, GetTLI, GetBFI, PSI, ORE);
2227+
GetAssumptionCache, GetBFI, GetTLI, PSI, ORE);
22302228
}
22312229

22322230
Optional<int> llvm::getInliningCostEstimate(
22332231
CallBase &Call, TargetTransformInfo &CalleeTTI,
2234-
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
2235-
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
2232+
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
2233+
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
22362234
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
22372235
const InlineParams Params = {/* DefaultThreshold*/ 0,
22382236
/*HintThreshold*/ {},
@@ -2244,8 +2242,8 @@ Optional<int> llvm::getInliningCostEstimate(
22442242
/*ColdCallSiteThreshold*/ {},
22452243
/* ComputeFullInlineCost*/ true};
22462244

2247-
InlineCostCallAnalyzer CA(*Call.getCalledFunction(), Call, Params, CalleeTTI,
2248-
GetAssumptionCache, GetBFI, PSI, ORE, true,
2245+
InlineCostCallAnalyzer CA(CalleeTTI, GetAssumptionCache, GetBFI, PSI, ORE,
2246+
*Call.getCalledFunction(), Call, Params, true,
22492247
/*IgnoreThreshold*/ true);
22502248
auto R = CA.analyze();
22512249
if (!R.isSuccess())
@@ -2317,9 +2315,9 @@ Optional<InlineResult> llvm::getAttributeBasedInliningDecision(
23172315
InlineCost llvm::getInlineCost(
23182316
CallBase &Call, Function *Callee, const InlineParams &Params,
23192317
TargetTransformInfo &CalleeTTI,
2320-
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
2318+
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
2319+
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
23212320
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
2322-
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
23232321
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
23242322

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

2338-
InlineCostCallAnalyzer CA(*Callee, Call, Params, CalleeTTI,
2339-
GetAssumptionCache, GetBFI, PSI, ORE);
2336+
InlineCostCallAnalyzer CA(CalleeTTI, GetAssumptionCache, GetBFI, PSI, ORE,
2337+
*Callee, Call, Params);
23402338
InlineResult ShouldInline = CA.analyze();
23412339

23422340
LLVM_DEBUG(CA.dump());

llvm/lib/Target/AMDGPU/AMDGPUInline.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,14 @@ InlineCost AMDGPUInliner::getInlineCost(CallBase &CB) {
208208
}
209209

210210
OptimizationRemarkEmitter ORE(Caller);
211-
auto GetAssumptionCache = [this](Function &F) -> AssumptionCache & {
211+
std::function<AssumptionCache &(Function &)> GetAssumptionCache =
212+
[this](Function &F) -> AssumptionCache & {
212213
return ACT->getAssumptionCache(F);
213214
};
214215

215-
auto IC = llvm::getInlineCost(CB, Callee, LocalParams, TTI,
216-
GetAssumptionCache, GetTLI, nullptr, PSI,
217-
RemarksEnabled ? &ORE : nullptr);
216+
auto IC =
217+
llvm::getInlineCost(CB, Callee, LocalParams, TTI, GetAssumptionCache,
218+
None, GetTLI, PSI, RemarksEnabled ? &ORE : nullptr);
218219

219220
if (IC && !IC.isAlways() && !Callee->hasFnAttribute(Attribute::InlineHint)) {
220221
// Single BB does not increase total BB amount, thus subtract 1

llvm/lib/Transforms/IPO/AlwaysInliner.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ PreservedAnalyses AlwaysInlinerPass::run(Module &M,
3636
// Add inline assumptions during code generation.
3737
FunctionAnalysisManager &FAM =
3838
MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
39-
auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
39+
std::function<AssumptionCache &(Function &)> GetAssumptionCache =
40+
[&](Function &F) -> AssumptionCache & {
4041
return FAM.getResult<AssumptionAnalysis>(F);
4142
};
42-
InlineFunctionInfo IFI(/*cg=*/nullptr, GetAssumptionCache);
43+
InlineFunctionInfo IFI(/*cg=*/nullptr, &GetAssumptionCache);
4344

4445
SmallSetVector<CallBase *, 16> Calls;
4546
bool Changed = false;

llvm/lib/Transforms/IPO/InlineSimple.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class SimpleInliner : public LegacyInlinerBase {
6868
[&](Function &F) -> AssumptionCache & {
6969
return ACT->getAssumptionCache(F);
7070
};
71-
return llvm::getInlineCost(CB, Params, TTI, GetAssumptionCache, GetTLI,
72-
/*GetBFI=*/nullptr, PSI,
71+
return llvm::getInlineCost(CB, Params, TTI, GetAssumptionCache,
72+
/*GetBFI=*/None, GetTLI, PSI,
7373
RemarksEnabled ? &ORE : nullptr);
7474
}
7575

llvm/lib/Transforms/IPO/Inliner.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ inlineCallsImpl(CallGraphSCC &SCC, CallGraph &CG,
395395
std::swap(CallSites[I--], CallSites[--FirstCallInSCC]);
396396

397397
InlinedArrayAllocasTy InlinedArrayAllocas;
398-
InlineFunctionInfo InlineInfo(&CG, GetAssumptionCache, PSI);
398+
InlineFunctionInfo InlineInfo(&CG, &GetAssumptionCache, PSI);
399399

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

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

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

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

0 commit comments

Comments
 (0)