Skip to content

Commit 2ea14e0

Browse files
committed
[flang][OpenMP] Implement OmpDirectiveName, use in OmpDirectiveSpecification
The `OmpDirectiveName` class has a source in addition to wrapping the llvm::omp::Directive.
1 parent 32f5437 commit 2ea14e0

File tree

11 files changed

+89
-35
lines changed

11 files changed

+89
-35
lines changed

flang/include/flang/Parser/dump-parse-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ class ParseTreeDumper {
489489
NODE(parser, OmpOtherwiseClause)
490490
NODE(parser, OmpWhenClause)
491491
NODE(OmpWhenClause, Modifier)
492+
NODE(parser, OmpDirectiveName)
492493
NODE(parser, OmpDirectiveSpecification)
493494
NODE(parser, OmpTraitPropertyName)
494495
NODE(parser, OmpTraitScore)

flang/include/flang/Parser/parse-tree.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3464,6 +3464,18 @@ WRAPPER_CLASS(PauseStmt, std::optional<StopCode>);
34643464
struct OmpClause;
34653465
struct OmpDirectiveSpecification;
34663466

3467+
struct OmpDirectiveName {
3468+
// No boilerplates: this class should be copyable, movable, etc.
3469+
constexpr OmpDirectiveName() = default;
3470+
constexpr OmpDirectiveName(const OmpDirectiveName &) = default;
3471+
// Construct from an already parsed text. Use Verbatim for this because
3472+
// Verbatim's source corresponds to an actual source location.
3473+
OmpDirectiveName(const Verbatim &name);
3474+
using WrapperTrait = std::true_type;
3475+
CharBlock source;
3476+
llvm::omp::Directive v{llvm::omp::Directive::OMPD_unknown};
3477+
};
3478+
34673479
// 2.1 Directives or clauses may accept a list or extended-list.
34683480
// A list item is a variable, array section or common block name (enclosed
34693481
// in slashes). An extended list item is a list item or a procedure Name.
@@ -4493,7 +4505,10 @@ struct OmpClauseList {
44934505
struct OmpDirectiveSpecification {
44944506
CharBlock source;
44954507
TUPLE_CLASS_BOILERPLATE(OmpDirectiveSpecification);
4496-
std::tuple<llvm::omp::Directive, std::optional<std::list<OmpArgument>>,
4508+
llvm::omp::Directive DirId() const { //
4509+
return std::get<OmpDirectiveName>(t).v;
4510+
}
4511+
std::tuple<OmpDirectiveName, std::optional<std::list<OmpArgument>>,
44974512
std::optional<OmpClauseList>>
44984513
t;
44994514
};

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,38 @@ namespace Fortran::parser {
2727
constexpr auto startOmpLine = skipStuffBeforeStatement >> "!$OMP "_sptok;
2828
constexpr auto endOmpLine = space >> endOfLine;
2929

30+
template <typename Parser> struct UnwrapParser {
31+
static_assert(
32+
Parser::resultType::WrapperTrait::value && "Wrapper class required");
33+
using resultType = decltype(Parser::resultType::v);
34+
constexpr UnwrapParser(Parser p) : parser_(p) {}
35+
36+
std::optional<resultType> Parse(ParseState &state) const {
37+
if (auto result{parser_.Parse(state)}) {
38+
return result->v;
39+
}
40+
return std::nullopt;
41+
}
42+
43+
private:
44+
const Parser parser_;
45+
};
46+
47+
template <typename Parser> constexpr auto unwrap(const Parser &p) {
48+
return UnwrapParser<Parser>(p);
49+
}
50+
3051
/// Parse OpenMP directive name (this includes compound directives).
3152
struct OmpDirectiveNameParser {
32-
using resultType = llvm::omp::Directive;
53+
using resultType = OmpDirectiveName;
3354
using Token = TokenStringMatch<false, false>;
3455

3556
std::optional<resultType> Parse(ParseState &state) const {
3657
for (const NameWithId &nid : directives()) {
3758
if (attempt(Token(nid.first.data())).Parse(state)) {
38-
return nid.second;
59+
OmpDirectiveName n;
60+
n.v = nid.second;
61+
return n;
3962
}
4063
}
4164
return std::nullopt;
@@ -218,7 +241,7 @@ TYPE_PARSER(construct<OmpTraitSelectorName::Value>(
218241
TYPE_PARSER(sourced(construct<OmpTraitSelectorName>(
219242
// Parse predefined names first (because of SIMD).
220243
construct<OmpTraitSelectorName>(Parser<OmpTraitSelectorName::Value>{}) ||
221-
construct<OmpTraitSelectorName>(OmpDirectiveNameParser{}) ||
244+
construct<OmpTraitSelectorName>(unwrap(OmpDirectiveNameParser{})) ||
222245
// identifier-or-string for extensions
223246
construct<OmpTraitSelectorName>(
224247
applyFunction(nameToString, Parser<Name>{})) ||
@@ -480,7 +503,8 @@ TYPE_PARSER(sourced(construct<OmpFromClause::Modifier>(
480503
TYPE_PARSER(sourced(
481504
construct<OmpGrainsizeClause::Modifier>(Parser<OmpPrescriptiveness>{})))
482505

483-
TYPE_PARSER(sourced(construct<OmpIfClause::Modifier>(OmpDirectiveNameParser{})))
506+
TYPE_PARSER(
507+
sourced(construct<OmpIfClause::Modifier>(unwrap(OmpDirectiveNameParser{}))))
484508

485509
TYPE_PARSER(sourced(construct<OmpInReductionClause::Modifier>(
486510
Parser<OmpReductionIdentifier>{})))
@@ -775,9 +799,9 @@ TYPE_PARSER(construct<OmpMessageClause>(expr))
775799

776800
TYPE_PARSER(construct<OmpHoldsClause>(indirect(expr)))
777801
TYPE_PARSER(construct<OmpAbsentClause>(many(maybe(","_tok) >>
778-
construct<llvm::omp::Directive>(OmpDirectiveNameParser{}))))
802+
construct<llvm::omp::Directive>(unwrap(OmpDirectiveNameParser{})))))
779803
TYPE_PARSER(construct<OmpContainsClause>(many(maybe(","_tok) >>
780-
construct<llvm::omp::Directive>(OmpDirectiveNameParser{}))))
804+
construct<llvm::omp::Directive>(unwrap(OmpDirectiveNameParser{})))))
781805

782806
TYPE_PARSER("ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
783807
parenthesized(Parser<OmpAbsentClause>{}))) ||
@@ -972,7 +996,7 @@ TYPE_PARSER(sourced(construct<OmpErrorDirective>(
972996
// --- Parsers for directives and constructs --------------------------
973997

974998
TYPE_PARSER(sourced(construct<OmpDirectiveSpecification>( //
975-
OmpDirectiveNameParser{},
999+
sourced(OmpDirectiveNameParser{}),
9761000
maybe(parenthesized(nonemptyList(Parser<OmpArgument>{}))),
9771001
maybe(Parser<OmpClauseList>{}))))
9781002

flang/lib/Parser/parse-tree.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,21 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Name &x) {
253253
return os << x.ToString();
254254
}
255255

256+
OmpDirectiveName::OmpDirectiveName(const Verbatim &name) {
257+
std::string_view nameView{name.source.begin(), name.source.size()};
258+
std::string nameLower{ToLowerCaseLetters(nameView)};
259+
// If the name was actually "unknown" then accept it, otherwise flag
260+
// OMPD_unknown (the default return value from getOpenMPDirectiveKind)
261+
// as an error.
262+
if (nameLower != "unknown") {
263+
v = llvm::omp::getOpenMPDirectiveKind(nameLower);
264+
assert(v != llvm::omp::Directive::OMPD_unknown && "Unrecognized directive");
265+
} else {
266+
v = llvm::omp::Directive::OMPD_unknown;
267+
}
268+
source = name.source;
269+
}
270+
256271
OmpDependenceType::Value OmpDoacross::GetDepType() const {
257272
return common::visit( //
258273
common::visitors{

flang/lib/Parser/unparse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ class UnparseVisitor {
20952095
}
20962096
void Unparse(const OmpDirectiveSpecification &x) {
20972097
using ArgList = std::list<parser::OmpArgument>;
2098-
Walk(std::get<llvm::omp::Directive>(x.t));
2098+
Walk(std::get<OmpDirectiveName>(x.t));
20992099
if (auto &args{std::get<std::optional<ArgList>>(x.t)}) {
21002100
Put("(");
21012101
Walk(*args);

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ void OmpStructureChecker::CheckHintClause(
625625
}
626626

627627
void OmpStructureChecker::Enter(const parser::OmpDirectiveSpecification &x) {
628-
PushContextAndClauseSets(x.source, std::get<llvm::omp::Directive>(x.t));
628+
PushContextAndClauseSets(x.source, x.DirId());
629629
}
630630

631631
void OmpStructureChecker::Leave(const parser::OmpDirectiveSpecification &) {

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
352352
}
353353

354354
bool Pre(const parser::OmpDirectiveSpecification &x) {
355-
PushContext(x.source, std::get<llvm::omp::Directive>(x.t));
355+
PushContext(x.source, x.DirId());
356356
return true;
357357
}
358358
void Post(const parser::OmpDirectiveSpecification &) { PopContext(); }

flang/lib/Semantics/resolve-names.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,11 +1775,10 @@ bool OmpVisitor::Pre(const parser::OmpDirectiveSpecification &x) {
17751775
// Disable the semantic analysis for it for now to allow the compiler to
17761776
// parse METADIRECTIVE without flagging errors.
17771777
AddOmpSourceRange(x.source);
1778-
auto dirId{std::get<llvm::omp::Directive>(x.t)};
17791778
auto &maybeArgs{std::get<std::optional<std::list<parser::OmpArgument>>>(x.t)};
17801779
auto &maybeClauses{std::get<std::optional<parser::OmpClauseList>>(x.t)};
17811780

1782-
switch (dirId) {
1781+
switch (x.DirId()) {
17831782
case llvm::omp::Directive::OMPD_declare_mapper:
17841783
if (maybeArgs && maybeClauses) {
17851784
const parser::OmpArgument &first{maybeArgs->front()};

flang/test/Parser/OpenMP/metadirective-dirspec.f90

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ subroutine f00(x)
2525
!PARSE-TREE: | | | | | | LiteralConstant -> LogicalLiteralConstant
2626
!PARSE-TREE: | | | | | | | bool = 'true'
2727
!PARSE-TREE: | | OmpDirectiveSpecification
28-
!PARSE-TREE: | | | llvm::omp::Directive = allocate
28+
!PARSE-TREE: | | | OmpDirectiveName -> llvm::omp::Directive = allocate
2929
!PARSE-TREE: | | | OmpArgument -> OmpLocator -> OmpObject -> Designator -> DataRef -> Name = 'x'
3030
!PARSE-TREE: | | | OmpClauseList ->
3131

@@ -51,7 +51,7 @@ subroutine f01(x)
5151
!PARSE-TREE: | | | | | | LiteralConstant -> LogicalLiteralConstant
5252
!PARSE-TREE: | | | | | | | bool = 'true'
5353
!PARSE-TREE: | | OmpDirectiveSpecification
54-
!PARSE-TREE: | | | llvm::omp::Directive = critical
54+
!PARSE-TREE: | | | OmpDirectiveName -> llvm::omp::Directive = critical
5555
!PARSE-TREE: | | | OmpArgument -> OmpLocator -> OmpObject -> Designator -> DataRef -> Name = 'x'
5656
!PARSE-TREE: | | | OmpClauseList ->
5757

@@ -76,7 +76,7 @@ subroutine f02
7676
!PARSE-TREE: | | | | | | LiteralConstant -> LogicalLiteralConstant
7777
!PARSE-TREE: | | | | | | | bool = 'true'
7878
!PARSE-TREE: | | OmpDirectiveSpecification
79-
!PARSE-TREE: | | | llvm::omp::Directive = declare mapper
79+
!PARSE-TREE: | | | OmpDirectiveName -> llvm::omp::Directive = declare mapper
8080
!PARSE-TREE: | | | OmpArgument -> OmpMapperSpecifier
8181
!PARSE-TREE: | | | | Name = 'mymapper'
8282
!PARSE-TREE: | | | | TypeSpec -> IntrinsicTypeSpec -> IntegerTypeSpec ->
@@ -120,7 +120,7 @@ subroutine f03
120120
!PARSE-TREE: | | | | | | LiteralConstant -> LogicalLiteralConstant
121121
!PARSE-TREE: | | | | | | | bool = 'true'
122122
!PARSE-TREE: | | OmpDirectiveSpecification
123-
!PARSE-TREE: | | | llvm::omp::Directive = declare reduction
123+
!PARSE-TREE: | | | OmpDirectiveName -> llvm::omp::Directive = declare reduction
124124
!PARSE-TREE: | | | OmpArgument -> OmpReductionSpecifier
125125
!PARSE-TREE: | | | | OmpReductionIdentifier -> DefinedOperator -> IntrinsicOperator = Add
126126
!PARSE-TREE: | | | | OmpTypeNameList -> OmpTypeSpecifier -> TypeSpec -> DerivedTypeSpec
@@ -158,7 +158,7 @@ subroutine f04
158158
!PARSE-TREE: | | | | | | LiteralConstant -> LogicalLiteralConstant
159159
!PARSE-TREE: | | | | | | | bool = 'true'
160160
!PARSE-TREE: | | OmpDirectiveSpecification
161-
!PARSE-TREE: | | | llvm::omp::Directive = declare simd
161+
!PARSE-TREE: | | | OmpDirectiveName -> llvm::omp::Directive = declare simd
162162
!PARSE-TREE: | | | OmpArgument -> OmpLocator -> OmpObject -> Designator -> DataRef -> Name = 'f04'
163163
!PARSE-TREE: | | | OmpClauseList ->
164164
!PARSE-TREE: ImplicitPart ->
@@ -183,7 +183,7 @@ subroutine f05
183183
!PARSE-TREE: | | | | | | LiteralConstant -> LogicalLiteralConstant
184184
!PARSE-TREE: | | | | | | | bool = 'true'
185185
!PARSE-TREE: | | OmpDirectiveSpecification
186-
!PARSE-TREE: | | | llvm::omp::Directive = declare target
186+
!PARSE-TREE: | | | OmpDirectiveName -> llvm::omp::Directive = declare target
187187
!PARSE-TREE: | | | OmpArgument -> OmpLocator -> OmpObject -> Designator -> DataRef -> Name = 'f05'
188188
!PARSE-TREE: | | | OmpClauseList ->
189189
!PARSE-TREE: ImplicitPart ->
@@ -210,7 +210,7 @@ subroutine f06(x, y)
210210
!PARSE-TREE: | | | | | | LiteralConstant -> LogicalLiteralConstant
211211
!PARSE-TREE: | | | | | | | bool = 'true'
212212
!PARSE-TREE: | | OmpDirectiveSpecification
213-
!PARSE-TREE: | | | llvm::omp::Directive = flush
213+
!PARSE-TREE: | | | OmpDirectiveName -> llvm::omp::Directive = flush
214214
!PARSE-TREE: | | | OmpArgument -> OmpLocator -> OmpObject -> Designator -> DataRef -> Name = 'x'
215215
!PARSE-TREE: | | | OmpArgument -> OmpLocator -> OmpObject -> Designator -> DataRef -> Name = 'y'
216216
!PARSE-TREE: | | | OmpClauseList ->
@@ -237,6 +237,6 @@ subroutine f07
237237
!PARSE-TREE: | | | | | | LiteralConstant -> LogicalLiteralConstant
238238
!PARSE-TREE: | | | | | | | bool = 'true'
239239
!PARSE-TREE: | | OmpDirectiveSpecification
240-
!PARSE-TREE: | | | llvm::omp::Directive = threadprivate
240+
!PARSE-TREE: | | | OmpDirectiveName -> llvm::omp::Directive = threadprivate
241241
!PARSE-TREE: | | | OmpArgument -> OmpLocator -> OmpObject -> Designator -> DataRef -> Name = 't'
242242
!PARSE-TREE: | | | OmpClauseList ->

flang/test/Parser/OpenMP/metadirective-v50.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ subroutine f01
2424
!PARSE-TREE: | | | | | | LiteralConstant -> LogicalLiteralConstant
2525
!PARSE-TREE: | | | | | | | bool = 'true'
2626
!PARSE-TREE: | | OmpDirectiveSpecification
27-
!PARSE-TREE: | | | llvm::omp::Directive = nothing
27+
!PARSE-TREE: | | | OmpDirectiveName -> llvm::omp::Directive = nothing
2828
!PARSE-TREE: | | | OmpClauseList ->
2929
!PARSE-TREE: | OmpClause -> Default -> OmpDefaultClause -> OmpDirectiveSpecification
30-
!PARSE-TREE: | | llvm::omp::Directive = nothing
30+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = nothing
3131
!PARSE-TREE: | | OmpClauseList ->

0 commit comments

Comments
 (0)