Skip to content

Commit 58f9c4f

Browse files
authored
[flang][OpenMP] Semantic checks for IN_REDUCTION and TASK_REDUCTION (#118841)
Update parsing of these two clauses and add semantic checks for them. Simplify some code in IsReductionAllowedForType and CheckReductionOperator.
1 parent 6cfad63 commit 58f9c4f

File tree

16 files changed

+394
-191
lines changed

16 files changed

+394
-191
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,10 @@ class ParseTreeDumper {
593593
NODE(parser, OmpReductionClause)
594594
NODE(OmpReductionClause, Modifier)
595595
NODE(parser, OmpInReductionClause)
596+
NODE(OmpInReductionClause, Modifier)
596597
NODE(parser, OmpReductionCombiner)
598+
NODE(parser, OmpTaskReductionClause)
599+
NODE(OmpTaskReductionClause, Modifier)
597600
NODE(OmpReductionCombiner, FunctionCombiner)
598601
NODE(parser, OmpReductionInitializerClause)
599602
NODE(parser, OmpReductionIdentifier)

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3960,11 +3960,14 @@ struct OmpIfClause {
39603960
std::tuple<MODIFIERS(), ScalarLogicalExpr> t;
39613961
};
39623962

3963-
// OMP 5.0 2.19.5.6 in_reduction-clause -> IN_REDUCTION (reduction-identifier:
3964-
// variable-name-list)
3963+
// Ref: [5.0:170-176], [5.1:197-205], [5.2:138-139]
3964+
//
3965+
// in-reduction-clause ->
3966+
// IN_REDUCTION(reduction-identifier: list) // since 5.0
39653967
struct OmpInReductionClause {
39663968
TUPLE_CLASS_BOILERPLATE(OmpInReductionClause);
3967-
std::tuple<OmpReductionIdentifier, OmpObjectList> t;
3969+
MODIFIER_BOILERPLATE(OmpReductionIdentifier);
3970+
std::tuple<MODIFIERS(), OmpObjectList> t;
39683971
};
39693972

39703973
// Ref: [4.5:199-201], [5.0:288-290], [5.1:321-322], [5.2:115-117]
@@ -4079,6 +4082,16 @@ struct OmpScheduleClause {
40794082
std::tuple<MODIFIERS(), Kind, std::optional<ScalarIntExpr>> t;
40804083
};
40814084

4085+
// Ref: [5.0:232-234], [5.1:264-266], [5.2:137]
4086+
//
4087+
// task-reduction-clause ->
4088+
// TASK_REDUCTION(reduction-identifier: list) // since 5.0
4089+
struct OmpTaskReductionClause {
4090+
TUPLE_CLASS_BOILERPLATE(OmpTaskReductionClause);
4091+
MODIFIER_BOILERPLATE(OmpReductionIdentifier);
4092+
std::tuple<MODIFIERS(), OmpObjectList> t;
4093+
};
4094+
40824095
// Ref: [4.5:107-109], [5.0:176-180], [5.1:205-210], [5.2:167-168]
40834096
//
40844097
// to-clause (in DECLARE TARGET) ->

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -859,10 +859,14 @@ Init make(const parser::OmpClause::Init &inp,
859859
InReduction make(const parser::OmpClause::InReduction &inp,
860860
semantics::SemanticsContext &semaCtx) {
861861
// inp.v -> parser::OmpInReductionClause
862-
auto &t0 = std::get<parser::OmpReductionIdentifier>(inp.v.t);
862+
auto &mods = semantics::OmpGetModifiers(inp.v);
863+
auto *m0 =
864+
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
863865
auto &t1 = std::get<parser::OmpObjectList>(inp.v.t);
866+
assert(m0 && "OmpReductionIdentifier is required");
867+
864868
return InReduction{
865-
{/*ReductionIdentifiers=*/{makeReductionOperator(t0, semaCtx)},
869+
{/*ReductionIdentifiers=*/{makeReductionOperator(*m0, semaCtx)},
866870
/*List=*/makeObjects(t1, semaCtx)}};
867871
}
868872

@@ -1155,17 +1159,17 @@ Reduction make(const parser::OmpClause::Reduction &inp,
11551159
);
11561160

11571161
auto &mods = semantics::OmpGetModifiers(inp.v);
1158-
auto *t0 =
1162+
auto *m0 =
11591163
semantics::OmpGetUniqueModifier<parser::OmpReductionModifier>(mods);
1160-
auto *t1 =
1164+
auto *m1 =
11611165
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
1162-
auto &t2 = std::get<parser::OmpObjectList>(inp.v.t);
1163-
assert(t1 && "OmpReductionIdentifier is required");
1166+
auto &t1 = std::get<parser::OmpObjectList>(inp.v.t);
1167+
assert(m1 && "OmpReductionIdentifier is required");
11641168

11651169
return Reduction{
1166-
{/*ReductionModifier=*/maybeApplyToV(convert, t0),
1167-
/*ReductionIdentifiers=*/{makeReductionOperator(*t1, semaCtx)},
1168-
/*List=*/makeObjects(t2, semaCtx)}};
1170+
{/*ReductionModifier=*/maybeApplyToV(convert, m0),
1171+
/*ReductionIdentifiers=*/{makeReductionOperator(*m1, semaCtx)},
1172+
/*List=*/makeObjects(t1, semaCtx)}};
11691173
}
11701174

11711175
// Relaxed: empty
@@ -1259,13 +1263,13 @@ TaskReduction make(const parser::OmpClause::TaskReduction &inp,
12591263
semantics::SemanticsContext &semaCtx) {
12601264
// inp.v -> parser::OmpReductionClause
12611265
auto &mods = semantics::OmpGetModifiers(inp.v);
1262-
auto *t0 =
1266+
auto *m0 =
12631267
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
12641268
auto &t1 = std::get<parser::OmpObjectList>(inp.v.t);
1265-
assert(t0 && "OmpReductionIdentifier is required");
1269+
assert(m0 && "OmpReductionIdentifier is required");
12661270

12671271
return TaskReduction{
1268-
{/*ReductionIdentifiers=*/{makeReductionOperator(*t0, semaCtx)},
1272+
{/*ReductionIdentifiers=*/{makeReductionOperator(*m0, semaCtx)},
12691273
/*List=*/makeObjects(t1, semaCtx)}};
12701274
}
12711275

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ TYPE_PARSER(sourced(
282282

283283
TYPE_PARSER(sourced(construct<OmpIfClause::Modifier>(OmpDirectiveNameParser{})))
284284

285+
TYPE_PARSER(sourced(construct<OmpInReductionClause::Modifier>(
286+
Parser<OmpReductionIdentifier>{})))
287+
285288
TYPE_PARSER(sourced(construct<OmpLastprivateClause::Modifier>(
286289
Parser<OmpLastprivateModifier>{})))
287290

@@ -306,6 +309,9 @@ TYPE_PARSER(sourced(construct<OmpScheduleClause::Modifier>(sourced(
306309
construct<OmpScheduleClause::Modifier>(Parser<OmpChunkModifier>{}) ||
307310
construct<OmpScheduleClause::Modifier>(Parser<OmpOrderingModifier>{})))))
308311

312+
TYPE_PARSER(sourced(construct<OmpTaskReductionClause::Modifier>(
313+
Parser<OmpReductionIdentifier>{})))
314+
309315
TYPE_PARSER(sourced(construct<OmpToClause::Modifier>(
310316
sourced(construct<OmpToClause::Modifier>(Parser<OmpExpectation>{}) ||
311317
construct<OmpToClause::Modifier>(Parser<OmpMapper>{}) ||
@@ -407,7 +413,12 @@ TYPE_PARSER(construct<OmpReductionClause>(
407413

408414
// OMP 5.0 2.19.5.6 IN_REDUCTION (reduction-identifier: variable-name-list)
409415
TYPE_PARSER(construct<OmpInReductionClause>(
410-
Parser<OmpReductionIdentifier>{} / ":", Parser<OmpObjectList>{}))
416+
maybe(nonemptyList(Parser<OmpInReductionClause::Modifier>{}) / ":"),
417+
Parser<OmpObjectList>{}))
418+
419+
TYPE_PARSER(construct<OmpTaskReductionClause>(
420+
maybe(nonemptyList(Parser<OmpTaskReductionClause::Modifier>{}) / ":"),
421+
Parser<OmpObjectList>{}))
411422

412423
// OMP 5.0 2.11.4 allocate-clause -> ALLOCATE ([allocator:] variable-name-list)
413424
// OMP 5.2 2.13.4 allocate-clause -> ALLOCATE ([allocate-modifier
@@ -609,15 +620,15 @@ TYPE_PARSER(
609620
parenthesized(Parser<OmpObjectList>{}))) ||
610621
"PROC_BIND" >> construct<OmpClause>(construct<OmpClause::ProcBind>(
611622
parenthesized(Parser<OmpProcBindClause>{}))) ||
612-
"REDUCTION" >> construct<OmpClause>(construct<OmpClause::Reduction>(
613-
parenthesized(Parser<OmpReductionClause>{}))) ||
623+
"REDUCTION"_id >> construct<OmpClause>(construct<OmpClause::Reduction>(
624+
parenthesized(Parser<OmpReductionClause>{}))) ||
614625
"IN_REDUCTION" >> construct<OmpClause>(construct<OmpClause::InReduction>(
615626
parenthesized(Parser<OmpInReductionClause>{}))) ||
616627
"DETACH" >> construct<OmpClause>(construct<OmpClause::Detach>(
617628
parenthesized(Parser<OmpDetachClause>{}))) ||
618629
"TASK_REDUCTION" >>
619630
construct<OmpClause>(construct<OmpClause::TaskReduction>(
620-
parenthesized(Parser<OmpReductionClause>{}))) ||
631+
parenthesized(Parser<OmpTaskReductionClause>{}))) ||
621632
"RELAXED" >> construct<OmpClause>(construct<OmpClause::Relaxed>()) ||
622633
"RELEASE" >> construct<OmpClause>(construct<OmpClause::Release>()) ||
623634
"REVERSE_OFFLOAD" >>

flang/lib/Parser/unparse.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,13 +2143,18 @@ class UnparseVisitor {
21432143
}
21442144
void Unparse(const OmpReductionClause &x) {
21452145
using Modifier = OmpReductionClause::Modifier;
2146-
Walk(std::get<std::optional<std::list<Modifier>>>(x.t), ":");
2146+
Walk(std::get<std::optional<std::list<Modifier>>>(x.t), ": ");
21472147
Walk(std::get<OmpObjectList>(x.t));
21482148
}
21492149
void Unparse(const OmpDetachClause &x) { Walk(x.v); }
21502150
void Unparse(const OmpInReductionClause &x) {
2151-
Walk(std::get<OmpReductionIdentifier>(x.t));
2152-
Put(":");
2151+
using Modifier = OmpInReductionClause::Modifier;
2152+
Walk(std::get<std::optional<std::list<Modifier>>>(x.t), ": ");
2153+
Walk(std::get<OmpObjectList>(x.t));
2154+
}
2155+
void Unparse(const OmpTaskReductionClause &x) {
2156+
using Modifier = OmpTaskReductionClause::Modifier;
2157+
Walk(std::get<std::optional<std::list<Modifier>>>(x.t), ": ");
21532158
Walk(std::get<OmpObjectList>(x.t));
21542159
}
21552160
void Unparse(const OmpAllocateClause &x) {

0 commit comments

Comments
 (0)