[flang-commits] [flang] [flang][OpenMP] Don't allow namelist variables with threadprivate (PR #130957)

via flang-commits flang-commits at lists.llvm.org
Wed Mar 12 06:14:36 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-openmp

Author: Tom Eccles (tblah)

<details>
<summary>Changes</summary>

While the standard doesn't explicitly mention namelist variables in this case, it does prohibit privatization of namelist variables. Flang decided to also prohibit namelist variables from appearing in a reduction clause.

Variables that are part of a namelist are linked to I/O mechanisms, and their memory association and initialization are managed by the compiler in a way that conflicts with the requirements of threadprivate variables. I propose we should add this restriction.

Fixes #<!-- -->112538

---
Full diff: https://github.com/llvm/llvm-project/pull/130957.diff


2 Files Affected:

- (modified) flang/lib/Semantics/check-omp-structure.cpp (+9) 
- (added) flang/test/Semantics/OpenMP/threadprivate09.f90 (+12) 


``````````diff
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 5fcebdca0bc5f..e98c9c3091cb4 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -1516,6 +1516,10 @@ void OmpStructureChecker::CheckThreadprivateOrDeclareTargetVar(
                       "A variable in a %s directive cannot appear in an "
                       "EQUIVALENCE statement"_err_en_US,
                       ContextDirectiveAsFortran());
+                } else if (name->symbol->test(Symbol::Flag::InNamelist)) {
+                  context_.Say(name->source,
+                      "A variable in a %s directive cannot appear in a NAMELIST"_err_en_US,
+                      ContextDirectiveAsFortran());
                 } else if (name->symbol->test(Symbol::Flag::OmpThreadprivate) &&
                     GetContext().directive ==
                         llvm::omp::Directive::OMPD_declare_target) {
@@ -1571,6 +1575,11 @@ void OmpStructureChecker::CheckThreadprivateOrDeclareTargetVar(
                           ContextDirectiveAsFortran(), obj->name(),
                           name.symbol->name());
                     }
+                    if (obj->test(Symbol::Flag::InNamelist)) {
+                      context_.Say(name.source,
+                          "A variable in a %s directive cannot appear in a NAMELIST"_err_en_US,
+                          ContextDirectiveAsFortran());
+                    }
                   }
                 }
               }
diff --git a/flang/test/Semantics/OpenMP/threadprivate09.f90 b/flang/test/Semantics/OpenMP/threadprivate09.f90
new file mode 100644
index 0000000000000..02236d19d9d01
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/threadprivate09.f90
@@ -0,0 +1,12 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 %openmp_flags
+
+! Check that namelist variables cannot be used with threadprivate
+
+module m
+  integer :: nam1
+  common /com1/nam1
+  namelist /nam/nam1
+
+  !ERROR: A variable in a THREADPRIVATE directive cannot appear in a NAMELIST
+  !$omp threadprivate(/com1/)
+end

``````````

</details>


https://github.com/llvm/llvm-project/pull/130957


More information about the flang-commits mailing list