Skip to content

[flang][OpenMP] catch namelist access through equivalence #130804

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

Merged
merged 1 commit into from
Mar 12, 2025
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
23 changes: 20 additions & 3 deletions flang/lib/Semantics/resolve-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,24 @@ void OmpAttributeVisitor::ResolveOmpObjectList(
}
}

/// True if either symbol is in a namelist or some other symbol in the same
/// equivalence set as symbol is in a namelist.
static bool SymbolOrEquivalentIsInNamelist(const Symbol &symbol) {
auto isInNamelist{[](const Symbol &sym) {
const Symbol &ultimate{sym.GetUltimate()};
return ultimate.test(Symbol::Flag::InNamelist);
}};

const EquivalenceSet *eqv{FindEquivalenceSet(symbol)};
if (!eqv) {
return isInNamelist(symbol);
}

return llvm::any_of(*eqv, [isInNamelist](const EquivalenceObject &obj) {
return isInNamelist(obj.symbol);
});
}

void OmpAttributeVisitor::ResolveOmpObject(
const parser::OmpObject &ompObject, Symbol::Flag ompFlag) {
common::visit(
Expand Down Expand Up @@ -2468,15 +2486,14 @@ void OmpAttributeVisitor::ResolveOmpObject(
.str()));
}
if (ompFlag == Symbol::Flag::OmpReduction) {
const Symbol &ultimateSymbol{symbol->GetUltimate()};
// Using variables inside of a namelist in OpenMP reductions
// is allowed by the standard, but is not allowed for
// privatisation. This looks like an oversight. If the
// namelist is hoisted to a global, we cannot apply the
// mapping for the reduction variable: resulting in incorrect
// results. Disabling this hoisting could make some real
// production code go slower. See discussion in #109303
if (ultimateSymbol.test(Symbol::Flag::InNamelist)) {
if (SymbolOrEquivalentIsInNamelist(*symbol)) {
context_.Say(name->source,
"Variable '%s' in NAMELIST cannot be in a REDUCTION clause"_err_en_US,
name->ToString());
Expand Down Expand Up @@ -2838,7 +2855,7 @@ void OmpAttributeVisitor::CheckObjectIsPrivatizable(
clauseName = "LASTPRIVATE";
}

if (ultimateSymbol.test(Symbol::Flag::InNamelist)) {
if (SymbolOrEquivalentIsInNamelist(symbol)) {
context_.Say(name.source,
"Variable '%s' in NAMELIST cannot be in a %s clause"_err_en_US,
name.ToString(), clauseName.str());
Expand Down
32 changes: 32 additions & 0 deletions flang/test/Semantics/OpenMP/equivalence-namelist.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
! RUN: %python %S/../test_errors.py %s %flang -fopenmp

! The openmp standard only dissallows namelist for privatization, but flang
! also does not allow it for reduction as this would be difficult to support.
!
! Variables in equivalence with variables in the namelist pose the same
! implementation problems.

subroutine test01()
integer::va
equivalence (va,vva)
namelist /na1/vva
va=1

!ERROR: Variable 'va' in NAMELIST cannot be in a REDUCTION clause
!$omp parallel reduction(+:va)
write(*,na1)
!$omp end parallel
end subroutine test01


subroutine test02()
integer::va
equivalence (va,vva)
namelist /na1/vva
va=1

!ERROR: Variable 'va' in NAMELIST cannot be in a PRIVATE clause
!$omp parallel private(va)
write(*,na1)
!$omp end parallel
end subroutine test02