Skip to content

[flang][OpenMP] Accept modern syntax of FLUSH construct #128975

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 4 commits into from
Mar 3, 2025
Merged
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
17 changes: 14 additions & 3 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -4888,12 +4888,23 @@ struct OpenMPDispatchConstruct {
t;
};

// 2.17.8 flush -> FLUSH [memory-order-clause] [(variable-name-list)]
// [4.5:162-165], [5.0:242-246], [5.1:275-279], [5.2:315-316], [6.0:498-500]
//
// flush-construct ->
// FLUSH [(list)] // since 4.5, until 4.5
// flush-construct ->
// FLUSH [memory-order-clause] [(list)] // since 5.0, until 5.1
// flush-construct ->
// FLUSH [(list)] [clause-list] // since 5.2
//
// memory-order-clause -> // since 5.0, until 5.1
// ACQ_REL | RELEASE | ACQUIRE | // since 5.0
// SEQ_CST // since 5.1
struct OpenMPFlushConstruct {
TUPLE_CLASS_BOILERPLATE(OpenMPFlushConstruct);
CharBlock source;
std::tuple<Verbatim, std::optional<std::list<OmpMemoryOrderClause>>,
std::optional<OmpObjectList>>
std::tuple<Verbatim, std::optional<OmpObjectList>,
std::optional<OmpClauseList>, /*TrailingClauses=*/bool>
t;
};

Expand Down
7 changes: 3 additions & 4 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3275,13 +3275,12 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
const auto &objectList =
std::get<std::optional<parser::OmpObjectList>>(flushConstruct.t);
const auto &clauseList =
std::get<std::optional<std::list<parser::OmpMemoryOrderClause>>>(
flushConstruct.t);
std::get<std::optional<parser::OmpClauseList>>(flushConstruct.t);
ObjectList objects =
objectList ? makeObjects(*objectList, semaCtx) : ObjectList{};
List<Clause> clauses =
clauseList ? makeList(*clauseList,
[&](auto &&s) { return makeClause(s.v, semaCtx); })
clauseList ? makeList(clauseList->v,
[&](auto &&s) { return makeClause(s, semaCtx); })
: List<Clause>{};
mlir::Location currentLocation = converter.genLocation(verbatim.source);

