public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch, fortran] PR fortran/100094 - Undefined pointers have incorrect rank when using optimization
@ 2021-04-15 11:56 José Rui Faustino de Sousa
  2021-04-15 12:28 ` Tobias Burnus
  0 siblings, 1 reply; 2+ messages in thread
From: José Rui Faustino de Sousa @ 2021-04-15 11:56 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 857 bytes --]

Hi All!

Proposed patch to:

PR100094 - Undefined pointers have incorrect rank when using optimization

Patch tested only on x86_64-pc-linux-gnu.

Pointers, and allocatables, must carry TKR information even when 
undefined. The patch adds code to initialize both pointers and 
allocatables element size, rank and type as soon as possible to do so. 
Latter initialization will work for allocatables, but not for pointers 
since one can not test meaningfully the association status of undefined 
pointers.

Thank you very much.

Best regards,
José Rui

Fortran: Add missing TKR initialization [PR100094]

gcc/fortran/ChangeLog:

	PR fortran/100094
	* trans-array.c (gfc_trans_deferred_array): Add code to initialize
	pointers and allocatables with correct TKR parameters.

gcc/testsuite/ChangeLog:

	PR fortran/100094
	* gfortran.dg/PR100094.f90: New test.


[-- Attachment #2: PR100094.patch --]
[-- Type: text/x-patch, Size: 1696 bytes --]

diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c
index be5eb89350f..2bd69724366 100644
--- a/gcc/fortran/trans-array.c
+++ b/gcc/fortran/trans-array.c
@@ -10920,6 +10920,20 @@ gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block)
 	}
     }
 
+  /* Set initial TKR for pointers and allocatables */
+  if (GFC_DESCRIPTOR_TYPE_P (type)
+      && (sym->attr.pointer || sym->attr.allocatable))
+    {
+      tree etype;
+
+      gcc_assert (sym->as && sym->as->rank>=0);
+      tmp = gfc_conv_descriptor_dtype (descriptor);
+      etype = gfc_get_element_type (type);
+      tmp = fold_build2_loc (input_location, MODIFY_EXPR,
+  			     TREE_TYPE (tmp), tmp,
+  			     gfc_get_dtype_rank_type (sym->as->rank, etype));
+      gfc_add_expr_to_block (&init, tmp);
+    }
   gfc_restore_backend_locus (&loc);
   gfc_init_block (&cleanup);
 
diff --git a/gcc/testsuite/gfortran.dg/PR100094.f90 b/gcc/testsuite/gfortran.dg/PR100094.f90
new file mode 100644
index 00000000000..f2f7f1631dc
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/PR100094.f90
@@ -0,0 +1,37 @@
+! { dg-do run }
+!
+! Test the fix for PR100094
+!
+
+program foo_p
+
+  implicit none
+
+  integer, parameter :: n = 11
+  
+  integer, pointer :: pout(:)
+  integer,  target :: a(n)
+  integer          :: i
+  
+  a = [(i, i=1,n)]
+  call foo(pout)
+  if(.not.associated(pout)) stop 1
+  if(.not.associated(pout, a)) stop 2
+  if(any(pout/=a)) stop 3
+  stop
+
+contains
+
+  subroutine foo(that)
+    integer, pointer, intent(out) :: that(..)
+
+    select rank(that)
+    rank(1)
+      that => a
+    rank default
+      stop 4
+    end select
+    return
+  end subroutine foo
+
+end program foo_p

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Patch, fortran] PR fortran/100094 - Undefined pointers have incorrect rank when using optimization
  2021-04-15 11:56 [Patch, fortran] PR fortran/100094 - Undefined pointers have incorrect rank when using optimization José Rui Faustino de Sousa
@ 2021-04-15 12:28 ` Tobias Burnus
  0 siblings, 0 replies; 2+ messages in thread
From: Tobias Burnus @ 2021-04-15 12:28 UTC (permalink / raw)
  To: José Rui Faustino de Sousa, fortran, gcc-patches

On 15.04.21 13:56, José Rui Faustino de Sousa via Gcc-patches wrote:

> Proposed patch to:
> PR100094 - Undefined pointers have incorrect rank when using optimization
> Patch tested only on x86_64-pc-linux-gnu.

LGTM - thanks!

Tobias

> Pointers, and allocatables, must carry TKR information even when
> undefined. The patch adds code to initialize both pointers and
> allocatables element size, rank and type as soon as possible to do so.
> Latter initialization will work for allocatables, but not for pointers
> since one can not test meaningfully the association status of
> undefined pointers.
>
> Thank you very much.
>
> Best regards,
> José Rui
>
> Fortran: Add missing TKR initialization [PR100094]
>
> gcc/fortran/ChangeLog:
>
>     PR fortran/100094
>     * trans-array.c (gfc_trans_deferred_array): Add code to initialize
>     pointers and allocatables with correct TKR parameters.
>
> gcc/testsuite/ChangeLog:
>
>     PR fortran/100094
>     * gfortran.dg/PR100094.f90: New test.
>
-----------------
Mentor Graphics (Deutschland) GmbH, Arnulfstrasse 201, 80634 München Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Frank Thürauf

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-04-15 12:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-15 11:56 [Patch, fortran] PR fortran/100094 - Undefined pointers have incorrect rank when using optimization José Rui Faustino de Sousa
2021-04-15 12:28 ` Tobias Burnus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).