Skip to content

[Flang][OpenMP] Allow copyprivate and nowait on the directive clauses #127769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions flang/lib/Semantics/check-omp-structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,39 @@ void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
deviceConstructFound_ = true;
}

if (GetContext().directive == llvm::omp::Directive::OMPD_single) {
bool foundCopyPrivate{false};
bool foundNowait{false};
parser::CharBlock NowaitSource{""};
auto catchCopyPrivateNowaitClauses = [&](const auto &dir) {
for (auto &clause : std::get<parser::OmpClauseList>(dir.t).v) {
if (clause.Id() == llvm::omp::Clause::OMPC_copyprivate) {
if (foundCopyPrivate) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is incorrect and needs to be tied to the list-items in the copyprivate clause. Several copyprivate clauses can be at the directive, as long as they have different list-items.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this, thanks for noticing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

context_.Say(clause.source,
"At most one COPYPRIVATE clause can appear on the SINGLE directive"_err_en_US);
} else {
foundCopyPrivate = true;
}
} else if (clause.Id() == llvm::omp::Clause::OMPC_nowait) {
if (foundNowait) {
context_.Say(clause.source,
"At most one NOWAIT clause can appear on the SINGLE directive"_err_en_US);
} else {
foundNowait = true;
NowaitSource = clause.source;
}
}
}
};
catchCopyPrivateNowaitClauses(beginBlockDir);
catchCopyPrivateNowaitClauses(endBlockDir);
unsigned version{context_.langOptions().OpenMPVersion};
if (version <= 52 && foundCopyPrivate && foundNowait) {
context_.Say(NowaitSource,
"NOWAIT clause must not be used with COPYPRIVATE clause on the SINGLE directive"_err_en_US);
}
}

