https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128211

--- Comment #9 from savishwakarm at nvidia dot com ---
(In reply to [email protected] from comment #6)
> On Fri, 27 Nov 2026, savishwakarm at nvidia dot com wrote:
> 
> > 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.
> 
> Can we have a standalone testcase with this loop?

Attached is a standalone testcase (pr128211-meanshift.c), reduced
automatically from the MeanShiftImage loop with cvise, so the loop bounds
look odd (the v loop only runs for negative height; the driver passes
height = -20). Also attached is a small driver to time it
(meanshift_driver.c).

  gcc -Ofast -mcpu=olympus -S pr128211-meanshift.c

(-mcpu=neoverse-v2 gives the same assignment.)

Inner loop of mean_shift with r17-6424 (u = x25, count = x24, different
callee-saved pairs):

.L10:
        madd    x0, x25, x25, x26
        cbz     x0, .L8
        scvtf   d28, x25
        mov     x0, x19
        mov     w2, w20
        mov     w1, 1
        add     x25, x25, 1
        stp     d28, d15, [x28]
        bl      get_pixels
        ldrsh   w0, [x28, 16]
        cmp     w0, 0
        cinc    x24, x24, ne
        cmp     x25, x27
        bne     .L10

With r17-6425 (u = x25, count = x26, same pair x25/x26):

.L10:
        madd    x0, x25, x25, x27
        cbz     x0, .L8
        scvtf   d28, x25
        mov     x0, x19
        mov     w2, w20
        mov     w1, 1
        add     x25, x25, 1
        stp     d28, d15, [x24]
        bl      get_pixels
        ldrsh   w0, [x24, 16]
        cmp     w0, 0
        cinc    x26, x26, ne
        cmp     x25, x28
        bne     .L10

The loop body is the same apart from register numbers. The difference
comes from the loop bounds computed before the loop (x19 = width):

r17-6424, the inner loop end is derived from the negated lower bound:

        neg     x21, x19, asr 1         // -(width/2)
        asr     x22, x19, 1             //   width/2
        ...
        sub     x27, x6, x21            // 1 - (-(width/2)), x6 = 1

r17-6425, width/2 is used again for the end:

        neg     x21, x19, asr 1         // -(width/2)
        asr     x23, x19, 1             //   width/2
        ...
        add     x28, x23, 1             // width/2 + 1

and the outer loop end is recomputed per row (add x22, x0, 1). The changed
live ranges shift the callee-saved assignment. r17-6465 with r17-6425
reverted gets the r17-6424 assignment again.

u is converted and stored for the callee (scvtf d28, x25), so the next call
depends on u, while count depends on the callee's result. Any callee that
saves x25/x26 with one stp/ldp therefore makes the restored u wait for
count.

To time it, build mean_shift with each compiler and link it with the same
driver object; the driver's get_pixels saves/restores x19-x28 in STP/LDP
pairs and computes its result from the u-derived value:

  gcc -O2 -fno-math-errno -mcpu=olympus -c meanshift_driver.c
  gcc -Ofast -mcpu=olympus -c pr128211-meanshift.c
  gcc pr128211-meanshift.o meanshift_driver.o -lm

Time per call, r17-6425 relative to r17-6424:
  Neoverse-V2: about 2.4x slower
  Olympus:     about 3.3x slower

Swapping only x24 and x26 in the r17-6425 assembly of mean_shift (so count
leaves u's pair, nothing else changes) recovers it fully on both.

The assignment is fragile (with -mcpu=generic it flips, and at -O2 neither
revision pairs them), consistent with an allocation side effect rather than
a problem in r17-6425 itself.

Reply via email to