Skip to main content

cranelift_native/
lib.rs

1//! Performs autodetection of the host for the purposes of running
2//! Cranelift to generate code to run on the same machine.
3
4#![deny(missing_docs)]
5
6use cranelift_codegen::isa;
7use cranelift_codegen::settings::Configurable;
8use target_lexicon::Triple;
9
10#[cfg(all(target_arch = "riscv64", target_os = "linux"))]
11mod riscv;
12
13/// Return an `isa` builder configured for the current host
14/// machine, or `Err(())` if the host machine is not supported
15/// in the current configuration.
16pub fn builder() -> Result<isa::Builder, &'static str> {
17    builder_with_options(true)
18}
19
20/// Return an `isa` builder configured for the current host
21/// machine, or `Err(())` if the host machine is not supported
22/// in the current configuration.
23///
24/// Selects the given backend variant specifically; this is
25/// useful when more than one backend exists for a given target
26/// (e.g., on x86-64).
27pub fn builder_with_options(infer_native_flags: bool) -> Result<isa::Builder, &'static str> {
28    let mut isa_builder = isa::lookup(Triple::host()).map_err(|err| match err {
29        isa::LookupError::SupportDisabled => "support for architecture disabled at compile time",
30        isa::LookupError::Unsupported => "unsupported architecture",
31    })?;
32    if infer_native_flags {
33        self::infer_native_flags(&mut isa_builder)?;
34    }
35    Ok(isa_builder)
36}
37
38/// Return an `isa` builder configured for the current host
39/// machine, or `Err(())` if the host machine is not supported
40/// in the current configuration.
41///
42/// Selects the given backend variant specifically; this is
43/// useful when more than one backend exists for a given target
44/// (e.g., on x86-64).
45pub fn infer_native_flags(isa_builder: &mut dyn Configurable) -> Result<(), &'static str> {
46    #[cfg(target_arch = "x86_64")]
47    {
48        if !std::is_x86_feature_detected!("sse2") {
49            return Err("x86 support requires SSE2");
50        }
51
52        if std::is_x86_feature_detected!("cmpxchg16b") {
53            isa_builder.enable("has_cmpxchg16b").unwrap();
54        }
55        if std::is_x86_feature_detected!("sse3") {
56            isa_builder.enable("has_sse3").unwrap();
57        }
58        if std::is_x86_feature_detected!("ssse3") {
59            isa_builder.enable("has_ssse3").unwrap();
60        }
61        if std::is_x86_feature_detected!("sse4.1") {
62            isa_builder.enable("has_sse41").unwrap();
63        }
64        if std::is_x86_feature_detected!("sse4.2") {
65            isa_builder.enable("has_sse42").unwrap();
66        }
67        if std::is_x86_feature_detected!("popcnt") {
68            isa_builder.enable("has_popcnt").unwrap();
69        }
70        if std::is_x86_feature_detected!("avx") {
71            isa_builder.enable("has_avx").unwrap();
72        }
73        if std::is_x86_feature_detected!("avx2") {
74            isa_builder.enable("has_avx2").unwrap();
75        }
76        if std::is_x86_feature_detected!("fma") {
77            isa_builder.enable("has_fma").unwrap();
78        }
79        if std::is_x86_feature_detected!("avxvnni") {
80            isa_builder.enable("has_avx_vnni").unwrap();
81        }
82        if std::is_x86_feature_detected!("avx512vnni") {
83            isa_builder.enable("has_avx512vnni").unwrap();
84        }
85        if std::is_x86_feature_detected!("bmi1") {
86            isa_builder.enable("has_bmi1").unwrap();
87        }
88        if std::is_x86_feature_detected!("bmi2") {
89            isa_builder.enable("has_bmi2").unwrap();
90        }
91        if std::is_x86_feature_detected!("avx512bitalg") {
92            isa_builder.enable("has_avx512bitalg").unwrap();
93        }
94        if std::is_x86_feature_detected!("avx512dq") {
95            isa_builder.enable("has_avx512dq").unwrap();
96        }
97        if std::is_x86_feature_detected!("avx512f") {
98            isa_builder.enable("has_avx512f").unwrap();
99        }
100        if std::is_x86_feature_detected!("avx512vl") {
101            isa_builder.enable("has_avx512vl").unwrap();
102        }
103        if std::is_x86_feature_detected!("avx512vbmi") {
104            isa_builder.enable("has_avx512vbmi").unwrap();
105        }
106        if std::is_x86_feature_detected!("lzcnt") {
107            isa_builder.enable("has_lzcnt").unwrap();
108        }
109    }
110
111    #[cfg(target_arch = "aarch64")]
112    {
113        if std::arch::is_aarch64_feature_detected!("lse") {
114            isa_builder.enable("has_lse").unwrap();
115        }
116
117        if std::arch::is_aarch64_feature_detected!("paca") {
118            isa_builder.enable("has_pauth").unwrap();
119        }
120
121        if std::arch::is_aarch64_feature_detected!("fp16") {
122            isa_builder.enable("has_fp16").unwrap();
123        }
124
125        if std::arch::is_aarch64_feature_detected!("dotprod") {
126            isa_builder.enable("has_dotprod").unwrap();
127        }
128
129        if std::arch::is_aarch64_feature_detected!("i8mm") {
130            isa_builder.enable("has_i8mm").unwrap();
131        }
132
133        if cfg!(target_os = "macos") {
134            // Pointer authentication is always available on Apple Silicon.
135            isa_builder.enable("sign_return_address").unwrap();
136            // macOS enforces the use of the B key for return addresses.
137            isa_builder.enable("sign_return_address_with_bkey").unwrap();
138        }
139    }
140
141    // `is_s390x_feature_detected` is nightly only for now, so use the
142    // STORE FACILITY LIST EXTENDED instruction as a temporary measure.
143    #[cfg(target_arch = "s390x")]
144    {
145        let mut facility_list: [u64; 4] = [0; 4];
146        unsafe {
147            core::arch::asm!(
148                "stfle 0({})",
149                in(reg_addr) facility_list.as_mut_ptr() ,
150                inout("r0") facility_list.len() as u64 - 1 => _,
151                options(nostack)
152            );
153        }
154        let get_facility_bit = |n: usize| {
155            // NOTE: bits are numbered from the left.
156            facility_list[n / 64] & (1 << (63 - (n % 64))) != 0
157        };
158
159        if get_facility_bit(61) {
160            isa_builder.enable("has_mie3").unwrap();
161        }
162        if get_facility_bit(84) {
163            isa_builder.enable("has_mie4").unwrap();
164        }
165        if get_facility_bit(148) {
166            isa_builder.enable("has_vxrs_ext2").unwrap();
167        }
168        if get_facility_bit(198) {
169            isa_builder.enable("has_vxrs_ext3").unwrap();
170        }
171    }
172
173    // `is_riscv_feature_detected` is nightly only for now, use
174    // getauxval from the libc crate directly as a temporary measure.
175    #[cfg(all(target_arch = "riscv64", target_os = "linux"))]
176    {
177        // Try both hwcap and cpuinfo
178        // HWCAP only returns single letter extensions, cpuinfo returns all of
179        // them but may not be available in some systems (QEMU < 8.1).
180        riscv::hwcap_detect(isa_builder)?;
181
182        // Ignore errors for cpuinfo. QEMU versions prior to 8.1 do not emulate
183        // the cpuinfo interface, so we can't rely on it being present for now.
184        let _ = riscv::cpuinfo_detect(isa_builder);
185    }
186
187    // On all other architectures (e.g. wasm32) we won't infer any native flags,
188    // but still need to use the `isa_builder` to avoid compiler warnings.
189    let _ = isa_builder;
190    Ok(())
191}
192
193/// Version number of this crate.
194pub const VERSION: &str = env!("CARGO_PKG_VERSION");
195
196#[cfg(test)]
197mod tests {
198    use super::builder;
199    use cranelift_codegen::isa::CallConv;
200    use cranelift_codegen::settings;
201
202    #[test]
203    fn test() {
204        if let Ok(isa_builder) = builder() {
205            let flag_builder = settings::builder();
206            let isa = isa_builder
207                .finish(settings::Flags::new(flag_builder))
208                .unwrap();
209
210            if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
211                assert_eq!(isa.default_call_conv(), CallConv::AppleAarch64);
212            } else if cfg!(unix) {
213                assert_eq!(isa.default_call_conv(), CallConv::SystemV);
214            } else if cfg!(windows) {
215                assert_eq!(isa.default_call_conv(), CallConv::WindowsFastcall);
216            }
217
218            if cfg!(target_pointer_width = "64") {
219                assert_eq!(isa.pointer_bits(), 64);
220            } else if cfg!(target_pointer_width = "32") {
221                assert_eq!(isa.pointer_bits(), 32);
222            } else if cfg!(target_pointer_width = "16") {
223                assert_eq!(isa.pointer_bits(), 16);
224            }
225        }
226    }
227}