Skip to content

[Flang][OpenMP][Sema] Module support for REQUIRES directive #77082

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions flang/lib/Semantics/check-omp-structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,22 @@ void OmpStructureChecker::Leave(const parser::OpenMPAtomicConstruct &) {
dirContext_.pop_back();
}

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<ModuleDetails>(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());
}
}

// Clauses
// Mainly categorized as
// 1. Checks on 'OmpClauseList' from 'parse-tree.h'.
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Semantics/check-omp-structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class OmpStructureChecker
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"

Expand Down
43 changes: 43 additions & 0 deletions flang/lib/Semantics/mod-file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ static void PutShape(
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(
Expand Down Expand Up @@ -163,6 +165,7 @@ std::string ModFileWriter::GetAsString(const Symbol &symbol) {
uses_.str().clear();
all << useExtraAttrs_.str();
useExtraAttrs_.str().clear();
PutOmpRequires(all, details);
all << decls_.str();
decls_.str().clear();
auto str{contains_.str()};
Expand Down Expand Up @@ -604,6 +607,8 @@ void ModFileWriter::PutSubprogram(const Symbol &symbol) {
}
}
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())};
Expand Down Expand Up @@ -995,6 +1000,44 @@ llvm::raw_ostream &PutLower(llvm::raw_ostream &os, std::string_view str) {
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->test(WithOmpDeclarative::RequiresFlag::ReverseOffload)) {
os << " reverse_offload";
}
if (flags->test(WithOmpDeclarative::RequiresFlag::UnifiedAddress)) {
os << " unified_address";
}
if (flags->test(WithOmpDeclarative::RequiresFlag::UnifiedSharedMemory)) {
os << " unified_shared_memory";
}
if (flags->test(WithOmpDeclarative::RequiresFlag::DynamicAllocators)) {
os << " dynamic_allocators";
}
}
if (auto *memOrder{details.ompAtomicDefaultMemOrder()}) {
os << " atomic_default_mem_order(";
switch (*memOrder) {
case common::OmpAtomicDefaultMemOrderType::SeqCst:
os << "seq_cst";
break;
case common::OmpAtomicDefaultMemOrderType::AcqRel:
os << "acq_rel";
break;
case common::OmpAtomicDefaultMemOrderType::Relaxed:
os << "relaxed";
break;
}
os << ')';
}
os << '\n';
}
return os;
}

void PutOpenACCDirective(llvm::raw_ostream &os, const Symbol &symbol) {
if (symbol.test(Symbol::Flag::AccDeclare)) {
os << "!$acc declare ";
Expand Down
38 changes: 37 additions & 1 deletion flang/lib/Semantics/resolve-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,42 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {

void Post(const parser::Name &);

void Post(const parser::SpecificationPart &x) {
// Look at USE statements from here so that we also have access to a source
// object, needed to tie the statement to a scope.
for (auto &stmt :
std::get<
std::list<parser::Statement<common::Indirection<parser::UseStmt>>>>(
x.t)) {
const parser::UseStmt &useStmt = stmt.statement.value();
Symbol *moduleSym{useStmt.moduleName.symbol};
if (!moduleSym) {
continue;
}

// Gather information from the imported module's symbol details.
WithOmpDeclarative::RequiresFlags flags;
std::optional<common::OmpAtomicDefaultMemOrderType> memOrder;
common::visit(
[&](auto &details) {
if constexpr (std::is_base_of_v<ModuleDetails,
std::decay_t<decltype(details)>>) {
if (details.has_ompRequires()) {
flags = *details.ompRequires();
}
if (details.has_ompAtomicDefaultMemOrder()) {
memOrder = *details.ompAtomicDefaultMemOrder();
}
}
},
moduleSym->details());

// Merge requires clauses into USE statement's parents.
Scope &scope = context_.FindScope(stmt.source);
AddOmpRequiresToScope(scope, flags, memOrder);
}
}

// 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) {
Expand Down Expand Up @@ -2279,7 +2315,7 @@ void ResolveOmpTopLevelParts(

// Gather REQUIRES clauses from all non-module top-level program unit symbols,
// combine them together ensuring compatibility and apply them to all these
// program units. Modules are skipped because their REQUIRES clauses should be
// program units. Modules are skipped because their REQUIRES clauses are
// propagated via USE statements instead.
WithOmpDeclarative::RequiresFlags combinedFlags;
std::optional<common::OmpAtomicDefaultMemOrderType> combinedMemOrder;
Expand Down
24 changes: 24 additions & 0 deletions flang/lib/Semantics/rewrite-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class OmpRewriteMutator : public DirectiveRewriteMutator {

bool Pre(parser::OpenMPAtomicConstruct &);
bool Pre(parser::OpenMPRequiresConstruct &);
void Post(parser::UseStmt &);

private:
bool atomicDirectiveDefaultOrderFound_{false};
Expand Down Expand Up @@ -165,6 +166,29 @@ bool OmpRewriteMutator::Pre(parser::OpenMPRequiresConstruct &x) {
return false;
}

// Check that a module containing a REQUIRES statement with the
// `atomic_default_mem_order` clause is not USEd after an atomic operation
// without memory order defined.
void OmpRewriteMutator::Post(parser::UseStmt &x) {
semantics::Symbol *symbol{x.moduleName.symbol};
if (!symbol) {
// Cannot check used module if it wasn't resolved.
return;
}

auto *details = symbol->detailsIf<ModuleDetails>();
if (atomicDirectiveDefaultOrderFound_ && details &&
details->has_ompAtomicDefaultMemOrder()) {
context_.Say(x.moduleName.source,
"'%s' module containing '%s' REQUIRES clause imported lexically after "
"atomic operation without a memory order clause"_err_en_US,
x.moduleName.ToString(),
parser::ToUpperCaseLetters(llvm::omp::getOpenMPClauseName(
llvm::omp::OMPC_atomic_default_mem_order)
.str()));
}
}

bool RewriteOmpParts(SemanticsContext &context, parser::Program &program) {
if (!context.IsEnabled(common::LanguageFeature::OpenMP)) {
return true;
Expand Down
3 changes: 3 additions & 0 deletions flang/test/Semantics/Inputs/requires_module.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module requires_module
!$omp requires atomic_default_mem_order(seq_cst), unified_shared_memory
end module
14 changes: 14 additions & 0 deletions flang/test/Semantics/OpenMP/requires10.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
! 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
17 changes: 17 additions & 0 deletions flang/test/Semantics/OpenMP/requires11.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
! 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
! Target-related clauses in REQUIRES directives must come strictly before any
! device constructs, such as declare target with extended list. Test that this
! is propagated from imported modules.

subroutine f
!$omp declare target (f)
end subroutine f

program requires
!ERROR: 'requires_module' module containing device-related REQUIRES directive imported lexically after device construct
use requires_module
end program requires
19 changes: 19 additions & 0 deletions flang/test/Semantics/OpenMP/requires12.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
! 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
! atomic_default_mem_order clauses in REQUIRES directives must come strictly
! before any atomic constructs with no explicit memory order set. Test that this
! is propagated from imported modules.

subroutine f
integer :: a = 0
!$omp atomic
a = a + 1
end subroutine f

program requires
!ERROR: 'requires_module' module containing 'ATOMIC_DEFAULT_MEM_ORDER' REQUIRES clause imported lexically after atomic operation without a memory order clause
use requires_module
end program requires