Pick MSVC -arch: based on CARGO_CFG_TARGET_FEATURE for x86 / x86_64 targets#713
Pick MSVC -arch: based on CARGO_CFG_TARGET_FEATURE for x86 / x86_64 targets#713wildbook wants to merge 1 commit intorust-lang:mainfrom
Conversation
|
Hm, I was under the impression that I586 was intended explicitly for older CPUs that do not have newer instructions so is this not working as intended? Otherwise, how is it different from i686? |
|
It is, but the forced downgrade for For "normal" MSVC it's already working "as intended", it's just that "as intended" in this case means "assume the target supports SSE2 and nothing else" (MSVC's default) for I had issues due to the former part, and implemented the latter while I was at it since I was already working on making it slightly smarter. |
This is my first contribution to cc-rs, and I'm more than open to any critique or questions.
As it is now,
-arch:IA32is passed for 32-bit builds if the target isi586orclang-clis being used. This explicit override often breaks assumptions and always downgrades 32-bitclang-clbuilds from the default-arch:SSE2to-arch:IA32, potentially impacting performance.This PR attempts to only downgrade when there's reason to, and otherwise attempt to pick a target
-archthat matches the features set inCARGO_CFG_TARGET_FEATURE. Picking an-archthat followsCARGO_CFG_TARGET_FEATUREalso means crates (both x86 and x86_64) that opt into support forAVXorAVX2throughtarget-featurewill now also have any dependencies built withccbenefit from that.I'm using
clang-clto build a few 32-bit projects, so I'm most likely running into issues with this far more often than most people are. While usually it's at most a bit annoying thatccbuilds aren't respectingtarget-feature, it actually breaks my builds.Today I ran into this same issue again when trying to integrate
mimalloc_rust, which requiresSSE2for atomics.Since this is the third crate I've attempted to use that has issues due to the same forced override, I decided to try fix the problem in
cc-rsinstead of forking yet another crate just to add.flag_if_supported("-arch:SSE2")to be able to build it successfully.I opted to not add support for AVX512 for now since MSVC bundles all AVX512 extensions under the same
-arch:AVX512, while Rust treats each extension as a separate feature. Adding support for it is possible, but I don't need it myself and I don't know how to do it cleanly, so I'll leave that to someone else. For now it should fallback toAVX2, which is still an upgrade over the previousIA32(clang-cl) /SSE2(cl.exe).It's also possible to do the same thing for ARM builds, but since I have no experience with ARM I'll leave that task too for someone else.