Make __cpuid_count()
a pure function
#147961
Open
+2
−2
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.
Crates like
raw_cpuid
usecore::arch::x86_64::__cpuid_count()
to determine x86 CPU information. It's great that core provides such a function, instead of having to write inline assembly everywhere; but core's implementation does not use theasm!
attributespure
andnomem
. This means that calls to__cpuid_count()
can't be elided or deduplicated. I'm writing some target-feature enhancement code (akin tomultiversion
), and I'd like to rely on CPUID getting optimized away appropriately.The lack of these attributes was probably an oversight; I looked through the Git history and couldn't see any discussion of the purity of the instruction. While CPUID can have some less-than-pure effects (it acts like a strong memory fence), that's not the primary use case for it -- users who explicitly rely on those effects should write inline assembly where they can be more explicit about it.
In a future where CPUID is an LLVM intrinsic, this wouldn't matter, and LLVM would presumably be able to determine the purity of the intrinsic automatically. But until then, marking it as pure would be a nice optimization hint.
If there are concerns that users may wish to rely on the mild side effects of CPUID, or if there are genuine side effects I wasn't aware of, perhaps a
__pure_cpuid_count()
instruction could be added. I think it's better to make__cpuid_count()
pure, but I'll leave that decision to the reviewer.