llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.01k stars 11.96k forks source link

[Flang] Incorrect execution result of subarray operation passed to subroutine #71368

Closed ohno-fj closed 11 months ago

ohno-fj commented 1 year ago
Version of flang-new : 18.0.0(1c876ff5155c4feeb2b2885eb3e6abda17c4b7f4)

The result of subarray operation passed to subroutine is incorrect.
When I specify -flang-experimental-hlfir at the compilation, I can avoid the execution error.

The following are the test program, Flang-new, Gfortran and ifort compilation result.

target_tp01_3.f90:

module m
contains
  subroutine sub(a_arg,b_arg,c_arg)
    integer, dimension(:),target:: a_arg
    integer, dimension(:),target:: b_arg,c_arg
    a_arg = b_arg + c_arg
  end subroutine sub
end module m

program main
  use m
  integer,dimension(50)::x
  integer,pointer      :: a_pointer(:)
  allocate (a_pointer(4))
  a_pointer = (/100000,2,300000,40/)
  call sub (a_pointer(2:3),a_pointer(1:2),a_pointer(3:4))
  write(6,*) "a_pointer(3) = ", a_pointer(3)
  x(a_pointer(3))=1
  print *,'pass'
end program main
$ flang-new target_tp01_3.f90; ./a.out
 a_pointer(3) =  400040
Segmentation fault (core dumped)
$
$ flang-new target_tp01_3.f90 -flang-experimental-hlfir; ./a.out
 a_pointer(3) =  42
 pass
$
$ gfortran target_tp01_3.f90; ./a.out
 a_pointer(3) =           42
 pass
$
$ ifort target_tp01_3.f90; ./a.out
 a_pointer(3) =           42
 pass
$
llvmbot commented 1 year ago

@llvm/issue-subscribers-flang-frontend

Author: None (ohno-fj)

``` Version of flang-new : 18.0.0(1c876ff5155c4feeb2b2885eb3e6abda17c4b7f4) ``` The result of subarray operation passed to `subroutine` is incorrect. When I specify `-flang-experimental-hlfir` at the compilation, I can avoid the execution error. The following are the test program, Flang-new, Gfortran and ifort compilation result. target_tp01_3.f90: ```fortran module m contains subroutine sub(a_arg,b_arg,c_arg) integer, dimension(:),target:: a_arg integer, dimension(:),target:: b_arg,c_arg a_arg = b_arg + c_arg end subroutine sub end module m program main use m integer,dimension(50)::x integer,pointer :: a_pointer(:) allocate (a_pointer(4)) a_pointer = (/100000,2,300000,40/) call sub (a_pointer(2:3),a_pointer(1:2),a_pointer(3:4)) write(6,*) "a_pointer(3) = ", a_pointer(3) x(a_pointer(3))=1 print *,'pass' end program main ``` ``` $ flang-new target_tp01_3.f90; ./a.out a_pointer(3) = 400040 Segmentation fault (core dumped) $ ``` ``` $ flang-new target_tp01_3.f90 -flang-experimental-hlfir; ./a.out a_pointer(3) = 42 pass $ ``` ``` $ gfortran target_tp01_3.f90; ./a.out a_pointer(3) = 42 pass $ ``` ``` $ ifort target_tp01_3.f90; ./a.out a_pointer(3) = 42 pass $ ```
yus3710-fj commented 11 months ago

This issue isn't reproduced now because HLFIR is enabled by default.

$ flang-new -v target_tp01_3.f90
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git ba89749cd24950c9c51f4cc64277ce0421d86d20)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12
Selected GCC installation: /opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12
 :
$ ./a.out 
 a_pointer(3) =  42
 pass