Repository navigation
Description
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
Jorropo commentedon Jul 14, 2026
Thanks for the bug report, this is intentional but I didn't realized it would have this consequence.
I did wrote code to fold:
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.
Jorropo commentedon Jul 14, 2026
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:
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 < ywhich generate the correct condition in the carry flag.)gabyhelp commentedon Jul 14, 2026
Related Issues
Related Code Changes
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Jorropo commentedon Jul 14, 2026
Actually the fix for #79887 should already fix the bug that was preventing me from adding the rule in the past.
Jorropo commentedon Jul 14, 2026
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 }Jorropo commentedon Jul 14, 2026
Tentatively marked it
wait-releasesince 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.
gopherbot commentedon Jul 14, 2026
Change https://go.dev/cl/800460 mentions this issue:
cmd/compile: on amd64 optimize x + carry and x - carry into ADC and SBBJorropo commentedon Jul 14, 2026
Thanks to the repro you've sent I've also found an other limitation with our attempts to optimize
ADCandSBB#80400cmd/compile: on amd64 optimize x + carry and x - carry into ADC and SBB
cmd/compile: on amd64 optimize x + carry and x - carry into ADC and SBB