forked from llvm/llvm-project
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MLIR][Bufferization] BufferResultsToOutParams: Add an option to elim…
…inate AllocOp and avoid Copy (llvm#90011) Add an option hoist-static-allocs to remove the unnecessary memref.alloc and memref.copy after this pass, when the memref in ReturnOp is allocated by memref.alloc and is statically shaped. Instead, it replaces the uses of the allocated memref with the memref in the out argument. By default, BufferResultsToOutParams will result in a memcpy operation to copy the originally returned memref to the output argument memref. This is inefficient when the source of memcpy (the returned memref in the original ReturnOp) is from a local AllocOp. The pass can use the output argument memref to replace the locally allocated memref for better performance.hoist-static-allocs avoids dynamic allocation and memory movement. This option will be critical for performance-sensivtive applications, which require BufferResultsToOutParams pass for a caller-owned output buffer calling convension.
- Loading branch information
Showing
4 changed files
with
65 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
mlir/test/Transforms/buffer-results-to-out-params-elim.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// RUN: mlir-opt -allow-unregistered-dialect -p 'builtin.module(buffer-results-to-out-params{hoist-static-allocs})' %s | FileCheck %s | ||
|
||
// CHECK-LABEL: func @basic( | ||
// CHECK-SAME: %[[ARG:.*]]: memref<8x64xf32>) { | ||
// CHECK-NOT: memref.alloc() | ||
// CHECK: "test.source"(%[[ARG]]) : (memref<8x64xf32>) -> () | ||
// CHECK: return | ||
// CHECK: } | ||
func.func @basic() -> (memref<8x64xf32>) { | ||
%b = memref.alloc() : memref<8x64xf32> | ||
"test.source"(%b) : (memref<8x64xf32>) -> () | ||
return %b : memref<8x64xf32> | ||
} | ||
|
||
// CHECK-LABEL: func @basic_no_change( | ||
// CHECK-SAME: %[[ARG:.*]]: memref<f32>) { | ||
// CHECK: %[[RESULT:.*]] = "test.source"() : () -> memref<f32> | ||
// CHECK: memref.copy %[[RESULT]], %[[ARG]] : memref<f32> to memref<f32> | ||
// CHECK: return | ||
// CHECK: } | ||
func.func @basic_no_change() -> (memref<f32>) { | ||
%0 = "test.source"() : () -> (memref<f32>) | ||
return %0 : memref<f32> | ||
} | ||
|
||
// CHECK-LABEL: func @basic_dynamic( | ||
// CHECK-SAME: %[[D:.*]]: index, %[[ARG:.*]]: memref<?xf32>) { | ||
// CHECK: %[[RESULT:.*]] = memref.alloc(%[[D]]) : memref<?xf32> | ||
// CHECK: "test.source"(%[[RESULT]]) : (memref<?xf32>) -> () | ||
// CHECK: memref.copy %[[RESULT]], %[[ARG]] | ||
// CHECK: return | ||
// CHECK: } | ||
func.func @basic_dynamic(%d: index) -> (memref<?xf32>) { | ||
%b = memref.alloc(%d) : memref<?xf32> | ||
"test.source"(%b) : (memref<?xf32>) -> () | ||
return %b : memref<?xf32> | ||
} |