Skip to content
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

proposal: x/sys/cpu: expose runtime AMD64 microarchitecture level #58015

Open
dolmen opened this issue Jan 26, 2023 · 5 comments
Open

proposal: x/sys/cpu: expose runtime AMD64 microarchitecture level #58015

dolmen opened this issue Jan 26, 2023 · 5 comments
Labels
Milestone

Comments

@dolmen
Copy link
Contributor

dolmen commented Jan 26, 2023

In package golang.org/x/sys/cpu add an int symbol [0-4] (0 if not AMD64) that exposes the AMD64 microarchitecture level of the machine running the program.

References:

@gopherbot gopherbot added this to the Proposal milestone Jan 26, 2023
@mvdan
Copy link
Member

mvdan commented Jan 26, 2023

Worth noting that this information is already available as a build tag, like //go:build amd64.v3. Are there cases where an integer value would help?

Also, would this be a constant or a variable? I imagine a constant would be better, given that it's known at build time.

@ianlancetaylor
Copy link
Contributor

@mvdan If I understand correctly the value is not known at build time. At build time we know the minimum supported microarchitecture level, from the GOAMD64 environment variable. But at run time we know the actual microarchitecture level, which must be at least as high but may be higher. Similarly, a build tag doesn't help here.

@mvdan
Copy link
Member

mvdan commented Jan 26, 2023

Ah, this api would actually fetch the level of the machine running the program, which might well be higher than the one the binary was built for. I see the distinction now.

@dolmen dolmen changed the title proposal: x/sys/cpu: expose AMD64 microarchitecture level proposal: x/sys/cpu: expose runtime AMD64 microarchitecture level Feb 1, 2023
@martisch
Copy link
Contributor

martisch commented Feb 14, 2023

I think generally its better to directly spell out the specific features that should gate an optimized code path at runtime instead of using microachitectural level. That way optimized code might be triggered more often as it is not conditional on the support of some instructions it doesnt use.

e.g. LZCNT, BMI can be available without AVX2 support.

example code used with internal/cpu:

var useAVX2 = cpu.X86.HasAVX2 && cpu.X86.HasBMI1 && cpu.X86.HasBMI2

@CAFxX
Copy link
Contributor

CAFxX commented May 26, 2023

A use case for this is selecting one among multiple binaries compiled for different GOAMD64 values. I currently use klauspost/cpuid for this exact purpose in https://github.com/CAFxX/mgo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

6 participants