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

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Incorrect result of FORALL when nested functions are used for the mask
        </td>
    </tr>

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

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

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

<pre>
    @jeanPerier #118070 has one more regression.

Source program:

```fortran
program main

  implicit none
  type ty1
    integer :: cmp
  end type ty1
 integer :: arr(4)
  integer :: i

  do i = 1, 4
    arr(i) = i
  end do

  forall (i=1:4, bar(foo(i)) == 3)
    arr(i - 2) = arr(i)
  end forall

  do i = 1, 4
    print *, arr(i)
  end do

contains

  pure function foo(dmy)
    implicit none
    integer, intent(in)::dmy
 type(ty1) :: foo(2)
    foo%cmp = dmy
  end function

  pure function bar(dmy)
    implicit none
    type(ty1), intent(in):: dmy(2)
    integer :: bar
    bar = dmy(1)%cmp
  end function

end program
```

Run (on AArch64):

```console
$ flang test.f90
$ ./a.out
 1
 2
 3
 4
```

The statement in `FORALL` shoud be executed only for `i == 3` and it should output:

```console
 3
 2
 3
 4
```

Curiously, this happens only with `-O0`.

Tested with the latest Flang/LLVM `main` branch (commit 8d550aa0f027eb2cf32850f3905dc1db22317587).

This is detected by [Fujitsu Compiler Test Suite run on Linaro CI](https://linaro.atlassian.net/browse/LLVM-1459). The soruce program above is a simplified version of [Fujitsu/Fortran/0608/Fujitsu-Fortran-0608_0218.test](https://github.com/fujitsu/compiler-test-suite/blob/main/Fortran/0608/0608_0218.f90).

Your #119219 fixed #118922 but did not fixed this issue.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEVc1u2zgQfhr6MrBAjSRbOvjgJhBQIIsu2mKBPS0oaWQxlUiBP0n99gtSsh232SwQK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