diff mbox

[Fortran] PRs 61881/61888 - Fix issues with SIZEOF, CLASS(*) and assumed-rank

Message ID 53D2EC76.3000907@net-b.de
State New
Headers show

Commit Message

Tobias Burnus July 25, 2014, 11:47 p.m. UTC
The patch has been motivated by the needs for implementing the openacc 
module. In particular, it does:

- Fix passing intrinsic types to CLASS(*) [F2003]
- Fix STORAGE_SIZE for polymorphic arrays [F2003]
- Permit the vendor intrinsic SIZEOF also for TYPE(*) if and only if an 
array descriptor has been used [extend GNU extension]
- Fix SIZEOF with assumed-rank [fix GNU extension]
- Document that SIZEOF's result are not well defined for noncontiguous 
arrays. [fix GNU extension]

Build and regtested on x86-64-gnu-linux.
OK for the trunk?

Tobias

Comments

Paul Richard Thomas July 26, 2014, 9:34 a.m. UTC | #1
Dear Tobias,

Whilst I am aware that we can now use the single line C++ comment,
would it perhaps be a better idea to stick with the C style comments
just for uniformity?

+      if (arg->ts.type == BT_CLASS)
+    {
+      tmp = gfc_vtable_size_get (TREE_OPERAND (argse.expr, 0));
+      tmp = fold_convert (result_type, tmp);
+      goto done;
+    }

Is there any possibility that the class object will be adorned by any
kind of reference here?  In which case, you should drill down through
the TREE_OPERANDS to find it.

Otherwise, this is OK for trunk.

Thanks for the patch

Paul


On 26 July 2014 01:47, Tobias Burnus <burnus@net-b.de> wrote:
> The patch has been motivated by the needs for implementing the openacc
> module. In particular, it does:
>
> - Fix passing intrinsic types to CLASS(*) [F2003]
> - Fix STORAGE_SIZE for polymorphic arrays [F2003]
> - Permit the vendor intrinsic SIZEOF also for TYPE(*) if and only if an
> array descriptor has been used [extend GNU extension]
> - Fix SIZEOF with assumed-rank [fix GNU extension]
> - Document that SIZEOF's result are not well defined for noncontiguous
> arrays. [fix GNU extension]
>
> Build and regtested on x86-64-gnu-linux.
> OK for the trunk?
>
> Tobias
Tobias Burnus July 26, 2014, 9:52 a.m. UTC | #2
Dear Paul,

Paul Richard Thomas wrote:
> Whilst I am aware that we can now use the single line C++ comment,
> would it perhaps be a better idea to stick with the C style comments
> just for uniformity?

Okay.

> +      if (arg->ts.type == BT_CLASS)
> +    {
> +      tmp = gfc_vtable_size_get (TREE_OPERAND (argse.expr, 0));
> +      tmp = fold_convert (result_type, tmp);
> +      goto done;
> +    }
>
> Is there any possibility that the class object will be adorned by any
> kind of reference here?  In which case, you should drill down through
> the TREE_OPERANDS to find it.

I think it should be fine - it just removes the outermost component 
reference, which should give the class struct, independently whether it 
is class_var or dt(5)%comp(7)%class_comp.

> Otherwise, this is OK for trunk.
> Thanks for the patch

Assuming that the second part is okay, I have now committed it with the 
comment-style change as Rev. 213079.

Thanks for the patch review!

Tobias

PS: Next on my to-do list is to post a RFC version of openacc_lib.h / 
module openacc, using the patch, for the gomp-4_0-branch. And then I 
want to continue on the locking/critical section support for coarrays.

PPS: I realized that the sub-pointer issues, where the actual stride is 
not a multiple of the element length, very quickly and badly hits me 
with scalar derived-type coarrays with array components. Thus, to 
support those, I am also very interested in getting the new array 
descriptor up and running and onto the trunk… I do not really like the 
idea of coding around that issue.
Thomas Schwinge Sept. 2, 2014, 5:03 p.m. UTC | #3
Hi Tobias!

