Skip to content

Commit 1a08b15

Browse files
authored
[flang][OpenMP] Avoid early returns, NFC (#117231)
Frontend code is generally nested. Follow-up to #116658.
1 parent e79cd24 commit 1a08b15

File tree

3 files changed

+77
-73
lines changed

3 files changed

+77
-73
lines changed

flang/lib/Lower/OpenMP/Clauses.h

+10
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ std::optional<ResultTy> maybeApplyToV(FuncTy &&func, const ArgTy *arg) {
163163
return func(arg->v);
164164
}
165165

166+
template <
167+
typename FuncTy, //
168+
typename ArgTy, //
169+
typename ResultTy = std::invoke_result_t<FuncTy, typename ArgTy::Value>>
170+
std::optional<ResultTy> maybeApplyToV(FuncTy &&func, const ArgTy *arg) {
171+
if (!arg)
172+
return std::nullopt;
173+
return std::move(func(arg->v));
174+
}
175+
166176
std::optional<Object> getBaseObject(const Object &object,
167177
semantics::SemanticsContext &semaCtx);
168178

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

+33-37
Original file line numberDiff line numberDiff line change
@@ -2865,45 +2865,41 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Reduction &x) {
28652865

28662866
bool OmpStructureChecker::CheckReductionOperators(
28672867
const parser::OmpClause::Reduction &x) {
2868+
bool ok = false;
28682869
auto &modifiers{OmpGetModifiers(x.v)};
2869-
const auto *definedOp{
2870-
OmpGetUniqueModifier<parser::OmpReductionIdentifier>(modifiers)};
2871-
if (!definedOp) {
2872-
return false;
2870+
if (const auto *ident{
2871+
OmpGetUniqueModifier<parser::OmpReductionIdentifier>(modifiers)}) {
2872+
2873+
auto visitOperator{[&](const parser::DefinedOperator &dOpr) {
2874+
if (const auto *intrinsicOp{
2875+
std::get_if<parser::DefinedOperator::IntrinsicOperator>(
2876+
&dOpr.u)}) {
2877+
ok = CheckIntrinsicOperator(*intrinsicOp);
2878+
} else {
2879+
context_.Say(GetContext().clauseSource,
2880+
"Invalid reduction operator in REDUCTION clause."_err_en_US,
2881+
ContextDirectiveAsFortran());
2882+
}
2883+
}};
2884+
2885+
auto visitDesignator{[&](const parser::ProcedureDesignator &procD) {
2886+
const parser::Name *name{std::get_if<parser::Name>(&procD.u)};
2887+
if (name && name->symbol) {
2888+
const SourceName &realName{name->symbol->GetUltimate().name()};
2889+
if (realName == "max" || realName == "min" || realName == "iand" ||
2890+
realName == "ior" || realName == "ieor") {
2891+
ok = true;
2892+
}
2893+
}
2894+
if (!ok) {
2895+
context_.Say(GetContext().clauseSource,
2896+
"Invalid reduction identifier in REDUCTION "
2897+
"clause."_err_en_US,
2898+
ContextDirectiveAsFortran());
2899+
}
2900+
}};
2901+
common::visit(common::visitors{visitOperator, visitDesignator}, ident->u);
28732902
}
2874-
bool ok = false;
2875-
common::visit(
2876-
common::visitors{
2877-
[&](const parser::DefinedOperator &dOpr) {
2878-
if (const auto *intrinsicOp{
2879-
std::get_if<parser::DefinedOperator::IntrinsicOperator>(
2880-
&dOpr.u)}) {
2881-
ok = CheckIntrinsicOperator(*intrinsicOp);
2882-
} else {
2883-
context_.Say(GetContext().clauseSource,
2884-
"Invalid reduction operator in REDUCTION clause."_err_en_US,
2885-
ContextDirectiveAsFortran());
2886-
}
2887-
},
2888-
[&](const parser::ProcedureDesignator &procD) {
2889-
const parser::Name *name{std::get_if<parser::Name>(&procD.u)};
2890-
if (name && name->symbol) {
2891-
const SourceName &realName{name->symbol->GetUltimate().name()};
2892-
if (realName == "max" || realName == "min" ||
2893-
realName == "iand" || realName == "ior" ||
2894-
realName == "ieor") {
2895-
ok = true;
2896-
}
2897-
}
2898-
if (!ok) {
2899-
context_.Say(GetContext().clauseSource,
2900-
"Invalid reduction identifier in REDUCTION "
2901-
"clause."_err_en_US,
2902-
ContextDirectiveAsFortran());
2903-
}
2904-
},
2905-
},
2906-
definedOp->u);
29072903

29082904
return ok;
29092905
}

flang/lib/Semantics/resolve-directives.cpp

+34-36
Original file line numberDiff line numberDiff line change
@@ -522,49 +522,47 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
522522
const auto &objList{std::get<parser::OmpObjectList>(x.v.t)};
523523
ResolveOmpObjectList(objList, Symbol::Flag::OmpReduction);
524524

525-
auto &modifiers{OmpGetModifiers(x.v)};
526-
if (!modifiers) {
527-
return false;
528-
}
529-
530-
auto createDummyProcSymbol = [&](const parser::Name *name) {
531-
// If name resolution failed, create a dummy symbol
532-
const auto namePair{
533-
currScope().try_emplace(name->source, Attrs{}, ProcEntityDetails{})};
534-
auto &newSymbol{*namePair.first->second};
535-
if (context_.intrinsics().IsIntrinsic(name->ToString())) {
536-
newSymbol.attrs().set(Attr::INTRINSIC);
537-
}
538-
name->symbol = &newSymbol;
539-
};
525+
if (auto &modifiers{OmpGetModifiers(x.v)}) {
526+
auto createDummyProcSymbol = [&](const parser::Name *name) {
527+
// If name resolution failed, create a dummy symbol
528+
const auto namePair{currScope().try_emplace(
529+
name->source, Attrs{}, ProcEntityDetails{})};
530+
auto &newSymbol{*namePair.first->second};
531+
if (context_.intrinsics().IsIntrinsic(name->ToString())) {
532+
newSymbol.attrs().set(Attr::INTRINSIC);
533+
}
534+
name->symbol = &newSymbol;
535+
};
540536

541-
for (auto &mod : *modifiers) {
542-
if (!std::holds_alternative<parser::OmpReductionIdentifier>(mod.u)) {
543-
continue;
544-
}
545-
auto &opr{std::get<parser::OmpReductionIdentifier>(mod.u)};
546-
if (auto *procD{parser::Unwrap<parser::ProcedureDesignator>(opr.u)}) {
547-
if (auto *name{parser::Unwrap<parser::Name>(procD->u)}) {
548-
if (!name->symbol) {
549-
if (!ResolveName(name)) {
550-
createDummyProcSymbol(name);
537+
for (auto &mod : *modifiers) {
538+
if (!std::holds_alternative<parser::OmpReductionIdentifier>(mod.u)) {
539+
continue;
540+
}
541+
auto &opr{std::get<parser::OmpReductionIdentifier>(mod.u)};
542+
if (auto *procD{parser::Unwrap<parser::ProcedureDesignator>(opr.u)}) {
543+
if (auto *name{parser::Unwrap<parser::Name>(procD->u)}) {
544+
if (!name->symbol) {
545+
if (!ResolveName(name)) {
546+
createDummyProcSymbol(name);
547+
}
551548
}
552549
}
553-
}
554-
if (auto *procRef{parser::Unwrap<parser::ProcComponentRef>(procD->u)}) {
555-
if (!procRef->v.thing.component.symbol) {
556-
if (!ResolveName(&procRef->v.thing.component)) {
557-
createDummyProcSymbol(&procRef->v.thing.component);
550+
if (auto *procRef{
551+
parser::Unwrap<parser::ProcComponentRef>(procD->u)}) {
552+
if (!procRef->v.thing.component.symbol) {
553+
if (!ResolveName(&procRef->v.thing.component)) {
554+
createDummyProcSymbol(&procRef->v.thing.component);
555+
}
558556
}
559557
}
560558
}
561559
}
562-
}
563-
using ReductionModifier = parser::OmpReductionModifier;
564-
if (auto *maybeModifier{
565-
OmpGetUniqueModifier<ReductionModifier>(modifiers)}) {
566-
if (maybeModifier->v == ReductionModifier::Value::Inscan) {
567-
ResolveOmpObjectList(objList, Symbol::Flag::OmpInScanReduction);
560+
using ReductionModifier = parser::OmpReductionModifier;
561+
if (auto *maybeModifier{
562+
OmpGetUniqueModifier<ReductionModifier>(modifiers)}) {
563+
if (maybeModifier->v == ReductionModifier::Value::Inscan) {
564+
ResolveOmpObjectList(objList, Symbol::Flag::OmpInScanReduction);
565+
}
568566
}
569567
}
570568
return false;

0 commit comments

Comments
 (0)