As side-remark or follow up: I have also experimented
with the attached patch.
On the host side, the omp_finish_file call in toplev.c comes
late enough that the the variables is gone and one no longer
writes it to the var table.
However, the write_lto() → output_offload_tables() call is
that early that both the offload table and the variable is
still written. – Hence, this patch fails at run time as
the two tables host_table & target_data have a different size.
Tobias
-----------------
Mentor Graphics (Deutschland) GmbH, Arnulfstraße 201, 80634 München / Germany
Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Alexander
Walter
openmp: skip removed variables for offloading (PRs 94848 + 95551)
gcc/ChangeLog:
PR lto/94848
PR middle-end/95551
* omp-offload.c (add_decls_addresses_to_decl_constructor,
omp_finish_file): Skip variables which have been removed.
libgomp/ChangeLog:
PR lto/94848
PR middle-end/95551
* testsuite/libgomp.fortran/target-var.f90: New test.
gcc/lto-cgraph.c | 3 +++
gcc/omp-offload.c | 7 ++++++-
libgomp/testsuite/libgomp.fortran/target-var.f90 | 32 ++++++++++++++++++++++++
diff --git a/gcc/lto-cgraph.c b/gcc/lto-cgraph.c
index a671c67..0c18ce3 100644
--- a/gcc/lto-cgraph.c
+++ b/gcc/lto-cgraph.c
@@ -1077,6 +1077,9 @@ output_offload_tables (void)
for (unsigned i = 0; i < vec_safe_length (offload_vars); i++)
{
+ if (!symtab_node::get ((*offload_vars)[i]))
+ continue;
+
streamer_write_enum (ob->main_stream, LTO_symtab_tags,
LTO_symtab_last_tag, LTO_symtab_variable);
lto_output_var_decl_ref (ob->decl_state, ob->main_stream,
diff --git a/gcc/omp-offload.c b/gcc/omp-offload.c
index b2df91a..8994ce3 100644
--- a/gcc/omp-offload.c
+++ b/gcc/omp-offload.c
@@ -125,6 +125,9 @@ add_decls_addresses_to_decl_constructor (vec<tree, va_gc> *v_decls,
#endif
&& lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (it));
+ if (is_var && !symtab_node::get (it))
+ continue;
+
tree size = NULL_TREE;
if (is_var)
size = fold_convert (const_ptr_type_node, DECL_SIZE_UNIT (it));
@@ -341,7 +344,7 @@ omp_finish_file (void)
add_decls_addresses_to_decl_constructor (offload_vars, v_v);
tree vars_decl_type = build_array_type_nelts (pointer_sized_int_node,
- num_vars * 2);
+ vec_safe_length (v_v));
tree funcs_decl_type = build_array_type_nelts (pointer_sized_int_node,
num_funcs);
SET_TYPE_ALIGN (vars_decl_type, TYPE_ALIGN (pointer_sized_int_node));
@@ -381,6 +384,8 @@ omp_finish_file (void)
for (unsigned i = 0; i < num_vars; i++)
{
tree it = (*offload_vars)[i];
+ if (!symtab_node::get (it))
+ continue;
#ifdef ACCEL_COMPILER
if (DECL_HAS_VALUE_EXPR_P (it)
&& lookup_attribute ("omp declare target link",
diff --git a/libgomp/testsuite/libgomp.fortran/target-var.f90 b/libgomp/testsuite/libgomp.fortran/target-var.f90
new file mode 100644
index 00000000000..5e5ccd47c96
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/target-var.f90
@@ -0,0 +1,32 @@
+! { dg-additional-options "-O3" }
+!
+! With -O3 the static local variable A.10 generated for
+! the array constructor [-2, -4, ..., -20] is optimized
+! away - which has to be handled in the offload_vars table.
+!
+program main
+ implicit none (type, external)
+ integer :: j
+ integer, allocatable :: A(:)
+
+ A = [(3*j, j=1, 10)]
+ call bar (A)
+ deallocate (A)
+contains
+ subroutine bar (array)
+ integer :: i
+ integer :: array(:)
+
+ !$omp target map(from:array)
+ !$acc parallel copyout(array)
+ array = [(-2*i, i = 1, size(array))]
+ !$omp do private(array)
+ !$acc loop gang private(array)
+ do i = 1, 10
+ array(i) = 9*i
+ end do
+ if (any (array /= [(-2*i, i = 1, 10)])) error stop 2
+ !$omp end target
+ !$acc end parallel
+ end subroutine bar
+end