On Sat, 26 Jul 2014 01:47:02 +0200, Tobias Burnus <burnus@net-b.de> wrote:
> 2014-07-26  Tobias Burnus  <burnus@net-b.de>
> 
> 	* check.c (gfc_check_sizeof): Permit for assumed type if and
> 	only if it has an array descriptor.
> 	* intrinsic.c (do_ts29113_check): Permit SIZEOF.
> 	(add_functions): SIZEOF is an Inquiry function.
> 	* intrinsic.texi (SIZEOF): Add note that only contiguous
> 	arrays are permitted.
> 	* trans-expr.c (gfc_conv_intrinsic_to_class): Handle assumed
> 	rank.
> 	* trans-intrinsic.c (gfc_conv_intrinsic_sizeof): Handle
> 	assumed type + array descriptor, CLASS and assumed rank.
> 	(gfc_conv_intrinsic_storage_size): Handle class arrays.
> 
> 2014-07-26  Tobias Burnus  <burnus@net-b.de>
> 
> 	* gfortran.dg/sizeof_2.f90: Change dg-error.
> 	* gfortran.dg/sizeof_4.f90: New.
> 	* gfortran.dg/storage_size_1.f08: Correct expected
> 	value.

I noticed that the sizeof_4.f90 test case has not been checked in,
probably just forgot to svn add the file?

Searching for it in my emails, I also noticed that a year ago a similar
patch has been posted in
<http://news.gmane.org/find-root.php?message_id=%3CCAKwh3qi633jU-ojPKqRa_16DKWhXn9L2N0Wr4trAG9p1dJ-sXg%40mail.gmail.com%3E>,
but that is now probably obsolete.


Grüße,
 Thomas
Tobias Burnus Sept. 3, 2014, 6:43 a.m. UTC | #4
Thomas Schwinge wrote:
> On Sat, 26 Jul 2014 01:47:02 +0200, Tobias Burnus <burnus@net-b.de> wrote:
>> [...]
>> 2014-07-26  Tobias Burnus  <burnus@net-b.de>
>>
>> 	* gfortran.dg/sizeof_4.f90: New.
>> [...]
> I noticed that the sizeof_4.f90 test case has not been checked in,
> probably just forgot to svn add the file?

I have now committed it as Rev. 214843 - it was lying indeed around in 
my commit-tree, lacking the "svn add".

Tobias
diff mbox

Patch

2014-07-26  Tobias Burnus  <burnus@net-b.de>

	* check.c (gfc_check_sizeof): Permit for assumed type if and
	only if it has an array descriptor.
	* intrinsic.c (do_ts29113_check): Permit SIZEOF.
	(add_functions): SIZEOF is an Inquiry function.
	* intrinsic.texi (SIZEOF): Add note that only contiguous
	arrays are permitted.
	* trans-expr.c (gfc_conv_intrinsic_to_class): Handle assumed
	rank.
	* trans-intrinsic.c (gfc_conv_intrinsic_sizeof): Handle
	assumed type + array descriptor, CLASS and assumed rank.
	(gfc_conv_intrinsic_storage_size): Handle class arrays.

2014-07-26  Tobias Burnus  <burnus@net-b.de>

	* gfortran.dg/sizeof_2.f90: Change dg-error.
	* gfortran.dg/sizeof_4.f90: New.
	* gfortran.dg/storage_size_1.f08: Correct expected
	value.

diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c
index eff2c4c..ff7e53d 100644
--- a/gcc/fortran/check.c
+++ b/gcc/fortran/check.c
@@ -3902,7 +3902,12 @@  gfc_check_sizeof (gfc_expr *arg)
       return false;
     }
 
