Skip to content

Commit e5ef881

Browse files
[Flang][OpenMP] Consider renames when processing reduction intrinsics
Fixes #68654
1 parent 3911841 commit e5ef881

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
@@ -688,6 +688,9 @@ class ReductionProcessor {
688688
const Fortran::parser::ProcedureDesignator &pd) {
689689
const auto *name{Fortran::parser::Unwrap<Fortran::parser::Name>(pd)};
690690
assert(name && "Invalid Reduction Intrinsic.");
691+
if (!name->symbol->GetUltimate().attrs().test(
692+
Fortran::semantics::Attr::INTRINSIC))
693+
return false;
691694
auto redType = llvm::StringSwitch<std::optional<IntrinsicProc>>(
692695
getRealName(name).ToString())
693696
.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
@@ -2199,18 +2199,20 @@ bool OmpStructureChecker::CheckReductionOperators(
21992199
},
22002200
[&](const parser::ProcedureDesignator &procD) {
22012201
const parser::Name *name{std::get_if<parser::Name>(&procD.u)};
2202-
if (name) {
2203-
if (name->source == "max" || name->source == "min" ||
2204-
name->source == "iand" || name->source == "ior" ||
2205-
name->source == "ieor") {
2202+
if (name && name->symbol) {
2203+
const SourceName &realName{name->symbol->GetUltimate().name()};
2204+
if (realName == "max" || realName == "min" ||
2205+
realName == "iand" || realName == "ior" ||
2206+
realName == "ieor") {
22062207
ok = true;
2207-
} else {
2208-
context_.Say(GetContext().clauseSource,
2209-
"Invalid reduction identifier in REDUCTION "
2210-
"clause."_err_en_US,
2211-
ContextDirectiveAsFortran());
22122208
}
22132209
}
2210+
if (!ok) {
2211+
context_.Say(GetContext().clauseSource,
2212+
"Invalid reduction identifier in REDUCTION "
2213+
"clause."_err_en_US,
2214+
ContextDirectiveAsFortran());
2215+
}
22142216
},
22152217
},
22162218
definedOp.u);

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,21 +480,28 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
480480
bool Pre(const parser::OmpClause::Reduction &x) {
481481
const parser::OmpReductionOperator &opr{
482482
std::get<parser::OmpReductionOperator>(x.v.t)};
483+
auto createDummyProcSymbol = [&](const parser::Name *name) {
484+
// If name resolution failed, create a dummy symbol
485+
const auto namePair{
486+
currScope().try_emplace(name->source, Attrs{}, ProcEntityDetails{})};
487+
auto &newSymbol{*namePair.first->second};
488+
name->symbol = &newSymbol;
489+
};
483490
if (const auto *procD{parser::Unwrap<parser::ProcedureDesignator>(opr.u)}) {
484491
if (const auto *name{parser::Unwrap<parser::Name>(procD->u)}) {
485492
if (!name->symbol) {
486-
const auto namePair{currScope().try_emplace(
487-
name->source, Attrs{}, ProcEntityDetails{})};
488-
auto &symbol{*namePair.first->second};
489-
name->symbol = &symbol;
490-
name->symbol->set(Symbol::Flag::OmpReduction);
491-
AddToContextObjectWithDSA(*name->symbol, Symbol::Flag::OmpReduction);
493+
if (!ResolveName(name)) {
494+
createDummyProcSymbol(name);
495+
}
492496
}
493497
}
494498
if (const auto *procRef{
495499
parser::Unwrap<parser::ProcComponentRef>(procD->u)}) {
496-
ResolveOmp(*procRef->v.thing.component.symbol,
497-
Symbol::Flag::OmpReduction, currScope());
500+
if (!procRef->v.thing.component.symbol) {
501+
if (!ResolveName(&procRef->v.thing.component)) {
502+
createDummyProcSymbol(&procRef->v.thing.component);
503+
}
504+
}
498505
}
499506
}
500507
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)