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

Justin Fargnoli llvmlistbot at llvm.org
Fri Oct 13 22:20:02 PDT 2023


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

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`. 

>From 8f8b644792d5e26f76d98bd8ba45440443f9a260 Mon Sep 17 00:00:00 2001
From: justinfargnoli <justinfargnoli at gmail.com>
Date: Fri, 13 Oct 2023 18:16:45 -0700
Subject: [PATCH 1/2] Initial commit

---
 .../mlir/Analysis/DataFlow/DeadCodeAnalysis.h        |  2 +-
 mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp      | 12 +++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)

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.

>From ec79bc71013c8433a83948a256b313a0f93cf826 Mon Sep 17 00:00:00 2001
From: justinfargnoli <justinfargnoli at gmail.com>
Date: Fri, 13 Oct 2023 18:34:11 -0700
Subject: [PATCH 2/2] Add test

---
 .../DataFlow/test-dead-code-analysis-regression.mlir         | 5 +++++
 1 file changed, 5 insertions(+)
 create mode 100644 mlir/test/Analysis/DataFlow/test-dead-code-analysis-regression.mlir

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
+}



More information about the Mlir-commits mailing list