Skip to content

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

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

aeubanks
Copy link
Contributor

@aeubanks aeubanks commented May 20, 2025

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

BBInfo doesn't exist for unreachable blocks, see https://reviews.llvm.org/D27280.

Fixes #135828.

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 llvm#135828.
@llvmbot llvmbot added PGO Profile Guided Optimizations llvm:transforms labels May 20, 2025
@llvmbot
Copy link
Member

llvmbot commented May 20, 2025

@llvm/pr-subscribers-pgo

@llvm/pr-subscribers-llvm-transforms

Author: Arthur Eubanks (aeubanks)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/140804.diff

3 Files Affected:

  • (modified) llvm/include/llvm/Transforms/Instrumentation/CFGMST.h (-6)
  • (added) llvm/test/Transforms/PGOProfile/Inputs/unreachable-block.proftext (+9)
  • (added) llvm/test/Transforms/PGOProfile/unreachable-block.ll (+12)
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
+}

@mtrofin
Copy link
Member

mtrofin commented May 20, 2025

Is the more general "unreachable BB" a case?

use split-file

change caller to not expect BBInfo
@aeubanks aeubanks changed the title [CFGMST] Remove special case for entry block with no successors [PGO] Don't unconditionally request BBInfo in verifyFuncBFI() May 21, 2025
@aeubanks
Copy link
Contributor Author

I've updated the PR and description based on more digging, ptal

Comment on lines +2090 to +2091
if (!BBInfo)
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we emit a warning in this case? Why is it ok to silently ignore here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an FDO-optimized build, hash mismatch errors are detected and logged (

std::string Msg =
IPE.message() + std::string(" ") + F.getName().str() +
std::string(" Hash = ") + std::to_string(FuncInfo.FunctionHash) +
std::string(" up to ") + std::to_string(MismatchedFuncSum) +
std::string(" count discarded");
Ctx.diagnose(
DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning));
).

Is it feasible to use similar mechanism to log the error here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only unreachable basic blocks don't have a BBInfo, those shouldn't matter

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, is it possible to assert that BB is unreachable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing DominatorTree wasn't (yet) computed in -O0 and he'd rather not compute it?

@mtrofin mtrofin requested a review from xur-llvm May 21, 2025 18:23
Copy link
Member

@mtrofin mtrofin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

one alternative would be to DCE unreachable BBs as a prelude to instrumentation. But this patch could be OK too.

I'm curious, how did we end up with unreachable BBs at this stage anyway? The early inliner?

@aeubanks
Copy link
Contributor Author

I'm curious, how did we end up with unreachable BBs at this stage anyway? The early inliner?

-O0 :)

@hiraditya
Copy link
Collaborator

nice find btw!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:transforms PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[PGOInstrUse] "dereferencing end() iterator" on unreachable block
6 participants