[Mlir-commits] [mlir] 57e714b - [mlir][linalg][bufferize] Add pass options for `createDeallocs`

Matthias Springer llvmlistbot at llvm.org
Wed Jan 12 01:55:51 PST 2022


Author: Matthias Springer
Date: 2022-01-12T18:55:36+09:00
New Revision: 57e714bcc813403f3003f522fcc4390d47a80c6e

URL: https://github.com/llvm/llvm-project/commit/57e714bcc813403f3003f522fcc4390d47a80c6e
DIFF: https://github.com/llvm/llvm-project/commit/57e714bcc813403f3003f522fcc4390d47a80c6e.diff

LOG: [mlir][linalg][bufferize] Add pass options for `createDeallocs`

This change makes it possible to use a different buffer deallocation strategy. E.g., `-buffer-deallocation` can be used, which also works for allocations that are not in destination-passing style.

Differential Revision: https://reviews.llvm.org/D117096

Added: 
    mlir/test/Dialect/Linalg/comprehensive-function-bufferize-compat.mlir

Modified: 
    mlir/include/mlir/Dialect/Linalg/Passes.td
    mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferizePass.cpp
    mlir/test/lib/Dialect/Linalg/TestComprehensiveBufferize.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Linalg/Passes.td b/mlir/include/mlir/Dialect/Linalg/Passes.td
index b9ac21641e1b..b2befd03a3b5 100644
--- a/mlir/include/mlir/Dialect/Linalg/Passes.td
+++ b/mlir/include/mlir/Dialect/Linalg/Passes.td
@@ -58,6 +58,9 @@ def LinalgComprehensiveModuleBufferize :
             /*default=*/"false",
            "(Experimental) Try to eliminate init_tensor operations that are "
            "anchored at an insert_slice op">,
+    Option<"createDeallocs", "create-deallocs", "bool", /*default=*/"true",
+           "Specify if buffers should be deallocated. For compatibility with "
+           "core bufferization passes.">,
   ];
   let constructor = "mlir::createLinalgComprehensiveModuleBufferizePass()";
 }

diff  --git a/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferizePass.cpp b/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferizePass.cpp
index 2da7df6da834..e92f852cd6c8 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferizePass.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferizePass.cpp
@@ -92,6 +92,7 @@ void LinalgComprehensiveModuleBufferize::runOnOperation() {
   options->analysisFuzzerSeed = analysisFuzzerSeed;
   options->testAnalysisOnly = testAnalysisOnly;
   options->printConflicts = printConflicts;
+  options->createDeallocs = createDeallocs;
 
   // Enable InitTensorOp elimination.
   if (initTensorElimination) {

diff  --git a/mlir/test/Dialect/Linalg/comprehensive-function-bufferize-compat.mlir b/mlir/test/Dialect/Linalg/comprehensive-function-bufferize-compat.mlir
new file mode 100644
index 000000000000..6a07a96c0aee
--- /dev/null
+++ b/mlir/test/Dialect/Linalg/comprehensive-function-bufferize-compat.mlir
@@ -0,0 +1,31 @@
+// RUN: mlir-opt %s \
+// RUN:     -test-comprehensive-function-bufferize="allow-return-memref allow-unknown-ops create-deallocs=0" \
+// RUN:     -split-input-file | \
+// RUN: FileCheck %s --check-prefix=CHECK-NODEALLOC
+
+// RUN: mlir-opt %s \
+// RUN:     -test-comprehensive-function-bufferize="allow-return-memref allow-unknown-ops create-deallocs=0" \
+// RUN:     -buffer-deallocation | \
+// RUN: FileCheck %s --check-prefix=CHECK-BUFFERDEALLOC
+
+// CHECK-NODEALLOC-LABEL: func @out_of_place_bufferization
+// CHECK-BUFFERDEALLOC-LABEL: func @out_of_place_bufferization
+func @out_of_place_bufferization(%t1 : tensor<?xf32>) -> (f32, f32) {
+  //     CHECK-NODEALLOC: memref.alloc
+  //     CHECK-NODEALLOC: memref.copy
+  // CHECK-NODEALLOC-NOT: memref.dealloc
+
+  //     CHECK-BUFFERDEALLOC: %[[alloc:.*]] = memref.alloc
+  //     CHECK-BUFFERDEALLOC: memref.copy
+  //     CHECK-BUFFERDEALLOC: memref.dealloc %[[alloc]]
+
+  %cst = arith.constant 0.0 : f32
+  %idx = arith.constant 5 : index
+
+  // This bufferizes out-of-place. An allocation + copy will be inserted.
+  %0 = tensor.insert %cst into %t1[%idx] : tensor<?xf32>
+
+  %1 = tensor.extract %t1[%idx] : tensor<?xf32>
+  %2 = tensor.extract %0[%idx] : tensor<?xf32>
+  return %1, %2 : f32, f32
+}

diff  --git a/mlir/test/lib/Dialect/Linalg/TestComprehensiveBufferize.cpp b/mlir/test/lib/Dialect/Linalg/TestComprehensiveBufferize.cpp
index 506b99e5d655..844b21cde828 100644
--- a/mlir/test/lib/Dialect/Linalg/TestComprehensiveBufferize.cpp
+++ b/mlir/test/lib/Dialect/Linalg/TestComprehensiveBufferize.cpp
@@ -92,6 +92,10 @@ struct TestComprehensiveFunctionBufferize
       *this, "dialect-filter",
       llvm::cl::desc("Bufferize only ops from the specified dialects"),
       llvm::cl::ZeroOrMore, llvm::cl::MiscFlags::CommaSeparated};
+  Option<bool> createDeallocs{
+      *this, "create-deallocs",
+      llvm::cl::desc("Specify if buffers should be deallocated"),
+      llvm::cl::init(true)};
 };
 } // namespace
 
@@ -105,6 +109,7 @@ void TestComprehensiveFunctionBufferize::runOnFunction() {
   options->allowUnknownOps = allowUnknownOps;
   options->testAnalysisOnly = testAnalysisOnly;
   options->analysisFuzzerSeed = analysisFuzzerSeed;
+  options->createDeallocs = createDeallocs;
 
   if (dialectFilter.hasValue()) {
     options->dialectFilter.emplace();


        


More information about the Mlir-commits mailing list