Hello world,
This fixes the final part of PR 40598, assigning an array constructor
to a slice of an array. The most difficult part was that the coarray
initialization code did strange things and then called
gfc_trans_assignment, which was confused. Fixed by adding the
contiguous attribute on the symbol temporarily, which also keeps
gfc_is_simply_contiguous happy.
Regression-tested. OK for trunk?
Best regards
Thomas
gcc/fortran/ChangeLog:
PR fortran/40598
* dump-parse-tree.cc (show_expr): Show shape of array constructors.
* expr.cc (gfc_is_simply_contiguous): If strict, do not
count a single element as contiguous.
* trans-decl.cc (generate_coarray_sym_init): Temporarily set
contiguous attribute on sym before invoking gfc_trans_assignment.
* trans-expr.cc (gfc_trans_array_constructor_copy): Handle
variable-sized arrays.
(copyable_array_p): Remove.
(copyable_slice_p): Coarrays are allowed if the artificial
contiguous attribute is set.
(gfc_trans_assignment): Avoid multiple calls to copyable_slice_p.
Use __builtin_memcpy for contiguous slices when the RHS is
a constant array constructor.
gcc/testsuite/ChangeLog:
PR fortran/40598
* gfortran.dg/array_constructor_14.f90: Adjust scans of original tree
dump.
* gfortran.dg/array_constructor_47.f90: Likewise.
* gfortran.dg/array_memcpy_7.f90: New test.
diff --git a/gcc/fortran/dump-parse-tree.cc b/gcc/fortran/dump-parse-tree.cc
index da185baecec..f86cfda66d7 100644
--- a/gcc/fortran/dump-parse-tree.cc
+++ b/gcc/fortran/dump-parse-tree.cc
@@ -567,6 +567,21 @@ show_expr (gfc_expr *p)
fputs (" /)", dumpfile);
show_ref (p->ref);
+ fputs (" shape ([", dumpfile);
+ if (p->shape != NULL)
+ {
+ int i;
+ for (i=0; i<p->rank-1; i++)
+ {
+ mpz_out_str (dumpfile, 10, p->shape[i]);
+ fputs (", ", dumpfile);
+ }
+ mpz_out_str (dumpfile, 10, p->shape[i]);
+ }
+ else
+ fputs ("NULL", dumpfile);
+
+ fputs ("])", dumpfile);
break;
case EXPR_NULL:
diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc
index d31c807b958..c1f2eeebf49 100644
--- a/gcc/fortran/expr.cc
+++ b/gcc/fortran/expr.cc
@@ -6560,7 +6560,7 @@ gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element)
if (ar->dimen_type[i] == DIMEN_ELEMENT)
{
/* An extent of one does not hurt as long as we aren't strict. */
- if (ar->as && ar->as->lower[i] && ar->as->upper[i]
+ if (!strict && ar->as && ar->as->lower[i] && ar->as->upper[i]
&& ar->start[i]
&& gfc_dep_compare_expr (ar->as->lower[i], ar->as->upper[i]) == 0
&& gfc_dep_compare_expr (ar->as->lower[i], ar->start[i]) == 0)
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 5129637165a..d0333b79399 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -6374,10 +6374,14 @@ generate_coarray_sym_init (gfc_symbol *sym)
if (cnext)
{
+ gfc_expr *v = sym->value;
+ int rank = gfc_symbol_rank (sym);
/* An EXPR_ARRAY with a rank > 1 here has to come from a
- DATA statement. Set its rank here as not to confuse
+ DATA statement. Set its shape here to avoid confusing the
the following steps. */
- sym->value->rank = 1;
+ v->shape = gfc_get_shape (rank);
+ for (int i = 0; i < rank; i++)
+ spec_dimen_size (sym->as, i, &(v->shape[i]));
}
else
{
@@ -6391,9 +6395,14 @@ generate_coarray_sym_init (gfc_symbol *sym)
}
sym->attr.pointer = 1;
+ /* We need to fake the contiguous attribute so that copyable_slice_p
+ knows that it can actually work with this coarray and
+ gfc_is_simply_contiguous returns true. */
+ sym->attr.contiguous = 1;
tmp = gfc_trans_assignment (gfc_lval_expr_from_sym (sym), sym->value,
true, false);
sym->attr.pointer = 0;
+ sym->attr.contiguous = 0;
gfc_add_expr_to_block (&caf_init_block, tmp);
}
else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pointer_comp)
diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 7ab71ee05ba..4022d7f0bcc 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -13206,39 +13206,73 @@ static tree
gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
{
unsigned HOST_WIDE_INT nelem;
- tree dst, dtype;
+ tree dst_decl, dtype, dst;
tree src, stype;
tree len;
- tree tmp;
+ tree call;
+ gfc_se dst_se;
+ stmtblock_t block;
nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
if (nelem == 0)
return NULL_TREE;
- dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
- dtype = TREE_TYPE (dst);
- if (POINTER_TYPE_P (dtype))
- dtype = TREE_TYPE (dtype);
- if (!GFC_ARRAY_TYPE_P (dtype))
- return NULL_TREE;
+ gfc_init_block (&block);
+ gfc_init_se (&dst_se, NULL);
+ gfc_conv_expr_descriptor (&dst_se, expr1);
+ gfc_add_block_to_block (&block, &dst_se.pre);
- /* Determine the lengths of the array. */
- len = GFC_TYPE_ARRAY_SIZE (dtype);
- if (!len || TREE_CODE (len) != INTEGER_CST)
- return NULL_TREE;
+ len = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (dst_se.expr)));
+ len = fold_convert (gfc_array_index_type, len);
- /* Confirm that the constructor is the same size. */
- if (compare_tree_int (len, nelem) != 0)
- return NULL_TREE;
+ for (int dim = 0; dim < expr1->rank; dim++)
+ {
+ tree ex_src, lb_dst, ub_dst, ex_dst;
+
+ ex_src = gfc_conv_mpz_to_tree (expr2->shape[dim], gfc_index_integer_kind);
+
+ lb_dst = gfc_conv_array_lbound (dst_se.expr, dim);
+ ub_dst = gfc_conv_array_ubound (dst_se.expr, dim);
+ ex_dst = gfc_conv_array_extent_dim (lb_dst, ub_dst, NULL);
+
+ len = fold_build2_loc (input_location, MULT_EXPR,
+ gfc_array_index_type, len,
+ fold_convert (gfc_array_index_type, ex_dst));
+ if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) != 0)
+ {
+ tree cond;
+ char *msg;
- tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
- len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
- fold_convert (gfc_array_index_type, tmp));
+ cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
+ ex_dst, ex_src);
+ msg = xasprintf ("Array bound mismatch for dimension %d "
+ "of array '%s' (%%ld/%%ld)", dim + 1,
+ expr1->symtree->name);
+ gfc_trans_runtime_check (true, false, cond, &block, &expr1->where, msg,
+ fold_convert (long_integer_type_node, ex_src),
+ fold_convert (long_integer_type_node, ex_dst));
+ }
+ }
+
+
+ dst_decl = gfc_get_symbol_decl (expr1->symtree->n.sym);
+ dtype = TREE_TYPE (dst_decl);
+ if (POINTER_TYPE_P (dtype))
+ dtype = TREE_TYPE (dtype);
+
+ if (gfc_full_array_ref_p (expr1->ref, NULL) && GFC_ARRAY_TYPE_P (dtype))
+ dst = dst_decl;
+ else
+ dst = gfc_conv_descriptor_data_get (dst_se.expr);
stype = gfc_typenode_for_spec (&expr2->ts);
src = gfc_build_constant_array_constructor (expr2, stype);
- return gfc_build_memcpy_call (dst, src, len);
+ call = gfc_build_memcpy_call (dst, src, len);
+ gfc_add_expr_to_block (&block, call);
+ gfc_add_block_to_block (&block, &dst_se.post);
+
+ return gfc_finish_block (&block);
}
@@ -14449,44 +14483,6 @@ gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
}
-/* Check whether EXPR is a copyable array. */
-
-static bool
-copyable_array_p (gfc_expr * expr)
-{
- if (expr->expr_type != EXPR_VARIABLE)
- return false;
-
- /* First check it's an array. */
- if (expr->rank < 1 || !expr->ref || expr->ref->next)
- return false;
-
- if (!gfc_full_array_ref_p (expr->ref, NULL))
- return false;
-
- /* Next check that it's of a simple enough type. */
- switch (expr->ts.type)
- {
- case BT_INTEGER:
- case BT_REAL:
- case BT_COMPLEX:
- case BT_LOGICAL:
- return true;
-
- case BT_CHARACTER:
- return false;
-
- case_bt_struct:
- return (!expr->ts.u.derived->attr.alloc_comp
- && !expr->ts.u.derived->attr.pdt_type);
-
- default:
- break;
- }
-
- return false;
-}
-
/* Check whether EXPR is a copyable slice. */
static bool
@@ -14499,9 +14495,12 @@ copyable_slice_p (gfc_expr * expr)
if (expr->rank < 1 || !expr->ref || expr->ref->next)
return false;
- /* Don't touch anything with coarrays. */
- if (gfc_is_coindexed (expr) || expr->symtree->n.sym->attr.codimension
- || expr->corank > 0)
+ /* Don't touch anything with coarrays, unless it has been artifiially
+ marked as contiguous in generate_coarray_sym_init. */
+
+ if (!expr->symtree->n.sym->attr.contiguous
+ && (gfc_is_coindexed (expr) || expr->symtree->n.sym->attr.codimension
+ || expr->corank > 0))
return false;
if (!gfc_is_simply_contiguous (expr, false, false))
@@ -14540,6 +14539,7 @@ gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
bool dealloc, bool use_vptr_copy, bool may_alias)
{
tree tmp;
+ bool copyable_slice_expr1;
/* Special case a single function returning an array. */
if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
@@ -14549,8 +14549,10 @@ gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
return tmp;
}
+ copyable_slice_expr1 = copyable_slice_p (expr1);
+
/* Special case assigning an array or array slice to zero. */
- if (copyable_slice_p (expr1) && is_zero_initializer_p (expr2))
+ if (copyable_slice_expr1 && is_zero_initializer_p (expr2))
{
tmp = gfc_trans_zero_assign (expr1);
if (tmp)
@@ -14558,7 +14560,7 @@ gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
}
/* Special case copying or moving one array slice to another. */
- if (copyable_slice_p (expr1)
+ if (copyable_slice_expr1
&& !gfc_is_reallocatable_lhs (expr1)
&& copyable_slice_p (expr2)
&& gfc_compare_types (&expr1->ts, &expr2->ts))
@@ -14569,7 +14571,8 @@ gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
}
/* Special case initializing an array from a constant array constructor. */
- if (copyable_array_p (expr1)
+ if (copyable_slice_expr1
+ && !gfc_is_reallocatable_lhs (expr1)
&& expr2->expr_type == EXPR_ARRAY
&& gfc_compare_types (&expr1->ts, &expr2->ts))
{
diff --git a/gcc/testsuite/gfortran.dg/array_constructor_14.f90 b/gcc/testsuite/gfortran.dg/array_constructor_14.f90
index c318bb8587f..b65c0eca186 100644
--- a/gcc/testsuite/gfortran.dg/array_constructor_14.f90
+++ b/gcc/testsuite/gfortran.dg/array_constructor_14.f90
@@ -11,4 +11,4 @@ subroutine bar(x)
x = (/ 3, 1, 4, 1 /)
end subroutine
-! { dg-final { scan-tree-dump-times "data" 0 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_memcpy" 2 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/array_constructor_47.f90 b/gcc/testsuite/gfortran.dg/array_constructor_47.f90
index 19ec8c8fd8f..a26ae73d3e9 100644
--- a/gcc/testsuite/gfortran.dg/array_constructor_47.f90
+++ b/gcc/testsuite/gfortran.dg/array_constructor_47.f90
@@ -19,5 +19,6 @@ program main
eleven_ones = [ (1._dp, i=1,11) ]
end function eleven_ones
end program main
-! { dg-final { scan-tree-dump-times "while" 4 "original" } }
+! { dg-final { scan-tree-dump-times "while" 3 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_memcpy" 1 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/array_memcpy_7.f90 b/gcc/testsuite/gfortran.dg/array_memcpy_7.f90
new file mode 100644
index 00000000000..bc3fa514192
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/array_memcpy_7.f90
@@ -0,0 +1,10 @@
+! { dg-do run }
+! { dg-additional-options "-fdump-tree-original" }
+program memain
+ integer, dimension(4,3) :: a
+ a = 0
+ a(2:3,2) = [1,2]
+ if (any(reshape(a,[size(a)]) /= [0,0,0,0,0,1,2,0,0,0,0,0])) stop 1
+end program memain
+! { dg-final { scan-tree-dump-times "__builtin_memcpy" 1 "original" } }
+