Skip to content

[flang][OpenMP] Move reductions from loop to teams when loop is mapped to distribute #132920

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 2 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions flang/lib/Optimizer/OpenMP/GenericLoopConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,38 @@ class GenericLoopConversionPattern
rewriteToWsloop(loopOp, rewriter);
break;
case GenericLoopCombinedInfo::TeamsLoop:
if (teamsLoopCanBeParallelFor(loopOp))
if (teamsLoopCanBeParallelFor(loopOp)) {
rewriteToDistributeParallelDo(loopOp, rewriter);
else
} else {
auto teamsOp = llvm::cast<mlir::omp::TeamsOp>(loopOp->getParentOp());
auto teamsBlockArgIface =
llvm::cast<mlir::omp::BlockArgOpenMPOpInterface>(*teamsOp);
auto loopBlockArgIface =
llvm::cast<mlir::omp::BlockArgOpenMPOpInterface>(*loopOp);

for (unsigned i = 0; i < loopBlockArgIface.numReductionBlockArgs();
++i) {
mlir::BlockArgument loopRedBlockArg =
loopBlockArgIface.getReductionBlockArgs()[i];
mlir::BlockArgument teamsRedBlockArg =
teamsBlockArgIface.getReductionBlockArgs()[i];
rewriter.replaceAllUsesWith(loopRedBlockArg, teamsRedBlockArg);
}

for (unsigned i = 0; i < loopBlockArgIface.numReductionBlockArgs();
++i) {
loopOp.getRegion().eraseArgument(
loopBlockArgIface.getReductionBlockArgsStart());
}

loopOp.removeReductionModAttr();
loopOp.getReductionVarsMutable().clear();
loopOp.removeReductionByrefAttr();
loopOp.removeReductionSymsAttr();

rewriteToDistribute(loopOp, rewriter);
}

break;
}

Expand Down
37 changes: 37 additions & 0 deletions flang/test/Lower/OpenMP/loop-directive.f90
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,40 @@ subroutine multi_block_teams
end select
!$omp end target teams
end subroutine


! Verifies that reductions are hoisted to the parent `teams` directive and removed
! from the `loop` directive when `loop` is mapped to `distribute`.

! CHECK-LABEL: func.func @_QPteams_loop_cannot_be_parallel_for_with_reductions
subroutine teams_loop_cannot_be_parallel_for_with_reductions
implicit none
integer :: x, y, i, p

! CHECK: %[[ADD_RED:.*]]:2 = hlfir.declare %{{.*}} {uniq_name = "_QF{{.*}}Ex"}
! CHECK: %[[MUL_RED:.*]]:2 = hlfir.declare %{{.*}} {uniq_name = "_QF{{.*}}Ey"}
! CHECK: omp.teams reduction(
! CHECK-SAME: @add_reduction_i32 %[[ADD_RED]]#0 -> %[[ADD_RED_ARG:[^[:space:]]*]],
! CHECK-SAME: @multiply_reduction_i32 %[[MUL_RED]]#0 -> %[[MUL_RED_ARG:.*]] : {{.*}}) {

! CHECK: omp.distribute private(@{{.*}} %{{.*}} -> %{{.*}}, @{{.*}} %{{.*}} -> %{{.*}} : {{.*}}) {
! CHECK: %[[ADD_RED_DECL:.*]]:2 = hlfir.declare %[[ADD_RED_ARG]] {uniq_name = "_QF{{.*}}Ex"}
! CHECK: %[[MUL_RED_DECL:.*]]:2 = hlfir.declare %[[MUL_RED_ARG]] {uniq_name = "_QF{{.*}}Ey"}

! CHECK: %[[ADD_RES:.*]] = arith.addi %{{.*}}, %{{.*}} : i32
! CHECK: hlfir.assign %[[ADD_RES]] to %[[ADD_RED_DECL]]#0 : i32, !fir.ref<i32>

! CHECK: %[[MUL_RES:.*]] = arith.muli %{{.*}}, %{{.*}} : i32
! CHECK: hlfir.assign %[[MUL_RES]] to %[[MUL_RED_DECL]]#0 : i32, !fir.ref<i32>
! CHECK: omp.yield
! CHECK: }
! CHECK: omp.terminator
! CHECK: }
!$omp teams loop reduction(+: x) reduction(*: y) private(p)
do i = 1, 5
call foo()
x = x + i
y = y * i
p = 42
end do
end subroutine