On 10/22/26 10:21 AM, Jin Ma wrote:
Hi Jin,
Thanks for looking into this. The numbers look very promising, I'll do some
benchmarks with this as well when I get the chance.
Hi Michiel,
Thank you for the review and for offering to run benchmarks. I
tried several ways to keep fusible memory accesses adjacent in the
final instruction stream. Three working prototypes use the same
C9501 fusion base:
PR #5, scheduling priority hook: group accesses by kind, base,
and offset. This is a small change, but it does not find an
actual partner; the outcome depends on scheduling.
https://github.com/majin2020/gcc/pull/5
This is the patch previously posted at:
https://patchwork.sourceware.org/project/gcc/patch/[email protected]/
PR #4, Two insns: extend the common search and movement logic
in gcc/pair-fusion.cc and add a RISC-V adapter. It finds actual
pairs while retaining two scalar RTL insns. It changes common
code, and sched2 can still separate the pair.
https://github.com/majin2020/gcc/pull/4
PR #3, this RFC (Temporary RTL): use the existing pair-fusion
search, represent a selected pair as one RTL insn through sched2,
then split it. It leaves the common pass unchanged and preserves
adjacency, but adds a temporary form and split, and prevents
sched2 from scheduling the accesses independently.
https://github.com/majin2020/gcc/pull/3
All three were measured on SPEC CPU 2017 Integer built with
-march=rv64gcv -mabi=lp64d -O3 -flto -mtune=xt-c9501fdvt.
PR #4 and PR #3 used explicit -mpair-fusion. The counts are
nonoverlapping adjacent memory pairs in application functions of
the final binaries, using the C9501 rules. Each change is relative
to the same baseline.
PR #5, scheduling priority hook:
Case Change vs baseline
500.perlbench_r +8.06%
502.gcc_r +3.63%
505.mcf_r +10.69%
520.omnetpp_r +3.79%
523.xalancbmk_r +2.07%
525.x264_r +7.79%
531.deepsjeng_r +7.88%
541.leela_r +4.14%
548.exchange2_r +11.89%
557.xz_r +3.59%
SUM +4.05%
PR #4, Two insns:
Case Change vs baseline
500.perlbench_r +7.50%
502.gcc_r +2.78%
505.mcf_r +8.81%
520.omnetpp_r +3.14%
523.xalancbmk_r +1.44%
525.x264_r +8.55%
531.deepsjeng_r +7.68%
541.leela_r +3.78%
548.exchange2_r +20.58%
557.xz_r +3.39%
SUM +3.44%
PR #3, Temporary RTL:
Case Change vs baseline
500.perlbench_r +11.31%
502.gcc_r +6.14%
505.mcf_r +11.32%
520.omnetpp_r +8.93%
523.xalancbmk_r +5.95%
525.x264_r +12.17%
531.deepsjeng_r +12.53%
541.leela_r +9.57%
548.exchange2_r +26.21%
557.xz_r +8.58%
SUM +7.38%
Tons of good data here. I'm not terribly surprised by most of it. The
clear winner is generating temporary RTL, but I suspect that's going to
require a fair amount of iteration on the implementation.
#5 is simple and should be non-controversial. I would think we can
likely get it in this cycle for gcc-17.
I would think #4 would be non-controversial as well and may interact
favorably with #5. Ideally we'd look to land it too, hopefully picking
up that significant improvement on exchange2.
AArch64 also uses TARGET_SCHED_FUSION_PRIORITY to order loads and
stores by kind, base, and offset. It has a separate ldp_fusion pass
that uses gcc/pair-fusion.cc to find memory pairs. This suggests
that the priority hook can proceed independently of explicit pair
formation. AArch64 ultimately emits LDP/STP instructions, whereas
C9501 still needs two scalar accesses, so the pair representation
needs separate consideration.
Right. And what you're doing for C9501 is roughly what I want to be
doing for our design as well.
I agree that duplicated patterns and CFI recovery need care. I
first explored keeping two scalar insns throughout. PR #4 is that
prototype. It requires changes to the common search, movement,
and grouping logic in gcc/pair-fusion.cc. Covering base updates and
frame accesses would also require address rewriting and CFI handling
there. That is a substantial change. A RISC-V-only pass would
duplicate candidate search and alias analysis, with limited benefit.
But this should all be fairly generic. I don't immediately understand
why CFI would be any different for RISC-V than ARM, so I'd like to
understand that concern. Any changes in search/movement/etc are a bit
of a surprise, so I'll want to understand those better. BUt at a high
level re-using the pair-fusion code after light generalization shouldn't
be controversial.
The current RFC, PR #3, instead uses a temporary PARALLEL to fit the
existing gen_pair interface and splits it after sched2. The common
pass still handles candidate search and base selection. This does
not establish that rewriting across base updates explains the
measured gap. The extra patterns, split pass, and CFI and note
restoration need careful review. There is a targeted frame CFI test.
I think my big question here would be why is it so much more effective.
It sounds like we're seeing something come along after the fact and
split up. In some cases that's going to be desirable, in others likely
not. I think we need to really understand the real performance
impacts. It sound like you're counting fused pairs statically rather
than dynamically. Furthermore, it doesn't account for real performance
concerns. For example, pairing can interact in interesting ways with
things like store-forward-bypass avoidance at the uarch level.
Given the implementation scope and static results, I would prefer
to advance the scheduling priority hook in PR #5 first. It follows
an approach also used by AArch64, has a smaller implementation, and
its current total gain is close to that of PR #4. PR #3 and PR #4
could complement the hook in later work: PR #3 keeps a selected pair
adjacent through sched2 but needs a temporary representation and
split; PR #4 leaves the accesses separately schedulable but requires
common framework changes and does not yet keep them adjacent through
sched2.
I'd tend to agree. #5 seems like a no-brainer for this release. I would
like to hope that meaningful amounts of #4 would be non-controversial
and complement #5 and could ideally get into this release too. And we
dive deeply into the issues addressed by #3 to decide how best to fix
for gcc-18.
Jeff