Am 01.08.25 um 14:00 schrieb H.J. Lu:
Call set_decl_tls_model only after a variable has been fully processed,
not in the middle of processing it.

gcc/fortran/

        PR fortran/107421
        * trans-common.cc (build_common_decl): Call set_decl_tls_model
        after processing a variable.
        * trans-decl.cc (gfc_finish_var_decl): Likewise.
        (get_proc_pointer_decl): Likewise.

gcc/testsuite/

        PR fortran/107421
        * gfortran.dg/gomp/pr107421.f90: New test.

Signed-off-by: H.J. Lu <hjl.to...@gmail.com>
---
  gcc/fortran/trans-common.cc                 |  7 ++++---
  gcc/fortran/trans-decl.cc                   | 14 +++++++-------
  gcc/testsuite/gfortran.dg/gomp/pr107421.f90 | 15 +++++++++++++++
  3 files changed, 26 insertions(+), 10 deletions(-)
  create mode 100644 gcc/testsuite/gfortran.dg/gomp/pr107421.f90

diff --git a/gcc/fortran/trans-common.cc b/gcc/fortran/trans-common.cc
index 2db50da20dd..135d3047a15 100644
--- a/gcc/fortran/trans-common.cc
+++ b/gcc/fortran/trans-common.cc
@@ -469,9 +469,6 @@ build_common_decl (gfc_common_head *com, tree union_type, 
bool is_init)
gfc_set_decl_location (decl, &com->where); - if (com->threadprivate)
-       set_decl_tls_model (decl, decl_default_tls_model (decl));
-
        if (com->omp_device_type != OMP_DEVICE_TYPE_UNSET)
        {
          tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEVICE_TYPE);
@@ -536,6 +533,10 @@ build_common_decl (gfc_common_head *com, tree union_type, 
bool is_init)
        DECL_COMMON (decl) = 0;
        DECL_DEFER_OUTPUT (decl) = 0;
      }
+
+  if (com->threadprivate)
+    set_decl_tls_model (decl, decl_default_tls_model (decl));
+
    return decl;
  }
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 741acc052ee..4a3667859d1 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -823,11 +823,6 @@ gfc_finish_var_decl (tree decl, gfc_symbol * sym)
          }
      }
- /* Handle threadprivate variables. */
-  if (sym->attr.threadprivate
-      && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
-    set_decl_tls_model (decl, decl_default_tls_model (decl));
-
    if (sym->attr.omp_allocate && TREE_STATIC (decl))
      {
        struct gfc_omp_namelist *n;
@@ -846,6 +841,11 @@ gfc_finish_var_decl (tree decl, gfc_symbol * sym)
    if (sym->attr.ext_attr & (1 << EXT_ATTR_WEAK))
      declare_weak (decl);
+ /* Handle threadprivate variables. */
+  if (sym->attr.threadprivate
+      && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
+    set_decl_tls_model (decl, decl_default_tls_model (decl));
+
    gfc_finish_decl_attrs (decl, &sym->attr);
  }
@@ -2218,13 +2218,13 @@ get_proc_pointer_decl (gfc_symbol *sym)
                                                  false, true);
      }
+ add_attributes_to_decl (&decl, sym);
+
    /* Handle threadprivate procedure pointers.  */
    if (sym->attr.threadprivate
        && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
      set_decl_tls_model (decl, decl_default_tls_model (decl));
- add_attributes_to_decl (&decl, sym);
-
    return decl;
  }
diff --git a/gcc/testsuite/gfortran.dg/gomp/pr107421.f90 b/gcc/testsuite/gfortran.dg/gomp/pr107421.f90
new file mode 100644
index 00000000000..db98dced8ce
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/pr107421.f90
@@ -0,0 +1,15 @@
+! { dg-additional-options "-fdump-ipa-whole-program" }
+! { dg-additional-options "-mno-direct-extern-access" { target { i?86-*-* 
x86_64-*-* } } }
+
+integer :: i
+
+common /c/ i
+
+!$omp threadprivate (/c/)
+
+i = 0
+
+end
+
+! tls_model should be tls-initial-exec due to common block.
+! { dg-final { scan-ipa-dump "Varpool flags: tls-initial-exec" "whole-program" 
} }

Although I am not an expert in that particular area of gcc - the patch
works as advertised and looks good to me.

OK for mainline.

Thanks for the patch!

Harald

Reply via email to