[PATCH] Allow Flang style pointer printing in pointer-to-pointer.exp

Sharma, Alok Kumar AlokKumar.Sharma@amd.com
Fri Nov 13 08:46:19 GMT 2020


Hi Andrew,

Thanks a lot for spending time on this.

Let my share some of the examples in favor of what I call Fortran style pointer printing.
Please consider below example.
-------------------- 
     1  program main
     2    integer :: var1
     3    integer, pointer :: var2
     4    integer :: res
     5    var1 = 3
     6    allocate (var2)
     7    var2 = 4
     8    res = var1 + var2
     9    print *, "var1 = " , var1
    10    print *, "var2 = " , var2
    11    print *, "var1 + var2 = " , var1 + var2
    12    print *, "res = " , res
    13
    14  end program main
------------------- 
Let us compile it with Flang and debug under gdb
-------------------
Breakpoint 3, main () at ptr.f90:8
8         res = var1 + var2
(gdb) p var1 + var2
$1 = 7
(gdb) n

Breakpoint 2, main () at ptr.f90:9
9         print *, "var1 = " , var1
(gdb) p res
$2 = 7
(gdb) b 12
Breakpoint 4 at 0x201dbd: file ptr.f90, line 12.
(gdb) c
Continuing.
 var1 =             3
 var2 =             4
 var1 + var2 =             7

Breakpoint 4, main () at ptr.f90:12
12        print *, "res = " , res
(gdb) p var1
$3 = 3
(gdb) p var2
$4 = 4
(gdb) p var1 + var2
$5 = 7
(gdb) p res
$6 = 7
-------------------- 
Please note that variable var2 (with pointer attribute) in program and in debugger behaves similarly. 
The variables with or without pointer attribute are no different inside program and debugger.

Please consider the debug session of same program (compiled with Gfortran)
--------------------
(gdb)
8         res = var1 + var2
(gdb)
9         print *, "var1 = " , var1
(gdb) p var1
$1 = 3
(gdb) p var2
$2 = (PTR TO -> ( integer(kind=4) )) 0x55555575a820
(gdb) p var1 + var2
$3 = (PTR TO -> ( integer(kind=4) )) 0x55555575a82c
(gdb) p res
$4 = 7
(gdb) p *$3
$5 = 0
-------------------- 

I felt first debug session was more convenient for Fortran programmer.
In my opinion, Fortran pointers are less like c pointers 
 - As Fortran does not expose address of pointer variable.
 - Fortran doesn't facilitate dereferencing mechanism (*ptr).
I believe Fortran pointers are pointers with safety.
 - A mechanism to avoid attaching pointer variable to any random variable (target attribute is needed).
 - The role of pointer is more in frontend of compiler.

Regards,
Alok


-----Original Message-----
From: Andrew Burgess <andrew.burgess@embecosm.com> 
Sent: Thursday, November 12, 2020 3:05 AM
To: Sharma, Alok Kumar <AlokKumar.Sharma@amd.com>
Cc: gdb-patches@sourceware.org; George, Jini Susan <JiniSusan.George@amd.com>; Achra, Nitika <Nitika.Achra@amd.com>
Subject: Re: [PATCH] Allow Flang style pointer printing in pointer-to-pointer.exp

[CAUTION: External Email]

* Sharma, Alok Kumar <AlokKumar.Sharma@amd.com> [2020-11-09 18:47:48 +0000]:

> Hi All,
>
> I request you all to please review this patch. Please find the below details.
>
> Problem Description:
> Printing of fortran variable with pointer attribute inside debugger is 
> different for Flang when compared to gfortran. For Gfortran it is like 
> c pointers (as in c/c++ source with printf, like * for dereferencing). 
> For Flang it is like fortran pointers (as in fortran source with 
> print, value is printed without need of dereferencing).
> Resolution:
> Test case is changed to allow Flang style as well.
>
> gdb/testsuite/ChangeLog
>
>         * gdb.fortran/pointer-to-pointer.exp: Allow Flang style pointer
>         printing.
>
> Regards,
> Alok
>
>
> From b731543011c3913723b7568d9cae10ec1a389f18 Mon Sep 17 00:00:00 2001
> From: Alok Kumar Sharma <AlokKumar.Sharma@amd.com>
> Date: Mon, 9 Nov 2020 23:16:21 +0530
> Subject: [PATCH] Allow Flang style pointer printing in 
> pointer-to-pointer.exp
>
> Printing of fortran variable with pointer attribute inside debugger is 
> different for Flang when compared to gfortran. For Gfortran it is like 
> c pointers (as in c/c++ source with printf, like * for dereferencing). 
> For Flang it is like fortran pointers (as in fortran source with 
> print, value is printed without need of dereferencing).
> Test case is changed to allow Flang style as well.

