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

    <tr>
        <th>Summary</th>
        <td>
            [Flang] allocated status of a saved ALLOCATABLE variable is lost when subprograms are executed starting from an ENTRY statement under -O2 optimization
        </td>
    </tr>

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

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

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

<pre>
    ```
Version of flang : 22.0.0(9e5f9ff82f2f060aac73a965ab37fdbb6b53cfe0)/AArch64
```

In the attached test program (`short.f90`), a saved `ALLOCATABLE` variable `x` is declared in a subprogram `sub`, and accessed from its latter part separated by the entry point `ent`. The `allocated(x)` status is checked inside the latter part.  

According to the Fortran standard 2023, section 8.5.16 SAVE attribute, a local variable of a subprogram with the `SAVE` attribute retains its association status, allocation status, definition status, and value after execution of a `RETURN` or `END` statement—except when it is a pointer whose target becomes undefined. Furthermore, if the variable belongs to a subprogram, it is shared across all entry points of that subprogram.  

Therefore, using `x` when subprograms are executed starting from an ENTRY statement the latter part is standard-compliant and should behave consistently regardless of optimization level.  

However, when compiled with Flang using `-O2` or higher, `allocated(x)` incorrectly returns `.false.` inside the latter part, resulting in a premature `STOP`. This behavior does not occur with `-O0` or `-O1`, nor with other compilers such as `gfortran` and `ifx`.

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

short.f90:
```fortran
call sub
call ent
end

subroutine sub
  integer, allocatable, dimension(:), save :: x
  allocate(x(10))
  return

entry ent
  if (.not. allocated(x)) then
    stop
  end if
  if ( x(5) .ne. 4 ) then
     print *,'pass'
  end if
end subroutine sub
```

```
$ flang short.f90 -O0
$ ./a.out
 pass
```
```
$ flang short.f90 -O1
$ ./a.out
 pass
```
```
$ flang short.f90 -O2
$ ./a.out
Fortran STOP
```

```
$ gfortran short.f90 -O0
$ ./a.out
 pass
```
```
$ gfortran short.f90 -O1
$ ./a.out
 pass
```
```
$ gfortran short.f90 -O2
$ ./a.out
 pass
```

```
$ ifx short.f90 -O0
$ ./a.out
 pass
```
```
$ ifx short.f90 -O1
$ ./a.out
 pass
```
```
$ ifx short.f90 -O2
$ ./a.out
 pass
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVl1v27gS_TX0yyACTVuy_OAHNY3vvUDRXPTmFthHihxZ7NKkQVJOsr9-MZT8ETeLLroFAkSyZs7MOfNByhjNziFuWPmBlR9ncki9DxvfO3_XfZu1Xr9uWMWnP958xRCNd-A76Kx0O2CLBoQoeMGZqNdYduuuq0UnOl5xKdVqIddVKdvFqtNtW7XlQnXImVgzsW2aoPpqyXhzHYHx5j8OUo8gU5KqRw0JY4JD8Lsg98BEzSoeex9S0a2zE6Hdg4Qoj6iBVbz59OnxvnlqPnx6YBWHowxGthbp0wv9YCJoVFYG1GAceQ7tGb_icWgz7D1Ip0EqhTGihi74PZgUwcqUMMBBhgQRDzLIhBra15w1uhRe4eCNS4SFLrGKF_DU5_DSWq_InIn6hRKvOMQk0xApKdWj-j3nFI3GDHcVqwAY9WmU8kEbt4Pks9HWhxSkIySnZdAguFhQ_hFVonLVRVnMK_hf8_WBZA2mHRKOolE-9iKR797K8WxSn2OwipM7JXxGgIBJGhezKjJGr4zM8UZKOcBI-O2PGjvjzK2l03CUdkCQHVHGF1RDmppNUvwvD0____KZMvCB3h8-fzzph3sS-kGwmrP1El8UHhI89-jAJFJWjhXBAM-9jwhJhh0maFH5PUYYXE4JdQHbIaQew96HLJDpMvuzPi1a73aRlL_WKZvmSLHPbSVV8DES_euOiMQl9TJduZ7L-tRjwG6KO0Sq76lhM5OLSwQZcBIINQkQEpnnDpUOHj4_ffntosttI-U0p1a5U35_sEa6lAsQez9YDS328oigvIsmJnTJvkLAnQzaYswk_CGZvfljrKzFI9ozj3_7ZzxiIBY5b4pgLOqxl7Z5bZzp3T2KqaC92fWj11_MiX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