polars_utils/
cpuid.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// So much conditional stuff going on here...
#![allow(dead_code, unreachable_code, unused)]

use std::sync::OnceLock;

#[cfg(target_arch = "x86_64")]
use raw_cpuid::CpuId;

#[cfg(target_feature = "bmi2")]
#[inline(never)]
#[cold]
fn detect_fast_bmi2() -> bool {
    let cpu_id = CpuId::new();
    let vendor = cpu_id.get_vendor_info().expect("could not read cpu vendor");
    if vendor.as_str() == "AuthenticAMD" || vendor.as_str() == "HygonGenuine" {
        let features = cpu_id
            .get_feature_info()
            .expect("could not read cpu feature info");
        let family_id = features.family_id();

        // Hardcoded blacklist of known-bad AMD families.
        // We'll assume any future releases that support BMI2 have a
        // proper implementation.
        !(0x15..=0x18).contains(&family_id)
    } else {
        true
    }
}

#[inline(always)]
pub fn has_fast_bmi2() -> bool {
    #[cfg(target_feature = "bmi2")]
    {
        static CACHE: OnceLock<bool> = OnceLock::new();
        return *CACHE.get_or_init(detect_fast_bmi2);
    }

    false
}

#[inline]
pub fn is_avx512_enabled() -> bool {
    #[cfg(target_arch = "x86_64")]
    {
        static CACHE: OnceLock<bool> = OnceLock::new();
        return *CACHE.get_or_init(|| {
            if !std::arch::is_x86_feature_detected!("avx512f") {
                return false;
            }

            if std::env::var("POLARS_DISABLE_AVX512")
                .map(|var| var == "1")
                .unwrap_or(false)
            {
                return false;
            }

            true
        });
    }

    false
}