> 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%

For PR #5, load-pair and store-pair sites increase by 2.83% and
5.52%, respectively.  The corresponding gains are 1.91% and 5.29%
for PR #4, and 3.39% and 12.18% for PR #3.  The total gains for
PR #5 and PR #4 differ by only 0.61 percentage points.

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.

> > and represents each selected pair as a temporary, recognized PARALLEL
> > insn containing two scalar SETs.
>
> I'm not sold on this approach. Duplicating the move patterns and having to
> restore CFI info and other properties during split feels error-prone. I prefer
> the pair-fusion.cc pass to be generalized to allow for emitting multiple
> instructions instead of just one. If that is possible the splitting pass and 
> the
> extra define_insn's will not be needed.
>

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.

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 that having a custom representation will also prevent sched2 from
> reasoning about the load and store, e.g. to adjust the load/store offset when
> moving it before or after increment/decrement.
>

I agree with this limitation.  PR #3 makes the two accesses one
schedulable unit, so sched2 cannot move them independently or adjust
one access's offset on its own.  PR #4 keeps two separate insns, so
sched2 can schedule them independently, but it may also separate a
pair that the pass brought together.

> > This gives sched2 one schedulable unit for the pair.  A RISC-V pass
> > immediately after sched2 restores the two scalar insns, preserving their
> > order, locations, register notes, and frame-related notes.
>
> This introduces a second representation of fused pairs during scheduling. Is
> there any advantage to this over SCHED_GROUP_P? Sticking to a single mechanism
> to glue instructions together is cleaner in my opinion. The hooks you 
> introduced
> in your previous patches will have to be adapted to interpret these insns as
> fused in addition to SCHED_GROUP_P as well.
>

The temporary insn in PR #3 fits the existing gen_pair interface;
it has no general scheduling advantage over SCHED_GROUP_P.  Sched2
sees one PARALLEL, so the existing macro-fusion hook for two scalar
insns cannot recognize the pair in the usual way.  PR #4 currently
moves two insns together in the pair pass but does not set
SCHED_GROUP_P; sched2 may separate them.  Using SCHED_GROUP_P would
also require evaluating the tradeoff with other fusion opportunities.

> Even if the merged insn is required, I prefer it to be split before sched2 to
> simplify scheduling.
>

Splitting before sched2 would expose two scalar insns to the
scheduler, but we would still have to keep the selected pair
together and maintain the temporary patterns and note restoration.
I do not see a clear simplification over PR #4 in that variant.

> We noticed when working on the scheduling for designs where load/store pair
> fusion is less profitable than some other fusion cases that using a single
> instruction to represent a fused pair is very inflexible and potentially locks
> you in that fusion too early or too late, while SCHED_GROUP_P is easily
> adjusted.
>
> Thanks,
> Michiel

Locking in a memory pair early could displace a more valuable
fusion.  I tested the instruction counts and performance across all
SPEC CPU 2017 Integer cases and have not observed a negative effect.
These tests do not cover every scheduling interaction, but they are
useful evidence for evaluating this prototype.

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 would appreciate the community's views.  Does it make sense to
advance the priority hook independently first?  For a subsequent
pair pass, should we explore keeping two insns together with
SCHED_GROUP_P, or continue refining the temporary RTL approach?
Which direction seems better for CFI handling and competing fusion
opportunities?

Thanks,
Jin

Reply via email to