-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: x/sys/cpu #24843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Sounds good to me. Thanks for filing this. The whole CPU detection situation has been getting increasingly messy. I would also be fine with putting a But I'm not sure what the point of even using Maybe we just put it in |
A non-internal |
Can't we make |
Oh, yes - my bad. Didn't recognize the |
Thanks for the proposal @aead. Creating x/arch/cpu and vendoring it where nedded sounds reasonable to me. |
Thanks for proposing this. Can't come soon enough - I literally wrote another piece of CPU-detection code bound for x/crypto last night. |
Sorry for the bike shedding, but right now x/arch contains tools used to develop for the architecture. Adding a library that executes at run time is an uneasy fit. Another option would be x/sys/cpu. |
That wouldn't bother me. Almost every x/ repo is a mix of tools and packages. But I'm also fine with x/sys. |
Okay, I can submit a first proposal for a |
Change https://golang.org/cl/107015 mentions this issue: |
Just wanted to point out that intel has a cpuid package with a more complete x86 cpu feature list and it's BSD 3-Clause licensed. https://github.com/intel-go/cpuid |
@AlexRouSg Thanks for the hint. I'm aware of this. However the standard library and the |
Sorry for bike shedding as well, but we could argue similarly that In any case, I'm fine with either |
@aead Maybe ask intel if they want to contribute to a |
Hm, while both
|
@AlexRouSg |
I do like how the
This wouldn't compile under option 2. |
Option 1 also has the advantage of nice, consistent godoc for everybody, without tweaking environment variables and/or URL parameters. Edit: oh, @aead already mentioned that. |
I also like option 1. It allows for something like:
|
Could we just move internal/cpu to be public in the std lib or would e.g. a update every 6 months be considered to slow for introduction of new features? The advantage i see is we have one place for crypto/runtime/std lib to source flags going into the future and one way to e.g. mask flags for all tests. Seems possible to still consider this for 1.11. |
@martisch Making
|
Didn't thought about the "fast-branch" use case where implementations are build not only on certain archs - like in a |
I think x/ supports the last 2-3 versions of go (correct me if i am wrong). So what i was thinking is we could make cpu public in standard lib now. In the mean time vendor a copy into x/crypto/internal which will be removed again in 2-3 versions and from then on x/crypto can rely on using the standard lib version. Not sure if that corresponds to @aead second option "Create a cpu package under /x/* and vendor it into the standard lib - like already done for e.g. chacha20poly1305.". The downside is that any new feature flags (AVX512) can take months/years to be usable by crypto with full backwards compatibility. One problem i see with vendoring |
I'm not completely sure about
Usually - if we write special assembly code - we should avoid raw
Completely agree here 👍 |
We remove support for older things when it becomes inconvenient, but we don't proactively break older versions just because we can. |
If I understand correctly, the main disadvantage of making
but that needn't be true. I'd like to suggest an alternative approach, similar to some other suggestions:
This deduplicates code within
@ianlancetaylor The approach would essentially force the runtime to expose the |
@tmthrgd When I say "some hack" I specifically mean some mechanism that does not invoke the Go 1 compatibility guarantee. As the Go 1 guarantee only operates at the source code level, there are a couple of such hacks available. On the other hand promoting internal/cpu to be a visible standard library package would invoke the Go 1 compatibility guarantee. Our experience with the syscall package suggests that that would be less than ideal in the long run. |
This CL introduces a new cpu package for CPU/platform feature detection. The cpu package is basically a copy of `internal/cpu` of the standard library. Revision: bf86aec25972f3a100c3aa58a6abcbcc35bdea49 This CL does not export ARM64 and PPC64 feature detection since at the moment ARM64/PPC64 requires standard library support. Updates golang/go#24843 Change-Id: I11bc1ca60b116e902c941b5887c00870dbb1f899 Reviewed-on: https://go-review.googlesource.com/107015 Reviewed-by: Brad Fitzpatrick <[email protected]>
Change https://golang.org/cl/110355 mentions this issue: |
Change https://golang.org/cl/110796 mentions this issue: |
CL 110355 switched out the adhoc cpu feature detection for x/sys/cpu, in doing so the AVX2 check was broken. The assembly code uses MULX which is part of BMI2. Updates golang/go#24843 Change-Id: I4719b8ff3211eb1c823099512e593e540d6f3be8 GitHub-Last-Rev: 70542b5 GitHub-Pull-Request: #44 Reviewed-on: https://go-review.googlesource.com/110796 Reviewed-by: Tobias Klauser <[email protected]>
This change updates the vendored copy of golang.org/x/crypto to commit 1a580b3. An import of golang.org/x/sys/cpu was replaced with an import of internal/cpu as required by #24843 (comment). The following bash command can be used to replicate this import update: find `pwd` -name '*.go' -exec sed -i 's/golang\.org\/x\/sys\/cpu/internal\/cpu/g' '{}' \; Change-Id: Ic80d361f940a96c70e4196f594d791c63421d73c Reviewed-on: https://go-review.googlesource.com/113175 Reviewed-by: Brad Fitzpatrick <[email protected]>
Change https://golang.org/cl/177897 mentions this issue: |
… detection This change removes package specific CPU-feature detection code and replaces it with x/sys/cpu. Fixes golang/go#24843 Change-Id: I150dd7b3aeb8eef428c91f9b1df741ceb8a87a24 Reviewed-on: https://go-review.googlesource.com/110355 Run-TryBot: Ilya Tocar <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
… detection This change removes package specific CPU-feature detection code and replaces it with x/sys/cpu. Fixes golang/go#24843 Change-Id: I150dd7b3aeb8eef428c91f9b1df741ceb8a87a24 Reviewed-on: https://go-review.googlesource.com/110355 Run-TryBot: Ilya Tocar <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
… detection This change removes package specific CPU-feature detection code and replaces it with x/sys/cpu. Fixes golang/go#24843 Change-Id: I150dd7b3aeb8eef428c91f9b1df741ceb8a87a24 Reviewed-on: https://go-review.googlesource.com/110355 Run-TryBot: Ilya Tocar <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
… detection This change removes package specific CPU-feature detection code and replaces it with x/sys/cpu. Fixes golang/go#24843 Change-Id: I150dd7b3aeb8eef428c91f9b1df741ceb8a87a24 Reviewed-on: https://go-review.googlesource.com/110355 Run-TryBot: Ilya Tocar <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
… detection This change removes package specific CPU-feature detection code and replaces it with x/sys/cpu. Fixes golang/go#24843 Change-Id: I150dd7b3aeb8eef428c91f9b1df741ceb8a87a24 Reviewed-on: https://go-review.googlesource.com/110355 Run-TryBot: Ilya Tocar <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
… detection This change removes package specific CPU-feature detection code and replaces it with x/sys/cpu. Fixes golang/go#24843 Change-Id: I150dd7b3aeb8eef428c91f9b1df741ceb8a87a24 Reviewed-on: https://go-review.googlesource.com/110355 Run-TryBot: Ilya Tocar <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
… detection This change removes package specific CPU-feature detection code and replaces it with x/sys/cpu. Fixes golang/go#24843 Change-Id: I150dd7b3aeb8eef428c91f9b1df741ceb8a87a24 Reviewed-on: https://go-review.googlesource.com/110355 Run-TryBot: Ilya Tocar <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
This is a proposal for unifying the CPU detection for
/x/crypto
similar to the standard libinternal/cpu
.Current situation
Currently different crypto implementations detect CPU features differently:
This has lead to asm code which was copied over and over again. Usually we try to avoid asm code if possible.
Why not use
internal/cpu
and the linkerThis CL tried to use the linker to reuse
internal/cpu
. But this approach does not work sincex/crypto
must also work with older Go releases and causes a performance regression at the moment.Possible Solution
One possible solution would be an internal package (e.g.
x/crypto/internal/cpu
) which contains functionality to detect CPU/platform specific features similar tointernal/cpu
in the standard library.This package would be vendored into the standard library because
chacha20poly1305
is used bycrypto/tls
.Using the linker approach showing in CL-106336 it's not required that
internal/cpu
duplicatesx/crypto/internal/cpu
. Instead we can use//go:linkname
to instruct the linker to bind vars ininternal/cpu
tox/crypto/internal/cpu
. This way both cpu packages only have to export the same API but the CPU feature detection (asm code) would be only inx/crypto/internal/cpu
.So this proposal would:
x/crypto/internal/cpu
.internal/cpu
tox/crypto/internal/cpu
.internal/cpu
but keep the export API.x/crypto/internal/cpu
.x/crypto/internal/cpu
into standard library (required bychacha20poly1305
)//go:linkname
directive to bind theinternal/cpu
API tox/crypto/internal/cpu
.Remark
While it possible to extract the package
x/crypto/internal/cpu
tox/internal/cpu
using the same approach described above it's not clear whether this is needed. AFAIK onlyx/crypto
uses asm code depending on the CPU/platform features.Ref: #24828
Also related: #24813
/cc @ianlancetaylor @tklauser @FiloSottile @gtank
The text was updated successfully, but these errors were encountered: