https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128211
savishwakarm at nvidia dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |savishwakarm at nvidia dot com
--- Comment #4 from savishwakarm at nvidia dot com ---
Another instance of the same pattern, from an unrelated change, with a
different reproducer.
Since r17-6425-gf61ae2cb8d3 ("tree-optimization/128672 - avoid UB in CSE of
-x / y"), 538.imagick_r built with -Ofast -flto -mcpu=olympus is about 13%
slower on Olympus. r17-6424-gd66469b5bfe is fine, and reverting r17-6425 on
top of r17-6465 restores the old performance.
r17-6425 keeps MeanShiftImage's innermost induction variable u in x20 and the
cinc-updated count in x19 across the call to GetVirtualPixelsFromNexus, so
both live in the callee's stp/ldp x19, x20 pair. r17-6424 keeps u in x19
(paired with the loop-invariant bound in x20) and count in x21.
The trigger is in
for (v=-(height/2); v<=height/2; v++)
for (u=-(width/2); u<=width/2; u++)
Before r17-6425, FRE reused the negated lower bound for height/2; after it,
width/2 and height/2 are computed separately (visible in the fre1 dump).
The two extra live values shift the callee-saved register assignment in the
hot loop. The loop body has the same instructions in the same order.
Here the value delayed by the pair is the induction variable rather than a
loop-invariant pointer: u feeds u*u, the next call's arguments and the loop
branch, so each iteration waits for the previous iteration's load, convert
and FP compare chain that produces count.
Profile on Olympus, r17-6425 relative to r17-6424:
- instructions: unchanged
- cycles: +13%
- backend stall cycles: about 3x; memory-bound stalls, cache misses and
branch mispredictions unchanged
- GetVirtualPixelsFromNexus (byte-identical in both builds, same
address): +32% cycles; MeanShiftImage: +25% cycles
- the most-stalled instruction in the slow build is madd x0, x20, x20, x25
(u*u), right after the ldp that restores u
Controls, by editing the LTO assembly of the slow build: loop alignment
changes and stack-slot layout changes make no difference, while renumbering
only the hot loop's callee-saved registers (so that u and count no longer
share a pair) recovers the whole regression (within 1% of r17-6424).
Attached is a variant of the reproducer that puts the induction variable in
the shared pair (pairdep_iv.c):
gcc -O2 -fno-math-errno -DCOUNT_REG='"x19"' pairdep_iv.c -lm # shared pair
gcc -O2 -fno-math-errno -DCOUNT_REG='"x21"' pairdep_iv.c -lm # split
With identical code apart from register numbers, the shared-pair version is
about 2.2x slower per iteration on Neoverse-V2.
Two unrelated middle-end changes (r17-3330 and r17-6425) have now hit this in
the same loop, which suggests a mitigation in register allocation or in
callee-saved pair formation would be more robust than fixing each trigger.