Skip to content

[Flang] Incorrect execution result when a pointer variable of the same name is defined for each cray pointer in two dependent modules, and a different array address is defined for each pointer variable #135579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ohno-fj opened this issue Apr 14, 2025 · 2 comments · Fixed by #136776
Assignees

Comments

@ohno-fj
Copy link

ohno-fj commented Apr 14, 2025

Version of flang : 21.0.0(9fe6f6a0d430b872750354c20f3e4a651bd1f135)/AArch64

In the attached program, a pointer variable of the same name is defined for each cray pointer in two dependent modules (1), and a different array address is defined for each pointer variable (2).
The execution result of such a program is incorrect.
The value of v1 in the main program is expected to be 1.0.

The following are the test program, Flang, Gfortran and ifx compilation/execution result.

sngg752d_2.f90:

module m1
  implicit none
  real::v1
  pointer (ptr,v1)  ! (1)
  real::x(2)
contains
  subroutine s
    implicit none
    x=[1,2]
    ptr=loc(x)  ! (2)
  end subroutine s
end module m1

module m2
  use m1
  implicit none
  private ptr
contains
  subroutine ss
    real::v2,y(2)
    pointer (ptr,v2)  ! (1)
    dimension v2(2)
    y=[11,12]
    ptr=loc(y)  ! (2)  The address assigned to ptr changes from the address of x to the address of y.
    write(6,*) "ss : v1 = ", v1, " v2 = ", v2
  end subroutine ss
end module m2

program main
  use m2
  implicit none
  call s
  call ss
  write(6,*) "main : v1 = ", v1
  print *,'pass'
end program main
$ flang sngg752d_2.f90; ./a.out
 ss : v1 =  11.  v2 =  11. 12.
 main : v1 =  -118116.53
 pass
$
$ gfortran -fcray-pointer sngg752d_2.f90; ./a.out
 ss : v1 =    1.00000000      v2 =    11.0000000       12.0000000
 main : v1 =    1.00000000
 pass
$
$ ifx sngg752d_2.f90; ./a.out
 ss : v1 =    1.000000      v2 =    11.00000       12.00000
 main : v1 =    1.000000
 pass
$
@ohno-fj ohno-fj added the flang Flang issues not falling into any other category label Apr 14, 2025
@klausler klausler assigned klausler and akuhlens and unassigned klausler Apr 14, 2025
@klausler
Copy link
Contributor

A Cray pointer declaration (pointer(ptr, val)) should create ptr in the current scope rather than use host association. Should be a small change in name resolution.

@llvmbot
Copy link
Member

llvmbot commented May 2, 2025

@llvm/issue-subscribers-flang-frontend

Author: None (ohno-fj)

``` Version of flang : 21.0.0(9fe6f6a)/AArch64 ```

In the attached program, a pointer variable of the same name is defined for each cray pointer in two dependent modules (1), and a different array address is defined for each pointer variable (2).
The execution result of such a program is incorrect.
The value of v1 in the main program is expected to be 1.0.

The following are the test program, Flang, Gfortran and ifx compilation/execution result.

sngg752d_2.f90:

module m1
  implicit none
  real::v1
  pointer (ptr,v1)  ! (1)
  real::x(2)
contains
  subroutine s
    implicit none
    x=[1,2]
    ptr=loc(x)  ! (2)
  end subroutine s
end module m1

module m2
  use m1
  implicit none
  private ptr
contains
  subroutine ss
    real::v2,y(2)
    pointer (ptr,v2)  ! (1)
    dimension v2(2)
    y=[11,12]
    ptr=loc(y)  ! (2)  The address assigned to ptr changes from the address of x to the address of y.
    write(6,*) "ss : v1 = ", v1, " v2 = ", v2
  end subroutine ss
end module m2

program main
  use m2
  implicit none
  call s
  call ss
  write(6,*) "main : v1 = ", v1
  print *,'pass'
end program main
$ flang sngg752d_2.f90; ./a.out
 ss : v1 =  11.  v2 =  11. 12.
 main : v1 =  -118116.53
 pass
$
$ gfortran -fcray-pointer sngg752d_2.f90; ./a.out
 ss : v1 =    1.00000000      v2 =    11.0000000       12.0000000
 main : v1 =    1.00000000
 pass
$
$ ifx sngg752d_2.f90; ./a.out
 ss : v1 =    1.000000      v2 =    11.00000       12.00000
 main : v1 =    1.000000
 pass
$

IanWood1 pushed a commit to IanWood1/llvm-project that referenced this issue May 6, 2025
…itialization (llvm#136776)

This PR:
- makes Cray pointer declarations shadow previous bindings instead of
modifying them,
- errors when the pointee of a cray pointee has the SAVE attribute, and
- adds a missing newline after dumping the list of cray pointers in a
scope.

Closes llvm#135579
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this issue May 6, 2025
…itialization (llvm#136776)

This PR:
- makes Cray pointer declarations shadow previous bindings instead of
modifying them,
- errors when the pointee of a cray pointee has the SAVE attribute, and
- adds a missing newline after dumping the list of cray pointers in a
scope.

Closes llvm#135579
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this issue May 6, 2025
…itialization (llvm#136776)

This PR:
- makes Cray pointer declarations shadow previous bindings instead of
modifying them,
- errors when the pointee of a cray pointee has the SAVE attribute, and
- adds a missing newline after dumping the list of cray pointers in a
scope.

Closes llvm#135579
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this issue May 7, 2025
…itialization (llvm#136776)

This PR:
- makes Cray pointer declarations shadow previous bindings instead of
modifying them,
- errors when the pointee of a cray pointee has the SAVE attribute, and
- adds a missing newline after dumping the list of cray pointers in a
scope.

Closes llvm#135579
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
5 participants