Skip to content

[MLIR] Add bufferization state class to OneShotBufferization pass #138143

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 7 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -578,6 +578,81 @@ class AnalysisState {
insideMutuallyExclusiveRegionsCache;
};

/// BufferizationState provides information about the state of the IR during the
/// bufferization process.
class BufferizationState {
public:
/// Base class for BufferizationState extensions that allow BufferizationState
/// to contain user-specified information in the state object. The extension
/// mechanism of BufferizationState mirrors the one of OneShotAnalysisState.
class Extension {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this entire extension mechanism is not needed anymore. Let's remove it for now, and bring it back if we find a use case for it in the future.

public:
/// Base virtual destructor.
// Out-of-line definition ensures symbols are emitted in a single object
// file.
virtual ~Extension();

protected:
/// Constructs an extension of the given state object.
Extension(BufferizationState &state) : state(state) {}

/// Provides read-only access to the parent OneShotAnalysisState object.
const BufferizationState &getBufferizationState() const { return state; }

private:
/// Back-reference to the state that is being extended.
BufferizationState &state;
};

/// Adds a new Extension of the type specified as template parameter,
/// constructing it with the arguments provided. The extension is owned by the
/// BufferizationState. It is expected that the state does not already have an
/// extension of the same type. Extension constructors are expected to take a
/// reference to BufferizationState as first argument, automatically supplied
/// by this call.
template <typename Ty, typename... Args>
Ty &addExtension(Args &&...args) {
static_assert(std::is_base_of<Extension, Ty>::value,
"only a class derived from "
"BufferizationState::Extension is allowed");
auto ptr = std::make_unique<Ty>(*this, std::forward<Args>(args)...);
auto result = extensions.try_emplace(TypeID::get<Ty>(), std::move(ptr));
assert(result.second && "extension already added");
return *static_cast<Ty *>(result.first->second.get());
}

/// Returns the extension of the specified type.
template <typename Ty>
Ty *getExtension() {
static_assert(std::is_base_of<Extension, Ty>::value,
"only a class derived from "
"BufferizationState::Extension is allowed");
auto iter = extensions.find(TypeID::get<Ty>());
if (iter == extensions.end())
return nullptr;
return static_cast<Ty *>(iter->second.get());
}

/// Returns the extension of the specified type.
template <typename Ty>
const Ty *getExtension() const {
return const_cast<BufferizationState *>(this)->getExtension<Ty>();
}

/// Get a reference to the collection of cached symbol tables.
SymbolTableCollection &getSymbolTables();

private:
/// Extensions attached to the state, identified by the TypeID of their type.
/// Only one extension of any given type is allowed.
DenseMap<TypeID, std::unique_ptr<Extension>> extensions;

/// The cached symbol tables.
/// The user is expected to update / invalidate the cached symbol tables if
/// the bufferized operation has the Symbol or SymbolTable traits.
SymbolTableCollection symbolTables;
};

/// Create an AllocTensorOp for the given shaped value (memref or tensor).
/// If `copy` is set, the shaped value is copied. Otherwise, a tensor with
/// undefined contents is allocated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ def BufferizableOpInterface : OpInterface<"BufferizableOpInterface"> {
/*retType=*/"::llvm::LogicalResult",
/*methodName=*/"bufferize",
/*args=*/(ins "::mlir::RewriterBase &":$rewriter,
"const ::mlir::bufferization::BufferizationOptions &":$options),
"const ::mlir::bufferization::BufferizationOptions &":$options,
"::mlir::bufferization::BufferizationState &":$state),
/*methodBody=*/"",
/*defaultImplementation=*/[{
llvm_unreachable("bufferize not implemented");
Expand Down
15 changes: 10 additions & 5 deletions mlir/include/mlir/Dialect/Bufferization/IR/BufferizationOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def Bufferization_AllocTensorOp : Bufferization_Op<"alloc_tensor",

let extraClassDeclaration = [{
LogicalResult bufferize(RewriterBase &rewriter,
const BufferizationOptions &options);
const BufferizationOptions &options,
BufferizationState &state);

bool resultBufferizesToMemoryWrite(OpResult opResult,
const AnalysisState &state);
Expand Down Expand Up @@ -282,7 +283,8 @@ def Bufferization_MaterializeInDestinationOp

let extraClassDeclaration = [{
LogicalResult bufferize(RewriterBase &rewriter,
const BufferizationOptions &options);
const BufferizationOptions &options,
BufferizationState &state);

bool bufferizesToMemoryRead(OpOperand &opOperand,
const AnalysisState &state);
Expand Down Expand Up @@ -375,7 +377,8 @@ def Bufferization_DeallocTensorOp : Bufferization_Op<"dealloc_tensor",
}

LogicalResult bufferize(RewriterBase &rewriter,
const BufferizationOptions &options);
const BufferizationOptions &options,
BufferizationState &state);
}];
}

