Skip to content

cmd/compile: materialized bits.Add64/Sub64 carries no longer fold into consumers on amd64 (regression from CL 778140) #80399

Closed

Description

@gaul

Go version

go version go1.27-devel_63d836396a linux/amd64 (regression vs go version go1.26.5 linux/amd64)

Output of go env in your module/workspace

GOARCH=amd64
GOOS=linux
GOAMD64=v1

(other fields defaults; not relevant)

What did you do?

Compiled a bits.Add64 chain whose final carry is consumed by ordinary arithmetic, the pattern that fiat-crypto generated code (crypto/internal/fips140/nistec/fiat, edwards25519, mldsa) hits pervasively via p521Uint1(x) + y:

package carry

import "math/bits"

func AddUseCarry(a, b [2]uint64, s uint64) uint64 {
	_, c := bits.Add64(a[0], b[0], 0)
	_, c2 := bits.Add64(a[1], b[1], c)
	return s + c2 // carry consumed by a plain add
}

go build -gcflags=-S with both toolchains.

What did you see happen?

go1.26.5 materializes and consumes the carry in two instructions — SBBQ produces a 0/−1 carrymask and the negation folds into the consumer (ADDQ x (NEGQ mask) → SUBQ x mask):

ADDQ  CX, DX
ADCQ  SI, BX
SBBQ  CX, CX
SUBQ  CX, AX

Tip (63d8363) spends three, because MOVBQZX (SETB flags) participates in no folds:

ADDQ    CX, DX
ADCQ    SI, BX
SETCS   CL
MOVBLZX CL, CX
ADDQ    CX, AX

This comes from commit 212065c (CL 778140, "cmd/compile: shuffle bits.Sub intrinsic generation on amd64", updates #76056), which changed the lowering of Select1 (Add64carry/Sub64borrow) from NEGQ (SBBQcarrymask flags) to MOVBQZX (SETB flags) so the carry is easier to pattern-match. For carries that feed the next Add64carry/Sub64borrow directly nothing changes — the round-trip elision was carried over ((Select1 (NEGLflags (MOVBQZX (SETB x)))) => x, AMD64.rules line 44) — and for a carry that is only returned the two forms tie at two instructions. But a carry consumed by arithmetic lost the (ADDQ x (NEGQ y)) => (SUBQ x y) composition, costing one instruction and 3 bytes per site.

fiat-generated field arithmetic consumes carries this way constantly. Copying the (byte-identical between the two releases) p521_fiat64.go into a standalone module and building its test binary with both toolchains:

function go1.26.5 tip 63d8363 delta
p521Mul text bytes 9164 9310 +146
instructions 2399 2425 +26
setcc/movzx 0 / 0 36 / 36
adc/sbb 324 288 −36
p521Square text bytes 7621 7821 +200
instructions 2007 2048 +41
setcc/movzx 0 / 0 36 / 36
adc/sbb 324 288 −36

(The 36 dropped sbbs are the former carrymask materializations, now setcc; in 1.26.5 twenty-six of their negations folded into consumer SUBQs and ten remained as NEGQ, i.e. 46 instructions total vs 72 now.)

Benchmarking the same standalone module (AMD Ryzen AI MAX+ 395, linux/amd64, count=5, spread < 1%):

            go1.26.5   tip 63d836396a
P521Mul     58.4 ns    59.8 ns   (+2.4%)
P521Square  49.7 ns    51.2 ns   (+2.9%)

An instruction-pattern scan (x86lint) of the fips140 packages linked into cmd/go counts ~520 new adjacent setcc; movzx pairs from this lowering (nistec/fiat 279, mldsa 86, nistec 75, edwards25519 46, bigmod 19).

What did you expect to see?

At least parity with go1.26.5. CL 778140's commit message describes the change as a no-op, which holds for isolated carries but not for consumed ones.

The SETB canonicalization itself seems like a good direction for #76056; what is missing is folds that let consumers eat the materialized form, e.g.

(ADDQ x (MOVBQZX (SETB f))) => consume CF directly (ADCQconst [0] x f — one instruction, better than either release)

and analogous ADDQconst/SUBQ forms, falling back to something no worse than the old SBBQ+SUBQ pair when the flags value cannot be scheduled to the consumer. With those in place this pattern would beat go1.26.5 rather than trail it.

cc @Jorropo @randall77

Activity

  1. Jorropo commented on Jul 14, 2026

    @Jorropo
    Member

    Thanks for the bug report, this is intentional but I didn't realized it would have this consequence.

    I did wrote code to fold:

    (ADDQ x (MOVBQZX (SETCS flags))) => (ADCQ x (Const64 [0]) flags)
    

    However I had to remove it before submitting the CL because it kept breaking compilation and I didn't investigated at the time, but the same bug showed up later (#79887) so I know why this happen and how to fix it.

  2. self-assigned this
    on Jul 14, 2026
  3. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Jul 14, 2026
  4. Jorropo commented on Jul 14, 2026

    @Jorropo
    Member

    The 1.25 assembly is quite funny and goes in a round about way, it's taking the inverse of an inverse to move stuff around from flags to GP.
    I think we should be able to do:

    ADDQ  CX, DX
    ADCQ  SI, BX
    ADCQ  $0, AX
    

    or something like that
    which is what I was trying to do before giving up (I only thought about using the carry flag from a compare which is almost always pointless since there is only unsigned x < y which generate the correct condition in the carry flag.)

  5. Jorropo commented on Jul 14, 2026

    @Jorropo
    Member

    Actually the fix for #79887 should already fix the bug that was preventing me from adding the rule in the past.

  6. Jorropo commented on Jul 14, 2026

    @Jorropo
    Member

    I might as well fix:

    package carry
    
    import "math/bits"
    
    func SubUseAddCarry(a, b [2]uint64, s uint64) uint64 {
    	_, c := bits.Add64(a[0], b[0], 0)
    	_, c2 := bits.Add64(a[1], b[1], c)
    -	return s + c2 // carry consumed by a plain add
    +	return s - c2 // carry consumed by a plain sub
    }
  7. Jorropo commented on Jul 14, 2026

    @Jorropo
    Member

    Tentatively marked it wait-release since it's not a major regression and we're very late in cycle, guesstimating are loosing like 2~3 cycles everytime this execute over the 1.25 version.

    Imo fine to fix in 1.28.

  8. gopherbot commented on Jul 14, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/800460 mentions this issue: cmd/compile: on amd64 optimize x + carry and x - carry into ADC and SBB

  9. Jorropo commented on Jul 14, 2026

    @Jorropo
    Member

    Thanks to the repro you've sent I've also found an other limitation with our attempts to optimize ADC and SBB #80400

  10. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Jul 14, 2026
  11. added this to the Unplanned milestone on Jul 14, 2026
  12. added a commit that references this issue on Jul 15, 2026
    0a6ccc5
  13. added a commit that references this issue on Aug 2, 2026
    98dea57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FixPendingIssues that have a fix which has not yet been reviewed or submitted.NeedsFixThe path to resolution is known, but the work has not been done.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions