Skip to content

Commit 4fc1141

Browse files
authored
[flang][OpenMP] Apply modifier representation to semantic checks (#116658)
Also, define helper macros in parse-tree.h. Apply the new modifier representation to the DEFAULTMAP and REDUCTION clauses, with testcases utilizing the new modifier validation. OpenMP modifier overhaul: #3/3
1 parent 29afbd5 commit 4fc1141

18 files changed

+240
-133
lines changed

flang/examples/FeatureList/FeatureList.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ struct NodeVisitor {
468468
READ_FEATURE(OmpDefaultClause::Type)
469469
READ_FEATURE(OmpDefaultmapClause)
470470
READ_FEATURE(OmpDefaultmapClause::ImplicitBehavior)
471-
READ_FEATURE(OmpDefaultmapClause::VariableCategory)
471+
READ_FEATURE(OmpVariableCategory::Value)
472472
READ_FEATURE(OmpDependClause)
473473
READ_FEATURE(OmpDependClause::TaskDep)
474474
READ_FEATURE(OmpDoacross::Sink)

flang/examples/FlangOmpReport/FlangOmpReportVisitor.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,9 @@ void OpenMPCounterVisitor::Post(
208208
"implicit_behavior=" + std::string{OmpDefaultmapClause::EnumToString(c)} +
209209
";";
210210
}
211-
void OpenMPCounterVisitor::Post(
212-
const OmpDefaultmapClause::VariableCategory &c) {
211+
void OpenMPCounterVisitor::Post(const OmpVariableCategory::Value &c) {
213212
clauseDetails +=
214-
"variable_category=" + std::string{OmpDefaultmapClause::EnumToString(c)} +
213+
"variable_category=" + std::string{OmpVariableCategory::EnumToString(c)} +
215214
";";
216215
}
217216
void OpenMPCounterVisitor::Post(const OmpScheduleModifierType::ModType &c) {

flang/examples/FlangOmpReport/FlangOmpReportVisitor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct OpenMPCounterVisitor {
6969
void Post(const OmpProcBindClause::Type &c);
7070
void Post(const OmpDefaultClause::Type &c);
7171
void Post(const OmpDefaultmapClause::ImplicitBehavior &c);
72-
void Post(const OmpDefaultmapClause::VariableCategory &c);
72+
void Post(const OmpVariableCategory::Value &c);
7373
void Post(const OmpDeviceTypeClause::Type &c);
7474
void Post(const OmpScheduleModifierType::ModType &c);
7575
void Post(const OmpLinearModifier::Value &c);

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,11 @@ class ParseTreeDumper {
509509
NODE(parser, OmpDeclareMapperSpecifier)
510510
NODE(parser, OmpDefaultClause)
511511
NODE_ENUM(OmpDefaultClause, Type)
512+
NODE(parser, OmpVariableCategory)
513+
NODE_ENUM(OmpVariableCategory, Value)
512514
NODE(parser, OmpDefaultmapClause)
513515
NODE_ENUM(OmpDefaultmapClause, ImplicitBehavior)
514-
NODE_ENUM(OmpDefaultmapClause, VariableCategory)
516+
NODE(OmpDefaultmapClause, Modifier)
515517
NODE(parser, OmpDependenceType)
516518
NODE_ENUM(OmpDependenceType, Value)
517519
NODE(parser, OmpTaskDependenceType)
@@ -568,8 +570,10 @@ class ParseTreeDumper {
568570
NODE_ENUM(OmpBindClause, Type)
569571
NODE(parser, OmpProcBindClause)
570572
NODE_ENUM(OmpProcBindClause, Type)
571-
NODE_ENUM(OmpReductionClause, ReductionModifier)
573+
NODE(parser, OmpReductionModifier)
574+
NODE_ENUM(OmpReductionModifier, Value)
572575
NODE(parser, OmpReductionClause)
576+
NODE(OmpReductionClause, Modifier)
573577
NODE(parser, OmpInReductionClause)
574578
NODE(parser, OmpReductionCombiner)
575579
NODE(OmpReductionCombiner, FunctionCombiner)

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

+40-9
Original file line numberDiff line numberDiff line change
@@ -3440,6 +3440,16 @@ struct OmpObject {
34403440

34413441
WRAPPER_CLASS(OmpObjectList, std::list<OmpObject>);
34423442

3443+
#define MODIFIER_BOILERPLATE(...) \
3444+
struct Modifier { \
3445+
using Variant = std::variant<__VA_ARGS__>; \
3446+
UNION_CLASS_BOILERPLATE(Modifier); \
3447+
CharBlock source; \
3448+
Variant u; \
3449+
}
3450+
3451+
#define MODIFIERS() std::optional<std::list<Modifier>>
3452+
34433453
inline namespace modifier {
34443454
// For uniformity, in all keyword modifiers the name of the type defined
34453455
// by ENUM_CLASS is "Value", e.g.
@@ -3505,12 +3515,20 @@ struct OmpLinearModifier {
35053515
// - | // since 4.5, until 5.2
35063516
// + | * | .AND. | .OR. | .EQV. | .NEQV. | // since 4.5
35073517
// MIN | MAX | IAND | IOR | IEOR // since 4.5
3508-
//
35093518
struct OmpReductionIdentifier {
35103519
UNION_CLASS_BOILERPLATE(OmpReductionIdentifier);
35113520
std::variant<DefinedOperator, ProcedureDesignator> u;
35123521
};
35133522

3523+
// Ref: [5.0:300-302], [5.1:332-334], [5.2:134-137]
3524+
//
3525+
// reduction-modifier ->
3526+
// DEFAULT | INSCAN | TASK // since 5.0
3527+
struct OmpReductionModifier {
3528+
ENUM_CLASS(Value, Default, Inscan, Task);
3529+
WRAPPER_CLASS_BOILERPLATE(OmpReductionModifier, Value);
3530+
};
3531+
35143532
// Ref: [4.5:169-170], [5.0:254-256], [5.1:287-289], [5.2:321]
35153533
//
35163534
// task-dependence-type -> // "dependence-type" in 5.1 and before
@@ -3521,6 +3539,17 @@ struct OmpTaskDependenceType {
35213539
ENUM_CLASS(Value, In, Out, Inout, Inoutset, Mutexinoutset, Depobj)
35223540
WRAPPER_CLASS_BOILERPLATE(OmpTaskDependenceType, Value);
35233541
};
3542+
3543+
// Ref: [4.5:229-230], [5.0:324-325], [5.1:357-358], [5.2:161-162]
3544+
//
3545+
// variable-category ->
3546+
// SCALAR | // since 4.5
3547+
// AGGREGATE | ALLOCATABLE | POINTER | // since 5.0
3548+
// ALL // since 5.2
3549+
struct OmpVariableCategory {
3550+
ENUM_CLASS(Value, Aggregate, All, Allocatable, Pointer, Scalar)
3551+
WRAPPER_CLASS_BOILERPLATE(OmpVariableCategory, Value);
3552+
};
35243553
} // namespace modifier
35253554

35263555
// --- Clauses
@@ -3578,8 +3607,8 @@ struct OmpDefaultmapClause {
35783607
TUPLE_CLASS_BOILERPLATE(OmpDefaultmapClause);
35793608
ENUM_CLASS(
35803609
ImplicitBehavior, Alloc, To, From, Tofrom, Firstprivate, None, Default)
3581-
ENUM_CLASS(VariableCategory, All, Scalar, Aggregate, Allocatable, Pointer)
3582-
std::tuple<ImplicitBehavior, std::optional<VariableCategory>> t;
3610+
MODIFIER_BOILERPLATE(OmpVariableCategory);
3611+
std::tuple<ImplicitBehavior, MODIFIERS()> t;
35833612
};
35843613

35853614
// 2.13.9 iteration-offset -> +/- non-negative-constant
@@ -3775,14 +3804,16 @@ struct OmpProcBindClause {
37753804
WRAPPER_CLASS_BOILERPLATE(OmpProcBindClause, Type);
37763805
};
37773806

3778-
// 2.15.3.6 reduction-clause -> REDUCTION (reduction-identifier:
3779-
// variable-name-list)
3807+
// Ref: [4.5:201-207], [5.0:300-302], [5.1:332-334], [5.2:134-137]
3808+
//
3809+
// reduction-clause ->
3810+
// REDUCTION(reduction-identifier: list) | // since 4.5
3811+
// REDUCTION([reduction-modifier,]
3812+
// reduction-identifier: list) // since 5.0
37803813
struct OmpReductionClause {
37813814
TUPLE_CLASS_BOILERPLATE(OmpReductionClause);
3782-
ENUM_CLASS(ReductionModifier, Inscan, Task, Default)
3783-
std::tuple<std::optional<ReductionModifier>, OmpReductionIdentifier,
3784-
OmpObjectList>
3785-
t;
3815+
MODIFIER_BOILERPLATE(OmpReductionModifier, OmpReductionIdentifier);
3816+
std::tuple<MODIFIERS(), OmpObjectList> t;
37863817
};
37873818

37883819
// 2.7.1 sched-modifier -> MONOTONIC | NONMONOTONIC | SIMD

flang/include/flang/Semantics/openmp-modifiers.h

+4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpLinearModifier>();
7070
template <>
7171
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpReductionIdentifier>();
7272
template <>
73+
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpReductionModifier>();
74+
template <>
7375
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpTaskDependenceType>();
76+
template <>
77+
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpVariableCategory>();
7478

7579
// Explanation of terminology:
7680
//

flang/lib/Lower/OpenMP/Clauses.cpp

+19-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "flang/Optimizer/Builder/Todo.h"
1414
#include "flang/Parser/parse-tree.h"
1515
#include "flang/Semantics/expression.h"
16+
#include "flang/Semantics/openmp-modifiers.h"
1617
#include "flang/Semantics/symbol.h"
1718

1819
#include "llvm/Frontend/OpenMP/OMPConstants.h"
@@ -572,7 +573,8 @@ Defaultmap make(const parser::OmpClause::Defaultmap &inp,
572573
);
573574

574575
CLAUSET_ENUM_CONVERT( //
575-
convert2, wrapped::VariableCategory, Defaultmap::VariableCategory,
576+
convert2, parser::OmpVariableCategory::Value,
577+
Defaultmap::VariableCategory,
576578
// clang-format off
577579
MS(Aggregate, Aggregate)
578580
MS(All, All)
@@ -582,10 +584,11 @@ Defaultmap make(const parser::OmpClause::Defaultmap &inp,
582584
// clang-format on
583585
);
584586

587+
auto &mods{semantics::OmpGetModifiers(inp.v)};
585588
auto &t0 = std::get<wrapped::ImplicitBehavior>(inp.v.t);
586-
auto &t1 = std::get<std::optional<wrapped::VariableCategory>>(inp.v.t);
589+
auto *t1 = semantics::OmpGetUniqueModifier<parser::OmpVariableCategory>(mods);
587590

588-
auto category = t1 ? convert2(*t1) : Defaultmap::VariableCategory::All;
591+
auto category = t1 ? convert2(t1->v) : Defaultmap::VariableCategory::All;
589592
return Defaultmap{{/*ImplicitBehavior=*/convert1(t0),
590593
/*VariableCategory=*/category}};
591594
}
@@ -1178,27 +1181,27 @@ ProcBind make(const parser::OmpClause::ProcBind &inp,
11781181
Reduction make(const parser::OmpClause::Reduction &inp,
11791182
semantics::SemanticsContext &semaCtx) {
11801183
// inp.v -> parser::OmpReductionClause
1181-
using wrapped = parser::OmpReductionClause;
1182-
11831184
CLAUSET_ENUM_CONVERT( //
1184-
convert, wrapped::ReductionModifier, Reduction::ReductionModifier,
1185+
convert, parser::OmpReductionModifier::Value,
1186+
Reduction::ReductionModifier,
11851187
// clang-format off
11861188
MS(Inscan, Inscan)
11871189
MS(Task, Task)
11881190
MS(Default, Default)
11891191
// clang-format on
11901192
);
11911193

1192-
auto &t0 =
1193-
std::get<std::optional<parser::OmpReductionClause::ReductionModifier>>(
1194-
inp.v.t);
1195-
auto &t1 = std::get<parser::OmpReductionIdentifier>(inp.v.t);
1194+
auto &mods = semantics::OmpGetModifiers(inp.v);
1195+
auto *t0 =
1196+
semantics::OmpGetUniqueModifier<parser::OmpReductionModifier>(mods);
1197+
auto *t1 =
1198+
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
11961199
auto &t2 = std::get<parser::OmpObjectList>(inp.v.t);
11971200
return Reduction{
11981201
{/*ReductionModifier=*/t0
1199-
? std::make_optional<Reduction::ReductionModifier>(convert(*t0))
1202+
? std::make_optional<Reduction::ReductionModifier>(convert(t0->v))
12001203
: std::nullopt,
1201-
/*ReductionIdentifiers=*/{makeReductionOperator(t1, semaCtx)},
1204+
/*ReductionIdentifiers=*/{makeReductionOperator(*t1, semaCtx)},
12021205
/*List=*/makeObjects(t2, semaCtx)}};
12031206
}
12041207

@@ -1319,10 +1322,12 @@ Permutation make(const parser::OmpClause::Permutation &inp,
13191322
TaskReduction make(const parser::OmpClause::TaskReduction &inp,
13201323
semantics::SemanticsContext &semaCtx) {
13211324
// inp.v -> parser::OmpReductionClause
1322-
auto &t0 = std::get<parser::OmpReductionIdentifier>(inp.v.t);
1325+
auto &mods = semantics::OmpGetModifiers(inp.v);
1326+
auto *t0 =
1327+
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
13231328
auto &t1 = std::get<parser::OmpObjectList>(inp.v.t);
13241329
return TaskReduction{
1325-
{/*ReductionIdentifiers=*/{makeReductionOperator(t0, semaCtx)},
1330+
{/*ReductionIdentifiers=*/{makeReductionOperator(*t0, semaCtx)},
13261331
/*List=*/makeObjects(t1, semaCtx)}};
13271332
}
13281333

flang/lib/Parser/openmp-parsers.cpp

+24-16
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ TYPE_PARSER(construct<OmpLinearModifier>( //
228228
TYPE_PARSER(construct<OmpReductionIdentifier>(Parser<DefinedOperator>{}) ||
229229
construct<OmpReductionIdentifier>(Parser<ProcedureDesignator>{}))
230230

231+
TYPE_PARSER(construct<OmpReductionModifier>(
232+
"INSCAN" >> pure(OmpReductionModifier::Value::Inscan) ||
233+
"TASK" >> pure(OmpReductionModifier::Value::Task) ||
234+
"DEFAULT" >> pure(OmpReductionModifier::Value::Default)))
235+
231236
TYPE_PARSER(construct<OmpTaskDependenceType>(
232237
"DEPOBJ" >> pure(OmpTaskDependenceType::Value::Depobj) ||
233238
"IN"_id >> pure(OmpTaskDependenceType::Value::In) ||
@@ -236,6 +241,22 @@ TYPE_PARSER(construct<OmpTaskDependenceType>(
236241
"MUTEXINOUTSET" >> pure(OmpTaskDependenceType::Value::Mutexinoutset) ||
237242
"OUT" >> pure(OmpTaskDependenceType::Value::Out)))
238243

244+
// This could be auto-generated.
245+
TYPE_PARSER(sourced(construct<OmpReductionClause::Modifier>(sourced(
246+
construct<OmpReductionClause::Modifier>(Parser<OmpReductionModifier>{}) ||
247+
construct<OmpReductionClause::Modifier>(
248+
Parser<OmpReductionIdentifier>{})))))
249+
250+
TYPE_PARSER(construct<OmpVariableCategory>(
251+
"AGGREGATE" >> pure(OmpVariableCategory::Value::Aggregate) ||
252+
"ALL"_id >> pure(OmpVariableCategory::Value::All) ||
253+
"ALLOCATABLE" >> pure(OmpVariableCategory::Value::Allocatable) ||
254+
"POINTER" >> pure(OmpVariableCategory::Value::Pointer) ||
255+
"SCALAR" >> pure(OmpVariableCategory::Value::Scalar)))
256+
257+
TYPE_PARSER(sourced(
258+
construct<OmpDefaultmapClause::Modifier>(Parser<OmpVariableCategory>{})))
259+
239260
// --- Parsers for clauses --------------------------------------------
240261

241262
// [5.0] 2.10.1 affinity([aff-modifier:] locator-list)
@@ -313,16 +334,7 @@ TYPE_PARSER(construct<OmpDefaultmapClause>(
313334
pure(OmpDefaultmapClause::ImplicitBehavior::Firstprivate) ||
314335
"NONE" >> pure(OmpDefaultmapClause::ImplicitBehavior::None) ||
315336
"DEFAULT" >> pure(OmpDefaultmapClause::ImplicitBehavior::Default)),
316-
maybe(":" >>
317-
construct<OmpDefaultmapClause::VariableCategory>(
318-
"ALL"_id >> pure(OmpDefaultmapClause::VariableCategory::All) ||
319-
"SCALAR" >> pure(OmpDefaultmapClause::VariableCategory::Scalar) ||
320-
"AGGREGATE" >>
321-
pure(OmpDefaultmapClause::VariableCategory::Aggregate) ||
322-
"ALLOCATABLE" >>
323-
pure(OmpDefaultmapClause::VariableCategory::Allocatable) ||
324-
"POINTER" >>
325-
pure(OmpDefaultmapClause::VariableCategory::Pointer)))))
337+
maybe(":" >> nonemptyList(Parser<OmpDefaultmapClause::Modifier>{}))))
326338

327339
// 2.7.1 SCHEDULE ([modifier1 [, modifier2]:]kind[, chunk_size])
328340
// Modifier -> MONITONIC | NONMONOTONIC | SIMD
@@ -379,12 +391,8 @@ TYPE_PARSER(construct<OmpIfClause>(
379391
scalarLogicalExpr))
380392

381393
TYPE_PARSER(construct<OmpReductionClause>(
382-
maybe(
383-
("INSCAN" >> pure(OmpReductionClause::ReductionModifier::Inscan) ||
384-
"TASK" >> pure(OmpReductionClause::ReductionModifier::Task) ||
385-
"DEFAULT" >> pure(OmpReductionClause::ReductionModifier::Default)) /
386-
","),
387-
Parser<OmpReductionIdentifier>{} / ":", Parser<OmpObjectList>{}))
394+
maybe(nonemptyList(Parser<OmpReductionClause::Modifier>{}) / ":"),
395+
Parser<OmpObjectList>{}))
388396

389397
// OMP 5.0 2.19.5.6 IN_REDUCTION (reduction-identifier: variable-name-list)
390398
TYPE_PARSER(construct<OmpInReductionClause>(

flang/lib/Parser/unparse.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -2189,10 +2189,8 @@ class UnparseVisitor {
21892189
Walk(":", x.step);
21902190
}
21912191
void Unparse(const OmpReductionClause &x) {
2192-
Walk(std::get<std::optional<OmpReductionClause::ReductionModifier>>(x.t),
2193-
",");
2194-
Walk(std::get<OmpReductionIdentifier>(x.t));
2195-
Put(":");
2192+
using Modifier = OmpReductionClause::Modifier;
2193+
Walk(std::get<std::optional<std::list<Modifier>>>(x.t), ":");
21962194
Walk(std::get<OmpObjectList>(x.t));
21972195
}
21982196
void Unparse(const OmpDetachClause &x) { Walk(x.v); }
@@ -2256,9 +2254,9 @@ class UnparseVisitor {
22562254
Walk(std::get<OmpObjectList>(x.t));
22572255
}
22582256
void Unparse(const OmpDefaultmapClause &x) {
2257+
using Modifier = OmpDefaultmapClause::Modifier;
22592258
Walk(std::get<OmpDefaultmapClause::ImplicitBehavior>(x.t));
2260-
Walk(":",
2261-
std::get<std::optional<OmpDefaultmapClause::VariableCategory>>(x.t));
2259+
Walk(":", std::get<std::optional<std::list<Modifier>>>(x.t));
22622260
}
22632261
void Unparse(const OmpToClause &x) {
22642262
auto &expect{
@@ -2906,7 +2904,7 @@ class UnparseVisitor {
29062904
WALK_NESTED_ENUM(OmpProcBindClause, Type) // OMP PROC_BIND
29072905
WALK_NESTED_ENUM(OmpDefaultClause, Type) // OMP DEFAULT
29082906
WALK_NESTED_ENUM(OmpDefaultmapClause, ImplicitBehavior) // OMP DEFAULTMAP
2909-
WALK_NESTED_ENUM(OmpDefaultmapClause, VariableCategory) // OMP DEFAULTMAP
2907+
WALK_NESTED_ENUM(OmpVariableCategory, Value) // OMP variable-category
29102908
WALK_NESTED_ENUM(
29112909
OmpLastprivateClause, LastprivateModifier) // OMP lastprivate-modifier
29122910
WALK_NESTED_ENUM(OmpScheduleModifierType, ModType) // OMP schedule-modifier
@@ -2915,8 +2913,7 @@ class UnparseVisitor {
29152913
WALK_NESTED_ENUM(OmpScheduleClause, ScheduleType) // OMP schedule-type
29162914
WALK_NESTED_ENUM(OmpDeviceClause, DeviceModifier) // OMP device modifier
29172915
WALK_NESTED_ENUM(OmpDeviceTypeClause, Type) // OMP DEVICE_TYPE
2918-
WALK_NESTED_ENUM(
2919-
OmpReductionClause, ReductionModifier) // OMP reduction-modifier
2916+
WALK_NESTED_ENUM(OmpReductionModifier, Value) // OMP reduction-modifier
29202917
WALK_NESTED_ENUM(OmpFromClause, Expectation) // OMP motion-expectation
29212918
WALK_NESTED_ENUM(OmpIfClause, DirectiveNameModifier) // OMP directive-modifier
29222919
WALK_NESTED_ENUM(OmpCancelType, Type) // OMP cancel-type

0 commit comments

Comments
 (0)