switch (beginDir.v) {
case llvm::omp::Directive::OMPD_target:
if (CheckTargetBlockOnlyTeams(block)) {
Expand Down Expand Up @@ -2852,12 +2885,6 @@ void OmpStructureChecker::Leave(const parser::OmpClauseList &) {
// clause
CheckMultListItems();

// 2.7.3 Single Construct Restriction
if (GetContext().directive == llvm::omp::Directive::OMPD_end_single) {
CheckNotAllowedIfClause(
llvm::omp::Clause::OMPC_copyprivate, {llvm::omp::Clause::OMPC_nowait});
}

auto testThreadprivateVarErr = [&](Symbol sym, parser::Name name,
llvmOmpClause clauseTy) {
if (sym.test(Symbol::Flag::OmpThreadprivate))
Expand Down Expand Up @@ -3481,15 +3508,6 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Private &x) {

void OmpStructureChecker::Enter(const parser::OmpClause::Nowait &x) {
CheckAllowedClause(llvm::omp::Clause::OMPC_nowait);
if (llvm::omp::noWaitClauseNotAllowedSet.test(GetContext().directive)) {
context_.Say(GetContext().clauseSource,
"%s clause is not allowed on the OMP %s directive,"
" use it on OMP END %s directive "_err_en_US,
parser::ToUpperCaseLetters(
getClauseName(llvm::omp::Clause::OMPC_nowait).str()),
parser::ToUpperCaseLetters(GetContext().directiveSource.ToString()),
parser::ToUpperCaseLetters(GetContext().directiveSource.ToString()));
}
}

bool OmpStructureChecker::IsDataRefTypeParamInquiry(
Expand Down Expand Up @@ -4220,15 +4238,6 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Copyprivate &x) {
CheckIntentInPointer(symbols, llvm::omp::Clause::OMPC_copyprivate);
CheckCopyingPolymorphicAllocatable(
symbols, llvm::omp::Clause::OMPC_copyprivate);
if (GetContext().directive == llvm::omp::Directive::OMPD_single) {
context_.Say(GetContext().clauseSource,
"%s clause is not allowed on the OMP %s directive,"
" use it on OMP END %s directive "_err_en_US,
parser::ToUpperCaseLetters(
getClauseName(llvm::omp::Clause::OMPC_copyprivate).str()),
parser::ToUpperCaseLetters(GetContext().directiveSource.ToString()),
parser::ToUpperCaseLetters(GetContext().directiveSource.ToString()));
}
}

void OmpStructureChecker::Enter(const parser::OmpClause::Lastprivate &x) {
Expand Down
7 changes: 3 additions & 4 deletions flang/test/Semantics/OpenMP/clause-validity01.f90
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,12 @@
!$omp parallel
b = 1
!ERROR: LASTPRIVATE clause is not allowed on the SINGLE directive
!ERROR: NOWAIT clause is not allowed on the OMP SINGLE directive, use it on OMP END SINGLE directive
!ERROR: NOWAIT clause must not be used with COPYPRIVATE clause on the SINGLE directive
!$omp single private(a) lastprivate(c) nowait
a = 3.14
!ERROR: Clause NOWAIT is not allowed if clause COPYPRIVATE appears on the END SINGLE directive
!ERROR: COPYPRIVATE variable 'a' may not appear on a PRIVATE or FIRSTPRIVATE clause on a SINGLE construct
!ERROR: At most one NOWAIT clause can appear on the SINGLE directive
!ERROR: At most one NOWAIT clause can appear on the SINGLE directive
!ERROR: At most one NOWAIT clause can appear on the END SINGLE directive
!$omp end single copyprivate(a) nowait nowait
c = 2
Expand All @@ -351,7 +352,6 @@
a = 1.0
!ERROR: COPYPRIVATE clause is not allowed on the END WORKSHARE directive
!$omp end workshare nowait copyprivate(a)
!ERROR: NOWAIT clause is not allowed on the OMP WORKSHARE directive, use it on OMP END WORKSHARE directive
!$omp workshare nowait
!$omp end workshare
!$omp end parallel
Expand Down Expand Up @@ -420,7 +420,6 @@
!$omp parallel
!ERROR: No ORDERED clause with a parameter can be specified on the DO SIMD directive
!ERROR: NOGROUP clause is not allowed on the DO SIMD directive
!ERROR: NOWAIT clause is not allowed on the OMP DO SIMD directive, use it on OMP END DO SIMD directive
!$omp do simd ordered(2) NOGROUP nowait
do i = 1, N
do j = 1, N
Expand Down
54 changes: 54 additions & 0 deletions flang/test/Semantics/OpenMP/single03.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp -fopenmp-version=52
!
! OpenMP Version 5.2
!
! 2.10.2 single Construct
! Copyprivate and Nowait clauses are allowed in both clause and end clause

subroutine omp_single
integer, save :: i
integer :: j
i = 10; j = 11

!ERROR: COPYPRIVATE variable 'i' is not PRIVATE or THREADPRIVATE in outer context
!ERROR: NOWAIT clause must not be used with COPYPRIVATE clause on the SINGLE directive
!$omp single copyprivate(i) nowait
print *, "omp single", i
!$omp end single

!$omp parallel private(i)
!$omp single copyprivate(i)
print *, "omp single", i
!$omp end single
!$omp end parallel

!$omp parallel
!ERROR: NOWAIT clause must not be used with COPYPRIVATE clause on the SINGLE directive
!$omp single nowait
print *, "omp single", i
!ERROR: COPYPRIVATE variable 'i' is not PRIVATE or THREADPRIVATE in outer context
!$omp end single copyprivate(i)

!ERROR: COPYPRIVATE variable 'i' is not PRIVATE or THREADPRIVATE in outer context
!$omp single copyprivate(i)
print *, "omp single", i
!ERROR: NOWAIT clause must not be used with COPYPRIVATE clause on the SINGLE directive
!$omp end single nowait

!ERROR: COPYPRIVATE variable 'j' may not appear on a PRIVATE or FIRSTPRIVATE clause on a SINGLE construct
!$omp single private(j) copyprivate(j)
print *, "omp single", j
!ERROR: At most one COPYPRIVATE clause can appear on the SINGLE directive
!ERROR: COPYPRIVATE variable 'j' may not appear on a PRIVATE or FIRSTPRIVATE clause on a SINGLE construct
!$omp end single copyprivate(j)

!$omp single nowait
print *, "omp single", j
!ERROR: At most one NOWAIT clause can appear on the SINGLE directive
!$omp end single nowait
!$omp end parallel

!$omp single nowait
print *, "omp single", i
!$omp end single
end subroutine omp_single
1 change: 0 additions & 1 deletion flang/test/Semantics/OpenMP/threadprivate04.f90
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ program main
!$omp parallel num_threads(x1)
!$omp end parallel

!ERROR: COPYPRIVATE clause is not allowed on the OMP SINGLE directive, use it on OMP END SINGLE directive
!$omp single copyprivate(x2, /blk1/)
!$omp end single

Expand Down