Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Generate the SIMD kernels with mhr3/gocc - #59

Open
mhr3 wants to merge 1 commit into
kelindar:mainfrom
mhr3:use-new-gocc
Open

mhr3 wants to merge 1 commit into
kelindar:mainfrom
mhr3:use-new-gocc

Conversation

@mhr3

@mhr3 mhr3 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

This regenerates the assembly kernels with mhr3/gocc, a fork of the gocc this repository already uses, and cleans up what sits around them. The Go API is unchanged.

What changes

  • Generator. codegen/generate.sh runs github.com/mhr3/gocc/cmd/gocc@v0.31.2 with go run. The kernels for several sources now declare a real Go frame. The current files copy the compiler's pushes and sub rsp as raw bytes into functions declared with a zero-size frame, and the Linux arm64 file uses x18, which is reserved on Windows and Android. go vet fails on main for the declared argument sizes and passes here.
  • One C source. codegen/simd.c replaces the four per-target files and is compiled for AVX2, AVX-512 and arm64. One arm64 file serves Linux and macOS, so simd_apple.* and simd_neon.* go away, and simd_avx.* becomes simd_avx2.*.
  • Slice signatures. The kernels take (a, b []uint64) and, for several sources, (a []uint64, b []*uint64). dimensionsOf is gone and pointersOf returns the []*uint64.
  • Count() on AVX-512 uses VPOPCNTQ. This covers what Use the accelerated Count on AVX-512 CPUs #53 and Count() not using fast path on AVX512 CPUs #46 are about.
  • AVX2 built at -O3 like the other targets (it was -O1), so its count kernel is vectorised too.
  • Smaller things. FromBytes/ToBytes use unsafe.Slice in place of the deprecated reflect.SliceHeader, and Filter no longer casts the predicate through unsafe.Pointer.

Things to decide before merging

  1. It requires Go 1.27. gocc writes an instruction as text when the Go assembler knows it, and the arm64 file contains VBIC, which the assembler accepts from 1.27.0. Go 1.21 to 1.26.4 fail with unrecognized instruction "VBIC". go.mod and the workflow are updated accordingly.
  2. Skylake-X, Cascade Lake and Cooper Lake move from AVX-512 to AVX2. The AVX-512 path now requires AVX512_VPOPCNTDQ (Ice Lake and later, Zen 4 and later). On a Xeon Platinum 8275CL the AVX-512 kernels lower the core clock from 3600 to 3100 MHz, and scalar code running between kernel calls gets 16% slower (167 µs to 194 µs for a fixed piece of work, with one short kernel call before each piece). The price is that the kernels for several sources are slower on those chips, see the last column below.
  3. Bitmaps under about 16 words get slower on AVX2. With -O3 the loops are unrolled: And of two 8-word bitmaps takes 11.2 ns where it took 8.9 ns on the 8275CL. From 16 words up it is faster (256 words: 43.9 ns to 32.8 ns).

Speed against main

Medians of 8 interleaved runs of the repository benchmarks (1M-bit bitmaps), in ns, main → this branch.

Benchmark Graviton 4 (NEON) EPYC 9R45 (AVX-512) Xeon 8275CL (AVX-512 → AVX2)
count 3697 → 3790 4006 → 591 9818 → 3585
and, andnot, or, xor 4394 → 4405 1177 → 1175 2795 → 2610
and4-batch, or4-batch, xor4-batch 13410 → 13465 2790 → 2785 5693 → 7771
or4-batch-ragged, xor4-batch-ragged 15403 → 15449 4107 → 4122 9828 → 9253

Testing

  • go vet is clean for linux and darwin on amd64 and arm64, with and without noasm.
  • The test suite passes on arm64 (NEON), on the 8275CL (AVX2), on the EPYC (AVX-512, earlier revision as above), and for amd64 under qemu. The two commands of the workflow pass locally with Go 1.27.0. The workflow itself runs on pushes only, so it has not run for this branch.
  • Beyond the suite: a random comparison of And, AndNot, Or, Xor and Count against the generic loops over mixed lengths, with and without extra bitmaps, and a run that calls the kernels from goroutines at 400 stack depths with the profiler and the collector running. Both pass on NEON and AVX2; neither is part of this PR.
  • TestManyStopsAtSourceLength is new. The kernels for several sources take their word count from the destination slice, and no existing test failed when the destination was passed uncut.
  • The panic described in Fix panic in Or/Xor when the other bitmap is empty #58 does not occur here, since the kernels no longer index other[0]. I did not add its tests.

Not run: macOS and Windows (vetted only). The assembly was generated with Ubuntu clang 19.1.7.

The assembly came from a generator that copied the compiler's stack
code as raw bytes into functions declared without a frame, and used
x18 in the file every arm64 system but macOS builds. go vet rejected
the files for their argument sizes. They are now made by
github.com/mhr3/gocc v0.31.2, which codegen/generate.sh runs with
go run: the kernels for several sources declare a real Go frame, x18
is not used, and go vet passes on linux and darwin, amd64 and arm64.

One source for every target

codegen/simd.c replaces the four per-target files. It is compiled for
AVX2, AVX-512 and arm64, and the one arm64 file serves Linux and
macOS, so simd_apple and simd_neon are gone. simd_avx is simd_avx2.

The kernels take slices. For one source that is

    func _and(a, b []uint64)

and for several

    func _and_many(a []uint64, b []*uint64)

where the word count is the length of a and the number of sources the
length of b. dimensionsOf, which packed the two counts into a uint64,
is gone, and pointersOf returns the []*uint64. The callers cut the
destination to the count they used to pass apart;
TestManyStopsAtSourceLength fails if they do not. The C signatures
leave the capacities out with gocc-omit-cap.

AVX2 at -O3

The AVX2 file was built with -O1, the others with -O3. Nothing says
why; the old generator wrote no constant data, and at -O1 the count
loop stays one POPCNT per word and needs none. With -O3 it counts with
a nibble table, and the logic kernels take four vectors per step. On a
Xeon Platinum 8275CL, for bitmaps of a million bits:

    count                  13059 ns -> 3551 ns
    and of four bitmaps     8790 ns -> 7870 ns
    and of two              2640 ns, as before

Below about 16 words the unrolled loops cost more than they save: and
of two bitmaps of 8 words takes 11.2 ns where it took 8.9 ns.

Which processors use AVX-512

Features are read from golang.org/x/sys/cpu instead of a level
computed from klauspost/cpuid. The AVX-512 kernels are used when the
processor has the F, DQ and VPOPCNTDQ subsets. DQ because the 512-bit
VANDPS, VORPS and VXORPS belong to it. VPOPCNTDQ because Count uses
VPOPCNTQ, and because asking for it keeps the path off the first
processors with AVX-512 (Skylake-X, Cascade Lake, Cooper Lake), which
lack it. Those lower the clock of a core that runs any 512-bit
instruction. On the 8275CL, with one short kernel call per piece of
scalar work:

    core clock, AVX2 kernels        3600 MHz
    core clock, AVX-512 kernels     3100 MHz
    the scalar work                 167 us -> 194 us

and the kernels for two bitmaps were 7% slower with AVX-512 than with
AVX2 there. They use the AVX2 kernels.

On an EPYC 9R45, which has the extension, counting a million bits
goes from 4006 ns to 591 ns.

Go 1.27

The generator writes an instruction as text when the Go assembler of
the day knows it, and the arm64 file has VBIC, which Go learned in
1.27. Earlier releases do not assemble it, so go.mod and the workflow
ask for 1.27.

Besides

The feature flags are declared in the build without assembly too, so
the tests compile with the noasm tag and on other architectures, and
the tests put back the flags the package detected instead of working
them out again. FromBytes and ToBytes use unsafe.Slice in place of
the deprecated reflect.SliceHeader. Filter converts the predicate's
answer with a closure the compiler inlines, where it cast the function
value through unsafe.Pointer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant