| Issue |
242533
|
| Summary |
[flang] Wrong result at -O2: TBAA facts about dummy arguments are applied across loop iterations after inlining
|
| Labels |
miscompilation,
flang:fir-hlfir
|
| Assignees |
|
| Reporter |
vzakhari
|
The program below prints `FAIL` when compiled with `flang -O2` or `-O3`:
```fortran
module recurrence
contains
subroutine step(a, b)
real :: a, b
a = b + 1.0
end subroutine step
end module recurrence
subroutine run(x, n)
use recurrence
integer :: n
real :: x(n)
integer :: i
do i = 1, n - 1
call step(x(i + 1), x(i)) ! iteration i writes what iteration i + 1 reads
end do
end subroutine run
program main
integer, parameter :: n = 1000
real :: x(n)
x = 0.0
call run(x, n)
if (x(n) == real(n - 1)) then
print *, 'PASS', x(n)
else
print *, 'FAIL', x(n), ' expected', real(n - 1)
end if
end program main
```
Each call of `step` is valid, because its two arguments are different elements of `x`. Consecutive calls overlap, though: iteration `i` writes `x(i + 1)`, which iteration `i + 1` reads as `b`. So `x(n)` must be 999.
```
$ flang -O2 recurrence.f90 -o recurrence && ./recurrence
FAIL 8. expected 999.
$ flang -O3 recurrence.f90 -o recurrence && ./recurrence
FAIL 8. expected 999.
$ flang -O2 -mllvm -enable-tbaa=false recurrence.f90 -o recurrence && ./recurrence
PASS 999.
$ flang -O2 -fno-vectorize recurrence.f90 -o recurrence && ./recurrence
PASS 999.
$ flang -O1 recurrence.f90 -o recurrence && ./recurrence
PASS 999.
$ flang -O2 -Rpass=loop-vectorize recurrence.f90 -o recurrence
recurrence.f90:14:3: remark: vectorized loop (vectorization width: 4, interleaved count: 2) [-Rpass=loop-vectorize]
recurrence.f90:14:3: remark: vectorized loop (vectorization width: 4, interleaved count: 2) [-Rpass=loop-vectorize]
```
The two remarks are for the loop in `run` and for its copy inlined into the main program.
Version: `flang version 24.0.0git (https://github.com/llvm/llvm-project 02ca8754da9a4c0083b4b8068481be6a9b72932b)`, on x86-64 Linux.
### Analysis
Flang gives the two dummy arguments of `step` sibling TBAA types under the function's TBAA root. This is the IR before any LLVM pass, from `flang -O2 -S -emit-llvm -mllvm -opt-bisect-limit=0 recurrence.f90 -o recurrence.ll 2>/dev/null` (the redirect hides a note for every skipped pass):
```llvm
define void @_QMrecurrencePstep(ptr noalias captures(none) %0, ptr noalias captures(none) %1) {
%3 = load float, ptr %1, align 4, !tbaa !4
%4 = fadd contract float %3, 1.000000e+00
store float %4, ptr %0, align 4, !tbaa !10
ret void
}
!4 = !{!5, !5, i64 0}
!5 = !{!"dummy arg data/_QMrecurrenceFstepEb", !6, i64 0}
!6 = !{!"dummy arg data", !7, i64 0}
!7 = !{!"any data access", !8, i64 0}
!8 = !{!"any access", !9, i64 0}
!9 = !{!"Flang function root _QMrecurrencePstep"}
!10 = !{!11, !11, i64 0}
!11 = !{!"dummy arg data/_QMrecurrenceFstepEa", !6, i64 0}
```
These tags state that `a` and `b` never overlap. That is true during one invocation of `step`, but a TBAA fact has no notion of an invocation: it applies to any two accesses that carry the tags. After `step` is inlined into the loop of `run`, the loop vectorizer asks whether the store to `a` and the load of `b` may alias. TBAA answers NoAlias, so their dependence across iterations is never checked, and the loop is vectorized without a runtime check.
The `noalias` attributes of the two arguments express the same per-invocation fact, and the inliner turns them into scoped `noalias` metadata. That form is handled correctly: its scopes are declared by `llvm.experimental.noalias.scope.decl` inside the loop, so LoopAccessAnalysis does not apply them across iterations. With `-mllvm -enable-tbaa=false` that metadata is still present, and the result is correct.
This is related to #240818, where two inlined copies of one procedure share the procedure's TBAA tree. Here every iteration comes from the same inline site, so a separate TBAA tree for each inline site would not help.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs