[go] cmd/compile: free doomed registers at loop headers

3 views
Skip to first unread message

Josh Bleecher Snyder (Gerrit)

unread,
Jul 18, 2026, 5:25:05 PM (8 days ago) Jul 18
to Keith Randall, goph...@pubsubhelper.golang.org, Josh Bleecher Snyder, golang-co...@googlegroups.com
Attention needed from Keith Randall

Josh Bleecher Snyder has uploaded the change for review

Josh Bleecher Snyder would like Keith Randall to review this change.

Commit message

cmd/compile: free doomed registers at loop headers

At the header of a loop that makes an unavoidable call,
values that are unused before that call needn't be in registers.
The call will clobber them anyway,
and the back-edge will do pointless reloads.
Free them. They'll be loaded lazily again when needed.

This shrinks total generated code size for std a bit,
from -0.07% to -0.2% depending on GOARCH.

More interesting, I instrumented the toolchain to
count spills at runtime on amd64.
Using the compiler as a test case, this reduces
spills by 3.1% while building std+cmd.
Using a subset of std tests that have fairly
stable execution paths as a test case, spills drop 2%.

There are a few microbenchmarks that jump noticeably,
such as a 16% sort.StableInt1K speed-up on arm64,
and a few minor regressions,
but most I tried are either neutral or small improvements,
which is about what you'd expect from
an average 2% reduction in spills.
Change-Id: I8fa88a665fb51fdd2c1fd13695c37ed7d50c89d4

Change diff

diff --git a/src/cmd/compile/internal/ssa/regalloc.go b/src/cmd/compile/internal/ssa/regalloc.go
index b2aea33..1aee9b8 100644
--- a/src/cmd/compile/internal/ssa/regalloc.go
+++ b/src/cmd/compile/internal/ssa/regalloc.go
@@ -1379,6 +1379,28 @@
}
}

+ // Look for loop headers of loops that contain unavoidable calls.
+ // That call will clobber all registers.
+ // Any value that's unused before the first such call is doomed.
+ // To avoid pointless backedge reloads, free such doomed values instead,
+ // and reload them lazily at their first use, after the call.
+ //
+ // v := ... // in a register
+ // for ... {
+ // ... // no use of v
+ // f() // clobbers registers
+ // ... = v // reload v here, not on the backedge
+ // }
+ doomDist := int32(math.MaxInt32)
+ if l := s.loopnest.b2l[b.ID]; l != nil && l.header == b && l.containsUnavoidableCall {
+ // The first call, if any, is at s.nextCall[0].
+ // A call in a later block is at least unlikelyDistance away.
+ doomDist = unlikelyDistance
+ if len(s.nextCall) > 0 {
+ doomDist = min(doomDist, s.nextCall[0])
+ }
+ }
+
// Save the starting state for use by merge edges.
// We append to a stack allocated variable that we'll
// later copy into s.startRegs in one fell swoop, to save
@@ -1394,6 +1416,11 @@
// specially during merge edge processing.
continue
}
+ // Drop values doomed by an intervening unavoidable call.
+ if s.values[v.ID].uses.dist >= doomDist && s.allocatable.hasReg(r) && !opcodeTable[v.Op].fixedReg {
+ s.freeReg(r)
+ continue
+ }
regList = append(regList, startReg{r, v, s.regs[r].c, s.values[v.ID].uses.pos})
s.startRegsMask = s.startRegsMask.addReg(r)
}

Change information

Files:
  • M src/cmd/compile/internal/ssa/regalloc.go
Change size: S
Delta: 1 file changed, 27 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention is currently required from:
  • Keith Randall
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I8fa88a665fb51fdd2c1fd13695c37ed7d50c89d4
Gerrit-Change-Number: 802740
Gerrit-PatchSet: 1
Gerrit-Owner: Josh Bleecher Snyder <josh...@gmail.com>
Gerrit-Reviewer: Josh Bleecher Snyder <josh...@gmail.com>
Gerrit-Reviewer: Keith Randall <k...@google.com>
Gerrit-Attention: Keith Randall <k...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Keith Randall (Gerrit)

unread,
Jul 20, 2026, 1:49:52 PM (6 days ago) Jul 20
to Josh Bleecher Snyder, goph...@pubsubhelper.golang.org, Keith Randall, golang...@luci-project-accounts.iam.gserviceaccount.com, Keith Randall, golang-co...@googlegroups.com
Attention needed from Josh Bleecher Snyder and Keith Randall

Keith Randall voted Code-Review+2

Code-Review+2
Open in Gerrit

Related details

