Skip to content

Commit dd63dff

Browse files
authored
[flang][OpenMP] Parse OpenMP 6.0 syntax of INIT clause (#171702)
This includes `FR(...)` for foreign runtime identifiers and `ATTR(...)` for extensions. Do not store string runtime ids as character literals in the AST. Use parser::Expr instead, since lowering would require evaluate::Expr for these ids, and we get evaluate::Expr from parser::Expr automatically. Use OpenMP 6.0 naming for AST nodes (since it's the "current" one).
1 parent b2dae2b commit dd63dff

File tree

8 files changed

+196
-212
lines changed

8 files changed

+196
-212
lines changed

flang/examples/FeatureList/FeatureList.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,9 @@ struct NodeVisitor {
495495
READ_FEATURE(OmpOrderClause::Ordering)
496496
READ_FEATURE(OmpOrderModifier)
497497
READ_FEATURE(OmpOrderModifier::Value)
498+
READ_FEATURE(OmpPreferenceSelector)
499+
READ_FEATURE(OmpPreferenceSpecification)
500+
READ_FEATURE(OmpPreferType)
498501
READ_FEATURE(OmpProcBindClause)
499502
READ_FEATURE(OmpProcBindClause::AffinityPolicy)
500503
READ_FEATURE(OmpReductionClause)
@@ -509,8 +512,6 @@ struct NodeVisitor {
509512
READ_FEATURE(OmpScheduleClause)
510513
READ_FEATURE(OmpScheduleClause::Kind)
511514
READ_FEATURE(OmpScheduleClause::Modifier)
512-
READ_FEATURE(OmpInteropRuntimeIdentifier)
513-
READ_FEATURE(OmpInteropPreference)
514515
READ_FEATURE(OmpInteropType)
515516
READ_FEATURE(OmpInteropType::Value)
516517
READ_FEATURE(OmpInitClause)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,6 @@ class ParseTreeDumper {
627627
NODE(parser, OmpInitializerExpression)
628628
NODE(parser, OmpInReductionClause)
629629
NODE(OmpInReductionClause, Modifier)
630-
NODE(parser, OmpInteropPreference)
631-
NODE(parser, OmpInteropRuntimeIdentifier)
632630
NODE(parser, OmpInteropType)
633631
NODE_ENUM(OmpInteropType, Value)
634632
NODE(parser, OmpIteration)
@@ -681,6 +679,9 @@ class ParseTreeDumper {
681679
NODE(parser, OmpOrderModifier)
682680
NODE_ENUM(OmpOrderModifier, Value)
683681
NODE(parser, OmpOtherwiseClause)
682+
NODE(parser, OmpPreferenceSelector)
683+
NODE(parser, OmpPreferenceSpecification)
684+
NODE(parser, OmpPreferType)
684685
NODE(parser, OmpPrescriptiveness)
685686
NODE_ENUM(OmpPrescriptiveness, Value)
686687
NODE(parser, OmpPresentModifier)

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4031,27 +4031,45 @@ struct OmpFallbackModifier {
40314031
WRAPPER_CLASS_BOILERPLATE(OmpFallbackModifier, Value);
40324032
};
40334033

4034-
// REF: [5.1:217-220], [5.2:293-294]
4034+
// Ref: [6.0:470-471]
40354035
//
4036-
// OmpInteropRuntimeIdentifier -> // since 5.2
4037-
// CharLiteralConstant || ScalarIntConstantExpr
4038-
struct OmpInteropRuntimeIdentifier {
4039-
UNION_CLASS_BOILERPLATE(OmpInteropRuntimeIdentifier);
4040-
std::variant<CharLiteralConstant, ScalarIntConstantExpr> u;
4036+
// preference-selector -> // since 6.0
4037+
// FR(foreign-runtime-identifier) |
4038+
// ATTR(preference-property-extension, ...)
4039+
struct OmpPreferenceSelector {
4040+
UNION_CLASS_BOILERPLATE(OmpPreferenceSelector);
4041+
using ForeignRuntimeIdentifier = common::Indirection<Expr>;
4042+
using PreferencePropertyExtension = common::Indirection<Expr>;
4043+
using Extensions = std::list<PreferencePropertyExtension>;
4044+
std::variant<ForeignRuntimeIdentifier, Extensions> u;
40414045
};
40424046

4043-
// REF: [5.1:217-220], [5.2:293-294]
4047+
// Ref: [6.0:470-471]
4048+
//
4049+
// preference-specification ->
4050+
// {preference-selector...} | // since 6.0
4051+
// foreign-runtime-identifier // since 5.1
4052+
struct OmpPreferenceSpecification {
4053+
UNION_CLASS_BOILERPLATE(OmpPreferenceSpecification);
4054+
using ForeignRuntimeIdentifier =
4055+
OmpPreferenceSelector::ForeignRuntimeIdentifier;
4056+
std::variant<std::list<OmpPreferenceSelector>, ForeignRuntimeIdentifier> u;
4057+
};
4058+
4059+
// REF: [5.1:217-220], [5.2:293-294], [6.0:470-471]
40444060
//
4045-
// OmpInteropPreference -> // since 5.2
4046-
// ([OmpRuntimeIdentifier, ...])
4047-
struct OmpInteropPreference {
4061+
// prefer-type -> // since 5.1
4062+
// PREFER_TYPE(preference-specification...)
4063+
struct OmpPreferType {
40484064
WRAPPER_CLASS_BOILERPLATE(
4049-
OmpInteropPreference, std::list<OmpInteropRuntimeIdentifier>);
4065+
OmpPreferType, std::list<OmpPreferenceSpecification>);
40504066
};
40514067

4052-
// REF: [5.1:217-220], [5.2:293-294]
4068+
// REF: [5.1:217-220], [5.2:293-294], [6.0:470-471]
40534069
//
4054-
// InteropType -> target || targetsync // since 5.2
4070+
// interop-type -> // since 5.1
4071+
// TARGET |
4072+
// TARGETSYNC
40554073
// There can be at most only two interop-type.
40564074
struct OmpInteropType {
40574075
ENUM_CLASS(Value, Target, TargetSync)
@@ -4997,7 +5015,7 @@ struct OmpWhenClause {
49975015
// There can be at most only two interop-type.
49985016
struct OmpInitClause {
49995017
TUPLE_CLASS_BOILERPLATE(OmpInitClause);
5000-
MODIFIER_BOILERPLATE(OmpInteropPreference, OmpInteropType);
5018+
MODIFIER_BOILERPLATE(OmpPreferType, OmpInteropType);
50015019
std::tuple<MODIFIERS(), OmpObject> t;
50025020
};
50035021

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ DECLARE_DESCRIPTOR(parser::OmpDimsModifier);
8585
DECLARE_DESCRIPTOR(parser::OmpDirectiveNameModifier);
8686
DECLARE_DESCRIPTOR(parser::OmpExpectation);
8787
DECLARE_DESCRIPTOR(parser::OmpFallbackModifier);
88-
DECLARE_DESCRIPTOR(parser::OmpInteropPreference);
8988
DECLARE_DESCRIPTOR(parser::OmpInteropType);
9089
DECLARE_DESCRIPTOR(parser::OmpIterator);
9190
DECLARE_DESCRIPTOR(parser::OmpLastprivateModifier);
@@ -96,6 +95,7 @@ DECLARE_DESCRIPTOR(parser::OmpMapType);
9695
DECLARE_DESCRIPTOR(parser::OmpMapTypeModifier);
9796
DECLARE_DESCRIPTOR(parser::OmpOrderModifier);
9897
DECLARE_DESCRIPTOR(parser::OmpOrderingModifier);
98+
DECLARE_DESCRIPTOR(parser::OmpPreferType);
9999
DECLARE_DESCRIPTOR(parser::OmpPrescriptiveness);
100100
DECLARE_DESCRIPTOR(parser::OmpPresentModifier);
101101
DECLARE_DESCRIPTOR(parser::OmpReductionIdentifier);

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -831,13 +831,6 @@ TYPE_PARSER(construct<OmpFallbackModifier>("FALLBACK"_tok >>
831831
"DEFAULT_MEM" >> pure(OmpFallbackModifier::Value::Default_Mem) ||
832832
"NULL" >> pure(OmpFallbackModifier::Value::Null))))
833833

834-
TYPE_PARSER(construct<OmpInteropRuntimeIdentifier>(
835-
construct<OmpInteropRuntimeIdentifier>(charLiteralConstant) ||
836-
construct<OmpInteropRuntimeIdentifier>(scalarIntConstantExpr)))
837-
838-
TYPE_PARSER(construct<OmpInteropPreference>(verbatim("PREFER_TYPE"_tok) >>
839-
parenthesized(nonemptyList(Parser<OmpInteropRuntimeIdentifier>{}))))
840-
841834
TYPE_PARSER(construct<OmpInteropType>(
842835
"TARGETSYNC" >> pure(OmpInteropType::Value::TargetSync) ||
843836
"TARGET" >> pure(OmpInteropType::Value::Target)))
@@ -898,6 +891,20 @@ TYPE_PARSER(construct<OmpOrderingModifier>(
898891
"NONMONOTONIC" >> pure(OmpOrderingModifier::Value::Nonmonotonic) ||
899892
"SIMD" >> pure(OmpOrderingModifier::Value::Simd)))
900893

894+
TYPE_PARSER( //
895+
construct<OmpPreferenceSelector>("FR" >> parenthesized(indirect(expr))) ||
896+
construct<OmpPreferenceSelector>(
897+
"ATTR" >> parenthesized(nonemptyList(indirect(expr)))))
898+
899+
TYPE_PARSER( //
900+
construct<OmpPreferenceSpecification>(
901+
braced(nonemptyList(Parser<OmpPreferenceSelector>()))) ||
902+
construct<OmpPreferenceSpecification>(indirect(expr)))
903+
904+
TYPE_PARSER(construct<OmpPreferType>( //
905+
"PREFER_TYPE" >>
906+
parenthesized(nonemptyList(Parser<OmpPreferenceSpecification>{}))))
907+
901908
TYPE_PARSER(construct<OmpPrescriptiveness>(
902909
"STRICT" >> pure(OmpPrescriptiveness::Value::Strict)))
903910

@@ -986,9 +993,9 @@ TYPE_PARSER(sourced(
986993
construct<OmpIfClause::Modifier>(Parser<OmpDirectiveNameModifier>{})))
987994

988995
TYPE_PARSER(sourced(
989-
construct<OmpInitClause::Modifier>(
990-
construct<OmpInitClause::Modifier>(Parser<OmpInteropPreference>{})) ||
991-
construct<OmpInitClause::Modifier>(Parser<OmpInteropType>{})))
996+
// Try interop-type first, since prefer-type can take arbitrary strings.
997+
construct<OmpInitClause::Modifier>(Parser<OmpInteropType>{}) ||
998+
construct<OmpInitClause::Modifier>(Parser<OmpPreferType>{})))
992999

9931000
TYPE_PARSER(sourced(construct<OmpInReductionClause::Modifier>(
9941001
Parser<OmpReductionIdentifier>{})))
@@ -1487,8 +1494,8 @@ TYPE_PARSER( //
14871494
"INBRANCH" >> construct<OmpClause>(construct<OmpClause::Inbranch>()) ||
14881495
"INDIRECT" >> construct<OmpClause>(construct<OmpClause::Indirect>(
14891496
maybe(parenthesized(scalarLogicalExpr)))) ||
1490-
"INIT" >> construct<OmpClause>(construct<OmpClause::Init>(
1491-
parenthesized(Parser<OmpInitClause>{}))) ||
1497+
"INIT"_id >> construct<OmpClause>(construct<OmpClause::Init>(
1498+
parenthesized(Parser<OmpInitClause>{}))) ||
14921499
"INCLUSIVE" >> construct<OmpClause>(construct<OmpClause::Inclusive>(
14931500
parenthesized(Parser<OmpObjectList>{}))) ||
14941501
"INITIALIZER" >> construct<OmpClause>(construct<OmpClause::Initializer>(

flang/lib/Parser/unparse.cpp

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,28 +2260,47 @@ class UnparseVisitor {
22602260
Walk(std::get<std::optional<std::list<Modifier>>>(x.t), ": ");
22612261
Walk(std::get<OmpObjectList>(x.t));
22622262
}
2263-
void Unparse(const OmpInteropPreference &x) { Walk(x.v, ","); }
2263+
void Unparse(const OmpPreferenceSelector &x) {
2264+
common::visit( //
2265+
common::visitors{
2266+
[&](const OmpPreferenceSelector::ForeignRuntimeIdentifier &s) {
2267+
Word("FR");
2268+
Put("(");
2269+
Walk(s);
2270+
Put(")");
2271+
},
2272+
[&](const OmpPreferenceSelector::Extensions &s) {
2273+
Word("ATTR");
2274+
Put("(");
2275+
Walk(s, ", ");
2276+
Put(")");
2277+
},
2278+
},
2279+
x.u);
2280+
}
2281+
void Unparse(const OmpPreferenceSpecification &x) {
2282+
common::visit( //
2283+
common::visitors{
2284+
[&](const std::list<OmpPreferenceSelector> &s) {
2285+
Put("{");
2286+
Walk(s, ", ");
2287+
Put("}");
2288+
},
2289+
[&](const OmpPreferenceSelector::ForeignRuntimeIdentifier &s) {
2290+
Walk(s);
2291+
},
2292+
},
2293+
x.u);
2294+
}
2295+
void Unparse(const OmpPreferType &x) {
2296+
Word("PREFER_TYPE");
2297+
Put("(");
2298+
Walk(x.v, ", ");
2299+
Put(")");
2300+
}
22642301
void Unparse(const OmpInitClause &x) {
22652302
using Modifier = OmpInitClause::Modifier;
2266-
auto &modifiers{std::get<std::optional<std::list<Modifier>>>(x.t)};
2267-
bool isTypeStart{true};
2268-
for (const Modifier &m : *modifiers) {
2269-
if (auto *interopPreferenceMod{
2270-
std::get_if<parser::OmpInteropPreference>(&m.u)}) {
2271-
Put("PREFER_TYPE(");
2272-
Walk(*interopPreferenceMod);
2273-
Put("),");
2274-
} else if (auto *interopTypeMod{
2275-
std::get_if<parser::OmpInteropType>(&m.u)}) {
2276-
if (isTypeStart) {
2277-
isTypeStart = false;
2278-
} else {
2279-
Put(",");
2280-
}
2281-
Walk(*interopTypeMod);
2282-
}
2283-
}
2284-
Put(": ");
2303+
Walk(std::get<std::optional<std::list<Modifier>>>(x.t), ": ");
22852304
Walk(std::get<OmpObject>(x.t));
22862305
}
22872306
void Unparse(const OmpMapClause &x) {
@@ -2578,15 +2597,8 @@ class UnparseVisitor {
25782597
}
25792598
void Unparse(const OpenMPInteropConstruct &x) {
25802599
BeginOpenMP();
2581-
Word("!$OMP INTEROP");
2582-
auto flags{std::get<OmpDirectiveSpecification::Flags>(x.v.t)};
2583-
if (flags.test(OmpDirectiveSpecification::Flag::DeprecatedSyntax)) {
2584-
Walk("(", std::get<std::optional<OmpArgumentList>>(x.v.t), ")");
2585-
Walk(" ", std::get<std::optional<OmpClauseList>>(x.v.t));
2586-
} else {
2587-
Walk(" ", std::get<std::optional<OmpClauseList>>(x.v.t));
2588-
Walk(" (", std::get<std::optional<OmpArgumentList>>(x.v.t), ")");
2589-
}
2600+
Word("!$OMP ");
2601+
Walk(x.v);
25902602
Put("\n");
25912603
EndOpenMP();
25922604
}

flang/lib/Semantics/openmp-modifiers.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -371,22 +371,6 @@ const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpFallbackModifier>() {
371371
return desc;
372372
}
373373

374-
template <>
375-
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpInteropPreference>() {
376-
static const OmpModifierDescriptor desc{
377-
/*name=*/"interop-preference",
378-
/*props=*/
379-
{
380-
{52, {OmpProperty::Unique}},
381-
},
382-
/*clauses=*/
383-
{
384-
{52, {Clause::OMPC_init}},
385-
},
386-
};
387-
return desc;
388-
}
389-
390374
template <>
391375
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpInteropType>() {
392376
static const OmpModifierDescriptor desc{
@@ -553,6 +537,22 @@ const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpOrderingModifier>() {
553537
return desc;
554538
}
555539

540+
template <>
541+
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpPreferType>() {
542+
static const OmpModifierDescriptor desc{
543+
/*name=*/"prefer-type",
544+
/*props=*/
545+
{
546+
{52, {OmpProperty::Unique}},
547+
},
548+
/*clauses=*/
549+
{
550+
{52, {Clause::OMPC_init}},
551+
},
552+
};
553+
return desc;
554+
}
555+
556556
template <>
557557
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpPrescriptiveness>() {
558558
static const OmpModifierDescriptor desc{

0 commit comments

Comments
 (0)