Skip to content

[MLIR] Keep cached symbol tables across buffer deallocation insertions #141956

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 4 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct DeallocationOptions {
/// BufferDeallocation pass.
class DeallocationState {
public:
DeallocationState(Operation *op);
DeallocationState(Operation *op, SymbolTableCollection &symbolTables);

// The state should always be passed by reference.
DeallocationState(const DeallocationState &) = delete;
Expand Down Expand Up @@ -189,7 +189,7 @@ class DeallocationState {
private:
// Symbol cache to lookup functions from call operations to check attributes
// on the function operation.
SymbolTableCollection symbolTable;
SymbolTableCollection &symbolTable;

// Mapping from each SSA value with MemRef type to the associated ownership in
// each block.
Expand Down
6 changes: 4 additions & 2 deletions mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ func::FuncOp buildDeallocationLibraryFunction(OpBuilder &builder, Location loc,
SymbolTable &symbolTable);

/// Run the ownership-based buffer deallocation.
LogicalResult deallocateBuffersOwnershipBased(FunctionOpInterface op,
DeallocationOptions options);
LogicalResult
deallocateBuffersOwnershipBased(FunctionOpInterface op,
DeallocationOptions options,
SymbolTableCollection &symbolTables);

// Options struct for BufferResultsToOutParams pass.
// Note: defined only here, not in tablegen.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ void Ownership::combine(Ownership other) { *this = getCombined(other); }
// DeallocationState
//===----------------------------------------------------------------------===//

DeallocationState::DeallocationState(Operation *op) : liveness(op) {}
DeallocationState::DeallocationState(Operation *op,
SymbolTableCollection &symbolTables)
: symbolTable(symbolTables), liveness(op) {}

void DeallocationState::updateOwnership(Value memref, Ownership ownership,
Block *block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ namespace {
/// program have a corresponding de-allocation.
class BufferDeallocation {
public:
BufferDeallocation(Operation *op, DeallocationOptions options)
: state(op), options(options) {}
BufferDeallocation(Operation *op, DeallocationOptions options,
SymbolTableCollection &symbolTables)
: state(op, symbolTables), options(options) {}

/// Performs the actual placement/creation of all dealloc operations.
LogicalResult deallocate(FunctionOpInterface op);
Expand Down Expand Up @@ -1027,11 +1028,13 @@ struct OwnershipBasedBufferDeallocationPass
DeallocationOptions options;
options.privateFuncDynamicOwnership = privateFuncDynamicOwnership;

mlir::SymbolTableCollection symbolTables;

auto status = getOperation()->walk([&](func::FuncOp func) {
if (func.isExternal())
return WalkResult::skip();

if (failed(deallocateBuffersOwnershipBased(func, options)))
if (failed(deallocateBuffersOwnershipBased(func, options, symbolTables)))
return WalkResult::interrupt();

return WalkResult::advance();
Expand All @@ -1047,11 +1050,11 @@ struct OwnershipBasedBufferDeallocationPass
// Implement bufferization API
//===----------------------------------------------------------------------===//

LogicalResult
bufferization::deallocateBuffersOwnershipBased(FunctionOpInterface op,
DeallocationOptions options) {
LogicalResult bufferization::deallocateBuffersOwnershipBased(
FunctionOpInterface op, DeallocationOptions options,
SymbolTableCollection &symbolTables) {
// Gather all required allocation nodes and prepare the deallocation phase.
BufferDeallocation deallocation(op, options);
BufferDeallocation deallocation(op, options, symbolTables);

// Place all required temporary clone and dealloc nodes.
return deallocation.deallocate(op);
Expand Down