Expand Down
22 changes: 19 additions & 3 deletions flang/lib/Parser/openmp-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,9 +1104,25 @@ TYPE_PARSER(sourced(construct<OmpAtomicClauseList>(
TYPE_PARSER(sourced(construct<OpenMPDepobjConstruct>(verbatim("DEPOBJ"_tok),
parenthesized(Parser<OmpObject>{}), sourced(Parser<OmpClause>{}))))

TYPE_PARSER(sourced(construct<OpenMPFlushConstruct>(verbatim("FLUSH"_tok),
many(maybe(","_tok) >> sourced(Parser<OmpMemoryOrderClause>{})),
maybe(parenthesized(Parser<OmpObjectList>{})))))
static OpenMPFlushConstruct makeFlushFromOldSyntax(Verbatim &&text,
std::optional<OmpClauseList> &&clauses,
std::optional<OmpObjectList> &&objects) {
bool oldSyntax{
clauses && !clauses->v.empty() && objects && !objects->v.empty()};
return OpenMPFlushConstruct{std::move(text), std::move(objects),
std::move(clauses),
/*TrailingClauses=*/!oldSyntax};
}

TYPE_PARSER(sourced( //
construct<OpenMPFlushConstruct>( //
applyFunction<OpenMPFlushConstruct>(makeFlushFromOldSyntax,
verbatim("FLUSH"_tok), maybe(Parser<OmpClauseList>{}),
maybe(parenthesized(Parser<OmpObjectList>{})))) ||

construct<OpenMPFlushConstruct>( //
verbatim("FLUSH"_tok), maybe(parenthesized(Parser<OmpObjectList>{})),
Parser<OmpClauseList>{}, pure(/*TrailingClauses=*/true))))

// Simple Standalone Directives
TYPE_PARSER(sourced(construct<OmpSimpleStandaloneDirective>(first(
Expand Down
11 changes: 8 additions & 3 deletions flang/lib/Parser/unparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2874,9 +2874,14 @@ class UnparseVisitor {
}
void Unparse(const OpenMPFlushConstruct &x) {
BeginOpenMP();
Word("!$OMP FLUSH ");
Walk(std::get<std::optional<std::list<OmpMemoryOrderClause>>>(x.t));
Walk(" (", std::get<std::optional<OmpObjectList>>(x.t), ")");
Word("!$OMP FLUSH");
if (std::get</*ClausesTrailing=*/bool>(x.t)) {
Walk("(", std::get<std::optional<OmpObjectList>>(x.t), ")");
Walk(" ", std::get<std::optional<OmpClauseList>>(x.t));
} else {
Walk(" ", std::get<std::optional<OmpClauseList>>(x.t));
Walk(" (", std::get<std::optional<OmpObjectList>>(x.t), ")");
}
Put("\n");
EndOpenMP();
}
Expand Down
14 changes: 12 additions & 2 deletions flang/lib/Semantics/check-omp-structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2139,16 +2139,26 @@ void OmpStructureChecker::Enter(const parser::OpenMPFlushConstruct &x) {
}

void OmpStructureChecker::Leave(const parser::OpenMPFlushConstruct &x) {
auto &flushList{std::get<std::optional<parser::OmpObjectList>>(x.t)};

if (FindClause(llvm::omp::Clause::OMPC_acquire) ||
FindClause(llvm::omp::Clause::OMPC_release) ||
FindClause(llvm::omp::Clause::OMPC_acq_rel)) {
if (const auto &flushList{
std::get<std::optional<parser::OmpObjectList>>(x.t)}) {
if (flushList) {
context_.Say(parser::FindSourceLocation(flushList),
"If memory-order-clause is RELEASE, ACQUIRE, or ACQ_REL, list items "
"must not be specified on the FLUSH directive"_err_en_US);
}
}

unsigned version{context_.langOptions().OpenMPVersion};
if (version >= 52) {
if (!std::get</*TrailingClauses=*/bool>(x.t)) {
context_.Say(parser::FindSourceLocation(flushList),
"The syntax \"FLUSH clause (object, ...)\" has been deprecated, use \"FLUSH(object, ...) clause\" instead"_warn_en_US);
}
}

dirContext_.pop_back();
}

Expand Down
10 changes: 6 additions & 4 deletions flang/test/Semantics/OpenMP/flush01.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
! RUN: %python %S/../test_errors.py %s %flang -fopenmp
! REQUIRES: openmp_runtime

! RUN: %python %S/../test_errors.py %s %flang_fc1 %openmp_flags -fopenmp-version=50

! 2.17.8 Flush construct [OpenMP 5.0]
! memory-order-clause ->
Expand All @@ -22,13 +24,13 @@
array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/)
!$omp flush acquire

!ERROR: expected end of line
!ERROR: PRIVATE clause is not allowed on the FLUSH directive
!$omp flush private(array)
!ERROR: expected end of line
!ERROR: NUM_THREADS clause is not allowed on the FLUSH directive
!$omp flush num_threads(4)

! Mix allowed and not allowed clauses.
!ERROR: expected end of line
!ERROR: NUM_THREADS clause is not allowed on the FLUSH directive
!$omp flush num_threads(4) acquire
end if
!$omp end parallel
Expand Down
7 changes: 7 additions & 0 deletions flang/test/Semantics/OpenMP/flush03.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp -fopenmp-version=52 -Werror

subroutine f00(x)
integer :: x
!ERROR: The syntax "FLUSH clause (object, ...)" has been deprecated, use "FLUSH(object, ...) clause" instead
!$omp flush seq_cst (x)
end
Loading