Skip to content

[AlwaysInline] Avoid unnecessary BFI fetches #117750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions llvm/lib/Transforms/IPO/AlwaysInliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ bool AlwaysInlineImpl(
Module &M, bool InsertLifetime, ProfileSummaryInfo &PSI,
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
function_ref<AAResults &(Function &)> GetAAR,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI) {
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
function_ref<BlockFrequencyInfo *(Function &)> GetCachedBFI) {
SmallSetVector<CallBase *, 16> Calls;
bool Changed = false;
SmallVector<Function *, 16> InlinedComdatFunctions;
Expand All @@ -61,9 +62,11 @@ bool AlwaysInlineImpl(
DebugLoc DLoc = CB->getDebugLoc();
BasicBlock *Block = CB->getParent();

InlineFunctionInfo IFI(GetAssumptionCache, &PSI,
GetBFI ? &GetBFI(*Caller) : nullptr,
GetBFI ? &GetBFI(F) : nullptr);
// Only update CallerBFI if already available. The CallerBFI update
// requires CalleeBFI.
BlockFrequencyInfo *CallerBFI = GetCachedBFI(*Caller);
InlineFunctionInfo IFI(GetAssumptionCache, &PSI, CallerBFI,
CallerBFI ? &GetBFI(F) : nullptr);

InlineResult Res = InlineFunction(*CB, IFI, /*MergeAttributes=*/true,
&GetAAR(F), InsertLifetime);
Expand Down Expand Up @@ -133,9 +136,12 @@ struct AlwaysInlinerLegacyPass : public ModulePass {
auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
return getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
};
auto GetCachedBFI = [](Function &) -> BlockFrequencyInfo * {
return nullptr;
};

return AlwaysInlineImpl(M, InsertLifetime, PSI, GetAssumptionCache, GetAAR,
/*GetBFI*/ nullptr);
/*GetBFI=*/nullptr, GetCachedBFI);
}

static char ID; // Pass identification, replacement for typeid
Expand Down Expand Up @@ -172,13 +178,16 @@ PreservedAnalyses AlwaysInlinerPass::run(Module &M,
auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {
return FAM.getResult<BlockFrequencyAnalysis>(F);
};
auto GetCachedBFI = [&](Function &F) -> BlockFrequencyInfo * {
return FAM.getCachedResult<BlockFrequencyAnalysis>(F);
};
auto GetAAR = [&](Function &F) -> AAResults & {
return FAM.getResult<AAManager>(F);
};
auto &PSI = MAM.getResult<ProfileSummaryAnalysis>(M);

bool Changed = AlwaysInlineImpl(M, InsertLifetime, PSI, GetAssumptionCache,
GetAAR, GetBFI);
GetAAR, GetBFI, GetCachedBFI);

return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
}