[flang-commits] [flang] Fix threadprivate variable scope inside BLOCK construct. (PR #88921)

via flang-commits flang-commits at lists.llvm.org
Tue Apr 16 08:58:19 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (harishch4)

<details>
<summary>Changes</summary>

When a local variable inside a BLOCK construct is used as threadprivate variable, llvm-flang throws below error:

> error: The THREADPRIVATE directive and the common block or variable in it must appear in the same declaration section of a scoping unit

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


2 Files Affected:

- (modified) flang/lib/Semantics/check-omp-structure.cpp (+1-1) 
- (modified) flang/test/Lower/OpenMP/threadprivate-hlfir.f90 (+19-2) 


``````````diff
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index e85d8d1f7ab533..bafa242a79302a 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -1048,7 +1048,7 @@ void OmpStructureChecker::CheckThreadprivateOrDeclareTargetVar(
                       name->symbol->GetUltimate().owner();
                   if (!curScope.IsTopLevel()) {
                     const semantics::Scope &declScope =
-                        GetProgramUnitContaining(curScope);
+                        GetProgramUnitOrBlockConstructContaining(curScope);
                     const semantics::Symbol *sym{
                         declScope.parent().FindSymbol(name->symbol->name())};
                     if (sym &&
diff --git a/flang/test/Lower/OpenMP/threadprivate-hlfir.f90 b/flang/test/Lower/OpenMP/threadprivate-hlfir.f90
index d39ae1e7011838..f02819ce625de6 100644
--- a/flang/test/Lower/OpenMP/threadprivate-hlfir.f90
+++ b/flang/test/Lower/OpenMP/threadprivate-hlfir.f90
@@ -15,8 +15,6 @@
 !CHECK:      %{{.*}} = fir.call @_FortranAioOutputInteger32(%{{.*}}, %[[TP_VAL]]) fastmath<contract> : (!fir.ref<i8>, i32) -> i1
 !CHECK:      omp.terminator
 
-!CHECK:  fir.global internal @_QFsubEa : i32
-
 subroutine sub()
   integer, save:: a
   !$omp threadprivate(a)
@@ -24,3 +22,22 @@ subroutine sub()
     print *, a
   !$omp end parallel
 end subroutine
+
+!CHECK-LABEL: @_QPsub2() {
+!CHECK:   %[[STACK:.*]] = fir.call @llvm.stacksave.p0() fastmath<contract> : () -> !fir.ref<i8>
+!CHECK:   %[[A:.*]] = fir.address_of(@_QFsub2B1Ea) : !fir.ref<i32>
+!CHECK:   %[[A_DECL:.*]]:2 = hlfir.declare %[[A]] {uniq_name = "_QFsub2B1Ea"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+!CHECK:   %[[TP_A:.*]] = omp.threadprivate %[[A_DECL]]#1 : !fir.ref<i32> -> !fir.ref<i32>
+!CHECK:   %[[TP_A_DECL:.*]]:2 = hlfir.declare %[[TP_A]] {uniq_name = "_QFsub2B1Ea"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+!CHECK:   fir.call @llvm.stackrestore.p0(%[[STACK]]) fastmath<contract> : (!fir.ref<i8>) -> ()
+
+subroutine sub2()
+  BLOCK
+    integer, save :: a
+    !$omp threadprivate(a)
+  END BLOCK
+end subroutine
+
+!CHECK:  fir.global internal @_QFsubEa : i32
+!CHECK:  fir.global internal @_QFsub2B1Ea : i32
+

``````````

</details>


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


More information about the flang-commits mailing list