Skip to content

Commit 18dd299

Browse files
skatrakergawy
andauthored
[Flang][MLIR][OpenMP] Host-evaluation of omp.loop bounds (llvm#133908)
This patch updates Flang lowering and kernel flags identification in MLIR so that loop bounds on `target teams loop` constructs are evaluated on the host, making the trip count available to the corresponding `__tgt_target_kernel` call emitted for the target region. This is necessary in order to properly execute these constructs as `target teams distribute parallel do`. Co-authored-by: Kareem Ergawy <[email protected]>
1 parent 7145ead commit 18dd299

File tree

5 files changed

+64
-16
lines changed

5 files changed

+64
-16
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,6 @@ static void processHostEvalClauses(lower::AbstractConverter &converter,
557557
HostEvalInfo &hostInfo = hostEvalInfo.back();
558558

559559
switch (extractOmpDirective(*ompEval)) {
560-
// Cases where 'teams' and target SPMD clauses might be present.
561560
case OMPD_teams_distribute_parallel_do:
562561
case OMPD_teams_distribute_parallel_do_simd:
563562
cp.processThreadLimit(stmtCtx, hostInfo.ops);
@@ -575,18 +574,16 @@ static void processHostEvalClauses(lower::AbstractConverter &converter,
575574
cp.processCollapse(loc, eval, hostInfo.ops, hostInfo.iv);
576575
break;
577576

578-
// Cases where 'teams' clauses might be present, and target SPMD is
579-
// possible by looking at nested evaluations.
580577
case OMPD_teams:
581578
cp.processThreadLimit(stmtCtx, hostInfo.ops);
582579
[[fallthrough]];
583580
case OMPD_target_teams:
584581
cp.processNumTeams(stmtCtx, hostInfo.ops);
585-
processSingleNestedIf(
586-
[](Directive nestedDir) { return topDistributeSet.test(nestedDir); });
582+
processSingleNestedIf([](Directive nestedDir) {
583+
return topDistributeSet.test(nestedDir) || topLoopSet.test(nestedDir);
584+
});
587585
break;
588586

589-
// Cases where only 'teams' host-evaluated clauses might be present.
590587
case OMPD_teams_distribute:
591588
case OMPD_teams_distribute_simd:
592589
cp.processThreadLimit(stmtCtx, hostInfo.ops);
@@ -597,6 +594,16 @@ static void processHostEvalClauses(lower::AbstractConverter &converter,
597594
cp.processNumTeams(stmtCtx, hostInfo.ops);
598595
break;
599596

597+
case OMPD_teams_loop:
598+
cp.processThreadLimit(stmtCtx, hostInfo.ops);
599+
[[fallthrough]];
600+
case OMPD_target_teams_loop:
601+
cp.processNumTeams(stmtCtx, hostInfo.ops);
602+
[[fallthrough]];
603+
case OMPD_loop:
604+
cp.processCollapse(loc, eval, hostInfo.ops, hostInfo.iv);
605+
break;
606+
600607
// Standalone 'target' case.
601608
case OMPD_target: {
602609
processSingleNestedIf(

flang/test/Lower/OpenMP/generic-loop-rewriting.f90

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ subroutine target_teams_loop
1111
implicit none
1212
integer :: x, i
1313

14-
!$omp target teams loop
14+
!$omp teams loop
1515
do i = 0, 10
1616
x = x + i
1717
end do
@@ -22,19 +22,15 @@ subroutine target_teams_loop
2222
implicit none
2323
integer :: x, i
2424

25-
!$omp target teams loop bind(teams)
25+
!$omp teams loop bind(teams)
2626
do i = 0, 10
2727
x = x + i
2828
end do
2929
end subroutine target_teams_loop
3030

3131
!CHECK-LABEL: func.func @_QPtarget_teams_loop
32-
!CHECK: omp.target map_entries(
33-
!CHECK-SAME: %{{.*}} -> %[[I_ARG:[^[:space:]]+]],
34-
!CHECK-SAME: %{{.*}} -> %[[X_ARG:[^[:space:]]+]] : {{.*}}) {
35-
36-
!CHECK: %[[I_DECL:.*]]:2 = hlfir.declare %[[I_ARG]]
37-
!CHECK: %[[X_DECL:.*]]:2 = hlfir.declare %[[X_ARG]]
32+
!CHECK: %[[I_DECL:.*]]:2 = hlfir.declare %{{.*}} {uniq_name = "{{.*}}i"}
33+
!CHECK: %[[X_DECL:.*]]:2 = hlfir.declare %{{.*}} {uniq_name = "{{.*}}x"}
3834

3935
!CHECK: omp.teams {
4036

@@ -51,6 +47,7 @@ end subroutine target_teams_loop
5147
!CHECK-SAME: (%[[LB]]) to (%[[UB]]) inclusive step (%[[STEP]]) {
5248
!CHECK: %[[I_PRIV_DECL:.*]]:2 = hlfir.declare %[[I_PRIV_ARG]]
5349
!CHECK: hlfir.assign %{{.*}} to %[[I_PRIV_DECL]]#0 : i32, !fir.ref<i32>
50+
!CHECK: hlfir.assign %{{.*}} to %[[X_DECL]]#0 : i32, !fir.ref<i32>
5451
!CHECK: }
5552
!CHECK: }
5653
!CHECK: }

flang/test/Lower/OpenMP/host-eval.f90

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,28 @@ subroutine distribute_simd()
258258
!$omp end distribute simd
259259
!$omp end teams
260260
end subroutine distribute_simd
261+
262+
! BOTH-LABEL: func.func @_QPloop
263+
subroutine loop()
264+
! BOTH: omp.target
265+
266+
! HOST-SAME: host_eval(%{{.*}} -> %[[LB:.*]], %{{.*}} -> %[[UB:.*]], %{{.*}} -> %[[STEP:.*]] : i32, i32, i32)
267+
268+
! DEVICE-NOT: host_eval({{.*}})
269+
! DEVICE-SAME: {
270+
271+
! BOTH: omp.teams
272+
!$omp target teams
273+
274+
! BOTH: omp.parallel
275+
276+
! BOTH: omp.distribute
277+
! BOTH-NEXT: omp.wsloop
278+
! BOTH-NEXT: omp.loop_nest
279+
280+
! HOST-SAME: (%{{.*}}) : i32 = (%[[LB]]) to (%[[UB]]) inclusive step (%[[STEP]])
281+
!$omp loop
282+
do i=1,10
283+
end do
284+
!$omp end target teams
285+
end subroutine loop

mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,16 +2058,19 @@ TargetOp::getKernelExecFlags(Operation *capturedOp) {
20582058
long numWrappers = std::distance(innermostWrapper, wrappers.end());
20592059

20602060
// Detect Generic-SPMD: target-teams-distribute[-simd].
2061+
// Detect SPMD: target-teams-loop.
20612062
if (numWrappers == 1) {
2062-
if (!isa<DistributeOp>(innermostWrapper))
2063+
if (!isa<DistributeOp, LoopOp>(innermostWrapper))
20632064
return OMP_TGT_EXEC_MODE_GENERIC;
20642065

20652066
Operation *teamsOp = (*innermostWrapper)->getParentOp();
20662067
if (!isa_and_present<TeamsOp>(teamsOp))
20672068
return OMP_TGT_EXEC_MODE_GENERIC;
20682069

20692070
if (teamsOp->getParentOp() == targetOp.getOperation())
2070-
return OMP_TGT_EXEC_MODE_GENERIC_SPMD;
2071+
return isa<DistributeOp>(innermostWrapper)
2072+
? OMP_TGT_EXEC_MODE_GENERIC_SPMD
2073+
: OMP_TGT_EXEC_MODE_SPMD;
20712074
}
20722075

20732076
// Detect SPMD: target-teams-distribute-parallel-wsloop[-simd].

mlir/test/Dialect/OpenMP/ops.mlir

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,6 +2879,22 @@ func.func @omp_target_host_eval(%x : i32) {
28792879
}
28802880
omp.terminator
28812881
}
2882+
2883+
// CHECK: omp.target host_eval(%{{.*}} -> %[[HOST_ARG:.*]] : i32) {
2884+
// CHECK: omp.teams {
2885+
// CHECK: omp.loop {
2886+
// CHECK: omp.loop_nest (%{{.*}}) : i32 = (%[[HOST_ARG]]) to (%[[HOST_ARG]]) step (%[[HOST_ARG]]) {
2887+
omp.target host_eval(%x -> %arg0 : i32) {
2888+
omp.teams {
2889+
omp.loop {
2890+
omp.loop_nest (%iv) : i32 = (%arg0) to (%arg0) step (%arg0) {
2891+
omp.yield
2892+
}
2893+
}
2894+
omp.terminator
2895+
}
2896+
omp.terminator
2897+
}
28822898
return
28832899
}
28842900

0 commit comments

Comments
 (0)