diff options
author | Matthias Springer <me@m-sp.org> | 2024-02-05 17:20:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-05 17:20:55 +0100 |
commit | c08d972a0043fe67de65ba331a144425c8cea449 (patch) | |
tree | 26455a395353cf783786a03535a187a822ed5234 | |
parent | 30f776f8149dcbda0b6467176488e6551d068e40 (diff) | |
download | llvm-c08d972a0043fe67de65ba331a144425c8cea449.zip llvm-c08d972a0043fe67de65ba331a144425c8cea449.tar.gz llvm-c08d972a0043fe67de65ba331a144425c8cea449.tar.bz2 |
[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.
6 files changed, 33 insertions, 26 deletions
diff --git a/mlir/include/mlir/Dialect/Bufferization/Pipelines/Passes.h b/mlir/include/mlir/Dialect/Bufferization/Pipelines/Passes.h index 32565b6..f220d20 100644 --- a/mlir/include/mlir/Dialect/Bufferization/Pipelines/Passes.h +++ b/mlir/include/mlir/Dialect/Bufferization/Pipelines/Passes.h @@ -13,6 +13,7 @@ #ifndef MLIR_DIALECT_BUFFERIZATION_PIPELINES_PASSES_H #define MLIR_DIALECT_BUFFERIZATION_PIPELINES_PASSES_H +#include "mlir/Dialect/Bufferization/IR/BufferDeallocationOpInterface.h" #include "mlir/Pass/PassOptions.h" namespace mlir { @@ -28,6 +29,13 @@ struct BufferDeallocationPipelineOptions "ownership of returned memrefs to callers. This can avoid spurious " "buffer clones in the callee."), llvm::cl::init(false)}; + + /// Implicit conversion to `DeallocationOptions`. + operator DeallocationOptions() const { + DeallocationOptions options; + options.privateFuncDynamicOwnership = privateFunctionDynamicOwnership; + return options; + } }; //===----------------------------------------------------------------------===// diff --git a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h index a6f668b..bb4b522 100644 --- a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h +++ b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h @@ -1,6 +1,7 @@ #ifndef MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_PASSES_H #define MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_PASSES_H +#include "mlir/Dialect/Bufferization/IR/BufferDeallocationOpInterface.h" #include "mlir/Pass/Pass.h" namespace mlir { @@ -31,7 +32,7 @@ std::unique_ptr<Pass> createBufferDeallocationPass(); /// Creates an instance of the OwnershipBasedBufferDeallocation pass to free all /// allocated buffers. std::unique_ptr<Pass> createOwnershipBasedBufferDeallocationPass( - bool privateFuncDynamicOwnership = false); + DeallocationOptions options = DeallocationOptions()); /// Creates a pass that optimizes `bufferization.dealloc` operations. For /// example, it reduces the number of alias checks needed at runtime using @@ -133,9 +134,9 @@ func::FuncOp buildDeallocationLibraryFunction(OpBuilder &builder, Location loc, /// Run buffer deallocation. LogicalResult deallocateBuffers(Operation *op); -/// Run ownership basedbuffer deallocation. +/// Run the ownership-based buffer deallocation. LogicalResult deallocateBuffersOwnershipBased(FunctionOpInterface op, - bool privateFuncDynamicOwnership); + DeallocationOptions options); /// Creates a pass that moves allocations upwards to reduce the number of /// required copies that are inserted during the BufferDeallocation pass. diff --git a/mlir/lib/Dialect/Bufferization/Pipelines/BufferizationPipelines.cpp b/mlir/lib/Dialect/Bufferization/Pipelines/BufferizationPipelines.cpp index a2878f0..7124eab 100644 --- a/mlir/lib/Dialect/Bufferization/Pipelines/BufferizationPipelines.cpp +++ b/mlir/lib/Dialect/Bufferization/Pipelines/BufferizationPipelines.cpp @@ -22,8 +22,7 @@ void mlir::bufferization::buildBufferDeallocationPipeline( OpPassManager &pm, const BufferDeallocationPipelineOptions &options) { pm.addPass(memref::createExpandReallocPass(/*emitDeallocs=*/false)); pm.addPass(createCanonicalizerPass()); - pm.addPass(createOwnershipBasedBufferDeallocationPass( - options.privateFunctionDynamicOwnership.getValue())); + pm.addPass(createOwnershipBasedBufferDeallocationPass(options)); pm.addPass(createCanonicalizerPass()); pm.addPass(createBufferDeallocationSimplificationPass()); pm.addPass(createLowerDeallocationsPass()); diff --git a/mlir/lib/Dialect/Bufferization/Pipelines/CMakeLists.txt b/mlir/lib/Dialect/Bufferization/Pipelines/CMakeLists.txt index 6e8dab6..d67b28b 100644 --- a/mlir/lib/Dialect/Bufferization/Pipelines/CMakeLists.txt +++ b/mlir/lib/Dialect/Bufferization/Pipelines/CMakeLists.txt @@ -5,6 +5,7 @@ add_mlir_dialect_library(MLIRBufferizationPipelines ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/Bufferization LINK_LIBS PUBLIC + MLIRBufferizationDialect MLIRBufferizationTransforms MLIRMemRefTransforms MLIRFuncDialect diff --git a/mlir/lib/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation.cpp b/mlir/lib/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation.cpp index fa4ea2c..0a7494c 100644 --- a/mlir/lib/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation.cpp +++ b/mlir/lib/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation.cpp @@ -166,10 +166,8 @@ namespace { /// program have a corresponding de-allocation. class BufferDeallocation { public: - BufferDeallocation(Operation *op, bool privateFuncDynamicOwnership) - : state(op) { - options.privateFuncDynamicOwnership = privateFuncDynamicOwnership; - } + BufferDeallocation(Operation *op, DeallocationOptions options) + : state(op), options(options) {} /// Performs the actual placement/creation of all dealloc operations. LogicalResult deallocate(FunctionOpInterface op); @@ -586,13 +584,9 @@ BufferDeallocation::updateFunctionSignature(FunctionOpInterface op) { if (!returnOperandTypes.empty()) resultTypes = returnOperandTypes[0]; - // TODO: it would be nice if the FunctionOpInterface had a method to not only - // get the function type but also set it. - op->setAttr( - "function_type", - TypeAttr::get(FunctionType::get( - op->getContext(), op.getFunctionBody().front().getArgumentTypes(), - resultTypes))); + op.setFunctionTypeAttr(TypeAttr::get(FunctionType::get( + op->getContext(), op.getFunctionBody().front().getArgumentTypes(), + resultTypes))); return success(); } @@ -1027,17 +1021,20 @@ struct OwnershipBasedBufferDeallocationPass : public bufferization::impl::OwnershipBasedBufferDeallocationBase< OwnershipBasedBufferDeallocationPass> { OwnershipBasedBufferDeallocationPass() = default; - OwnershipBasedBufferDeallocationPass(bool privateFuncDynamicOwnership) + OwnershipBasedBufferDeallocationPass(DeallocationOptions options) : OwnershipBasedBufferDeallocationPass() { - this->privateFuncDynamicOwnership.setValue(privateFuncDynamicOwnership); + this->privateFuncDynamicOwnership.setValue( + options.privateFuncDynamicOwnership); } void runOnOperation() override { + DeallocationOptions options; + options.privateFuncDynamicOwnership = privateFuncDynamicOwnership; + auto status = getOperation()->walk([&](func::FuncOp func) { if (func.isExternal()) return WalkResult::skip(); - if (failed(deallocateBuffersOwnershipBased(func, - privateFuncDynamicOwnership))) + if (failed(deallocateBuffersOwnershipBased(func, options))) return WalkResult::interrupt(); return WalkResult::advance(); @@ -1053,10 +1050,11 @@ struct OwnershipBasedBufferDeallocationPass // Implement bufferization API //===----------------------------------------------------------------------===// -LogicalResult bufferization::deallocateBuffersOwnershipBased( - FunctionOpInterface op, bool privateFuncDynamicOwnership) { +LogicalResult +bufferization::deallocateBuffersOwnershipBased(FunctionOpInterface op, + DeallocationOptions options) { // Gather all required allocation nodes and prepare the deallocation phase. - BufferDeallocation deallocation(op, privateFuncDynamicOwnership); + BufferDeallocation deallocation(op, options); // Place all required temporary clone and dealloc nodes. return deallocation.deallocate(op); @@ -1068,7 +1066,6 @@ LogicalResult bufferization::deallocateBuffersOwnershipBased( std::unique_ptr<Pass> mlir::bufferization::createOwnershipBasedBufferDeallocationPass( - bool privateFuncDynamicOwnership) { - return std::make_unique<OwnershipBasedBufferDeallocationPass>( - privateFuncDynamicOwnership); + DeallocationOptions options) { + return std::make_unique<OwnershipBasedBufferDeallocationPass>(options); } diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel index 78318c6..2b547fb 100644 --- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel @@ -13240,6 +13240,7 @@ cc_library( hdrs = ["include/mlir/Dialect/Bufferization/Pipelines/Passes.h"], includes = ["include"], deps = [ + ":BufferizationDialect", ":BufferizationToMemRef", ":BufferizationTransforms", ":FuncDialect", |