Skip to content

[Flang] Incorrect execution result when reshape intrinsic function is specified as the argument of function #138810

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 May 7, 2025 · 1 comment
Labels
flang Flang issues not falling into any other category question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@ohno-fj
Copy link

ohno-fj commented May 7, 2025

Version of flang : 21.0.0(842e5915778a820c63cf38b75bec932a6ea8c18b)/AArch64

When reshape intrinsic function is specified as the argument of function (ch and s), the function result is incorrect.
The above program is Polymorphism_22.f90.
Once the result of reshape intrinsic function is assigned to a variable, and that variable is specified as an argument to function (ch and s), the execution result is correct.

  • Before modification
  call s(ch(reshape([2,3,5,6],[2,2])))
  • After modification
  integer::res(2,2)
  res=reshape([2,3,5,6],[2,2])
  call s(ch(res))

The above program is Polymorphism_4.f90.

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

Polymorphism_22.f90:

program main
  interface
     subroutine s(unlim)
       class(*) :: unlim(:,:)
     end subroutine s
     function ch(dum)
       integer,pointer :: ch(:,:)
       integer,target :: dum(:,:)
     end function ch
  end interface
  call s(ch(reshape([2,3,5,6],[2,2])))
end program main

subroutine s(unlim)
  interface
     function fun(dmy)
       integer :: fun
       class(*):: dmy(2,2)
     end function fun
  end interface
  class(*) :: unlim(:,:)
  integer :: stat = 0
  stat = fun(unlim)
  if(stat/=1) then
     print*,101
  else
     print*,'PASS'
  endif
end subroutine s

function fun(dmy)
  integer :: fun
  class(*):: dmy(2,2)
  if(sizeof(dmy) /= (4*4)) print*,'201'
  fun=0
  select type(dmy)
  type is(complex)
     print*,'202'
  type is(integer)
!     if(dmy(2,1)/=3) print*,203
     print*, "dmy = ", dmy
     fun=1
  end select
end function fun

function ch(dum)
  integer,pointer :: ch(:,:)
  integer,target :: dum(:,:)
  ch=>dum
end function ch
$ flang Polymorphism_22.f90; ./a.out
 dmy =  -1431332800 10 -418684532 -878184479
 PASS
$
$ gfortran Polymorphism_22.f90; ./a.out
 dmy =            2           3           5           6
 PASS
$
$ ifx Polymorphism_22.f90; ./a.out
 dmy =            2           3           5           6
 PASS
$

Polymorphism_4.f90:

program main
  interface
     subroutine s(unlim)
       class(*) :: unlim(:,:)
     end subroutine s
     function ch(dum)
       integer,pointer :: ch(:,:)
       integer,target :: dum(:,:)
     end function ch
  end interface
  integer::res(2,2)
  res=reshape([2,3,5,6],[2,2])
  call s(ch(res))
end program main

subroutine s(unlim)
  interface
     function fun(dmy)
       integer :: fun
       class(*):: dmy(2,2)
     end function fun
  end interface
  class(*) :: unlim(:,:)
  integer :: stat = 0
  stat = fun(unlim)
  if(stat/=1) then
     print*,101
  else
     print*,'PASS'
  endif
end subroutine s

function fun(dmy)
  integer :: fun
  class(*):: dmy(2,2)
  if(sizeof(dmy) /= (4*4)) print*,'201'
  fun=0
  select type(dmy)
  type is(complex)
     print*,'202'
  type is(integer)
!     if(dmy(2,1)/=3) print*,203
     print*, "dmy = ", dmy
     fun=1
  end select
end function fun

function ch(dum)
  integer,pointer :: ch(:,:)
  integer,target :: dum(:,:)
  ch=>dum
end function ch
$ flang Polymorphism_4.f90; ./a.out
 dmy =  2 3 5 6
 PASS
$
$ gfortran Polymorphism_4.f90; ./a.out
 dmy =            2           3           5           6
 PASS
$
$ ifx Polymorphism_4.f90; ./a.out
 dmy =            2           3           5           6
 PASS
$
@ohno-fj ohno-fj added the flang Flang issues not falling into any other category label May 7, 2025
@klausler
Copy link
Contributor

klausler commented May 7, 2025

Fortran 2023 15.5.2.5 paragraph 11: "If the dummy argument has the TARGET attribute and the effective argument does not have the TARGET attribute or is an array section with a vector subscript, any pointers associated with the dummy argument
become undefined when execution of the procedure completes."

@klausler klausler closed this as completed May 7, 2025
@EugeneZelenko EugeneZelenko added the question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! label May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang Flang issues not falling into any other category question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Development

No branches or pull requests

3 participants