Skip to content

Commit 9925359

Browse files
[flang][llvm][openmp]Add Initializer clause to OMP.td (#129540)
Then use this in the Flang compiler for parsing the OpenMP declare reduction. This has no real functional change to the existing code, it's only moving the declaration itself around. A few tests has been updated, to reflect the new type names.
1 parent a537724 commit 9925359

File tree

12 files changed

+66
-46
lines changed

12 files changed

+66
-46
lines changed

flang/examples/FeatureList/FeatureList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ struct NodeVisitor {
514514
READ_FEATURE(OmpReductionClause)
515515
READ_FEATURE(OmpInReductionClause)
516516
READ_FEATURE(OmpReductionCombiner)
517-
READ_FEATURE(OmpReductionInitializerClause)
517+
READ_FEATURE(OmpInitializerClause)
518518
READ_FEATURE(OmpReductionIdentifier)
519519
READ_FEATURE(OmpAllocateClause)
520520
READ_FEATURE(OmpAllocateClause::Modifier)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,9 @@ class ParseTreeDumper {
638638
NODE(parser, OmpReductionCombiner)
639639
NODE(parser, OmpTaskReductionClause)
640640
NODE(OmpTaskReductionClause, Modifier)
641-
NODE(parser, OmpReductionInitializerProc)
642-
NODE(parser, OmpReductionInitializerExpr)
643-
NODE(parser, OmpReductionInitializerClause)
641+
NODE(parser, OmpInitializerProc)
642+
NODE(parser, OmpInitializerExpr)
643+
NODE(parser, OmpInitializerClause)
644644
NODE(parser, OmpReductionIdentifier)
645645
NODE(parser, OmpAllocateClause)
646646
NODE(OmpAllocateClause, Modifier)

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,6 +4246,20 @@ struct OmpInReductionClause {
42464246
std::tuple<MODIFIERS(), OmpObjectList> t;
42474247
};
42484248

4249+
// declare-reduction -> DECLARE REDUCTION (reduction-identifier : type-list
4250+
// : combiner) [initializer-clause]
4251+
struct OmpInitializerProc {
4252+
TUPLE_CLASS_BOILERPLATE(OmpInitializerProc);
4253+
std::tuple<ProcedureDesignator, std::list<ActualArgSpec>> t;
4254+
};
4255+
WRAPPER_CLASS(OmpInitializerExpr, Expr);
4256+
4257+
// Initialization for declare reduction construct
4258+
struct OmpInitializerClause {
4259+
UNION_CLASS_BOILERPLATE(OmpInitializerClause);
4260+
std::variant<OmpInitializerProc, OmpInitializerExpr> u;
4261+
};
4262+
42494263
// Ref: [4.5:199-201], [5.0:288-290], [5.1:321-322], [5.2:115-117]
42504264
//
42514265
// lastprivate-clause ->
@@ -4627,24 +4641,14 @@ struct OpenMPDeclareMapperConstruct {
46274641
std::tuple<Verbatim, OmpMapperSpecifier, OmpClauseList> t;
46284642
};
46294643

4644+
// ref: 5.2: Section 5.5.11 139-141
46304645
// 2.16 declare-reduction -> DECLARE REDUCTION (reduction-identifier : type-list
46314646
// : combiner) [initializer-clause]
4632-
struct OmpReductionInitializerProc {
4633-
TUPLE_CLASS_BOILERPLATE(OmpReductionInitializerProc);
4634-
std::tuple<ProcedureDesignator, std::list<ActualArgSpec>> t;
4635-
};
4636-
WRAPPER_CLASS(OmpReductionInitializerExpr, Expr);
4637-
4638-
struct OmpReductionInitializerClause {
4639-
UNION_CLASS_BOILERPLATE(OmpReductionInitializerClause);
4640-
std::variant<OmpReductionInitializerProc, OmpReductionInitializerExpr> u;
4641-
};
4642-
46434647
struct OpenMPDeclareReductionConstruct {
46444648
TUPLE_CLASS_BOILERPLATE(OpenMPDeclareReductionConstruct);
46454649
CharBlock source;
46464650
std::tuple<Verbatim, common::Indirection<OmpReductionSpecifier>,
4647-
std::optional<OmpReductionInitializerClause>>
4651+
std::optional<OmpClauseList>>
46484652
t;
46494653
};
46504654

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,10 @@ Init make(const parser::OmpClause::Init &inp,
863863
llvm_unreachable("Empty: init");
864864
}
865865

866-
// Initializer: missing-in-parser
866+
Initializer make(const parser::OmpClause::Initializer &inp,
867+
semantics::SemanticsContext &semaCtx) {
868+
llvm_unreachable("Empty: initializer");
869+
}
867870

868871
InReduction make(const parser::OmpClause::InReduction &inp,
869872
semantics::SemanticsContext &semaCtx) {

flang/lib/Lower/OpenMP/Clauses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ using Inbranch = tomp::clause::InbranchT<TypeTy, IdTy, ExprTy>;
231231
using Inclusive = tomp::clause::InclusiveT<TypeTy, IdTy, ExprTy>;
232232
using Indirect = tomp::clause::IndirectT<TypeTy, IdTy, ExprTy>;
233233
using Init = tomp::clause::InitT<TypeTy, IdTy, ExprTy>;
234+
using Initializer = tomp::clause::InitializerT<TypeTy, IdTy, ExprTy>;
234235
using InReduction = tomp::clause::InReductionT<TypeTy, IdTy, ExprTy>;
235236
using IsDevicePtr = tomp::clause::IsDevicePtrT<TypeTy, IdTy, ExprTy>;
236237
using Lastprivate = tomp::clause::LastprivateT<TypeTy, IdTy, ExprTy>;

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,8 @@ TYPE_PARSER("ABSENT" >> construct<OmpClause>(construct<OmpClause::Absent>(
855855
"INBRANCH" >> construct<OmpClause>(construct<OmpClause::Inbranch>()) ||
856856
"INCLUSIVE" >> construct<OmpClause>(construct<OmpClause::Inclusive>(
857857
parenthesized(Parser<OmpObjectList>{}))) ||
858+
"INITIALIZER" >> construct<OmpClause>(construct<OmpClause::Initializer>(
859+
parenthesized(Parser<OmpInitializerClause>{}))) ||
858860
"IS_DEVICE_PTR" >> construct<OmpClause>(construct<OmpClause::IsDevicePtr>(
859861
parenthesized(Parser<OmpObjectList>{}))) ||
860862
"LASTPRIVATE" >> construct<OmpClause>(construct<OmpClause::Lastprivate>(
@@ -1174,22 +1176,19 @@ TYPE_PARSER(construct<OmpBlockDirective>(first(
11741176
TYPE_PARSER(sourced(construct<OmpBeginBlockDirective>(
11751177
sourced(Parser<OmpBlockDirective>{}), Parser<OmpClauseList>{})))
11761178

1177-
TYPE_PARSER(construct<OmpReductionInitializerExpr>("OMP_PRIV =" >> expr))
1178-
TYPE_PARSER(
1179-
construct<OmpReductionInitializerProc>(Parser<ProcedureDesignator>{},
1180-
parenthesized(many(maybe(","_tok) >> Parser<ActualArgSpec>{}))))
1179+
TYPE_PARSER(construct<OmpInitializerExpr>("OMP_PRIV =" >> expr))
1180+
TYPE_PARSER(construct<OmpInitializerProc>(Parser<ProcedureDesignator>{},
1181+
parenthesized(many(maybe(","_tok) >> Parser<ActualArgSpec>{}))))
11811182

1182-
TYPE_PARSER(construct<OmpReductionInitializerClause>(
1183-
"INITIALIZER" >> parenthesized(construct<OmpReductionInitializerClause>(
1184-
Parser<OmpReductionInitializerExpr>{}) ||
1185-
construct<OmpReductionInitializerClause>(
1186-
Parser<OmpReductionInitializerProc>{}))))
1183+
TYPE_PARSER(construct<OmpInitializerClause>(
1184+
construct<OmpInitializerClause>(Parser<OmpInitializerExpr>{}) ||
1185+
construct<OmpInitializerClause>(Parser<OmpInitializerProc>{})))
11871186

11881187
// 2.16 Declare Reduction Construct
11891188
TYPE_PARSER(sourced(construct<OpenMPDeclareReductionConstruct>(
11901189
verbatim("DECLARE REDUCTION"_tok),
11911190
"(" >> indirect(Parser<OmpReductionSpecifier>{}) / ")",
1192-
maybe(Parser<OmpReductionInitializerClause>{}))))
1191+
maybe(Parser<OmpClauseList>{}))))
11931192

11941193
// declare-target with list
11951194
TYPE_PARSER(sourced(construct<OmpDeclareTargetWithList>(

flang/lib/Parser/unparse.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,28 +2699,24 @@ class UnparseVisitor {
26992699
void Unparse(const OmpDeclareTargetWithList &x) {
27002700
Put("("), Walk(x.v), Put(")");
27012701
}
2702-
void Unparse(const OmpReductionInitializerProc &x) {
2702+
void Unparse(const OmpInitializerProc &x) {
27032703
Walk(std::get<ProcedureDesignator>(x.t));
27042704
Put("(");
27052705
Walk(std::get<std::list<ActualArgSpec>>(x.t));
27062706
Put(")");
27072707
}
2708-
void Unparse(const OmpReductionInitializerExpr &x) {
2708+
void Unparse(const OmpInitializerExpr &x) {
27092709
Word("OMP_PRIV = ");
27102710
Walk(x.v);
27112711
}
2712-
void Unparse(const OmpReductionInitializerClause &x) {
2713-
Word(" INITIALIZER(");
2714-
Walk(x.u);
2715-
Put(")");
2716-
}
2712+
void Unparse(const OmpInitializerClause &x) { Walk(x.u); }
27172713
void Unparse(const OpenMPDeclareReductionConstruct &x) {
27182714
BeginOpenMP();
27192715
Word("!$OMP DECLARE REDUCTION ");
27202716
Put("(");
27212717
Walk(std::get<common::Indirection<OmpReductionSpecifier>>(x.t));
27222718
Put(")");
2723-
Walk(std::get<std::optional<OmpReductionInitializerClause>>(x.t));
2719+
Walk(std::get<std::optional<OmpClauseList>>(x.t));
27242720
Put("\n");
27252721
EndOpenMP();
27262722
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,18 @@ void OmpStructureChecker::Leave(const parser::OpenMPDeclareMapperConstruct &) {
16781678
dirContext_.pop_back();
16791679
}
16801680

1681+
void OmpStructureChecker::Enter(
1682+
const parser::OpenMPDeclareReductionConstruct &x) {
1683+
const auto &dir{std::get<parser::Verbatim>(x.t)};
1684+
PushContextAndClauseSets(
1685+
dir.source, llvm::omp::Directive::OMPD_declare_reduction);
1686+
}
1687+
1688+
void OmpStructureChecker::Leave(
1689+
const parser::OpenMPDeclareReductionConstruct &) {
1690+
dirContext_.pop_back();
1691+
}
1692+
16811693
void OmpStructureChecker::Enter(const parser::OpenMPDeclareTargetConstruct &x) {
16821694
const auto &dir{std::get<parser::Verbatim>(x.t)};
16831695
PushContext(dir.source, llvm::omp::Directive::OMPD_declare_target);
@@ -2990,6 +3002,7 @@ CHECK_SIMPLE_CLAUSE(Grainsize, OMPC_grainsize)
29903002
CHECK_SIMPLE_CLAUSE(Hint, OMPC_hint)
29913003
CHECK_SIMPLE_CLAUSE(Holds, OMPC_holds)
29923004
CHECK_SIMPLE_CLAUSE(Inclusive, OMPC_inclusive)
3005+
CHECK_SIMPLE_CLAUSE(Initializer, OMPC_initializer)
29933006
CHECK_SIMPLE_CLAUSE(Match, OMPC_match)
29943007
CHECK_SIMPLE_CLAUSE(Nontemporal, OMPC_nontemporal)
29953008
CHECK_SIMPLE_CLAUSE(NumTasks, OMPC_num_tasks)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class OmpStructureChecker
102102
void Leave(const parser::OpenMPDeclarativeAllocate &);
103103
void Enter(const parser::OpenMPDeclareMapperConstruct &);
104104
void Leave(const parser::OpenMPDeclareMapperConstruct &);
105+
void Enter(const parser::OpenMPDeclareReductionConstruct &);
106+
void Leave(const parser::OpenMPDeclareReductionConstruct &);
105107
void Enter(const parser::OpenMPDeclareTargetConstruct &);
106108
void Leave(const parser::OpenMPDeclareTargetConstruct &);
107109
void Enter(const parser::OpenMPDepobjConstruct &);

flang/lib/Semantics/resolve-names.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ class OmpVisitor : public virtual DeclarationVisitor {
14821482
return false;
14831483
}
14841484

1485-
bool Pre(const parser::OmpReductionInitializerProc &x) {
1485+
bool Pre(const parser::OmpInitializerProc &x) {
14861486
auto &procDes = std::get<parser::ProcedureDesignator>(x.t);
14871487
auto &name = std::get<parser::Name>(procDes.u);
14881488
auto *symbol{FindSymbol(NonDerivedTypeScope(), name)};
@@ -1496,13 +1496,9 @@ class OmpVisitor : public virtual DeclarationVisitor {
14961496

14971497
bool Pre(const parser::OpenMPDeclareReductionConstruct &x) {
14981498
AddOmpSourceRange(x.source);
1499-
parser::OmpClauseList emptyList{std::list<parser::OmpClause>{}};
15001499
ProcessReductionSpecifier(
15011500
std::get<Indirection<parser::OmpReductionSpecifier>>(x.t).value(),
1502-
emptyList);
1503-
auto &init =
1504-
std::get<std::optional<parser::OmpReductionInitializerClause>>(x.t);
1505-
Walk(init);
1501+
std::get<std::optional<parser::OmpClauseList>>(x.t));
15061502
return false;
15071503
}
15081504
bool Pre(const parser::OmpMapClause &);
@@ -1659,7 +1655,7 @@ class OmpVisitor : public virtual DeclarationVisitor {
16591655
void ProcessMapperSpecifier(const parser::OmpMapperSpecifier &spec,
16601656
const parser::OmpClauseList &clauses);
16611657
void ProcessReductionSpecifier(const parser::OmpReductionSpecifier &spec,
1662-
const parser::OmpClauseList &clauses);
1658+
const std::optional<parser::OmpClauseList> &clauses);
16631659
};
16641660

16651661
bool OmpVisitor::NeedsScope(const parser::OpenMPBlockConstruct &x) {
@@ -1754,7 +1750,7 @@ void OmpVisitor::ProcessMapperSpecifier(const parser::OmpMapperSpecifier &spec,
17541750

17551751
void OmpVisitor::ProcessReductionSpecifier(
17561752
const parser::OmpReductionSpecifier &spec,
1757-
const parser::OmpClauseList &clauses) {
1753+
const std::optional<parser::OmpClauseList> &clauses) {
17581754
const auto &id{std::get<parser::OmpReductionIdentifier>(spec.t)};
17591755
if (auto procDes{std::get_if<parser::ProcedureDesignator>(&id.u)}) {
17601756
if (auto *name{std::get_if<parser::Name>(&procDes->u)}) {
@@ -1796,7 +1792,7 @@ bool OmpVisitor::Pre(const parser::OmpDirectiveSpecification &x) {
17961792
if (maybeArgs && maybeClauses) {
17971793
const parser::OmpArgument &first{maybeArgs->front()};
17981794
if (auto *spec{std::get_if<parser::OmpReductionSpecifier>(&first.u)}) {
1799-
ProcessReductionSpecifier(*spec, *maybeClauses);
1795+
ProcessReductionSpecifier(*spec, maybeClauses);
18001796
}
18011797
}
18021798
break;

flang/test/Parser/OpenMP/declare-reduction-unparse.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ end subroutine initme
2323
!$omp declare reduction(red_add:integer(4):omp_out=omp_out+omp_in) initializer(initme(omp_priv,0))
2424
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareReductionConstruct
2525
!PARSE-TREE: OmpReductionCombiner -> AssignmentStmt = 'omp_out=omp_out+omp_in'
26-
!PARSE-TREE: OmpReductionInitializerClause -> OmpReductionInitializerProc
26+
!PARSE-TREE: OmpInitializerClause -> OmpInitializerProc
2727
!PARSE-TREE-NEXT: ProcedureDesignator -> Name = 'initme'
2828
res=init
2929
!$omp simd reduction(red_add:res)
@@ -58,4 +58,4 @@ end program main
5858
!PARSE-TREE: OmpReductionIdentifier -> ProcedureDesignator -> Name = 'my_add_red'
5959
!PARSE-TREE: DeclarationTypeSpec -> IntrinsicTypeSpec -> IntegerTypeSpec
6060
!PARSE-TREE: OmpReductionCombiner -> AssignmentStmt = 'omp_out=omp_out+omp_in'
61-
!PARSE-TREE: OmpReductionInitializerClause -> OmpReductionInitializerExpr -> Expr = '0_4'
61+
!PARSE-TREE: OmpInitializerClause -> OmpInitializerExpr -> Expr = '0_4'

llvm/include/llvm/Frontend/OpenMP/OMP.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ def OMPC_Indirect : Clause<"indirect"> {
244244
def OMPC_Init : Clause<"init"> {
245245
let clangClass = "OMPInitClause";
246246
}
247+
def OMPC_Initializer : Clause<"initializer"> {
248+
let flangClass = "OmpInitializerClause";
249+
}
247250
def OMPC_InReduction : Clause<"in_reduction"> {
248251
let clangClass = "OMPInReductionClause";
249252
let flangClass = "OmpInReductionClause";
@@ -668,6 +671,9 @@ def OMP_DeclareMapper : Directive<"declare mapper"> {
668671
let category = CA_Declarative;
669672
}
670673
def OMP_DeclareReduction : Directive<"declare reduction"> {
674+
let allowedOnceClauses = [
675+
VersionedClause<OMPC_Initializer>,
676+
];
671677
let association = AS_None;
672678
let category = CA_Declarative;
673679
}

0 commit comments

Comments
 (0)