Skip to content

Commit aab795a

Browse files
authored
[mlir] run buffer deallocation in transform tutorial (llvm#67978)
Buffer deallocation pipeline previously was incorrect when applied to functions. It has since been fixed. Make sure it is exercised in the tutorial to avoid leaking allocations.
1 parent c95fcd3 commit aab795a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

mlir/docs/Tutorials/transform/ChH.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,20 @@ bufferization is directly available as a transform operation.
497497
function_boundary_type_conversion = 1 : i32 }
498498
```
499499

500+
One-shot bufferization itself does not produce buffer deallocations, which may
501+
lead to leaks. So we have to run the buffer deallocation pass pipeline to avoid
502+
them. Note that the transform dialect seamlessly runs named passes and pass
503+
pipelines: if desired, one could replace complex `--pass-pipeline expressions`
504+
with operations. Note that we apply the pipeline to functions rather than entire
505+
module to avoid running it on the transform IR that is contained in the module.
506+
507+
```mlir
508+
%f = transform.structured.match ops{["func.func"]} in %arg1
509+
: (!transform.any_op) -> !transform.any_op
510+
transform.apply_registered_pass "buffer-deallocation-pipeline" to %f
511+
: (!transform.any_op) -> !transform.any_op
512+
```
513+
500514
In this particular case, the transformed IR could be directly bufferized. This
501515
is not always the case in general as some operations, in particular
502516
`tensor.empty` may not be bufferizable. Such operations need to be removed

mlir/test/Examples/transform/ChH/full.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: mlir-opt %s --test-transform-dialect-interpreter \
22
// RUN: --test-transform-dialect-erase-schedule \
33
// RUN: --math-uplift-to-fma \
4+
// RUN: --convert-bufferization-to-memref \
45
// RUN: --test-lower-to-llvm |\
56
// RUN: FileCheck %s
67

@@ -307,10 +308,22 @@ module attributes { transform.with_named_sequence } {
307308
// transformation process, so invalidation is not an issue. However, if
308309
// other transformations, such as loop unrolling, are required after
309310
// bufferization, new handles should be produced using the match operations.
311+
//
312+
// One-shot bufferization itself does not produce buffer deallocations,
313+
// which may lead to leaks. So we have to run the buffer deallocation pass
314+
// pipeline to avoid them. Note that the transform dialect seamlessly runs
315+
// named passes and pass pipelines: if desired, one could replace complex
316+
// --pass-pipeline expressions with operations. Note that we apply the
317+
// pipeline to functions rather than entire module to avoid running it
318+
// on the transform IR that is contained in the module.
310319
%arg1 = transform.bufferization.one_shot_bufferize %arg0 {
311320
bufferize_function_boundaries = true,
312321
function_boundary_type_conversion = 1 : i32 }
313322
: (!transform.any_op) -> !transform.any_op
323+
%f = transform.structured.match ops{["func.func"]} in %arg1
324+
: (!transform.any_op) -> !transform.any_op
325+
transform.apply_registered_pass "buffer-deallocation-pipeline" to %f
326+
: (!transform.any_op) -> !transform.any_op
314327

315328
// Apply general canonicalization and CSE to each function after
316329
// bufferization as new simplification opportunities may have appeared.

0 commit comments

Comments
 (0)