-  if (arg->ts.type == BT_ASSUMED)
+  // TYPE(*) is acceptable if and only if it uses an array descriptor.
+  if (arg->ts.type == BT_ASSUMED
+      && (arg->symtree->n.sym->as == NULL
+	  || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
+	      && arg->symtree->n.sym->as->type != AS_DEFERRED
+	      && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
     {
       gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be TYPE(*)",
 		 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
diff --git a/gcc/fortran/intrinsic.c b/gcc/fortran/intrinsic.c
index d681d70..1ad1e69 100644
--- a/gcc/fortran/intrinsic.c
+++ b/gcc/fortran/intrinsic.c
@@ -204,6 +204,7 @@  do_ts29113_check (gfc_intrinsic_sym *specific, gfc_actual_arglist *arg)
 	       && specific->id != GFC_ISYM_RANK
 	       && specific->id != GFC_ISYM_SHAPE
 	       && specific->id != GFC_ISYM_SIZE
+	       && specific->id != GFC_ISYM_SIZEOF
 	       && specific->id != GFC_ISYM_UBOUND
 	       && specific->id != GFC_ISYM_C_LOC)
 	{
@@ -2765,8 +2766,9 @@  add_functions (void)
 	     ar, BT_REAL, dr, REQUIRED, dm, BT_INTEGER, ii, OPTIONAL);
   make_from_module();
 
-  add_sym_1 ("sizeof", GFC_ISYM_SIZEOF, CLASS_IMPURE, ACTUAL_NO, BT_INTEGER, ii,
-	     GFC_STD_GNU, gfc_check_sizeof, gfc_simplify_sizeof, NULL,
+  add_sym_1 ("sizeof", GFC_ISYM_SIZEOF, CLASS_INQUIRY, ACTUAL_NO,
+	     BT_INTEGER, ii, GFC_STD_GNU,
+	     gfc_check_sizeof, gfc_simplify_sizeof, NULL,
 	     x, BT_UNKNOWN, 0, REQUIRED);
 
   make_generic ("sizeof", GFC_ISYM_SIZEOF, GFC_STD_GNU);
diff --git a/gcc/fortran/intrinsic.texi b/gcc/fortran/intrinsic.texi
index 152b46c..6c4cb09 100644
--- a/gcc/fortran/intrinsic.texi
+++ b/gcc/fortran/intrinsic.texi
@@ -12205,7 +12205,9 @@  to is returned.  If the argument is of a derived type with @code{POINTER}
 or @code{ALLOCATABLE} components, the return value does not account for
 the sizes of the data pointed to by these components. If the argument is
 polymorphic, the size according to the declared type is returned. The argument
-may not be a procedure or procedure pointer.
+may not be a procedure or procedure pointer. Note that the code assumes for
+arrays that those are contiguous; for contiguous arrays, it returns the
+storage or an array element multiplicated by the size of the array.
 
 @item @emph{Example}:
 @smallexample
diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 81f2137..02cec97 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -564,7 +564,7 @@  gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
   var = gfc_create_var (tmp, "class");
 
   /* Set the vptr.  */
-  ctree =  gfc_class_vptr_get (var);
+  ctree = gfc_class_vptr_get (var);
 
   vtab = gfc_find_vtab (&e->ts);
   gcc_assert (vtab);
@@ -573,7 +573,7 @@  gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
 		  fold_convert (TREE_TYPE (ctree), tmp));
 
   /* Now set the data field.  */
-  ctree =  gfc_class_data_get (var);
+  ctree = gfc_class_data_get (var);
   if (parmse->ss && parmse->ss->info->useflags)
     {
       /* For an array reference in an elemental procedure call we need
@@ -589,7 +589,16 @@  gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
 	{
 	  parmse->ss = NULL;
 	  gfc_conv_expr_reference (parmse, e);
-	  tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
+	  if (class_ts.u.derived->components->as
+	      && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
+	    {
+	      tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
+						   gfc_expr_attr (e));
+	      tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
+				     TREE_TYPE (ctree), tmp);
+	    }
+	  else
+	      tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
 	  gfc_add_modify (&parmse->pre, ctree, tmp);
 	}
       else
@@ -597,7 +606,14 @@  gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
 	  parmse->ss = ss;
 	  parmse->use_offset = 1;
 	  gfc_conv_expr_descriptor (parmse, e);
-	  gfc_add_modify (&parmse->pre, ctree, parmse->expr);
+	  if (class_ts.u.derived->components->as->rank != e->rank)
+	    {
+	      tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
+				     TREE_TYPE (ctree), parmse->expr);
+	      gfc_add_modify (&parmse->pre, ctree, tmp);
+	    }
+	  else
+	    gfc_add_modify (&parmse->pre, ctree, parmse->expr);
 	}
     }
 
diff --git a/gcc/fortran/trans-intrinsic.c b/gcc/fortran/trans-intrinsic.c
index 3de0b09..31c9207 100644
--- a/gcc/fortran/trans-intrinsic.c
+++ b/gcc/fortran/trans-intrinsic.c
@@ -5891,62 +5891,131 @@  gfc_conv_intrinsic_sizeof (gfc_se *se, gfc_expr *expr)
   gfc_expr *arg;
   gfc_se argse;
   tree source_bytes;
-  tree type;
   tree tmp;
   tree lower;
   tree upper;
+  tree byte_size;
   int n;
 
-  arg = expr->value.function.actual->expr;
-
   gfc_init_se (&argse, NULL);
+  arg = expr->value.function.actual->expr;
 
-  if (arg->rank == 0)
+  if (arg->rank || arg->ts.type == BT_ASSUMED)
+    gfc_conv_expr_descriptor (&argse, arg);
+  else
+    gfc_conv_expr_reference (&argse, arg);
+
+  if (arg->ts.type == BT_ASSUMED)
+    {
+      // This only works if an array descriptor has been passed; thus, extract
+      // the size from the descriptor.
+      gcc_assert (TYPE_PRECISION (gfc_array_index_type)
+		  == TYPE_PRECISION (size_type_node));
+      tmp = arg->symtree->n.sym->backend_decl;
+      tmp = DECL_LANG_SPECIFIC (tmp)
+	    && GFC_DECL_SAVED_DESCRIPTOR (tmp) != NULL_TREE
+	    ? GFC_DECL_SAVED_DESCRIPTOR (tmp) : tmp;
+      if (POINTER_TYPE_P (TREE_TYPE (tmp)))
+	tmp = build_fold_indirect_ref_loc (input_location, tmp);
+      tmp = fold_convert (size_type_node, gfc_conv_descriptor_dtype (tmp));
+      tmp = fold_build2_loc (input_location, RSHIFT_EXPR, TREE_TYPE (tmp), tmp,
+			     build_int_cst (TREE_TYPE (tmp),
+					    GFC_DTYPE_SIZE_SHIFT));
+      byte_size = fold_convert (gfc_array_index_type, tmp);
+    }
+  else if (arg->ts.type == BT_CLASS)
+    {
+      if (arg->rank)
+	byte_size = gfc_vtable_size_get (TREE_OPERAND (argse.expr, 0));
+      else
+	byte_size = gfc_vtable_size_get (argse.expr);
+    }
+  else
     {
-      if (arg->ts.type == BT_CLASS)
-	gfc_add_data_component (arg);
-
-      gfc_conv_expr_reference (&argse, arg);
-
-      type = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
-						 argse.expr));
-
-      /* Obtain the source word length.  */
       if (arg->ts.type == BT_CHARACTER)
-	se->expr = size_of_string_in_bytes (arg->ts.kind,
-					    argse.string_length);
+	byte_size = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
       else
-	se->expr = fold_convert (gfc_array_index_type, size_in_bytes (type)); 
+	{
+	  if (arg->rank == 0)
+	    byte_size = TREE_TYPE (build_fold_indirect_ref_loc (input_location,
+								argse.expr));
+	  else
+	    byte_size = gfc_get_element_type (TREE_TYPE (argse.expr));
+	  byte_size = fold_convert (gfc_array_index_type,
+				    size_in_bytes (byte_size));
+	}
     }
+
+  if (arg->rank == 0)
+    se->expr = byte_size;
   else
     {
       source_bytes = gfc_create_var (gfc_array_index_type, "bytes");
-      argse.want_pointer = 0;
-      gfc_conv_expr_descriptor (&argse, arg);
-      type = gfc_get_element_type (TREE_TYPE (argse.expr));
+      gfc_add_modify (&argse.pre, source_bytes, byte_size);
 
-      /* Obtain the argument's word length.  */
-      if (arg->ts.type == BT_CHARACTER)
-	tmp = size_of_string_in_bytes (arg->ts.kind, argse.string_length);
-      else
-	tmp = fold_convert (gfc_array_index_type,
-			    size_in_bytes (type)); 
-      gfc_add_modify (&argse.pre, source_bytes, tmp);
-
-      /* Obtain the size of the array in bytes.  */
-      for (n = 0; n < arg->rank; n++)
+      if (arg->rank == -1)
 	{
-	  tree idx;
-	  idx = gfc_rank_cst[n];
-	  lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
-	  upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
-	  tmp = fold_build2_loc (input_location, MINUS_EXPR,
-				 gfc_array_index_type, upper, lower);
-	  tmp = fold_build2_loc (input_location, PLUS_EXPR,
-				 gfc_array_index_type, tmp, gfc_index_one_node);
+	  tree cond, loop_var, exit_label;
+          stmtblock_t body;
+
+	  tmp = fold_convert (gfc_array_index_type,
+			      gfc_conv_descriptor_rank (argse.expr));
+	  loop_var = gfc_create_var (gfc_array_index_type, "i");
+	  gfc_add_modify (&argse.pre, loop_var, gfc_index_zero_node);
+          exit_label = gfc_build_label_decl (NULL_TREE);
+
+	  /* Create loop:
+	     for (;;)
+		{
+		  if (i >= rank)
+		    goto exit;
+		  source_bytes = source_bytes * array.dim[i].extent;
+		  i = i + 1;
+		}
+	      exit:  */
+	  gfc_start_block (&body);
+	  cond = fold_build2_loc (input_location, GE_EXPR, boolean_type_node,
+				  loop_var, tmp);
+	  tmp = build1_v (GOTO_EXPR, exit_label);
+	  tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
+				 cond, tmp, build_empty_stmt (input_location));
+	  gfc_add_expr_to_block (&body, tmp);
+
+	  lower = gfc_conv_descriptor_lbound_get (argse.expr, loop_var);
+	  upper = gfc_conv_descriptor_ubound_get (argse.expr, loop_var);
+	  tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
 	  tmp = fold_build2_loc (input_location, MULT_EXPR,
 				 gfc_array_index_type, tmp, source_bytes);
-	  gfc_add_modify (&argse.pre, source_bytes, tmp);
+	  gfc_add_modify (&body, source_bytes, tmp);
+
+	  tmp = fold_build2_loc (input_location, PLUS_EXPR,
+				 gfc_array_index_type, loop_var,
+				 gfc_index_one_node);
+	  gfc_add_modify_loc (input_location, &body, loop_var, tmp);
+
+	  tmp = gfc_finish_block (&body);
+
+	  tmp = fold_build1_loc (input_location, LOOP_EXPR, void_type_node,
+				 tmp);
+	  gfc_add_expr_to_block (&argse.pre, tmp);
+
+	  tmp = build1_v (LABEL_EXPR, exit_label);
+	  gfc_add_expr_to_block (&argse.pre, tmp);
+	}
+      else
+	{
+	  /* Obtain the size of the array in bytes.  */
+	  for (n = 0; n < arg->rank; n++)
+	    {
+	      tree idx;
+	      idx = gfc_rank_cst[n];
+	      lower = gfc_conv_descriptor_lbound_get (argse.expr, idx);
+	      upper = gfc_conv_descriptor_ubound_get (argse.expr, idx);
+	      tmp = gfc_conv_array_extent_dim (lower, upper, NULL);
+	      tmp = fold_build2_loc (input_location, MULT_EXPR,
+				     gfc_array_index_type, tmp, source_bytes);
+	      gfc_add_modify (&argse.pre, source_bytes, tmp);
+	    }
 	}
       se->expr = source_bytes;
     }
@@ -5970,13 +6039,13 @@  gfc_conv_intrinsic_storage_size (gfc_se *se, gfc_expr *expr)
   if (arg->rank == 0)
     {
       if (arg->ts.type == BT_CLASS)
-      {
-	gfc_add_vptr_component (arg);
-	gfc_add_size_component (arg);
-	gfc_conv_expr (&argse, arg);
-	tmp = fold_convert (result_type, argse.expr);
-	goto done;
-      }
+	{
+	  gfc_add_vptr_component (arg);
+	  gfc_add_size_component (arg);
+	  gfc_conv_expr (&argse, arg);
+	  tmp = fold_convert (result_type, argse.expr);
+	  goto done;
+	}
 
       gfc_conv_expr_reference (&argse, arg);
       type = TREE_TYPE (build_fold_indirect_ref_loc (input_location, 
@@ -5986,6 +6055,12 @@  gfc_conv_intrinsic_storage_size (gfc_se *se, gfc_expr *expr)
     {
       argse.want_pointer = 0;
       gfc_conv_expr_descriptor (&argse, arg);
+      if (arg->ts.type == BT_CLASS)
+	{
+	  tmp = gfc_vtable_size_get (TREE_OPERAND (argse.expr, 0));
+	  tmp = fold_convert (result_type, tmp);
+	  goto done;
+	}
       type = gfc_get_element_type (TREE_TYPE (argse.expr));
     }
     
diff --git a/gcc/testsuite/gfortran.dg/sizeof_2.f90 b/gcc/testsuite/gfortran.dg/sizeof_2.f90
index 5f19288..e6661a5 100644
--- a/gcc/testsuite/gfortran.dg/sizeof_2.f90
+++ b/gcc/testsuite/gfortran.dg/sizeof_2.f90
@@ -10,7 +10,7 @@  subroutine foo(x, y)
   integer(8) :: ii
   procedure() :: proc
 
-  ii = sizeof (x) ! { dg-error "Assumed-type argument at .1. is not permitted as actual argument to the intrinsic sizeof" }
+  ii = sizeof (x) ! { dg-error "'x' argument of 'sizeof' intrinsic at \\(1\\) shall not be TYPE\\(\\*\\)" }
   ii = c_sizeof (x) ! { dg-error "Assumed-type argument at .1. is not permitted as actual argument to the intrinsic c_sizeof" }
   ii = storage_size (x) ! { dg-error "Assumed-type argument at .1. is not permitted as actual argument to the intrinsic storage_size" }
 
diff --git a/gcc/testsuite/gfortran.dg/sizeof_4.f90 b/gcc/testsuite/gfortran.dg/sizeof_4.f90
new file mode 100644
index 0000000..d4d8baa
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/sizeof_4.f90
@@ -0,0 +1,95 @@ 
+! { dg-do run }
+!
+! PR fortran/61881
+! PR fortran/61888
+!
+!
+use iso_c_binding
+implicit none
+
+call dim0(5, 4)
+
+call dim1([1, 2, 3], 4*3)
+
+call dimd(5, 4)
+call dimd([1, 2, 3], 4*3)
+call dimd(reshape([1, 4, 2, 3],[2, 2]), 4*4)
+
+call tdim1([1, 2, 3], 4*3)
+call tdim1([1_8, 2_8, 3_8], 8*3)
+
+call tdimd(5, 4)
+call tdimd([1, 2, 3], 4*3)
+call tdimd(reshape([1, 4, 2, 3], [2, 2]), 4*4)
+call tdimd(5_8, 8)
+call tdimd([1_8, 2_8, 3_8], 8*3)
+call tdimd(reshape([1_8, 4_8, 2_8, 3_8],[2,2]), 8*4)
+
+call cdim0(5, 4)
+
+call cdim1([1, 2, 3], 4*3)
+
+call cdimd(5, 4)
+call cdimd([1, 2, 3], 4*3)
+call cdimd(reshape([1,4,2,3],[2,2]), 4*4)
+call cdimd(5_8, 8)
+call cdimd([1_8, 2_8, 3_8], 8*3)
+call cdimd(reshape([1_8, 4_8, 2_8, 3_8], [2, 2]), 8*4)
+
+contains
+
+subroutine dim0(x, expected_size)
+  integer :: x
+  integer, value :: expected_size
+  if (sizeof(x) /= expected_size) call abort()
+  if (storage_size(x)/8 /= expected_size) call abort()
+end
+
+subroutine dim1(x, expected_size)
+  integer, dimension(:) :: x
+  integer, value :: expected_size
+  if (sizeof(x) /= expected_size) call abort()
+  if (storage_size(x)/8*size(x) /= expected_size) call abort()
+end
+
+subroutine dimd(x, expected_size)
+  integer, dimension(..) :: x
+  integer, value :: expected_size
+  if (sizeof(x) /= expected_size) call abort()
+  if (storage_size(x)/8*size(x) /= expected_size) call abort()
+end
+
+subroutine cdim0(x, expected_size)
+  class(*) :: x
+  integer, value :: expected_size
+  if (sizeof(x) /= expected_size) call abort()
+  if (storage_size(x)/8 /= expected_size) call abort()
+end
+
+subroutine cdim1(x, expected_size)
+  class(*), dimension(:) :: x
+  integer, value :: expected_size
+  if (sizeof(x) /= expected_size) call abort()
+  if (storage_size(x)/8*size(x) /= expected_size) call abort()
+end
+
+subroutine cdimd(x, expected_size)
+  class(*), dimension(..) :: x
+  integer, value :: expected_size
+  if (sizeof(x) /= expected_size) call abort()
+  if (storage_size(x)/8*size(x) /= expected_size) call abort()
+end
+
+subroutine tdim1(x, expected_size)
+  type(*), dimension(:) :: x
+  integer, value :: expected_size
+  if (sizeof(x) /= expected_size) call abort()
+end
+
+subroutine tdimd(x, expected_size)
+  type(*), dimension(..) :: x
+  integer, value :: expected_size
+  if (sizeof(x) /= expected_size) call abort()
+end
+
+end
diff --git a/gcc/testsuite/gfortran.dg/storage_size_1.f08 b/gcc/testsuite/gfortran.dg/storage_size_1.f08
index ade9dfc..71d3589 100644
--- a/gcc/testsuite/gfortran.dg/storage_size_1.f08
+++ b/gcc/testsuite/gfortran.dg/storage_size_1.f08
@@ -25,7 +25,7 @@  if (storage_size(a)  /= 64) call abort()
 if (sizeof(b)        /= 24) call abort()
 if (storage_size(b)  /= 64) call abort()
 
-if (sizeof(cp)       /=  8) call abort()
+if (sizeof(cp)       /= 12) call abort()
 if (storage_size(cp) /= 96) call abort()
 
 end