Skip to content

Conversation

@sayantn
Copy link
Contributor

@sayantn sayantn commented Oct 5, 2025

Counterproposal to rust-lang/rust#146560

This makes __cpuid, __cpuid_count and __get_cpuid_max safe to use. All of these just call the CPUID instruction, so it is safe to call whenever the CPUID instruction is available (it has defined behavior even if unsupported leaf values are passes). There are cases when it might not be available

  • processors lower than i386, which afaik rust doesn't support anymore. The has_cpuid function was removed with this exact motivation, and it is justified
  • SGX environments can't use CPUID. Or more accurately, they can't trust the output of CPUID. It should also be mentioned that SGX has been deprecated by intel for quite some time now.

As these are very niche cases we propose to make cpuid safe in general.

Alternatives

Leave it unsafe

This is always an option. But for most use cases using __cpuid is perfectly safe, so it is inconvenient (and more so for projects that ban uses of unsafe).

Introduce a cpuid target feature, and take advantage of target_feature_11

This is what rust-lang/rust#146560 does. As all X86 target features (other than soft_float and x87) were introduced after CPUID, we can add a detection-only target feature cpuid that is implied by all X86 target features. This makes using it in i686 or x86_64 targets safe due to sse being auto-enabled in the targe description, whereas i386 remains unsafe as it doesn't enable sse

@rustbot
Copy link
Collaborator

rustbot commented Oct 5, 2025

r? @folkertdev

rustbot has assigned @folkertdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@traviscross
Copy link

traviscross commented Oct 5, 2025

Proposing FCP on this (in fact, even nominating it) is blocked on:

@traviscross traviscross added T-lang Relevant to the language team. I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. T-libs-api needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. labels Oct 13, 2025
@traviscross
Copy link

@sayantn, if you could please update the PR description with the details of the stabilization being proposed, the motivation, etc., that would be helpful to us in considering this nomination and for proposing FCP.

@joshtriplett
Copy link
Member

👍 for this. One clarification:

processors lower than i386

For the avoidance of ambiguity: lower than i586, which is lower than the current 32-bit x86 targets in Rust.

@traviscross
Copy link

Thanks @sayantn. Le'ts propose to do it.

@rfcbot fcp merge

@rust-rfcbot
Copy link
Collaborator

rust-rfcbot commented Oct 15, 2025

Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This PR / issue is in pFCP or FCP with a disposition to merge it. labels Oct 15, 2025
@tmandry
Copy link
Member

tmandry commented Oct 15, 2025

We discussed this in the lang team meeting today and the agreement was that if we add support for an older target like i586, we can make this unsafe and require target_feature for the intrinsic on that target. These intrinsics are already platform-specific, so it's natural to have a more target-specific definition that changes the safety of the intrinsic.

@rfcbot reviewed

@nikomatsakis
Copy link

@rfcbot reviewed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This PR / issue is in pFCP or FCP with a disposition to merge it. I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. T-lang Relevant to the language team. T-libs-api

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants