https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128966
Bug ID: 128966
Summary: [ia64] wrong code at -O2/-O3: late-combine rewrites an
RFmode spill reload as XFmode, mismatching the
stf.spill that wrote the slot
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: frank.scheiner at web dot de
CC: rdsandiford at googlemail dot com
Target Milestone: ---
Target: ia64-*-*
The following problem was discovered when porting musl to ia64 (AI-assisted,
[1]), during verification when working on failing math tests from libc-test
([2]). Also note the description is created by the very same AI (though later
LLM version than most of the port was done with) with only small changes by me
to fit my local environment exactly. Still everything described was actually
derived from and confirmed during debugging.
[1]: https://github.com/johnny-mnemonic/epic-musl
[2]: http://nsz.repo.hu/git/?p=libc-test
@Richard (Sandiford): I put you in CC directly as you authored the late combine
pass. Maybe you can spot the issue right away.
****
## 1. Summary
On ia64, the late-combine pass folds an **RFmode** memory load together with a
subsequent RF→XF register copy into a single **XFmode** memory load. RFmode and
XFmode are tieable in registers on ia64 but have **different memory
encodings**: RFmode accesses emit `stf.spill`/`ldf.fill` and preserve the
82-bit register format; XFmode accesses emit `stfe`/`ldfe` and use the 80-bit
memory format.
When the slot was written by `stf.spill`, the rewritten XFmode load
misinterprets the exponent field and corrupts the value. All downstream FP
arithmetic then operates on garbage.
## 2. Affected versions
| compiler | platform | result |
|----------|----------|--------|
| GCC 15.2.0 (native) | qemu-system-ia64 | **affected** |
| GCC 15.3.0 (native) | HPE Integrity rx2800 i2, real Itanium 2 | **affected**
|
| GCC 17.0.0 trunk (unpatched `ia64-linux` cross) | x86_64 host, codegen
inspection | **affected** |
| GCC 16.2.0 + T2 patches | rx2800 i2 | clean |
The 16.2.0 build in EPIC Slack currently carries a distribution patch from T2
([3]) that disables `-flate-combine-instructions` on ia64; it appears clean for
that reason rather than because of an upstream fix. Distributions carrying such
a patch may have masked this upstream. **Unpatched trunk still miscompiles.**
Note: trunk was at r17-6558-g78b9e69e110695 .
[3]:
https://github.com/epic-slack/current/blob/main/source/d/gcc/patches/0003-no-late-combine-for-ia64.patch
Wrong results are **bit-identical between emulation and real hardware**, so
this is not an emulation artifact.
## 3. Observed behaviour
Building musl libc's `src/math/powl.c` at `-O2`:
```
powl(-1.0L, 2.0L) = -0x1p+0 expected 0x1p+0
powl(-0.0L, 2.0L) = -0x0p+0 expected 0x0p+0
```
The affected slot holds `w = floorl(y)`, consumed by `fabsl(w)` in the
odd-integer test:
```c
w = floorl(y);
if (w == y) iyflg = 1;
if (iyflg) {
ya = fabsl(y);
ya = floorl(0.5 * ya);
yb = 0.5 * fabsl(w); /* w reloaded here - corrupted */
if (ya != yb) yoddint = 1;
}
```
With `yb` corrupted, `ya != yb` holds for **every** integer `y`, so all integer
exponents are treated as odd and the result is negated. The same defect
corrupts `logl`, `lgammal` and `lgammal_r`.
## 4. Root cause - RTL at the pass boundary
### GCC 15.2.0, `src/math/powl.c:251:14`
`postreload`:
```
(insn 1735 1768 308 37 (set (reg:RF 134 f6 [orig:980 w ] [980])
(mem/c:RF (reg:DI 14 r14 [1192]) [3 %sfp+48 S16 A128]))
"src/math/powl.c":251:14 130 {*movrf_internal} (nil))
(insn 308 1735 309 37 (set (reg:XF 136 f8)
(reg:XF 134 f6 [orig:980 w ] [980]))
"src/math/powl.c":251:14 129 {movxf_internal} (nil))
```
`late_combine2`:
```
(insn 308 1768 309 37 (set (reg:XF 136 f8)
(mem/c:XF (reg:DI 14 r14 [1192]) [3 %sfp+48 S16 A128]))
"src/math/powl.c":251:14 129 {movxf_internal} (nil))
```
`insn 1735` deleted; the MEM's mode changed **RF → XF**. Same alias slot `[3
%sfp+48]`.
### GCC 17.0.0 trunk - identical transformation
`postreload`:
```
(insn 1443 1475 251 35 (set (reg:RF 135 f7 [orig:962 w ] [962])
(mem/c:RF (reg:DI 14 r14 [1141]) [3 %sfp+48 S16 A128]))
"src/math/powl.c":251:14 130 {*movrf_internal})
(insn 251 1443 252 35 (set (reg:XF 136 f8)
(reg:XF 135 f7 [orig:962 w ] [962]))
"src/math/powl.c":251:14 129 {movxf_internal})
```
`late_combine2`:
```
(insn 251 1475 252 35 (set (reg:XF 136 f8)
(mem/c:XF (reg:DI 14 r14 [1141]) [3 %sfp+48 S16 A128]))
"src/math/powl.c":251:14 129 {movxf_internal})
```
The RF→XF **register** copy is a no-op in hardware; the RF→XF **memory access**
change is not.
## 5. Generated assembly
GCC 15.2.0, `powl.o`:
```
6f0: adds r14=64,r12 ; r14 = sp+64
6fc: mov f8=f3
700: stf.spill [r14]=f6 ; WRITE sp+64 (w), register format
70c: br.call fabsl
716: adds r14=48,r12
736: ldfe f7=[r36] ; 0.5 constant
740: stf.spill [r14]=f7 ; WRITE sp+48
746: fmpy.s0 f8=f8,f7
74c: br.call floorl
750: adds r14=64,r12
756: mov f4=f8
760: ldfe f8=[r14] ; READ sp+64 as memory format <-- WRONG
76c: br.call fabsl
780: ldf.fill f7=[r14] ; READ sp+48 - correctly paired
```
GCC 17 trunk shows the same shape at `15c0` (write `sp+64`) / `1620` (read
`sp+64` with `ldfe`), with `sp+48` correctly paired at `1600`/`1640`.
Per the IA-64 SDM (Vol. 3, `stf.spill` / `ldf.fill`), a slot written by
`stf.spill` must be read by `ldf.fill`.
## 6. Runtime evidence (gdb, breakpoint before the `ldfe`)
```
(gdb) p/x $r14
$1 = 0x60000fffffffb420 ; = sp+64
(gdb) x/2gx $r14
0x60000fffffffb420: 0x8000000000000000 0x0000000000010000
(gdb) p $f8
$2 = 1
(gdb) stepi ; execute ldfe f8=[r14]
(gdb) p $f8
$3 = 9.98238144410385893132e-19729
```
The stored image is `w = 2.0` in register format: significand
`0x8000000000000000`, exponent `0x10000` (bias 65535). The same value in memory
format has exponent `0x4000` (bias 16383). `ldfe` reads bytes 8–9 as `0x0000` -
a memory-format denormal - and yields a wild value instead of `2.0`.
## 7. Optimization bisect
```
-O0 correct
-O1 correct
-Os correct
-O2 WRONG
-O3 WRONG
-O2 -fno-late-combine-instructions correct
```
`-flate-combine-instructions` is enabled at **both** `-O2` and `-Os` (confirmed
via `-Q --help=optimizers`). It is therefore **necessary but not sufficient**:
an additional `-O2`-only condition (register pressure) is required before the
bad fold occurs.
Flags tested individually on top of the full build flag set, **none** of which
fix it:
```
-fira-region=mixed
-fno-ira-hoist-pressure
-fira-region=mixed -fno-ira-hoist-pressure
-ftree-ch
-freorder-blocks-algorithm=stc
-fno-ira-share-spill-slots
-fno-ira-share-save-slots
-fstack-reuse=none
-fno-schedule-insns -fno-schedule-insns2
```
## 8. Why only one site in the function is affected
Every other RF-mode fill in `powl` is consumed by an arithmetic or extend insn:
| fill insn | consumer | shape | fold |
|-----------|----------|-------|------|
| 1440 | 256 | `(mult:XF …)` | rejected |
| 1455 | 838 | `(mult:XF …)` | rejected |
| 1449 | 849 | `(minus:XF …)` | rejected |
| 1446 | 860 | `(plus:XF …)` | rejected |
| 1465 | 763 | `(float_extend:RF …)` | rejected |
| **1443** | **251** | **`(set (reg:XF f8) (reg:XF f7))`** | **applied** |
IA-64 FP arithmetic accepts **register operands only**, so folding a memory
load into those insns produces unrecognisable RTL and `recog` rejects the
combination - the correct pairing survives by accident.
`movxf_internal` **does** accept a memory operand. Folding into a plain copy
therefore produces perfectly legal RTL, and `recog` cannot detect that the
MEM's mode was rewritten to make it fit. The missing invariant is only
observable in this one shape.
This was independently confirmed on a second function: all RF fills consumed by
`addxf3` survived the pass unchanged.
## 9. Additional impact
Beyond incorrect results, the corrupted load produces memory-format
**denormal** operands, which trap to the kernel's IA-64 floating-point
software-assist handler. On real hardware:
```
powl.exe(24897): floating-point assist fault at ip 200000000007e282, isr
0000040000000008
powl.exe(24897): floating-point assist fault at ip 200000000007e292, isr
0000040000000008
```
These IPs coincide with the mispaired load. This causes repeated assist faults
during normal execution - a significant performance penalty and kernel log
noise - in addition to the wrong answers.
## 10. Notes on reduction
Attempts to reduce this to a small standalone function were unsuccessful. The
defect requires `w` to be assigned a caller-saved FP hard register and spilled
in RFmode across a call. With lower register pressure the value stays in a
callee-saved register and is never spilled; with higher pressure IRA demotes it
to a memory slot accessed in XFmode, which is correctly paired. Only the
intermediate regime produces an RF spill whose reload feeds a plain register
copy.
## 11. Possible area for a fix
The pass is `gcc/late-combine.cc`, using the RTL-SSA propagation machinery in
`gcc/rtl-ssa/`. The invalid step is reconciling the def/use mode difference by
rewriting the **MEM's** mode.
The backend cannot fix this: after substitution the RTL is a legal XFmode load,
indistinguishable from a legitimate one - the knowledge that the slot was
written in RFmode is lost at substitution time.
Possible approaches:
1. Refuse mode-changing substitution when the RHS is a `MEM` (conservative,
target-independent).
2. Gate it on a target predicate. Note that `TARGET_MODES_TIEABLE_P` concerns
*registers*, which is precisely the distinction being conflated; there is no
existing hook for "identical memory encoding".
Making RF/XF non-tieable on ia64 would be wrong - it is true for registers and
would degrade FP allocation broadly to work around a memory-side bug.
## 12. Workarounds
- Build at `-Os`, `-O1` or `-O0`.
- Or keep `-O2` and add `-fno-late-combine-instructions`.
musl's ia64 port currently uses `CFLAGS="-Os"` for this reason.