Skip to content

Commit 17cb8a5

Browse files
[Flang][OpenMP] Accept the reduction modifier (#86492)
Accept the reduction modifier in the Flang parser. Issue a TODO message during lowering. OpenMP 5.0 introduced the reduction modifier. Details can be seen in 2.19.5.4 reductionClause. OpenMP 5.2 relevant section is 5.5.8reductionClause. This will help remove some of the parser errors highlighted in the following post and also bring it to a well defined behaviour (produce TODO errors for unsupported features, do not crash). https://discourse.llvm.org/t/proposal-rename-flang-new-to-flang/69462/60
1 parent 1b310c4 commit 17cb8a5

File tree

10 files changed

+75
-8
lines changed

10 files changed

+75
-8
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ class ParseTreeDumper {
542542
NODE_ENUM(OmpOrderModifier, Kind)
543543
NODE(parser, OmpProcBindClause)
544544
NODE_ENUM(OmpProcBindClause, Type)
545+
NODE_ENUM(OmpReductionClause, ReductionModifier)
545546
NODE(parser, OmpReductionClause)
546547
NODE(parser, OmpInReductionClause)
547548
NODE(parser, OmpReductionCombiner)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3552,7 +3552,10 @@ struct OmpReductionOperator {
35523552
// variable-name-list)
35533553
struct OmpReductionClause {
35543554
TUPLE_CLASS_BOILERPLATE(OmpReductionClause);
3555-
std::tuple<OmpReductionOperator, OmpObjectList> t;
3555+
ENUM_CLASS(ReductionModifier, Inscan, Task, Default)
3556+
std::tuple<std::optional<ReductionModifier>, OmpReductionOperator,
3557+
OmpObjectList>
3558+
t;
35563559
};
35573560

35583561
// OMP 5.0 2.19.5.6 in_reduction-clause -> IN_REDUCTION (reduction-identifier:

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,12 +1005,28 @@ ProcBind make(const parser::OmpClause::ProcBind &inp,
10051005
Reduction make(const parser::OmpClause::Reduction &inp,
10061006
semantics::SemanticsContext &semaCtx) {
10071007
// inp.v -> parser::OmpReductionClause
1008-
auto &t0 = std::get<parser::OmpReductionOperator>(inp.v.t);
1009-
auto &t1 = std::get<parser::OmpObjectList>(inp.v.t);
1008+
using wrapped = parser::OmpReductionClause;
1009+
1010+
CLAUSET_ENUM_CONVERT( //
1011+
convert, wrapped::ReductionModifier, Reduction::ReductionModifier,
1012+
// clang-format off
1013+
MS(Inscan, Inscan)
1014+
MS(Task, Task)
1015+
MS(Default, Default)
1016+
// clang-format on
1017+
);
1018+
1019+
auto &t0 =
1020+
std::get<std::optional<parser::OmpReductionClause::ReductionModifier>>(
1021+
inp.v.t);
1022+
auto &t1 = std::get<parser::OmpReductionOperator>(inp.v.t);
1023+
auto &t2 = std::get<parser::OmpObjectList>(inp.v.t);
10101024
return Reduction{
1011-
{/*ReductionIdentifiers=*/{makeReductionOperator(t0, semaCtx)},
1012-
/*ReductionModifier=*/std::nullopt,
1013-
/*List=*/makeObjects(t1, semaCtx)}};
1025+
{/*ReductionModifier=*/t0
1026+
? std::make_optional<Reduction::ReductionModifier>(convert(*t0))
1027+
: std::nullopt,
1028+
/*ReductionIdentifiers=*/{makeReductionOperator(t1, semaCtx)},
1029+
/*List=*/makeObjects(t2, semaCtx)}};
10141030
}
10151031

10161032
// Relaxed: empty

flang/lib/Lower/OpenMP/ReductionProcessor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,11 @@ void ReductionProcessor::addDeclareReduction(
574574
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
575575
*reductionSymbols) {
576576
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
577+
578+
if (std::get<std::optional<omp::clause::Reduction::ReductionModifier>>(
579+
reduction.t))
580+
TODO(currentLocation, "Reduction modifiers are not supported");
581+
577582
mlir::omp::DeclareReductionOp decl;
578583
const auto &redOperatorList{
579584
std::get<omp::clause::Reduction::ReductionIdentifiers>(reduction.t)};

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ TYPE_PARSER(construct<OmpReductionOperator>(Parser<DefinedOperator>{}) ||
136136
construct<OmpReductionOperator>(Parser<ProcedureDesignator>{}))
137137

138138
TYPE_PARSER(construct<OmpReductionClause>(
139+
maybe(
140+
("INSCAN" >> pure(OmpReductionClause::ReductionModifier::Inscan) ||
141+
"TASK" >> pure(OmpReductionClause::ReductionModifier::Task) ||
142+
"DEFAULT" >> pure(OmpReductionClause::ReductionModifier::Default)) /
143+
","),
139144
Parser<OmpReductionOperator>{} / ":", Parser<OmpObjectList>{}))
140145

141146
// OMP 5.0 2.19.5.6 IN_REDUCTION (reduction-identifier: variable-name-list)

flang/lib/Parser/unparse.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,8 @@ class UnparseVisitor {
20902090
Walk(":", x.step);
20912091
}
20922092
void Unparse(const OmpReductionClause &x) {
2093+
Walk(std::get<std::optional<OmpReductionClause::ReductionModifier>>(x.t),
2094+
",");
20932095
Walk(std::get<OmpReductionOperator>(x.t));
20942096
Put(":");
20952097
Walk(std::get<OmpObjectList>(x.t));
@@ -2727,6 +2729,8 @@ class UnparseVisitor {
27272729
WALK_NESTED_ENUM(OmpScheduleClause, ScheduleType) // OMP schedule-type
27282730
WALK_NESTED_ENUM(OmpDeviceClause, DeviceModifier) // OMP device modifier
27292731
WALK_NESTED_ENUM(OmpDeviceTypeClause, Type) // OMP DEVICE_TYPE
2732+
WALK_NESTED_ENUM(
2733+
OmpReductionClause, ReductionModifier) // OMP reduction-modifier
27302734
WALK_NESTED_ENUM(OmpIfClause, DirectiveNameModifier) // OMP directive-modifier
27312735
WALK_NESTED_ENUM(OmpCancelType, Type) // OMP cancel-type
27322736
WALK_NESTED_ENUM(OmpOrderClause, Type) // OMP order-type

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2289,7 +2289,7 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Reduction &x) {
22892289
bool OmpStructureChecker::CheckReductionOperators(
22902290
const parser::OmpClause::Reduction &x) {
22912291

2292-
const auto &definedOp{std::get<0>(x.v.t)};
2292+
const auto &definedOp{std::get<parser::OmpReductionOperator>(x.v.t)};
22932293
bool ok = false;
22942294
common::visit(
22952295
common::visitors{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! RUN: %not_todo_cmd bbc -emit-fir -fopenmp -o - %s 2>&1 | FileCheck %s
2+
! RUN: %not_todo_cmd %flang_fc1 -emit-fir -fopenmp -o - %s 2>&1 | FileCheck %s
3+
4+
! CHECK: not yet implemented: Reduction modifiers are not supported
5+
6+
subroutine foo()
7+
integer :: i, j
8+
j = 0
9+
!$omp do reduction (inscan, *: j)
10+
do i = 1, 10
11+
j = j + 1
12+
end do
13+
end subroutine
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
! RUN: %flang_fc1 -fdebug-unparse-no-sema -fopenmp %s | FileCheck --ignore-case %s
2+
! RUN: %flang_fc1 -fdebug-dump-parse-tree-no-sema -fopenmp %s | FileCheck --check-prefix="PARSE-TREE" %s
3+
4+
subroutine foo()
5+
integer :: i, j
6+
j = 0
7+
! CHECK: !$OMP DO REDUCTION(TASK,*:j)
8+
! PARSE-TREE: | | ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPLoopConstruct
9+
! PARSE-TREE: | | | OmpBeginLoopDirective
10+
! PARSE-TREE: | | | | OmpLoopDirective -> llvm::omp::Directive = do
11+
! PARSE-TREE: | | | | OmpClauseList -> OmpClause -> Reduction -> OmpReductionClause
12+
! PARSE-TREE: | | | | | ReductionModifier = Task
13+
! PARSE-TREE: | | | | | OmpReductionOperator -> DefinedOperator -> IntrinsicOperator = Multiply
14+
! PARSE-TREE: | | | | | OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'j
15+
!$omp do reduction (task, *: j)
16+
do i = 1, 10
17+
j = j + 1
18+
end do
19+
!$omp end do
20+
end

llvm/include/llvm/Frontend/OpenMP/ClauseT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ struct ReductionT {
949949
using ReductionIdentifiers = ListT<type::ReductionIdentifierT<I, E>>;
950950
ENUM(ReductionModifier, Default, Inscan, Task);
951951
using TupleTrait = std::true_type;
952-
std::tuple<ReductionIdentifiers, OPT(ReductionModifier), List> t;
952+
std::tuple<OPT(ReductionModifier), ReductionIdentifiers, List> t;
953953
};
954954

955955
// V5.2: [15.8.1] `memory-order` clauses

0 commit comments

Comments
 (0)