if (expr->expr_type == EXPR_FUNCTION)
{
- if (expr->value.function.esym)
- return expr->value.function.esym->result->attr.contiguous;
+ if (expr->value.function.isym)
+ /* TRANSPOSE is the only intrinsic that may return a
+ non-contiguous array. It's treated as a special case in
+ gfc_conv_expr_descriptor too. */
+ return (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE);
+ else if (expr->value.function.esym)
+ /* Only a pointer to an array without the contiguous attribute
+ can be non-contiguous as a result value. */
+ return (expr->value.function.esym->result->attr.contiguous
+ || !expr->value.function.esym->result->attr.pointer);
else
{
/* Type-bound procedures. */
{
/* If the actual argument can be noncontiguous, copy-in/out is required,
if the dummy has either the CONTIGUOUS attribute or is an assumed-
- length assumed-length/assumed-size CHARACTER array. */
+ length assumed-length/assumed-size CHARACTER array. This only
+ applies if the actual argument is a "variable"; if it's some
+ non-lvalue expression, we are going to evaluate it to a
+ temporary below anyway. */
se.force_no_tmp = 1;
if ((fsym->attr.contiguous
|| (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length
&& (fsym->as->type == AS_ASSUMED_SIZE
|| fsym->as->type == AS_EXPLICIT)))
- && !gfc_is_simply_contiguous (e, false, true))
+ && !gfc_is_simply_contiguous (e, false, true)
+ && gfc_expr_is_variable (e))
{
bool optional = fsym->attr.optional;
fsym->attr.optional = 0;
fsym->attr.pointer);
}
else
+ /* This is where we introduce a temporary to store the
+ result of a non-lvalue array expression. */
gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
sym->name, NULL);
--- /dev/null
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+!
+! This program used to ICE in gimplification on the call to S, because it
+! was trying to copy out the array after the call to something that wasn't
+! an lvalue.
+
+program p
+ integer, pointer :: z(:)
+ integer, target :: x(3) = [1, 2, 3]
+ z => x
+ call s(shape(z))
+contains
+ subroutine s(x) bind(c)
+ integer, contiguous :: x(:)
+ end
+end
+
+! It should not emit any copy loops, just the loop for inlining SHAPE.
+! { dg-final { scan-tree-dump-times "while \\(1\\)" 1 "original" } }
+
+! It should not emit code to check the contiguous property.
+! { dg-final { scan-tree-dump-not "contiguous\\.\[0-9\]+" "original" } }
--- /dev/null
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+!
+! Check that copy loops to ensure contiguity of transpose result are
+! still generated after fixing pr103390, and that it does not ICE.
+
+program p
+ integer, pointer :: z(:,:)
+ integer, target :: x(3,3) = reshape ([1, 2, 3, 4, 5, 6, 7, 8, 9], shape(x))
+ z => x
+ call s(transpose(z))
+contains
+ subroutine s(x) bind(c)
+ integer, contiguous :: x(:,:)
+ end
+end
+
+! Expect 2 nested copy loops both before and after the call to S.
+! { dg-final { scan-tree-dump-times "while \\(1\\)" 4 "original" } }
+
--- /dev/null
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+!
+! Check that copy loops to ensure contiguity of the result of a function
+! that returns a non-pointer array are generated properly after fixing
+! pr103390, and that it does not ICE. In this case no copying is required.
+
+program p
+ integer, pointer :: z(:)
+ integer, target :: x(3) = [1, 2, 3]
+ z => x
+ call s(i(z))
+contains
+ function i(x)
+ integer :: i(3)
+ integer, pointer :: x(:)
+ i = x
+ end
+ subroutine s(x) bind(c)
+ integer, contiguous :: x(:)
+ end
+end
+
+! Expect one loop to copy the array contents to a temporary in function i.
+! { dg-final { scan-tree-dump-times "while \\(1\\)" 1 "original" } }
+
+! It should not emit code to check the contiguous property.
+! { dg-final { scan-tree-dump-not "contiguous\\.\[0-9\]+" "original" } }
+
--- /dev/null
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+!
+! Check that copy loops to ensure contiguity of the result of a function
+! that returns a pointer to an array are generated properly after fixing
+! pr103390, and that it does not ICE.
+
+program p
+ integer, pointer :: z(:)
+ integer, target :: x(3) = [1, 2, 3]
+ z => x
+ call s(i(z))
+contains
+ function i(x)
+ integer, pointer :: i(:)
+ integer, pointer :: x(:)
+ i => x
+ end
+ subroutine s(x) bind(c)
+ integer, contiguous :: x(:)
+ end
+end
+
+! Expect a copy loop both before and after the call to S.
+! { dg-final { scan-tree-dump-times "while \\(1\\)" 2 "original" } }
--- /dev/null
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+!
+! Check that copy loops to ensure contiguity of the result of a function
+! that returns a pointer to an array are generated properly after fixing
+! pr103390, and that it does not ICE. This variant is for an intent(in)
+! dummy argument so no copy-out is needed, only copy-in.
+
+program p
+ integer, pointer :: z(:)
+ integer, target :: x(3) = [1, 2, 3]
+ z => x
+ call s(i(z))
+contains
+ function i(x)
+ integer, pointer :: i(:)
+ integer, pointer :: x(:)
+ i => x
+ end
+ subroutine s(x) bind(c)
+ integer, contiguous, intent(in) :: x(:)
+ end
+end
+
+! Expect a copy loop before the call to S.
+! { dg-final { scan-tree-dump-times "while \\(1\\)" 1 "original" } }
--- /dev/null
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+!
+! Check that copy loops to ensure contiguity of transpose result are
+! generated properly after fixing pr103390, and that it does not ICE.
+! This variant is for an intent(in) dummy argument so no copy-out
+! is needed, only copy-in.
+
+program p
+ integer, pointer :: z(:,:)
+ integer, target :: x(3,3) = reshape ([1, 2, 3, 4, 5, 6, 7, 8, 9], shape(x))
+ z => x
+ call s(transpose(z))
+contains
+ subroutine s(x) bind(c)
+ integer, contiguous, intent(in) :: x(:,:)
+ end
+end
+
+! Expect 2 nested copy loops before the call to S.
+! { dg-final { scan-tree-dump-times "while \\(1\\)" 2 "original" } }
+
--- /dev/null
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+!
+! Check that copy loops to ensure contiguity of the result of an array
+! section expression are generated properly after fixing pr103390, and
+! that it does not ICE.
+
+program p
+ integer, pointer :: z(:)
+ integer :: A(5) = [1, 2, 3, 4, 5]
+ call s(A(::2))
+contains
+ subroutine s(x) bind(c)
+ integer, contiguous :: x(:)
+ end
+end
+
+! Expect copy loops before and after the call to S.
+! { dg-final { scan-tree-dump-times "while \\(1\\)" 2 "original" } }
--- /dev/null
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+!
+! Check that copy loops to ensure contiguity of the result of an array
+! section expression are generated properly after fixing pr103390,
+! and that it does not ICE. This case is for an intent(in)
+! dummy so no copy-out should occur, only copy-in.
+
+program p
+ integer, pointer :: z(:)
+ integer, parameter :: A(5) = [1, 2, 3, 4, 5]
+ call s(A(::2))
+contains
+ subroutine s(x) bind(c)
+ integer, contiguous, intent(in) :: x(:)
+ end
+end
+
+! Expect a copy loop before the call to S.
+! { dg-final { scan-tree-dump-times "while \\(1\\)" 1 "original" } }
--- /dev/null
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+!
+! Check that copy loops to ensure contiguity of the result of an elemental
+! array-valued expression are generated properly after fixing pr103390,
+! and that it does not ICE.
+
+program p
+ integer, pointer :: z(:)
+ integer :: a(3) = [1, 2, 3];
+ integer :: b(3) = [4, 5, 6];
+ call s(a + b);
+contains
+ subroutine s(x) bind(c)
+ integer, contiguous :: x(:)
+ end
+end
+
+! We only expect one loop before the call, to fill in the contiguous
+! temporary. No copy-out is needed since the temporary is effectively
+! an rvalue.
+! { dg-final { scan-tree-dump-times "while \\(1\\)" 1 "original" } }
+
+! It should not emit code to check the contiguous property.
+! { dg-final { scan-tree-dump-not "contiguous\\.\[0-9\]+" "original" } }
+