-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[OpenMP][MLIR] Hoist static allocas emitted by private init regions to the allocation IP of the construct
#171597
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
Open
ergawy
wants to merge
2
commits into
main
Choose a base branch
from
users/ergawy/hoist_init_allocas
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−8
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
79 changes: 79 additions & 0 deletions
79
mlir/test/Target/LLVMIR/openmp-private-allloca-hoisting.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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Tests that static alloca's in `omp.private ... init` regions are hoisted to | ||
| // the parent construct's alloca IP. | ||
| // RUN: mlir-translate -mlir-to-llvmir -split-input-file %s | FileCheck %s | ||
|
|
||
| llvm.func @foo1() | ||
| llvm.func @foo2() | ||
| llvm.func @foo3() | ||
| llvm.func @foo4() | ||
|
|
||
| omp.private {type = private} @multi_block.privatizer : f32 init { | ||
| ^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr): | ||
| %0 = llvm.mlir.constant(1 : i32) : i32 | ||
| %alloca1 = llvm.alloca %0 x !llvm.struct<(i64)> {alignment = 8 : i64} : (i32) -> !llvm.ptr | ||
|
|
||
| %1 = llvm.load %arg0 : !llvm.ptr -> f32 | ||
|
|
||
| %c1 = llvm.mlir.constant(1 : i32) : i32 | ||
| %c2 = llvm.mlir.constant(2 : i32) : i32 | ||
| %cond1 = llvm.icmp "eq" %c1, %c2 : i32 | ||
| llvm.cond_br %cond1, ^bb1, ^bb2 | ||
|
|
||
| ^bb1: | ||
| llvm.call @foo1() : () -> () | ||
| llvm.br ^bb3 | ||
|
|
||
| ^bb2: | ||
| llvm.call @foo2() : () -> () | ||
| llvm.br ^bb3 | ||
|
|
||
| ^bb3: | ||
| llvm.store %1, %arg1 : f32, !llvm.ptr | ||
|
|
||
| omp.yield(%arg1 : !llvm.ptr) | ||
| } | ||
|
|
||
| omp.private {type = private} @multi_block.privatizer2 : f32 init { | ||
| ^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr): | ||
| %0 = llvm.mlir.constant(1 : i32) : i32 | ||
| %alloca1 = llvm.alloca %0 x !llvm.struct<(ptr)> {alignment = 8 : i64} : (i32) -> !llvm.ptr | ||
|
|
||
| %1 = llvm.load %arg0 : !llvm.ptr -> f32 | ||
|
|
||
| %c1 = llvm.mlir.constant(1 : i32) : i32 | ||
| %c2 = llvm.mlir.constant(2 : i32) : i32 | ||
| %cond1 = llvm.icmp "eq" %c1, %c2 : i32 | ||
| llvm.cond_br %cond1, ^bb1, ^bb2 | ||
|
|
||
| ^bb1: | ||
| llvm.call @foo3() : () -> () | ||
| llvm.br ^bb3 | ||
|
|
||
| ^bb2: | ||
| llvm.call @foo4() : () -> () | ||
| llvm.br ^bb3 | ||
|
|
||
| ^bb3: | ||
| llvm.store %1, %arg1 : f32, !llvm.ptr | ||
|
|
||
| omp.yield(%arg1 : !llvm.ptr) | ||
| } | ||
|
|
||
| llvm.func @parallel_op_private_multi_block(%arg0: !llvm.ptr, %arg1: !llvm.ptr) { | ||
| omp.parallel private(@multi_block.privatizer %arg0 -> %arg2, | ||
| @multi_block.privatizer2 %arg1 -> %arg3 : !llvm.ptr, !llvm.ptr) { | ||
| %0 = llvm.load %arg2 : !llvm.ptr -> f32 | ||
| %1 = llvm.load %arg3 : !llvm.ptr -> f32 | ||
| omp.terminator | ||
| } | ||
| llvm.return | ||
| } | ||
|
|
||
| // CHECK: define internal void @parallel_op_private_multi_block..omp_par({{.*}}) {{.*}} { | ||
| // CHECK: omp.par.entry: | ||
| // Varify that both allocas were hoisted to the parallel region's entry block. | ||
| // CHECK: %{{.*}} = alloca { i64 }, align 8 | ||
| // CHECK-NEXT: %{{.*}} = alloca { ptr }, align 8 | ||
| // CHECK-NEXT: br label %omp.region.after_alloca | ||
| // CHECK: omp.region.after_alloca: | ||
| // CHECK: } |
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.
Why are array allocations special? I would have thought the case worth worrying about is array allocations which have a size determined dynamically - a statically sized array allocation should work okay.
Would multiple of these dynamically sized arrays still crash the GPU code?
Uh oh!
There was an error while loading. Please reload this page.
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.
For descriptors, the problematic allocations are the allocations of the temporary descriptor structures used to initialize the private storage. All such allocations are static allocations because they are just structs.
The dynamic arrays are allocated only when the original value is allocated, so these allocations has to be maintained in the proper branch since we read the shape from the original value.
The problem when we have many descriptors is that:
initregion of descriptor number 1 which includes temp allocations + the if-else branch for initialization.Because of that, such temp allocations are emitted between if-else branching.
I think the backend is smart enough to keep the dynamic allocations since they are obviously protected/tucked inside a branch while some of the static allocations after the branching joins again are problamtic since they are supposed to be uncoditional.