Skip to content

Commit

Permalink
[flang] [OpenMP] parse tree changes for OpenMPBlockConstruct (flang…
Browse files Browse the repository at this point in the history
…-compiler/f18#632)

* [OpenMP] parse tree changes for `OpenMPBlockConstruct`

1. merge `Workshare` and `Single` into `OpenMPBlockConstruct` because
   they both accept structured-block and syntax is similar to other block
   directives.

2. `OpenMPBlockConstruct` changes to structure like `{Begin, Block, End}`,
   where `Begin` and `End` are tuple of `{Directive, ClauseList}`.

3. Updated the check-omp-structure.* for necessary parts. Added all the END
   directive enumeration types that may have clauses.

More tests will be added during Semantics.

* [OpenMP] Update on Tim's suggestion

1. Fix unspecified enumeration for `OmpDirective` in the `OmpContext`.
   This is through getting rid of `PushContext(source)` function to
   make sure whenever it is about to push a NEW context, directive
   source location and enumeration are available. To do that, I moved
   around all the switches for directive into high level `Construct`'s
   `Enter` node. Besides fixing the issue, the side benefit is that
   whenever we call `GetContext().directive`, we are sure that the
   `directive` here was set already.

2. When `Enter` the `OmpEndBlockDirective` node, partial context
   information, such as directive source location or legal clause lists,
   needs to be reset. The new directive source location should be
   `OmpEndBlockDirective`'s `source`. The enumeration `directive`
   should not be reset for the END directives that do not accept
   clauses because nothing needs to be checked (for example any clause
   that is on `END PARALLEL` is illegal).

Original-commit: flang-compiler/f18@e5bd6b7
Reviewed-on: flang-compiler/f18#632
  • Loading branch information
gpupuck committed Aug 9, 2019
1 parent 3020151 commit b41d10b
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 275 deletions.
4 changes: 1 addition & 3 deletions flang/lib/parser/dump-parse-tree.h
Expand Up @@ -412,6 +412,7 @@ class ParseTreeDumper {
NODE(parser, OmpAtomicRead)
NODE(parser, OmpAtomicUpdate)
NODE(parser, OmpAtomicWrite)
NODE(parser, OmpBeginBlockDirective)
NODE(parser, OmpBlockDirective)
NODE_ENUM(parser::OmpBlockDirective, Directive)
NODE(parser, OmpCancelType)
Expand Down Expand Up @@ -470,7 +471,6 @@ class ParseTreeDumper {
NODE(parser, OmpEndDoSimd)
NODE(parser, OmpEndParallelSections)
NODE(parser, OmpEndSections)
NODE(parser, OmpEndSingle)
NODE(parser, OmpIfClause)
NODE_ENUM(parser::OmpIfClause, DirectiveNameModifier)
NODE(parser, OmpLinearClause)
Expand Down Expand Up @@ -533,10 +533,8 @@ class ParseTreeDumper {
NODE(parser, OpenMPSimpleStandaloneConstruct)
NODE(parser, OpenMPStandaloneConstruct)
NODE(parser, OpenMPSectionsConstruct)
NODE(parser, OpenMPSingleConstruct)
NODE(parser, OpenMPThreadprivate)
NODE(parser, OpenMPParallelSectionsConstruct)
NODE(parser, OpenMPWorkshareConstruct)
NODE(parser, OpenStmt)
NODE(parser, Optional)
NODE(parser, OptionalStmt)
Expand Down
39 changes: 15 additions & 24 deletions flang/lib/parser/openmp-grammar.h
Expand Up @@ -335,19 +335,24 @@ TYPE_PARSER(
endOfLine)

// Directives enclosing structured-block
TYPE_PARSER(sourced(construct<OmpBlockDirective>(
TYPE_PARSER(construct<OmpBlockDirective>(
first("MASTER" >> pure(OmpBlockDirective::Directive::Master),
"ORDERED" >> pure(OmpBlockDirective::Directive::Ordered),
"PARALLEL WORKSHARE" >>
pure(OmpBlockDirective::Directive::ParallelWorkshare),
"PARALLEL" >> pure(OmpBlockDirective::Directive::Parallel),
"SINGLE" >> pure(OmpBlockDirective::Directive::Single),
"TARGET DATA" >> pure(OmpBlockDirective::Directive::TargetData),
"TARGET PARALLEL" >> pure(OmpBlockDirective::Directive::TargetParallel),
"TARGET TEAMS" >> pure(OmpBlockDirective::Directive::TargetTeams),
"TARGET" >> pure(OmpBlockDirective::Directive::Target),
"TASK"_id >> pure(OmpBlockDirective::Directive::Task),
"TASKGROUP" >> pure(OmpBlockDirective::Directive::Taskgroup),
"TEAMS" >> pure(OmpBlockDirective::Directive::Teams)))))
"TEAMS" >> pure(OmpBlockDirective::Directive::Teams),
"WORKSHARE" >> pure(OmpBlockDirective::Directive::Workshare))))

TYPE_PARSER(construct<OmpBeginBlockDirective>(
sourced(Parser<OmpBlockDirective>{}), Parser<OmpClauseList>{}))

TYPE_PARSER(construct<OmpReductionInitializerClause>(
"INITIALIZER" >> parenthesized("OMP_PRIV =" >> expr)))
Expand Down Expand Up @@ -473,23 +478,9 @@ TYPE_PARSER(startOmpLine >>
endOmpLine)

// Block Construct
TYPE_PARSER(construct<OpenMPBlockConstruct>(Parser<OmpBlockDirective>{},
Parser<OmpClauseList>{} / endOmpLine, block,
Parser<OmpEndBlockDirective>{}))

// OMP SINGLE
TYPE_PARSER(startOmpLine >> construct<OmpEndSingle>(verbatim("END SINGLE"_tok),
Parser<OmpClauseList>{}) /
endOmpLine)

TYPE_PARSER(construct<OpenMPSingleConstruct>(verbatim("SINGLE"_tok),
Parser<OmpClauseList>{} / endOmpLine, block, Parser<OmpEndSingle>{}))

// OMP WORKSHARE
TYPE_PARSER(construct<OpenMPWorkshareConstruct>(
verbatim("WORKSHARE"_tok) / endOmpLine, block,
startOmpLine >> "END WORKSHARE"_tok >>
maybe(construct<OmpNowait>("NOWAIT"_tok) / endOmpLine)))
TYPE_PARSER(construct<OpenMPBlockConstruct>(
Parser<OmpBeginBlockDirective>{} / endOmpLine, block,
Parser<OmpEndBlockDirective>{} / endOmpLine))

// OMP END DO SIMD [NOWAIT]
TYPE_PARSER(construct<OmpEndDoSimd>(maybe(construct<OmpNowait>("NOWAIT"_tok))))
Expand Down Expand Up @@ -519,11 +510,9 @@ TYPE_PARSER(construct<OmpSection>(verbatim("SECTION"_tok) / endOmpLine))

TYPE_CONTEXT_PARSER("OpenMP construct"_en_US,
startOmpLine >>
first(construct<OpenMPConstruct>(Parser<OpenMPSingleConstruct>{}),
construct<OpenMPConstruct>(Parser<OpenMPSectionsConstruct>{}),
first(construct<OpenMPConstruct>(Parser<OpenMPSectionsConstruct>{}),
construct<OpenMPConstruct>(
Parser<OpenMPParallelSectionsConstruct>{}),
construct<OpenMPConstruct>(Parser<OpenMPWorkshareConstruct>{}),
construct<OpenMPConstruct>(Parser<OpenMPLoopConstruct>{}),
construct<OpenMPConstruct>(Parser<OpenMPBlockConstruct>{}),
// OpenMPBlockConstruct is attempted before
Expand All @@ -534,8 +523,10 @@ TYPE_CONTEXT_PARSER("OpenMP construct"_en_US,
construct<OpenMPConstruct>(Parser<OmpSection>{})))

// END OMP Block directives
TYPE_PARSER(startOmpLine >> "END"_tok >>
construct<OmpEndBlockDirective>(Parser<OmpBlockDirective>{}) / endOmpLine)
TYPE_PARSER(
startOmpLine >> construct<OmpEndBlockDirective>(
sourced("END"_tok >> Parser<OmpBlockDirective>{}),
Parser<OmpClauseList>{}))

// END OMP Loop directives
TYPE_PARSER(startOmpLine >> "END"_tok >>
Expand Down
43 changes: 18 additions & 25 deletions flang/lib/parser/parse-tree.h
Expand Up @@ -3481,26 +3481,11 @@ struct OpenMPParallelSectionsConstruct {
std::tuple<Verbatim, OmpClauseList, Block, OmpEndParallelSections> t;
};

// WORKSHARE
struct OpenMPWorkshareConstruct {
TUPLE_CLASS_BOILERPLATE(OpenMPWorkshareConstruct);
std::tuple<Verbatim, Block, std::optional<OmpNowait>> t;
};

// SINGLE
struct OmpEndSingle {
TUPLE_CLASS_BOILERPLATE(OmpEndSingle);
std::tuple<Verbatim, OmpClauseList> t;
};
struct OpenMPSingleConstruct {
TUPLE_CLASS_BOILERPLATE(OpenMPSingleConstruct);
std::tuple<Verbatim, OmpClauseList, Block, OmpEndSingle> t;
};

// OpenMP directive beginning or ending a block
struct OmpBlockDirective {
ENUM_CLASS(Directive, Master, Ordered, Parallel, ParallelWorkshare, Target,
TargetData, TargetParallel, TargetTeams, Task, Taskgroup, Teams);
ENUM_CLASS(Directive, Master, Ordered, Parallel, ParallelWorkshare, Single,
Target, TargetData, TargetParallel, TargetTeams, Task, Taskgroup, Teams,
Workshare);
WRAPPER_CLASS_BOILERPLATE(OmpBlockDirective, Directive);
CharBlock source;
};
Expand Down Expand Up @@ -3709,8 +3694,6 @@ struct OpenMPStandaloneConstruct {
u;
};

WRAPPER_CLASS(OmpEndBlockDirective, OmpBlockDirective);

// DO / DO SIMD
WRAPPER_CLASS(OmpEndDoSimd, std::optional<OmpNowait>);
WRAPPER_CLASS(OmpEndDo, std::optional<OmpNowait>);
Expand All @@ -3719,9 +3702,19 @@ struct OpenMPEndLoopDirective {
std::variant<OmpEndDoSimd, OmpEndDo, OmpLoopDirective> u;
};

struct OmpBeginBlockDirective {
TUPLE_CLASS_BOILERPLATE(OmpBeginBlockDirective);
std::tuple<OmpBlockDirective, OmpClauseList> t;
};

struct OmpEndBlockDirective {
TUPLE_CLASS_BOILERPLATE(OmpEndBlockDirective);
std::tuple<OmpBlockDirective, OmpClauseList> t;
};

struct OpenMPBlockConstruct {
TUPLE_CLASS_BOILERPLATE(OpenMPBlockConstruct);
std::tuple<OmpBlockDirective, OmpClauseList, Block, OmpEndBlockDirective> t;
std::tuple<OmpBeginBlockDirective, Block, OmpEndBlockDirective> t;
};

struct OpenMPLoopConstruct {
Expand All @@ -3731,10 +3724,10 @@ struct OpenMPLoopConstruct {

struct OpenMPConstruct {
UNION_CLASS_BOILERPLATE(OpenMPConstruct);
std::variant<OpenMPStandaloneConstruct, OpenMPSingleConstruct,
OpenMPSectionsConstruct, OpenMPParallelSectionsConstruct,
OpenMPWorkshareConstruct, OpenMPLoopConstruct, OpenMPBlockConstruct,
OpenMPAtomicConstruct, OpenMPCriticalConstruct, OmpSection>
std::variant<OpenMPStandaloneConstruct, OpenMPSectionsConstruct,
OpenMPParallelSectionsConstruct, OpenMPLoopConstruct,
OpenMPBlockConstruct, OpenMPAtomicConstruct, OpenMPCriticalConstruct,
OmpSection>
u;
};
}
Expand Down
30 changes: 3 additions & 27 deletions flang/lib/parser/unparse.cc
Expand Up @@ -2079,6 +2079,7 @@ class UnparseVisitor {
Word("PARALLEL WORKSHARE ");
break;
case OmpBlockDirective::Directive::Parallel: Word("PARALLEL "); break;
case OmpBlockDirective::Directive::Single: Word("SINGLE "); break;
case OmpBlockDirective::Directive::TargetData: Word("TARGET DATA "); break;
case OmpBlockDirective::Directive::TargetParallel:
Word("TARGET PARALLEL ");
Expand All @@ -2090,6 +2091,7 @@ class UnparseVisitor {
case OmpBlockDirective::Directive::Taskgroup: Word("TASKGROUP "); break;
case OmpBlockDirective::Directive::Task: Word("TASK "); break;
case OmpBlockDirective::Directive::Teams: Word("TEAMS "); break;
case OmpBlockDirective::Directive::Workshare: Word("WORKSHARE "); break;
}
}
void Unparse(const OmpMemoryClause &x) {
Expand Down Expand Up @@ -2266,19 +2268,6 @@ class UnparseVisitor {
Walk(x.v);
EndOpenMP();
}
void Unparse(const OpenMPSingleConstruct &x) {
BeginOpenMP();
Word("!$OMP SINGLE");
Walk(std::get<OmpClauseList>(x.t));
EndOpenMP();
Put("\n");
Walk(std::get<Block>(x.t), "");
BeginOpenMP();
Word("!$OMP END SINGLE ");
Walk(std::get<OmpEndSingle>(x.t));
Put("\n");
EndOpenMP();
}
void Unparse(const OmpSection &x) {
BeginOpenMP();
Word("!$OMP SECTION");
Expand Down Expand Up @@ -2309,18 +2298,6 @@ class UnparseVisitor {
Put("\n");
EndOpenMP();
}
void Unparse(const OpenMPWorkshareConstruct &x) {
BeginOpenMP();
Word("!$OMP WORKSHARE");
Put("\n");
EndOpenMP();
Walk(std::get<Block>(x.t), "");
BeginOpenMP();
Word("!$OMP END WORKSHARE ");
Walk(std::get<std::optional<OmpNowait>>(x.t));
Put("\n");
EndOpenMP();
}
void Unparse(const OpenMPCancellationPointConstruct &x) {
BeginOpenMP();
Word("!$OMP CANCELLATION POINT ");
Expand Down Expand Up @@ -2364,8 +2341,7 @@ class UnparseVisitor {
void Unparse(const OpenMPBlockConstruct &x) {
BeginOpenMP();
Word("!$OMP ");
Walk(std::get<OmpBlockDirective>(x.t));
Walk(std::get<OmpClauseList>(x.t));
Walk(std::get<OmpBeginBlockDirective>(x.t));
Put("\n");
EndOpenMP();
Walk(std::get<Block>(x.t), "");
Expand Down

0 comments on commit b41d10b

Please sign in to comment.