[llvm] [PGO] Don't unconditionally request BBInfo in verifyFuncBFI() (PR #140804)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Wed May 21 10:24:34 PDT 2025


https://github.com/aeubanks updated https://github.com/llvm/llvm-project/pull/140804

>From cba58725bbdf18417e6a514bccce5887335c192b Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Tue, 20 May 2025 21:16:05 +0000
Subject: [PATCH 1/4] [CFGMST] Remove special case for entry block with no
 successors

This breaks in the case where there are unreachable blocks after an entry block with no successors, which don't get visited in CFGMST, causing crashes.

Fixes #135828.
---
 .../include/llvm/Transforms/Instrumentation/CFGMST.h |  6 ------
 .../PGOProfile/Inputs/unreachable-block.proftext     |  9 +++++++++
 llvm/test/Transforms/PGOProfile/unreachable-block.ll | 12 ++++++++++++
 3 files changed, 21 insertions(+), 6 deletions(-)
 create mode 100644 llvm/test/Transforms/PGOProfile/Inputs/unreachable-block.proftext
 create mode 100644 llvm/test/Transforms/PGOProfile/unreachable-block.ll

diff --git a/llvm/include/llvm/Transforms/Instrumentation/CFGMST.h b/llvm/include/llvm/Transforms/Instrumentation/CFGMST.h
index f6bf045f7de2c..6f0c63b888353 100644
--- a/llvm/include/llvm/Transforms/Instrumentation/CFGMST.h
+++ b/llvm/include/llvm/Transforms/Instrumentation/CFGMST.h
@@ -133,12 +133,6 @@ template <class Edge, class BBInfo> class CFGMST {
     LLVM_DEBUG(dbgs() << "  Edge: from fake node to " << Entry->getName()
                       << " w = " << EntryWeight << "\n");
 
-    // Special handling for single BB functions.
-    if (succ_empty(Entry)) {
-      addEdge(Entry, nullptr, EntryWeight);
-      return;
-    }
-
     static const uint32_t CriticalEdgeMultiplier = 1000;
 
     for (BasicBlock &BB : F) {
diff --git a/llvm/test/Transforms/PGOProfile/Inputs/unreachable-block.proftext b/llvm/test/Transforms/PGOProfile/Inputs/unreachable-block.proftext
new file mode 100644
index 0000000000000..9be174fb32bca
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/Inputs/unreachable-block.proftext
@@ -0,0 +1,9 @@
+# IR level Instrumentation Flag
+:ir
+foo
+# Func Hash:
+742261418966908927
+# Num Counters:
+1
+# Counter Values:
+1
diff --git a/llvm/test/Transforms/PGOProfile/unreachable-block.ll b/llvm/test/Transforms/PGOProfile/unreachable-block.ll
new file mode 100644
index 0000000000000..bd55185649989
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/unreachable-block.ll
@@ -0,0 +1,12 @@
+; RUN: llvm-profdata merge %S/Inputs/unreachable-block.proftext -o %t.profdata
+; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S
+
+declare ptr @bar()
+
+define ptr @foo() {
+entry:
+  ret ptr null
+
+2:
+  ret ptr null
+}

>From 478d1eb6cdcfb4b775e36c820902876ec1b54064 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Tue, 20 May 2025 21:20:49 +0000
Subject: [PATCH 2/4] add CHECK

---
 llvm/test/Transforms/PGOProfile/unreachable-block.ll | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/test/Transforms/PGOProfile/unreachable-block.ll b/llvm/test/Transforms/PGOProfile/unreachable-block.ll
index bd55185649989..fb277bd68307d 100644
--- a/llvm/test/Transforms/PGOProfile/unreachable-block.ll
+++ b/llvm/test/Transforms/PGOProfile/unreachable-block.ll
@@ -1,8 +1,9 @@
 ; RUN: llvm-profdata merge %S/Inputs/unreachable-block.proftext -o %t.profdata
-; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S
+; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s
 
 declare ptr @bar()
 
+; CHECK: define ptr @foo
 define ptr @foo() {
 entry:
   ret ptr null

>From 5fda3c3d46ff81356dbd49bbb3caa6dad4d77824 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Wed, 21 May 2025 00:16:23 +0000
Subject: [PATCH 3/4] Update

use split-file

change caller to not expect BBInfo
---
 .../llvm/Transforms/Instrumentation/CFGMST.h  |  6 ++++
 .../Instrumentation/PGOInstrumentation.cpp    |  8 +++--
 .../Inputs/unreachable-block.proftext         |  9 ------
 .../PGOProfile/unreachable-block.ll           | 13 ---------
 .../Transforms/PGOProfile/unreachable_bb2.ll  | 29 +++++++++++++++++++
 5 files changed, 40 insertions(+), 25 deletions(-)
 delete mode 100644 llvm/test/Transforms/PGOProfile/Inputs/unreachable-block.proftext
 delete mode 100644 llvm/test/Transforms/PGOProfile/unreachable-block.ll
 create mode 100644 llvm/test/Transforms/PGOProfile/unreachable_bb2.ll

diff --git a/llvm/include/llvm/Transforms/Instrumentation/CFGMST.h b/llvm/include/llvm/Transforms/Instrumentation/CFGMST.h
index 6f0c63b888353..f6bf045f7de2c 100644
--- a/llvm/include/llvm/Transforms/Instrumentation/CFGMST.h
+++ b/llvm/include/llvm/Transforms/Instrumentation/CFGMST.h
@@ -133,6 +133,12 @@ template <class Edge, class BBInfo> class CFGMST {
     LLVM_DEBUG(dbgs() << "  Edge: from fake node to " << Entry->getName()
                       << " w = " << EntryWeight << "\n");
 
+    // Special handling for single BB functions.
+    if (succ_empty(Entry)) {
+      addEdge(Entry, nullptr, EntryWeight);
+      return;
+    }
+
     static const uint32_t CriticalEdgeMultiplier = 1000;
 
     for (BasicBlock &BB : F) {
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index 7c73c16db02c8..a71b4bdf0eac0 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -2086,10 +2086,12 @@ static void verifyFuncBFI(PGOUseFunc &Func, LoopInfo &LI,
 
   unsigned BBNum = 0, BBMisMatchNum = 0, NonZeroBBNum = 0;
   for (auto &BBI : F) {
-    uint64_t CountValue = 0;
-    uint64_t BFICountValue = 0;
+    PGOUseBBInfo *BBInfo = Func.findBBInfo(&BBI);
+    if (!BBInfo)
+      continue;
 
-    CountValue = Func.getBBInfo(&BBI).Count.value_or(CountValue);
+    uint64_t CountValue = Func.getBBInfo(&BBI).Count.value_or(CountValue);
+    uint64_t BFICountValue = 0;
 
     BBNum++;
     if (CountValue)
diff --git a/llvm/test/Transforms/PGOProfile/Inputs/unreachable-block.proftext b/llvm/test/Transforms/PGOProfile/Inputs/unreachable-block.proftext
deleted file mode 100644
index 9be174fb32bca..0000000000000
--- a/llvm/test/Transforms/PGOProfile/Inputs/unreachable-block.proftext
+++ /dev/null
@@ -1,9 +0,0 @@
-# IR level Instrumentation Flag
-:ir
-foo
-# Func Hash:
-742261418966908927
-# Num Counters:
-1
-# Counter Values:
-1
diff --git a/llvm/test/Transforms/PGOProfile/unreachable-block.ll b/llvm/test/Transforms/PGOProfile/unreachable-block.ll
deleted file mode 100644
index fb277bd68307d..0000000000000
--- a/llvm/test/Transforms/PGOProfile/unreachable-block.ll
+++ /dev/null
@@ -1,13 +0,0 @@
-; RUN: llvm-profdata merge %S/Inputs/unreachable-block.proftext -o %t.profdata
-; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s
-
-declare ptr @bar()
-
-; CHECK: define ptr @foo
-define ptr @foo() {
-entry:
-  ret ptr null
-
-2:
-  ret ptr null
-}
diff --git a/llvm/test/Transforms/PGOProfile/unreachable_bb2.ll b/llvm/test/Transforms/PGOProfile/unreachable_bb2.ll
new file mode 100644
index 0000000000000..a94f93dbe2ee1
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/unreachable_bb2.ll
@@ -0,0 +1,29 @@
+; RUN: split-file %s %t
+; RUN: llvm-profdata merge %t/a.proftext -o %t/a.profdata
+; RUN: opt < %t/a.ll -passes=pgo-instr-use -pgo-test-profile-file=%t/a.profdata -S | FileCheck %s
+
+;--- a.ll
+
+declare ptr @bar()
+
+; CHECK: define ptr @foo
+; Ensure the profile hash matches. If it doesn't we emit the "instr_prof_hash_mismatch" metadata.
+; CHECK-NOT: instr_prof_hash_mismatch
+define ptr @foo() {
+entry:
+  ret ptr null
+
+2:
+  ret ptr null
+}
+
+;--- a.proftext
+# IR level Instrumentation Flag
+:ir
+foo
+# Func Hash:
+742261418966908927
+# Num Counters:
+1
+# Counter Values:
+1

>From 1a34e20e6199e73f6fe01ee24b21e21646c01e91 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Wed, 21 May 2025 00:19:48 +0000
Subject: [PATCH 4/4] reuse BBInfo

---
 llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index a71b4bdf0eac0..a063fb2ec3fe1 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -2090,7 +2090,7 @@ static void verifyFuncBFI(PGOUseFunc &Func, LoopInfo &LI,
     if (!BBInfo)
       continue;
 
-    uint64_t CountValue = Func.getBBInfo(&BBI).Count.value_or(CountValue);
+    uint64_t CountValue = BBInfo->Count.value_or(CountValue);
     uint64_t BFICountValue = 0;
 
     BBNum++;



More information about the llvm-commits mailing list