-
Notifications
You must be signed in to change notification settings - Fork 17
[Transform] Only use gc runtime allocator for stack-like alloca ops #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
71e675b
fallback memref transformation when alloc / dealloc not in FILO fashion
bd4d269
add test case
2329aff
Merge remote-tracking branch 'origin/main' into zhangyan/enhance_allo…
8a7f5c4
remove alias
dda790d
align gc bufferization pipeline
zhczhong cf4ad61
stash
d2ed37f
Merge remote-tracking branch 'origin/zhicong/align_bufferization_pipe…
2cf5dcf
fix comment
d987fa2
add test case
6ff8ef8
Merge remote-tracking branch 'origin/main' into zhangyan/enhance_allo…
87a51b1
only convert stack-style alloca to cgc runtime allocator, with deallo…
3fdce87
update
9494fdc
update
6668da9
Merge remote-tracking branch 'origin/main' into zhangyan/enhance_allo…
280ca6c
Merge remote-tracking branch 'origin/main' into zhangyan/enhance_allo…
06a580e
fix comment
5ba2a01
add test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
80 changes: 32 additions & 48 deletions
80
test/mlir/test/gc/Dialect/CPURuntime/memref-to-cpuruntime.mlir
This file contains hidden or 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 |
---|---|---|
@@ -1,68 +1,52 @@ | ||
// RUN: gc-opt --split-input-file --convert-memref-to-cpuruntime %s -verify-diagnostics | FileCheck %s | ||
func.func @alloc() { | ||
// CHECK-LABEL: func @alloc() | ||
|
||
func.func @alloca() { | ||
// CHECK-LABEL: func @alloca() | ||
// CHECK: %[[m0:.*]] = cpuruntime.alloc() : memref<1024xf32> | ||
%m0 = memref.alloc() : memref<1024xf32> | ||
%m0 = memref.alloca() : memref<1024xf32> | ||
scf.forall (%i) in (32) { | ||
} | ||
// CHECK: cpuruntime.dealloc %[[m0]] : memref<1024xf32> | ||
cpuruntime.dealloc %m0 : memref<1024xf32> | ||
return | ||
} | ||
|
||
func.func @thread_alloc() { | ||
// CHECK-LABEL: func.func @thread_alloc() | ||
func.func @thread_alloca() { | ||
// CHECK-LABEL: func.func @thread_alloca() | ||
// CHECK: %[[m0:.*]] = cpuruntime.alloc thread_local() : memref<1024xf32> | ||
scf.forall (%i) in (32) { | ||
%0 = memref.alloc() : memref<1024xf32> | ||
%0 = memref.alloca() : memref<1024xf32> | ||
// CHECK: cpuruntime.dealloc thread_local %[[m0]] : memref<1024xf32> | ||
memref.dealloc %0 : memref<1024xf32> | ||
} | ||
return | ||
} | ||
|
||
func.func @return_alloc() -> memref<32x18xf32> { | ||
// CHECK-LABEL: func @return_alloc() -> memref<32x18xf32> | ||
// CHECK: %[[m0:.*]] = memref.alloc() : memref<32x18xf32> | ||
%0 = memref.alloc() : memref<32x18xf32> | ||
return %0 : memref<32x18xf32> | ||
func.func @dynamic_ranked_alloca(%arg0: memref<*xf32>) { | ||
// CHECK-LABEL: func @dynamic_ranked_alloca(%arg0: memref<*xf32>) | ||
// CHECK: %[[RANK:.*]] = memref.rank %{{.*}} : memref<*xf32> | ||
// CHECK: %[[m0:.*]] = cpuruntime.alloc(%[[RANK]]) : memref<?xindex> | ||
// CHECK: cpuruntime.dealloc %[[m0]] : memref<?xindex> | ||
%0 = memref.rank %arg0 : memref<*xf32> | ||
%alloca = memref.alloca(%0) : memref<?xindex> | ||
return | ||
} | ||
|
||
func.func @yield_alloc() -> memref<32x18xf32> { | ||
// CHECK-LABEL: func @yield_alloc() -> memref<32x18xf32> | ||
// CHECK: %[[m0:.*]] = memref.alloc() : memref<32x18xf32> | ||
%c32 = arith.constant 32 : index | ||
%c1 = arith.constant 1 : index | ||
%c0 = arith.constant 0 : index | ||
%lastBuffer = memref.alloc() : memref<32x18xf32> | ||
scf.for %arg3 = %c0 to %c32 step %c1 iter_args(%arg1 = %lastBuffer) -> (memref<32x18xf32>) { | ||
%newBuffer = memref.alloc() : memref<32x18xf32> | ||
memref.dealloc %arg1 : memref<32x18xf32> | ||
scf.yield %newBuffer : memref<32x18xf32> | ||
func.func @loop_nested_if_alloca(%arg0: index, %arg1: index, %arg2: index, %arg3: memref<2xf32>) { | ||
// CHECK-LABEL: func @loop_nested_if_alloca(%arg0: index, %arg1: index, %arg2: index, %arg3: memref<2xf32>) | ||
// CHECK: %[[m0:.*]] = cpuruntime.alloc() : memref<2xf32> | ||
%alloca = memref.alloca() : memref<2xf32> | ||
%0 = scf.for %arg5 = %arg0 to %arg1 step %arg2 iter_args(%arg6 = %arg3) -> (memref<2xf32>) { | ||
%1 = arith.cmpi eq, %arg5, %arg1 : index | ||
%2 = scf.if %1 -> (memref<2xf32>) { | ||
// CHECK: yield %[[m0]] : memref<2xf32> | ||
scf.yield %alloca : memref<2xf32> | ||
} else { | ||
// CHECK: %[[m1:.*]] = cpuruntime.alloc() : memref<2xf32> | ||
// CHECK: cpuruntime.dealloc %[[m1]] : memref<2xf32> | ||
%alloca_0 = memref.alloca() : memref<2xf32> | ||
scf.yield %arg6 : memref<2xf32> | ||
} | ||
scf.yield %2 : memref<2xf32> | ||
} | ||
return %lastBuffer : memref<32x18xf32> | ||
} | ||
|
||
func.func @return_view_alloc() -> memref<16xf32> { | ||
// CHECK-LABEL: func @return_view_alloc() -> memref<16xf32> | ||
// CHECK: %[[m0:.*]] = memref.alloc() : memref<128xi8> | ||
%c0 = arith.constant 0: index | ||
%f0 = arith.constant 0.0: f32 | ||
%alloc = memref.alloc() : memref<128xi8> | ||
%view = memref.view %alloc[%c0][] : memref<128xi8> to memref<32xf32> | ||
%subview = memref.subview %view[0][16][1] : memref<32xf32> to memref<16xf32> | ||
return %subview : memref<16xf32> | ||
} | ||
|
||
func.func @alloc_dealloc_view() { | ||
// CHECK-LABEL: func @alloc_dealloc_view() | ||
// CHECK: %[[m0:.*]] = cpuruntime.alloc() : memref<128xi8> | ||
%c0 = arith.constant 0: index | ||
%f0 = arith.constant 0.0: f32 | ||
%alloc = memref.alloc() : memref<128xi8> | ||
%view = memref.view %alloc[%c0][] : memref<128xi8> to memref<32xf32> | ||
%subview = memref.subview %view[0][16][1] : memref<32xf32> to memref<16xf32> | ||
// CHECK: cpuruntime.dealloc | ||
memref.dealloc %subview : memref<16xf32> | ||
// CHECK: cpuruntime.dealloc %[[m0]] : memref<2xf32> | ||
return | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please insert deallocop at the end of the parent alloca_scope op. It may not be the same as
op->getParentRegion()
And I am not sure why we need to find
firstDeallocOp
?Another issue is that, if the alloca size is very small, I think we'd better keep using the alloca instead of a runtime function. In GC v1 I remember that we have a size limit at ~128 bytes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what I concern about, the current pass is trying to convert all the alloca (no matter the size) into runtime allocator which is not that efficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion.
getParentWithTrait<OpTrait::AutomaticAllocationScope>()
to locate the dealloc op's position.firstDeallocOp
to ensure the deallocation order obeys FILO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please double check the IR result of
promote-allocation-to-stack
. AFAIK, there is no dealloc for alloca ops - it is "deallocated" via the endAutomaticAllocationScope
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, from
promote-allocation-to-stack
to the end ofdeallocation pipeline
, no dealloc will be created for alloca ops. It will be dealloced automatically at the end ofAutomaticAllocationScope
.The above
while loop
only searchescpuruntime::deallocOp
, to make sure the generated dealloc order as FILO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to check cpuruntime::deallocOp that has just been created by us in this pass?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we walk through the
AutomaticAllocationScope
. If we get the below IR, the first allocated bufferm0
will be first deallocated at the end of the scope. Then the next allocated bufferm1
will be the next op to create thedealloc
op, whose position should be the previous node ofdealloc %m0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
traverse the
allocaOp
first, and then insertdeallocOp
in a reverse order now.