diff --git a/mlir/include/mlir/Dialect/Bufferization/Transforms/Bufferize.h b/mlir/include/mlir/Dialect/Bufferization/Transforms/Bufferize.h index 1603dfcbae558..ebed2c354bfca 100644 --- a/mlir/include/mlir/Dialect/Bufferization/Transforms/Bufferize.h +++ b/mlir/include/mlir/Dialect/Bufferization/Transforms/Bufferize.h @@ -56,12 +56,6 @@ class BufferizeTypeConverter : public TypeConverter { /// populateEliminateBufferizeMaterializationsPatterns. void populateBufferizeMaterializationLegality(ConversionTarget &target); -/// Populate patterns to eliminate bufferize materializations. -/// -/// In particular, these are the tensor_load/buffer_cast ops. -void populateEliminateBufferizeMaterializationsPatterns( - const BufferizeTypeConverter &typeConverter, RewritePatternSet &patterns); - /// Bufferize `op` and its nested ops that implement `BufferizableOpInterface`. /// /// Note: This function does not resolve read-after-write conflicts. Use this diff --git a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h index ab9a48f3473c2..fe43a05c81fdc 100644 --- a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h +++ b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.h @@ -200,10 +200,6 @@ std::unique_ptr createEmptyTensorToAllocTensorPass(); /// Drop all memref function results that are equivalent to a function argument. LogicalResult dropEquivalentBufferResults(ModuleOp module); -/// Creates a pass that finalizes a partial bufferization by removing remaining -/// bufferization.to_tensor and bufferization.to_memref operations. -std::unique_ptr> createFinalizingBufferizePass(); - /// Create a pass that bufferizes all ops that implement BufferizableOpInterface /// with One-Shot Bufferize. std::unique_ptr createOneShotBufferizePass(); diff --git a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td index 2743de43fb9cf..3e93f33ffe0fb 100644 --- a/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td +++ b/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td @@ -343,22 +343,6 @@ def BufferResultsToOutParams : Pass<"buffer-results-to-out-params", "ModuleOp"> let dependentDialects = ["memref::MemRefDialect"]; } -def FinalizingBufferize : Pass<"finalizing-bufferize", "func::FuncOp"> { - let summary = "Finalize a partial bufferization"; - let description = [{ - A bufferize pass that finalizes a partial bufferization by removing - remaining `bufferization.to_tensor` and `bufferization.to_buffer` operations. - - The removal of those operations is only possible if the operations only - exist in pairs, i.e., all uses of `bufferization.to_tensor` operations are - `bufferization.to_buffer` operations. - - This pass will fail if not all operations can be removed or if any operation - with tensor typed operands remains. - }]; - let constructor = "mlir::bufferization::createFinalizingBufferizePass()"; -} - def DropEquivalentBufferResults : Pass<"drop-equivalent-buffer-results", "ModuleOp"> { let summary = "Remove MemRef return values that are equivalent to a bbArg"; let description = [{ diff --git a/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp b/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp index 1d009b03754c5..62ce2583f4fa1 100644 --- a/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp +++ b/mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp @@ -26,7 +26,6 @@ namespace mlir { namespace bufferization { -#define GEN_PASS_DEF_FINALIZINGBUFFERIZE #define GEN_PASS_DEF_BUFFERIZATIONBUFFERIZE #define GEN_PASS_DEF_ONESHOTBUFFERIZE #include "mlir/Dialect/Bufferization/Transforms/Passes.h.inc" @@ -98,75 +97,6 @@ void mlir::bufferization::populateBufferizeMaterializationLegality( } namespace { -// In a finalizing bufferize conversion, we know that all tensors have been -// converted to memrefs, thus, this op becomes an identity. -class BufferizeToTensorOp - : public OpConversionPattern { -public: - using OpConversionPattern::OpConversionPattern; - LogicalResult - matchAndRewrite(bufferization::ToTensorOp op, OpAdaptor adaptor, - ConversionPatternRewriter &rewriter) const override { - rewriter.replaceOp(op, adaptor.getMemref()); - return success(); - } -}; -} // namespace - -namespace { -// In a finalizing bufferize conversion, we know that all tensors have been -// converted to memrefs, thus, this op becomes an identity. -class BufferizeToMemrefOp - : public OpConversionPattern { -public: - using OpConversionPattern::OpConversionPattern; - LogicalResult - matchAndRewrite(bufferization::ToMemrefOp op, OpAdaptor adaptor, - ConversionPatternRewriter &rewriter) const override { - rewriter.replaceOp(op, adaptor.getTensor()); - return success(); - } -}; -} // namespace - -void mlir::bufferization::populateEliminateBufferizeMaterializationsPatterns( - const BufferizeTypeConverter &typeConverter, RewritePatternSet &patterns) { - patterns.add(typeConverter, - patterns.getContext()); -} - -namespace { -struct FinalizingBufferizePass - : public bufferization::impl::FinalizingBufferizeBase< - FinalizingBufferizePass> { - using FinalizingBufferizeBase< - FinalizingBufferizePass>::FinalizingBufferizeBase; - - void runOnOperation() override { - auto func = getOperation(); - auto *context = &getContext(); - - BufferizeTypeConverter typeConverter; - RewritePatternSet patterns(context); - ConversionTarget target(*context); - - populateEliminateBufferizeMaterializationsPatterns(typeConverter, patterns); - - // If all result types are legal, and all block arguments are legal (ensured - // by func conversion above), then all types in the program are legal. - // - // We also check that the operand types are legal to avoid creating invalid - // IR. For example, this prevents - // populateEliminateBufferizeMaterializationsPatterns from updating the - // types of the operands to a return op without updating the enclosing - // function. - target.markUnknownOpDynamicallyLegal( - [&](Operation *op) { return typeConverter.isLegal(op); }); - - if (failed(applyFullConversion(func, target, std::move(patterns)))) - signalPassFailure(); - } -}; static LayoutMapOption parseLayoutMapOption(const std::string &s) { if (s == "fully-dynamic-layout-map") @@ -331,11 +261,6 @@ std::unique_ptr mlir::bufferization::createOneShotBufferizePass( return std::make_unique(options); } -std::unique_ptr> -mlir::bufferization::createFinalizingBufferizePass() { - return std::make_unique(); -} - //===----------------------------------------------------------------------===// // BufferizableOpInterface-based Bufferization //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Dialect/SparseTensor/Pipelines/SparseTensorPipelines.cpp b/mlir/lib/Dialect/SparseTensor/Pipelines/SparseTensorPipelines.cpp index abc4a4c252841..96ccf9a9a2408 100644 --- a/mlir/lib/Dialect/SparseTensor/Pipelines/SparseTensorPipelines.cpp +++ b/mlir/lib/Dialect/SparseTensor/Pipelines/SparseTensorPipelines.cpp @@ -55,8 +55,6 @@ void mlir::sparse_tensor::buildSparsifier(OpPassManager &pm, // Storage specifier lowering and bufferization wrap-up. pm.addPass(createStorageSpecifierToLLVMPass()); pm.addNestedPass(createCanonicalizerPass()); - pm.addNestedPass( - mlir::bufferization::createFinalizingBufferizePass()); // GPU code generation. const bool gpuCodegen = options.gpuTriple.hasValue(); diff --git a/mlir/test/Dialect/Bufferization/Transforms/finalizing-bufferize.mlir b/mlir/test/Dialect/Bufferization/Transforms/finalizing-bufferize.mlir deleted file mode 100644 index bae94c1be4da9..0000000000000 --- a/mlir/test/Dialect/Bufferization/Transforms/finalizing-bufferize.mlir +++ /dev/null @@ -1,95 +0,0 @@ -// RUN: mlir-opt %s -finalizing-bufferize -split-input-file -verify-diagnostics | FileCheck %s - -// CHECK-LABEL: func @eliminate_materializations( -// CHECK-SAME: %[[ARG:.*]]: memref) -> memref { -// CHECK: return %[[ARG]] : memref -func.func @eliminate_materializations(%arg0: memref) -> memref { - %0 = bufferization.to_tensor %arg0 : memref - %1 = bufferization.to_memref %0 : memref - return %1 : memref -} - -// ----- - -func.func @unable_to_convert_lone_buffer_cast() -> memref { - // expected-error @+1 {{failed to legalize operation 'test.source'}} - %0 = "test.source"() : () -> tensor - %1 = bufferization.to_memref %0 : memref - return %1 : memref -} - -// ----- - -func.func @unable_to_convert_lone_tensor_load(%arg0: memref) { - %0 = bufferization.to_tensor %arg0 : memref - // expected-error @+1 {{failed to legalize operation 'test.sink'}} - "test.sink"(%0) : (tensor) -> () - return -} - -// ----- - -// CHECK-LABEL: func @dyn_layout_to_no_layout_cast( -// CHECK-SAME: %[[arg:.*]]: memref>) -// CHECK: %[[c0:.*]] = arith.constant 0 : index -// CHECK: %[[dim:.*]] = memref.dim %[[arg]], %[[c0]] -// CHECK: %[[alloc:.*]] = memref.alloc(%[[dim]]) : memref -// CHECK: memref.copy %[[arg]], %[[alloc]] -// CHECK: return %[[alloc]] -func.func @dyn_layout_to_no_layout_cast(%m: memref>) -> memref { - %0 = bufferization.to_tensor %m : memref> - %1 = bufferization.to_memref %0 : memref - return %1 : memref -} - -// ----- - -// CHECK-LABEL: func @fancy_layout_to_no_layout_cast( -// CHECK-SAME: %[[arg:.*]]: memref>) -// CHECK: %[[c0:.*]] = arith.constant 0 : index -// CHECK: %[[dim:.*]] = memref.dim %[[arg]], %[[c0]] -// CHECK: %[[alloc:.*]] = memref.alloc(%[[dim]]) : memref -// CHECK: memref.copy %[[arg]], %[[alloc]] -// CHECK: return %[[alloc]] -func.func @fancy_layout_to_no_layout_cast(%m: memref>) -> memref { - %0 = bufferization.to_tensor %m : memref> - %1 = bufferization.to_memref %0 : memref - return %1 : memref -} - -// ----- - -// CHECK-LABEL: func @static_layout_to_no_layout_cast( -// CHECK-SAME: %[[arg:.*]]: memref>) -// CHECK: %[[c0:.*]] = arith.constant 0 : index -// CHECK: %[[dim:.*]] = memref.dim %[[arg]], %[[c0]] -// CHECK: %[[alloc:.*]] = memref.alloc(%[[dim]]) : memref -// CHECK: memref.copy %[[arg]], %[[alloc]] -// CHECK: return %[[alloc]] -func.func @static_layout_to_no_layout_cast(%m: memref>) -> memref { - %0 = bufferization.to_tensor %m : memref> - %1 = bufferization.to_memref %0 : memref - return %1 : memref -} - -// ----- - -// TODO: to_memref with layout maps not supported yet. This should fold to a -// memref.cast. -func.func @no_layout_to_dyn_layout_cast(%m: memref) -> memref> { - %0 = bufferization.to_tensor %m : memref - // expected-error @+1 {{failed to legalize unresolved materialization from ('memref') to ('memref>') that remained live after conversion}} - %1 = bufferization.to_memref %0 : memref> - // expected-note @below{{see existing live user here}} - return %1 : memref> -} - -// ----- - -func.func @illegal_unranked_to_rank(%m: memref<*xf32>) -> memref { - // expected-note @+1 {{prior use here}} - %0 = bufferization.to_tensor %m : memref<*xf32> - // expected-error @+1 {{expects different type than prior uses: 'tensor' vs 'tensor<*xf32>'}} - %1 = bufferization.to_memref %0 : memref - return %1 : memref -}