[flang-commits] [flang] [Flang][OpenMP] Fix issue with named constants in SHARED and FIRSTPRIVATE clauses (PR #154335)
via flang-commits
flang-commits at lists.llvm.org
Tue Aug 19 06:41:28 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-openmp
Author: Michael Klemm (mjklemm)
<details>
<summary>Changes</summary>
The seemingly was a regression that prevented the usage of named constant (w/ PARAMETER attribute) in SHARED and FIRSTPRIVATE clauses. This PR corrects that.
---
Full diff: https://github.com/llvm/llvm-project/pull/154335.diff
4 Files Affected:
- (modified) flang/include/flang/Semantics/openmp-utils.h (+1)
- (modified) flang/lib/Semantics/check-omp-structure.cpp (+14-1)
- (modified) flang/lib/Semantics/openmp-utils.cpp (+4)
- (added) flang/test/Semantics/OpenMP/named-constants.f90 (+44)
``````````diff
diff --git a/flang/include/flang/Semantics/openmp-utils.h b/flang/include/flang/Semantics/openmp-utils.h
index 1c54124a5738a..df09e7a9e24b5 100644
--- a/flang/include/flang/Semantics/openmp-utils.h
+++ b/flang/include/flang/Semantics/openmp-utils.h
@@ -65,6 +65,7 @@ std::optional<parser::CharBlock> GetObjectSource(
bool IsCommonBlock(const Symbol &sym);
bool IsExtendedListItem(const Symbol &sym);
bool IsVariableListItem(const Symbol &sym);
+bool IsNamedConstantListItem(const Symbol &sym);
bool IsVarOrFunctionRef(const MaybeExpr &expr);
bool IsMapEnteringType(parser::OmpMapType::Value type);
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index d9092565449da..5c3e6cad79c95 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -2561,11 +2561,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) &&
+ !(IsNamedConstantListItem(*symbol) && SharedOrFirstprivate)) {
deferredNonVariables_.insert({symbol, source});
}
}
diff --git a/flang/lib/Semantics/openmp-utils.cpp b/flang/lib/Semantics/openmp-utils.cpp
index e8df346ccdc3e..663814b440034 100644
--- a/flang/lib/Semantics/openmp-utils.cpp
+++ b/flang/lib/Semantics/openmp-utils.cpp
@@ -130,6 +130,10 @@ bool IsVariableListItem(const Symbol &sym) {
return evaluate::IsVariable(sym) || sym.attrs().test(Attr::POINTER);
}
+bool IsNamedConstantListItem(const Symbol &sym) {
+ return sym.attrs().test(Attr::PARAMETER);
+}
+
bool IsExtendedListItem(const Symbol &sym) {
return IsVariableListItem(sym) || sym.IsSubprogram();
}
diff --git a/flang/test/Semantics/OpenMP/named-constants.f90 b/flang/test/Semantics/OpenMP/named-constants.f90
new file mode 100644
index 0000000000000..ac0850066cebe
--- /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
``````````
</details>
https://github.com/llvm/llvm-project/pull/154335
More information about the flang-commits
mailing list