[llvm] [CodeGenPrepare] Report an error if ProfileSummaryAnalysis is not available (PR #199268)

Justin Lebar via llvm-commits llvm-commits at lists.llvm.org
Sat May 23 23:06:00 PDT 2026


https://github.com/jlebar updated https://github.com/llvm/llvm-project/pull/199268

>From 6a6ec4b2cffb2c7f6a63469e2d4e4553dfb3a533 Mon Sep 17 00:00:00 2001
From: Justin Lebar <justin.lebar at gmail.com>
Date: Thu, 21 May 2026 21:32:39 -0700
Subject: [PATCH 1/2] [CodeGenPrepare] Report missing ProfileSummaryAnalysis

CodeGenPrepare's NewPM entrypoint reads ProfileSummaryAnalysis through
ModuleAnalysisManagerFunctionProxy::getCachedResult(). A bare
opt -passes=codegenprepare invocation does not require the module analysis,
so the pass would otherwise dereference a null PSI later in _run.

Emit a fatal usage error with the
opt -passes='require<profile-summary>,function(codegenprepare)' spelling
instead of crashing.

Fixes https://github.com/llvm/llvm-project/issues/173360.
---
 llvm/lib/CodeGen/CodeGenPrepare.cpp                         | 5 +++++
 .../test/Transforms/CodeGenPrepare/X86/null-psi-no-crash.ll | 6 ++++++
 2 files changed, 11 insertions(+)
 create mode 100644 llvm/test/Transforms/CodeGenPrepare/X86/null-psi-no-crash.ll

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 74a0502d8cb7c..03517381f6d72 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -570,6 +570,11 @@ bool CodeGenPrepare::run(Function &F, FunctionAnalysisManager &AM) {
   BFI = &AM.getResult<BlockFrequencyAnalysis>(F);
   auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
   PSI = MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
+  if (!PSI)
+    reportFatalUsageError("this pass requires the profile-summary module "
+                          "analysis to be available; when running opt, use "
+                          "-passes='require<profile-summary>,"
+                          "function(codegenprepare)'");
   BBSectionsProfileReader =
       AM.getCachedResult<BasicBlockSectionsProfileReaderAnalysis>(F);
   DomTreeUpdater DTUpdater(&AM.getResult<DominatorTreeAnalysis>(F),
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/null-psi-no-crash.ll b/llvm/test/Transforms/CodeGenPrepare/X86/null-psi-no-crash.ll
new file mode 100644
index 0000000000000..bc500f6bb1bc8
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/null-psi-no-crash.ll
@@ -0,0 +1,6 @@
+; RUN: not opt -passes=codegenprepare -mtriple=x86_64-linux-gnu -disable-output < %s 2>&1 | FileCheck %s
+
+; CHECK: LLVM ERROR: this pass requires the profile-summary module analysis to be available
+define void @f() {
+  ret void
+}

>From d8c5711bbe1d2a2165a5031a47ea0f6c4d256dd4 Mon Sep 17 00:00:00 2001
From: Justin Lebar <justin.lebar at gmail.com>
Date: Sat, 23 May 2026 23:05:43 -0700
Subject: [PATCH 2/2] Review comments

---
 llvm/lib/CodeGen/CodeGenPrepare.cpp                          | 4 +---
 llvm/test/Transforms/CodeGenPrepare/X86/null-psi-no-crash.ll | 2 ++
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 03517381f6d72..9b756dc7b03b7 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -572,9 +572,7 @@ bool CodeGenPrepare::run(Function &F, FunctionAnalysisManager &AM) {
   PSI = MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
   if (!PSI)
     reportFatalUsageError("this pass requires the profile-summary module "
-                          "analysis to be available; when running opt, use "
-                          "-passes='require<profile-summary>,"
-                          "function(codegenprepare)'");
+                          "analysis to be available");
   BBSectionsProfileReader =
       AM.getCachedResult<BasicBlockSectionsProfileReaderAnalysis>(F);
   DomTreeUpdater DTUpdater(&AM.getResult<DominatorTreeAnalysis>(F),
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/null-psi-no-crash.ll b/llvm/test/Transforms/CodeGenPrepare/X86/null-psi-no-crash.ll
index bc500f6bb1bc8..c5ce8099f3f52 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/null-psi-no-crash.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/null-psi-no-crash.ll
@@ -1,5 +1,7 @@
 ; RUN: not opt -passes=codegenprepare -mtriple=x86_64-linux-gnu -disable-output < %s 2>&1 | FileCheck %s
 
+; Check that if we do not require PSI in the pipeline, we error out instead of crashing.
+
 ; CHECK: LLVM ERROR: this pass requires the profile-summary module analysis to be available
 define void @f() {
   ret void



More information about the llvm-commits mailing list