Pre-RFC: is_x86_feature_detected! in core

Summary

Make the is_x86_feature_detected! macro available in core on x86 and x86_64 targets.

Motivation

It's common for crates that only perform computation to sooner or later to get a request to work as no_std crates, so it should be easy for compute-only crates to be no_std crates. Also, (especially when SIMD is used) some crates that perform computation benefit from function multiversioning on x86 and x86_64. Multiversioning builds on the is_x86_feature_detected! macro.

is_x86_feature_detected! being in std but not in core causes a situation where no_std crates that benefit from multiversioning need to bother users of the crate with an std feature to explicitly opt into multiversioning when the user of the crate is actually OK with linking std.

If is_x86_feature_detected! was available in core, no_std crates wouldn't need bother crate users about multiversioning like this.

Guide-level explanation

The is_x86_feature_detected! macro is now available in core.

Reference-level explanation

On targets where executing the cpuid instruction is allowed and its results accurately describe what instruction set extensions are OK to use, core::is_x86_feature_detected! should work like std::is_x86_feature_detected! works today. This applies at least to x86 and x86_64 targets that currently have std in addition to having core.

On special targets without std, where either the cpuid instruction is not allowed or its answers don't correlate with what's OK to use, such as SGX or kernel-mode targets for kernels that e.g. don't save/restore register state across syscalls for all registers relevant to extensions reported as supported by cpuid, the responses of the is_x86_feature_detected! macro must depend on what target_features are statically known to be available rather than on querying cpuid.

Drawbacks

Making is_x86_feature_detected! available in core for targets where it cannot be run-time check using cpuid reduces clarity on what's a run-time check and what's not. (However, it's already the case that the macro promotes statically-answerable queries to static results, so the line is already fuzzy by design.)

Rationale and alternatives

As noted in the Motivation section above, is_x86_feature_detected! being in std complicates things for crates that are conceptually compute-only and otherwise could be no_std.

In practice, the mainstream usage of dispatch based on run-time availability of CPU features is pretty much all on the x86 and x86_64 architectures. Considering that this is the common case and in this common case, the detection works based on an instruction rather than a syscall, the common case should not be inconvenienced by the observation that on some other architectures CPU feature detection requires syscalls and, therefore, belongs in std. The macro already has "x86" in its name and is not available on those other architectures anyway.

That instruction-based detection might not work on special targets isn't a good enough reason to inconveniece common targets. A kernel-mode target might not save/restore FPU register state across syscalls and decide to refrain from using the FPU in kernel mode. Still, this isn't a reason to withhold floating-point functionality from core. Instead "this special target disallows cpuid" should modeled analogously to "this special target wants to refrain from emitting FPU operations".

Still letting is_x86_feature_detected! be available in core in such scenarios is likely more beneficial than withholding it, since having it available promotes code portability into special environments by letting is_x86_feature_detected! to resolve into cfg.

An alternative to moving is_x86_feature_detected! to core is to use the the core_detect crate. This alternative does not fully solve the problem, because if you depend on some other crate that rather mints an std feature than uses core_detect (as is the case with the multiversion crate), you are back to where you started due to dependencies.

Prior art

core_detect crate shows that x86 feature detection in no_std environment is feasible and practical.

core_detect uses code that comes from the standard library. In general, it's a sign of the standard library not meeting the needs of users well, when the relevant code exists in the standard library but is not available for use via the standard library (in relevant configurations) and the issue can be worked around by copying and pasting code from the standard library into a crate.

7 Likes

Hmm, maybe we should do this for as many arches as possible, rather than just x86

For most other architectures there is no cpuid equivalent. Getting supported target features requires asking the OS about it instead which internally reads it from either a device tree or uses a privileged instruction.

1 Like

Perhaps as a future extension, but could a target allowlist x86 features that it wants to let you detect with CPUID? That way, the target could still permit runtime detection via CPUID for things that are optional, without accidentally permitting too much.

For example, a target might want to require SSE2, permit AVX2 if present in this CPU (Intel still released new processors without AVX2 in 2021), but ban AVX-512 to save the ~1.5 KiB of AVX-512 state in the XSAVE area.

For this we need something like this:

For "most", but not all. For example, s390x has stfle.

I don't see why.

Without that, a library that uses is_x86_feature_detected! has to handle three cases:

  1. This target has this feature unconditionally enabled, and is_x86_feature_detected! is a constant true.
  2. This target detects this feature at runtime, and is_x86_feature_detected! is a runtime value.
  3. This target has this feature unconditionally disabled, and is_x86_feature_detected! is a constant false.

At the moment, the targets get split into two:

  1. This target has some features unconditionally enabled, and some detected at runtime.
  2. This target has some features unconditionally enabled, and the rest unconditionally disabled.

I'm suggesting that because any library that cares has to handle all three cases anyway, it's not that big a pain to change the targets so that all targets have some features unconditionally enabled, some detected at runtime, and some unconditionally disabled, based on the idea that a target can choose to filter the set of features that can be detected at runtime.

1 Like

As discussed in the proposal, in the current version of Rust it's not possible. When you pass -C target-feature=-foo it does not result in a constant false for is_x86_feature_detected!("foo"), so you get the second case. This is especially problematic with soft float targets, see: Deal with ABI-incompatible features by considering them "never enabled" · Issue #160301 · rust-lang/rust · GitHub

But that's not something I'm changing from the original proposal - my suggestion is that this case (which is already in the original proposal) is changed to allow an allow list of features that are runtime detected, not just a list of always disabled features.

The original proposal says:

This means that the is_x86_feature_detected! proposal will require libraries to think about this case, whether or not we extend "statically known to be available" to "statically known to be available or on an allow list of features that can be queried".

On "special" targets like SGX (and Miri) is_x86_feature_detected! just falls back to compile-time target feature detection. Technically, nothing prevents you from writing your own runtime target feature detection code (e.g. using external flags) and using "disabled" features based on it. Currently Rust simply does not have a notion of allow/deny target feature lists, all target features are simply in the "allow" list.

I understand that.

The bit that you're not explaining clearly is why it's OK for libraries to handle is_x86_feature_detected! as fully compile time, can return a compile-time false or a compile time true (x86_64-fortanix-unknown-sgx), and OK for libraries to handle is_x86_feature_detected! as a mix of compile time true and runtime true or false (x86_64-unknown-linux-gnu), but not OK for libraries to handle is_x86_feature_detected! as a mix of compile time true or false and runtime true or false.

I simply do not see why there's a second change needed if is_x86_feature_detected! can return a compile time false on targets where it can also return a runtime false (or true, depending on the hardware it's running on).

That feels like an orthogonal issue that already exists because we have targets where it can be a compile time true, or a runtime value, and targets where it can be a compile time true or false already, and if you're handling both of those sets of targets, then you should also be able to handle targets where it can be a compile time true or false or a runtime value.

This does not by itself prevent us from doing it in core. Core is still target-specific, and is allowed to make some syscalls, or talk to the VDSO. If doing the equivalent of cpuid requires asking the OS, we can ask the OS.

1 Like

That seems quite dubious to me, what if someone wants to reimplement parts for libc, e.g. for interposition, or strictly sandboxed code that gets killed if it makes calls it's not supposed to? I'm pretty sure that are use-cases out there that rely on core not talking to external stuff.

We already have documentation that core makes very few calls, and none of them talk to the OS.

I think EIIs or - if we can't get the overhead of that down - some static that may or may not require manual initialization depending on environment, would be a better approach.

2 Likes