Attention is currently required from:
  • Josh Bleecher Snyder
  • Keith Randall
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I8fa88a665fb51fdd2c1fd13695c37ed7d50c89d4
Gerrit-Change-Number: 802740
Gerrit-PatchSet: 1
Gerrit-Owner: Josh Bleecher Snyder <josh...@gmail.com>
Gerrit-Reviewer: Josh Bleecher Snyder <josh...@gmail.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Keith Randall <k...@google.com>
Gerrit-Attention: Josh Bleecher Snyder <josh...@gmail.com>
Gerrit-Attention: Keith Randall <k...@google.com>
Gerrit-Comment-Date: Mon, 20 Jul 2026 17:49:47 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Keith Randall (Gerrit)

unread,
Jul 20, 2026, 1:50:11 PM (6 days ago) Jul 20
to Josh Bleecher Snyder, goph...@pubsubhelper.golang.org, Keith Randall, golang...@luci-project-accounts.iam.gserviceaccount.com, golang-co...@googlegroups.com
Attention needed from Josh Bleecher Snyder

Keith Randall voted Code-Review+1

Code-Review+1
Open in Gerrit

Related details

Attention is currently required from:
  • Josh Bleecher Snyder
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I8fa88a665fb51fdd2c1fd13695c37ed7d50c89d4
Gerrit-Change-Number: 802740
Gerrit-PatchSet: 1
Gerrit-Owner: Josh Bleecher Snyder <josh...@gmail.com>
Gerrit-Reviewer: Josh Bleecher Snyder <josh...@gmail.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Keith Randall <k...@google.com>
Gerrit-Attention: Josh Bleecher Snyder <josh...@gmail.com>
Gerrit-Comment-Date: Mon, 20 Jul 2026 17:50:05 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Carlos Amedee (Gerrit)

unread,
Jul 21, 2026, 5:37:55 PM (5 days ago) Jul 21
to Josh Bleecher Snyder, goph...@pubsubhelper.golang.org, Keith Randall, Keith Randall, golang...@luci-project-accounts.iam.gserviceaccount.com, golang-co...@googlegroups.com
Attention needed from Josh Bleecher Snyder

Carlos Amedee voted Code-Review+1

Code-Review+1
Open in Gerrit

Related details

Attention is currently required from:
  • Josh Bleecher Snyder
Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement satisfiedReview-Enforcement
    • requirement satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I8fa88a665fb51fdd2c1fd13695c37ed7d50c89d4
    Gerrit-Change-Number: 802740
    Gerrit-PatchSet: 1
    Gerrit-Owner: Josh Bleecher Snyder <josh...@gmail.com>
    Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
    Gerrit-Reviewer: Josh Bleecher Snyder <josh...@gmail.com>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Keith Randall <k...@google.com>
    Gerrit-Attention: Josh Bleecher Snyder <josh...@gmail.com>
    Gerrit-Comment-Date: Tue, 21 Jul 2026 21:37:51 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    open
    diffy

    Josh Bleecher Snyder (Gerrit)

    unread,
    Jul 21, 2026, 10:12:48 PM (5 days ago) Jul 21
    to Josh Bleecher Snyder, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Carlos Amedee, Keith Randall, Keith Randall, golang...@luci-project-accounts.iam.gserviceaccount.com, golang-co...@googlegroups.com

    Josh Bleecher Snyder submitted the change

    Change information

    Commit message:
    cmd/compile: free doomed registers at loop headers

    At the header of a loop that makes an unavoidable call,
    values that are unused before that call needn't be in registers.
    The call will clobber them anyway,
    and the back-edge will do pointless reloads.
    Free them. They'll be loaded lazily again when needed.

    This shrinks total generated code size for std a bit,
    from -0.07% to -0.2% depending on GOARCH.

    More interesting, I instrumented the toolchain to
    count spills at runtime on amd64.
    Using the compiler as a test case, this reduces
    spills by 3.1% while building std+cmd.
    Using a subset of std tests that have fairly
    stable execution paths as a test case, spills drop 2%.

    There are a few microbenchmarks that jump noticeably,
    such as a 16% sort.StableInt1K speed-up on arm64,
    and a few minor regressions,
    but most I tried are either neutral or small improvements,
    which is about what you'd expect from
    an average 2% reduction in spills.
    Change-Id: I8fa88a665fb51fdd2c1fd13695c37ed7d50c89d4
    Files:
    • M src/cmd/compile/internal/ssa/regalloc.go
    Change size: S
    Delta: 1 file changed, 27 insertions(+), 0 deletions(-)
    Branch: refs/heads/master
    Submit Requirements:
    Open in Gerrit
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: merged
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I8fa88a665fb51fdd2c1fd13695c37ed7d50c89d4
    Gerrit-Change-Number: 802740
    Gerrit-PatchSet: 2
    open
    diffy
    satisfied_requirement
    Reply all
    Reply to author
    Forward
    0 new messages