https://gcc.gnu.org/g:6b48fc950cb653186f911bcf4c22ab2fb0a30959
commit 6b48fc950cb653186f911bcf4c22ab2fb0a30959 Author: Mikael Morin <[email protected]> Date: Wed Oct 22 18:57:10 2025 +0200 fortran: array descriptor: Add accessors for the elem_len field [PR122521] Regression tested on aarch64-unknown-linux-gnu. OK for master? -- >8 -- Add accessor functions to get or set the value of the elem_len field of array descriptors, and remove from the public API the function giving direct acces to the field. PR fortran/122521 gcc/fortran/ChangeLog: * trans-descriptor.cc (gfc_conv_descriptor_elem_len): Make static and rename ... (conv_descriptor_elem_len): ... to this. (gfc_conv_descriptor_elem_len_get, gfc_conv_descriptor_elem_len_set): New functions. * trans-descriptor.h (gfc_conv_descriptor_elem_len): Remove declaration. (gfc_conv_descriptor_elem_len_get, gfc_conv_descriptor_elem_len_set): New declarations. * trans-decl.cc (gfc_conv_cfi_to_gfc): Use gfc_conv_descriptor_elem_len_get to get the value of the elem_len field and gfc_conv_descriptor_elem_len_set to set it. * trans-array.cc (gfc_array_init_size, gfc_alloc_allocatable_for_assignment): Likewise. * trans-expr.cc (gfc_conv_scalar_to_descriptor, gfc_conv_gfc_desc_to_cfi_desc, gfc_trans_pointer_assignment): Likewise. * trans-intrinsic.cc (gfc_conv_is_contiguous_expr, gfc_conv_intrinsic_sizeof): Likewise. * trans-openmp.cc (gfc_omp_array_size, gfc_omp_deep_mapping_item): Likewise. Diff: --- gcc/fortran/trans-array.cc | 6 ++---- gcc/fortran/trans-decl.cc | 10 +++++----- gcc/fortran/trans-descriptor.cc | 27 ++++++++++++++++++++++++--- gcc/fortran/trans-descriptor.h | 3 ++- gcc/fortran/trans-expr.cc | 10 ++++------ gcc/fortran/trans-intrinsic.cc | 9 ++------- gcc/fortran/trans-openmp.cc | 6 +++--- 7 files changed, 42 insertions(+), 29 deletions(-) diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index 950ebf758132..9a1c801384c2 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -6071,9 +6071,7 @@ gfc_array_init_size (tree descriptor, int rank, int corank, tree * poffset, { tmp = gfc_conv_descriptor_dtype (descriptor); gfc_add_modify (pblock, tmp, gfc_get_dtype (type)); - tmp = gfc_conv_descriptor_elem_len (descriptor); - gfc_add_modify (pblock, tmp, - fold_convert (TREE_TYPE (tmp), expr3_elem_size)); + gfc_conv_descriptor_elem_len_set (pblock, descriptor, expr3_elem_size); } else { @@ -11815,7 +11813,7 @@ gfc_alloc_allocatable_for_assignment (gfc_loopinfo *loop, { /* Unfortunately, the lhs vptr is set too early in many cases. Play it safe by using the descriptor element length. */ - tmp = gfc_conv_descriptor_elem_len (desc); + tmp = gfc_conv_descriptor_elem_len_get (desc); elemsize1 = fold_convert (gfc_array_index_type, tmp); } else diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc index ee3e3be46fef..bc5e13488eef 100644 --- a/gcc/fortran/trans-decl.cc +++ b/gcc/fortran/trans-decl.cc @@ -7519,8 +7519,8 @@ gfc_conv_cfi_to_gfc (stmtblock_t *init, stmtblock_t *finally, if (sym->ts.type == BT_ASSUMED) { /* For type(*), take elem_len + dtype.type from the actual argument. */ - gfc_add_modify (&block, gfc_conv_descriptor_elem_len (gfc_desc), - gfc_get_cfi_desc_elem_len (cfi)); + gfc_conv_descriptor_elem_len_set (&block, gfc_desc, + gfc_get_cfi_desc_elem_len (cfi)); tree cond; tree ctype = gfc_get_cfi_desc_type (cfi); ctype = fold_build2_loc (input_location, BIT_AND_EXPR, TREE_TYPE (ctype), @@ -7751,7 +7751,7 @@ gfc_conv_cfi_to_gfc (stmtblock_t *init, stmtblock_t *finally, /* memcpy (lhs + idx*elem_len, rhs + shift, elem_len) */ tree elem_len; if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc))) - elem_len = gfc_conv_descriptor_elem_len (gfc_desc); + elem_len = gfc_conv_descriptor_elem_len_get (gfc_desc); else elem_len = gfc_get_cfi_desc_elem_len (cfi); lhs = fold_build2_loc (input_location, MULT_EXPR, size_type_node, @@ -7789,7 +7789,7 @@ gfc_conv_cfi_to_gfc (stmtblock_t *init, stmtblock_t *finally, /* if do_copy_inout: gfc->dspan = gfc->dtype.elem_len We use gfc instead of cfi on the RHS as this might be a constant. */ tmp = fold_convert (gfc_array_index_type, - gfc_conv_descriptor_elem_len (gfc_desc)); + gfc_conv_descriptor_elem_len_get (gfc_desc)); if (!do_copy_inout) { /* gfc->dspan = ((cfi->dim[0].sm % gfc->elem_len) @@ -7991,7 +7991,7 @@ done: /* memcpy (lhs + shift, rhs + idx*elem_len, elem_len) */ tree elem_len; if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc))) - elem_len = gfc_conv_descriptor_elem_len (gfc_desc); + elem_len = gfc_conv_descriptor_elem_len_get (gfc_desc); else elem_len = gfc_get_cfi_desc_elem_len (cfi); rhs = fold_build2_loc (input_location, MULT_EXPR, size_type_node, diff --git a/gcc/fortran/trans-descriptor.cc b/gcc/fortran/trans-descriptor.cc index d24e5c6f1787..77ecb647f171 100644 --- a/gcc/fortran/trans-descriptor.cc +++ b/gcc/fortran/trans-descriptor.cc @@ -187,10 +187,11 @@ gfc_conv_descriptor_version (tree desc) } -/* Return the element length from the descriptor dtype field. */ +/* Return a reference to the element length field of the array descriptor + DESC. */ -tree -gfc_conv_descriptor_elem_len (tree desc) +static tree +conv_descriptor_elem_len (tree desc) { tree tmp; tree dtype; @@ -204,6 +205,26 @@ gfc_conv_descriptor_elem_len (tree desc) dtype, tmp, NULL_TREE); } +/* Return the element length value of the array descriptor DESC. */ + +tree +gfc_conv_descriptor_elem_len_get (tree desc) +{ + return conv_descriptor_elem_len (desc); +} + +/* Add code to BLOCK assigning VALUE to the element length field of the array + descriptor DESC. */ + +void +gfc_conv_descriptor_elem_len_set (stmtblock_t *block, tree desc, tree value) +{ + location_t loc = input_location; + tree t = conv_descriptor_elem_len (desc); + gfc_add_modify_loc (loc, block, t, + fold_convert_loc (loc, TREE_TYPE (t), value)); +} + tree gfc_conv_descriptor_attribute (tree desc) diff --git a/gcc/fortran/trans-descriptor.h b/gcc/fortran/trans-descriptor.h index b68a28bddffe..afc8f5d442e3 100644 --- a/gcc/fortran/trans-descriptor.h +++ b/gcc/fortran/trans-descriptor.h @@ -23,7 +23,6 @@ along with GCC; see the file COPYING3. If not see tree gfc_conv_descriptor_dtype (tree); tree gfc_conv_descriptor_rank (tree); tree gfc_conv_descriptor_version (tree); -tree gfc_conv_descriptor_elem_len (tree); tree gfc_conv_descriptor_attribute (tree); tree gfc_conv_descriptor_type (tree); tree gfc_get_descriptor_dimension (tree); @@ -32,6 +31,7 @@ tree gfc_conv_descriptor_token (tree); tree gfc_conv_descriptor_data_get (tree); tree gfc_conv_descriptor_offset_get (tree); +tree gfc_conv_descriptor_elem_len_get (tree); tree gfc_conv_descriptor_span_get (tree); tree gfc_conv_descriptor_stride_get (tree, tree); @@ -40,6 +40,7 @@ tree gfc_conv_descriptor_ubound_get (tree, tree); void gfc_conv_descriptor_data_set (stmtblock_t *, tree, tree); void gfc_conv_descriptor_offset_set (stmtblock_t *, tree, tree); +void gfc_conv_descriptor_elem_len_set (stmtblock_t *, tree, tree); void gfc_conv_descriptor_span_set (stmtblock_t *, tree, tree); void gfc_conv_descriptor_stride_set (stmtblock_t *, tree, tree, tree); void gfc_conv_descriptor_lbound_set (stmtblock_t *, tree, tree, tree); diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index 8a96d6e13949..cb9daf4d068a 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -140,7 +140,7 @@ gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr) gfc_get_dtype_rank_type (0, etype)); gfc_conv_descriptor_data_set (&se->pre, desc, scalar); gfc_conv_descriptor_span_set (&se->pre, desc, - gfc_conv_descriptor_elem_len (desc)); + gfc_conv_descriptor_elem_len_get (desc)); /* Copy pointer address back - but only if it could have changed and if the actual argument is a pointer and not, e.g., NULL(). */ @@ -6409,7 +6409,7 @@ gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym) } else if (e->ts.type == BT_ASSUMED) { - tmp = gfc_conv_descriptor_elem_len (gfc); + tmp = gfc_conv_descriptor_elem_len_get (gfc); tmp2 = gfc_get_cfi_desc_elem_len (cfi); gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp)); } @@ -6424,7 +6424,7 @@ gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym) tree type = fold_convert (TREE_TYPE (ctype), gfc_conv_descriptor_type (gfc)); tree kind = fold_convert (TREE_TYPE (ctype), - gfc_conv_descriptor_elem_len (gfc)); + gfc_conv_descriptor_elem_len_get (gfc)); kind = fold_build2_loc (input_location, LSHIFT_EXPR, TREE_TYPE (type), kind, build_int_cst (TREE_TYPE (type), CFI_type_kind_shift)); @@ -11551,9 +11551,7 @@ gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2) tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts)); elem_len = fold_convert (gfc_array_index_type, tmp); elem_len = gfc_evaluate_now (elem_len, &block); - tmp = gfc_conv_descriptor_elem_len (desc); - gfc_add_modify (&block, tmp, - fold_convert (TREE_TYPE (tmp), elem_len)); + gfc_conv_descriptor_elem_len_set (&block, desc, elem_len); } if (rank_remap) diff --git a/gcc/fortran/trans-intrinsic.cc b/gcc/fortran/trans-intrinsic.cc index f7bce965b731..0a611f7ffa3f 100644 --- a/gcc/fortran/trans-intrinsic.cc +++ b/gcc/fortran/trans-intrinsic.cc @@ -2391,7 +2391,7 @@ gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg) { tree span = gfc_conv_descriptor_span_get (desc); tmp = fold_convert (TREE_TYPE (span), - gfc_conv_descriptor_elem_len (desc)); + gfc_conv_descriptor_elem_len_get (desc)); cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, span, tmp); se->expr = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR, @@ -8393,7 +8393,6 @@ gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr) tree lower; tree upper; tree byte_size; - tree field; int n; gfc_init_se (&argse, NULL); @@ -8417,11 +8416,7 @@ gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr) if (POINTER_TYPE_P (TREE_TYPE (tmp))) tmp = build_fold_indirect_ref_loc (input_location, tmp); - tmp = gfc_conv_descriptor_dtype (tmp); - field = gfc_advance_chain (TYPE_FIELDS (get_dtype_type_node ()), - GFC_DTYPE_ELEM_LEN); - tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field), - tmp, field, NULL_TREE); + tmp = gfc_conv_descriptor_elem_len_get (tmp); byte_size = fold_convert (gfc_array_index_type, tmp); } diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc index 7243301cae0b..ce47902d4a9b 100644 --- a/gcc/fortran/trans-openmp.cc +++ b/gcc/fortran/trans-openmp.cc @@ -215,7 +215,7 @@ gfc_omp_array_size (tree decl, gimple_seq *pre_p) size = fold_convert (size_type_node, size); tree elemsz = gfc_get_element_type (TREE_TYPE (decl)); if (TREE_CODE (elemsz) == ARRAY_TYPE && TYPE_STRING_FLAG (elemsz)) - elemsz = gfc_conv_descriptor_elem_len (decl); + elemsz = gfc_conv_descriptor_elem_len_get (decl); else elemsz = TYPE_SIZE_UNIT (elemsz); size = fold_build2 (MULT_EXPR, size_type_node, size, elemsz); @@ -2292,7 +2292,7 @@ gfc_omp_deep_mapping_item (bool is_cnt, bool do_copy, bool do_alloc_check, /* TODO: Optimization: Shouldn't this be an expr. const, except for deferred-length strings. (Cf. also below). */ elem_len = (poly ? gfc_class_vtab_size_get (class_decl) - : gfc_conv_descriptor_elem_len (decl)); + : gfc_conv_descriptor_elem_len_get (decl)); tmp = (POINTER_TYPE_P (TREE_TYPE (decl)) ? build_fold_indirect_ref (decl) : decl); size = gfc_omp_get_array_size (loc, tmp, seq); @@ -2337,7 +2337,7 @@ gfc_omp_deep_mapping_item (bool is_cnt, bool do_copy, bool do_alloc_check, { if (elem_len == NULL_TREE) { - elem_len = gfc_conv_descriptor_elem_len (decl); + elem_len = gfc_conv_descriptor_elem_len_get (decl); size = fold_convert (size_type_node, gfc_omp_get_array_size (loc, decl, seq)); }
