-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
[flang][OpenMP] Lower REDUCTION clause for SECTIONS #97858
Merged
tblah
merged 5 commits into
llvm:main
from
tblah:ecclescake/sections_reduction_lowering
Jul 12, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
26c1467
[flang][OpenMP] Lower REDUCTION clause for SECTIONS
tblah 7b3412a
Add flang sections array reduction test
tblah e702e16
Fix sorting
tblah 06fe411
Add llvm_unreachable and comment
tblah 6bbb56a
Don't use genReductionVars for omp.sections
tblah 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 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
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,53 @@ | ||
! RUN: bbc -emit-hlfir -fopenmp %s -o - | FileCheck %s | ||
! RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s | ||
|
||
subroutine sectionsReduction(x) | ||
real, dimension(:) :: x | ||
|
||
!$omp parallel | ||
!$omp sections reduction(+:x) | ||
x = x + 1 | ||
!$omp section | ||
x = x + 2 | ||
!$omp end sections | ||
!$omp end parallel | ||
end subroutine | ||
|
||
|
||
! CHECK-LABEL: omp.declare_reduction @add_reduction_byref_box_Uxf32 : !fir.ref<!fir.box<!fir.array<?xf32>>> init { | ||
! [...] | ||
! CHECK: omp.yield | ||
! CHECK-LABEL: } combiner { | ||
! [...] | ||
! CHECK: omp.yield | ||
! CHECK-LABEL: } cleanup { | ||
! [...] | ||
! CHECK: omp.yield | ||
! CHECK: } | ||
|
||
! CHECK-LABEL: func.func @_QPsectionsreduction( | ||
! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "x"}) { | ||
! CHECK: %[[VAL_1:.*]] = fir.dummy_scope : !fir.dscope | ||
! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_1]] {uniq_name = "_QFsectionsreductionEx"} : (!fir.box<!fir.array<?xf32>>, !fir.dscope) -> (!fir.box<!fir.array<?xf32>>, !fir.box<!fir.array<?xf32>>) | ||
! CHECK: omp.parallel { | ||
! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.box<!fir.array<?xf32>> | ||
! CHECK: fir.store %[[VAL_2]]#1 to %[[VAL_3]] : !fir.ref<!fir.box<!fir.array<?xf32>>> | ||
! CHECK: omp.sections reduction(byref @add_reduction_byref_box_Uxf32 -> %[[VAL_3]] : !fir.ref<!fir.box<!fir.array<?xf32>>>) { | ||
! CHECK: ^bb0(%[[VAL_4:.*]]: !fir.ref<!fir.box<!fir.array<?xf32>>>): | ||
! CHECK: omp.section { | ||
! CHECK: ^bb0(%[[VAL_5:.*]]: !fir.ref<!fir.box<!fir.array<?xf32>>>): | ||
! [...] | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: omp.section { | ||
! CHECK: ^bb0(%[[VAL_23:.*]]: !fir.ref<!fir.box<!fir.array<?xf32>>>): | ||
! [...] | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: return | ||
! CHECK: } | ||
|
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,105 @@ | ||
! RUN: bbc -emit-hlfir -fopenmp %s -o - | FileCheck %s | ||
! RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s | ||
|
||
subroutine sectionsReduction(x,y) | ||
real :: x, y | ||
|
||
!$omp parallel | ||
!$omp sections reduction(+:x,y) | ||
x = x + 1 | ||
y = x | ||
!$omp section | ||
x = x + 2 | ||
y = x | ||
!$omp end sections | ||
!$omp end parallel | ||
|
||
!$omp parallel sections reduction(+:x) reduction(+:y) | ||
x = x + 1 | ||
y = x | ||
!$omp section | ||
x = x + 2 | ||
y = x | ||
!$omp end parallel sections | ||
end subroutine | ||
|
||
! CHECK-LABEL: omp.declare_reduction @add_reduction_f32 : f32 init { | ||
! CHECK: ^bb0(%[[VAL_0:.*]]: f32): | ||
! CHECK: %[[VAL_1:.*]] = arith.constant 0.000000e+00 : f32 | ||
! CHECK: omp.yield(%[[VAL_1]] : f32) | ||
! CHECK-LABEL: } combiner { | ||
! CHECK: ^bb0(%[[VAL_0:.*]]: f32, %[[VAL_1:.*]]: f32): | ||
! CHECK: %[[VAL_2:.*]] = arith.addf %[[VAL_0]], %[[VAL_1]] fastmath<contract> : f32 | ||
! CHECK: omp.yield(%[[VAL_2]] : f32) | ||
! CHECK: } | ||
|
||
! CHECK-LABEL: func.func @_QPsectionsreduction( | ||
! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<f32> {fir.bindc_name = "x"}, | ||
! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<f32> {fir.bindc_name = "y"}) { | ||
! CHECK: %[[VAL_2:.*]] = fir.dummy_scope : !fir.dscope | ||
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_2]] {uniq_name = "_QFsectionsreductionEx"} : (!fir.ref<f32>, !fir.dscope) -> (!fir.ref<f32>, !fir.ref<f32>) | ||
! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_1]] dummy_scope %[[VAL_2]] {uniq_name = "_QFsectionsreductionEy"} : (!fir.ref<f32>, !fir.dscope) -> (!fir.ref<f32>, !fir.ref<f32>) | ||
! CHECK: omp.parallel { | ||
! CHECK: omp.sections reduction(@add_reduction_f32 -> %[[VAL_3]]#0 : !fir.ref<f32>, @add_reduction_f32 -> %[[VAL_4]]#0 : !fir.ref<f32>) { | ||
! CHECK: ^bb0(%[[VAL_5:.*]]: !fir.ref<f32>, %[[VAL_6:.*]]: !fir.ref<f32>): | ||
! CHECK: omp.section { | ||
! CHECK: ^bb0(%[[VAL_7:.*]]: !fir.ref<f32>, %[[VAL_8:.*]]: !fir.ref<f32>): | ||
! CHECK: %[[VAL_9:.*]]:2 = hlfir.declare %[[VAL_7]] {uniq_name = "_QFsectionsreductionEx"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>) | ||
! CHECK: %[[VAL_10:.*]]:2 = hlfir.declare %[[VAL_8]] {uniq_name = "_QFsectionsreductionEy"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>) | ||
! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref<f32> | ||
! CHECK: %[[VAL_12:.*]] = arith.constant 1.000000e+00 : f32 | ||
! CHECK: %[[VAL_13:.*]] = arith.addf %[[VAL_11]], %[[VAL_12]] fastmath<contract> : f32 | ||
! CHECK: hlfir.assign %[[VAL_13]] to %[[VAL_9]]#0 : f32, !fir.ref<f32> | ||
! CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref<f32> | ||
! CHECK: hlfir.assign %[[VAL_14]] to %[[VAL_10]]#0 : f32, !fir.ref<f32> | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: omp.section { | ||
! CHECK: ^bb0(%[[VAL_15:.*]]: !fir.ref<f32>, %[[VAL_16:.*]]: !fir.ref<f32>): | ||
! CHECK: %[[VAL_17:.*]]:2 = hlfir.declare %[[VAL_15]] {uniq_name = "_QFsectionsreductionEx"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>) | ||
! CHECK: %[[VAL_18:.*]]:2 = hlfir.declare %[[VAL_16]] {uniq_name = "_QFsectionsreductionEy"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>) | ||
! CHECK: %[[VAL_19:.*]] = fir.load %[[VAL_17]]#0 : !fir.ref<f32> | ||
! CHECK: %[[VAL_20:.*]] = arith.constant 2.000000e+00 : f32 | ||
! CHECK: %[[VAL_21:.*]] = arith.addf %[[VAL_19]], %[[VAL_20]] fastmath<contract> : f32 | ||
! CHECK: hlfir.assign %[[VAL_21]] to %[[VAL_17]]#0 : f32, !fir.ref<f32> | ||
! CHECK: %[[VAL_22:.*]] = fir.load %[[VAL_17]]#0 : !fir.ref<f32> | ||
! CHECK: hlfir.assign %[[VAL_22]] to %[[VAL_18]]#0 : f32, !fir.ref<f32> | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: omp.parallel { | ||
! CHECK: omp.sections reduction(@add_reduction_f32 -> %[[VAL_3]]#0 : !fir.ref<f32>, @add_reduction_f32 -> %[[VAL_4]]#0 : !fir.ref<f32>) { | ||
! CHECK: ^bb0(%[[VAL_23:.*]]: !fir.ref<f32>, %[[VAL_24:.*]]: !fir.ref<f32>): | ||
! CHECK: omp.section { | ||
! CHECK: ^bb0(%[[VAL_25:.*]]: !fir.ref<f32>, %[[VAL_26:.*]]: !fir.ref<f32>): | ||
! CHECK: %[[VAL_27:.*]]:2 = hlfir.declare %[[VAL_25]] {uniq_name = "_QFsectionsreductionEx"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>) | ||
! CHECK: %[[VAL_28:.*]]:2 = hlfir.declare %[[VAL_26]] {uniq_name = "_QFsectionsreductionEy"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>) | ||
! CHECK: %[[VAL_29:.*]] = fir.load %[[VAL_27]]#0 : !fir.ref<f32> | ||
! CHECK: %[[VAL_30:.*]] = arith.constant 1.000000e+00 : f32 | ||
! CHECK: %[[VAL_31:.*]] = arith.addf %[[VAL_29]], %[[VAL_30]] fastmath<contract> : f32 | ||
! CHECK: hlfir.assign %[[VAL_31]] to %[[VAL_27]]#0 : f32, !fir.ref<f32> | ||
! CHECK: %[[VAL_32:.*]] = fir.load %[[VAL_27]]#0 : !fir.ref<f32> | ||
! CHECK: hlfir.assign %[[VAL_32]] to %[[VAL_28]]#0 : f32, !fir.ref<f32> | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: omp.section { | ||
! CHECK: ^bb0(%[[VAL_33:.*]]: !fir.ref<f32>, %[[VAL_34:.*]]: !fir.ref<f32>): | ||
! CHECK: %[[VAL_35:.*]]:2 = hlfir.declare %[[VAL_33]] {uniq_name = "_QFsectionsreductionEx"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>) | ||
! CHECK: %[[VAL_36:.*]]:2 = hlfir.declare %[[VAL_34]] {uniq_name = "_QFsectionsreductionEy"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>) | ||
! CHECK: %[[VAL_37:.*]] = fir.load %[[VAL_35]]#0 : !fir.ref<f32> | ||
! CHECK: %[[VAL_38:.*]] = arith.constant 2.000000e+00 : f32 | ||
! CHECK: %[[VAL_39:.*]] = arith.addf %[[VAL_37]], %[[VAL_38]] fastmath<contract> : f32 | ||
! CHECK: hlfir.assign %[[VAL_39]] to %[[VAL_35]]#0 : f32, !fir.ref<f32> | ||
! CHECK: %[[VAL_40:.*]] = fir.load %[[VAL_35]]#0 : !fir.ref<f32> | ||
! CHECK: hlfir.assign %[[VAL_40]] to %[[VAL_36]]#0 : f32, !fir.ref<f32> | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: omp.terminator | ||
! CHECK: } | ||
! CHECK: return | ||
! CHECK: } |
Oops, something went wrong.
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.
Wouldn't the
omp.sections
operation be the only one in this case that should have reduction information?omp.section
doesn't have a reduction clause, which matches the spec as far as I can tell.My understanding is that what we need to achieve here is to generate the
hlfir.declare
operations that would have been created in the parentomp.sections
, but had to be removed. Maybe the best way to have reduction-related block arguments inomp.sections
and the correspondinghlfir.declare
operations in each nestedomp.section
is to avoid using thegenReductionVars
functions and split its contents instead. Something like:This way no block arguments are added to the
omp.section
operations and there's no need to removehlfir.declare
operations inside ofomp.sections
. Maybe this alternative introduces problems I'm not aware of, so let me know.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 review and detailed example.
I didn't do this because conceptually the reduction variables are private to each SECTION so references to that variable in one SECTION are operating on different memory to another SECTION (until it is reduced, which is implicit in MLIR).
In practice, the lowering to LLVMIR (#97859) does just map the SECTIONS block arguments to the block arguments of each SECTION (and then this is only used as a mold for the private variable inside of the SECTION).
I'm more than happy to change it as you described if you still think that would be better.
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.
I see, thank you for the explanation. I think that approach makes sense, so I won't oppose it. I'd suggest documenting this correspondence between entry block arguments in the op descriptions for
omp.sections
andomp.section
, but that can be a separate NFC PR.I think it's still worth avoiding the addition+deletion of
hlfir.declare
ops inside ofomp.sections
by replacing the call to thereductionCallback
lambda with my suggestion above, but I'll leave it up to you if you prefer to leave it as it is. In my opinion, it's a minimal amount of code duplication in exchange for making it shorter and easier to follow.