Skip to content

Commit aac1d97

Browse files
[Flang][OpenMP] Consider renames when processing reduction intrinsics (#70822)
Fixes #68654 Depends on #70790
1 parent 497a860 commit aac1d97

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

flang/lib/Lower/OpenMP.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,9 @@ class ReductionProcessor {
745745
const Fortran::parser::ProcedureDesignator &pd) {
746746
const auto *name{Fortran::parser::Unwrap<Fortran::parser::Name>(pd)};
747747
assert(name && "Invalid Reduction Intrinsic.");
748+
if (!name->symbol->GetUltimate().attrs().test(
749+
Fortran::semantics::Attr::INTRINSIC))
750+
return false;
748751
auto redType = llvm::StringSwitch<std::optional<IntrinsicProc>>(
749752
getRealName(name).ToString())
750753
.Case("max", IntrinsicProc::MAX)

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,18 +2299,20 @@ bool OmpStructureChecker::CheckReductionOperators(
22992299
},
23002300
[&](const parser::ProcedureDesignator &procD) {
23012301
const parser::Name *name{std::get_if<parser::Name>(&procD.u)};
2302-
if (name) {
2303-
if (name->source == "max" || name->source == "min" ||
2304-
name->source == "iand" || name->source == "ior" ||
2305-
name->source == "ieor") {
2302+
if (name && name->symbol) {
2303+
const SourceName &realName{name->symbol->GetUltimate().name()};
2304+
if (realName == "max" || realName == "min" ||
2305+
realName == "iand" || realName == "ior" ||
2306+
realName == "ieor") {
23062307
ok = true;
2307-
} else {
2308-
context_.Say(GetContext().clauseSource,
2309-
"Invalid reduction identifier in REDUCTION "
2310-
"clause."_err_en_US,
2311-
ContextDirectiveAsFortran());
23122308
}
23132309
}
2310+
if (!ok) {
2311+
context_.Say(GetContext().clauseSource,
2312+
"Invalid reduction identifier in REDUCTION "
2313+
"clause."_err_en_US,
2314+
ContextDirectiveAsFortran());
2315+
}
23142316
},
23152317
},
23162318
definedOp.u);

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -481,21 +481,28 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
481481
bool Pre(const parser::OmpClause::Reduction &x) {
482482
const parser::OmpReductionOperator &opr{
483483
std::get<parser::OmpReductionOperator>(x.v.t)};
484+
auto createDummyProcSymbol = [&](const parser::Name *name) {
485+
// If name resolution failed, create a dummy symbol
486+
const auto namePair{
487+
currScope().try_emplace(name->source, Attrs{}, ProcEntityDetails{})};
488+
auto &newSymbol{*namePair.first->second};
489+
name->symbol = &newSymbol;
490+
};
484491
if (const auto *procD{parser::Unwrap<parser::ProcedureDesignator>(opr.u)}) {
485492
if (const auto *name{parser::Unwrap<parser::Name>(procD->u)}) {
486493
if (!name->symbol) {
487-
const auto namePair{currScope().try_emplace(
488-
name->source, Attrs{}, ProcEntityDetails{})};
489-
auto &symbol{*namePair.first->second};
490-
name->symbol = &symbol;
491-
name->symbol->set(Symbol::Flag::OmpReduction);
492-
AddToContextObjectWithDSA(*name->symbol, Symbol::Flag::OmpReduction);
494+
if (!ResolveName(name)) {
495+
createDummyProcSymbol(name);
496+
}
493497
}
494498
}
495499
if (const auto *procRef{
496500
parser::Unwrap<parser::ProcComponentRef>(procD->u)}) {
497-
ResolveOmp(*procRef->v.thing.component.symbol,
498-
Symbol::Flag::OmpReduction, currScope());
501+
if (!procRef->v.thing.component.symbol) {
502+
if (!ResolveName(&procRef->v.thing.component)) {
503+
createDummyProcSymbol(&procRef->v.thing.component);
504+
}
505+
}
499506
}
500507
}
501508
const auto &objList{std::get<parser::OmpObjectList>(x.v.t)};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! RUN: bbc -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
2+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
3+
4+
! CHECK: omp.wsloop reduction(@max_i_32
5+
! CHECK: omp.reduction
6+
7+
module m1
8+
intrinsic max
9+
end module m1
10+
program main
11+
use m1, ren=>max
12+
n=0
13+
!$omp parallel do reduction(ren:n)
14+
do i=1,100
15+
n=max(n,i)
16+
end do
17+
if (n/=100) print *,101
18+
print *,'pass'
19+
end program main

0 commit comments

Comments
 (0)