| Issue |
235747
|
| Summary |
[flang] Wrong result at -O1 and above: SAVE local re-read after a call that re-enters its procedure
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
A procedure that reads one of its own SAVE variables after a call that can re-enter the procedure gets a stale value at -O1 and above. FIR alias analysis decides that the call cannot modify the SAVE variable, and FIR LICM hoists the load out of the loop.
Direct recursion:
```fortran
recursive subroutine r(mode)
integer, intent(in) :: mode
integer, save :: cnt = 0
integer :: k, s
if (mode == 1) then
cnt = cnt + 1
return
end if
s = 0
do k = 1, 3
call r(1)
s = s + cnt
end do
print *, s
end subroutine
program main
call r(0)
end program
```
```console
$ flang -O0 t.f90 -o t && ./t
6
$ flang -O2 t.f90 -o t && ./t
0
```
Indirect recursion through an external procedure (`r` -> `ext` -> `r`) fails the same way:
```fortran
recursive subroutine r(mode)
integer, intent(in) :: mode
integer, save :: cnt = 0
integer :: k, s
interface
subroutine ext()
end subroutine
end interface
if (mode == 1) then
cnt = cnt + 1
return
end if
s = 0
do k = 1, 3
call ext()
s = s + cnt
end do
print *, s
end subroutine
subroutine ext()
interface
recursive subroutine r(mode)
integer, intent(in) :: mode
end subroutine
end interface
call r(1)
end subroutine
program main
interface
recursive subroutine r(mode)
integer, intent(in) :: mode
end subroutine
end interface
call r(0)
end program
```
Both programs should print 6. They print 6 at -O0, and 0 at -O1, -O2 and -O3. Adding `-mmlir -disable-fir-licm` restores 6. Reproduced with flang built from main at 2b1813fb91f4, and still at bcd59662bd52.
**Cause.** In `AliasAnalysis::getCallModRef` (`flang/lib/Optimizer/Analysis/AliasAnalysis.cpp`), a call to a Fortran user procedure is `NoModRef` for a SAVE local (a global that `isSavedLocal` accepts) unless the variable is passed as an argument:
```cpp
// At that stage, it has been ruled out that local (including the saved ones)
// and dummy cannot be indirectly accessed in the call.
```
That premise holds for a non-SAVE local, because each invocation has its own instance. It does not hold for a SAVE local: all invocations share one instance, so a callee that re-enters the owning procedure can define it. A procedure may be invoked recursively unless it is NON_RECURSIVE; per F2023 15.6.2.1 p3, "The RECURSIVE prefix-spec is advisory only." The callee may also be an external procedure whose body is not visible, as in the second example. The rule dates from PR 117164, and FIR LICM (PR 173438) turns it into a wrong result here.
PR 233433 fixes a related case with the same root: a host SAVE variable that a sibling internal procedure modifies. It does not fix this one. No internal procedures are involved here, and both programs still print 0 with that PR applied.
**Possible fix.** Trust `NoModRef` for a SAVE local only when the owning `func.func` has `fir.proc_attrs<non_recursive>`. Lowering already sets that attribute for NON_RECURSIVE procedures, and under `-fno-automatic` for procedures that are not explicitly RECURSIVE. The cost is that procedures are recursive by default, so with default options this disables the optimization for SAVE locals in almost every procedure. Keeping `NoModRef` more often would need proof that the callee cannot reach the owning procedure, which needs call-graph information that the alias analysis does not have today.
(This issue was filed with the help of AI)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs