Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
codegen/generate.shrunsgithub.com/mhr3/gocc/cmd/gocc@v0.31.2withgo run. The kernels for several sources now declare a real Go frame. The current files copy the compiler's pushes andsub rspas raw bytes into functions declared with a zero-size frame, and the Linux arm64 file usesx18, which is reserved on Windows and Android.go vetfails onmainfor the declared argument sizes and passes here.codegen/simd.creplaces the four per-target files and is compiled for AVX2, AVX-512 and arm64. One arm64 file serves Linux and macOS, sosimd_apple.*andsimd_neon.*go away, andsimd_avx.*becomessimd_avx2.*.(a, b []uint64)and, for several sources,(a []uint64, b []*uint64).dimensionsOfis gone andpointersOfreturns the[]*uint64.Count()on AVX-512 usesVPOPCNTQ. This covers what Use the accelerated Count on AVX-512 CPUs #53 and Count() not using fast path on AVX512 CPUs #46 are about.-O3like the other targets (it was-O1), so its count kernel is vectorised too.FromBytes/ToBytesuseunsafe.Slicein place of the deprecatedreflect.SliceHeader, andFilterno longer casts the predicate throughunsafe.Pointer.Things to decide before merging
VBIC, which the assembler accepts from 1.27.0. Go 1.21 to 1.26.4 fail withunrecognized instruction "VBIC".go.modand the workflow are updated accordingly.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.-O3the loops are unrolled:Andof 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
mainMedians of 8 interleaved runs of the repository benchmarks (1M-bit bitmaps), in ns,
main→ this branch.countand,andnot,or,xorand4-batch,or4-batch,xor4-batchor4-batch-ragged,xor4-batch-raggedTesting
go vetis clean for linux and darwin on amd64 and arm64, with and withoutnoasm.And,AndNot,Or,XorandCountagainst 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.TestManyStopsAtSourceLengthis 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.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.