|
| 1 | +//! Runtime CPU feature detection and compatibility guard for SIMD-tuned |
| 2 | +//! binaries. The compile-time build flavour (OS, arch, SIMD level, git hash, |
| 3 | +//! build timestamp) is assembled into `VERSION_BODY` by `build.rs` and read |
| 4 | +//! via `env!("VERSION_BODY")`. |
| 5 | +
|
| 6 | +use anyhow::{Result, bail}; |
| 7 | + |
| 8 | +// ============================================================================ |
| 9 | +// Binary target (compile-time) |
| 10 | +// ============================================================================ |
| 11 | + |
| 12 | +/// Returns the microarchitecture level this binary was compiled for. |
| 13 | +pub fn binary_target() -> &'static str { |
| 14 | + #[cfg(target_arch = "x86_64")] |
| 15 | + { |
| 16 | + if cfg!(target_feature = "avx512f") { |
| 17 | + "x86-64-v4" |
| 18 | + } else if cfg!(target_feature = "avx2") { |
| 19 | + "x86-64-v3" |
| 20 | + } else { |
| 21 | + "x86-64" |
| 22 | + } |
| 23 | + } |
| 24 | + #[cfg(target_arch = "aarch64")] |
| 25 | + { |
| 26 | + if cfg!(target_feature = "sve") { |
| 27 | + "aarch64-neoverse-v1" |
| 28 | + } else { |
| 29 | + "aarch64" |
| 30 | + } |
| 31 | + } |
| 32 | + #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))] |
| 33 | + { |
| 34 | + "unknown" |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// ============================================================================ |
| 39 | +// Runtime CPU feature detection |
| 40 | +// ============================================================================ |
| 41 | + |
| 42 | +/// Detects CPU features available at runtime, ordered most → least capable. |
| 43 | +pub fn detected_features() -> Vec<&'static str> { |
| 44 | + let mut features = Vec::new(); |
| 45 | + |
| 46 | + #[cfg(target_arch = "x86_64")] |
| 47 | + { |
| 48 | + if is_x86_feature_detected!("avx512f") { |
| 49 | + features.push("AVX-512"); |
| 50 | + } |
| 51 | + if is_x86_feature_detected!("avx2") { |
| 52 | + features.push("AVX2"); |
| 53 | + } |
| 54 | + if is_x86_feature_detected!("sse4.2") { |
| 55 | + features.push("SSE4.2"); |
| 56 | + } |
| 57 | + if is_x86_feature_detected!("popcnt") { |
| 58 | + features.push("POPCNT"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + #[cfg(target_arch = "aarch64")] |
| 63 | + { |
| 64 | + if std::arch::is_aarch64_feature_detected!("sve2") { |
| 65 | + features.push("SVE2"); |
| 66 | + } |
| 67 | + if std::arch::is_aarch64_feature_detected!("sve") { |
| 68 | + features.push("SVE"); |
| 69 | + } |
| 70 | + features.push("NEON"); |
| 71 | + } |
| 72 | + |
| 73 | + features |
| 74 | +} |
| 75 | + |
| 76 | +// ============================================================================ |
| 77 | +// Upgrade hint |
| 78 | +// ============================================================================ |
| 79 | + |
| 80 | +/// Returns an upgrade suggestion if a faster binary variant is available for |
| 81 | +/// the current CPU, `None` if this binary is already optimal. |
| 82 | +pub fn upgrade_hint() -> Option<String> { |
| 83 | + #[cfg(target_arch = "x86_64")] |
| 84 | + { |
| 85 | + let has_avx512 = is_x86_feature_detected!("avx512f"); |
| 86 | + let has_avx2 = is_x86_feature_detected!("avx2"); |
| 87 | + let target = binary_target(); |
| 88 | + |
| 89 | + if target == "x86-64" && has_avx512 { |
| 90 | + return Some("Use the x86-64-v4 build for best performance.".to_string()); |
| 91 | + } |
| 92 | + if target == "x86-64" && has_avx2 { |
| 93 | + return Some("Use the x86-64-v3 build for better performance.".to_string()); |
| 94 | + } |
| 95 | + if target == "x86-64-v3" && has_avx512 { |
| 96 | + return Some("Use the x86-64-v4 build for best performance.".to_string()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + #[cfg(all(target_arch = "aarch64", target_os = "linux"))] |
| 101 | + { |
| 102 | + let has_sve = std::arch::is_aarch64_feature_detected!("sve"); |
| 103 | + let target = binary_target(); |
| 104 | + |
| 105 | + if target == "aarch64" && has_sve { |
| 106 | + return Some("Use the aarch64-neoverse-v1 build for better performance.".to_string()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // No runtime hint for macOS aarch64: the `apple-m1` variant is a compiler |
| 111 | + // tuning target, not a distinct CPU feature set, so runtime detection |
| 112 | + // can't tell the two builds apart. |
| 113 | + |
| 114 | + None |
| 115 | +} |
| 116 | + |
| 117 | +// ============================================================================ |
| 118 | +// CPU compatibility guard |
| 119 | +// ============================================================================ |
| 120 | + |
| 121 | +/// Checks that the CPU supports the features required by this binary. |
| 122 | +/// Call at the very start of `main()` — before any SIMD code runs — to give |
| 123 | +/// a friendly error instead of a SIGILL crash. |
| 124 | +pub fn check_cpu_compat() -> Result<()> { |
| 125 | + #[cfg(target_arch = "x86_64")] |
| 126 | + match binary_target() { |
| 127 | + "x86-64-v4" if !is_x86_feature_detected!("avx512f") => bail!( |
| 128 | + "This ruSTAR binary was compiled for x86-64-v4 (AVX-512) but your CPU \ |
| 129 | + does not support AVX-512.\nPlease use the x86-64-v3 or baseline build instead.\n\ |
| 130 | + See: https://github.com/Psy-Fer/ruSTAR#installation" |
| 131 | + ), |
| 132 | + "x86-64-v3" if !is_x86_feature_detected!("avx2") => bail!( |
| 133 | + "This ruSTAR binary was compiled for x86-64-v3 (AVX2) but your CPU \ |
| 134 | + does not support AVX2.\nPlease use the baseline build instead.\n\ |
| 135 | + See: https://github.com/Psy-Fer/ruSTAR#installation" |
| 136 | + ), |
| 137 | + _ => {} |
| 138 | + } |
| 139 | + |
| 140 | + #[cfg(target_arch = "aarch64")] |
| 141 | + if binary_target() == "aarch64-neoverse-v1" && !std::arch::is_aarch64_feature_detected!("sve") { |
| 142 | + bail!( |
| 143 | + "This ruSTAR binary was compiled for aarch64-neoverse-v1 (SVE) but your CPU \ |
| 144 | + does not support SVE.\nPlease use the baseline aarch64 build instead.\n\ |
| 145 | + See: https://github.com/Psy-Fer/ruSTAR#installation" |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + Ok(()) |
| 150 | +} |
| 151 | + |
| 152 | +/// One-line summary of CPU features detected at runtime. No upgrade hint. |
| 153 | +/// Example: `CPU: AVX2 SSE4.2 POPCNT detected` |
| 154 | +pub fn cpu_detected_line() -> String { |
| 155 | + let features = detected_features(); |
| 156 | + if features.is_empty() { |
| 157 | + "CPU: none detected".to_string() |
| 158 | + } else { |
| 159 | + format!("CPU: {} detected", features.join(" ")) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +#[cfg(test)] |
| 164 | +mod tests { |
| 165 | + use super::*; |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn binary_target_is_not_empty() { |
| 169 | + assert!(!binary_target().is_empty()); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn version_body_contains_expected_fields() { |
| 174 | + let body = env!("VERSION_BODY"); |
| 175 | + assert!(body.contains(env!("GIT_SHORT_HASH"))); |
| 176 | + assert!(body.contains("built ")); |
| 177 | + } |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn cpu_detected_line_starts_with_cpu() { |
| 181 | + assert!(cpu_detected_line().starts_with("CPU:")); |
| 182 | + } |
| 183 | + |
| 184 | + #[test] |
| 185 | + fn check_cpu_compat_passes_on_current_hardware() { |
| 186 | + assert!(check_cpu_compat().is_ok()); |
| 187 | + } |
| 188 | +} |
0 commit comments