Skip to content

Commit 767db5b

Browse files
committed
[llvm][NFC] Cleanup uses of std::function in Inlining-related APIs
Summary: Replacing uses of std::function pointers or refs, or Optional, to function_ref, since the usage pattern allows that. If the function is optional, using a default parameter value (nullptr). This led to a few parameter reshufles, to push all optionals to the end of the parameter list. Reviewers: davidxl, dblaikie Subscribers: arsenm, jvesely, nhaehnle, eraman, hiraditya, haicheng, kerbowa, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D79917
1 parent 0b3e478 commit 767db5b

File tree

11 files changed

+112
-121
lines changed

11 files changed

+112
-121
lines changed

llvm/include/llvm/Analysis/InlineCost.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,14 @@ 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 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);
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);
225227

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

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

263267
/// Minimal filter to detect invalid constructs for inlining.
264268
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(CallGraph *cg = nullptr,
175-
std::function<AssumptionCache &(Function &)>
176-
*GetAssumptionCache = nullptr,
177-
ProfileSummaryInfo *PSI = nullptr,
178-
BlockFrequencyInfo *CallerBFI = nullptr,
179-
BlockFrequencyInfo *CalleeBFI = nullptr)
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)
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-
std::function<AssumptionCache &(Function &)> *GetAssumptionCache;
186+
function_ref<AssumptionCache &(Function &)> GetAssumptionCache;
187187
ProfileSummaryInfo *PSI;
188188
BlockFrequencyInfo *CallerBFI, *CalleeBFI;
189189

llvm/lib/Analysis/InlineAdvisor.cpp

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

101101
auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
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 & {
102+
auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
107103
return FAM.getResult<AssumptionAnalysis>(F);
108104
};
109105
auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {
@@ -119,8 +115,8 @@ DefaultInlineAdvisor::getAdvice(CallBase &CB, FunctionAnalysisManager &FAM) {
119115
bool RemarksEnabled =
120116
Callee.getContext().getDiagHandlerPtr()->isMissedOptRemarkEnabled(
121117
DEBUG_TYPE);
122-
return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, {GetBFI},
123-
GetTLI, PSI, RemarksEnabled ? &ORE : nullptr);
118+
return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, GetTLI,
119+
GetBFI, PSI, RemarksEnabled ? &ORE : nullptr);
124120
};
125121
auto OIC = llvm::shouldInline(CB, GetInlineCost, ORE);
126122
return std::make_unique<DefaultInlineAdvice>(this, CB, OIC, ORE);

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 29 additions & 27 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-
std::function<AssumptionCache &(Function &)> &GetAssumptionCache;
151+
function_ref<AssumptionCache &(Function &)> GetAssumptionCache;
152152

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

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

