Skip to content

Commit

Permalink
build: only enable simd on targets which feature SSE
Browse files Browse the repository at this point in the history
While odd, it is possible for x86_64 to not have SSE enabled. In that
case, we want to avoid enabling the explicit SIMD options, as it will
result in a compilation failure. We address this by checking the CPU
target features at compile time. If SSE isn't in them, then we don't
compile the SIMD stuff.

We don't need to do this for SSE4.2 or AVX since they are selected at
runtime. For SSE2, the code assumes it exists for x86_64.

Fixes #57, Closes #77
  • Loading branch information
mkroening authored and BurntSushi committed Oct 28, 2020
1 parent 153e3af commit 3f9b977
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ fn main() {
enable_libc();
}

// This adds various simd cfgs if this compiler supports it.
// This adds various simd cfgs if this compiler and target support it.
//
// This can be disabled with RUSTFLAGS="--cfg memchr_disable_auto_simd", but
// this is generally only intended for testing.
//
// On targets which don't feature SSE2, this is disabled, as LLVM wouln't know
// how to work with SSE2 operands. Enabling SSE4.2 and AVX on SSE2-only targets
// is not a problem. In that case, the fastest option will be chosen at
// runtime.
fn enable_simd_optimizations() {
if is_env_set("CARGO_CFG_MEMCHR_DISABLE_AUTO_SIMD") {
if is_env_set("CARGO_CFG_MEMCHR_DISABLE_AUTO_SIMD")
|| !target_has_feature("sse2")
{
return;
}
println!("cargo:rustc-cfg=memchr_runtime_simd");
Expand Down Expand Up @@ -59,3 +66,9 @@ fn is_feature_set(name: &str) -> bool {
fn is_env_set(name: &str) -> bool {
env::var_os(name).is_some()
}

fn target_has_feature(feature: &str) -> bool {
env::var("CARGO_CFG_TARGET_FEATURE")
.map(|features| features.contains(feature))
.unwrap_or(false)
}

0 comments on commit 3f9b977

Please sign in to comment.