https://gcc.gnu.org/g:a6310d58aac505bdea37a6a0d6ab6b9593d01259

commit r17-4073-ga6310d58aac505bdea37a6a0d6ab6b9593d01259
Author: Tobias Burnus <[email protected]>
Date:   Thu Sep 10 13:25:39 2026 +0200

    lto.cc: OpenMP's offload_handle_link_vars - fix for global variables 
[PR127273]
    
    This is about variables that appear in OpenMP's 'link' clause of
    the 'declare target' directive. On the host, they are normal
    static variables - while on the device, they are pointer variables
    that point to the actual data, i.e. the access 'var' on the host
    becomes '* var$linkptr' on the device side, using a VALUE_EXPR
    to achieve this.  The offload_handle_link_vars handles this
    ('#if ACCEL_COMPILER').
    
    For global variables, additional issues occurred:
    * The function would be called multiple times for the same variable
      each time calling 'build_decl' again.
      Solution: If there is already a VALUE_EXPR, skip.
    * Some code, in particular ipa_reference_var_uid assumes that also
      decl->decl_with_vis.symtab_node is not NULL.
      Solution: Call varpool_node::finalize_decl to create it.
    * For some reasons some IPA analysis assumes that it is not needed
      and, hence, the node can be removed, which fails (see previous item).
      Solution: Use force_output.
    
    As global static variables have to be output via the offload_vars,
    the host and device side need to exist to make sure the n-th variable
    in the host table is the same as the n-th variable on the device side,
    i.e. forcing the output makes sense.
    
            PR middle-end/127273
    
    gcc/lto/ChangeLog:
    
            * lto.cc (offload_handle_link_vars): Create node and force_output;
            avoid creating the same '...$linkptr' var multiple times.
    
    libgomp/ChangeLog:
    
            * testsuite/libgomp.c/usm_env_handling-1-self.c: Remove 'dg-ice'.

Diff:
---
 gcc/lto/lto.cc                                        | 4 ++++
 libgomp/testsuite/libgomp.c/usm_env_handling-1-self.c | 8 ++------
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/gcc/lto/lto.cc b/gcc/lto/lto.cc
index 76837c27be81..d22827bbcc0a 100644
--- a/gcc/lto/lto.cc
+++ b/gcc/lto/lto.cc
@@ -487,6 +487,8 @@ offload_handle_link_vars (void)
     if (lookup_attribute ("omp declare target link",
                          DECL_ATTRIBUTES (var->decl)))
       {
+       if (DECL_HAS_VALUE_EXPR_P (var->decl))
+         continue;
        tree type = build_pointer_type (TREE_TYPE (var->decl));
        tree link_ptr_var = build_decl (UNKNOWN_LOCATION, VAR_DECL,
                                        clone_function_name (var->decl,
@@ -498,6 +500,8 @@ offload_handle_link_vars (void)
        SET_DECL_ASSEMBLER_NAME (link_ptr_var, DECL_NAME (link_ptr_var));
        SET_DECL_VALUE_EXPR (var->decl, build_simple_mem_ref (link_ptr_var));
        DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
+       varpool_node::finalize_decl (link_ptr_var);
+       varpool_node::get (link_ptr_var)->force_output = var->force_output;
       }
 #endif
 }
diff --git a/libgomp/testsuite/libgomp.c/usm_env_handling-1-self.c 
b/libgomp/testsuite/libgomp.c/usm_env_handling-1-self.c
index 37a7b915b18d..79cbd9ff0311 100644
--- a/libgomp/testsuite/libgomp.c/usm_env_handling-1-self.c
+++ b/libgomp/testsuite/libgomp.c/usm_env_handling-1-self.c
@@ -2,12 +2,8 @@
 
 /* Check that a global static variable is consistently updated on the device. 
*/
 
-// ICE for A$linkptr in nvptx lto1 with ipa_reference_var_uid
-// when compiling with -fopenmp -O1 or higher
-// {Segmentation [Ff]ault}{ offload_target_nvptx } }
-// { dg-ice "PR target/127273" { offload_target_nvptx } }
-// { dg-excess-errors "'mkoffload' failure etc." { xfail { 
offload_target_nvptx } } } */
-
+// PR middle-end/127273 for an ICE fix
+// PR middle-end/122205 for converting enter to link for self_maps
 
 #define USE_SELF_MAPS 1
 #include "usm_env_handling-1.c"

Reply via email to