Skip to content

Commit e79cd24

Browse files
authored
[flang][OpenMP] Use new modifier code in ORDER and SCHEDULE clauses (#117081)
This actually simplifies the AST node for the schedule clause: the two allowed modifiers can be easily classified as the ordering-modifier and the chunk-modifier during parsing without the need to create additional classes.
1 parent ab28d38 commit e79cd24

File tree

14 files changed

+267
-207
lines changed

14 files changed

+267
-207
lines changed

flang/examples/FeatureList/FeatureList.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,9 @@ struct NodeVisitor {
505505
READ_FEATURE(OmpObject)
506506
READ_FEATURE(OmpObjectList)
507507
READ_FEATURE(OmpOrderClause)
508-
READ_FEATURE(OmpOrderClause::Type)
508+
READ_FEATURE(OmpOrderClause::Ordering)
509509
READ_FEATURE(OmpOrderModifier)
510-
READ_FEATURE(OmpOrderModifier::Kind)
510+
READ_FEATURE(OmpOrderModifier::Value)
511511
READ_FEATURE(OmpProcBindClause)
512512
READ_FEATURE(OmpProcBindClause::Type)
513513
READ_FEATURE(OmpReductionClause)
@@ -522,16 +522,16 @@ struct NodeVisitor {
522522
READ_FEATURE(OmpAllocateClause::AllocateModifier::ComplexModifier)
523523
READ_FEATURE(OmpAllocateClause::AllocateModifier::Align)
524524
READ_FEATURE(OmpScheduleClause)
525-
READ_FEATURE(OmpScheduleClause::ScheduleType)
525+
READ_FEATURE(OmpScheduleClause::Kind)
526+
READ_FEATURE(OmpScheduleClause::Modifier)
526527
READ_FEATURE(OmpDeviceClause)
527528
READ_FEATURE(OmpDeviceClause::DeviceModifier)
528529
READ_FEATURE(OmpDeviceTypeClause)
529530
READ_FEATURE(OmpDeviceTypeClause::Type)
530-
READ_FEATURE(OmpScheduleModifier)
531-
READ_FEATURE(OmpScheduleModifier::Modifier1)
532-
READ_FEATURE(OmpScheduleModifier::Modifier2)
533-
READ_FEATURE(OmpScheduleModifierType)
534-
READ_FEATURE(OmpScheduleModifierType::ModType)
531+
READ_FEATURE(OmpChunkModifier)
532+
READ_FEATURE(OmpChunkModifier::Value)
533+
READ_FEATURE(OmpOrderingModifier)
534+
READ_FEATURE(OmpOrderingModifier::Value)
535535
READ_FEATURE(OmpSectionBlocks)
536536
READ_FEATURE(OmpSectionsDirective)
537537
READ_FEATURE(OmpSimpleStandaloneDirective)

flang/examples/FlangOmpReport/FlangOmpReportVisitor.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,26 @@ void OpenMPCounterVisitor::Post(const OmpVariableCategory::Value &c) {
213213
"variable_category=" + std::string{OmpVariableCategory::EnumToString(c)} +
214214
";";
215215
}
216-
void OpenMPCounterVisitor::Post(const OmpScheduleModifierType::ModType &c) {
216+
void OpenMPCounterVisitor::Post(const OmpChunkModifier::Value &c) {
217217
clauseDetails +=
218-
"modifier=" + std::string{OmpScheduleModifierType::EnumToString(c)} + ";";
218+
"modifier=" + std::string{OmpChunkModifier::EnumToString(c)} + ";";
219219
}
220220
void OpenMPCounterVisitor::Post(const OmpLinearModifier::Value &c) {
221221
clauseDetails +=
222222
"modifier=" + std::string{OmpLinearModifier::EnumToString(c)} + ";";
223223
}
224+
void OpenMPCounterVisitor::Post(const OmpOrderingModifier::Value &c) {
225+
clauseDetails +=
226+
"modifier=" + std::string{OmpOrderingModifier::EnumToString(c)} + ";";
227+
}
224228
void OpenMPCounterVisitor::Post(const OmpTaskDependenceType::Value &c) {
225229
clauseDetails +=
226230
"type=" + std::string{OmpTaskDependenceType::EnumToString(c)} + ";";
227231
}
228232
void OpenMPCounterVisitor::Post(const OmpMapClause::Type &c) {
229233
clauseDetails += "type=" + std::string{OmpMapClause::EnumToString(c)} + ";";
230234
}
231-
void OpenMPCounterVisitor::Post(const OmpScheduleClause::ScheduleType &c) {
235+
void OpenMPCounterVisitor::Post(const OmpScheduleClause::Kind &c) {
232236
clauseDetails +=
233237
"type=" + std::string{OmpScheduleClause::EnumToString(c)} + ";";
234238
}

flang/examples/FlangOmpReport/FlangOmpReportVisitor.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ struct OpenMPCounterVisitor {
7171
void Post(const OmpDefaultmapClause::ImplicitBehavior &c);
7272
void Post(const OmpVariableCategory::Value &c);
7373
void Post(const OmpDeviceTypeClause::Type &c);
74-
void Post(const OmpScheduleModifierType::ModType &c);
74+
void Post(const OmpChunkModifier::Value &c);
7575
void Post(const OmpLinearModifier::Value &c);
76+
void Post(const OmpOrderingModifier::Value &c);
7677
void Post(const OmpTaskDependenceType::Value &c);
7778
void Post(const OmpMapClause::Type &c);
78-
void Post(const OmpScheduleClause::ScheduleType &c);
79+
void Post(const OmpScheduleClause::Kind &c);
7980
void Post(const OmpIfClause::DirectiveNameModifier &c);
8081
void Post(const OmpCancelType::Type &c);
8182
void Post(const OmpClause &c);

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,10 @@ class ParseTreeDumper {
559559
NODE(parser, OmpObject)
560560
NODE(parser, OmpObjectList)
561561
NODE(parser, OmpOrderClause)
562-
NODE_ENUM(OmpOrderClause, Type)
562+
NODE(OmpOrderClause, Modifier)
563+
NODE_ENUM(OmpOrderClause, Ordering)
563564
NODE(parser, OmpOrderModifier)
564-
NODE_ENUM(OmpOrderModifier, Kind)
565+
NODE_ENUM(OmpOrderModifier, Value)
565566
NODE(parser, OmpGrainsizeClause)
566567
NODE_ENUM(OmpGrainsizeClause, Prescriptiveness)
567568
NODE(parser, OmpNumTasksClause)
@@ -585,17 +586,17 @@ class ParseTreeDumper {
585586
NODE(OmpAllocateClause::AllocateModifier, ComplexModifier)
586587
NODE(OmpAllocateClause::AllocateModifier, Align)
587588
NODE(parser, OmpScheduleClause)
588-
NODE_ENUM(OmpScheduleClause, ScheduleType)
589+
NODE(OmpScheduleClause, Modifier)
590+
NODE_ENUM(OmpScheduleClause, Kind)
589591
NODE(parser, OmpDeviceClause)
590592
NODE_ENUM(OmpDeviceClause, DeviceModifier)
591593
NODE(parser, OmpDeviceTypeClause)
592594
NODE_ENUM(OmpDeviceTypeClause, Type)
593595
NODE(parser, OmpUpdateClause)
594-
NODE(parser, OmpScheduleModifier)
595-
NODE(OmpScheduleModifier, Modifier1)
596-
NODE(OmpScheduleModifier, Modifier2)
597-
NODE(parser, OmpScheduleModifierType)
598-
NODE_ENUM(OmpScheduleModifierType, ModType)
596+
NODE(parser, OmpChunkModifier)
597+
NODE_ENUM(OmpChunkModifier, Value)
598+
NODE(parser, OmpOrderingModifier)
599+
NODE_ENUM(OmpOrderingModifier, Value)
599600
NODE(parser, OmpSectionBlocks)
600601
NODE(parser, OmpSectionsDirective)
601602
NODE(parser, OmpSimpleStandaloneDirective)

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

+54-27
Original file line numberDiff line numberDiff line change
@@ -3457,6 +3457,17 @@ inline namespace modifier {
34573457
// ENUM_CLASS(Value, Keyword1, Keyword2);
34583458
// };
34593459

3460+
// Ref: [5.2:252-254]
3461+
//
3462+
// chunk-modifier ->
3463+
// SIMD // since 5.2
3464+
//
3465+
// Prior to 5.2 "chunk-modifier" was a part of "modifier" on SCHEDULE clause.
3466+
struct OmpChunkModifier {
3467+
ENUM_CLASS(Value, Simd)
3468+
WRAPPER_CLASS_BOILERPLATE(OmpChunkModifier, Value);
3469+
};
3470+
34603471
// Ref: [5.0:47-49], [5.1:49-51], [5.2:67-69]
34613472
//
34623473
// iterator-specifier ->
@@ -3508,6 +3519,30 @@ struct OmpLinearModifier {
35083519
WRAPPER_CLASS_BOILERPLATE(OmpLinearModifier, Value);
35093520
};
35103521

3522+
// Ref: [4.5:56-63], [5.0:101-109], [5.1:126-133], [5.2:252-254]
3523+
//
3524+
// modifier ->
3525+
// MONOTONIC | NONMONOTONIC | SIMD // since 4.5, until 5.1
3526+
// ordering-modifier ->
3527+
// MONOTONIC | NONMONOTONIC // since 5.2
3528+
//
3529+
// Until 5.1, the SCHEDULE clause accepted up to two instances of "modifier".
3530+
// Since 5.2 "modifier" was replaced with "ordering-modifier" and "chunk-
3531+
// modifier".
3532+
struct OmpOrderingModifier {
3533+
ENUM_CLASS(Value, Monotonic, Nonmonotonic, Simd)
3534+
WRAPPER_CLASS_BOILERPLATE(OmpOrderingModifier, Value);
3535+
};
3536+
3537+
// Ref: [5.1:125-126], [5.2:233-234]
3538+
//
3539+
// order-modifier ->
3540+
// REPRODUCIBLE | UNCONSTRAINED // since 5.1
3541+
struct OmpOrderModifier {
3542+
ENUM_CLASS(Value, Reproducible, Unconstrained)
3543+
WRAPPER_CLASS_BOILERPLATE(OmpOrderModifier, Value);
3544+
};
3545+
35113546
// Ref: [4.5:201-207], [5.0:293-299], [5.1:325-331], [5.2:124]
35123547
//
35133548
// reduction-identifier ->
@@ -3786,16 +3821,16 @@ struct OmpMapClause {
37863821
t;
37873822
};
37883823

3789-
// 2.9.5 order-clause -> ORDER ([order-modifier :]concurrent)
3790-
struct OmpOrderModifier {
3791-
ENUM_CLASS(Kind, Reproducible, Unconstrained)
3792-
WRAPPER_CLASS_BOILERPLATE(OmpOrderModifier, Kind);
3793-
};
3794-
3824+
// Ref: [5.0:101-109], [5.1:126-134], [5.2:233-234]
3825+
//
3826+
// order-clause ->
3827+
// ORDER(CONCURRENT) | // since 5.0
3828+
// ORDER([order-modifier:] CONCURRENT) // since 5.1
37953829
struct OmpOrderClause {
37963830
TUPLE_CLASS_BOILERPLATE(OmpOrderClause);
3797-
ENUM_CLASS(Type, Concurrent)
3798-
std::tuple<std::optional<OmpOrderModifier>, Type> t;
3831+
ENUM_CLASS(Ordering, Concurrent)
3832+
MODIFIER_BOILERPLATE(OmpOrderModifier);
3833+
std::tuple<MODIFIERS(), Ordering> t;
37993834
};
38003835

38013836
// 2.5 proc-bind-clause -> PROC_BIND (MASTER | CLOSE | SPREAD)
@@ -3816,27 +3851,19 @@ struct OmpReductionClause {
38163851
std::tuple<MODIFIERS(), OmpObjectList> t;
38173852
};
38183853

3819-
// 2.7.1 sched-modifier -> MONOTONIC | NONMONOTONIC | SIMD
3820-
struct OmpScheduleModifierType {
3821-
ENUM_CLASS(ModType, Monotonic, Nonmonotonic, Simd)
3822-
WRAPPER_CLASS_BOILERPLATE(OmpScheduleModifierType, ModType);
3823-
};
3824-
3825-
struct OmpScheduleModifier {
3826-
TUPLE_CLASS_BOILERPLATE(OmpScheduleModifier);
3827-
WRAPPER_CLASS(Modifier1, OmpScheduleModifierType);
3828-
WRAPPER_CLASS(Modifier2, OmpScheduleModifierType);
3829-
std::tuple<Modifier1, std::optional<Modifier2>> t;
3830-
};
3831-
3832-
// 2.7.1 schedule-clause -> SCHEDULE ([sched-modifier1] [, sched-modifier2]:]
3833-
// kind[, chunk_size])
3854+
// Ref: [4.5:56-63], [5.0:101-109], [5.1:126-133], [5.2:252-254]
3855+
//
3856+
// schedule-clause ->
3857+
// SCHEDULE([modifier[, modifier]:]
3858+
// kind[, chunk-size]) // since 4.5, until 5.1
3859+
// schedule-clause ->
3860+
// SCHEDULE([ordering-modifier], chunk-modifier],
3861+
// kind[, chunk_size]) // since 5.2
38343862
struct OmpScheduleClause {
38353863
TUPLE_CLASS_BOILERPLATE(OmpScheduleClause);
3836-
ENUM_CLASS(ScheduleType, Static, Dynamic, Guided, Auto, Runtime)
3837-
std::tuple<std::optional<OmpScheduleModifier>, ScheduleType,
3838-
std::optional<ScalarIntExpr>>
3839-
t;
3864+
ENUM_CLASS(Kind, Static, Dynamic, Guided, Auto, Runtime)
3865+
MODIFIER_BOILERPLATE(OmpOrderingModifier, OmpChunkModifier);
3866+
std::tuple<MODIFIERS(), Kind, std::optional<ScalarIntExpr>> t;
38403867
};
38413868

38423869
// Ref: [4.5:107-109], [5.0:176-180], [5.1:205-210], [5.2:167-168]

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

+6
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,19 @@ struct OmpModifierDescriptor {
6161

6262
template <typename SpecificTy> const OmpModifierDescriptor &OmpGetDescriptor();
6363

64+
template <>
65+
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpChunkModifier>();
6466
template <>
6567
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpDependenceType>();
6668
template <>
6769
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpIterator>();
6870
template <>
6971
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpLinearModifier>();
7072
template <>
73+
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpOrderModifier>();
74+
template <>
75+
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpOrderingModifier>();
76+
template <>
7177
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpReductionIdentifier>();
7278
template <>
7379
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpReductionModifier>();

flang/lib/Lower/OpenMP/Clauses.cpp

+24-51
Original file line numberDiff line numberDiff line change
@@ -1109,28 +1109,26 @@ Order make(const parser::OmpClause::Order &inp,
11091109
using wrapped = parser::OmpOrderClause;
11101110

11111111
CLAUSET_ENUM_CONVERT( //
1112-
convert1, parser::OmpOrderModifier::Kind, Order::OrderModifier,
1112+
convert1, parser::OmpOrderModifier::Value, Order::OrderModifier,
11131113
// clang-format off
11141114
MS(Reproducible, Reproducible)
11151115
MS(Unconstrained, Unconstrained)
11161116
// clang-format on
11171117
);
11181118

11191119
CLAUSET_ENUM_CONVERT( //
1120-
convert2, wrapped::Type, Order::Ordering,
1120+
convert2, wrapped::Ordering, Order::Ordering,
11211121
// clang-format off
11221122
MS(Concurrent, Concurrent)
11231123
// clang-format on
11241124
);
11251125

1126-
auto &t0 = std::get<std::optional<parser::OmpOrderModifier>>(inp.v.t);
1127-
auto &t1 = std::get<wrapped::Type>(inp.v.t);
1126+
auto &mods = semantics::OmpGetModifiers(inp.v);
1127+
auto *t0 = semantics::OmpGetUniqueModifier<parser::OmpOrderModifier>(mods);
1128+
auto &t1 = std::get<wrapped::Ordering>(inp.v.t);
11281129

1129-
auto convert3 = [&](const parser::OmpOrderModifier &s) {
1130-
return convert1(s.v);
1131-
};
1132-
return Order{
1133-
{/*OrderModifier=*/maybeApply(convert3, t0), /*Ordering=*/convert2(t1)}};
1130+
return Order{{/*OrderModifier=*/maybeApplyToV(convert1, t0),
1131+
/*Ordering=*/convert2(t1)}};
11341132
}
11351133

11361134
Ordered make(const parser::OmpClause::Ordered &inp,
@@ -1197,10 +1195,10 @@ Reduction make(const parser::OmpClause::Reduction &inp,
11971195
auto *t1 =
11981196
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
11991197
auto &t2 = std::get<parser::OmpObjectList>(inp.v.t);
1198+
assert(t1 && "OmpReductionIdentifier is required");
1199+
12001200
return Reduction{
1201-
{/*ReductionModifier=*/t0
1202-
? std::make_optional<Reduction::ReductionModifier>(convert(t0->v))
1203-
: std::nullopt,
1201+
{/*ReductionModifier=*/maybeApplyToV(convert, t0),
12041202
/*ReductionIdentifiers=*/{makeReductionOperator(*t1, semaCtx)},
12051203
/*List=*/makeObjects(t2, semaCtx)}};
12061204
}
@@ -1221,7 +1219,7 @@ Schedule make(const parser::OmpClause::Schedule &inp,
12211219
using wrapped = parser::OmpScheduleClause;
12221220

12231221
CLAUSET_ENUM_CONVERT( //
1224-
convert1, wrapped::ScheduleType, Schedule::Kind,
1222+
convert1, wrapped::Kind, Schedule::Kind,
12251223
// clang-format off
12261224
MS(Static, Static)
12271225
MS(Dynamic, Dynamic)
@@ -1232,57 +1230,30 @@ Schedule make(const parser::OmpClause::Schedule &inp,
12321230
);
12331231

12341232
CLAUSET_ENUM_CONVERT( //
1235-
convert2, parser::OmpScheduleModifierType::ModType,
1236-
Schedule::OrderingModifier,
1233+
convert2, parser::OmpOrderingModifier::Value, Schedule::OrderingModifier,
12371234
// clang-format off
12381235
MS(Monotonic, Monotonic)
12391236
MS(Nonmonotonic, Nonmonotonic)
12401237
// clang-format on
12411238
);
12421239

12431240
CLAUSET_ENUM_CONVERT( //
1244-
convert3, parser::OmpScheduleModifierType::ModType,
1245-
Schedule::ChunkModifier,
1241+
convert3, parser::OmpChunkModifier::Value, Schedule::ChunkModifier,
12461242
// clang-format off
12471243
MS(Simd, Simd)
12481244
// clang-format on
12491245
);
12501246

1251-
auto &t0 = std::get<std::optional<parser::OmpScheduleModifier>>(inp.v.t);
1252-
auto &t1 = std::get<wrapped::ScheduleType>(inp.v.t);
1253-
auto &t2 = std::get<std::optional<parser::ScalarIntExpr>>(inp.v.t);
1254-
1255-
if (!t0) {
1256-
return Schedule{{/*Kind=*/convert1(t1), /*OrderingModifier=*/std::nullopt,
1257-
/*ChunkModifier=*/std::nullopt,
1258-
/*ChunkSize=*/maybeApply(makeExprFn(semaCtx), t2)}};
1259-
}
1260-
1261-
// The members of parser::OmpScheduleModifier correspond to OrderingModifier,
1262-
// and ChunkModifier, but they can appear in any order.
1263-
auto &m1 = std::get<parser::OmpScheduleModifier::Modifier1>(t0->t);
1264-
auto &m2 =
1265-
std::get<std::optional<parser::OmpScheduleModifier::Modifier2>>(t0->t);
1266-
1267-
std::optional<Schedule::OrderingModifier> omod;
1268-
std::optional<Schedule::ChunkModifier> cmod;
1269-
1270-
if (m1.v.v == parser::OmpScheduleModifierType::ModType::Simd) {
1271-
// m1 is chunk-modifier
1272-
cmod = convert3(m1.v.v);
1273-
if (m2)
1274-
omod = convert2(m2->v.v);
1275-
} else {
1276-
// m1 is ordering-modifier
1277-
omod = convert2(m1.v.v);
1278-
if (m2)
1279-
cmod = convert3(m2->v.v);
1280-
}
1247+
auto &mods = semantics::OmpGetModifiers(inp.v);
1248+
auto *t0 = semantics::OmpGetUniqueModifier<parser::OmpOrderingModifier>(mods);
1249+
auto *t1 = semantics::OmpGetUniqueModifier<parser::OmpChunkModifier>(mods);
1250+
auto &t2 = std::get<wrapped::Kind>(inp.v.t);
1251+
auto &t3 = std::get<std::optional<parser::ScalarIntExpr>>(inp.v.t);
12811252

1282-
return Schedule{{/*Kind=*/convert1(t1),
1283-
/*OrderingModifier=*/omod,
1284-
/*ChunkModifier=*/cmod,
1285-
/*ChunkSize=*/maybeApply(makeExprFn(semaCtx), t2)}};
1253+
return Schedule{{/*Kind=*/convert1(t2),
1254+
/*OrderingModifier=*/maybeApplyToV(convert2, t0),
1255+
/*ChunkModifier=*/maybeApplyToV(convert3, t1),
1256+
/*ChunkSize=*/maybeApply(makeExprFn(semaCtx), t3)}};
12861257
}
12871258

12881259
// SeqCst: empty
@@ -1326,6 +1297,8 @@ TaskReduction make(const parser::OmpClause::TaskReduction &inp,
13261297
auto *t0 =
13271298
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
13281299
auto &t1 = std::get<parser::OmpObjectList>(inp.v.t);
1300+
assert(t0 && "OmpReductionIdentifier is required");
1301+
13291302
return TaskReduction{
13301303
{/*ReductionIdentifiers=*/{makeReductionOperator(*t0, semaCtx)},
13311304
/*List=*/makeObjects(t1, semaCtx)}};

0 commit comments

Comments
 (0)