[Mlir-commits] [mlir] [MLIR][DataFlow] Guard getTerminator() with mightHaveTerminator() check (PR #188727)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Mar 26 04:28:52 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: Mehdi Amini (joker-eph)
<details>
<summary>Changes</summary>
`isRegionOrCallableReturn` in `DeadCodeAnalysis.cpp` called `Block::getTerminator()` without first checking
`Block::mightHaveTerminator()`. For regions with the `NoTerminator` trait (e.g. `acc.kernel_environment`), blocks do not have a terminator and `getTerminator()` asserts.
Add a `mightHaveTerminator()` guard before calling `getTerminator()`.
Fixes #<!-- -->188408
Assisted-by: Claude Code
---
Full diff: https://github.com/llvm/llvm-project/pull/188727.diff
2 Files Affected:
- (modified) mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp (+1)
- (added) mlir/test/Analysis/DataFlow/test-dead-code-analysis-no-terminator.mlir (+17)
``````````diff
diff --git a/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp b/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp
index 936b0c678f20c..c36efbf2e1472 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;
}
diff --git a/mlir/test/Analysis/DataFlow/test-dead-code-analysis-no-terminator.mlir b/mlir/test/Analysis/DataFlow/test-dead-code-analysis-no-terminator.mlir
new file mode 100644
index 0000000000000..7d6e85201c123
--- /dev/null
+++ b/mlir/test/Analysis/DataFlow/test-dead-code-analysis-no-terminator.mlir
@@ -0,0 +1,17 @@
+// RUN: mlir-opt --sccp %s | FileCheck %s
+
+// Regression test for https://github.com/llvm/llvm-project/issues/188408
+// DeadCodeAnalysis crashed when visiting a block inside a region with the
+// NoTerminator trait (e.g. acc.kernel_environment) because
+// isRegionOrCallableReturn called Block::getTerminator() without first
+// checking Block::mightHaveTerminator().
+
+// CHECK-LABEL: func.func @f
+func.func @f(%arg0: memref<8xi32>) {
+ acc.kernel_environment {
+ acc.compute_region {
+ acc.yield
+ } {origin = "acc.parallel"}
+ }
+ return
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/188727
More information about the Mlir-commits
mailing list