Skip to content

[flang][OpenMP] Parse WHEN, OTHERWISE, MATCH clauses plus METADIRECTIVE #121817

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 12 commits into from
Jan 29, 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
6 changes: 6 additions & 0 deletions flang/include/flang/Parser/dump-parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ class ParseTreeDumper {
NODE(parser, NullInit)
NODE(parser, ObjectDecl)
NODE(parser, OldParameterStmt)
NODE(parser, OmpMetadirectiveDirective)
NODE(parser, OmpMatchClause)
NODE(parser, OmpOtherwiseClause)
NODE(parser, OmpWhenClause)
NODE(OmpWhenClause, Modifier)
NODE(parser, OmpDirectiveSpecification)
NODE(parser, OmpTraitPropertyName)
NODE(parser, OmpTraitScore)
NODE(parser, OmpTraitPropertyExtension)
Expand Down
57 changes: 55 additions & 2 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3459,6 +3459,14 @@ WRAPPER_CLASS(PauseStmt, std::optional<StopCode>);
struct OmpClause;
struct OmpClauseList;

struct OmpDirectiveSpecification {
TUPLE_CLASS_BOILERPLATE(OmpDirectiveSpecification);
std::tuple<llvm::omp::Directive,
std::optional<common::Indirection<OmpClauseList>>>
t;
CharBlock source;
};

// 2.1 Directives or clauses may accept a list or extended-list.
// A list item is a variable, array section or common block name (enclosed
// in slashes). An extended list item is a list item or a procedure Name.
Expand Down Expand Up @@ -3962,14 +3970,21 @@ struct OmpBindClause {

// Ref: [4.5:46-50], [5.0:74-78], [5.1:92-96], [5.2:109]
//
// When used as a data-sharing clause:
// default-clause ->
// DEFAULT(data-sharing-attribute) // since 4.5
// data-sharing-attribute ->
// SHARED | NONE | // since 4.5
// PRIVATE | FIRSTPRIVATE // since 5.0
//
// When used in METADIRECTIVE:
// default-clause ->
// DEFAULT(directive-specification) // since 5.0, until 5.1
// See also otherwise-clause.
struct OmpDefaultClause {
ENUM_CLASS(DataSharingAttribute, Private, Firstprivate, Shared, None)
WRAPPER_CLASS_BOILERPLATE(OmpDefaultClause, DataSharingAttribute);
UNION_CLASS_BOILERPLATE(OmpDefaultClause);
std::variant<DataSharingAttribute, OmpDirectiveSpecification> u;
};

// Ref: [4.5:103-107], [5.0:324-325], [5.1:357-358], [5.2:161-162]
Expand Down Expand Up @@ -4187,6 +4202,16 @@ struct OmpMapClause {
std::tuple<MODIFIERS(), OmpObjectList, /*CommaSeparated=*/bool> t;
};

// Ref: [5.0:58-60], [5.1:63-68], [5.2:194-195]
//
// match-clause ->
// MATCH (context-selector-specification) // since 5.0
struct OmpMatchClause {
// The context-selector is an argument.
WRAPPER_CLASS_BOILERPLATE(
OmpMatchClause, traits::OmpContextSelectorSpecification);
};

// Ref: [5.2:217-218]
// message-clause ->
// MESSAGE("message-text")
Expand Down Expand Up @@ -4217,6 +4242,17 @@ struct OmpOrderClause {
std::tuple<MODIFIERS(), Ordering> t;
};

// Ref: [5.0:56-57], [5.1:60-62], [5.2:191]
//
// otherwise-clause ->
// DEFAULT ([directive-specification]) // since 5.0, until 5.1
// otherwise-clause ->
// OTHERWISE ([directive-specification])] // since 5.2
struct OmpOtherwiseClause {
WRAPPER_CLASS_BOILERPLATE(
OmpOtherwiseClause, std::optional<OmpDirectiveSpecification>);
};

// Ref: [4.5:46-50], [5.0:74-78], [5.1:92-96], [5.2:229-230]
//
// proc-bind-clause ->
Expand Down Expand Up @@ -4302,6 +4338,17 @@ struct OmpUpdateClause {
std::variant<OmpDependenceType, OmpTaskDependenceType> u;
};

// Ref: [5.0:56-57], [5.1:60-62], [5.2:190-191]
//
// when-clause ->
// WHEN (context-selector :
// [directive-specification]) // since 5.0
struct OmpWhenClause {
TUPLE_CLASS_BOILERPLATE(OmpWhenClause);
MODIFIER_BOILERPLATE(OmpContextSelector);
std::tuple<MODIFIERS(), std::optional<OmpDirectiveSpecification>> t;
};

// OpenMP Clauses
struct OmpClause {
UNION_CLASS_BOILERPLATE(OmpClause);
Expand All @@ -4326,6 +4373,12 @@ struct OmpClauseList {

// --- Directives and constructs

struct OmpMetadirectiveDirective {
TUPLE_CLASS_BOILERPLATE(OmpMetadirectiveDirective);
std::tuple<OmpClauseList> t;
CharBlock source;
};

// Ref: [5.1:89-90], [5.2:216]
//
// nothing-directive ->
Expand Down Expand Up @@ -4724,7 +4777,7 @@ struct OpenMPStandaloneConstruct {
CharBlock source;
std::variant<OpenMPSimpleStandaloneConstruct, OpenMPFlushConstruct,
OpenMPCancelConstruct, OpenMPCancellationPointConstruct,
OpenMPDepobjConstruct>
OpenMPDepobjConstruct, OmpMetadirectiveDirective>
u;
};

Expand Down
57 changes: 45 additions & 12 deletions flang/lib/Lower/OpenMP/Clauses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,6 @@ MAKE_EMPTY_CLASS(Threadprivate, Threadprivate);

MAKE_INCOMPLETE_CLASS(AdjustArgs, AdjustArgs);
MAKE_INCOMPLETE_CLASS(AppendArgs, AppendArgs);
MAKE_INCOMPLETE_CLASS(Match, Match);
// MAKE_INCOMPLETE_CLASS(Otherwise, ); // missing-in-parser
MAKE_INCOMPLETE_CLASS(When, When);

List<IteratorSpecifier>
makeIteratorSpecifiers(const parser::OmpIteratorSpecifier &inp,
Expand Down Expand Up @@ -528,8 +525,13 @@ Copyprivate make(const parser::OmpClause::Copyprivate &inp,
return Copyprivate{/*List=*/makeObjects(inp.v, semaCtx)};
}

Default make(const parser::OmpClause::Default &inp,
semantics::SemanticsContext &semaCtx) {
// The Default clause is overloaded in OpenMP 5.0 and 5.1: it can be either
// a data-sharing clause, or a METADIRECTIVE clause. In the latter case, it
// has been superseded by the OTHERWISE clause.
// Disambiguate this in this representation: for the DSA case, create Default,
// and in the other case create Otherwise.
Default makeDefault(const parser::OmpClause::Default &inp,
semantics::SemanticsContext &semaCtx) {
// inp.v -> parser::OmpDefaultClause
using wrapped = parser::OmpDefaultClause;

Expand All @@ -543,7 +545,13 @@ Default make(const parser::OmpClause::Default &inp,
// clang-format on
);

return Default{/*DataSharingAttribute=*/convert(inp.v.v)};
auto dsa = std::get<wrapped::DataSharingAttribute>(inp.v.u);
return Default{/*DataSharingAttribute=*/convert(dsa)};
}

Otherwise makeOtherwise(const parser::OmpClause::Default &inp,
semantics::SemanticsContext &semaCtx) {
return Otherwise{};
}

Defaultmap make(const parser::OmpClause::Defaultmap &inp,
Expand Down Expand Up @@ -997,7 +1005,11 @@ Map make(const parser::OmpClause::Map &inp,
/*LocatorList=*/makeObjects(t4, semaCtx)}};
}

// Match: incomplete
Match make(const parser::OmpClause::Match &inp,
semantics::SemanticsContext &semaCtx) {
return Match{};
}

// MemoryOrder: empty
// Mergeable: empty

Expand Down Expand Up @@ -1101,7 +1113,11 @@ Ordered make(const parser::OmpClause::Ordered &inp,
return Ordered{/*N=*/maybeApply(makeExprFn(semaCtx), inp.v)};
}

// Otherwise: incomplete, missing-in-parser
// See also Default.
Otherwise make(const parser::OmpClause::Otherwise &inp,
semantics::SemanticsContext &semaCtx) {
return Otherwise{};
}

Partial make(const parser::OmpClause::Partial &inp,
semantics::SemanticsContext &semaCtx) {
Expand Down Expand Up @@ -1356,15 +1372,32 @@ UsesAllocators make(const parser::OmpClause::UsesAllocators &inp,
}

// Weak: empty
// When: incomplete

When make(const parser::OmpClause::When &inp,
semantics::SemanticsContext &semaCtx) {
return When{};
}

// Write: empty
} // namespace clause

Clause makeClause(const parser::OmpClause &cls,
semantics::SemanticsContext &semaCtx) {
return Fortran::common::visit(
[&](auto &&s) {
return makeClause(cls.Id(), clause::make(s, semaCtx), cls.source);
return Fortran::common::visit( //
common::visitors{
[&](const parser::OmpClause::Default &s) {
using DSA = parser::OmpDefaultClause::DataSharingAttribute;
if (std::holds_alternative<DSA>(s.v.u)) {
return makeClause(llvm::omp::Clause::OMPC_default,
clause::makeDefault(s, semaCtx), cls.source);
} else {
return makeClause(llvm::omp::Clause::OMPC_otherwise,
clause::makeOtherwise(s, semaCtx), cls.source);
}
},
[&](auto &&s) {
return makeClause(cls.Id(), clause::make(s, semaCtx), cls.source);
},
},
cls.u);
}
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Lower/OpenMP/Clauses.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ using OmpxBare = tomp::clause::OmpxBareT<TypeTy, IdTy, ExprTy>;
using OmpxDynCgroupMem = tomp::clause::OmpxDynCgroupMemT<TypeTy, IdTy, ExprTy>;
using Ordered = tomp::clause::OrderedT<TypeTy, IdTy, ExprTy>;
using Order = tomp::clause::OrderT<TypeTy, IdTy, ExprTy>;
using Otherwise = tomp::clause::OtherwiseT<TypeTy, IdTy, ExprTy>;
using Partial = tomp::clause::PartialT<TypeTy, IdTy, ExprTy>;
using Priority = tomp::clause::PriorityT<TypeTy, IdTy, ExprTy>;
using Private = tomp::clause::PrivateT<TypeTy, IdTy, ExprTy>;
Expand Down
8 changes: 8 additions & 0 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ extractOmpDirective(const parser::OpenMPConstruct &ompConstruct) {
[](const parser::OpenMPCancellationPointConstruct &c) {
return llvm::omp::OMPD_cancellation_point;
},
[](const parser::OmpMetadirectiveDirective &c) {
return llvm::omp::OMPD_metadirective;
},
[](const parser::OpenMPDepobjConstruct &c) {
return llvm::omp::OMPD_depobj;
}},
Expand Down Expand Up @@ -3231,6 +3234,11 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
TODO(converter.getCurrentLocation(), "OpenMPDepobjConstruct");
}

static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx,
lower::pft::Evaluation &eval,
const parser::OmpMetadirectiveDirective &construct) {}

static void
genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
Expand Down
36 changes: 34 additions & 2 deletions flang/lib/Parser/openmp-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ static TypeDeclarationStmt makeIterSpecDecl(std::list<ObjectName> &&names) {
makeEntityList(std::move(names)));
}

TYPE_PARSER(sourced(construct<OmpDirectiveSpecification>(
OmpDirectiveNameParser{}, maybe(indirect(Parser<OmpClauseList>{})))))

// --- Parsers for context traits -------------------------------------

static std::string nameToString(Name &&name) { return name.ToString(); }
Expand Down Expand Up @@ -501,6 +504,9 @@ TYPE_PARSER(sourced(construct<OmpToClause::Modifier>(
construct<OmpToClause::Modifier>(Parser<OmpMapper>{}) ||
construct<OmpToClause::Modifier>(Parser<OmpIterator>{})))))

TYPE_PARSER(sourced(construct<OmpWhenClause::Modifier>( //
Parser<OmpContextSelector>{})))

// --- Parsers for clauses --------------------------------------------

/// `MOBClause` is a clause that has a
Expand All @@ -527,13 +533,18 @@ TYPE_PARSER(construct<OmpAffinityClause>(
Parser<OmpObjectList>{}))

// 2.15.3.1 DEFAULT (PRIVATE | FIRSTPRIVATE | SHARED | NONE)
TYPE_PARSER(construct<OmpDefaultClause>(
TYPE_PARSER(construct<OmpDefaultClause::DataSharingAttribute>(
"PRIVATE" >> pure(OmpDefaultClause::DataSharingAttribute::Private) ||
"FIRSTPRIVATE" >>
pure(OmpDefaultClause::DataSharingAttribute::Firstprivate) ||
"SHARED" >> pure(OmpDefaultClause::DataSharingAttribute::Shared) ||
"NONE" >> pure(OmpDefaultClause::DataSharingAttribute::None)))

TYPE_PARSER(construct<OmpDefaultClause>(
construct<OmpDefaultClause>(
Parser<OmpDefaultClause::DataSharingAttribute>{}) ||
construct<OmpDefaultClause>(Parser<OmpDirectiveSpecification>{})))

// 2.5 PROC_BIND (MASTER | CLOSE | PRIMARY | SPREAD)
TYPE_PARSER(construct<OmpProcBindClause>(
"CLOSE" >> pure(OmpProcBindClause::AffinityPolicy::Close) ||
Expand Down Expand Up @@ -698,6 +709,16 @@ TYPE_PARSER(construct<OmpOrderClause>(
maybe(nonemptyList(Parser<OmpOrderClause::Modifier>{}) / ":"),
"CONCURRENT" >> pure(OmpOrderClause::Ordering::Concurrent)))

TYPE_PARSER(construct<OmpMatchClause>(
Parser<traits::OmpContextSelectorSpecification>{}))

TYPE_PARSER(construct<OmpOtherwiseClause>(
maybe(sourced(Parser<OmpDirectiveSpecification>{}))))

TYPE_PARSER(construct<OmpWhenClause>(
maybe(nonemptyList(Parser<OmpWhenClause::Modifier>{}) / ":"),
maybe(sourced(Parser<OmpDirectiveSpecification>{}))))

// OMP 5.2 12.6.1 grainsize([ prescriptiveness :] scalar-integer-expression)
TYPE_PARSER(construct<OmpGrainsizeClause>(
maybe(nonemptyList(Parser<OmpGrainsizeClause::Modifier>{}) / ":"),
Expand Down Expand Up @@ -815,6 +836,8 @@ TYPE_PARSER(
parenthesized(Parser<OmpObjectList>{}))) ||
"MAP" >> construct<OmpClause>(construct<OmpClause::Map>(
parenthesized(Parser<OmpMapClause>{}))) ||
"MATCH" >> construct<OmpClause>(construct<OmpClause::Match>(
parenthesized(Parser<OmpMatchClause>{}))) ||
"MERGEABLE" >> construct<OmpClause>(construct<OmpClause::Mergeable>()) ||
"MESSAGE" >> construct<OmpClause>(construct<OmpClause::Message>(
parenthesized(Parser<OmpMessageClause>{}))) ||
Expand All @@ -839,6 +862,8 @@ TYPE_PARSER(
parenthesized(Parser<OmpOrderClause>{}))) ||
"ORDERED" >> construct<OmpClause>(construct<OmpClause::Ordered>(
maybe(parenthesized(scalarIntConstantExpr)))) ||
"OTHERWISE" >> construct<OmpClause>(construct<OmpClause::Otherwise>(
maybe(parenthesized(Parser<OmpOtherwiseClause>{})))) ||
"PARTIAL" >> construct<OmpClause>(construct<OmpClause::Partial>(
maybe(parenthesized(scalarIntConstantExpr)))) ||
"PRIORITY" >> construct<OmpClause>(construct<OmpClause::Priority>(
Expand Down Expand Up @@ -894,7 +919,9 @@ TYPE_PARSER(
parenthesized(nonemptyList(name)))) ||
"UNTIED" >> construct<OmpClause>(construct<OmpClause::Untied>()) ||
"UPDATE" >> construct<OmpClause>(construct<OmpClause::Update>(
parenthesized(Parser<OmpUpdateClause>{}))))
parenthesized(Parser<OmpUpdateClause>{}))) ||
"WHEN" >> construct<OmpClause>(construct<OmpClause::When>(
parenthesized(Parser<OmpWhenClause>{}))))

// [Clause, [Clause], ...]
TYPE_PARSER(sourced(construct<OmpClauseList>(
Expand All @@ -914,6 +941,9 @@ TYPE_PARSER(sourced(construct<OpenMPUtilityConstruct>(
sourced(construct<OpenMPUtilityConstruct>(
sourced(Parser<OmpNothingDirective>{}))))))

TYPE_PARSER(sourced(construct<OmpMetadirectiveDirective>(
"METADIRECTIVE" >> Parser<OmpClauseList>{})))

// Omp directives enclosing do loop
TYPE_PARSER(sourced(construct<OmpLoopDirective>(first(
"DISTRIBUTE PARALLEL DO SIMD" >>
Expand Down Expand Up @@ -1059,6 +1089,8 @@ TYPE_PARSER(
construct<OpenMPStandaloneConstruct>(Parser<OpenMPCancelConstruct>{}) ||
construct<OpenMPStandaloneConstruct>(
Parser<OpenMPCancellationPointConstruct>{}) ||
construct<OpenMPStandaloneConstruct>(
Parser<OmpMetadirectiveDirective>{}) ||
construct<OpenMPStandaloneConstruct>(Parser<OpenMPDepobjConstruct>{})) /
endOfLine)

Expand Down
Loading