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 @@ -14,6 +14,8 @@ #include "flang/Common/enum-set.h" #include "flang/Common/reference.h" #include "flang/Common/visit.h" +#include "flang/Parser/parse-tree.h" +#include "llvm/ADT/BitmaskEnum.h" #include "llvm/ADT/DenseMapInfo.h" #include @@ -45,8 +47,47 @@ using MutableSymbolRef = common::Reference; using MutableSymbolVector = std::vector; +namespace { +LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); +// Flags representing OpenMP requires clauses. +enum OmpRequiresFlags { + None = 0x00, + ReverseOffload = 0x01, + UnifiedAddress = 0x02, + UnifiedSharedMemory = 0x04, + DynamicAllocators = 0x08, + LLVM_MARK_AS_BITMASK_ENUM(DynamicAllocators) +}; +} // namespace + +// Mixin for details with OpenMP declarative constructs. +class WithOmpDeclarative { + using OmpAtomicOrderType = parser::OmpAtomicDefaultMemOrderClause::Type; + +public: + bool has_ompRequires() const { return ompRequires_.has_value(); } + const OmpRequiresFlags *ompRequires() const { + return ompRequires_ ? &*ompRequires_ : nullptr; + } + void set_ompRequires(OmpRequiresFlags flags) { ompRequires_ = flags; } + + bool has_ompAtomicDefaultMemOrder() const { + return ompAtomicDefaultMemOrder_.has_value(); + } + const OmpAtomicOrderType *ompAtomicDefaultMemOrder() const { + return ompAtomicDefaultMemOrder_ ? &*ompAtomicDefaultMemOrder_ : nullptr; + } + void set_ompAtomicDefaultMemOrder(OmpAtomicOrderType flags) { + ompAtomicDefaultMemOrder_ = flags; + } + +private: + std::optional ompRequires_; + std::optional ompAtomicDefaultMemOrder_; +}; + // A module or submodule. -class ModuleDetails { +class ModuleDetails : public WithOmpDeclarative { public: ModuleDetails(bool isSubmodule = false) : isSubmodule_{isSubmodule} {} bool isSubmodule() const { return isSubmodule_; } @@ -63,7 +104,7 @@ const Scope *scope_{nullptr}; }; -class MainProgramDetails { +class MainProgramDetails : public WithOmpDeclarative { public: private: }; @@ -85,7 +126,7 @@ // A subroutine or function definition, or a subprogram interface defined // in an INTERFACE block as part of the definition of a dummy procedure // or a procedure pointer (with just POINTER). -class SubprogramDetails : public WithBindName { +class SubprogramDetails : public WithBindName, public WithOmpDeclarative { public: bool isFunction() const { return result_ != nullptr; } bool isInterface() const { return isInterface_; } 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 @@ -608,7 +608,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/canonicalize-omp.cpp b/flang/lib/Semantics/canonicalize-omp.cpp --- a/flang/lib/Semantics/canonicalize-omp.cpp +++ b/flang/lib/Semantics/canonicalize-omp.cpp @@ -17,7 +17,10 @@ // after this pass. // 2. Associate declarative OMP allocation directives with their // respective executable allocation directive -// 3. TBD +// 3. set the default memory order, defined by any 'requires' directive +// present in the compilation unit, as explicit memory order of all atomic +// operations that don't already have it specified. +// 4. TBD namespace Fortran::semantics { using namespace parser::literals; @@ -50,6 +53,26 @@ void Post(parser::ExecutionPart &body) { RewriteOmpAllocations(body); } + // Assume at this point that any such clauses appear only on 'requires' + // directives that are visited before any atomic operations, and that all + // instances (if present) specify the same ordering. Other semantic checks + // later ensure that these restrictions are met. + void Post(parser::OmpClause::AtomicDefaultMemOrder &x) { + switch (x.v.v) { + case parser::OmpAtomicDefaultMemOrderClause::Type::AcqRel: + atomicDefaultMemOrder_ = llvm::omp::Clause::OMPC_acq_rel; + break; + case parser::OmpAtomicDefaultMemOrderClause::Type::Relaxed: + atomicDefaultMemOrder_ = llvm::omp::Clause::OMPC_relaxed; + break; + case parser::OmpAtomicDefaultMemOrderClause::Type::SeqCst: + atomicDefaultMemOrder_ = llvm::omp::Clause::OMPC_seq_cst; + break; + } + } + + void Post(parser::OpenMPAtomicConstruct &atomic) { RewriteOmpAtomic(atomic); } + private: template T *GetConstructIf(parser::ExecutionPartConstruct &x) { if (auto *y{std::get_if(&x.u)}) { @@ -149,7 +172,68 @@ } } + void RewriteOmpAtomic(parser::OpenMPAtomicConstruct &atomic) { + // Rewrite atomic constructs to add an explicit memory ordering to all + // that do not specify it, honoring in this way the + // `atomic_default_mem_order` clause of the 'requires' directive. + if (!atomicDefaultMemOrder_) { + return; + } + + auto findMemOrderClause = + [](const std::list &clauses) { + return std::find_if( + clauses.begin(), clauses.end(), [](const auto &clause) { + return std::get_if( + &clause.u); + }) != clauses.end(); + }; + + // Get the clause list to which the new memory order clause must be added, + // only if there are no other memory order clauses present for this atomic + // directive. + auto *clauseList = common::visit( + common::visitors{[&](parser::OmpAtomic &atomicConstruct) { + // OmpAtomic only has a single list of clauses. + auto &clauses{std::get( + atomicConstruct.t)}; + return !findMemOrderClause(clauses.v) ? &clauses.v + : nullptr; + }, + [&](auto &atomicConstruct) { + // All other atomic constructs have two lists of clauses. + auto &clausesLhs{std::get<0>(atomicConstruct.t)}; + auto &clausesRhs{std::get<2>(atomicConstruct.t)}; + return !findMemOrderClause(clausesLhs.v) && + !findMemOrderClause(clausesRhs.v) + ? &clausesRhs.v + : nullptr; + }}, + atomic.u); + + if (clauseList) { + switch (*atomicDefaultMemOrder_) { + case llvm::omp::Clause::OMPC_acq_rel: + clauseList->push_back(parser::OmpMemoryOrderClause( + parser::OmpClause(parser::OmpClause::AcqRel()))); + break; + case llvm::omp::Clause::OMPC_relaxed: + clauseList->push_back(parser::OmpMemoryOrderClause( + parser::OmpClause(parser::OmpClause::Relaxed()))); + break; + case llvm::omp::Clause::OMPC_seq_cst: + clauseList->push_back(parser::OmpMemoryOrderClause( + parser::OmpClause(parser::OmpClause::SeqCst()))); + break; + default: + llvm_unreachable("Unexpected clause used as memory ordering"); + break; + } + } + } + parser::Messages &messages_; + std::optional atomicDefaultMemOrder_; }; bool CanonicalizeOmp(parser::Messages &messages, parser::Program &program) { 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 @@ -182,6 +182,8 @@ void Enter(const parser::OmpAtomicCapture &); void Leave(const parser::OmpAtomic &); + void Enter(const parser::UseStmt &); + #define GEN_FLANG_CLAUSE_CHECK_ENTER #include "llvm/Frontend/OpenMP/OMP.inc" @@ -268,6 +270,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]--; } @@ -281,6 +284,9 @@ LastType }; int directiveNest_[LastType + 1] = {0}; + + bool deviceConstructFound_{false}; + bool atomicDirectiveDefaultOrderFound_{false}; }; } // 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); } @@ -1155,21 +1156,40 @@ const auto &dir{std::get(x.t)}; const auto &spec{std::get(x.t)}; if (const auto *objectList{parser::Unwrap(spec.u)}) { + deviceConstructFound_ = true; CheckSymbolNames(dir.source, *objectList); 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)}) { - CheckSymbolNames(dir.source, toClause->v); - CheckIsVarPartOfAnotherVar(dir.source, toClause->v); - CheckThreadprivateOrDeclareTargetVar(toClause->v); - } else if (const auto *linkClause{ - std::get_if(&clause.u)}) { - CheckSymbolNames(dir.source, linkClause->v); - CheckIsVarPartOfAnotherVar(dir.source, linkClause->v); - CheckThreadprivateOrDeclareTargetVar(linkClause->v); + common::visit( + common::visitors{ + [&](const parser::OmpClause::To &toClause) { + toClauseFound = true; + CheckSymbolNames(dir.source, toClause.v); + CheckIsVarPartOfAnotherVar(dir.source, toClause.v); + CheckThreadprivateOrDeclareTargetVar(toClause.v); + }, + [&](const parser::OmpClause::Link &linkClause) { + CheckSymbolNames(dir.source, linkClause.v); + 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; } } } @@ -1671,6 +1691,9 @@ if (rightHandClauseList) { checkForValidMemoryOrderClause(rightHandClauseList); } + if (numMemoryOrderClause == 0) { + atomicDirectiveDefaultOrderFound_ = true; + } } void OmpStructureChecker::Enter(const parser::OpenMPAtomicConstruct &x) { @@ -1867,7 +1890,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) @@ -1877,7 +1899,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) @@ -1890,7 +1911,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) @@ -1911,8 +1931,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) @@ -2836,4 +2854,78 @@ clause.u); } +void OmpStructureChecker::Enter( + const parser::OmpClause::AtomicDefaultMemOrder &x) { + CheckAllowed(llvm::omp::Clause::OMPC_atomic_default_mem_order); + + // 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())); + } +} + +void OmpStructureChecker::Enter(const parser::UseStmt &x) { + semantics::Symbol *symbol{x.moduleName.symbol}; + if (!symbol) { + // Cannot check used module if it wasn't resolved. + return; + } + + auto &details = std::get(symbol->details()); + if (details.has_ompRequires() && deviceConstructFound_) { + context_.Say(x.moduleName.source, + "'%s' module containing device-related REQUIRES directive imported " + "lexically after device construct"_err_en_US, + x.moduleName.ToString()); + } + + if (details.has_ompAtomicDefaultMemOrder() && + atomicDirectiveDefaultOrderFound_) { + context_.Say(x.moduleName.source, + "'%s' module containing '%s' REQUIRES clause imported lexically after " + "atomic operation with the '%s' clause not defined"_err_en_US, + x.moduleName.ToString(), + parser::ToUpperCaseLetters( + getClauseName(llvmOmpClause::OMPC_atomic_default_mem_order).str()), + parser::ToUpperCaseLetters( + getClauseName(llvmOmpClause::OMPC_memory_order).str())); + } +} + } // namespace Fortran::semantics diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp --- a/flang/lib/Semantics/mod-file.cpp +++ b/flang/lib/Semantics/mod-file.cpp @@ -57,6 +57,8 @@ static llvm::raw_ostream &PutAttr(llvm::raw_ostream &, Attr); static llvm::raw_ostream &PutType(llvm::raw_ostream &, const DeclTypeSpec &); static llvm::raw_ostream &PutLower(llvm::raw_ostream &, std::string_view); +static llvm::raw_ostream &PutOmpRequires( + llvm::raw_ostream &, const WithOmpDeclarative &); static std::error_code WriteFile( const std::string &, const std::string &, bool = true); static bool FileContentsMatch( @@ -162,6 +164,7 @@ uses_.str().clear(); all << useExtraAttrs_.str(); useExtraAttrs_.str().clear(); + PutOmpRequires(all, details); all << decls_.str(); decls_.str().clear(); auto str{contains_.str()}; @@ -505,6 +508,8 @@ } } os << '\n'; + // print OpenMP requires + PutOmpRequires(os, details); // walk symbols, collect ones needed for interface const Scope &scope{ details.entryScope() ? *details.entryScope() : DEREF(symbol.scope())}; @@ -874,6 +879,44 @@ return os; } +llvm::raw_ostream &PutOmpRequires( + llvm::raw_ostream &os, const WithOmpDeclarative &details) { + if (details.has_ompRequires() || details.has_ompAtomicDefaultMemOrder()) { + os << "!$omp requires"; + if (auto *flags{details.ompRequires()}) { + if (*flags & OmpRequiresFlags::ReverseOffload) { + os << " reverse_offload"; + } + if (*flags & OmpRequiresFlags::UnifiedAddress) { + os << " unified_address"; + } + if (*flags & OmpRequiresFlags::UnifiedSharedMemory) { + os << " unified_shared_memory"; + } + if (*flags & OmpRequiresFlags::DynamicAllocators) { + os << " dynamic_allocators"; + } + } + if (auto *memOrder{details.ompAtomicDefaultMemOrder()}) { + os << " atomic_default_mem_order("; + switch (*memOrder) { + case parser::OmpAtomicDefaultMemOrderClause::Type::SeqCst: + os << "seq_cst"; + break; + case parser::OmpAtomicDefaultMemOrderClause::Type::AcqRel: + os << "acq_rel"; + break; + case parser::OmpAtomicDefaultMemOrderClause::Type::Relaxed: + os << "relaxed"; + break; + } + os << ')'; + } + os << '\n'; + } + return os; +} + struct Temp { Temp(int fd, std::string path) : fd{fd}, path{path} {} Temp(Temp &&t) : fd{std::exchange(t.fd, -1)}, path{std::move(t.path)} {} 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 @@ -57,6 +57,12 @@ dirContext_.emplace_back(source, dir, context_.FindScope(source)); } void PopContext() { dirContext_.pop_back(); } + Symbol *GetDeclContext() { + CHECK(!declContext_.empty()); + return declContext_.back(); + } + void PushDeclContext(Symbol *symbol) { declContext_.push_back(symbol); } + void PopDeclContext() { declContext_.pop_back(); } void SetContextDirectiveSource(parser::CharBlock &dir) { GetContext().directiveSource = dir; } @@ -108,6 +114,7 @@ UnorderedSymbolSet dataSharingAttributeObjects_; // on one directive SemanticsContext &context_; std::vector dirContext_; // used as a stack + std::vector declContext_; // used as a stack }; class AccAttributeVisitor : DirectiveAttributeVisitor { @@ -274,11 +281,6 @@ return true; } - bool Pre(const parser::SpecificationPart &x) { - Walk(std::get>(x.t)); - return true; - } - bool Pre(const parser::StmtFunctionStmt &x) { const auto &parsedExpr{std::get>(x.t)}; if (const auto *expr{GetExpr(context_, parsedExpr)}) { @@ -326,6 +328,39 @@ bool Pre(const parser::OpenMPRequiresConstruct &x) { PushContext(x.source, llvm::omp::Directive::OMPD_requires); + + // Gather information from the clauses. + OmpRequiresFlags flags{OmpRequiresFlags::None}; + std::optional memOrder; + for (auto &clause : std::get(x.t).v) { + flags |= common::visit( + common::visitors{ + [&memOrder]( + const parser::OmpClause::AtomicDefaultMemOrder &atomic) { + memOrder = atomic.v.v; + return OmpRequiresFlags::None; + }, + [](const parser::OmpClause::ReverseOffload &) { + return OmpRequiresFlags::ReverseOffload; + }, + [](const parser::OmpClause::UnifiedAddress &) { + return OmpRequiresFlags::UnifiedAddress; + }, + [](const parser::OmpClause::UnifiedSharedMemory &) { + return OmpRequiresFlags::UnifiedSharedMemory; + }, + [](const parser::OmpClause::DynamicAllocators &) { + return OmpRequiresFlags::DynamicAllocators; + }, + [&](const auto &) { + context_.Say(x.source, + "Unexpected clause in REQUIRES construct"_err_en_US); + return OmpRequiresFlags::None; + }}, + clause.u); + } + // Merge clauses into parents' symbols details. + AddOmpRequiresToSymbol(currScope().symbol(), flags, memOrder); return true; } void Post(const parser::OpenMPRequiresConstruct &) { PopContext(); } @@ -333,6 +368,33 @@ bool Pre(const parser::OpenMPDeclareTargetConstruct &); void Post(const parser::OpenMPDeclareTargetConstruct &) { PopContext(); } + void Post(const parser::UseStmt &x) { + Symbol *symbol{x.moduleName.symbol}; + if (!symbol) { + return; + } + + // Gather information from the imported module's symbol details. + OmpRequiresFlags flags{OmpRequiresFlags::None}; + std::optional memOrder; + common::visit( + [&](auto &details) { + if constexpr (std::is_base_of_v>) { + if (details.has_ompRequires()) { + flags = *details.ompRequires(); + } + if (details.has_ompAtomicDefaultMemOrder()) { + memOrder = *details.ompAtomicDefaultMemOrder(); + } + } + }, + symbol->details()); + + // Merge requires clauses into `use` statement parents. + AddOmpRequiresToSymbol(GetDeclContext(), flags, memOrder); + } + bool Pre(const parser::OpenMPThreadprivate &); void Post(const parser::OpenMPThreadprivate &) { PopContext(); } @@ -451,6 +513,70 @@ void Post(const parser::Name &); + // Keep track of contexts inside of which `SpecificationPart`s can be found + // to allow matching `Use` statements with their parent scopes. + bool Pre(const parser::MainProgram &x) { + const auto &end{std::get>(x.t)}; + PushDeclContext(context_.FindScope(end.source).symbol()); + return true; + } + bool Pre(const parser::Module &x) { + const auto &name{std::get>(x.t)}; + PushDeclContext(name.statement.v.symbol); + return true; + } + bool Pre(const parser::Submodule &x) { + const auto &name{std::get>(x.t)}; + PushDeclContext(std::get(name.statement.t).symbol); + return true; + } + bool Pre(const parser::FunctionSubprogram &x) { + const auto &name{std::get>(x.t)}; + PushDeclContext(std::get(name.statement.t).symbol); + return true; + } + bool Pre(const parser::InterfaceBody::Function &x) { + const auto &name{std::get>(x.t)}; + PushDeclContext(std::get(name.statement.t).symbol); + return true; + } + bool Pre(const parser::SubroutineSubprogram &x) { + const auto &name{std::get>(x.t)}; + PushDeclContext(std::get(name.statement.t).symbol); + return true; + } + bool Pre(const parser::InterfaceBody::Subroutine &x) { + const auto &name{std::get>(x.t)}; + PushDeclContext(std::get(name.statement.t).symbol); + return true; + } + bool Pre(const parser::SeparateModuleSubprogram &x) { + const auto &name{ + std::get>(x.t)}; + PushDeclContext(name.statement.v.symbol); + return true; + } + bool Pre(const parser::BlockConstruct &x) { + const auto &end{std::get>(x.t)}; + PushDeclContext(context_.FindScope(end.source).symbol()); + return true; + } + bool Pre(const parser::BlockData &x) { + const auto &end{std::get>(x.t)}; + PushDeclContext(context_.FindScope(end.source).symbol()); + return true; + } + void Post(const parser::MainProgram &) { PopDeclContext(); } + void Post(const parser::Module &) { PopDeclContext(); } + void Post(const parser::Submodule &) { PopDeclContext(); } + void Post(const parser::FunctionSubprogram &) { PopDeclContext(); } + void Post(const parser::InterfaceBody::Function &) { PopDeclContext(); } + void Post(const parser::SubroutineSubprogram &) { PopDeclContext(); } + void Post(const parser::InterfaceBody::Subroutine &) { PopDeclContext(); } + void Post(const parser::SeparateModuleSubprogram &) { PopDeclContext(); } + void Post(const parser::BlockConstruct &) { PopDeclContext(); } + void Post(const parser::BlockData &) { PopDeclContext(); } + // Keep track of labels in the statements that causes jumps to target labels void Post(const parser::GotoStmt &gotoStmt) { CheckSourceLabel(gotoStmt.v); } void Post(const parser::ComputedGotoStmt &computedGotoStmt) { @@ -586,6 +712,9 @@ bool HasSymbolInEnclosingScope(const Symbol &, Scope &); std::int64_t ordCollapseLevel{0}; + + void AddOmpRequiresToSymbol(Symbol *symbol, OmpRequiresFlags flags, + std::optional memOrder); }; template @@ -1987,4 +2116,39 @@ return llvm::is_contained(symbols, symbol); } +void OmpAttributeVisitor::AddOmpRequiresToSymbol(Symbol *symbol, + OmpRequiresFlags flags, + std::optional memOrder) { + while (symbol) { + common::visit( + [&](auto &details) { + // Store clauses information into the symbol for the parent and + // enclosing modules, programs, functions and subroutines. + if constexpr (std::is_convertible_v) { + if (flags != OmpRequiresFlags::None) { + if (const OmpRequiresFlags * otherFlags{details.ompRequires()}) { + flags |= *otherFlags; + } + details.set_ompRequires(flags); + } + if (memOrder) { + if (details.has_ompAtomicDefaultMemOrder() && + *details.ompAtomicDefaultMemOrder() != *memOrder) { + context_.Say(symbol->scope()->sourceRange(), + "Conflicting '%s' REQUIRES clauses found in compilation " + "unit"_err_en_US, + parser::ToUpperCaseLetters(llvm::omp::getOpenMPClauseName( + llvm::omp::Clause::OMPC_atomic_default_mem_order) + .str())); + } + details.set_ompAtomicDefaultMemOrder(*memOrder); + } + } + }, + symbol->details()); + symbol = symbol->scope()->parent().symbol(); + } +} + } // namespace Fortran::semantics diff --git a/flang/test/Semantics/Inputs/requires_module.f90 b/flang/test/Semantics/Inputs/requires_module.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/Inputs/requires_module.f90 @@ -0,0 +1,3 @@ +module requires_module + !$omp requires atomic_default_mem_order(seq_cst) +end module diff --git a/flang/test/Semantics/OpenMP/requires-rewrite.f90 b/flang/test/Semantics/OpenMP/requires-rewrite.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/OpenMP/requires-rewrite.f90 @@ -0,0 +1,76 @@ +! RUN: %flang_fc1 -fopenmp -fdebug-dump-parse-tree %s 2>&1 | FileCheck %s +! Ensure that requires atomic_default_mem_order is used to update atomic +! operations with no explicit memory order set. +program requires + !$omp requires atomic_default_mem_order(seq_cst) + implicit none + integer :: i, j, k + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomic + ! CHECK-NOT: OmpMemoryOrderClause -> OmpClause -> SeqCst + ! CHECK: OmpMemoryOrderClause -> OmpClause -> Relaxed + !$omp atomic relaxed + i = j + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomic + ! CHECK: OmpMemoryOrderClause -> OmpClause -> SeqCst + !$omp atomic + i = j + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomicUpdate + ! CHECK-NOT: OmpMemoryOrderClause -> OmpClause -> SeqCst + ! CHECK: OmpMemoryOrderClause -> OmpClause -> Relaxed + !$omp atomic relaxed update + i = j + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomicUpdate + ! CHECK-NOT: OmpMemoryOrderClause -> OmpClause -> SeqCst + ! CHECK: OmpMemoryOrderClause -> OmpClause -> Relaxed + !$omp atomic update relaxed + i = j + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomicUpdate + ! CHECK: OmpMemoryOrderClause -> OmpClause -> SeqCst + !$omp atomic update + i = j + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomicCapture + ! CHECK-NOT: OmpMemoryOrderClause -> OmpClause -> SeqCst + ! CHECK: OmpMemoryOrderClause -> OmpClause -> Relaxed + !$omp atomic relaxed capture + i = j + j = k + !$omp end atomic + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomicCapture + ! CHECK-NOT: OmpMemoryOrderClause -> OmpClause -> SeqCst + ! CHECK: OmpMemoryOrderClause -> OmpClause -> Relaxed + !$omp atomic capture relaxed + i = j + j = k + !$omp end atomic + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomicCapture + ! CHECK: OmpMemoryOrderClause -> OmpClause -> SeqCst + !$omp atomic capture + i = j + j = k + !$omp end atomic + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomicWrite + ! CHECK-NOT: OmpMemoryOrderClause -> OmpClause -> SeqCst + ! CHECK: OmpMemoryOrderClause -> OmpClause -> Relaxed + !$omp atomic relaxed write + i = j + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomicWrite + ! CHECK-NOT: OmpMemoryOrderClause -> OmpClause -> SeqCst + ! CHECK: OmpMemoryOrderClause -> OmpClause -> Relaxed + !$omp atomic write relaxed + i = j + + ! CHECK-LABEL: OpenMPAtomicConstruct -> OmpAtomicWrite + ! CHECK: OmpMemoryOrderClause -> OmpClause -> SeqCst + !$omp atomic write + i = j +end program requires 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,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 found within a +! compilation unit must specify the same ordering. + +!ERROR: Conflicting 'ATOMIC_DEFAULT_MEM_ORDER' REQUIRES clauses found in compilation unit +program main + contains + subroutine f + !$omp requires atomic_default_mem_order(seq_cst) + end subroutine f + + subroutine g + !$omp requires atomic_default_mem_order(relaxed) + end subroutine g +end program 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 diff --git a/flang/test/Semantics/OpenMP/requires07.f90 b/flang/test/Semantics/OpenMP/requires07.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/OpenMP/requires07.f90 @@ -0,0 +1,13 @@ +! RUN: rm -rf %t && mkdir %t +! RUN: %flang_fc1 -fsyntax-only -fopenmp -module-dir %t '%S/../Inputs/requires_module.f90' +! RUN: %python %S/../test_errors.py %s %flang -fopenmp -module-dir %t +! 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. Test that this is propagated +! from imported modules + +!ERROR: Conflicting 'ATOMIC_DEFAULT_MEM_ORDER' REQUIRES clauses found in compilation unit +use requires_module +!$omp requires atomic_default_mem_order(relaxed) +end program