Expand Down Expand Up @@ -458,7 +461,8 @@ def Bufferization_ToTensorOp : Bufferization_Op<"to_tensor", [
//===------------------------------------------------------------------===//

LogicalResult bufferize(RewriterBase &rewriter,
const BufferizationOptions &options) const {
const BufferizationOptions &options,
BufferizationState &state) const {
// to_tensor/to_buffer pairs fold away after bufferization.
return success();
}
Expand Down Expand Up @@ -550,7 +554,8 @@ def Bufferization_ToBufferOp : Bufferization_Op<"to_buffer", [
}

LogicalResult bufferize(RewriterBase &rewriter,
const BufferizationOptions &options);
const BufferizationOptions &options,
BufferizationState &state);
}];

let assemblyFormat = [{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class GlobalOp;
} // namespace memref

namespace bufferization {
class BufferizationState;

/// A simple analysis that detects allocation operations.
class BufferPlacementAllocs {
Expand Down Expand Up @@ -122,9 +123,14 @@ class BufferPlacementTransformationBase {
// Globals are created lazily at the top of the enclosing ModuleOp with pretty
// names. Duplicates are avoided.
FailureOr<memref::GlobalOp> getGlobalFor(arith::ConstantOp constantOp,
SymbolTableCollection &symbolTables,
uint64_t alignment,
Attribute memorySpace = {});

void removeSymbol(Operation *op, BufferizationState &state);

void insertSymbol(Operation *op, BufferizationState &state);

} // namespace bufferization
} // namespace mlir

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct BufferizationStatistics {
/// additional buffer copies or set "options.copyBeforeWrite = true". The
/// general bufferization entry point is `runOneShotBufferize`.
LogicalResult bufferizeOp(Operation *op, const BufferizationOptions &options,
BufferizationState &bufferizationState,
BufferizationStatistics *statistics = nullptr);

/// Bufferize the signature of `block` and its callers (i.e., ops that have the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ LogicalResult analyzeOp(Operation *op, OneShotAnalysisState &state,
/// Run One-Shot Bufferize on the given op: Analysis + Bufferization
LogicalResult
runOneShotBufferize(Operation *op, const OneShotBufferizationOptions &options,
BufferizationState &state,
BufferizationStatistics *statistics = nullptr);

} // namespace bufferization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace bufferization {
struct BufferizationStatistics;
class OneShotAnalysisState;
struct OneShotBufferizationOptions;
class BufferizationState;

/// Analyze `moduleOp` and its nested ops. Bufferization decisions are stored in
/// `state`.
Expand All @@ -38,6 +39,7 @@ analyzeModuleOp(ModuleOp moduleOp, OneShotAnalysisState &state,
/// will be inserted only to these FuncOps.
llvm::LogicalResult
bufferizeModuleOp(ModuleOp moduleOp, const OneShotBufferizationOptions &options,
BufferizationState &state,
BufferizationStatistics *statistics = nullptr);

/// Remove bufferization attributes on every FuncOp arguments in the ModuleOp.
Expand All @@ -50,7 +52,7 @@ void removeBufferizationAttributesInModule(ModuleOp moduleOp);
llvm::LogicalResult runOneShotModuleBufferize(
ModuleOp moduleOp,
const bufferization::OneShotBufferizationOptions &options,
BufferizationStatistics *statistics = nullptr);
BufferizationState &state, BufferizationStatistics *statistics = nullptr);

} // namespace bufferization
} // namespace mlir
Expand Down
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/Linalg/Transforms/Transforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace mlir {
namespace bufferization {
class AllocTensorOp;
class OneShotAnalysisState;
class BufferizationState;
} // namespace bufferization

namespace linalg {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ struct ConstantOpInterface
: public BufferizableOpInterface::ExternalModel<ConstantOpInterface,
arith::ConstantOp> {
LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const BufferizationOptions &options) const {
const BufferizationOptions &options,
BufferizationState &state) const {
auto constantOp = cast<arith::ConstantOp>(op);
auto type = dyn_cast<RankedTensorType>(constantOp.getType());

Expand All @@ -46,7 +47,8 @@ struct ConstantOpInterface
// Create global memory segment and replace tensor with memref pointing to
// that memory segment.
FailureOr<memref::GlobalOp> globalOp =
getGlobalFor(constantOp, options.bufferAlignment, memorySpace);
getGlobalFor(constantOp, state.getSymbolTables(),
options.bufferAlignment, memorySpace);
if (failed(globalOp))
return failure();
memref::GlobalOp globalMemref = *globalOp;
Expand Down Expand Up @@ -83,7 +85,8 @@ struct IndexCastOpInterface
}

LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const BufferizationOptions &options) const {
const BufferizationOptions &options,
BufferizationState &state) const {
auto castOp = cast<arith::IndexCastOp>(op);
auto resultTensorType = cast<TensorType>(castOp.getType());

Expand Down Expand Up @@ -131,7 +134,8 @@ struct SelectOpInterface
}

LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const BufferizationOptions &options) const {
const BufferizationOptions &options,
BufferizationState &state) const {
auto selectOp = cast<arith::SelectOp>(op);
Location loc = selectOp.getLoc();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ void AnalysisState::resetCache() {
insideMutuallyExclusiveRegionsCache.clear();
}

BufferizationState::Extension::~Extension() = default;

SymbolTableCollection &BufferizationState::getSymbolTables() {
return symbolTables;
}

Region *bufferization::getNextEnclosingRepetitiveRegion(
Region *region, const BufferizationOptions &options) {
assert(isRepetitiveRegion(region, options) && "expected repetitive region");
Expand Down
12 changes: 8 additions & 4 deletions mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ void mlir::bufferization::populateDynamicDimSizes(
//===----------------------------------------------------------------------===//

LogicalResult AllocTensorOp::bufferize(RewriterBase &rewriter,
const BufferizationOptions &options) {
const BufferizationOptions &options,
BufferizationState &state) {
OpBuilder::InsertionGuard g(rewriter);
Location loc = getLoc();

Expand Down Expand Up @@ -529,7 +530,8 @@ void CloneOp::getCanonicalizationPatterns(RewritePatternSet &results,
//===----------------------------------------------------------------------===//

LogicalResult DeallocTensorOp::bufferize(RewriterBase &rewriter,
const BufferizationOptions &options) {
const BufferizationOptions &options,
BufferizationState &state) {
FailureOr<Value> buffer = getBuffer(rewriter, getTensor(), options);
if (failed(buffer))
return failure();
Expand Down Expand Up @@ -576,7 +578,8 @@ MaterializeInDestinationOp::getAliasingValues(OpOperand &opOperand,

LogicalResult
MaterializeInDestinationOp::bufferize(RewriterBase &rewriter,
const BufferizationOptions &options) {
const BufferizationOptions &options,
BufferizationState &state) {
bool tensorDest = isa<TensorType>(getDest().getType());
Value buffer;
if (tensorDest) {
Expand Down Expand Up @@ -861,7 +864,8 @@ void ToBufferOp::getCanonicalizationPatterns(RewritePatternSet &results,
}

LogicalResult ToBufferOp::bufferize(RewriterBase &rewriter,
const BufferizationOptions &options) {
const BufferizationOptions &options,
BufferizationState &state) {
// Fold to_buffer(to_tensor(x)) to x. Insert a cast if necessary.
(void)foldToBufferToTensorPair(rewriter, *this, options);
// Note: The return value of `bufferize` indicates whether there was an error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,21 @@ transform::OneShotBufferizeOp::apply(transform::TransformRewriter &rewriter,
}

auto payloadOps = state.getPayloadOps(getTarget());
BufferizationState bufferizationState;

for (Operation *target : payloadOps) {
if (!isa<ModuleOp, FunctionOpInterface>(target))
return emitSilenceableError() << "expected module or function target";
auto moduleOp = dyn_cast<ModuleOp>(target);
if (options.bufferizeFunctionBoundaries) {
if (!moduleOp)
return emitSilenceableError() << "expected module target";
if (failed(bufferization::runOneShotModuleBufferize(moduleOp, options)))
if (failed(bufferization::runOneShotModuleBufferize(moduleOp, options,
bufferizationState)))
return emitSilenceableError() << "bufferization failed";
} else {
if (failed(bufferization::runOneShotBufferize(target, options)))
if (failed(bufferization::runOneShotBufferize(target, options,
bufferizationState)))
return emitSilenceableError() << "bufferization failed";
}
}
Expand Down Expand Up @@ -162,6 +166,7 @@ class BufferizationTransformDialectExtension
registerTransformOps<
#define GET_OP_LIST
#include "mlir/Dialect/Bufferization/TransformOps/BufferizationTransformOps.cpp.inc"

>();
}
};
Expand Down
23 changes: 20 additions & 3 deletions mlir/lib/Dialect/Bufferization/Transforms/BufferUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ BufferPlacementTransformationBase::BufferPlacementTransformationBase(
//===----------------------------------------------------------------------===//

FailureOr<memref::GlobalOp>
bufferization::getGlobalFor(arith::ConstantOp constantOp, uint64_t alignment,
Attribute memorySpace) {
bufferization::getGlobalFor(arith::ConstantOp constantOp,
SymbolTableCollection &symbolTables,
uint64_t alignment, Attribute memorySpace) {
auto type = cast<RankedTensorType>(constantOp.getType());
auto moduleOp = constantOp->getParentOfType<ModuleOp>();
if (!moduleOp)
Expand All @@ -127,7 +128,7 @@ bufferization::getGlobalFor(arith::ConstantOp constantOp, uint64_t alignment,
// Create a builder without an insertion point. We will insert using the
// symbol table to guarantee unique names.
OpBuilder globalBuilder(moduleOp.getContext());
SymbolTable symbolTable(moduleOp);
SymbolTable &symbolTable = symbolTables.getSymbolTable(moduleOp);

// Create a pretty name.
SmallString<64> buf;
Expand Down Expand Up @@ -158,3 +159,19 @@ bufferization::getGlobalFor(arith::ConstantOp constantOp, uint64_t alignment,
global->moveBefore(&moduleOp.front());
return global;
}

namespace mlir::bufferization {
void removeSymbol(Operation *op, BufferizationState &state) {
SymbolTable &symbolTable = state.getSymbolTables().getSymbolTable(
op->getParentWithTrait<OpTrait::SymbolTable>());

symbolTable.remove(op);
}

void insertSymbol(Operation *op, BufferizationState &state) {
SymbolTable &symbolTable = state.getSymbolTables().getSymbolTable(
op->getParentWithTrait<OpTrait::SymbolTable>());

symbolTable.insert(op);
}
} // namespace mlir::bufferization
Loading
Loading