[Mlir-commits] [mlir] [mlir][DeadCodeAnalysis] Fix predecessors when running DeadCodeAnalysis on callable (PR #179146)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Feb 1 14:39:56 PST 2026


https://github.com/neildhar created https://github.com/llvm/llvm-project/pull/179146

`initializeSymbolCallables` only identifies potentially escaping functions inside a `SymbolTable` operation. If the top level operation is itself a callable, we should always treat it as escaping because we are not analysing its potential users.

>From 9962217f803ab41c127330d6401d85632c1a208e Mon Sep 17 00:00:00 2001
From: Neil Dhar <neildhar at meta.com>
Date: Sun, 1 Feb 2026 14:21:50 -0800
Subject: [PATCH] Fix predecessors when running DeadCodeAnalysis on callable

`initializeSymbolCallables` only identifies potentially escaping
functions inside a `SymbolTable` operation. If the top level operation
is itself a callable, we should always treat it as escaping because we
are not analysing its potential users.
---
 mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp      |  6 ++++++
 .../DataFlow/test-dead-code-analysis-func.mlir       | 12 ++++++++++++
 2 files changed, 18 insertions(+)
 create mode 100644 mlir/test/Analysis/DataFlow/test-dead-code-analysis-func.mlir

diff --git a/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp b/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp
index 3ce0f94e0c6da..a71dfd3a42958 100644
--- a/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp
+++ b/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp
@@ -149,6 +149,12 @@ LogicalResult DeadCodeAnalysis::initialize(Operation *top) {
            << OpWithFlags(top, OpPrintingFlags().skipRegions());
   }
 
+  // If the top level op is a callable, we cannot identify all of its callers.
+  if (auto c = dyn_cast<CallableOpInterface>(top)) {
+    auto *state = getOrCreate<PredecessorState>(getProgramPointAfter(c));
+    propagateIfChanged(state, state->setHasUnknownPredecessors());
+  }
+
   // Mark as overdefined the predecessors of symbol callables with potentially
   // unknown predecessors.
   initializeSymbolCallables(top);
diff --git a/mlir/test/Analysis/DataFlow/test-dead-code-analysis-func.mlir b/mlir/test/Analysis/DataFlow/test-dead-code-analysis-func.mlir
new file mode 100644
index 0000000000000..b9c1e72e5be56
--- /dev/null
+++ b/mlir/test/Analysis/DataFlow/test-dead-code-analysis-func.mlir
@@ -0,0 +1,12 @@
+// RUN: mlir-opt --pass-pipeline="builtin.module(func.func(test-dead-code-analysis))" 2>&1 %s | FileCheck %s
+
+// Test that when dead code analysis is run on a single function, we correctly
+// identify that we do not know all of the predecessors.
+// CHECK:      foo:
+// CHECK-NEXT:   region #0
+// CHECK-NEXT:     ^bb0 = live
+// CHECK-NEXT: op_preds: predecessors: (none)
+func.func @foo(%arg0: i32) -> i32
+    attributes {tag = "foo"} {
+  return {a} %arg0 : i32
+}



More information about the Mlir-commits mailing list