[Mlir-commits] [mlir] Add method to invalidate cached symbol table (PR #138014)

Michele Scuttari llvmlistbot at llvm.org
Wed Apr 30 11:39:43 PDT 2025


https://github.com/mscuttari created https://github.com/llvm/llvm-project/pull/138014

This PR adds a method to the `SymbolTableCollection` class to invalidate the cached symbol table for an operation.

This is important when doing IR modifications that erase and also create operations having the `OpTrait::SymbolTable` trait. If a symbol table of an erased operation is not invalidated, a new operation sharing the same address would be associated with outdated, and wrong, information.

>From 360018bb428b14b352ba2b6421b8f23cd856764e Mon Sep 17 00:00:00 2001
From: Michele Scuttari <michele.scuttari at outlook.com>
Date: Wed, 30 Apr 2025 20:38:05 +0200
Subject: [PATCH] Add method to invalidate cached symbol table

---
 mlir/include/mlir/IR/SymbolTable.h | 7 +++++++
 mlir/lib/IR/SymbolTable.cpp        | 7 +++++++
 2 files changed, 14 insertions(+)

diff --git a/mlir/include/mlir/IR/SymbolTable.h b/mlir/include/mlir/IR/SymbolTable.h
index 597c6a9a1d891..a22d6f3a0426b 100644
--- a/mlir/include/mlir/IR/SymbolTable.h
+++ b/mlir/include/mlir/IR/SymbolTable.h
@@ -316,6 +316,13 @@ class SymbolTableCollection {
   /// Lookup, or create, a symbol table for an operation.
   SymbolTable &getSymbolTable(Operation *op);
 
+  /// Invalidate the cached symbol table for an operation.
+  /// This is important when doing IR modifications that erase and also create
+  /// operations having the 'OpTrait::SymbolTable' trait. If a symbol table of
+  /// an erased operation is not invalidated, a new operation sharing the same
+  /// address would be associated with outdated, and wrong, information.
+  void invalidateSymbolTable(Operation *op);
+
 private:
   friend class LockedSymbolTableCollection;
 
diff --git a/mlir/lib/IR/SymbolTable.cpp b/mlir/lib/IR/SymbolTable.cpp
index 76c9b7b1e8afa..6a97adaba6cdc 100644
--- a/mlir/lib/IR/SymbolTable.cpp
+++ b/mlir/lib/IR/SymbolTable.cpp
@@ -998,6 +998,13 @@ SymbolTable &SymbolTableCollection::getSymbolTable(Operation *op) {
   return *it.first->second;
 }
 
+void SymbolTableCollection::invalidateSymbolTable(Operation *op) {
+  auto it = symbolTables.find(op);
+
+  if (it != symbolTables.end())
+    symbolTables.erase(it);
+}
+
 //===----------------------------------------------------------------------===//
 // LockedSymbolTableCollection
 //===----------------------------------------------------------------------===//



More information about the Mlir-commits mailing list