Skip to content

Conversation

@nataliakokoromyti
Copy link

@nataliakokoromyti nataliakokoromyti commented Dec 23, 2025

this PR fixes a crash in CodeGenPrepare when PSI is null.

Fixes #173360.

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Dec 23, 2025

@llvm/pr-subscribers-llvm-transforms

Author: None (nataliakokoromyti)

Changes

this PR fixes a crash in CodeGenPrepare when PSI is null.


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

1 Files Affected:

  • (added) llvm/test/Transforms/CodeGenPrepare/pr173360-null-psi.ll (+21)
diff --git a/llvm/test/Transforms/CodeGenPrepare/pr173360-null-psi.ll b/llvm/test/Transforms/CodeGenPrepare/pr173360-null-psi.ll
new file mode 100644
index 0000000000000..53500c0bcb481
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/pr173360-null-psi.ll
@@ -0,0 +1,21 @@
+; NOTE: This test verifies that CodeGenPrepare doesn't crash when PSI is null
+; RUN: opt -passes=codegenprepare < %s -S | FileCheck %s
+;
+; This test case triggered a null pointer dereference in CodeGenPrepare::_run()
+; when ProfileSummaryInfo (PSI) was not available. The pass attempted to call
+; PSI->hasHugeWorkingSetSize() without checking if PSI was null.
+;
+; The fix adds null checks before dereferencing PSI.
+; See: https://github.com/llvm/llvm-project/issues/173360
+
+target triple = "x86_64-unknown-linux-gnu"
+
+; CHECK-LABEL: @f(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    %iv.next.reg2mem = alloca i32, align 4
+; CHECK-NEXT:    ret void
+define void @f(ptr %A, i32 %n) {
+entry:
+  %iv.next.reg2mem = alloca i32, align 4
+  ret void
+}

@zwuis
Copy link
Contributor

zwuis commented Dec 23, 2025

Where is the fix? Only test file is modified.

@nataliakokoromyti nataliakokoromyti marked this pull request as draft December 23, 2025 14:59
@nataliakokoromyti nataliakokoromyti marked this pull request as ready for review December 23, 2025 16:45
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Please also add a test.

@nikic nikic changed the title fix CodeGenPrepare crash with null PSI (issue #173360) [CodeGenPrepare] Fix crash with null PSI Dec 23, 2025
@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/lib/CodeGen/CodeGenPrepare.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index aa645d87f..ee1ad6de5 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -605,7 +605,8 @@ bool CodeGenPrepare::_run(Function &F) {
 
   /// This optimization identifies DIV instructions that can be
   /// profitably bypassed and carried out with a shorter, faster divide.
-  if (!OptSize && PSI && !PSI->hasHugeWorkingSetSize() && TLI->isSlowDivBypassed()) {
+  if (!OptSize && PSI && !PSI->hasHugeWorkingSetSize() &&
+      TLI->isSlowDivBypassed()) {
     const DenseMap<unsigned int, unsigned int> &BypassWidths =
         TLI->getBypassSlowDivWidths();
     BasicBlock *BB = &*F.begin();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opt crashes in CodeGenPrepare.cpp

4 participants