diff --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h --- a/flang/include/flang/Parser/parse-tree.h +++ b/flang/include/flang/Parser/parse-tree.h @@ -3798,12 +3798,8 @@ UNION_CLASS_BOILERPLATE(OpenMPConstruct); std::variant -======= - OpenMPExecutableAllocate, OpenMPCriticalConstruct> ->>>>>>> 535696d... [flang]Add Parser Support for Allocate Directive u; }; diff --git a/flang/include/flang/Semantics/symbol.h b/flang/include/flang/Semantics/symbol.h --- a/flang/include/flang/Semantics/symbol.h +++ b/flang/include/flang/Semantics/symbol.h @@ -504,9 +504,10 @@ // OpenMP data-copying attribute OmpCopyIn, // OpenMP miscellaneous flags - OmpCommonBlock, OmpReduction, OmpAllocate, OmpDeclareSimd, - OmpDeclareTarget, OmpThreadprivate, OmpDeclareReduction, OmpFlushed, - OmpCriticalLock, OmpIfSpecified, OmpNone, OmpPreDetermined, OmpAligned); + OmpCommonBlock, OmpReduction, OmpAligned, OmpAllocate, + OmpAllocateDirective, OmpDeclareSimd, OmpDeclareTarget, OmpThreadprivate, + OmpDeclareReduction, OmpFlushed, OmpCriticalLock, OmpIfSpecified, OmpNone, + OmpPreDetermined); using Flags = common::EnumSet; const Scope &owner() const { return *owner_; } diff --git a/flang/lib/Parser/type-parsers.h b/flang/lib/Parser/type-parsers.h --- a/flang/lib/Parser/type-parsers.h +++ b/flang/lib/Parser/type-parsers.h @@ -92,7 +92,6 @@ constexpr Parser expr; // R1022 constexpr Parser specificationExpr; // R1028 constexpr Parser assignmentStmt; // R1032 -constexpr Parser allocateStmt; constexpr Parser pointerAssignmentStmt; // R1033 constexpr Parser whereStmt; // R1041, R1045, R1046 constexpr Parser whereConstruct; // R1042 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 @@ -143,7 +143,6 @@ void Enter(const parser::OmpClause::NumTasks &); void Enter(const parser::OmpClause::NumTeams &); void Enter(const parser::OmpClause::NumThreads &); - void Enter(const parser::OmpClause::Allocator &); void Enter(const parser::OmpClause::Ordered &); void Enter(const parser::OmpClause::Priority &); void Enter(const parser::OmpClause::Private &); @@ -176,6 +175,9 @@ void Enter(const parser::OmpReductionClause &); void Enter(const parser::OmpScheduleClause &); + void CheckObjectListStructure( + const parser::CharBlock &source, const parser::OmpObjectList &objList); + private: bool HasInvalidWorksharingNesting( const parser::CharBlock &, const OmpDirectiveSet &); 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 @@ -181,13 +181,38 @@ void OmpStructureChecker::Enter(const parser::OpenMPDeclarativeAllocate &x) { const auto &dir{std::get(x.t)}; + const auto &objectList{std::get(x.t)}; PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_allocate); + CheckObjectListStructure(dir.source, objectList); } -void OmpStructureChecker::Leave(const parser::OpenMPDeclarativeAllocate &) { +void OmpStructureChecker::Leave(const parser::OpenMPDeclarativeAllocate &x) { dirContext_.pop_back(); } +void OmpStructureChecker::CheckObjectListStructure( + const parser::CharBlock &source, const parser::OmpObjectList &objList) { + + for (const auto &ompObject : objList.v) { + std::visit( + common::visitors{ + [&](const parser::Designator &designator) { + if (std::get_if(&designator.u)) { + if ((parser::Unwrap(ompObject)) || + (parser::Unwrap(ompObject))) { + context_.Say(source, + "A variable that is part of another variable (as an " + "array or structure element)" + " cannot appear in an ALLOCATE directive"_err_en_US); + } + } + }, + [&](const parser::Name &name) {}, + }, + ompObject.u); + } +} + void OmpStructureChecker::Enter(const parser::OpenMPDeclareTargetConstruct &x) { const auto &dir{std::get(x.t)}; PushContext(dir.source, llvm::omp::Directive::OMPD_declare_target); @@ -428,47 +453,6 @@ } } } -void OmpStructureChecker::Enter(const parser::OmpClause::Priority &x) { - CheckAllowed(llvm::omp::Clause::OMPC_priority); - RequiresPositiveParameter(llvm::omp::Clause::OMPC_priority, x.v); -} -void OmpStructureChecker::Enter(const parser::OmpClause::Private &) { - CheckAllowed(llvm::omp::Clause::OMPC_private); -} -void OmpStructureChecker::Enter(const parser::OmpClause::Allocator &x) { - CheckAllowed(llvm::omp::Clause::OMPC_allocator); - RequiresPositiveParameter(llvm::omp::Clause::OMPC_allocator, x.v); -} -void OmpStructureChecker::Enter(const parser::OmpClause::Safelen &x) { - CheckAllowed(llvm::omp::Clause::OMPC_safelen); - RequiresConstantPositiveParameter(llvm::omp::Clause::OMPC_safelen, x.v); -} -void OmpStructureChecker::Enter(const parser::OmpClause::Shared &) { - CheckAllowed(llvm::omp::Clause::OMPC_shared); -} -void OmpStructureChecker::Enter(const parser::OmpClause::Simdlen &x) { - CheckAllowed(llvm::omp::Clause::OMPC_simdlen); - RequiresConstantPositiveParameter(llvm::omp::Clause::OMPC_simdlen, x.v); -} -void OmpStructureChecker::Enter(const parser::OmpClause::ThreadLimit &x) { - CheckAllowed(llvm::omp::Clause::OMPC_thread_limit); - RequiresPositiveParameter(llvm::omp::Clause::OMPC_thread_limit, x.v); -} -void OmpStructureChecker::Enter(const parser::OmpClause::To &) { - CheckAllowed(llvm::omp::Clause::OMPC_to); -} -void OmpStructureChecker::Enter(const parser::OmpClause::Link &) { - CheckAllowed(llvm::omp::Clause::OMPC_link); -} -void OmpStructureChecker::Enter(const parser::OmpClause::Uniform &) { - CheckAllowed(llvm::omp::Clause::OMPC_uniform); -} -void OmpStructureChecker::Enter(const parser::OmpClause::UseDevicePtr &) { - CheckAllowed(llvm::omp::Clause::OMPC_use_device_ptr); -} -void OmpStructureChecker::Enter(const parser::OmpClause::IsDevicePtr &) { - CheckAllowed(llvm::omp::Clause::OMPC_is_device_ptr); -} void OmpStructureChecker::Enter(const parser::OmpClause::Shared &x) { CheckAllowed(llvm::omp::Clause::OMPC_shared); diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp --- a/flang/lib/Semantics/resolve-directives.cpp +++ b/flang/lib/Semantics/resolve-directives.cpp @@ -116,7 +116,7 @@ bool Pre(const parser::SpecificationPart &x) { Walk(std::get>(x.t)); - return false; + return true; } bool Pre(const parser::OpenACCBlockConstruct &); @@ -259,12 +259,22 @@ bool Pre(const parser::OpenMPThreadprivate &); void Post(const parser::OpenMPThreadprivate &) { PopContext(); } + bool Pre(const parser::OpenMPDeclarativeAllocate &); + void Post(const parser::OpenMPDeclarativeAllocate &) { PopContext(); } + + bool Pre(const parser::OpenMPExecutableAllocate &); + void Post(const parser::OpenMPExecutableAllocate &); + // 2.15.3 Data-Sharing Attribute Clauses void Post(const parser::OmpDefaultClause &); bool Pre(const parser::OmpClause::Shared &x) { ResolveOmpObjectList(x.v, Symbol::Flag::OmpShared); return false; } + bool Pre(const parser::OmpClause::Allocator &x) { + hasAllocator = true; + return false; + } bool Pre(const parser::OmpClause::Private &x) { ResolveOmpObjectList(x.v, Symbol::Flag::OmpPrivate); return false; @@ -340,6 +350,8 @@ std::vector allocateNames_; // on one directive SymbolSet privateDataSharingAttributeObjects_; // on one directive + bool inTarget = false; + bool hasAllocator = false; void AddAllocateName(const parser::Name *&object) { allocateNames_.push_back(object); @@ -366,6 +378,7 @@ void ResolveOmpNameList(const std::list &, Symbol::Flag); void ResolveOmpName(const parser::Name &, Symbol::Flag); Symbol *ResolveName(const parser::Name *); + Symbol *ResolveOmpObjectScope(const parser::Name *); Symbol *DeclareOrMarkOtherAccessEntity(const parser::Name &, Symbol::Flag); Symbol *DeclareOrMarkOtherAccessEntity(Symbol &, Symbol::Flag); void CheckMultipleAppearances( @@ -750,14 +763,19 @@ } bool OmpAttributeVisitor::Pre(const parser::OpenMPBlockConstruct &x) { + inTarget = false; const auto &beginBlockDir{std::get(x.t)}; const auto &beginDir{std::get(beginBlockDir.t)}; switch (beginDir.v) { + case llvm::omp::Directive::OMPD_target: { + PushContext(beginDir.source, beginDir.v); + inTarget = true; + break; + } case llvm::omp::Directive::OMPD_master: case llvm::omp::Directive::OMPD_ordered: case llvm::omp::Directive::OMPD_parallel: case llvm::omp::Directive::OMPD_single: - case llvm::omp::Directive::OMPD_target: case llvm::omp::Directive::OMPD_target_data: case llvm::omp::Directive::OMPD_task: case llvm::omp::Directive::OMPD_teams: @@ -1000,6 +1018,22 @@ return true; } +bool OmpAttributeVisitor::Pre(const parser::OpenMPDeclarativeAllocate &x) { + PushContext(x.source, llvm::omp::Directive::OMPD_allocate); + const auto &list{std::get(x.t)}; + ResolveOmpObjectList(list, Symbol::Flag::OmpAllocateDirective); + return false; +} + +bool OmpAttributeVisitor::Pre(const parser::OpenMPExecutableAllocate &x) { + hasAllocator = false; + PushContext(x.source, llvm::omp::Directive::OMPD_allocate); + const auto &list{std::get>(x.t)}; + if (list) + ResolveOmpObjectList(*list, Symbol::Flag::OmpAllocateDirective); + return true; +} + void OmpAttributeVisitor::Post(const parser::OmpDefaultClause &x) { if (!dirContext_.empty()) { switch (x.v) { @@ -1019,6 +1053,17 @@ } } +void OmpAttributeVisitor::Post(const parser::OpenMPExecutableAllocate &x) { + if (inTarget && !hasAllocator) + // TODO: expand this check to exclude the case when a requires + // directive with the dynamic_allocators clause is present + // in the same compilation unit (OMP5.0 2.11.3). + context_.Say(x.source, + "ALLOCATE directives that appear in a TARGET region " + "must specify an allocator clause"_err_en_US); + PopContext(); +} + // For OpenMP constructs, check all the data-refs within the constructs // and adjust the symbol for each Name if necessary void OmpAttributeVisitor::Post(const parser::Name &name) { @@ -1088,6 +1133,22 @@ return nullptr; } +Symbol *OmpAttributeVisitor::ResolveOmpObjectScope(const parser::Name *name) { + + if (auto *prev{name ? GetContext().scope.parent().FindSymbol(name->source) + : nullptr}) { + name->symbol = prev; + return nullptr; + } + + if (auto *ompSymbol{ + name ? GetContext().scope.FindSymbol(name->source) : nullptr}) { + name->symbol = ompSymbol; + return ompSymbol; + } + return nullptr; +} + void OmpAttributeVisitor::ResolveOmpObjectList( const parser::OmpObjectList &ompObjectList, Symbol::Flag ompFlag) { for (const auto &ompObject : ompObjectList.v) { @@ -1117,6 +1178,12 @@ AddAllocateName(name); } } + if (ResolveOmpObjectScope(name) == nullptr && + ompFlag == Symbol::Flag::OmpAllocateDirective) { + context_.Say(designator.source, // 2.15.3 + "List items must be declared in the same scoping unit " + "in which the ALLOCATE directive appears"_err_en_US); + } } } else { // Array sections to be changed to substrings as needed diff --git a/flang/test/Semantics/omp-allocate01.f90 b/flang/test/Semantics/omp-allocate01.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/omp-allocate01.f90 @@ -0,0 +1,24 @@ +! RUN: %S/test_errors.sh %s %t %f18 -fopenmp +! OpenMP Version 5.0 +! 2.11.3 allocate Directive +! The allocate directive must appear in the same scope as the declarations of +! each of its list items and must follow all such declarations. + +subroutine allocate() +use omp_lib + integer :: x + contains + subroutine sema() + integer :: a, b + real, dimension (:,:), allocatable :: darray + + !ERROR: List items must be declared in the same scoping unit in which the ALLOCATE directive appears + !$omp allocate(x) + print *, a + + !ERROR: List items must be declared in the same scoping unit in which the ALLOCATE directive appears + !$omp allocate(x) allocator(omp_default_mem_alloc) + allocate ( darray(a, b) ) + end subroutine sema + +end subroutine allocate \ No newline at end of file diff --git a/flang/test/Semantics/omp-allocate02.f90 b/flang/test/Semantics/omp-allocate02.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/omp-allocate02.f90 @@ -0,0 +1,24 @@ +! RUN: %S/test_errors.sh %s %t %f18 -fopenmp +! OpenMP Version 5.0 +! 2.11.3 allocate Directive +! At most one allocator clause can appear on the allocate directive. + +subroutine allocate() +use omp_lib + integer :: x, y + integer :: a, b + real, dimension (:,:), allocatable :: darray + + !$omp allocate(x, y) allocator(omp_default_mem_alloc) + + !ERROR: At most one ALLOCATOR clause can appear on the ALLOCATE directive + !$omp allocate(x, y) allocator(omp_default_mem_alloc) allocator(omp_default_mem_alloc) + + !$omp allocate(x) allocator(omp_default_mem_alloc) + allocate ( darray(a, b) ) + + !ERROR: At most one ALLOCATOR clause can appear on the ALLOCATE directive + !$omp allocate(x) allocator(omp_default_mem_alloc) allocator(omp_default_mem_alloc) + allocate ( darray(a, b) ) + +end subroutine allocate \ No newline at end of file diff --git a/flang/test/Semantics/omp-allocate03.f90 b/flang/test/Semantics/omp-allocate03.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/omp-allocate03.f90 @@ -0,0 +1,17 @@ +! RUN: %S/test_errors.sh %s %t %f18 -fopenmp +! OpenMP Version 5.0 +! 2.11.3 allocate Directive +! A variable that is part of another variable (as an array or +! structure element) cannot appear in an allocate directive. +subroutine allocate() +use omp_lib + + type my_type + integer :: array(10) + end type my_type + type(my_type) :: my_var + + !ERROR: A variable that is part of another variable (as an array or structure element) cannot appear in an ALLOCATE directive + !$omp allocate(my_var%array) + +end subroutine allocate \ No newline at end of file diff --git a/flang/test/Semantics/omp-allocate04.f90 b/flang/test/Semantics/omp-allocate04.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/omp-allocate04.f90 @@ -0,0 +1,14 @@ +! RUN: %S/test_errors.sh %s %t %f18 -fopenmp +! OpenMP Version 5.0 +! 2.11.3 allocate Directive +! Only the allocator clause is allowed on the allocate directive +subroutine allocate() +use omp_lib + + integer :: x, y + + !$omp allocate(x) allocator(omp_default_mem_alloc) + + !ERROR: PRIVATE clause is not allowed on the ALLOCATE directive + !$omp allocate(y) private(y) +end subroutine allocate \ No newline at end of file diff --git a/flang/test/Semantics/omp-allocate05.f90 b/flang/test/Semantics/omp-allocate05.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/omp-allocate05.f90 @@ -0,0 +1,24 @@ +! RUN: %S/test_errors.sh %s %t %f18 -fopenmp +! OpenMP Version 5.0 +! 2.11.3 allocate Directive +! allocate directives that appear in a target region must specify an allocator +! clause unless a requires directive with the dynamic_allocators clause is present +! in the same compilation unit. + +subroutine allocate() +use omp_lib + integer :: a, b + real, dimension (:,:), allocatable :: darray + + !$omp target + !$omp allocate allocator(omp_default_mem_alloc) + allocate ( darray(a, b) ) + !$omp end target + + !$omp target + !ERROR: ALLOCATE directives that appear in a TARGET region must specify an allocator clause + !$omp allocate + allocate ( darray(a, b) ) + !$omp end target + +end subroutine allocate \ No newline at end of file diff --git a/llvm/include/llvm/Frontend/OpenMP/OMP.td b/llvm/include/llvm/Frontend/OpenMP/OMP.td --- a/llvm/include/llvm/Frontend/OpenMP/OMP.td +++ b/llvm/include/llvm/Frontend/OpenMP/OMP.td @@ -1464,7 +1464,7 @@ ]; } def OMP_Allocate : Directive<"allocate"> { - let allowedClauses = [ + let allowedOnceClauses = [ VersionedClause ]; }