[llvm] [ModuleSummary] Avoid redundant domtree/BFI computations (PR #176646)

Alexis Engelke via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 18 04:45:48 PST 2026


https://github.com/aengelke created https://github.com/llvm/llvm-project/pull/176646

This avoid duplicate computation of the dominator tree if the BFI is fetched from the analysis manager. Additionally, if the BFI is specifically computed for this pass, don't keep it around until the end of the pass.

The unconditional BFI computation looks unintended, introduced in 2d5487cf446f9a58f6d2f76df33102423c83f285.

NB: I have no idea what I'm doing in the tests. Maybe this is because the function has no profile data?

>From 9d2f77a529d4dfadccf4b0c9f600587eed73ff78 Mon Sep 17 00:00:00 2001
From: Alexis Engelke <engelke at in.tum.de>
Date: Sun, 18 Jan 2026 12:28:37 +0000
Subject: [PATCH] [ModuleSummary] Avoid redundant domtree/BFI computations

This avoid duplicate computation of the dominator tree if the BFI is
fetched from the analysis manager. Additionally, if the BFI is
specifically computed for this pass, don't keep it around until the end
of the pass.
---
 llvm/lib/Analysis/ModuleSummaryAnalysis.cpp          | 12 ++++++------
 .../thinlto-function-summary-callgraph-relbf.ll      |  4 ++--
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
index 55f87a4e3a099..7778fe803f71a 100644
--- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
+++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
@@ -1063,7 +1063,7 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
     std::unique_ptr<BlockFrequencyInfo> BFIPtr;
     if (GetBFICallback)
       BFI = GetBFICallback(F);
-    else if (F.hasProfileData()) {
+    if (!BFI && F.hasProfileData()) {
       LoopInfo LI{DT};
       BranchProbabilityInfo BPI{F, LI};
       BFIPtr = std::make_unique<BlockFrequencyInfo>(F, BPI, LI);
@@ -1165,7 +1165,7 @@ ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
   return buildModuleSummaryIndex(
       M,
       [&FAM](const Function &F) {
-        return &FAM.getResult<BlockFrequencyAnalysis>(
+        return FAM.getCachedResult<BlockFrequencyAnalysis>(
             *const_cast<Function *>(&F));
       },
       &PSI,
@@ -1199,9 +1199,9 @@ bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
   Index.emplace(buildModuleSummaryIndex(
       M,
       [this](const Function &F) {
-        return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
-                         *const_cast<Function *>(&F))
-                     .getBFI());
+        auto *BFIWP =
+            this->getAnalysisIfAvailable<BlockFrequencyInfoWrapperPass>();
+        return BFIWP ? &BFIWP->getBFI() : nullptr;
       },
       PSI,
       [&](const Function &F) -> const StackSafetyInfo * {
@@ -1220,7 +1220,7 @@ bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) {
 
 void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.setPreservesAll();
-  AU.addRequired<BlockFrequencyInfoWrapperPass>();
+  AU.addUsedIfAvailable<BlockFrequencyInfoWrapperPass>();
   AU.addRequired<ProfileSummaryInfoWrapperPass>();
   AU.addRequired<StackSafetyInfoWrapperPass>();
 }
diff --git a/llvm/test/Bitcode/thinlto-function-summary-callgraph-relbf.ll b/llvm/test/Bitcode/thinlto-function-summary-callgraph-relbf.ll
index 3c827247a6c9f..4ddc7507ba204 100644
--- a/llvm/test/Bitcode/thinlto-function-summary-callgraph-relbf.ll
+++ b/llvm/test/Bitcode/thinlto-function-summary-callgraph-relbf.ll
@@ -14,7 +14,7 @@
 ; CHECK-NEXT:    <VERSION
 ; CHECK-NEXT:    <FLAGS
 ; See if the call to func is registered.
-; CHECK-NEXT:    <PERMODULE_RELBF {{.*}} op4=1 {{.*}} op9=256
+; CHECK-NEXT:    <PERMODULE_RELBF {{.*}} op4=1 {{.*}} op9=0
 ; CHECK-NEXT:  </GLOBALVAL_SUMMARY_BLOCK>
 ; CHECK: <STRTAB_BLOCK
 ; CHECK-NEXT: blob data = 'undefinedglobmainfunc{{.*}}'
@@ -39,5 +39,5 @@ declare void @func(...) #1
 
 ; DIS: ^0 = module: (path: "{{.*}}", hash: (0, 0, 0, 0, 0))
 ; DIS: ^1 = gv: (name: "func") ; guid = 7289175272376759421
-; DIS: ^2 = gv: (name: "main", summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 0, canAutoHide: 0, importType: definition), insts: 3, calls: ((callee: ^1, relbf: 256)), refs: (readonly ^3)))) ; guid = 15822663052811949562
+; DIS: ^2 = gv: (name: "main", summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 0, canAutoHide: 0, importType: definition), insts: 3, calls: ((callee: ^1)), refs: (readonly ^3)))) ; guid = 15822663052811949562
 ; DIS: ^3 = gv: (name: "undefinedglob") ; guid = 18036901804029949403



More information about the llvm-commits mailing list