Skip to content

cmd/compile: break false dependency on Zen 4 and 5 (compress/expand) #81609

Open

Description

@stapelberg

The simd/archsimd packages added in Go 1.26 and Go 1.27 work great for me, see also https://michael.stapelberg.ch/posts/2026-09-06-dcs-fast-turbopfor-go-simd/

I did run into one issue, though, which is that AMD Zen 4 and Zen 5 CPUs have a false dependency in their compress and expand implementation.

The effect of this is that certain workloads, like the TurboPFor decoder I’m using in Debian Code Search, slow down significantly: consecutive expands need 4 cycles instead of 1 cycle.

LLVM has a fix for this: LLVM emits a SIMD Zeroing Idiom before compress/expand to break the false dependency. See AMD’s “Software Optimization Guide for the AMD Zen5 Microarchitecture”, section 2.9.2 for more details on zeroing idioms. See llvm/llvm-project#206849 for the LLVM issue with more details.

I can send a CL to make Go break the false dependency, too.

Steps to reproduce:

  1. Clone https://github.com/Debian/dcs
  2. Run make bench-baseline in internal/turbopfor/pfordec
  3. Undo the workaround I added:
diff --git i/internal/turbopfor/pfordec/bitunpack256vex_amd64.go w/internal/turbopfor/pfordec/bitunpack256vex_amd64.go
index 7bf24c8b..48534fda 100644
--- i/internal/turbopfor/pfordec/bitunpack256vex_amd64.go
+++ w/internal/turbopfor/pfordec/bitunpack256vex_amd64.go
@@ -130,10 +130,7 @@ func bitunpack256v32Ex(input []byte, fulloutput []uint32, nbits int, exmap *[32]
 
 // gatherExceptions loads 8 exceptions at offset j, per bitmask m.
 func gatherExceptions(exceptions *[256]uint32, j int, m byte) archsimd.Uint32x8 {
-	// VPEXPANDD has a false dependency on Zen 4 and Zen 5 (at least).
-	// Xor(zero) is a workaround; the Go compiler should do this.
-	var zero archsimd.Uint32x8
-	return archsimd.LoadUint32x8Array((*[8]uint32)(exceptions[j : j+8])).Xor(zero).Expand(archsimd.Mask32x8FromBits(m))
+	return archsimd.LoadUint32x8Array((*[8]uint32)(exceptions[j : j+8])).Expand(archsimd.Mask32x8FromBits(m))
 }
 
 func bitunpack256v32ExSIMDUnrolled[T bitWidthT](input []byte, output *[256]uint32, exmap *[32]byte, exceptions *[256]uint32) {
  1. Run make bench-relative and observe how performance drops to half:
make -k -C \~/dcs/internal/turbopfor/pfordec/ bench-relative
make: Entering directory '/home/michael/dcs/internal/turbopfor/pfordec'
go test -pgo=decode.cpuprof
PASS
ok  	github.com/Debian/dcs/internal/turbopfor/pfordec	0.020s
taskset -c 1 go test -pgo=decode.cpuprof -run=^$ -bench=. -benchtime=200000x -count=6 | tee bench.txt
[…]
benchstat -filter '/impl:go .unit:(Mval/s)' baseline.txt bench.txt
goos: linux
goarch: amd64
pkg: github.com/Debian/dcs/internal/turbopfor/pfordec
cpu: AMD Ryzen 9 9950X3D 16-Core Processor          
                                              │ baseline.txt │              bench.txt              │
                                              │    Mval/s    │    Mval/s     vs base               │
[…]
Decode/n=2048/vals=debian-mix/impl=go           12.121k ± 1%   6.641k ±  0%  -45.21% (p=0.002 n=6)
[…]

Activity

  1. stapelberg commented on Sep 19, 2026

    @stapelberg
    ContributorAuthor

    Sent http://go.dev/cl/835405 to break the false dependency.

  2. gopherbot commented on Sep 19, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/835405 mentions this issue: cmd/compile: break false dependency on Zen 4 and 5

  3. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Sep 29, 2026
  4. prattmic commented on Sep 29, 2026

    @prattmic
    Member

    cc @golang/compiler

  5. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions