Skip to content

Commit fac6a85

Browse files
committed
[flang][OpenMP] Apply modifier representation to semantic checks
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 e8bbc26 commit fac6a85

File tree

15 files changed

+236
-128
lines changed

15 files changed

+236
-128
lines changed

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

Lines changed: 6 additions & 2 deletions
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)
@@ -567,8 +569,10 @@ class ParseTreeDumper {
567569
NODE_ENUM(OmpBindClause, Type)
568570
NODE(parser, OmpProcBindClause)
569571
NODE_ENUM(OmpProcBindClause, Type)
570-
NODE_ENUM(OmpReductionClause, ReductionModifier)
572+
NODE(parser, OmpReductionModifier)
573+
NODE_ENUM(OmpReductionModifier, Value)
571574
NODE(parser, OmpReductionClause)
575+
NODE(OmpReductionClause, Modifier)
572576
NODE(parser, OmpInReductionClause)
573577
NODE(parser, OmpReductionCombiner)
574578
NODE(OmpReductionCombiner, FunctionCombiner)

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

Lines changed: 40 additions & 9 deletions
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
@@ -3771,14 +3800,16 @@ struct OmpProcBindClause {
37713800
WRAPPER_CLASS_BOILERPLATE(OmpProcBindClause, Type);
37723801
};
37733802

3774-
// 2.15.3.6 reduction-clause -> REDUCTION (reduction-identifier:
3775-
// variable-name-list)
3803+
// Ref: [4.5:201-207], [5.0:300-302], [5.1:332-334], [5.2:134-137]
3804+
//
3805+
// reduction-clause ->
3806+
// REDUCTION(reduction-identifier: list) | // since 4.5
3807+
// REDUCTION([reduction-modifier,]
3808+
// reduction-identifier: list) // since 5.0
37763809
struct OmpReductionClause {
37773810
TUPLE_CLASS_BOILERPLATE(OmpReductionClause);
3778-
ENUM_CLASS(ReductionModifier, Inscan, Task, Default)
3779-
std::tuple<std::optional<ReductionModifier>, OmpReductionIdentifier,
3780-
OmpObjectList>
3781-
t;
3811+
MODIFIER_BOILERPLATE(OmpReductionModifier, OmpReductionIdentifier);
3812+
std::tuple<MODIFIERS(), OmpObjectList> t;
37823813
};
37833814

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

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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "flang/Evaluate/expression.h"
1313
#include "flang/Parser/parse-tree.h"
1414
#include "flang/Semantics/expression.h"
15+
#include "flang/Semantics/openmp-modifiers.h"
1516
#include "flang/Semantics/symbol.h"
1617

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

573574
CLAUSET_ENUM_CONVERT( //
574-
convert2, wrapped::VariableCategory, Defaultmap::VariableCategory,
575+
convert2, parser::OmpVariableCategory::Value,
576+
Defaultmap::VariableCategory,
575577
// clang-format off
576578
MS(Aggregate, Aggregate)
577579
MS(All, All)
@@ -581,10 +583,11 @@ Defaultmap make(const parser::OmpClause::Defaultmap &inp,
581583
// clang-format on
582584
);
583585

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

587-
auto category = t1 ? convert2(*t1) : Defaultmap::VariableCategory::All;
590+
auto category = t1 ? convert2(t1->v) : Defaultmap::VariableCategory::All;
588591
return Defaultmap{{/*ImplicitBehavior=*/convert1(t0),
589592
/*VariableCategory=*/category}};
590593
}
@@ -1173,27 +1176,27 @@ ProcBind make(const parser::OmpClause::ProcBind &inp,
11731176
Reduction make(const parser::OmpClause::Reduction &inp,
11741177
semantics::SemanticsContext &semaCtx) {
11751178
// inp.v -> parser::OmpReductionClause
1176-
using wrapped = parser::OmpReductionClause;
1177-
11781179
CLAUSET_ENUM_CONVERT( //
1179-
convert, wrapped::ReductionModifier, Reduction::ReductionModifier,
1180+
convert, parser::OmpReductionModifier::Value,
1181+
Reduction::ReductionModifier,
11801182
// clang-format off
11811183
MS(Inscan, Inscan)
11821184
MS(Task, Task)
11831185
MS(Default, Default)
11841186
// clang-format on
11851187
);
11861188

1187-
auto &t0 =
1188-
std::get<std::optional<parser::OmpReductionClause::ReductionModifier>>(
1189-
inp.v.t);
1190-
auto &t1 = std::get<parser::OmpReductionIdentifier>(inp.v.t);
1189+
auto &mods = semantics::OmpGetModifiers(inp.v);
1190+
auto *t0 =
1191+
semantics::OmpGetUniqueModifier<parser::OmpReductionModifier>(mods);
1192+
auto *t1 =
1193+
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
11911194
auto &t2 = std::get<parser::OmpObjectList>(inp.v.t);
11921195
return Reduction{
11931196
{/*ReductionModifier=*/t0
1194-
? std::make_optional<Reduction::ReductionModifier>(convert(*t0))
1197+
? std::make_optional<Reduction::ReductionModifier>(convert(t0->v))
11951198
: std::nullopt,
1196-
/*ReductionIdentifiers=*/{makeReductionOperator(t1, semaCtx)},
1199+
/*ReductionIdentifiers=*/{makeReductionOperator(*t1, semaCtx)},
11971200
/*List=*/makeObjects(t2, semaCtx)}};
11981201
}
11991202

@@ -1314,10 +1317,12 @@ Permutation make(const parser::OmpClause::Permutation &inp,
13141317
TaskReduction make(const parser::OmpClause::TaskReduction &inp,
13151318
semantics::SemanticsContext &semaCtx) {
13161319
// inp.v -> parser::OmpReductionClause
1317-
auto &t0 = std::get<parser::OmpReductionIdentifier>(inp.v.t);
1320+
auto &mods = semantics::OmpGetModifiers(inp.v);
1321+
auto *t0 =
1322+
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
13181323
auto &t1 = std::get<parser::OmpObjectList>(inp.v.t);
13191324
return TaskReduction{
1320-
{/*ReductionIdentifiers=*/{makeReductionOperator(t0, semaCtx)},
1325+
{/*ReductionIdentifiers=*/{makeReductionOperator(*t0, semaCtx)},
13211326
/*List=*/makeObjects(t1, semaCtx)}};
13221327
}
13231328

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ TYPE_PARSER(construct<OmpLinearModifier>( //
229229
TYPE_PARSER(construct<OmpReductionIdentifier>(Parser<DefinedOperator>{}) ||
230230
construct<OmpReductionIdentifier>(Parser<ProcedureDesignator>{}))
231231

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

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

242263
// [5.0] 2.10.1 affinity([aff-modifier:] locator-list)
@@ -309,16 +330,7 @@ TYPE_PARSER(construct<OmpDefaultmapClause>(
309330
pure(OmpDefaultmapClause::ImplicitBehavior::Firstprivate) ||
310331
"NONE" >> pure(OmpDefaultmapClause::ImplicitBehavior::None) ||
311332
"DEFAULT" >> pure(OmpDefaultmapClause::ImplicitBehavior::Default)),
312-
maybe(":" >>
313-
construct<OmpDefaultmapClause::VariableCategory>(
314-
"ALL"_id >> pure(OmpDefaultmapClause::VariableCategory::All) ||
315-
"SCALAR" >> pure(OmpDefaultmapClause::VariableCategory::Scalar) ||
316-
"AGGREGATE" >>
317-
pure(OmpDefaultmapClause::VariableCategory::Aggregate) ||
318-
"ALLOCATABLE" >>
319-
pure(OmpDefaultmapClause::VariableCategory::Allocatable) ||
320-
"POINTER" >>
321-
pure(OmpDefaultmapClause::VariableCategory::Pointer)))))
333+
maybe(":" >> nonemptyList(Parser<OmpDefaultmapClause::Modifier>{}))))
322334

323335
// 2.7.1 SCHEDULE ([modifier1 [, modifier2]:]kind[, chunk_size])
324336
// Modifier -> MONITONIC | NONMONOTONIC | SIMD
@@ -375,12 +387,8 @@ TYPE_PARSER(construct<OmpIfClause>(
375387
scalarLogicalExpr))
376388

377389
TYPE_PARSER(construct<OmpReductionClause>(
378-
maybe(
379-
("INSCAN" >> pure(OmpReductionClause::ReductionModifier::Inscan) ||
380-
"TASK" >> pure(OmpReductionClause::ReductionModifier::Task) ||
381-
"DEFAULT" >> pure(OmpReductionClause::ReductionModifier::Default)) /
382-
","),
383-
Parser<OmpReductionIdentifier>{} / ":", Parser<OmpObjectList>{}))
390+
maybe(nonemptyList(Parser<OmpReductionClause::Modifier>{}) / ":"),
391+
Parser<OmpObjectList>{}))
384392

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

flang/lib/Parser/unparse.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,10 +2179,8 @@ class UnparseVisitor {
21792179
Walk(":", x.step);
21802180
}
21812181
void Unparse(const OmpReductionClause &x) {
2182-
Walk(std::get<std::optional<OmpReductionClause::ReductionModifier>>(x.t),
2183-
",");
2184-
Walk(std::get<OmpReductionIdentifier>(x.t));
2185-
Put(":");
2182+
using Modifier = OmpReductionClause::Modifier;
2183+
Walk(std::get<std::optional<std::list<Modifier>>>(x.t), ":");
21862184
Walk(std::get<OmpObjectList>(x.t));
21872185
}
21882186
void Unparse(const OmpDetachClause &x) { Walk(x.v); }
@@ -2246,9 +2244,9 @@ class UnparseVisitor {
22462244
Walk(std::get<OmpObjectList>(x.t));
22472245
}
22482246
void Unparse(const OmpDefaultmapClause &x) {
2247+
using Modifier = OmpDefaultmapClause::Modifier;
22492248
Walk(std::get<OmpDefaultmapClause::ImplicitBehavior>(x.t));
2250-
Walk(":",
2251-
std::get<std::optional<OmpDefaultmapClause::VariableCategory>>(x.t));
2249+
Walk(":", std::get<std::optional<std::list<Modifier>>>(x.t));
22522250
}
22532251
void Unparse(const OmpToClause &x) {
22542252
auto &expect{
@@ -2896,7 +2894,7 @@ class UnparseVisitor {
28962894
WALK_NESTED_ENUM(OmpProcBindClause, Type) // OMP PROC_BIND
28972895
WALK_NESTED_ENUM(OmpDefaultClause, Type) // OMP DEFAULT
28982896
WALK_NESTED_ENUM(OmpDefaultmapClause, ImplicitBehavior) // OMP DEFAULTMAP
2899-
WALK_NESTED_ENUM(OmpDefaultmapClause, VariableCategory) // OMP DEFAULTMAP
2897+
WALK_NESTED_ENUM(OmpVariableCategory, Value) // OMP variable-category
29002898
WALK_NESTED_ENUM(
29012899
OmpLastprivateClause, LastprivateModifier) // OMP lastprivate-modifier
29022900
WALK_NESTED_ENUM(OmpScheduleModifierType, ModType) // OMP schedule-modifier
@@ -2905,8 +2903,7 @@ class UnparseVisitor {
29052903
WALK_NESTED_ENUM(OmpScheduleClause, ScheduleType) // OMP schedule-type
29062904
WALK_NESTED_ENUM(OmpDeviceClause, DeviceModifier) // OMP device modifier
29072905
WALK_NESTED_ENUM(OmpDeviceTypeClause, Type) // OMP DEVICE_TYPE
2908-
WALK_NESTED_ENUM(
2909-
OmpReductionClause, ReductionModifier) // OMP reduction-modifier
2906+
WALK_NESTED_ENUM(OmpReductionModifier, Value) // OMP reduction-modifier
29102907
WALK_NESTED_ENUM(OmpFromClause, Expectation) // OMP motion-expectation
29112908
WALK_NESTED_ENUM(OmpIfClause, DirectiveNameModifier) // OMP directive-modifier
29122909
WALK_NESTED_ENUM(OmpCancelType, Type) // OMP cancel-type

0 commit comments

Comments
 (0)