Skip to content

Conversation

sayantn
Copy link
Contributor

@sayantn sayantn commented Sep 14, 2025

Tracking issue: #146558

r? @Amanieu

@rustbot label O-x86_32 O-x86_64 A-target-feature T-compiler T-libs-api

@rustbot
Copy link
Collaborator

rustbot commented Sep 14, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

stdarch is developed in its own repository. If possible, consider making this change to rust-lang/stdarch instead.

cc @Amanieu, @folkertdev, @sayantn

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. A-target-feature Area: Enabling/disabling target features like AVX, Neon, etc. O-x86_32 Target: x86 processors, 32 bit (like i686-*) (also known as IA-32, i386, i586, i686) O-x86_64 Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64) T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Sep 14, 2025
@rust-log-analyzer

This comment has been minimized.

@sayantn sayantn force-pushed the cpuid-target-feature branch from 8282246 to d1b9074 Compare September 14, 2025 23:32
@sayantn sayantn force-pushed the cpuid-target-feature branch from d1b9074 to 5372c28 Compare September 15, 2025 18:43
@rust-log-analyzer

This comment has been minimized.

 - Make `cpuid` and friends safe with `#[target_feature(enable = "cpuid")]`
 - Update documentation
@sayantn sayantn force-pushed the cpuid-target-feature branch from 5372c28 to 38e000a Compare September 15, 2025 20:28
@rustbot
Copy link
Collaborator

rustbot commented Sep 15, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Amanieu Amanieu added T-lang Relevant to the language team and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 15, 2025
@Amanieu
Copy link
Member

Amanieu commented Sep 15, 2025

This PR adds a new cpuid target feature which allows the use of the CPUID intrinsics in stdarch (implemented using inline asm). This doesn't have any codegen impact (it's not passed through to LLVM), but allows the CPUID intrinsics to be made safe using target features 1.1.

Since CPUID support is implicitly enabled on most of our x86 targets, this effectively makes the CPUID intrinsic safe to call even before the cpuid target feature is enabled, hence the FCP.

@rfcbot merge

@rust-rfcbot
Copy link
Collaborator

rust-rfcbot commented Sep 15, 2025

Team member @Amanieu 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 issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 15, 2025
@traviscross traviscross added I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Sep 15, 2025
@traviscross
Copy link
Contributor

cc @RalfJung

@RalfJung
Copy link
Member

I am a bit surprised to see this exposed as a target feature. The tracking issue says

  • very old CPUs don't support it
  • target_env = "sgx" doesn't support it

However, even very old CPUs will have x87, right? So it seems like this will be enabled there?
And for SGX, this amounts to a promise that SGX will never support any target feature. Is that truly a promise we can make?

Is the CPUID instruction actually unsafe in those situations? If it reliably segfaults, it would not be unsafe (as long as the inline asm block doesn't claim it will always return).

@joboet
Copy link
Member

joboet commented Sep 16, 2025

Is the CPUID instruction actually unsafe in those situations? If it reliably segfaults, it would not be unsafe (as long as the inline asm block doesn't claim it will always return).

The Intel reference says that

In earlier IA-32 processors that do not support the CPUID instruction, execution of the instruction results in an invalid opcode (#UD) exception being generated.

so yes, it will reliably abort the program. This could result in problems though if LLVM decides to add a cpuid intrinsic at some point if it assumes it to work unconditionally...

On SGX, cpuid is supported, but the results cannot be trusted so it should not be used in most cases.

@RalfJung
Copy link
Member

This could result in problems though if LLVM decides to add a cpuid intrinsic at some point if it assumes it to work unconditionally...

LLVM is not involved here at all, cpuid is proposed as a Rust-only target feature.

@joboet
Copy link
Member

joboet commented Sep 16, 2025

This could result in problems though if LLVM decides to add a cpuid intrinsic at some point if it assumes it to work unconditionally...

LLVM is not involved here at all, cpuid is proposed as a Rust-only target feature.

@RalfJung
Copy link
Member

RalfJung commented Sep 16, 2025

I don't think hypothetical future LLVM extensions are that relevant here. Even if that happens, we can just keep using the inline asm version.

On SGX, cpuid is supported, but the results cannot be trusted so it should not be used in most cases.

Hm, interesting. I don't think we have used target features for "should not be used" before. Also this does not resolve the problem of "what if we ever want to enable any of the other target features (that imply cpuid) in SGX". Like, is it so inconceivable that SGX might get access to vector extensions one day?

To be clear, I don't have a strong opinion either way, this just feels like an "odd" target feature and I couldn't find much discussion of why it is a target feature in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-target-feature Area: Enabling/disabling target features like AVX, Neon, etc. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-lang-nominated Nominated for discussion during a lang team meeting. O-x86_32 Target: x86 processors, 32 bit (like i686-*) (also known as IA-32, i386, i586, i686) O-x86_64 Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64) P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-lang Relevant to the language team T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants