[Mlir-commits] [mlir] [mlir][DeadCodeAnalysis] Don't Require `RegionBranchTerminatorOpInterface` in `visitRegionTerminator()` (PR #69043)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Oct 14 00:13:34 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Justin Fargnoli (justinfargnoli)

<details>
<summary>Changes</summary>

Fix for a crash reported in #<!-- -->64975. 

The crash occurs in the cast located [here](https://github.com/llvm/llvm-project/blob/ece5dd101c7e4dc2fd23428abd312f75fd3d3eaf/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp#L262) because `llvm.unreachable` doesn't implement `RegionBranchTerminatorOpInterface`. 

The crash is caused by `DeadCodeAnalysis` assuming that `isa<RegionBranchOpInterface>(op->getParentOp())` implies `isa<RegionBranchTerminatorOpInterface>(op)` in `DeadCodeAnalysis::visit()`. 

This patch tried to fix this by enabling the analysis to proceed regardless of whether `op` is a `RegionBranchTerminatorOpInterface`. 

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


3 Files Affected:

- (modified) mlir/include/mlir/Analysis/DataFlow/DeadCodeAnalysis.h (+1-1) 
- (modified) mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp (+7-5) 
- (added) mlir/test/Analysis/DataFlow/test-dead-code-analysis-regression.mlir (+5) 


``````````diff
diff --git a/mlir/include/mlir/Analysis/DataFlow/DeadCodeAnalysis.h b/mlir/include/mlir/Analysis/DataFlow/DeadCodeAnalysis.h
index 7a6fea8326a58b8..ce262c3ac5291ff 100644
--- a/mlir/include/mlir/Analysis/DataFlow/DeadCodeAnalysis.h
+++ b/mlir/include/mlir/Analysis/DataFlow/DeadCodeAnalysis.h
@@ -208,7 +208,7 @@ class DeadCodeAnalysis : public DataFlowAnalysis {
   /// Visit the given terminator operation that exits a region under an
   /// operation with control-flow semantics. These are terminators with no CFG
   /// successors.
-  void visitRegionTerminator(RegionBranchTerminatorOpInterface op,
+  void visitRegionTerminator(Operation *op,
                              RegionBranchOpInterface branch);
 
   /// Visit the given terminator operation that exits a callable region. These
diff --git a/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp b/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp
index d423d37b9770c69..30a862fa048f97a 100644
--- a/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp
+++ b/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp
@@ -259,8 +259,7 @@ LogicalResult DeadCodeAnalysis::visit(ProgramPoint point) {
   if (isRegionOrCallableReturn(op)) {
     if (auto branch = dyn_cast<RegionBranchOpInterface>(op->getParentOp())) {
       // Visit the exiting terminator of a region.
-      visitRegionTerminator(cast<RegionBranchTerminatorOpInterface>(op),
-                            branch);
+      visitRegionTerminator(op, branch);
     } else if (auto callable =
                    dyn_cast<CallableOpInterface>(op->getParentOp())) {
       // Visit the exiting terminator of a callable.
@@ -379,14 +378,17 @@ void DeadCodeAnalysis::visitRegionBranchOperation(
   }
 }
 
-void DeadCodeAnalysis::visitRegionTerminator(
-    RegionBranchTerminatorOpInterface op, RegionBranchOpInterface branch) {
+void DeadCodeAnalysis::visitRegionTerminator(Operation *op, 
+                                             RegionBranchOpInterface branch) {
   std::optional<SmallVector<Attribute>> operands = getOperandValues(op);
   if (!operands)
     return;
 
   SmallVector<RegionSuccessor> successors;
-  op.getSuccessorRegions(*operands, successors);
+  if (auto terminator = dyn_cast<RegionBranchTerminatorOpInterface>(op))
+    terminator.getSuccessorRegions(*operands, successors);
+  else
+    branch.getSuccessorRegions(op->getParentRegion(), successors);
 
   // Mark successor region entry blocks as executable and add this op to the
   // list of predecessors.
diff --git a/mlir/test/Analysis/DataFlow/test-dead-code-analysis-regression.mlir b/mlir/test/Analysis/DataFlow/test-dead-code-analysis-regression.mlir
new file mode 100644
index 000000000000000..70c44970519cc24
--- /dev/null
+++ b/mlir/test/Analysis/DataFlow/test-dead-code-analysis-regression.mlir
@@ -0,0 +1,5 @@
+// RUN: mlir-opt -int-range-optimizations %s 
+
+%0 = scf.execute_region -> tensor<5x16xi16> {
+  llvm.unreachable
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/69043


More information about the Mlir-commits mailing list