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

    <tr>
        <th>Summary</th>
        <td>
            [flang] Behavior of gfortran test depends on optimization level: pr117797.f90
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          tarunprabhu
      </td>
    </tr>
</table>

<pre>
    Steps to reproduce:

Assuming that the [LLVM test suite](https://github.com/llvm/llvm-test-suite) is checked out in ./llvm-test-suite, compile the test at various optimization levels
```
$ flang -o a0.out -O0 ./llvm-test-suite/Fortran/gfortran/regression/pr117797.f90
$ flang -o a1.out -O1 ./llvm-test-suite/Fortran/gfortran/regression/pr117797.f90
$ flang -o a2.out -O2 ./llvm-test-suite/Fortran/gfortran/regression/pr117797.f90
$ flang -o a3.out -O3 ./llvm-test-suite/Fortran/gfortran/regression/pr117797.f90
$ flang -o alto.out -flto=auto ./llvm-test-suite/Fortran/gfortran/regression/pr117797.f90
```
When these are run, the executables created with `-O0` and `-flto=auto` encounter a `STOP` statement in the code, but those compiled with optimizations do not. 
```
$ ./a0.out 
Fortran STOP: code 2
$ ./a1.out
$ ./a2.out
$ ./a3.out
$ ./alto.out
Fortran STOP: code 2
```

Expected behavior: The stop message should never be printed.

What follows is a slightly reduced code that reproduces this behavior. If the `class default` branch of the `select` statement is removed, the `STOP` message is printed at all optimization levels.

```
module foo

  type, public :: any_matrix
 class(*), allocatable :: value(:,:)
  end type

contains

  function bar(this) result(uptr)
    class(any_matrix), target, intent(in) :: this
 class(*), pointer :: uptr(:,:)
    uptr => this%value ! Seg. fault in trans-array.cc(gfc_get_array_span) here
  end function

  function build(this) result (res)
    class(*) :: this(:,:)
    type(any_matrix) :: res
    res%value = this
  end function

  function evaluate (this) result (res)
    class(*) :: this(:,:)
    character(len = 2, kind = 1), allocatable :: res(:)
      select type (ans => this)
        type is (character(*))
          res = reshape (ans, [4])
        type is (integer)
          allocate (res (8))
          write (res, '(i2)') ans
 class default
          res = ['no','t ','OK','!!']
      end select
  end

end module

  use foo
  class(*), allocatable :: up (:, :)
  integer :: i(2,2) = reshape ([1,2,3,4], [2,2])

  up = bar (build (i))
  if (any (evaluate (up) /= [' 1',' 2',' 3',' 4'])) stop 2

 deallocate (up)
end
```

The cases in the `SELECT` statement seem to have an effect on the behavior. Removing only the `character(*)` case results in correct behavior at all optimization levels. Removing only the `class default` cases causes the error to occur only at `-O0` but not at any other optimization level.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0V1tv27gS_jX0yyCGRFmR_eAH52Lg4PQgB9ti-xjQ0sjiliYFXtxmf_1iSElRYqfYhxYwIoaa6zcfhyPhnDxqxC0r71j5sBDBd8ZuvbBB91YcurA4mOZl-9lj78AbsNhb04QaWbFjGf12zoWT1EfwnfDgOwRW3n369Of_wKPz4IL0yMoHxted970jPb5nfH-UvguHZW1OjO-VOo-PG1K7SWp8A9JB3WH9DRswwYPUsLwmeA-1OfVSYYwgehYezsJKExyY3suT_Ft4aTQoPKNyFPxtNvyyHeMraJXQR7gxILIl-bp5yq472--N9VZoyqKdlhaPFp2Thv7pbZ5X1aZatptL8_lgPv895vlgnv8e88Vgvvg95pU3yUGrvGHFgwje_DJPs4p_7VATWRyCsAg2aGIRsQd_YB28OCh0UFsUHhv4Ln0H7Da7eSJ1ELqJ_81ipG3UtQnaowVBrz9_efo_bTsvPJ5QR_qSh9o0kbOHQCfGOBzZOzia89VBY0Abv4QrlCVcBraybDfgAdFvsYtugM9FI_PmG_z9RvF-Y6jHT83Po8p2jz96rAm0A3biLI0l4S8dgvOmhxM6J44IrjNBNaDxjBYOCL2V2mOzTDa-UjNpjVLmu6MeIMApeey8egGL1H-a5D42nakpOfCddJPfJfynTR3pNquVcA4abEVQnopysELXHZhJwqHC2r-rlwOLJ3PGZiTHrKxjJtKNwVPPEU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