On Wed, Apr 20, 2022 at 03:19:38PM +0200, Tobias Burnus wrote: > For array-descriptor vars, the descriptor is assigned to a temporary. However, > this failed when the clause's argument was in turn in a data-sharing clause > as the outer context's VALUE_EXPR wasn't used. > > gcc/ChangeLog: > > * omp-low.cc (lower_omp_target): Fix use_device_{addr,ptr} with list > item that is in an outer data-sharing clause. > > libgomp/ChangeLog: > > * testsuite/libgomp.fortran/use_device_addr-5.f90: New test. > > gcc/omp-low.cc | 22 ++-- > .../libgomp.fortran/use_device_addr-5.f90 | 143 > +++++++++++++++++++++ > 2 files changed, 156 insertions(+), 9 deletions(-) > > diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc > index bf5779b6543..6e387fd9a61 100644 > --- a/gcc/omp-low.cc > +++ b/gcc/omp-low.cc > @@ -13656,26 +13656,30 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, > omp_context *ctx) > new_var = lookup_decl (var, ctx); > new_var = DECL_VALUE_EXPR (new_var); > tree v = new_var; > + tree v2 = var; > + if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR > + || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_ADDR) > + { > + v2 = maybe_lookup_decl_in_outer_ctx (var, ctx); > + if (DECL_HAS_VALUE_EXPR_P (v2)) > + v2 = DECL_VALUE_EXPR (v2);
I don't understand the above 2 lines, why do you need that? Regardless whether v2 has DECL_VALUE_EXPR or not, the type of the DECL_VALUE_EXPR (v2) and v2 should be the same, build_fold_indirect_ref should work on both and then v2 is only used as second operand of gimplify_assign, where the gimplifier makes sure to handle DECL_VALUE_EXPR correctly. I certainly don't see any difference in the *.omplower dump if I comment out the above 2 lines. Otherwise LGTM, so if the 2 lines aren't needed, please also drop the {}s around v2 = maybe_lookup_decl_in_outer_ctx (var, ctx); and reindent. > + } > > if (is_ref) > { > - var = build_fold_indirect_ref (var); > - gimplify_expr (&var, &assign_body, NULL, is_gimple_val, > - fb_rvalue); > - v = create_tmp_var_raw (TREE_TYPE (var), get_name (var)); > + v2 = build_fold_indirect_ref (v2); > + v = create_tmp_var_raw (TREE_TYPE (v2), get_name (var)); > gimple_add_tmp_var (v); > TREE_ADDRESSABLE (v) = 1; > - gimple_seq_add_stmt (&assign_body, > - gimple_build_assign (v, var)); > + gimplify_assign (v, v2, &assign_body); > tree rhs = build_fold_addr_expr (v); > gimple_seq_add_stmt (&assign_body, > gimple_build_assign (new_var, rhs)); > } > else > - gimple_seq_add_stmt (&assign_body, > - gimple_build_assign (new_var, var)); > + gimplify_assign (new_var, v2, &assign_body); > > - tree v2 = lang_hooks.decls.omp_array_data (unshare_expr (v), > false); > + v2 = lang_hooks.decls.omp_array_data (unshare_expr (v), false); > gcc_assert (v2); > gimplify_expr (&x, &assign_body, NULL, is_gimple_val, > fb_rvalue); > gimple_seq_add_stmt (&assign_body, Jakub