[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:57:47 PDT 2024


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

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

>From efc8d3c0a57ff6dbd2b7e2759146a1e808b8c637 Mon Sep 17 00:00:00 2001
From: Harish Chambeti <harishcse44 at gmail.com>
Date: Tue, 16 Apr 2024 21:22:05 +0530
Subject: [PATCH] Fix threadprivate variable scope inside BLOCK construct.

---
 flang/lib/Semantics/check-omp-structure.cpp   |  2 +-
 .../test/Lower/OpenMP/threadprivate-hlfir.f90 | 21 +++++++++++++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)

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
+



More information about the flang-commits mailing list