384384
public:
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)
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)
390391
: TTI(TTI), GetAssumptionCache(GetAssumptionCache), GetBFI(GetBFI),
391392
PSI(PSI), F(Callee), DL(F.getParent()->getDataLayout()), ORE(ORE),
392393
CandidateCall(Call), EnableLoadElimination(true) {}
@@ -504,8 +505,8 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
504505
InlineConstants::IndirectCallThreshold;
505506
/// FIXME: if InlineCostCallAnalyzer is derived from, this may need
506507
/// to instantiate the derived class.
507-
InlineCostCallAnalyzer CA(TTI, GetAssumptionCache, GetBFI, PSI, ORE, *F,
508-
Call, IndirectCallParams, false);
508+
InlineCostCallAnalyzer CA(*F, Call, IndirectCallParams, TTI,
509+
GetAssumptionCache, GetBFI, PSI, ORE, false);
509510
if (CA.analyze().isSuccess()) {
510511
// We were able to inline the indirect call! Subtract the cost from the
511512
// threshold to get the bonus we want to apply, but don't go below zero.
@@ -693,13 +694,14 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
693694

694695
public:
695696
InlineCostCallAnalyzer(
697+
Function &Callee, CallBase &Call, const InlineParams &Params,
696698
const TargetTransformInfo &TTI,
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,
699+
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
700+
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
701+
ProfileSummaryInfo *PSI = nullptr,
702+
OptimizationRemarkEmitter *ORE = nullptr, bool BoostIndirect = true,
701703
bool IgnoreThreshold = false)
702-
: CallAnalyzer(TTI, GetAssumptionCache, GetBFI, PSI, ORE, Callee, Call),
704+
: CallAnalyzer(Callee, Call, TTI, GetAssumptionCache, GetBFI, PSI, ORE),
703705
ComputeFullInlineCost(OptComputeFullInlineCost ||
704706
Params.ComputeFullInlineCost || ORE),
705707
Params(Params), Threshold(Params.DefaultThreshold),
@@ -1298,7 +1300,7 @@ void InlineCostCallAnalyzer::updateThreshold(CallBase &Call, Function &Callee) {
12981300
// Callsite hotness and coldness can be determined if sample profile is
12991301
// used (which adds hotness metadata to calls) or if caller's
13001302
// BlockFrequencyInfo is available.
1301-
BlockFrequencyInfo *CallerBFI = GetBFI ? &((*GetBFI)(*Caller)) : nullptr;
1303+
BlockFrequencyInfo *CallerBFI = GetBFI ? &(GetBFI(*Caller)) : nullptr;
13021304
auto HotCallSiteThreshold = getHotCallSiteThreshold(Call, CallerBFI);
13031305
if (!Caller->hasOptSize() && HotCallSiteThreshold) {
13041306
LLVM_DEBUG(dbgs() << "Hot callsite.\n");
@@ -1765,7 +1767,7 @@ bool CallAnalyzer::visitSwitchInst(SwitchInst &SI) {
17651767
// does not (yet) fire.
17661768

17671769
unsigned JumpTableSize = 0;
1768-
BlockFrequencyInfo *BFI = GetBFI ? &((*GetBFI)(F)) : nullptr;
1770+
BlockFrequencyInfo *BFI = GetBFI ? &(GetBFI(F)) : nullptr;
17691771
unsigned NumCaseCluster =
17701772
TTI.getEstimatedNumberOfCaseClusters(SI, JumpTableSize, PSI, BFI);
17711773

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

22202222
InlineCost llvm::getInlineCost(
22212223
CallBase &Call, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
2222-
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
2223-
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
2224+
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
22242225
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
2226+
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
22252227
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
22262228
return getInlineCost(Call, Call.getCalledFunction(), Params, CalleeTTI,
2227-
GetAssumptionCache, GetBFI, GetTLI, PSI, ORE);
2229+
GetAssumptionCache, GetTLI, GetBFI, PSI, ORE);
22282230
}
22292231

22302232
Optional<int> llvm::getInliningCostEstimate(
22312233
CallBase &Call, TargetTransformInfo &CalleeTTI,
2232-
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
2233-
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
2234+
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
2235+
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
22342236
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
22352237
const InlineParams Params = {/* DefaultThreshold*/ 0,
22362238
/*HintThreshold*/ {},
@@ -2242,8 +2244,8 @@ Optional<int> llvm::getInliningCostEstimate(
22422244
/*ColdCallSiteThreshold*/ {},
22432245
/* ComputeFullInlineCost*/ true};
22442246

2245-
InlineCostCallAnalyzer CA(CalleeTTI, GetAssumptionCache, GetBFI, PSI, ORE,
2246-
*Call.getCalledFunction(), Call, Params, true,
2247+
InlineCostCallAnalyzer CA(*Call.getCalledFunction(), Call, Params, CalleeTTI,
2248+
GetAssumptionCache, GetBFI, PSI, ORE, true,
22472249
/*IgnoreThreshold*/ true);
22482250
auto R = CA.analyze();
22492251
if (!R.isSuccess())
@@ -2315,9 +2317,9 @@ Optional<InlineResult> llvm::getAttributeBasedInliningDecision(
23152317
InlineCost llvm::getInlineCost(
23162318
CallBase &Call, Function *Callee, const InlineParams &Params,
23172319
TargetTransformInfo &CalleeTTI,
2318-
std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
2319-
Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
2320+
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
23202321
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
2322+
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
23212323
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
23222324

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

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

23402342
LLVM_DEBUG(CA.dump());

llvm/lib/Target/AMDGPU/AMDGPUInline.cpp

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

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

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

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

llvm/lib/Transforms/IPO/AlwaysInliner.cpp

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

4544
SmallSetVector<CallBase *, 16> Calls;
4645
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,
72-
/*GetBFI=*/None, GetTLI, PSI,
71+
return llvm::getInlineCost(CB, Params, TTI, GetAssumptionCache, GetTLI,
72+
/*GetBFI=*/nullptr, PSI,
7373
RemarksEnabled ? &ORE : nullptr);
7474
}
7575

llvm/lib/Transforms/IPO/Inliner.cpp

Lines changed: 3 additions & 4 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,8 +804,7 @@ PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
804804

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

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

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

0 commit comments

Comments
 (0)