From a5f1ddd115a7ec55bc085402cc73c4133894e3b0 Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Tue, 19 Aug 2025 17:52:27 +0200 Subject: [PATCH] [Flang][OpenMP] Fix issue with named constants in SHARED and FIRSTPRIVATE clauses (#154335) The seemingly was a regression that prevented the usage of named constant (w/ PARAMETER attribute) in SHARED and FIRSTPRIVATE clauses. This PR corrects that. --- flang/lib/Semantics/check-omp-structure.cpp | 15 ++++++- .../test/Semantics/OpenMP/named-constants.f90 | 44 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 flang/test/Semantics/OpenMP/named-constants.f90 diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp index 9b1733796ddc..2b36b085ae08 100644 --- a/flang/lib/Semantics/check-omp-structure.cpp +++ b/flang/lib/Semantics/check-omp-structure.cpp @@ -2560,11 +2560,24 @@ void OmpStructureChecker::Enter(const parser::OmpClause &x) { break; } + // Named constants are OK to be used within 'shared' and 'firstprivate' + // clauses. The check for this happens a few lines below. + bool SharedOrFirstprivate = false; + switch (x.Id()) { + case llvm::omp::Clause::OMPC_shared: + case llvm::omp::Clause::OMPC_firstprivate: + SharedOrFirstprivate = true; + break; + default: + break; + } + if (const parser::OmpObjectList *objList{GetOmpObjectList(x)}) { SymbolSourceMap symbols; GetSymbolsInObjectList(*objList, symbols); for (const auto &[symbol, source] : symbols) { - if (!IsVariableListItem(*symbol)) { + if (!IsVariableListItem(*symbol) && + !(IsNamedConstant(*symbol) && SharedOrFirstprivate)) { deferredNonVariables_.insert({symbol, source}); } } diff --git a/flang/test/Semantics/OpenMP/named-constants.f90 b/flang/test/Semantics/OpenMP/named-constants.f90 new file mode 100644 index 000000000000..ac0850066ceb --- /dev/null +++ b/flang/test/Semantics/OpenMP/named-constants.f90 @@ -0,0 +1,44 @@ +!RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp + +module named_constants + implicit none +contains + subroutine shrd() + implicit none + integer, parameter :: n = 7 + real, parameter :: m = 7.0 + logical, parameter :: l = .false. + integer, dimension(3), parameter :: a = [1, 2, 3] + ! no error expected + !$omp parallel shared(n, m, l, a) + print *, n, m, l, a + !$omp end parallel + end subroutine shrd + + subroutine frstprvt() + implicit none + integer, parameter :: n = 7 + real, parameter :: m = 7.0 + logical, parameter :: l = .false. + integer, dimension(3), parameter :: a = [1, 2, 3] + ! no error expected + !$omp parallel firstprivate(n, m, l, a) + print *, n, m, l, a + !$omp end parallel + end subroutine frstprvt + + subroutine prvt() + implicit none + integer, parameter :: n = 7 + real, parameter :: m = 7.0 + logical, parameter :: l = .false. + integer, dimension(3), parameter :: a = [1, 2, 3] + !ERROR: 'n' must be a variable + !ERROR: 'm' must be a variable + !ERROR: 'l' must be a variable + !ERROR: 'a' must be a variable + !$omp parallel private(n, m, l, a) + print *, n, m, l, a + !$omp end parallel + end subroutine prvt +end module named_constants