Skip to content

Commit c08d972

Browse files
[mlir][bufferization][NFC] Pass DeallocationOptions instead of flags (#80675)
Pass `DeallocationOptions` instead of `privateFuncDynamicOwnership`. This will make it easier to add new options in the future.
1 parent 30f776f commit c08d972

File tree

6 files changed

+33
-26
lines changed

6 files changed

+33
-26
lines changed

mlir/include/mlir/Dialect/Bufferization/Pipelines/Passes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef MLIR_DIALECT_BUFFERIZATION_PIPELINES_PASSES_H
1414
#define MLIR_DIALECT_BUFFERIZATION_PIPELINES_PASSES_H
1515

16+
#include "mlir/Dialect/Bufferization/IR/BufferDeallocationOpInterface.h"
1617
#include "mlir/Pass/PassOptions.h"
1718

1819
namespace mlir {
@@ -28,6 +29,13 @@ struct BufferDeallocationPipelineOptions
2829
"ownership of returned memrefs to callers. This can avoid spurious "
2930
"buffer clones in the callee."),
3031
llvm::cl::init(false)};
32+
33+
/// Implicit conversion to `DeallocationOptions`.
34+
operator DeallocationOptions() const {
35+
DeallocationOptions options;
36+
options.privateFuncDynamicOwnership = privateFunctionDynamicOwnership;
37+
return options;
38+
}
3139
};
3240

3341
//===----------------------------------------------------------------------===//

mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_PASSES_H
22
#define MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_PASSES_H
33

4+
#include "mlir/Dialect/Bufferization/IR/BufferDeallocationOpInterface.h"
45
#include "mlir/Pass/Pass.h"
56

67
namespace mlir {
@@ -31,7 +32,7 @@ std::unique_ptr<Pass> createBufferDeallocationPass();
3132
/// Creates an instance of the OwnershipBasedBufferDeallocation pass to free all
3233
/// allocated buffers.
3334
std::unique_ptr<Pass> createOwnershipBasedBufferDeallocationPass(
34-
bool privateFuncDynamicOwnership = false);
35+
DeallocationOptions options = DeallocationOptions());
3536

3637
/// Creates a pass that optimizes `bufferization.dealloc` operations. For
3738
/// example, it reduces the number of alias checks needed at runtime using
@@ -133,9 +134,9 @@ func::FuncOp buildDeallocationLibraryFunction(OpBuilder &builder, Location loc,
133134
/// Run buffer deallocation.
134135
LogicalResult deallocateBuffers(Operation *op);
135136

136-
/// Run ownership basedbuffer deallocation.
137+
/// Run the ownership-based buffer deallocation.
137138
LogicalResult deallocateBuffersOwnershipBased(FunctionOpInterface op,
138-
bool privateFuncDynamicOwnership);
139+
DeallocationOptions options);
139140

140141
/// Creates a pass that moves allocations upwards to reduce the number of
141142
/// required copies that are inserted during the BufferDeallocation pass.

