<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/72598>72598</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Execution error of `FORALL` with a partially overlapped array assignment
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang:ir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          yus3710-fj
      </td>
    </tr>
</table>

<pre>
    This is an issue from Fujitsu testsuite.

The execution of a program compiled by flang-new terminates with a fatal error.

In the program, there is an array assignment in `FORALL`. The array appears in both of LHS and RHS in the statement and the indices of them are different. In addition, the array is an assumed-size array.
If HLFIR is enabled, the error doesn't occur.

The following are the test program, Flang-new and gfortran execution result.

```fortran
! test.f90
program foralls
 integer(4)::t(10)=(/1,2,3,4,5,6,7,8,9,10/)
 integer(4)::u(10)=(/1,2,3,4,5,6,7,8,9,10/)
  call sub(t,u)
  if (any(t/=(/1,2,0,0,0,0,0,0,0,0/))) print *,'t->',t
  if (any(u/=(/1,2,1,1,1,1,1,1,1,1/))) print *,'u->',u
  print *,'pass'
end

subroutine sub(t,u)
  integer(4)::t(*)
 integer(4)::u(*)
  forall(i=2:9)
    t(i+1)=t(i-1)-(i-1)
 u(i+1)=u(i-1)-i+2
  endforall
end subroutine 
```
```console
$ flang-new -v test.f90 
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git acef83c142619abf270145c91093c76c9d6d5ede)
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
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -o /tmp/test-94bdd9.o -x f95-cpp-input test.f90
 "/opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12/../../../../bin/ld" -pie -EL --hash-style=gnu --eh-frame-hdr -m aarch64linux -dynamic-linker /lib/ld-linux-aarch64.so.1 -o a.out /lib/../lib64/Scrt1.o /lib/../lib64/crti.o /opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12/crtbeginS.o -L/path/to/install/lib/clang/18/lib/aarch64-unknown-linux-gnu -L/opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12 -L/opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/test-94bdd9.o -L/path/to/install/lib -lFortran_main -lFortranRuntime -lFortranDecimal -lm -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12/crtendS.o /lib/../lib64/crtn.o
$ ./a.out
malloc(): corrupted top size
Aborted (core dumped)
$ flang-new -flang-experimental-hlfir test.f90 
$ ./a.out 
 pass
```
```console
$ gfortran -v test.f90 
Driving: gfortran -v test.f90 -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/aarch64-redhat-linux/8/lto-wrapper
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.rockylinux.org/ --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.5.0 20210514 (Red Hat 8.5.0-18) (GCC)
 :
$ ./a.out 
 pass
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8GNtu2zrya5gXgoJE-fqQB1euTwsE6EHSxT4GFDmS2VKklpckPl-_ICX5ksQ5W6BnAccxZ4Zz53CGzDnZaoBbNP-E5tsbFvze2NtDcOWyyEnz46Y24nD7fS8dlg4zjaVzAXBjTYd34Yf0LmAPzrsgPWQo36J8M3x_3wOGF-DBS6OxaTDDvTWtZR3mpuulAoHrA24U0y3R8Iw92E5q5sHhZ-n3mOGGeaYwWGvsBeuvGvs9TOwQreLSwqghs5Yd8GBZB9pjqTFa5Ltv95u7O7TIMxxVG6n6Hph1kaQ2fh_VvPvygJkW-P7LQwRHQc4zD4lVRESI1EJycJHe76HDzAIWsmnAgvYZ_qoxE0JGy0ftRnmjhs6FDgRx8q8RMZr3tcFf7nZf7yMdaFYrENP-5AUsDDiN6NJjw3mwbxzeGKXMs9Rt0ijui8E599Tu6O9oS9sY6y3TZ5Gy4ILyF5zRIh8-I_UIpUXinjXrfIBM8W2MZUq5AYil9tCCRXQ1Q3SNyg0qNx7RVZGn5RbRFaK7AtGKIlqViFYzRKs5otUC0WqJaLVCtFojWsUdu7jpOuPwOxhjzpTCLtSIrjyiVThDyQYjumL6MOB2b-TkH_8lMemDeyu1x4huEK1iTAkqPyMa9fLvSQvvSSs-_rsuLZykhUnaK5KeORcpEha0OE8JF2prgpcarvnpStQT97-L4AXRmE6IriQqtxSVm_UZEmOfMPRTMUQ9LUlckOOvkThcUoYTZYTSiSVoMYqcLMdn5r46Eq-W3GhnFEwnZHZW38jT8biMTE64J7AuHr5ileVZHmO-97530SMxhrtW-n2oM246RHdKPU3_SG_ND-A-a6XHjEOzKnkxo4tizeqGLvNiNufrIl-XfLnga7EQcxBwdMh3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