Hi,

This is v2 of the RFC [1]. Thanks to Vladimir's review [2], v2 drops
the indirect-call check and keeps a loop region when the pseudos live
across its calls do not fit in the callee-saved registers and the
calls are more frequent than the moves on the loop boundary.

With x86-64 APX EGPR the caller-saved GPRs grow from 9 to 25 while
callee-saved stays at 6 (rbx, rbp, r12-r15). The inflated register
file makes IRA's low_pressure_loop_node_p return true for more loops,
so these loops are flattened and no region splitting is done for them.
Without the loop region, a pseudo that lives across the calls in the
loop gets one hard register for the whole function, often a
caller-saved one, and is then spilled around every call in the loop.
The example extracted from tachyon is like below, with -mapxf

movq    %rax, 32(%rsp)
call    *(%rdx)
movq    32(%rsp), %rax
movq    8(%rax), %rax ;->base pointer assigned with caller-saved

Without APX the loop is kept as a region and the code is

call    *%rax
movq    8(%rbx), %rbx ;->base pointer assigned with callee-saved

The spill-call-reload-deref sequence on the critical chain brings
about 7% regression.

This series introduces TARGET_IRA_LOOP_REGION_FLATTEN_COST so that a
target can keep a low-pressure loop as a separate IRA region. It is
called before such a loop is flattened, when more pseudos live across
calls in the loop than the callee-saved registers of their class, and
the pseudos live across the loop without being referenced can cover
the excess. Summed over the pseudos live into or out of the loop, IRA
passes the hook call_freq - the frequency of the calls they live
across, and border_freq - the frequency of the loop entry and exit
edges they live on, plus the number of these edges. The loop is kept
if the result is positive. 

The x86 implementation returns

  call_freq - border_freq

for a loop with one entry and one exit edge when APX EGPR is active,
and 0 otherwise. This keeps the call-heavy loops as separate IRA regions,
so the pseudos live across their calls still get callee-saved registers.

With the patch, tachyon gets 7% improvement under APX, and SPEC2017
and SPEC2026 are flat.

Changes in v2:

1. The x86 hook no longer looks at indirect calls. The call in tachyon
happens to be indirect, but the problem exists for any call. The
pressure test and the frequencies are now computed in the middle-end,
and the target only compares them.

2. We first tried the common solution suggested in the review: create
a region if the register pressure of the pseudos crossing calls is
more than the callee-saved registers of the class, with the flag
propagated to the parent loops. On SPEC2026 with APX, 714.cpython_r
regressed 4-5% and 708.sqlite_r 3-4%. The regressed hotspot loop has
more than 6 integer pseudos live at nearly every call, so it always
passes the test, and every loop containing it is kept:

                     flattened loops   boundary copies
  trunk                   1579                3
  pressure test             94             7008

So the pressure test alone is a rough model and does not consider the
penalty of the boundary copies. The biggest loops always pass it and
also have the most expensive copies. v2 computes this pressure in IRA
for every loop and keeps the test as the first condition, then adds a
second one: a region only helps when some pseudos can give up their
registers inside the loop, and those are the pseudos live across it
without being referenced. The frequency comparison in the hook is
the cost side.

We also tried to fix it in coloring after the loop is flattened, but
coloring cannot split a live range again. To give the pseudo live
across the calls a callee-saved register inside the loop, another
pseudo has to leave that register for the part of its range inside
the loop, which is exactly what loop region splitting provides.

3. It is still a hook. We understand new hooks are better avoided.
The pressure test only uses generic IRA data and is done in
ira-build.cc. What is left for the target is how to compare the two
frequencies and how many entry and exit edges to accept. This part is
an untuned model. border_freq counts a move on each entry and exit
edge for every pseudo live on it, but a pseudo that gets the same hard
register inside and outside the loop needs no move, and we cannot know
before coloring how many of them there will be. So border_freq is only
an upper bound of the boundary cost, and how to weigh it against
call_freq is a target choice.
The x86 choice is the one that keeps SPEC flat with APX, and we have
no data that it fits other targets. If current hook enabled for all
targets, it will keep some wrong loops. e.g. 714.cpython_r
regressed 3% on aarch64 Neoverse-N1. So the hook limits the change to
x86 APX. The default is NULL, then IRA computes nothing and other
targets are unchanged.

Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.

Appreciated for any comments.

[1] https://gcc.gnu.org/pipermail/gcc-patches/2026-June/720425.html
[2] https://gcc.gnu.org/pipermail/gcc-patches/2026-June/720524.html

Hongyu Wang (2):
  ira: Add TARGET_IRA_LOOP_REGION_FLATTEN_COST
  i386: Implement TARGET_IRA_LOOP_REGION_FLATTEN_COST for APX

 gcc/config/i386/i386.cc                       |  20 ++
 gcc/doc/tm.texi                               |  26 +++
 gcc/doc/tm.texi.in                            |   2 +
 gcc/ira-build.cc                              | 203 ++++++++++++++++--
 gcc/ira-int.h                                 |  11 +
 gcc/ira-lives.cc                              |  42 +++-
 gcc/target.def                                |  30 +++
 .../i386/ira-loop-region-flatten-1.c          |  94 ++++++++
 .../i386/ira-loop-region-flatten-2.c          |  29 +++
 .../i386/ira-loop-region-flatten-3.c          |   6 +
 .../i386/ira-loop-region-flatten-4.c          |  26 +++
 .../i386/ira-loop-region-flatten-5.c          |  25 +++
 12 files changed, 495 insertions(+), 19 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-1.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-3.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-4.c
 create mode 100644 gcc/testsuite/gcc.target/i386/ira-loop-region-flatten-5.c

-- 
2.31.1

Reply via email to