| Issue |
243690
|
| Summary |
[flang] Wrong result with `-mllvm -inline-all`: a store through an ASSOCIATE name gets the TBAA tag of an inlined procedure
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
After FIR inlining, a store through an ASSOCIATE name whose selector is a dummy argument gets its TBAA tag from the inlined procedure's scope instead of the caller's. The tag then claims that the store does not alias the inlined procedure's own dummy arguments, and LLVM moves a load of the same memory across the store.
`m.f90`:
```fortran
module m
contains
subroutine g(b, k, r)
real :: b(4), r
integer :: k
r = r + b(k)
end subroutine
subroutine s(a, k, v, r, n)
real :: a(4), v, r
integer :: k, n, i
i = 0
10 continue
i = i + 1
call g(a, k, r)
associate (x => a)
x(1) = v + i
end associate
if (i < n) goto 10
end subroutine
end module
```
`main.f90`, compiled without inlining:
```fortran
program main
use m
real :: a(4), r
a = 0.0
r = 0.0
call s(a, 1, 10.0, r, 3)
print *, r ! expected: 23. (0 + 11 + 12)
end program
```
```console
$ flang -O2 -mllvm -inline-all -c m.f90
$ flang -c main.f90
$ flang main.o m.o && ./a.out
0.
$ flang -O2 -c m.f90 && flang main.o m.o && ./a.out
23.
$ flang -O2 -mllvm -inline-all -mllvm -enable-tbaa=false -c m.f90 && flang main.o m.o && ./a.out
23.
```
Each iteration reads `a(1)` through `b(k)` in `g` and then sets `a(1)`, so `r` must be 0 + 11 + 12 = 23. With `-mllvm -inline-all`, the program prints `0.` at -O1, -O2 and -O3, and `23.` at -O0. Writing `a(1) = v + i` directly instead of through the ASSOCIATE name gives `23.` at every level.
Versions: `flang version 24.0.0git (https://github.com/llvm/llvm-project 5f673392019e3a4b05bf7208e5f9345f52baf3f3)` and flang 21.1.5, both on x86-64 Linux, with the same results.
### Analysis
The IR of `s` before any LLVM pass (`flang -O2 -mllvm -inline-all -S -emit-llvm -mllvm -opt-bisect-limit=0 m.f90 -o m.ll 2>/dev/null`), with comments added:
```llvm
%18 = load float, ptr %17, align 4, !tbaa !28 ; b(k), in the inlined g
...
store float %23, ptr %24, align 4, !tbaa !33 ; x(1) = v + i
...
!17 = !{!"any data access", !18, i64 0}
!18 = !{!"any access", !19, i64 0}
!19 = !{!"Flang function root _QMmPs"}
!22 = !{!"dummy arg data", !23, i64 0}
!23 = !{!"any data access", !24, i64 0}
!24 = !{!"any access", !25, i64 0}
!25 = !{!"Flang function root _QMmPs - Scope 1"}
!28 = !{!29, !29, i64 0}
!29 = !{!"dummy arg data/_QMmFgEb", !22, i64 0}
!30 = !{!31, !31, i64 0}
!31 = !{!"dummy arg data/_QMmFsEv", !32, i64 0}
!32 = !{!"dummy arg data", !17, i64 0}
!33 = !{!34, !34, i64 0}
!34 = !{!"dummy arg data/_QMmFsEa", !22, i64 0}
```
The store to `s`'s dummy `a` is tagged `dummy arg data/_QMmFsEa` under `!22`, which is the tree of `Scope 1`, the scope of the inlined `g`. There it is a sibling of `g`'s `dummy arg data/_QMmFgEb`, so TBAA says the store and the load of `b(k)` do not alias. The caller's other dummy arguments, such as `v` (`!31`), stay in the caller's tree under `!32`. With `a(1) = v + i` written directly, the store is tagged `dummy arg data/_QMmFsEa` under `!32`, as expected.
The scope comes from [`AddAliasTags.cpp`](https://github.com/llvm/llvm-project/blob/5f673392019e3a4b05bf7208e5f9345f52baf3f3/flang/lib/Optimizer/Transforms/AddAliasTags.cpp#L677-L688). The source of the store is the dummy argument `a`, but its instantiation point is the `fir.declare` of the ASSOCIATE name `x`: `fir::AliasAnalysis::getSource` keeps the innermost `fir.declare` on the access path as the instantiation point, and walks on until it reaches the declaration of `a`, which has a `dummy_scope`. The declaration of `x` has no `dummy_scope` operand, so the pass falls back to [`getDeclarationScope`](https://github.com/llvm/llvm-project/blob/5f673392019e3a4b05bf7208e5f9345f52baf3f3/flang/lib/Optimizer/Transforms/AddAliasTags.cpp#L362-L373), which returns the nearest dominating `fir.dummy_scope`. After inlining, that is the `fir.dummy_scope` of the inlined `g`, which precedes the ASSOCIATE in the loop body. The tag is then built in `g`'s scope from the name of `s`'s dummy argument.
The scope of a dummy-argument access should come from the scope of the dummy argument's own declaration (here the `fir.declare` of `a`, which carries `s`'s `dummy_scope`), not from whichever `fir.dummy_scope` happens to dominate an intermediate declaration.
Any operation that leaves a `fir.declare` without `dummy_scope` on the access path of a dummy argument can trigger this. PR 243516, under review, does so for array-constructor assignments, which would make this much more common. Related, but with different mechanisms: 242533, 243661 and 240818.
(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