Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][OpenMP] Avoid early returns, NFC #117231

Merged
merged 17 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions flang/examples/FeatureList/FeatureList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,9 @@ struct NodeVisitor {
READ_FEATURE(OmpObject)
READ_FEATURE(OmpObjectList)
READ_FEATURE(OmpOrderClause)
READ_FEATURE(OmpOrderClause::Type)
READ_FEATURE(OmpOrderClause::Ordering)
READ_FEATURE(OmpOrderModifier)
READ_FEATURE(OmpOrderModifier::Kind)
READ_FEATURE(OmpOrderModifier::Value)
READ_FEATURE(OmpProcBindClause)
READ_FEATURE(OmpProcBindClause::Type)
READ_FEATURE(OmpReductionClause)
Expand All @@ -522,16 +522,16 @@ struct NodeVisitor {
READ_FEATURE(OmpAllocateClause::AllocateModifier::ComplexModifier)
READ_FEATURE(OmpAllocateClause::AllocateModifier::Align)
READ_FEATURE(OmpScheduleClause)
READ_FEATURE(OmpScheduleClause::ScheduleType)
READ_FEATURE(OmpScheduleClause::Kind)
READ_FEATURE(OmpScheduleClause::Modifier)
READ_FEATURE(OmpDeviceClause)
READ_FEATURE(OmpDeviceClause::DeviceModifier)
READ_FEATURE(OmpDeviceTypeClause)
READ_FEATURE(OmpDeviceTypeClause::Type)
READ_FEATURE(OmpScheduleModifier)
READ_FEATURE(OmpScheduleModifier::Modifier1)
READ_FEATURE(OmpScheduleModifier::Modifier2)
READ_FEATURE(OmpScheduleModifierType)
READ_FEATURE(OmpScheduleModifierType::ModType)
READ_FEATURE(OmpChunkModifier)
READ_FEATURE(OmpChunkModifier::Value)
READ_FEATURE(OmpOrderingModifier)
READ_FEATURE(OmpOrderingModifier::Value)
READ_FEATURE(OmpSectionBlocks)
READ_FEATURE(OmpSectionsDirective)
READ_FEATURE(OmpSimpleStandaloneDirective)
Expand Down
10 changes: 7 additions & 3 deletions flang/examples/FlangOmpReport/FlangOmpReportVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,26 @@ void OpenMPCounterVisitor::Post(const OmpVariableCategory::Value &c) {
"variable_category=" + std::string{OmpVariableCategory::EnumToString(c)} +
";";
}
void OpenMPCounterVisitor::Post(const OmpScheduleModifierType::ModType &c) {
void OpenMPCounterVisitor::Post(const OmpChunkModifier::Value &c) {
clauseDetails +=
"modifier=" + std::string{OmpScheduleModifierType::EnumToString(c)} + ";";
"modifier=" + std::string{OmpChunkModifier::EnumToString(c)} + ";";
}
void OpenMPCounterVisitor::Post(const OmpLinearModifier::Value &c) {
clauseDetails +=
"modifier=" + std::string{OmpLinearModifier::EnumToString(c)} + ";";
}
void OpenMPCounterVisitor::Post(const OmpOrderingModifier::Value &c) {
clauseDetails +=
"modifier=" + std::string{OmpOrderingModifier::EnumToString(c)} + ";";
}
void OpenMPCounterVisitor::Post(const OmpTaskDependenceType::Value &c) {
clauseDetails +=
"type=" + std::string{OmpTaskDependenceType::EnumToString(c)} + ";";
}
void OpenMPCounterVisitor::Post(const OmpMapClause::Type &c) {
clauseDetails += "type=" + std::string{OmpMapClause::EnumToString(c)} + ";";
}
void OpenMPCounterVisitor::Post(const OmpScheduleClause::ScheduleType &c) {
void OpenMPCounterVisitor::Post(const OmpScheduleClause::Kind &c) {
clauseDetails +=
"type=" + std::string{OmpScheduleClause::EnumToString(c)} + ";";
}
Expand Down
5 changes: 3 additions & 2 deletions flang/examples/FlangOmpReport/FlangOmpReportVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ struct OpenMPCounterVisitor {
void Post(const OmpDefaultmapClause::ImplicitBehavior &c);
void Post(const OmpVariableCategory::Value &c);
void Post(const OmpDeviceTypeClause::Type &c);
void Post(const OmpScheduleModifierType::ModType &c);
void Post(const OmpChunkModifier::Value &c);
void Post(const OmpLinearModifier::Value &c);
void Post(const OmpOrderingModifier::Value &c);
void Post(const OmpTaskDependenceType::Value &c);
void Post(const OmpMapClause::Type &c);
void Post(const OmpScheduleClause::ScheduleType &c);
void Post(const OmpScheduleClause::Kind &c);
void Post(const OmpIfClause::DirectiveNameModifier &c);
void Post(const OmpCancelType::Type &c);
void Post(const OmpClause &c);
Expand Down
17 changes: 9 additions & 8 deletions flang/include/flang/Parser/dump-parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,10 @@ class ParseTreeDumper {
NODE(parser, OmpObject)
NODE(parser, OmpObjectList)
NODE(parser, OmpOrderClause)
NODE_ENUM(OmpOrderClause, Type)
NODE(OmpOrderClause, Modifier)
NODE_ENUM(OmpOrderClause, Ordering)
NODE(parser, OmpOrderModifier)
NODE_ENUM(OmpOrderModifier, Kind)
NODE_ENUM(OmpOrderModifier, Value)
NODE(parser, OmpGrainsizeClause)
NODE_ENUM(OmpGrainsizeClause, Prescriptiveness)
NODE(parser, OmpNumTasksClause)
Expand All @@ -585,17 +586,17 @@ class ParseTreeDumper {
NODE(OmpAllocateClause::AllocateModifier, ComplexModifier)
NODE(OmpAllocateClause::AllocateModifier, Align)
NODE(parser, OmpScheduleClause)
NODE_ENUM(OmpScheduleClause, ScheduleType)
NODE(OmpScheduleClause, Modifier)
NODE_ENUM(OmpScheduleClause, Kind)
NODE(parser, OmpDeviceClause)
NODE_ENUM(OmpDeviceClause, DeviceModifier)
NODE(parser, OmpDeviceTypeClause)
NODE_ENUM(OmpDeviceTypeClause, Type)
NODE(parser, OmpUpdateClause)
NODE(parser, OmpScheduleModifier)
NODE(OmpScheduleModifier, Modifier1)
NODE(OmpScheduleModifier, Modifier2)
NODE(parser, OmpScheduleModifierType)
NODE_ENUM(OmpScheduleModifierType, ModType)
NODE(parser, OmpChunkModifier)
NODE_ENUM(OmpChunkModifier, Value)
NODE(parser, OmpOrderingModifier)
NODE_ENUM(OmpOrderingModifier, Value)
NODE(parser, OmpSectionBlocks)
NODE(parser, OmpSectionsDirective)
NODE(parser, OmpSimpleStandaloneDirective)
Expand Down
81 changes: 54 additions & 27 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3457,6 +3457,17 @@ inline namespace modifier {
// ENUM_CLASS(Value, Keyword1, Keyword2);
// };

// Ref: [5.2:252-254]
//
// chunk-modifier ->
// SIMD // since 5.2
//
// Prior to 5.2 "chunk-modifier" was a part of "modifier" on SCHEDULE clause.
struct OmpChunkModifier {
ENUM_CLASS(Value, Simd)
WRAPPER_CLASS_BOILERPLATE(OmpChunkModifier, Value);
};

// Ref: [5.0:47-49], [5.1:49-51], [5.2:67-69]
//
// iterator-specifier ->
Expand Down Expand Up @@ -3508,6 +3519,30 @@ struct OmpLinearModifier {
WRAPPER_CLASS_BOILERPLATE(OmpLinearModifier, Value);
};

// Ref: [4.5:56-63], [5.0:101-109], [5.1:126-133], [5.2:252-254]
//
// modifier ->
// MONOTONIC | NONMONOTONIC | SIMD // since 4.5, until 5.1
// ordering-modifier ->
// MONOTONIC | NONMONOTONIC // since 5.2
//
// Until 5.1, the SCHEDULE clause accepted up to two instances of "modifier".
// Since 5.2 "modifier" was replaced with "ordering-modifier" and "chunk-
// modifier".
struct OmpOrderingModifier {
ENUM_CLASS(Value, Monotonic, Nonmonotonic, Simd)
WRAPPER_CLASS_BOILERPLATE(OmpOrderingModifier, Value);
};

// Ref: [5.1:125-126], [5.2:233-234]
//
// order-modifier ->
// REPRODUCIBLE | UNCONSTRAINED // since 5.1
struct OmpOrderModifier {
ENUM_CLASS(Value, Reproducible, Unconstrained)
WRAPPER_CLASS_BOILERPLATE(OmpOrderModifier, Value);
};

// Ref: [4.5:201-207], [5.0:293-299], [5.1:325-331], [5.2:124]
//
// reduction-identifier ->
Expand Down Expand Up @@ -3786,16 +3821,16 @@ struct OmpMapClause {
t;
};

// 2.9.5 order-clause -> ORDER ([order-modifier :]concurrent)
struct OmpOrderModifier {
ENUM_CLASS(Kind, Reproducible, Unconstrained)
WRAPPER_CLASS_BOILERPLATE(OmpOrderModifier, Kind);
};

// Ref: [5.0:101-109], [5.1:126-134], [5.2:233-234]
//
// order-clause ->
// ORDER(CONCURRENT) | // since 5.0
// ORDER([order-modifier:] CONCURRENT) // since 5.1
struct OmpOrderClause {
TUPLE_CLASS_BOILERPLATE(OmpOrderClause);
ENUM_CLASS(Type, Concurrent)
std::tuple<std::optional<OmpOrderModifier>, Type> t;
ENUM_CLASS(Ordering, Concurrent)
MODIFIER_BOILERPLATE(OmpOrderModifier);
std::tuple<MODIFIERS(), Ordering> t;
};

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

// 2.7.1 sched-modifier -> MONOTONIC | NONMONOTONIC | SIMD
struct OmpScheduleModifierType {
ENUM_CLASS(ModType, Monotonic, Nonmonotonic, Simd)
WRAPPER_CLASS_BOILERPLATE(OmpScheduleModifierType, ModType);
};

struct OmpScheduleModifier {
TUPLE_CLASS_BOILERPLATE(OmpScheduleModifier);
WRAPPER_CLASS(Modifier1, OmpScheduleModifierType);
WRAPPER_CLASS(Modifier2, OmpScheduleModifierType);
std::tuple<Modifier1, std::optional<Modifier2>> t;
};

// 2.7.1 schedule-clause -> SCHEDULE ([sched-modifier1] [, sched-modifier2]:]
// kind[, chunk_size])
// Ref: [4.5:56-63], [5.0:101-109], [5.1:126-133], [5.2:252-254]
//
// schedule-clause ->
// SCHEDULE([modifier[, modifier]:]
// kind[, chunk-size]) // since 4.5, until 5.1
// schedule-clause ->
// SCHEDULE([ordering-modifier], chunk-modifier],
// kind[, chunk_size]) // since 5.2
struct OmpScheduleClause {
TUPLE_CLASS_BOILERPLATE(OmpScheduleClause);
ENUM_CLASS(ScheduleType, Static, Dynamic, Guided, Auto, Runtime)
std::tuple<std::optional<OmpScheduleModifier>, ScheduleType,
std::optional<ScalarIntExpr>>
t;
ENUM_CLASS(Kind, Static, Dynamic, Guided, Auto, Runtime)
MODIFIER_BOILERPLATE(OmpOrderingModifier, OmpChunkModifier);
std::tuple<MODIFIERS(), Kind, std::optional<ScalarIntExpr>> t;
};

// Ref: [4.5:107-109], [5.0:176-180], [5.1:205-210], [5.2:167-168]
Expand Down
6 changes: 6 additions & 0 deletions flang/include/flang/Semantics/openmp-modifiers.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,19 @@ struct OmpModifierDescriptor {

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

template <>
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpChunkModifier>();
template <>
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpDependenceType>();
template <>
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpIterator>();
template <>
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpLinearModifier>();
template <>
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpOrderModifier>();
template <>
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpOrderingModifier>();
template <>
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpReductionIdentifier>();
template <>
const OmpModifierDescriptor &OmpGetDescriptor<parser::OmpReductionModifier>();
Expand Down
75 changes: 24 additions & 51 deletions flang/lib/Lower/OpenMP/Clauses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,28 +1109,26 @@ Order make(const parser::OmpClause::Order &inp,
using wrapped = parser::OmpOrderClause;

CLAUSET_ENUM_CONVERT( //
convert1, parser::OmpOrderModifier::Kind, Order::OrderModifier,
convert1, parser::OmpOrderModifier::Value, Order::OrderModifier,
// clang-format off
MS(Reproducible, Reproducible)
MS(Unconstrained, Unconstrained)
// clang-format on
);

CLAUSET_ENUM_CONVERT( //
convert2, wrapped::Type, Order::Ordering,
convert2, wrapped::Ordering, Order::Ordering,
// clang-format off
MS(Concurrent, Concurrent)
// clang-format on
);

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

auto convert3 = [&](const parser::OmpOrderModifier &s) {
return convert1(s.v);
};
return Order{
{/*OrderModifier=*/maybeApply(convert3, t0), /*Ordering=*/convert2(t1)}};
return Order{{/*OrderModifier=*/maybeApplyToV(convert1, t0),
/*Ordering=*/convert2(t1)}};
}

Ordered make(const parser::OmpClause::Ordered &inp,
Expand Down Expand Up @@ -1197,10 +1195,10 @@ Reduction make(const parser::OmpClause::Reduction &inp,
auto *t1 =
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
auto &t2 = std::get<parser::OmpObjectList>(inp.v.t);
assert(t1 && "OmpReductionIdentifier is required");

return Reduction{
{/*ReductionModifier=*/t0
? std::make_optional<Reduction::ReductionModifier>(convert(t0->v))
: std::nullopt,
{/*ReductionModifier=*/maybeApplyToV(convert, t0),
/*ReductionIdentifiers=*/{makeReductionOperator(*t1, semaCtx)},
/*List=*/makeObjects(t2, semaCtx)}};
}
Expand All @@ -1221,7 +1219,7 @@ Schedule make(const parser::OmpClause::Schedule &inp,
using wrapped = parser::OmpScheduleClause;

CLAUSET_ENUM_CONVERT( //
convert1, wrapped::ScheduleType, Schedule::Kind,
convert1, wrapped::Kind, Schedule::Kind,
// clang-format off
MS(Static, Static)
MS(Dynamic, Dynamic)
Expand All @@ -1232,57 +1230,30 @@ Schedule make(const parser::OmpClause::Schedule &inp,
);

CLAUSET_ENUM_CONVERT( //
convert2, parser::OmpScheduleModifierType::ModType,
Schedule::OrderingModifier,
convert2, parser::OmpOrderingModifier::Value, Schedule::OrderingModifier,
// clang-format off
MS(Monotonic, Monotonic)
MS(Nonmonotonic, Nonmonotonic)
// clang-format on
);

CLAUSET_ENUM_CONVERT( //
convert3, parser::OmpScheduleModifierType::ModType,
Schedule::ChunkModifier,
convert3, parser::OmpChunkModifier::Value, Schedule::ChunkModifier,
// clang-format off
MS(Simd, Simd)
// clang-format on
);

auto &t0 = std::get<std::optional<parser::OmpScheduleModifier>>(inp.v.t);
auto &t1 = std::get<wrapped::ScheduleType>(inp.v.t);
auto &t2 = std::get<std::optional<parser::ScalarIntExpr>>(inp.v.t);

if (!t0) {
return Schedule{{/*Kind=*/convert1(t1), /*OrderingModifier=*/std::nullopt,
/*ChunkModifier=*/std::nullopt,
/*ChunkSize=*/maybeApply(makeExprFn(semaCtx), t2)}};
}

// The members of parser::OmpScheduleModifier correspond to OrderingModifier,
// and ChunkModifier, but they can appear in any order.
auto &m1 = std::get<parser::OmpScheduleModifier::Modifier1>(t0->t);
auto &m2 =
std::get<std::optional<parser::OmpScheduleModifier::Modifier2>>(t0->t);

std::optional<Schedule::OrderingModifier> omod;
std::optional<Schedule::ChunkModifier> cmod;

if (m1.v.v == parser::OmpScheduleModifierType::ModType::Simd) {
// m1 is chunk-modifier
cmod = convert3(m1.v.v);
if (m2)
omod = convert2(m2->v.v);
} else {
// m1 is ordering-modifier
omod = convert2(m1.v.v);
if (m2)
cmod = convert3(m2->v.v);
}
auto &mods = semantics::OmpGetModifiers(inp.v);
auto *t0 = semantics::OmpGetUniqueModifier<parser::OmpOrderingModifier>(mods);
auto *t1 = semantics::OmpGetUniqueModifier<parser::OmpChunkModifier>(mods);
auto &t2 = std::get<wrapped::Kind>(inp.v.t);
auto &t3 = std::get<std::optional<parser::ScalarIntExpr>>(inp.v.t);

return Schedule{{/*Kind=*/convert1(t1),
/*OrderingModifier=*/omod,
/*ChunkModifier=*/cmod,
/*ChunkSize=*/maybeApply(makeExprFn(semaCtx), t2)}};
return Schedule{{/*Kind=*/convert1(t2),
/*OrderingModifier=*/maybeApplyToV(convert2, t0),
/*ChunkModifier=*/maybeApplyToV(convert3, t1),
/*ChunkSize=*/maybeApply(makeExprFn(semaCtx), t3)}};
}

// SeqCst: empty
Expand Down Expand Up @@ -1326,6 +1297,8 @@ TaskReduction make(const parser::OmpClause::TaskReduction &inp,
auto *t0 =
semantics::OmpGetUniqueModifier<parser::OmpReductionIdentifier>(mods);
auto &t1 = std::get<parser::OmpObjectList>(inp.v.t);
assert(t0 && "OmpReductionIdentifier is required");

return TaskReduction{
{/*ReductionIdentifiers=*/{makeReductionOperator(*t0, semaCtx)},
/*List=*/makeObjects(t1, semaCtx)}};
Expand Down
Loading
Loading