diff --git a/flang/lib/Parser/openmp-parsers.cpp b/flang/lib/Parser/openmp-parsers.cpp --- a/flang/lib/Parser/openmp-parsers.cpp +++ b/flang/lib/Parser/openmp-parsers.cpp @@ -569,7 +569,7 @@ // 2.4 Requires construct TYPE_PARSER(sourced(construct( - verbatim("REQUIRES"_tok), some(Parser{} / maybe(","_tok))))) + verbatim("REQUIRES"_tok), Parser{}))) // 2.15.2 Threadprivate directive TYPE_PARSER(sourced(construct( diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h --- a/flang/lib/Semantics/check-omp-structure.h +++ b/flang/lib/Semantics/check-omp-structure.h @@ -266,6 +266,7 @@ const parser::OmpObjectList &ompObjectList); void CheckPredefinedAllocatorRestriction( const parser::CharBlock &source, const parser::Name &name); + void CheckAllowedRequiresClause(llvmOmpClause clause); bool isPredefinedAllocator{false}; void EnterDirectiveNest(const int index) { directiveNest_[index]++; } void ExitDirectiveNest(const int index) { directiveNest_[index]--; } @@ -279,6 +280,11 @@ LastType }; int directiveNest_[LastType + 1] = {0}; + + bool deviceConstructFound = false; + bool atomicDirectiveDefaultOrderFound = false; + std::optional + atomicDefaultMemoryOrder; }; } // namespace Fortran::semantics #endif // FORTRAN_SEMANTICS_CHECK_OMP_STRUCTURE_H_ diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp --- a/flang/lib/Semantics/check-omp-structure.cpp +++ b/flang/lib/Semantics/check-omp-structure.cpp @@ -789,6 +789,7 @@ switch (beginDir.v) { case llvm::omp::Directive::OMPD_target: + deviceConstructFound = true; if (CheckTargetBlockOnlyTeams(block)) { EnterDirectiveNest(TargetBlockOnlyTeams); } @@ -1121,18 +1122,37 @@ const auto &dir{std::get(x.t)}; const auto &spec{std::get(x.t)}; if (const auto *objectList{parser::Unwrap(spec.u)}) { + deviceConstructFound = true; CheckIsVarPartOfAnotherVar(dir.source, *objectList); CheckThreadprivateOrDeclareTargetVar(*objectList); } else if (const auto *clauseList{ parser::Unwrap(spec.u)}) { + bool toClauseFound = false, deviceTypeClauseFound = false; for (const auto &clause : clauseList->v) { - if (const auto *toClause{std::get_if(&clause.u)}) { - CheckIsVarPartOfAnotherVar(dir.source, toClause->v); - CheckThreadprivateOrDeclareTargetVar(toClause->v); - } else if (const auto *linkClause{ - std::get_if(&clause.u)}) { - CheckIsVarPartOfAnotherVar(dir.source, linkClause->v); - CheckThreadprivateOrDeclareTargetVar(linkClause->v); + common::visit( + common::visitors{ + [&](const parser::OmpClause::To &toClause) { + CheckIsVarPartOfAnotherVar(dir.source, toClause.v); + CheckThreadprivateOrDeclareTargetVar(toClause.v); + toClauseFound = true; + }, + [&](const parser::OmpClause::Link &linkClause) { + CheckIsVarPartOfAnotherVar(dir.source, linkClause.v); + CheckThreadprivateOrDeclareTargetVar(linkClause.v); + }, + [&](const parser::OmpClause::DeviceType &deviceTypeClause) { + deviceTypeClauseFound = true; + if (deviceTypeClause.v.v != + parser::OmpDeviceTypeClause::Type::Host) { + deviceConstructFound = true; + } + }, + [&](const auto &) {}, + }, + clause.u); + + if (toClauseFound && !deviceTypeClauseFound) { + deviceConstructFound = true; } } } @@ -1634,6 +1654,9 @@ if (rightHandClauseList) { checkForValidMemoryOrderClause(rightHandClauseList); } + if (numMemoryOrderClause == 0) { + atomicDirectiveDefaultOrderFound = true; + } } void OmpStructureChecker::Enter(const parser::OpenMPAtomicConstruct &x) { @@ -1827,7 +1850,6 @@ // Following clauses do not have a separate node in parse-tree.h. CHECK_SIMPLE_CLAUSE(AcqRel, OMPC_acq_rel) CHECK_SIMPLE_CLAUSE(Acquire, OMPC_acquire) -CHECK_SIMPLE_CLAUSE(AtomicDefaultMemOrder, OMPC_atomic_default_mem_order) CHECK_SIMPLE_CLAUSE(Affinity, OMPC_affinity) CHECK_SIMPLE_CLAUSE(Allocate, OMPC_allocate) CHECK_SIMPLE_CLAUSE(Capture, OMPC_capture) @@ -1837,7 +1859,6 @@ CHECK_SIMPLE_CLAUSE(Detach, OMPC_detach) CHECK_SIMPLE_CLAUSE(DeviceType, OMPC_device_type) CHECK_SIMPLE_CLAUSE(DistSchedule, OMPC_dist_schedule) -CHECK_SIMPLE_CLAUSE(DynamicAllocators, OMPC_dynamic_allocators) CHECK_SIMPLE_CLAUSE(Exclusive, OMPC_exclusive) CHECK_SIMPLE_CLAUSE(Final, OMPC_final) CHECK_SIMPLE_CLAUSE(Flush, OMPC_flush) @@ -1850,7 +1871,6 @@ CHECK_SIMPLE_CLAUSE(Nontemporal, OMPC_nontemporal) CHECK_SIMPLE_CLAUSE(Order, OMPC_order) CHECK_SIMPLE_CLAUSE(Read, OMPC_read) -CHECK_SIMPLE_CLAUSE(ReverseOffload, OMPC_reverse_offload) CHECK_SIMPLE_CLAUSE(Threadprivate, OMPC_threadprivate) CHECK_SIMPLE_CLAUSE(Threads, OMPC_threads) CHECK_SIMPLE_CLAUSE(Inbranch, OMPC_inbranch) @@ -1871,8 +1891,6 @@ CHECK_SIMPLE_CLAUSE(Sizes, OMPC_sizes) CHECK_SIMPLE_CLAUSE(TaskReduction, OMPC_task_reduction) CHECK_SIMPLE_CLAUSE(To, OMPC_to) -CHECK_SIMPLE_CLAUSE(UnifiedAddress, OMPC_unified_address) -CHECK_SIMPLE_CLAUSE(UnifiedSharedMemory, OMPC_unified_shared_memory) CHECK_SIMPLE_CLAUSE(Uniform, OMPC_uniform) CHECK_SIMPLE_CLAUSE(Unknown, OMPC_unknown) CHECK_SIMPLE_CLAUSE(Untied, OMPC_untied) @@ -2796,4 +2814,61 @@ clause.u); } +void OmpStructureChecker::Enter( + const parser::OmpClause::AtomicDefaultMemOrder &x) { + CheckAllowed(llvm::omp::Clause::OMPC_atomic_default_mem_order); + + // Check that multiple occurrences of this clause match + parser::OmpAtomicDefaultMemOrderClause::Type memOrder = x.v.v; + if (atomicDefaultMemoryOrder && atomicDefaultMemoryOrder != memOrder) { + context_.Say(GetContext().clauseSource, + "Conflicting '%s' REQUIRES clauses found in compilation " + "unit"_err_en_US, + parser::ToUpperCaseLetters( + getClauseName(llvmOmpClause::OMPC_atomic_default_mem_order).str())); + return; + } + atomicDefaultMemoryOrder = memOrder; + // Check that it does not appear after an atomic operation without + // memory_order defined + if (atomicDirectiveDefaultOrderFound) { + context_.Say(GetContext().clauseSource, + "'%s' REQUIRES clause found lexically after atomic " + "operation with the '%s' clause not defined"_err_en_US, + parser::ToUpperCaseLetters( + getClauseName(llvmOmpClause::OMPC_atomic_default_mem_order).str()), + parser::ToUpperCaseLetters( + getClauseName(llvmOmpClause::OMPC_memory_order).str())); + } +} + +void OmpStructureChecker::Enter(const parser::OmpClause::DynamicAllocators &x) { + CheckAllowedRequiresClause(llvm::omp::Clause::OMPC_dynamic_allocators); +} + +void OmpStructureChecker::Enter(const parser::OmpClause::ReverseOffload &x) { + CheckAllowedRequiresClause(llvm::omp::Clause::OMPC_reverse_offload); +} + +void OmpStructureChecker::Enter(const parser::OmpClause::UnifiedAddress &x) { + CheckAllowedRequiresClause(llvm::omp::Clause::OMPC_unified_address); +} + +void OmpStructureChecker::Enter( + const parser::OmpClause::UnifiedSharedMemory &x) { + CheckAllowedRequiresClause(llvm::omp::Clause::OMPC_unified_shared_memory); +} + +void OmpStructureChecker::CheckAllowedRequiresClause(llvmOmpClause clause) { + CheckAllowed(clause); + + // Check that it does not appear after a device construct + if (deviceConstructFound) { + context_.Say(GetContext().clauseSource, + "'%s' REQUIRES clause found lexically after device " + "construct"_err_en_US, + parser::ToUpperCaseLetters(getClauseName(clause).str())); + } +} + } // namespace Fortran::semantics diff --git a/flang/test/Semantics/OpenMP/requires01.f90 b/flang/test/Semantics/OpenMP/requires01.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/OpenMP/requires01.f90 @@ -0,0 +1,14 @@ +! RUN: %python %S/../test_errors.py %s %flang -fopenmp +! OpenMP Version 5.0 +! 2.4 Requires directive +! All atomic_default_mem_order clauses in 'requires' directives found within a +! compilation unit must specify the same ordering. + +subroutine f + !$omp requires atomic_default_mem_order(seq_cst) +end subroutine f + +subroutine g + !ERROR: Conflicting 'ATOMIC_DEFAULT_MEM_ORDER' REQUIRES clauses found in compilation unit + !$omp requires atomic_default_mem_order(relaxed) +end subroutine g diff --git a/flang/test/Semantics/OpenMP/requires02.f90 b/flang/test/Semantics/OpenMP/requires02.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/OpenMP/requires02.f90 @@ -0,0 +1,17 @@ +! RUN: %python %S/../test_errors.py %s %flang -fopenmp +! OpenMP Version 5.0 +! 2.4 Requires directive +! All atomic_default_mem_order clauses in 'requires' directives must come +! strictly before any atomic directives on which the memory_order clause is not +! specified. + +subroutine f + integer :: a = 0 + !$omp atomic + a = a + 1 +end subroutine f + +subroutine g + !ERROR: 'ATOMIC_DEFAULT_MEM_ORDER' REQUIRES clause found lexically after atomic operation with the 'MEMORY_ORDER' clause not defined + !$omp requires atomic_default_mem_order(relaxed) +end subroutine g diff --git a/flang/test/Semantics/OpenMP/requires03.f90 b/flang/test/Semantics/OpenMP/requires03.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/OpenMP/requires03.f90 @@ -0,0 +1,21 @@ +! RUN: %python %S/../test_errors.py %s %flang -fopenmp +! OpenMP Version 5.0 +! 2.4 Requires directive +! Target-related clauses in 'requires' directives must come strictly before any +! device constructs, such as target regions. + +subroutine f + !$omp target + !$omp end target +end subroutine f + +subroutine g + !ERROR: 'DYNAMIC_ALLOCATORS' REQUIRES clause found lexically after device construct + !$omp requires dynamic_allocators + !ERROR: 'REVERSE_OFFLOAD' REQUIRES clause found lexically after device construct + !$omp requires reverse_offload + !ERROR: 'UNIFIED_ADDRESS' REQUIRES clause found lexically after device construct + !$omp requires unified_address + !ERROR: 'UNIFIED_SHARED_MEMORY' REQUIRES clause found lexically after device construct + !$omp requires unified_shared_memory +end subroutine g diff --git a/flang/test/Semantics/OpenMP/requires04.f90 b/flang/test/Semantics/OpenMP/requires04.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/OpenMP/requires04.f90 @@ -0,0 +1,20 @@ +! RUN: %python %S/../test_errors.py %s %flang -fopenmp +! OpenMP Version 5.0 +! 2.4 Requires directive +! Target-related clauses in 'requires' directives must come strictly before any +! device constructs, such as declare target with device_type=nohost|any. + +subroutine f + !$omp declare target device_type(nohost) +end subroutine f + +subroutine g + !ERROR: 'DYNAMIC_ALLOCATORS' REQUIRES clause found lexically after device construct + !$omp requires dynamic_allocators + !ERROR: 'REVERSE_OFFLOAD' REQUIRES clause found lexically after device construct + !$omp requires reverse_offload + !ERROR: 'UNIFIED_ADDRESS' REQUIRES clause found lexically after device construct + !$omp requires unified_address + !ERROR: 'UNIFIED_SHARED_MEMORY' REQUIRES clause found lexically after device construct + !$omp requires unified_shared_memory +end subroutine g diff --git a/flang/test/Semantics/OpenMP/requires05.f90 b/flang/test/Semantics/OpenMP/requires05.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/OpenMP/requires05.f90 @@ -0,0 +1,20 @@ +! RUN: %python %S/../test_errors.py %s %flang -fopenmp +! OpenMP Version 5.0 +! 2.4 Requires directive +! Target-related clauses in 'requires' directives must come strictly before any +! device constructs, such as declare target with 'to' clause and no device_type. + +subroutine f + !$omp declare target to(f) +end subroutine f + +subroutine g + !ERROR: 'DYNAMIC_ALLOCATORS' REQUIRES clause found lexically after device construct + !$omp requires dynamic_allocators + !ERROR: 'REVERSE_OFFLOAD' REQUIRES clause found lexically after device construct + !$omp requires reverse_offload + !ERROR: 'UNIFIED_ADDRESS' REQUIRES clause found lexically after device construct + !$omp requires unified_address + !ERROR: 'UNIFIED_SHARED_MEMORY' REQUIRES clause found lexically after device construct + !$omp requires unified_shared_memory +end subroutine g diff --git a/flang/test/Semantics/OpenMP/requires06.f90 b/flang/test/Semantics/OpenMP/requires06.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/OpenMP/requires06.f90 @@ -0,0 +1,20 @@ +! RUN: %python %S/../test_errors.py %s %flang -fopenmp +! OpenMP Version 5.0 +! 2.4 Requires directive +! Target-related clauses in 'requires' directives must come strictly before any +! device constructs, such as declare target with extended list. + +subroutine f + !$omp declare target (f) +end subroutine f + +subroutine g + !ERROR: 'DYNAMIC_ALLOCATORS' REQUIRES clause found lexically after device construct + !$omp requires dynamic_allocators + !ERROR: 'REVERSE_OFFLOAD' REQUIRES clause found lexically after device construct + !$omp requires reverse_offload + !ERROR: 'UNIFIED_ADDRESS' REQUIRES clause found lexically after device construct + !$omp requires unified_address + !ERROR: 'UNIFIED_SHARED_MEMORY' REQUIRES clause found lexically after device construct + !$omp requires unified_shared_memory +end subroutine g