Skip to content

Commit de75db2

Browse files
committed
[Flang][OpenMP] Allow host evaluation of loop bounds for distribute
This patch adds `target teams distribute [simd]` and equivalent construct nests to the list of cases where loop bounds can be evaluated in the host, as they represent Generic-SPMD kernels for which the trip count must also be evaluated in advance to the kernel call.
1 parent 36e1b5f commit de75db2

File tree

2 files changed

+110
-5
lines changed

2 files changed

+110
-5
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,11 @@ static void processHostEvalClauses(lower::AbstractConverter &converter,
562562
[[fallthrough]];
563563
case OMPD_distribute_parallel_do:
564564
case OMPD_distribute_parallel_do_simd:
565-
cp.processCollapse(loc, eval, hostInfo.ops, hostInfo.iv);
566565
cp.processNumThreads(stmtCtx, hostInfo.ops);
566+
[[fallthrough]];
567+
case OMPD_distribute:
568+
case OMPD_distribute_simd:
569+
cp.processCollapse(loc, eval, hostInfo.ops, hostInfo.iv);
567570
break;
568571

569572
// Cases where 'teams' clauses might be present, and target SPMD is
@@ -573,10 +576,8 @@ static void processHostEvalClauses(lower::AbstractConverter &converter,
573576
[[fallthrough]];
574577
case OMPD_target_teams:
575578
cp.processNumTeams(stmtCtx, hostInfo.ops);
576-
processSingleNestedIf([](Directive nestedDir) {
577-
return nestedDir == OMPD_distribute_parallel_do ||
578-
nestedDir == OMPD_distribute_parallel_do_simd;
579-
});
579+
processSingleNestedIf(
580+
[](Directive nestedDir) { return topDistributeSet.test(nestedDir); });
580581
break;
581582

582583
// Cases where only 'teams' host-evaluated clauses might be present.
@@ -586,6 +587,7 @@ static void processHostEvalClauses(lower::AbstractConverter &converter,
586587
[[fallthrough]];
587588
case OMPD_target_teams_distribute:
588589
case OMPD_target_teams_distribute_simd:
590+
cp.processCollapse(loc, eval, hostInfo.ops, hostInfo.iv);
589591
cp.processNumTeams(stmtCtx, hostInfo.ops);
590592
break;
591593

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,106 @@ subroutine distribute_parallel_do_simd()
155155
!$omp end distribute parallel do simd
156156
!$omp end teams
157157
end subroutine distribute_parallel_do_simd
158+
159+
! BOTH-LABEL: func.func @_QPdistribute
160+
subroutine distribute()
161+
! BOTH: omp.target
162+
163+
! HOST-SAME: host_eval(%{{.*}} -> %[[LB:.*]], %{{.*}} -> %[[UB:.*]], %{{.*}} -> %[[STEP:.*]] : i32, i32, i32)
164+
165+
! DEVICE-NOT: host_eval({{.*}})
166+
! DEVICE-SAME: {
167+
168+
! BOTH: omp.teams
169+
!$omp target teams
170+
171+
! BOTH: omp.distribute
172+
! BOTH-NEXT: omp.loop_nest
173+
174+
! HOST-SAME: (%{{.*}}) : i32 = (%[[LB]]) to (%[[UB]]) inclusive step (%[[STEP]])
175+
!$omp distribute
176+
do i=1,10
177+
call foo()
178+
end do
179+
!$omp end distribute
180+
!$omp end target teams
181+
182+
! BOTH: omp.target
183+
! BOTH-NOT: host_eval({{.*}})
184+
! BOTH-SAME: {
185+
! BOTH: omp.teams
186+
!$omp target teams
187+
call foo() !< Prevents this from being Generic-SPMD.
188+
189+
! BOTH: omp.distribute
190+
!$omp distribute
191+
do i=1,10
192+
call foo()
193+
end do
194+
!$omp end distribute
195+
!$omp end target teams
196+
197+
! BOTH: omp.teams
198+
!$omp teams
199+
200+
! BOTH: omp.distribute
201+
!$omp distribute
202+
do i=1,10
203+
call foo()
204+
end do
205+
!$omp end distribute
206+
!$omp end teams
207+
end subroutine distribute
208+
209+
! BOTH-LABEL: func.func @_QPdistribute_simd
210+
subroutine distribute_simd()
211+
! BOTH: omp.target
212+
213+
! HOST-SAME: host_eval(%{{.*}} -> %[[LB:.*]], %{{.*}} -> %[[UB:.*]], %{{.*}} -> %[[STEP:.*]] : i32, i32, i32)
214+
215+
! DEVICE-NOT: host_eval({{.*}})
216+
! DEVICE-SAME: {
217+
218+
! BOTH: omp.teams
219+
!$omp target teams
220+
221+
! BOTH: omp.distribute
222+
! BOTH-NEXT: omp.simd
223+
! BOTH-NEXT: omp.loop_nest
224+
225+
! HOST-SAME: (%{{.*}}) : i32 = (%[[LB]]) to (%[[UB]]) inclusive step (%[[STEP]])
226+
!$omp distribute simd
227+
do i=1,10
228+
call foo()
229+
end do
230+
!$omp end distribute simd
231+
!$omp end target teams
232+
233+
! BOTH: omp.target
234+
! BOTH-NOT: host_eval({{.*}})
235+
! BOTH-SAME: {
236+
! BOTH: omp.teams
237+
!$omp target teams
238+
call foo() !< Prevents this from being Generic-SPMD.
239+
240+
! BOTH: omp.distribute
241+
! BOTH-NEXT: omp.simd
242+
!$omp distribute simd
243+
do i=1,10
244+
call foo()
245+
end do
246+
!$omp end distribute simd
247+
!$omp end target teams
248+
249+
! BOTH: omp.teams
250+
!$omp teams
251+
252+
! BOTH: omp.distribute
253+
! BOTH-NEXT: omp.simd
254+
!$omp distribute simd
255+
do i=1,10
256+
call foo()
257+
end do
258+
!$omp end distribute simd
259+
!$omp end teams
260+
end subroutine distribute_simd

0 commit comments

Comments
 (0)