Looking through the patch I'm not convinced.

You seem to be saying that given the following Fortran variables:

  type(some_type), pointer :: var1
  type(some_type) :: var2

Within GDB `ptype var1` would be the same as `ptype var2`.  Is this correct?

I don't claim to be any expert on the internals of a Fortran compiler, but is it always the case that the "pointer"-ness of a variable can be removed?  Or is it specific to this very simple test case, and Flang has chosen to optimise this case?

>
> gdb/testsuite/ChangeLog
>
>       * gdb.fortran/pointer-to-pointer.exp: Allow Flang style pointer
>       printing.
>
> ---
>  gdb/testsuite/ChangeLog                       |  5 +++
>  .../gdb.fortran/pointer-to-pointer.exp        | 32 ++++++++++++++++---
>  2 files changed, 32 insertions(+), 5 deletions(-)
>
> diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 
> d3e81a2f08..8380176024 100644
> --- a/gdb/testsuite/ChangeLog
> +++ b/gdb/testsuite/ChangeLog
> @@ -1,3 +1,8 @@
> +2020-11-09  Alok Kumar Sharma  <AlokKumar.Sharma@amd.com>
> +
> +     * gdb.fortran/pointer-to-pointer.exp: Allow Flang style pointer
> +     printing.
> +
>  2020-11-06  Andrew Burgess  <andrew.burgess@embecosm.com>
>
>       * gdb.base/debug-expr.c: Add extra function to allow for an diff 
> --git a/gdb/testsuite/gdb.fortran/pointer-to-pointer.exp 
> b/gdb/testsuite/gdb.fortran/pointer-to-pointer.exp
> index 4c643c2990..b4bc7c0122 100644
> --- a/gdb/testsuite/gdb.fortran/pointer-to-pointer.exp
> +++ b/gdb/testsuite/gdb.fortran/pointer-to-pointer.exp
> @@ -36,14 +36,36 @@ set real4 [fortran_real4]  gdb_breakpoint 
> [gdb_get_line_number "Break Here"]  gdb_continue_to_breakpoint "Break 
> Here"
>
> -gdb_test "print *buffer" \
> -    " = \\( alpha = \\(1\\.5, 2\\.5, 3\\.5, 4\\.5, 5\\.5\\) \\)"
> +# Flang and gfortran compilers treats pointer type variables differently.
> +# Flang treats it as fortran pointer while gfortran treats it as c 
> +prointer ( # dereferencing with * operator)

This comment seems confusing.  It's not gfortran that is dereferencing the pointer with '*' it's GDB.  The C dereference syntax was used because Fortran doesn't require manual dereferencing, so there is no Fortran dereference syntax.

> +proc buffer_pointer {} {
> +    if {[test_compiler_info {clang-*}]} {
> +        return "buffer"
> +    } else {
> +        return "*buffer"
> +    }
> +}
> +
> +proc buffer_alpha_dim {} {
> +    if {[test_compiler_info {clang-*}]} {
> +        return "5"
> +    } else {
> +        return ":"
> +    }
> +}

This difference makes me more suspicious that this is a case of Flang optimising this code.  I'm pretty sure that if the alpha array was being modelled as a dynamic type within the code then GDB would display ':' here.  Maybe you can confirm, but I think if you check the DWARF you'll see Flag has optimised alpha to a static 5 element array.

Thanks,
Andrew
>
> +set l_buffer_value "\\(1\\.5, 2\\.5, 3\\.5, 4\\.5, 5\\.5\\) \\)"
>  set l_buffer_type [multi_line \
>                      "Type l_buffer" \
> -                    "    $real4 :: alpha\\(:\\)" \
> +                    "    $real4 :: alpha\\([buffer_alpha_dim]\\)" \
>                      "End Type l_buffer" ]
>
> -gdb_test "ptype buffer" "type = PTR TO -> \\( ${l_buffer_type} \\)"
> -gdb_test "ptype *buffer" "type = ${l_buffer_type}"
> +gdb_test "print [buffer_pointer]" \
> +    " = \\( alpha = ${l_buffer_value}"
> +gdb_test "ptype [buffer_pointer]" "type = ${l_buffer_type}"
>  gdb_test "ptype buffer%alpha" "type = $real4 \\(5\\)"
> +
> +if {![test_compiler_info {clang-*}]} {
> +  gdb_test "ptype buffer" "type = PTR TO -> \\( ${l_buffer_type} \\)"
> +}
> --
> 2.17.1



More information about the Gdb-patches mailing list