Skip to content

Commit 2f24587

Browse files
authored
[flang][OpenMP] Handle unstructured CF in compound loop constructs (#111111)
Fixes a bug in handling unstructured control-flow in compound loop constructs. The fix makes sure that unstructured CF does not get lowered until we reach the last item of the compound construct. This way, we avoid moving block of unstructured loops in-between the middle items of the construct and messing (i.e. adding operations) to these block while doing so.
1 parent 208f42f commit 2f24587

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,11 @@ static void createBodyOfOp(mlir::Operation &op, const OpWithBodyGenInfo &info,
590590
mlir::Operation *marker = insertMarker(firOpBuilder);
591591

592592
// If it is an unstructured region, create empty blocks for all evaluations.
593-
if (info.eval.lowerAsUnstructured())
593+
if (lower::omp::isLastItemInQueue(item, queue) &&
594+
info.eval.lowerAsUnstructured()) {
594595
lower::createEmptyRegionBlocks<mlir::omp::TerminatorOp, mlir::omp::YieldOp>(
595596
firOpBuilder, info.eval.getNestedEvaluations());
597+
}
596598

597599
// Start with privatization, so that the lowering of the nested
598600
// code will use the right symbols.
@@ -966,7 +968,8 @@ static void genBodyOfTargetOp(
966968

967969
// Create blocks for unstructured regions. This has to be done since
968970
// blocks are initially allocated with the function as the parent region.
969-
if (eval.lowerAsUnstructured()) {
971+
if (lower::omp::isLastItemInQueue(item, queue) &&
972+
eval.lowerAsUnstructured()) {
970973
lower::createEmptyRegionBlocks<mlir::omp::TerminatorOp, mlir::omp::YieldOp>(
971974
firOpBuilder, eval.getNestedEvaluations());
972975
}

flang/test/Lower/OpenMP/loop-compound.f90

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
! RUN: %flang_fc1 -fopenmp -emit-hlfir %s -o - | FileCheck %s
66

77
program main
8-
integer :: i
8+
integer :: i,j
99

1010
! TODO When composite constructs are supported add:
1111
! - TASKLOOP SIMD
@@ -231,4 +231,38 @@ program main
231231
do i = 1, 10
232232
end do
233233
!$omp end teams distribute simd
234+
235+
! ----------------------------------------------------------------------------
236+
! Unstructured control-flow in loop
237+
! ----------------------------------------------------------------------------
238+
! CHECK: omp.target
239+
! CHECK: omp.teams
240+
! CHECK: omp.parallel
241+
! CHECK: omp.distribute
242+
! CHECK-NEXT: omp.wsloop
243+
! CHECK-NEXT: omp.loop_nest
244+
!
245+
! Verify the conrol-flow of the unstructured inner loop.
246+
! CHECK: cf.br ^[[BB1:.*]]
247+
! CHECK: ^[[BB1]]:
248+
! CHECK: cf.br ^[[BB2:.*]]
249+
! CHECK: ^[[BB2]]:
250+
! CHECK: cf.cond_br %{{.*}}, ^[[BB3:.*]], ^[[BB6:.*]]
251+
! CHECK: ^[[BB3]]:
252+
! CHECK: cf.cond_br %{{.*}}, ^[[BB4:.*]], ^[[BB5:.*]]
253+
! CHECK: ^[[BB4]]:
254+
! CHECK: cf.br ^[[BB6]]
255+
! CHECK: ^[[BB5]]:
256+
! CHECK: cf.br ^[[BB2]]
257+
! CHECK: ^[[BB6]]:
258+
! CHECK-NEXT: omp.yield
259+
!$omp target teams distribute parallel do
260+
do i = 1, 10
261+
outerloop: do j = i-1, i+i
262+
if (j == i) then
263+
exit outerloop
264+
end if
265+
end do outerloop
266+
end do
267+
!$omp end target teams distribute parallel do
234268
end program main

0 commit comments

Comments
 (0)