Skip to content

Commit 6dda3b1

Browse files
chandraghaleChandra Ghale
andauthored
[flang][OpenMP]: Allow orphaned distribute construct (#163546)
If there is a call inside a TEAMS construct, and that call contains a DISTRIBUTE construct, the DISTRIBUTE region is considered to be enclosed by the TEAMS region (based on the dynamic extent of the construct). Currently, Flang diagnoses this as an error, which is incorrect. For eg : ``` subroutine f !$omp distribute do i = 1, 100 ... end do end subroutine subroutine g !$omp teams call f ! this call is ok, distribute enclosed by teams !$omp end teams end subroutine ``` This patch adjusts the nesting check for the OpenMP DISTRIBUTE directive. It retains the error for DISTRIBUTE directives that are incorrectly nested lexically but downgrades it to a warning for orphaned directives to allow dynamic nesting, such as when a subroutine with DISTRIBUTE is called from within a TEAMS region. Co-authored-by: Chandra Ghale <[email protected]>
1 parent c8cf393 commit 6dda3b1

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

flang/lib/Semantics/check-omp-loop.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,23 @@ using namespace Fortran::semantics::omp;
127127

128128
void OmpStructureChecker::HasInvalidDistributeNesting(
129129
const parser::OpenMPLoopConstruct &x) {
130-
bool violation{false};
131130
const parser::OmpDirectiveName &beginName{x.BeginDir().DirName()};
132131
if (llvm::omp::topDistributeSet.test(beginName.v)) {
133132
// `distribute` region has to be nested
134-
if (!CurrentDirectiveIsNested()) {
135-
violation = true;
136-
} else {
133+
if (CurrentDirectiveIsNested()) {
137134
// `distribute` region has to be strictly nested inside `teams`
138135
if (!llvm::omp::bottomTeamsSet.test(GetContextParent().directive)) {
139-
violation = true;
136+
context_.Say(beginName.source,
137+
"`DISTRIBUTE` region has to be strictly nested inside `TEAMS` "
138+
"region."_err_en_US);
140139
}
140+
} else {
141+
// If not lexically nested (orphaned), issue a warning.
142+
context_.Say(beginName.source,
143+
"`DISTRIBUTE` must be dynamically enclosed in a `TEAMS` "
144+
"region."_warn_en_US);
141145
}
142146
}
143-
if (violation) {
144-
context_.Say(beginName.source,
145-
"`DISTRIBUTE` region has to be strictly nested inside `TEAMS` "
146-
"region."_err_en_US);
147-
}
148147
}
149148
void OmpStructureChecker::HasInvalidLoopBinding(
150149
const parser::OpenMPLoopConstruct &x) {

flang/test/Semantics/OpenMP/combined-constructs.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ program main
77
real(8) :: a(256), b(256)
88
N = 256
99

10-
!ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region.
10+
!WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region.
1111
!$omp distribute simd
1212
do i = 1, N
1313
a(i) = 3.14d0

flang/test/Semantics/OpenMP/do05.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ program omp_do
4949
end do
5050
!$omp end parallel do simd
5151

52-
!ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region.
52+
!WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region.
5353
!$omp distribute parallel do
5454
do i=1,10
5555
if( i == 3 ) then
@@ -64,7 +64,7 @@ program omp_do
6464
end do
6565
!$omp end distribute parallel do
6666

67-
!ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region.
67+
!WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region.
6868
!$omp distribute parallel do simd
6969
do i=1,10
7070
if( i == 3 ) then

flang/test/Semantics/OpenMP/linear-iter.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ SUBROUTINE LINEAR_BAD(N)
5353
!$omp end teams
5454
!$omp end target
5555

56-
!ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region.
56+
!WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region.
5757
!ERROR: Variable 'j' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
5858
!$omp distribute simd linear(i,j)
5959
do i = 1, N
@@ -63,7 +63,7 @@ SUBROUTINE LINEAR_BAD(N)
6363
enddo
6464
!$omp end distribute simd
6565

66-
!ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region.
66+
!WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region.
6767
!ERROR: Variable 'j' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
6868
!$omp distribute simd linear(i,j) collapse(1)
6969
do i = 1, N
@@ -73,7 +73,7 @@ SUBROUTINE LINEAR_BAD(N)
7373
enddo
7474
!$omp end distribute simd
7575

76-
!ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region.
76+
!WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region.
7777
!$omp distribute simd linear(i,j) collapse(2)
7878
do i = 1, N
7979
do j = 1, N

flang/test/Semantics/OpenMP/nested-distribute.f90

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
! RUN: %python %S/../test_errors.py %s %flang -fopenmp
22
! Check OpenMP clause validity for the following directives:
33
! 2.10 Device constructs
4+
5+
subroutine f
6+
integer :: i
7+
!WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region.
8+
!$omp distribute
9+
do i = 1, 100
10+
print *, "hello"
11+
end do
12+
end subroutine
413
program main
514

615
real(8) :: arrayA(256), arrayB(256)
@@ -108,4 +117,8 @@ program main
108117
end do
109118
!$omp end distribute
110119
!$omp end task
120+
121+
!$omp teams
122+
call foo
123+
!$omp end teams
111124
end program main

0 commit comments

Comments
 (0)