[Mlir-commits] [mlir] [mlir][func] Fix a crash in DuplicateFunctionEliminationPass (PR #209667)

Longsheng Mou llvmlistbot at llvm.org
Tue Jul 14 19:59:47 PDT 2026


https://github.com/CoTinker created https://github.com/llvm/llvm-project/pull/209667

Previously, we used `SymbolUserMap::replaceAllUsesWith` to replace symbols, but this could not update the symbol table cached by `SymbolUserMap` during traversal, leading to a crash. This PR switches to `SymbolTable::replaceAllSymbolUses`, which always operates on the latest symbol table and avoids the crash. Fixes #209648.

>From 9a90651c9299881f1d44a1eb5f0d1ec0c626c9d6 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Wed, 15 Jul 2026 10:55:53 +0800
Subject: [PATCH 1/2] [mlir][func] Fix a crash in
 DuplicateFunctionEliminationPass

Previously, we used `SymbolUserMap::replaceAllUsesWith` to replace symbols, but this could not update the symbol table cached by `SymbolUserMap` during traversal, leading to a crash. This PR switches to `SymbolTable::replaceAllSymbolUses`, which always operates on the latest symbol table and avoids the crash.
---
 .../Func/Transforms/DuplicateFunctionElimination.cpp        | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Dialect/Func/Transforms/DuplicateFunctionElimination.cpp b/mlir/lib/Dialect/Func/Transforms/DuplicateFunctionElimination.cpp
index a2186e433f544..c480e469fdcc8 100644
--- a/mlir/lib/Dialect/Func/Transforms/DuplicateFunctionElimination.cpp
+++ b/mlir/lib/Dialect/Func/Transforms/DuplicateFunctionElimination.cpp
@@ -101,12 +101,12 @@ struct DuplicateFunctionEliminationPass
 
     // Update all symbol uses to reference unique func op
     // representants and erase redundant func ops.
-    SymbolTableCollection symbolTable;
-    SymbolUserMap userMap(symbolTable, module);
     for (auto it : toBeErased) {
       StringAttr oldSymbol = it.getSymNameAttr();
       StringAttr newSymbol = getRepresentant[oldSymbol].getSymNameAttr();
-      userMap.replaceAllUsesWith(it, newSymbol);
+      if (failed(
+              SymbolTable::replaceAllSymbolUses(oldSymbol, newSymbol, module)))
+        return signalPassFailure();
       it.erase();
     }
   }

>From ba869255ca1fb1df2ce972a580326558c6f07ec7 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Wed, 15 Jul 2026 10:58:18 +0800
Subject: [PATCH 2/2] add test

---
 .../Func/duplicate-function-elimination.mlir  | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/mlir/test/Dialect/Func/duplicate-function-elimination.mlir b/mlir/test/Dialect/Func/duplicate-function-elimination.mlir
index 4d00d8a954d17..b6588b39c5a87 100644
--- a/mlir/test/Dialect/Func/duplicate-function-elimination.mlir
+++ b/mlir/test/Dialect/Func/duplicate-function-elimination.mlir
@@ -412,3 +412,29 @@ func.func @user(%arg0: tensor<f32>) -> tensor<f32> {
 // CHECK:     @user
 // CHECK-2:     constant @identity
 // CHECK:       call @identity
+
+// -----
+
+func.func @callee0() {
+  return
+}
+
+func.func @caller0() {
+  call @callee1() : () -> ()
+  return
+}
+
+func.func @caller1() {
+  call @callee1() : () -> ()
+  return
+}
+
+func.func @callee1() {
+  return
+}
+
+// CHECK:     @callee0
+// CHECK:     @caller0
+// CHECK:       call @callee0()
+// CHECK-NOT: @caller1
+// CHECK-NOT: @callee1



More information about the Mlir-commits mailing list