Skip to content

Commit

Permalink
[Flang][OpenMP] Update DO CONCURRENT conversion for the device
Browse files Browse the repository at this point in the history
This patch makes changes to the `DoConcurrentConversion` pass to follow the
"hoisted omp.parallel" representation when converting `do concurrent`
constructs into `target teams distribute parallel do`.
  • Loading branch information
skatrak committed Aug 13, 2024
1 parent 01c368d commit 9a95922
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
23 changes: 8 additions & 15 deletions flang/lib/Optimizer/Transforms/DoConcurrentConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,7 @@ class DoConcurrentConversion : public mlir::OpConversionPattern<fir::DoLoopOp> {

targetOp = genTargetOp(doLoop.getLoc(), rewriter, mapper,
outermostLoopLives, targetClauseOps);
genTeamsOp(doLoop.getLoc(), rewriter, loopNest, mapper,
loopNestClauseOps);
genDistributeOp(doLoop.getLoc(), rewriter);
genTeamsOp(doLoop.getLoc(), rewriter);
}

mlir::omp::ParallelOp parallelOp = genParallelOp(
Expand All @@ -611,6 +609,9 @@ class DoConcurrentConversion : public mlir::OpConversionPattern<fir::DoLoopOp> {
looputils::localizeLoopLocalValue(local, parallelOp.getRegion(),
rewriter);

if (mapToDevice)
genDistributeOp(doLoop.getLoc(), rewriter);

mlir::omp::LoopNestOp ompLoopNest =
genWsLoopOp(rewriter, loopNest.back().first, mapper, loopNestClauseOps);

Expand Down Expand Up @@ -800,18 +801,14 @@ class DoConcurrentConversion : public mlir::OpConversionPattern<fir::DoLoopOp> {
}

mlir::omp::TeamsOp
genTeamsOp(mlir::Location loc, mlir::ConversionPatternRewriter &rewriter,
looputils::LoopNestToIndVarMap &loopNest, mlir::IRMapping &mapper,
mlir::omp::LoopNestOperands &loopNestClauseOps) const {
genTeamsOp(mlir::Location loc,
mlir::ConversionPatternRewriter &rewriter) const {
auto teamsOp = rewriter.create<mlir::omp::TeamsOp>(
loc, /*clauses=*/mlir::omp::TeamsOperands{});

rewriter.createBlock(&teamsOp.getRegion());
rewriter.setInsertionPoint(rewriter.create<mlir::omp::TerminatorOp>(loc));

genLoopNestIndVarAllocs(rewriter, loopNest, mapper);
genLoopNestClauseOps(loc, rewriter, loopNest, mapper, loopNestClauseOps);

return teamsOp;
}

Expand Down Expand Up @@ -905,12 +902,8 @@ class DoConcurrentConversion : public mlir::OpConversionPattern<fir::DoLoopOp> {
rewriter.createBlock(&parallelOp.getRegion());
rewriter.setInsertionPoint(rewriter.create<mlir::omp::TerminatorOp>(loc));

// If mapping to host, the local induction variable and loop bounds need to
// be emitted as part of the `omp.parallel` op.
if (!mapToDevice) {
genLoopNestIndVarAllocs(rewriter, loopNest, mapper);
genLoopNestClauseOps(loc, rewriter, loopNest, mapper, loopNestClauseOps);
}
genLoopNestIndVarAllocs(rewriter, loopNest, mapper);
genLoopNestClauseOps(loc, rewriter, loopNest, mapper, loopNestClauseOps);

return parallelOp;
}
Expand Down
3 changes: 1 addition & 2 deletions flang/test/Transforms/DoConcurrent/basic_device.f90
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ program do_concurrent_basic

! CHECK: %[[A_DEV_DECL:.*]]:2 = hlfir.declare %[[A_ARG]]
! CHECK: omp.teams {
! CHECK-NEXT: omp.parallel {

! CHECK-NEXT: %[[ITER_VAR:.*]] = fir.alloca i32 {bindc_name = "i"}
! CHECK-NEXT: %[[BINDING:.*]]:2 = hlfir.declare %[[ITER_VAR]] {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
Expand All @@ -54,8 +55,6 @@ program do_concurrent_basic
! CHECK: %[[STEP:.*]] = arith.constant 1 : index

! CHECK-NEXT: omp.distribute {
! CHECK-NEXT: omp.parallel {

! CHECK-NEXT: omp.wsloop {

! CHECK-NEXT: omp.loop_nest (%[[ARG0:.*]]) : index = (%[[LB]]) to (%[[UB]]) inclusive step (%[[STEP]]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ program main
! DEVICE: omp.target
! DEVICE: omp.teams

! HOST: omp.parallel {
! COMMON: omp.parallel {

! COMMON-NEXT: %[[ITER_VAR_I:.*]] = fir.alloca i32 {bindc_name = "i"}
! COMMON-NEXT: %[[BINDING_I:.*]]:2 = hlfir.declare %[[ITER_VAR_I]] {uniq_name = "_QFEi"}
Expand Down Expand Up @@ -94,8 +94,7 @@ program main
! COMMON: %[[UB_K:.*]] = fir.convert %[[C30]] : (i32) -> index
! COMMON: %[[STEP_K:.*]] = arith.constant 1 : index

! DEVICE: omp.distribute
! DEVICE-NEXT: omp.parallel
! DEVICE: omp.distribute

! COMMON: omp.wsloop {
! COMMON-NEXT: omp.loop_nest
Expand Down
4 changes: 3 additions & 1 deletion flang/test/Transforms/DoConcurrent/not_perfectly_nested.f90
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ program main
! DEVICE: %[[TARGET_K_DECL:.*]]:2 = hlfir.declare %[[K_ARG]] {uniq_name = "_QFEk"}

! DEVICE: omp.teams
! DEVICE: omp.distribute

! COMMON: omp.parallel {

! DEVICE: omp.distribute

! COMMON: omp.wsloop {
! COMMON: omp.loop_nest ({{[^[:space:]]+}}) {{.*}} {
! COMMON: fir.do_loop %[[J_IV:.*]] = {{.*}} {
Expand Down
4 changes: 3 additions & 1 deletion flang/test/Transforms/DoConcurrent/skip_all_nested_loops.f90
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ program main
! DEVICE: %[[TARGET_K_DECL:.*]]:2 = hlfir.declare %[[K_ARG]] {uniq_name = "_QFEk"}

! DEVICE: omp.teams
! DEVICE: omp.distribute

! COMMON: omp.parallel {

! DEVICE: omp.distribute

! COMMON: omp.wsloop {
! COMMON: omp.loop_nest ({{[^[:space:]]+}}) {{.*}} {
! COMMON: fir.do_loop {{.*}} iter_args(%[[J_IV:.*]] = {{.*}}) -> {{.*}} {
Expand Down

0 comments on commit 9a95922

Please sign in to comment.