Skip to content

[MLIR][DataFlow] Fix two crashes in DeadCodeAnalysis on empty/no-terminator regions#188548

Merged
joker-eph merged 1 commit into
llvm:mainfrom
joker-eph:fix/issue-187972
Mar 25, 2026
Merged

[MLIR][DataFlow] Fix two crashes in DeadCodeAnalysis on empty/no-terminator regions#188548
joker-eph merged 1 commit into
llvm:mainfrom
joker-eph:fix/issue-187972

Conversation

@joker-eph

Copy link
Copy Markdown
Contributor

Two related assertion failures in DeadCodeAnalysis when processing OpenACC operations:

  1. visitRegionBranchEdges (issue [MLIR] Assertion !NodePtr->isKnownSentinel() in DeadCodeAnalysis::visitRegionBranchEdges when processing acc.serial with empty region #187972): When a RegionSuccessor refers to an empty region (no blocks), calling getSuccessor()->front() dereferences a sentinel ilist iterator, crashing with "!NodePtr->isKnownSentinel()". Fix: skip successors whose region is empty.

  2. isRegionOrCallableReturn (issue [MLIR][SCCP][OpenACC] Assertion mightHaveTerminator() in Block::getTerminator when DeadCodeAnalysis::initializeRecursively walks into acc.kernel_environment containing acc.compute_region #188408): When iterating over ops in a nested acc region whose blocks do not have a required terminator, Block::getTerminator() is called without first checking mightHaveTerminator(), triggering "Assertion `mightHaveTerminator()' failed". Fix: guard the getTerminator() call with mightHaveTerminator().

Fixes #187972, #188408

Assisted-by: Claude Code

…inator regions

Two related assertion failures in DeadCodeAnalysis when processing OpenACC
operations:

1. visitRegionBranchEdges (issue llvm#187972): When a RegionSuccessor refers to
   an empty region (no blocks), calling getSuccessor()->front() dereferences
   a sentinel ilist iterator, crashing with "\!NodePtr->isKnownSentinel()".
   Fix: skip successors whose region is empty.

2. isRegionOrCallableReturn (issue llvm#188408): When iterating over ops in a
   nested acc region whose blocks do not have a required terminator,
   Block::getTerminator() is called without first checking
   mightHaveTerminator(), triggering "Assertion `mightHaveTerminator()'
   failed".
   Fix: guard the getTerminator() call with mightHaveTerminator().

Fixes llvm#187972, llvm#188408

Assisted-by: Claude Code
@llvmbot

llvmbot commented Mar 25, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-mlir

Author: Mehdi Amini (joker-eph)

Changes

Two related assertion failures in DeadCodeAnalysis when processing OpenACC operations:

  1. visitRegionBranchEdges (issue #187972): When a RegionSuccessor refers to an empty region (no blocks), calling getSuccessor()->front() dereferences a sentinel ilist iterator, crashing with "!NodePtr->isKnownSentinel()". Fix: skip successors whose region is empty.

  2. isRegionOrCallableReturn (issue #188408): When iterating over ops in a nested acc region whose blocks do not have a required terminator, Block::getTerminator() is called without first checking mightHaveTerminator(), triggering "Assertion `mightHaveTerminator()' failed". Fix: guard the getTerminator() call with mightHaveTerminator().

Fixes #187972, #188408

Assisted-by: Claude Code


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

2 Files Affected:

  • (modified) mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp (+4)
  • (modified) mlir/test/Transforms/sccp.mlir (+31)
diff --git a/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp b/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp
index 936b0c678f20c..38811d06ecd8c 100644
--- a/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp
+++ b/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp
@@ -249,6 +249,7 @@ void DeadCodeAnalysis::initializeSymbolCallables(Operation *top) {
 static bool isRegionOrCallableReturn(Operation *op) {
   return op->getBlock() != nullptr && !op->getNumSuccessors() &&
          isa<RegionBranchOpInterface, CallableOpInterface>(op->getParentOp()) &&
+         op->getBlock()->mightHaveTerminator() &&
          op->getBlock()->getTerminator() == op;
 }
 
@@ -521,6 +522,9 @@ void DeadCodeAnalysis::visitRegionBranchEdges(
     const SmallVector<RegionSuccessor> &successors) {
   for (const RegionSuccessor &successor : successors) {
     // The successor can be either an entry block or the parent operation.
+    // Skip empty regions — they have no entry block to mark executable.
+    if (!successor.isParent() && successor.getSuccessor()->empty())
+      continue;
     ProgramPoint *point =
         successor.isParent()
             ? getProgramPointAfter(regionBranchOp)
diff --git a/mlir/test/Transforms/sccp.mlir b/mlir/test/Transforms/sccp.mlir
index c78c8594c0ba5..138acbb42d1a9 100644
--- a/mlir/test/Transforms/sccp.mlir
+++ b/mlir/test/Transforms/sccp.mlir
@@ -255,3 +255,34 @@ func.func @no_crash_with_different_source_type() {
   %1 = vector.broadcast %0 : i64 to vector<128xi64>
   llvm.return
 }
+
+// -----
+
+// Regression test for https://github.com/llvm/llvm-project/issues/187972
+// DeadCodeAnalysis::visitRegionBranchEdges must not dereference the front()
+// of an empty region (acc.serial with no blocks).
+
+// CHECK-LABEL: no_crash_acc_serial_empty_region
+func.func @no_crash_acc_serial_empty_region() {
+  // CHECK: acc.serial
+  acc.serial {
+  }
+  return
+}
+
+// -----
+
+// Regression test for https://github.com/llvm/llvm-project/issues/188408
+// isRegionOrCallableReturn must guard getTerminator() with mightHaveTerminator()
+// to avoid asserting on blocks in nested acc regions without terminators.
+
+// CHECK-LABEL: no_crash_acc_kernel_environment
+func.func @no_crash_acc_kernel_environment(%data: memref<8xi32>) {
+  // CHECK: acc.kernel_environment
+  acc.kernel_environment {
+    acc.compute_region {
+      acc.yield
+    } {origin = "acc.parallel"}
+  }
+  return
+}

@linuxlonelyeagle linuxlonelyeagle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

@joker-eph joker-eph merged commit 820eaa4 into llvm:main Mar 25, 2026
12 checks passed
@llvm-ci

llvm-ci commented Mar 26, 2026

Copy link
Copy Markdown

LLVM Buildbot has detected a new failure on builder ppc64le-mlir-rhel-clang running on ppc64le-mlir-rhel-test while building mlir at step 6 "test-build-check-mlir-build-only-check-mlir".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/129/builds/41367

Here is the relevant piece of the build log for the reference
Step 6 (test-build-check-mlir-build-only-check-mlir) failure: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
...
PASS: MLIR :: mlir-tblgen/type-constraints.td (3978 of 3988)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/11/25 (3979 of 3988)
PASS: MLIR-Unit :: IR/./MLIRIRTests/0/142 (3980 of 3988)
PASS: MLIR-Unit :: IR/./MLIRIRTests/37/142 (3981 of 3988)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/12/25 (3982 of 3988)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/13/25 (3983 of 3988)
PASS: MLIR-Unit :: IR/./MLIRIRTests/38/142 (3984 of 3988)
PASS: MLIR :: mlir-reduce/dce-test.mlir (3985 of 3988)
PASS: MLIR :: mlir-runner/simple.mlir (3986 of 3988)
PASS: MLIR :: mlir-tblgen/llvm-intrinsics.td (3987 of 3988)
command timed out: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=2554.193480

Aadarsh-Keshri pushed a commit to Aadarsh-Keshri/llvm-project that referenced this pull request Mar 28, 2026
…inator regions (llvm#188548)

Two related assertion failures in DeadCodeAnalysis when processing
OpenACC operations:

1. visitRegionBranchEdges (issue llvm#187972): When a RegionSuccessor refers
to an empty region (no blocks), calling getSuccessor()->front()
dereferences a sentinel ilist iterator, crashing with
"\!NodePtr->isKnownSentinel()". Fix: skip successors whose region is
empty.

2. isRegionOrCallableReturn (issue llvm#188408): When iterating over ops in
a nested acc region whose blocks do not have a required terminator,
Block::getTerminator() is called without first checking
mightHaveTerminator(), triggering "Assertion `mightHaveTerminator()'
failed". Fix: guard the getTerminator() call with mightHaveTerminator().

Fixes llvm#187972, llvm#188408

Assisted-by: Claude Code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MLIR] Assertion !NodePtr->isKnownSentinel() in DeadCodeAnalysis::visitRegionBranchEdges when processing acc.serial with empty region

4 participants