Skip to content

Commit 68180d8

Browse files
authored
[flang][OpenMP] Use OmpDirectiveSpecification in standalone directives (#131163)
This uses OmpDirectiveSpecification in the rest of the standalone directives.
1 parent 82912fd commit 68180d8

17 files changed

+263
-155
lines changed

flang/examples/FlangOmpReport/FlangOmpReportVisitor.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,16 @@ std::string OpenMPCounterVisitor::getName(const OpenMPConstruct &c) {
127127
return std::visit(
128128
Fortran::common::visitors{
129129
[&](const OpenMPStandaloneConstruct &c) -> std::string {
130-
return std::visit(
131-
Fortran::common::visitors{
132-
[&](const OpenMPSimpleStandaloneConstruct &d) {
130+
return common::visit(
131+
common::visitors{
132+
[&](const OmpMetadirectiveDirective &d) {
133+
return normalize_construct_name(d.source.ToString());
134+
},
135+
[&](auto &&d) {
133136
const CharBlock &source{
134137
std::get<OmpDirectiveName>(d.v.t).source};
135138
return normalize_construct_name(source.ToString());
136139
},
137-
[&](const auto &c) {
138-
// Get source from the directive or verbatim fields
139-
const CharBlock &source{std::get<0>(c.t).source};
140-
return normalize_construct_name(source.ToString());
141-
},
142140
},
143141
c.u);
144142
},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ class ParseTreeDumper {
484484
NODE(parser, OmpLocatorList)
485485
NODE(parser, OmpReductionSpecifier)
486486
NODE(parser, OmpArgument)
487+
NODE(parser, OmpArgumentList)
487488
NODE(parser, OmpMetadirectiveDirective)
488489
NODE(parser, OmpMatchClause)
489490
NODE(parser, OmpOtherwiseClause)

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3557,6 +3557,11 @@ struct OmpArgument {
35573557
OmpMapperSpecifier, OmpReductionSpecifier>
35583558
u;
35593559
};
3560+
3561+
struct OmpArgumentList {
3562+
WRAPPER_CLASS_BOILERPLATE(OmpArgumentList, std::list<OmpArgument>);
3563+
CharBlock source;
3564+
};
35603565
} // namespace arguments
35613566

35623567
inline namespace traits {
@@ -4511,10 +4516,11 @@ struct OmpDirectiveSpecification {
45114516
llvm::omp::Directive DirId() const { //
45124517
return std::get<OmpDirectiveName>(t).v;
45134518
}
4519+
const OmpArgumentList &Arguments() const;
45144520
const OmpClauseList &Clauses() const;
45154521

45164522
CharBlock source;
4517-
std::tuple<OmpDirectiveName, std::optional<std::list<OmpArgument>>,
4523+
std::tuple<OmpDirectiveName, std::optional<OmpArgumentList>,
45184524
std::optional<OmpClauseList>, Flags>
45194525
t;
45204526
};
@@ -4865,16 +4871,15 @@ struct OmpLoopDirective {
48654871

48664872
// 2.14.2 cancellation-point -> CANCELLATION POINT construct-type-clause
48674873
struct OpenMPCancellationPointConstruct {
4868-
TUPLE_CLASS_BOILERPLATE(OpenMPCancellationPointConstruct);
4874+
WRAPPER_CLASS_BOILERPLATE(
4875+
OpenMPCancellationPointConstruct, OmpDirectiveSpecification);
48694876
CharBlock source;
4870-
std::tuple<Verbatim, OmpClauseList> t;
48714877
};
48724878

48734879
// 2.14.1 cancel -> CANCEL construct-type-clause [ [,] if-clause]
48744880
struct OpenMPCancelConstruct {
4875-
TUPLE_CLASS_BOILERPLATE(OpenMPCancelConstruct);
4881+
WRAPPER_CLASS_BOILERPLATE(OpenMPCancelConstruct, OmpDirectiveSpecification);
48764882
CharBlock source;
4877-
std::tuple<Verbatim, OmpClauseList> t;
48784883
};
48794884

48804885
// Ref: [5.0:254-255], [5.1:287-288], [5.2:322-323]
@@ -4884,9 +4889,8 @@ struct OpenMPCancelConstruct {
48844889
// destroy-clause |
48854890
// update-clause
48864891
struct OpenMPDepobjConstruct {
4887-
TUPLE_CLASS_BOILERPLATE(OpenMPDepobjConstruct);
4892+
WRAPPER_CLASS_BOILERPLATE(OpenMPDepobjConstruct, OmpDirectiveSpecification);
48884893
CharBlock source;
4889-
std::tuple<Verbatim, OmpObject, OmpClause> t;
48904894
};
48914895

48924896
// Ref: [5.2: 200-201]
@@ -4927,11 +4931,8 @@ struct OpenMPDispatchConstruct {
49274931
// ACQ_REL | RELEASE | ACQUIRE | // since 5.0
49284932
// SEQ_CST // since 5.1
49294933
struct OpenMPFlushConstruct {
4930-
TUPLE_CLASS_BOILERPLATE(OpenMPFlushConstruct);
4934+
WRAPPER_CLASS_BOILERPLATE(OpenMPFlushConstruct, OmpDirectiveSpecification);
49314935
CharBlock source;
4932-
std::tuple<Verbatim, std::optional<OmpObjectList>,
4933-
std::optional<OmpClauseList>, /*TrailingClauses=*/bool>
4934-
t;
49354936
};
49364937

49374938
struct OpenMPSimpleStandaloneConstruct {

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,25 @@ Object makeObject(const parser::OmpObject &object,
132132
return makeObject(std::get<parser::Designator>(object.u), semaCtx);
133133
}
134134

135+
ObjectList makeObjects(const parser::OmpArgumentList &objects,
136+
semantics::SemanticsContext &semaCtx) {
137+
return makeList(objects.v, [&](const parser::OmpArgument &arg) {
138+
return common::visit(
139+
common::visitors{
140+
[&](const parser::OmpLocator &locator) -> Object {
141+
if (auto *object = std::get_if<parser::OmpObject>(&locator.u)) {
142+
return makeObject(*object, semaCtx);
143+
}
144+
llvm_unreachable("Expecting object");
145+
},
146+
[](auto &&s) -> Object { //
147+
llvm_unreachable("Expecting object");
148+
},
149+
},
150+
arg.u);
151+
});
152+
}
153+
135154
std::optional<Object> getBaseObject(const Object &object,
136155
semantics::SemanticsContext &semaCtx) {
137156
// If it's just the symbol, then there is no base.

flang/lib/Lower/OpenMP/Clauses.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ inline ObjectList makeObjects(const parser::OmpObjectList &objects,
143143
return makeList(objects.v, makeObjectFn(semaCtx));
144144
}
145145

146+
ObjectList makeObjects(const parser::OmpArgumentList &objects,
147+
semantics::SemanticsContext &semaCtx);
148+
146149
template <typename FuncTy, //
147150
typename ArgTy, //
148151
typename ResultTy = std::invoke_result_t<FuncTy, ArgTy>>

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,22 +3317,16 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
33173317
static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
33183318
semantics::SemanticsContext &semaCtx,
33193319
lower::pft::Evaluation &eval,
3320-
const parser::OpenMPFlushConstruct &flushConstruct) {
3321-
const auto &verbatim = std::get<parser::Verbatim>(flushConstruct.t);
3322-
const auto &objectList =
3323-
std::get<std::optional<parser::OmpObjectList>>(flushConstruct.t);
3324-
const auto &clauseList =
3325-
std::get<std::optional<parser::OmpClauseList>>(flushConstruct.t);
3326-
ObjectList objects =
3327-
objectList ? makeObjects(*objectList, semaCtx) : ObjectList{};
3320+
const parser::OpenMPFlushConstruct &construct) {
3321+
const auto &argumentList = construct.v.Arguments();
3322+
const auto &clauseList = construct.v.Clauses();
3323+
ObjectList objects = makeObjects(argumentList, semaCtx);
33283324
List<Clause> clauses =
3329-
clauseList ? makeList(clauseList->v,
3330-
[&](auto &&s) { return makeClause(s, semaCtx); })
3331-
: List<Clause>{};
3332-
mlir::Location currentLocation = converter.genLocation(verbatim.source);
3325+
makeList(clauseList.v, [&](auto &&s) { return makeClause(s, semaCtx); });
3326+
mlir::Location currentLocation = converter.genLocation(construct.source);
33333327

33343328
ConstructQueue queue{buildConstructQueue(
3335-
converter.getFirOpBuilder().getModule(), semaCtx, eval, verbatim.source,
3329+
converter.getFirOpBuilder().getModule(), semaCtx, eval, construct.source,
33363330
llvm::omp::Directive::OMPD_flush, clauses)};
33373331
genFlushOp(converter, symTable, semaCtx, eval, currentLocation, objects,
33383332
queue, queue.begin());
@@ -3359,11 +3353,12 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
33593353
const parser::OpenMPDepobjConstruct &construct) {
33603354
// These values will be ignored until the construct itself is implemented,
33613355
// but run them anyway for the sake of testing (via a Todo test).
3362-
auto &ompObj = std::get<parser::OmpObject>(construct.t);
3363-
const Object &depObj = makeObject(ompObj, semaCtx);
3364-
Clause clause = makeClause(std::get<parser::OmpClause>(construct.t), semaCtx);
3365-
(void)depObj;
3366-
(void)clause;
3356+
ObjectList objects = makeObjects(construct.v.Arguments(), semaCtx);
3357+
assert(objects.size() == 1);
3358+
List<Clause> clauses = makeClauses(construct.v.Clauses(), semaCtx);
3359+
assert(clauses.size() == 1);
3360+
(void)objects;
3361+
(void)clauses;
33673362

33683363
TODO(converter.getCurrentLocation(), "OpenMPDepobjConstruct");
33693364
}

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ TYPE_PARSER(sourced( //
236236

237237
TYPE_PARSER(construct<OmpLocatorList>(nonemptyList(Parser<OmpLocator>{})))
238238

239+
TYPE_PARSER(sourced( //
240+
construct<OmpArgumentList>(nonemptyList(Parser<OmpArgument>{}))))
241+
239242
TYPE_PARSER( //
240243
construct<OmpTypeSpecifier>(Parser<DeclarationTypeSpec>{}) ||
241244
construct<OmpTypeSpecifier>(Parser<TypeSpec>{}))
@@ -1057,9 +1060,9 @@ TYPE_PARSER(sourced(construct<OmpErrorDirective>(
10571060

10581061
TYPE_PARSER(sourced(construct<OmpDirectiveName>(OmpDirectiveNameParser{})))
10591062

1060-
OmpDirectiveSpecification static makeFlushFromOldSyntax1(Verbatim &&text,
1063+
OmpDirectiveSpecification static makeFlushFromOldSyntax(Verbatim &&text,
10611064
std::optional<OmpClauseList> &&clauses,
1062-
std::optional<std::list<OmpArgument>> &&args,
1065+
std::optional<OmpArgumentList> &&args,
10631066
OmpDirectiveSpecification::Flags &&flags) {
10641067
return OmpDirectiveSpecification{OmpDirectiveName(text), std::move(args),
10651068
std::move(clauses), std::move(flags)};
@@ -1073,15 +1076,15 @@ TYPE_PARSER(sourced(
10731076
// lists absent in the parsed result.
10741077
// E.g. for FLUSH(x) SEQ_CST it would find no clauses following
10751078
// the directive name, parse the argument list "(x)" and stop.
1076-
applyFunction<OmpDirectiveSpecification>(makeFlushFromOldSyntax1,
1079+
applyFunction<OmpDirectiveSpecification>(makeFlushFromOldSyntax,
10771080
verbatim("FLUSH"_tok) / !lookAhead("("_tok),
10781081
maybe(Parser<OmpClauseList>{}),
1079-
maybe(parenthesized(nonemptyList(Parser<OmpArgument>{}))),
1082+
maybe(parenthesized(Parser<OmpArgumentList>{})),
10801083
pure(OmpDirectiveSpecification::Flags::DeprecatedSyntax))) ||
10811084
// Parse the standard syntax: directive [(arguments)] [clauses]
10821085
construct<OmpDirectiveSpecification>( //
10831086
sourced(OmpDirectiveNameParser{}),
1084-
maybe(parenthesized(nonemptyList(Parser<OmpArgument>{}))),
1087+
maybe(parenthesized(Parser<OmpArgumentList>{})),
10851088
maybe(Parser<OmpClauseList>{}),
10861089
pure(OmpDirectiveSpecification::Flags::None))))
10871090

@@ -1157,14 +1160,6 @@ TYPE_PARSER(sourced(construct<OmpLoopDirective>(first(
11571160
TYPE_PARSER(sourced(construct<OmpBeginLoopDirective>(
11581161
sourced(Parser<OmpLoopDirective>{}), Parser<OmpClauseList>{})))
11591162

1160-
// 2.14.2 Cancellation Point construct
1161-
TYPE_PARSER(sourced(construct<OpenMPCancellationPointConstruct>(
1162-
verbatim("CANCELLATION POINT"_tok), Parser<OmpClauseList>{})))
1163-
1164-
// 2.14.1 Cancel construct
1165-
TYPE_PARSER(sourced(construct<OpenMPCancelConstruct>(
1166-
verbatim("CANCEL"_tok), Parser<OmpClauseList>{})))
1167-
11681163
TYPE_PARSER(sourced(construct<OmpFailClause>(
11691164
parenthesized(indirect(Parser<OmpMemoryOrderClause>{})))))
11701165

@@ -1205,29 +1200,6 @@ TYPE_PARSER(sourced(construct<OmpAtomicClause>(
12051200
TYPE_PARSER(sourced(construct<OmpAtomicClauseList>(
12061201
many(maybe(","_tok) >> sourced(Parser<OmpAtomicClause>{})))))
12071202

1208-
TYPE_PARSER(sourced(construct<OpenMPDepobjConstruct>(verbatim("DEPOBJ"_tok),
1209-
parenthesized(Parser<OmpObject>{}), sourced(Parser<OmpClause>{}))))
1210-
1211-
static OpenMPFlushConstruct makeFlushFromOldSyntax(Verbatim &&text,
1212-
std::optional<OmpClauseList> &&clauses,
1213-
std::optional<OmpObjectList> &&objects) {
1214-
bool oldSyntax{
1215-
clauses && !clauses->v.empty() && objects && !objects->v.empty()};
1216-
return OpenMPFlushConstruct{std::move(text), std::move(objects),
1217-
std::move(clauses),
1218-
/*TrailingClauses=*/!oldSyntax};
1219-
}
1220-
1221-
TYPE_PARSER(sourced( //
1222-
construct<OpenMPFlushConstruct>( //
1223-
applyFunction<OpenMPFlushConstruct>(makeFlushFromOldSyntax,
1224-
verbatim("FLUSH"_tok), maybe(Parser<OmpClauseList>{}),
1225-
maybe(parenthesized(Parser<OmpObjectList>{})))) ||
1226-
1227-
construct<OpenMPFlushConstruct>( //
1228-
verbatim("FLUSH"_tok), maybe(parenthesized(Parser<OmpObjectList>{})),
1229-
Parser<OmpClauseList>{}, pure(/*TrailingClauses=*/true))))
1230-
12311203
static bool IsSimpleStandalone(const OmpDirectiveName &name) {
12321204
switch (name.v) {
12331205
case llvm::omp::Directive::OMPD_barrier:
@@ -1249,6 +1221,36 @@ TYPE_PARSER(sourced( //
12491221
predicated(OmpDirectiveNameParser{}, IsSimpleStandalone) >=
12501222
Parser<OmpDirectiveSpecification>{})))
12511223

1224+
static inline constexpr auto IsDirective(llvm::omp::Directive dir) {
1225+
return [dir](const OmpDirectiveName &name) -> bool { return dir == name.v; };
1226+
}
1227+
1228+
TYPE_PARSER(sourced( //
1229+
construct<OpenMPFlushConstruct>(
1230+
predicated(OmpDirectiveNameParser{},
1231+
IsDirective(llvm::omp::Directive::OMPD_flush)) >=
1232+
Parser<OmpDirectiveSpecification>{})))
1233+
1234+
// 2.14.2 Cancellation Point construct
1235+
TYPE_PARSER(sourced( //
1236+
construct<OpenMPCancellationPointConstruct>(
1237+
predicated(OmpDirectiveNameParser{},
1238+
IsDirective(llvm::omp::Directive::OMPD_cancellation_point)) >=
1239+
Parser<OmpDirectiveSpecification>{})))
1240+
1241+
// 2.14.1 Cancel construct
1242+
TYPE_PARSER(sourced( //
1243+
construct<OpenMPCancelConstruct>(
1244+
predicated(OmpDirectiveNameParser{},
1245+
IsDirective(llvm::omp::Directive::OMPD_cancel)) >=
1246+
Parser<OmpDirectiveSpecification>{})))
1247+
1248+
TYPE_PARSER(sourced( //
1249+
construct<OpenMPDepobjConstruct>(
1250+
predicated(OmpDirectiveNameParser{},
1251+
IsDirective(llvm::omp::Directive::OMPD_depobj)) >=
1252+
Parser<OmpDirectiveSpecification>{})))
1253+
12521254
// Standalone Constructs
12531255
TYPE_PARSER(
12541256
sourced( //

flang/lib/Parser/parse-tree.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,14 @@ llvm::omp::Clause OmpClause::Id() const {
337337
return std::visit([](auto &&s) { return getClauseIdForClass(s); }, u);
338338
}
339339

340+
const OmpArgumentList &OmpDirectiveSpecification::Arguments() const {
341+
static OmpArgumentList empty{decltype(OmpArgumentList::v){}};
342+
if (auto &arguments = std::get<std::optional<OmpArgumentList>>(t)) {
343+
return *arguments;
344+
}
345+
return empty;
346+
}
347+
340348
const OmpClauseList &OmpDirectiveSpecification::Clauses() const {
341349
static OmpClauseList empty{decltype(OmpClauseList::v){}};
342350
if (auto &clauses = std::get<std::optional<OmpClauseList>>(t)) {

flang/lib/Parser/unparse.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,8 @@ class UnparseVisitor {
20752075
}
20762076

20772077
// OpenMP Clauses & Directives
2078+
void Unparse(const OmpArgumentList &x) { Walk(x.v, ", "); }
2079+
20782080
void Unparse(const OmpTypeNameList &x) { //
20792081
Walk(x.v, ",");
20802082
}
@@ -2095,8 +2097,7 @@ class UnparseVisitor {
20952097
}
20962098
void Unparse(const OmpDirectiveSpecification &x) {
20972099
auto unparseArgs{[&]() {
2098-
using ArgList = std::list<parser::OmpArgument>;
2099-
if (auto &args{std::get<std::optional<ArgList>>(x.t)}) {
2100+
if (auto &args{std::get<std::optional<OmpArgumentList>>(x.t)}) {
21002101
Put("(");
21012102
Walk(*args);
21022103
Put(")");
@@ -2823,15 +2824,15 @@ class UnparseVisitor {
28232824
}
28242825
void Unparse(const OpenMPCancellationPointConstruct &x) {
28252826
BeginOpenMP();
2826-
Word("!$OMP CANCELLATION POINT ");
2827-
Walk(std::get<OmpClauseList>(x.t));
2827+
Word("!$OMP ");
2828+
Walk(x.v);
28282829
Put("\n");
28292830
EndOpenMP();
28302831
}
28312832
void Unparse(const OpenMPCancelConstruct &x) {
28322833
BeginOpenMP();
2833-
Word("!$OMP CANCEL ");
2834-
Walk(std::get<OmpClauseList>(x.t));
2834+
Word("!$OMP ");
2835+
Walk(x.v);
28352836
Put("\n");
28362837
EndOpenMP();
28372838
}
@@ -2858,23 +2859,21 @@ class UnparseVisitor {
28582859
}
28592860
void Unparse(const OpenMPDepobjConstruct &x) {
28602861
BeginOpenMP();
2861-
Word("!$OMP DEPOBJ");
2862-
Put("(");
2863-
Walk(std::get<OmpObject>(x.t));
2864-
Put(") ");
2865-
Walk(std::get<OmpClause>(x.t));
2862+
Word("!$OMP ");
2863+
Walk(x.v);
28662864
Put("\n");
28672865
EndOpenMP();
28682866
}
28692867
void Unparse(const OpenMPFlushConstruct &x) {
28702868
BeginOpenMP();
28712869
Word("!$OMP FLUSH");
2872-
if (std::get</*ClausesTrailing=*/bool>(x.t)) {
2873-
Walk("(", std::get<std::optional<OmpObjectList>>(x.t), ")");
2874-
Walk(" ", std::get<std::optional<OmpClauseList>>(x.t));
2870+
using Flags = OmpDirectiveSpecification::Flags;
2871+
if (std::get<Flags>(x.v.t) == Flags::DeprecatedSyntax) {
2872+
Walk("(", std::get<std::optional<OmpArgumentList>>(x.v.t), ")");
2873+
Walk(" ", std::get<std::optional<OmpClauseList>>(x.v.t));
28752874
} else {
2876-
Walk(" ", std::get<std::optional<OmpClauseList>>(x.t));
2877-
Walk(" (", std::get<std::optional<OmpObjectList>>(x.t), ")");
2875+
Walk(" ", std::get<std::optional<OmpClauseList>>(x.v.t));
2876+
Walk(" (", std::get<std::optional<OmpArgumentList>>(x.v.t), ")");
28782877
}
28792878
Put("\n");
28802879
EndOpenMP();

0 commit comments

Comments
 (0)