| Issue |
241963
|
| Summary |
[flang] Accesses to an EQUIVALENCE member at a misaligned block offset claim the member's natural alignment
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
When a strictly aligned member of an EQUIVALENCE storage sequence sits at a byte offset that is not a multiple of its alignment, flang leaves it misaligned (as ifx 2026.1, nagfor 7.2 and nvfortran 26.9 do; gfortran instead shifts the block start), but every access to it still claims the natural alignment. LangRef makes an overstated access alignment undefined behavior. Default INTEGER and DOUBLE PRECISION may share an equivalence set, so this program is conforming:
```fortran
subroutine s
integer :: i(3)
double precision :: d
equivalence (i(2), d)
d = 1.0d0
print *, mod(loc(d), 8)
end subroutine
subroutine t
integer, save :: i(3)
double precision, save :: d
equivalence (i(2), d)
d = 1.0d0
print *, mod(loc(d), 8)
end subroutine
program main
call s
call t
end program
```
`flang -O2 x.f90 && ./a.out` (same at `-O0`) on x86_64 prints `4 4`: since 240895 the local block is `alloca [12 x i8], align 8`, and the SAVE block is a global with `align 64`, so `d` is at 4 mod 8 in both. Yet the store is emitted as
```llvm
store double 1.000000e+00, ptr getelementptr inbounds nuw (i8, ptr @_QFtEd, i64 4), align 8
```
The member's offset in the block is known at compile time, so the access could claim only the alignment it actually has (4 here). nvfortran also warns about this layout (`Equivalence forces d to be unaligned`), and ifx does for the case where two members cannot both be aligned (`equivalence (i(1), d1), (i(2), d2)`); flang is silent.
With a nonstandard variant (INTEGER and REAL(16), the REAL(16) member at offset 4) the misaligned 16-byte store crashes with SIGSEGV at `-O0` on x86_64.
(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