rust-lang / rustc_codegen_cranelift

Cranelift based backend for rustc
Apache License 2.0
1.62k stars 101 forks source link

Emulate `cpuid` using C/Rust library? #1053

Closed playXE closed 4 years ago

playXE commented 4 years ago

I believe it's possible to link output binary with some C/Rust dylib that will have function that provides cpuid implementation, this way it will be possible to compile crates that use cpuid.

bjorn3 commented 4 years ago

https://github.com/bjorn3/rustc_codegen_cranelift/blob/a3cd293148e0678074cbbec06e5d3e9159a14ea0/patches/0016-Disable-cpuid-intrinsic.patch pretends that __cpuid is not available by returning false from has_cpuid. You are supposed to check this before calling __cpuid, as there are other cases where __cpuid is not available, like running in an sgx enclave or no sse support on the cpu.

Making __cpuid call a function implemented in C would be possible. This would however break almost all SIMD using crates, as many SIMD intrinsics are not yet implemented. Because has_cpuid returns false, most is_x86_feature_detected calls return false, which in many cases causes those crates to fallback to a SIMD free version, or use a version that only uses SIMD intrinsics already implemented in cg_clif.