Skip to content

Commit 0661af8

Browse files
[Flang][OpenMP] Add restriction about subobjects to firstprivate and … (llvm#89608)
…lastprivate OpenMP 5.2 standard (Section 5.3) defines privatization for list items. Section 3.2.1 in the standard defines list items to exclude variables that are part of other variables. This patch adds the restriction to firstprivate and lastprivates, it was previously added for privates. Fixes llvm#67227 Note: The specific checks that are added here are explicitly called out in OpenMP 4.0 (https://www.openmp.org/wp-content/uploads/OpenMP4.0.0.pdf) Sections 2.14.3.4 and 2.14.3.5 but in later standards have become implicit through other definitions.
1 parent d8b253b commit 0661af8

13 files changed

+68
-18
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,11 +2471,11 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Ordered &x) {
24712471

24722472
void OmpStructureChecker::Enter(const parser::OmpClause::Shared &x) {
24732473
CheckAllowed(llvm::omp::Clause::OMPC_shared);
2474-
CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v);
2474+
CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v, "SHARED");
24752475
}
24762476
void OmpStructureChecker::Enter(const parser::OmpClause::Private &x) {
24772477
CheckAllowed(llvm::omp::Clause::OMPC_private);
2478-
CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v);
2478+
CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v, "PRIVATE");
24792479
CheckIntentInPointer(x.v, llvm::omp::Clause::OMPC_private);
24802480
}
24812481

@@ -2513,7 +2513,8 @@ bool OmpStructureChecker::IsDataRefTypeParamInquiry(
25132513
}
25142514

25152515
void OmpStructureChecker::CheckIsVarPartOfAnotherVar(
2516-
const parser::CharBlock &source, const parser::OmpObjectList &objList) {
2516+
const parser::CharBlock &source, const parser::OmpObjectList &objList,
2517+
llvm::StringRef clause) {
25172518
for (const auto &ompObject : objList.v) {
25182519
common::visit(
25192520
common::visitors{
@@ -2539,7 +2540,8 @@ void OmpStructureChecker::CheckIsVarPartOfAnotherVar(
25392540
context_.Say(source,
25402541
"A variable that is part of another variable (as an "
25412542
"array or structure element) cannot appear in a "
2542-
"PRIVATE or SHARED clause"_err_en_US);
2543+
"%s clause"_err_en_US,
2544+
clause.data());
25432545
}
25442546
}
25452547
}
@@ -2552,6 +2554,8 @@ void OmpStructureChecker::CheckIsVarPartOfAnotherVar(
25522554

25532555
void OmpStructureChecker::Enter(const parser::OmpClause::Firstprivate &x) {
25542556
CheckAllowed(llvm::omp::Clause::OMPC_firstprivate);
2557+
2558+
CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v, "FIRSTPRIVATE");
25552559
CheckIsLoopIvPartOfClause(llvmOmpClause::OMPC_firstprivate, x.v);
25562560

25572561
SymbolSourceMap currSymbols;
@@ -2888,6 +2892,8 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Copyprivate &x) {
28882892
void OmpStructureChecker::Enter(const parser::OmpClause::Lastprivate &x) {
28892893
CheckAllowed(llvm::omp::Clause::OMPC_lastprivate);
28902894

2895+
CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v, "LASTPRIVATE");
2896+
28912897
DirectivesClauseTriple dirClauseTriple;
28922898
SymbolSourceMap currSymbols;
28932899
GetSymbolsInObjectList(x.v, currSymbols);

flang/lib/Semantics/check-omp-structure.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ class OmpStructureChecker
163163
void CheckDependArraySection(
164164
const common::Indirection<parser::ArrayElement> &, const parser::Name &);
165165
bool IsDataRefTypeParamInquiry(const parser::DataRef *dataRef);
166-
void CheckIsVarPartOfAnotherVar(
167-
const parser::CharBlock &source, const parser::OmpObjectList &objList);
166+
void CheckIsVarPartOfAnotherVar(const parser::CharBlock &source,
167+
const parser::OmpObjectList &objList, llvm::StringRef clause = "");
168168
void CheckThreadprivateOrDeclareTargetVar(
169169
const parser::OmpObjectList &objList);
170170
void CheckSymbolNames(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
! RUN: %python %S/../test_errors.py %s %flang -fopenmp
2+
! OpenMP Version 5.2, Sections 3.2.1 & 5.3
3+
subroutine omp_firstprivate(init)
4+
integer :: init
5+
integer :: a(10)
6+
type my_type
7+
integer :: val
8+
end type my_type
9+
type(my_type) :: my_var
10+
11+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a FIRSTPRIVATE clause
12+
!$omp parallel firstprivate(a(2))
13+
a(2) = init
14+
!$omp end parallel
15+
16+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a FIRSTPRIVATE clause
17+
!$omp parallel firstprivate(my_var%val)
18+
my_var%val = init
19+
!$omp end parallel
20+
end subroutine
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
! RUN: %python %S/../test_errors.py %s %flang -fopenmp
2+
! OpenMP Version 5.2, Sections 3.2.1 & 5.3
3+
subroutine omp_lastprivate(init)
4+
integer :: init
5+
integer :: i, a(10)
6+
type my_type
7+
integer :: val
8+
end type my_type
9+
type(my_type) :: my_var
10+
11+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a LASTPRIVATE clause
12+
!$omp do lastprivate(a(2))
13+
do i=1, 10
14+
a(2) = init
15+
end do
16+
!$omp end do
17+
18+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a LASTPRIVATE clause
19+
!$omp do lastprivate(my_var%val)
20+
do i=1, 10
21+
my_var%val = init
22+
end do
23+
!$omp end do
24+
end subroutine

flang/test/Semantics/OpenMP/parallel-private01.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ program omp_parallel_private
1010

1111
type(my_type) :: my_var
1212

13-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
13+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE clause
1414
!$omp parallel private(my_var%array)
1515
do i = 1, 10
1616
c(i) = a(i) + b(i) + k

flang/test/Semantics/OpenMP/parallel-private02.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ program omp_parallel_private
1010
array(i) = i
1111
end do
1212

13-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
13+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE clause
1414
!$omp parallel private(array(i))
1515
do i = 1, 10
1616
c(i) = a(i) + b(i) + k

flang/test/Semantics/OpenMP/parallel-private03.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ program omp_parallel_private
1717
arr(i) = 0.0
1818
end do
1919

20-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
20+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE clause
2121
!$omp parallel private(arr(i),intx)
2222
do i = 1, 10
2323
c(i) = a(i) + b(i) + k

flang/test/Semantics/OpenMP/parallel-private04.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ program omp_parallel_private
1717
arr(i) = 0.0
1818
end do
1919

20-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
20+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE clause
2121
!$omp parallel private(arr,intx,my_var%array(1))
2222
do i = 1, 10
2323
c(i) = a(i) + b(i) + k

flang/test/Semantics/OpenMP/parallel-sections01.f90

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ program OmpConstructSections01
1717
do i = 1, 10
1818
array(i) = i
1919
end do
20-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
20+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a SHARED clause
2121
!$omp parallel sections shared(array(i))
2222
!$omp end parallel sections
23-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
23+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a SHARED clause
2424
!$omp parallel sections shared(my_var%array)
2525
!$omp end parallel sections
2626

@@ -30,7 +30,7 @@ program OmpConstructSections01
3030
if (NT) 20, 30, 40
3131
!ERROR: invalid branch into an OpenMP structured block
3232
goto 20
33-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
33+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE clause
3434
!$omp parallel sections private(my_var%array)
3535
!$omp section
3636
print *, "This is a single statement structured block"
@@ -53,7 +53,7 @@ program OmpConstructSections01
5353
30 print *, "Error in opening file"
5454
!$omp end parallel sections
5555
10 print *, 'Jump from section'
56-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
56+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE clause
5757
!$omp parallel sections private(array(i))
5858
!$omp section
5959
40 print *, 'Error in opening file'

flang/test/Semantics/OpenMP/parallel-shared01.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ program omp_parallel_shared
1010

1111
type(my_type) :: my_var
1212

13-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
13+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a SHARED clause
1414
!$omp parallel shared(my_var%array)
1515
do i = 1, 10
1616
c(i) = a(i) + b(i) + k

flang/test/Semantics/OpenMP/parallel-shared02.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ program omp_parallel_shared
1010
array(i) = i
1111
end do
1212

13-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
13+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a SHARED clause
1414
!$omp parallel shared(array(i))
1515
do i = 1, 10
1616
c(i) = a(i) + b(i) + k

flang/test/Semantics/OpenMP/parallel-shared03.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ program omp_parallel_shared
1717
arr(i) = 0.0
1818
end do
1919

20-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
20+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a SHARED clause
2121
!$omp parallel shared(arr(i),intx)
2222
do i = 1, 10
2323
c(i) = a(i) + b(i) + k

flang/test/Semantics/OpenMP/parallel-shared04.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ program omp_parallel_shared
1717
arr(i) = 0.0
1818
end do
1919

20-
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a PRIVATE or SHARED clause
20+
!ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in a SHARED clause
2121
!$omp parallel shared(arr,intx,my_var%array(1))
2222
do i = 1, 10
2323
c(i) = a(i) + b(i) + k

0 commit comments

Comments
 (0)