Skip to content

[MLIR] Add method to invalidate cached symbol table #138014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025

Conversation

mscuttari
Copy link
Member

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.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Apr 30, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 30, 2025

@llvm/pr-subscribers-mlir-core

Author: Michele Scuttari (mscuttari)

Changes

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.


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

2 Files Affected:

  • (modified) mlir/include/mlir/IR/SymbolTable.h (+7)
  • (modified) mlir/lib/IR/SymbolTable.cpp (+7)
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
 //===----------------------------------------------------------------------===//

@llvmbot
Copy link
Member

llvmbot commented Apr 30, 2025

@llvm/pr-subscribers-mlir

Author: Michele Scuttari (mscuttari)

Changes

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.


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

2 Files Affected:

  • (modified) mlir/include/mlir/IR/SymbolTable.h (+7)
  • (modified) mlir/lib/IR/SymbolTable.cpp (+7)
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
 //===----------------------------------------------------------------------===//

@mscuttari mscuttari changed the title Add method to invalidate cached symbol table [MLIR] Add method to invalidate cached symbol table Apr 30, 2025
@mscuttari
Copy link
Member Author

@River707 It's the first time I land changes since the switch from Phabricator to Github, so I'd like to be sure that I'm not breaking any rules. Is it fine if I merge without the buildkite checks succeeding? Seems like it can't resolve the commit hash, but I don't understand why.

@River707
Copy link
Contributor

Ideally it should be green, weird that it's red. Can you try rebasing?

@mscuttari mscuttari force-pushed the symbol-table-collection branch from b812dc1 to bef614e Compare April 30, 2025 21:04
@mscuttari mscuttari merged commit 7ec1e0f into llvm:main May 1, 2025
11 checks passed
@mscuttari mscuttari deleted the symbol-table-collection branch May 1, 2025 07:12
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
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.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
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.
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
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.
mscuttari added a commit that referenced this pull request May 22, 2025
…38143)

This PR is a follow-up on #138125, and adds a bufferization state class providing information about the IR. The information currently consists of a cached list of symbol tables, which aims to solve the quadratic scaling of the bufferization task with respect to the number of symbols. The PR breaks API compatibility: the `bufferize` method of the `BufferizableOpInterface` has been enriched with a reference to a `BufferizationState` object.

The bufferization state must be kept in a valid state by the interface implementations. For example, if an operation with the `Symbol` trait is inserted or replaced, its parent `SymbolTable` must be updated accordingly (see, for example, the bufferization of `arith::ConstantOp`, where the symbol table of the module gets the new global symbol inserted). Similarly, the invalidation of a symbol table must be performed if an operation with the `SymbolTable` trait is removed (this can be performed using the `invalidateSymbolTable` method, introduced in #138014).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants