Skip to content

Commit 5ba7a3b

Browse files
authored
[flang][OpenMP] Parse cancel-directive-name as clause (llvm#130146)
The cancellable construct names on CANCEL or CANCELLATION POINT directives are actually clauses (with the same names as the corresponding constructs). Instead of parsing them into a custom structure, parse them as a clause, which will make CANCEL/CANCELLATION POINT follow the same uniform scheme as other constructs (<directive> [(<arguments>)] [clauses]).
1 parent 7e29277 commit 5ba7a3b

File tree

11 files changed

+182
-77
lines changed

11 files changed

+182
-77
lines changed

flang/examples/FeatureList/FeatureList.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,6 @@ struct NodeVisitor {
456456
READ_FEATURE(OmpBeginLoopDirective)
457457
READ_FEATURE(OmpBeginSectionsDirective)
458458
READ_FEATURE(OmpBlockDirective)
459-
READ_FEATURE(OmpCancelType)
460-
READ_FEATURE(OmpCancelType::Type)
461459
READ_FEATURE(OmpClause)
462460
READ_FEATURE(OmpClauseList)
463461
READ_FEATURE(OmpCriticalDirective)
@@ -551,7 +549,6 @@ struct NodeVisitor {
551549
READ_FEATURE(OpenMPAtomicConstruct)
552550
READ_FEATURE(OpenMPBlockConstruct)
553551
READ_FEATURE(OpenMPCancelConstruct)
554-
READ_FEATURE(OpenMPCancelConstruct::If)
555552
READ_FEATURE(OpenMPCancellationPointConstruct)
556553
READ_FEATURE(OpenMPConstruct)
557554
READ_FEATURE(OpenMPCriticalConstruct)

flang/examples/FlangOmpReport/FlangOmpReportVisitor.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,6 @@ void OpenMPCounterVisitor::Post(const OmpDirectiveNameModifier &c) {
265265
clauseDetails +=
266266
"name_modifier=" + llvm::omp::getOpenMPDirectiveName(c.v).str() + ";";
267267
}
268-
void OpenMPCounterVisitor::Post(const OmpCancelType::Type &c) {
269-
clauseDetails += "type=" + std::string{OmpCancelType::EnumToString(c)} + ";";
270-
}
271268
void OpenMPCounterVisitor::Post(const OmpClause &c) {
272269
PostClauseCommon(normalize_clause_name(c.source.ToString()));
273270
clauseDetails.clear();

flang/examples/FlangOmpReport/FlangOmpReportVisitor.h

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ struct OpenMPCounterVisitor {
7878
void Post(const OmpMapType::Value &c);
7979
void Post(const OmpScheduleClause::Kind &c);
8080
void Post(const OmpDirectiveNameModifier &c);
81-
void Post(const OmpCancelType::Type &c);
8281
void Post(const OmpClause &c);
8382
void PostClauseCommon(const ClauseInfo &ci);
8483

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,11 @@ class ParseTreeDumper {
540540
"llvm::omp::Directive = ", llvm::omp::getOpenMPDirectiveName(x))
541541
.str();
542542
}
543-
NODE(parser, OmpCancelType)
544-
NODE_ENUM(OmpCancelType, Type)
545543
NODE(parser, OmpClause)
546544
#define GEN_FLANG_DUMP_PARSE_TREE_CLAUSES
547545
#include "llvm/Frontend/OpenMP/OMP.inc"
548546
NODE(parser, OmpClauseList)
547+
NODE(parser, OmpCancellationConstructTypeClause)
549548
NODE(parser, OmpContainsClause)
550549
NODE(parser, OmpCriticalDirective)
551550
NODE(parser, OmpErrorDirective)
@@ -688,7 +687,6 @@ class ParseTreeDumper {
688687
NODE(parser, OpenMPAtomicConstruct)
689688
NODE(parser, OpenMPBlockConstruct)
690689
NODE(parser, OpenMPCancelConstruct)
691-
NODE(OpenMPCancelConstruct, If)
692690
NODE(parser, OpenMPCancellationPointConstruct)
693691
NODE(parser, OpenMPConstruct)
694692
NODE(parser, OpenMPCriticalConstruct)

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -4047,6 +4047,12 @@ struct OmpBindClause {
40474047
WRAPPER_CLASS_BOILERPLATE(OmpBindClause, Binding);
40484048
};
40494049

4050+
// Artificial clause to represent a cancellable construct.
4051+
struct OmpCancellationConstructTypeClause {
4052+
TUPLE_CLASS_BOILERPLATE(OmpCancellationConstructTypeClause);
4053+
std::tuple<OmpDirectiveName, std::optional<ScalarLogicalExpr>> t;
4054+
};
4055+
40504056
// Ref: [5.2:214]
40514057
//
40524058
// contains-clause ->
@@ -4859,26 +4865,18 @@ struct OmpLoopDirective {
48594865
CharBlock source;
48604866
};
48614867

4862-
// 2.14.1 construct-type-clause -> PARALLEL | SECTIONS | DO | TASKGROUP
4863-
struct OmpCancelType {
4864-
ENUM_CLASS(Type, Parallel, Sections, Do, Taskgroup)
4865-
WRAPPER_CLASS_BOILERPLATE(OmpCancelType, Type);
4866-
CharBlock source;
4867-
};
4868-
48694868
// 2.14.2 cancellation-point -> CANCELLATION POINT construct-type-clause
48704869
struct OpenMPCancellationPointConstruct {
48714870
TUPLE_CLASS_BOILERPLATE(OpenMPCancellationPointConstruct);
48724871
CharBlock source;
4873-
std::tuple<Verbatim, OmpCancelType> t;
4872+
std::tuple<Verbatim, OmpClauseList> t;
48744873
};
48754874

48764875
// 2.14.1 cancel -> CANCEL construct-type-clause [ [,] if-clause]
48774876
struct OpenMPCancelConstruct {
48784877
TUPLE_CLASS_BOILERPLATE(OpenMPCancelConstruct);
4879-
WRAPPER_CLASS(If, ScalarLogicalExpr);
48804878
CharBlock source;
4881-
std::tuple<Verbatim, OmpCancelType, std::optional<If>> t;
4879+
std::tuple<Verbatim, OmpClauseList> t;
48824880
};
48834881

48844882
// Ref: [5.0:254-255], [5.1:287-288], [5.2:322-323]

flang/lib/Parser/openmp-parsers.cpp

+39-14
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ template <typename Parser> constexpr auto unwrap(const Parser &p) {
5050
return UnwrapParser<Parser>(p);
5151
}
5252

53+
// Check (without advancing the parsing location) if the next thing in the
54+
// input would be accepted by the "checked" parser, and if so, run the "parser"
55+
// parser.
56+
// The intended use is with the "checker" parser being some token, followed
57+
// by a more complex parser that consumes the token plus more things, e.g.
58+
// "PARALLEL"_id >= Parser<OmpDirectiveSpecification>{}.
59+
//
60+
// The >= has a higher precedence than ||, so it can be used just like >>
61+
// in an alternatives parser without parentheses.
62+
template <typename PA, typename PB>
63+
constexpr auto operator>=(PA checker, PB parser) {
64+
return lookAhead(checker) >> parser;
65+
}
66+
5367
/// Parse OpenMP directive name (this includes compound directives).
5468
struct OmpDirectiveNameParser {
5569
using resultType = OmpDirectiveName;
@@ -575,6 +589,9 @@ TYPE_PARSER(construct<OmpAffinityClause>(
575589
maybe(nonemptyList(Parser<OmpAffinityClause::Modifier>{}) / ":"),
576590
Parser<OmpObjectList>{}))
577591

592+
TYPE_PARSER(construct<OmpCancellationConstructTypeClause>(
593+
OmpDirectiveNameParser{}, maybe(parenthesized(scalarLogicalExpr))))
594+
578595
// 2.15.3.1 DEFAULT (PRIVATE | FIRSTPRIVATE | SHARED | NONE)
579596
TYPE_PARSER(construct<OmpDefaultClause::DataSharingAttribute>(
580597
"PRIVATE" >> pure(OmpDefaultClause::DataSharingAttribute::Private) ||
@@ -804,8 +821,9 @@ TYPE_PARSER(construct<OmpAbsentClause>(many(maybe(","_tok) >>
804821
TYPE_PARSER(construct<OmpContainsClause>(many(maybe(","_tok) >>
805822
construct<llvm::omp::Directive>(unwrap(OmpDirectiveNameParser{})))))
806823

807-
TYPE_PARSER("ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
808-
parenthesized(Parser<OmpAbsentClause>{}))) ||
824+
TYPE_PARSER( //
825+
"ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
826+
parenthesized(Parser<OmpAbsentClause>{}))) ||
809827
"ACQUIRE" >> construct<OmpClause>(construct<OmpClause::Acquire>()) ||
810828
"ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
811829
"AFFINITY" >> construct<OmpClause>(construct<OmpClause::Affinity>(
@@ -982,7 +1000,20 @@ TYPE_PARSER("ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
9821000
"UPDATE" >> construct<OmpClause>(construct<OmpClause::Update>(
9831001
parenthesized(Parser<OmpUpdateClause>{}))) ||
9841002
"WHEN" >> construct<OmpClause>(construct<OmpClause::When>(
985-
parenthesized(Parser<OmpWhenClause>{}))))
1003+
parenthesized(Parser<OmpWhenClause>{}))) ||
1004+
// Cancellable constructs
1005+
"DO"_id >=
1006+
construct<OmpClause>(construct<OmpClause::CancellationConstructType>(
1007+
Parser<OmpCancellationConstructTypeClause>{})) ||
1008+
"PARALLEL"_id >=
1009+
construct<OmpClause>(construct<OmpClause::CancellationConstructType>(
1010+
Parser<OmpCancellationConstructTypeClause>{})) ||
1011+
"SECTIONS"_id >=
1012+
construct<OmpClause>(construct<OmpClause::CancellationConstructType>(
1013+
Parser<OmpCancellationConstructTypeClause>{})) ||
1014+
"TASKGROUP"_id >=
1015+
construct<OmpClause>(construct<OmpClause::CancellationConstructType>(
1016+
Parser<OmpCancellationConstructTypeClause>{})))
9861017

9871018
// [Clause, [Clause], ...]
9881019
TYPE_PARSER(sourced(construct<OmpClauseList>(
@@ -1096,20 +1127,13 @@ TYPE_PARSER(sourced(construct<OmpLoopDirective>(first(
10961127
TYPE_PARSER(sourced(construct<OmpBeginLoopDirective>(
10971128
sourced(Parser<OmpLoopDirective>{}), Parser<OmpClauseList>{})))
10981129

1099-
// 2.14.1 construct-type-clause -> PARALLEL | SECTIONS | DO | TASKGROUP
1100-
TYPE_PARSER(sourced(construct<OmpCancelType>(
1101-
first("PARALLEL" >> pure(OmpCancelType::Type::Parallel),
1102-
"SECTIONS" >> pure(OmpCancelType::Type::Sections),
1103-
"DO" >> pure(OmpCancelType::Type::Do),
1104-
"TASKGROUP" >> pure(OmpCancelType::Type::Taskgroup)))))
1105-
11061130
// 2.14.2 Cancellation Point construct
11071131
TYPE_PARSER(sourced(construct<OpenMPCancellationPointConstruct>(
1108-
verbatim("CANCELLATION POINT"_tok), Parser<OmpCancelType>{})))
1132+
verbatim("CANCELLATION POINT"_tok), Parser<OmpClauseList>{})))
11091133

11101134
// 2.14.1 Cancel construct
1111-
TYPE_PARSER(sourced(construct<OpenMPCancelConstruct>(verbatim("CANCEL"_tok),
1112-
Parser<OmpCancelType>{}, maybe("IF" >> parenthesized(scalarLogicalExpr)))))
1135+
TYPE_PARSER(sourced(construct<OpenMPCancelConstruct>(
1136+
verbatim("CANCEL"_tok), Parser<OmpClauseList>{})))
11131137

11141138
TYPE_PARSER(sourced(construct<OmpFailClause>(
11151139
parenthesized(indirect(Parser<OmpMemoryOrderClause>{})))))
@@ -1193,9 +1217,10 @@ TYPE_PARSER(
11931217
sourced(construct<OpenMPStandaloneConstruct>(
11941218
Parser<OpenMPSimpleStandaloneConstruct>{}) ||
11951219
construct<OpenMPStandaloneConstruct>(Parser<OpenMPFlushConstruct>{}) ||
1196-
construct<OpenMPStandaloneConstruct>(Parser<OpenMPCancelConstruct>{}) ||
1220+
// Try CANCELLATION POINT before CANCEL.
11971221
construct<OpenMPStandaloneConstruct>(
11981222
Parser<OpenMPCancellationPointConstruct>{}) ||
1223+
construct<OpenMPStandaloneConstruct>(Parser<OpenMPCancelConstruct>{}) ||
11991224
construct<OpenMPStandaloneConstruct>(
12001225
Parser<OmpMetadirectiveDirective>{}) ||
12011226
construct<OpenMPStandaloneConstruct>(Parser<OpenMPDepobjConstruct>{})) /

flang/lib/Parser/unparse.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -2851,15 +2851,14 @@ class UnparseVisitor {
28512851
void Unparse(const OpenMPCancellationPointConstruct &x) {
28522852
BeginOpenMP();
28532853
Word("!$OMP CANCELLATION POINT ");
2854-
Walk(std::get<OmpCancelType>(x.t));
2854+
Walk(std::get<OmpClauseList>(x.t));
28552855
Put("\n");
28562856
EndOpenMP();
28572857
}
28582858
void Unparse(const OpenMPCancelConstruct &x) {
28592859
BeginOpenMP();
28602860
Word("!$OMP CANCEL ");
2861-
Walk(std::get<OmpCancelType>(x.t));
2862-
Walk(std::get<std::optional<OpenMPCancelConstruct::If>>(x.t));
2861+
Walk(std::get<OmpClauseList>(x.t));
28632862
Put("\n");
28642863
EndOpenMP();
28652864
}
@@ -3034,7 +3033,6 @@ class UnparseVisitor {
30343033
OmpDeviceTypeClause, DeviceTypeDescription) // OMP device_type
30353034
WALK_NESTED_ENUM(OmpReductionModifier, Value) // OMP reduction-modifier
30363035
WALK_NESTED_ENUM(OmpExpectation, Value) // OMP motion-expectation
3037-
WALK_NESTED_ENUM(OmpCancelType, Type) // OMP cancel-type
30383036
WALK_NESTED_ENUM(OmpOrderClause, Ordering) // OMP ordering
30393037
WALK_NESTED_ENUM(OmpOrderModifier, Value) // OMP order-modifier
30403038
WALK_NESTED_ENUM(OmpPrescriptiveness, Value) // OMP prescriptiveness

0 commit comments

Comments
 (0)