mlir/lib/Dialect/Bufferization/Pipelines/BufferizationPipelines.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ void mlir::bufferization::buildBufferDeallocationPipeline(
2222
OpPassManager &pm, const BufferDeallocationPipelineOptions &options) {
2323
pm.addPass(memref::createExpandReallocPass(/*emitDeallocs=*/false));
2424
pm.addPass(createCanonicalizerPass());
25-
pm.addPass(createOwnershipBasedBufferDeallocationPass(
26-
options.privateFunctionDynamicOwnership.getValue()));
25+
pm.addPass(createOwnershipBasedBufferDeallocationPass(options));
2726
pm.addPass(createCanonicalizerPass());
2827
pm.addPass(createBufferDeallocationSimplificationPass());
2928
pm.addPass(createLowerDeallocationsPass());

mlir/lib/Dialect/Bufferization/Pipelines/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_mlir_dialect_library(MLIRBufferizationPipelines
55
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/Bufferization
66

77
LINK_LIBS PUBLIC
8+
MLIRBufferizationDialect
89
MLIRBufferizationTransforms
910
MLIRMemRefTransforms
1011
MLIRFuncDialect

mlir/lib/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,8 @@ namespace {
166166
/// program have a corresponding de-allocation.
167167
class BufferDeallocation {
168168
public:
169-
BufferDeallocation(Operation *op, bool privateFuncDynamicOwnership)
170-
: state(op) {
171-
options.privateFuncDynamicOwnership = privateFuncDynamicOwnership;
172-
}
169+
BufferDeallocation(Operation *op, DeallocationOptions options)
170+
: state(op), options(options) {}
173171

174172
/// Performs the actual placement/creation of all dealloc operations.
175173
LogicalResult deallocate(FunctionOpInterface op);
@@ -586,13 +584,9 @@ BufferDeallocation::updateFunctionSignature(FunctionOpInterface op) {
586584
if (!returnOperandTypes.empty())
587585
resultTypes = returnOperandTypes[0];
588586

589-
// TODO: it would be nice if the FunctionOpInterface had a method to not only
590-
// get the function type but also set it.
591-
op->setAttr(
592-
"function_type",
593-
TypeAttr::get(FunctionType::get(
594-
op->getContext(), op.getFunctionBody().front().getArgumentTypes(),
595-
resultTypes)));
587+
op.setFunctionTypeAttr(TypeAttr::get(FunctionType::get(
588+
op->getContext(), op.getFunctionBody().front().getArgumentTypes(),
589+
resultTypes)));
596590

597591
return success();
598592
}
@@ -1027,17 +1021,20 @@ struct OwnershipBasedBufferDeallocationPass
10271021
: public bufferization::impl::OwnershipBasedBufferDeallocationBase<
10281022
OwnershipBasedBufferDeallocationPass> {
10291023
OwnershipBasedBufferDeallocationPass() = default;
1030-
OwnershipBasedBufferDeallocationPass(bool privateFuncDynamicOwnership)
1024+
OwnershipBasedBufferDeallocationPass(DeallocationOptions options)
10311025
: OwnershipBasedBufferDeallocationPass() {
1032-
this->privateFuncDynamicOwnership.setValue(privateFuncDynamicOwnership);
1026+
this->privateFuncDynamicOwnership.setValue(
1027+
options.privateFuncDynamicOwnership);
10331028
}
10341029
void runOnOperation() override {
1030+
DeallocationOptions options;
1031+
options.privateFuncDynamicOwnership = privateFuncDynamicOwnership;
1032+
10351033
auto status = getOperation()->walk([&](func::FuncOp func) {
10361034
if (func.isExternal())
10371035
return WalkResult::skip();
10381036

1039-
if (failed(deallocateBuffersOwnershipBased(func,
1040-
privateFuncDynamicOwnership)))
1037+
if (failed(deallocateBuffersOwnershipBased(func, options)))
10411038
return WalkResult::interrupt();
10421039

10431040
return WalkResult::advance();
@@ -1053,10 +1050,11 @@ struct OwnershipBasedBufferDeallocationPass
10531050
// Implement bufferization API
10541051
//===----------------------------------------------------------------------===//
10551052

1056-
LogicalResult bufferization::deallocateBuffersOwnershipBased(
1057-
FunctionOpInterface op, bool privateFuncDynamicOwnership) {
1053+
LogicalResult
1054+
bufferization::deallocateBuffersOwnershipBased(FunctionOpInterface op,
1055+
DeallocationOptions options) {
10581056
// Gather all required allocation nodes and prepare the deallocation phase.
1059-
BufferDeallocation deallocation(op, privateFuncDynamicOwnership);
1057+
BufferDeallocation deallocation(op, options);
10601058

10611059
// Place all required temporary clone and dealloc nodes.
10621060
return deallocation.deallocate(op);
@@ -1068,7 +1066,6 @@ LogicalResult bufferization::deallocateBuffersOwnershipBased(
10681066

10691067
std::unique_ptr<Pass>
10701068
mlir::bufferization::createOwnershipBasedBufferDeallocationPass(
1071-
bool privateFuncDynamicOwnership) {
1072-
return std::make_unique<OwnershipBasedBufferDeallocationPass>(
1073-
privateFuncDynamicOwnership);
1069+
DeallocationOptions options) {
1070+
return std::make_unique<OwnershipBasedBufferDeallocationPass>(options);
10741071
}

utils/bazel/llvm-project-overlay/mlir/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13240,6 +13240,7 @@ cc_library(
1324013240
hdrs = ["include/mlir/Dialect/Bufferization/Pipelines/Passes.h"],
1324113241
includes = ["include"],
1324213242
deps = [
13243+
":BufferizationDialect",
1324313244
":BufferizationToMemRef",
1324413245
":BufferizationTransforms",
1324513246
":FuncDialect",

0 commit comments

Comments
 (0)