xref: /wasmtime-44.0.1/cranelift/native/src/lib.rs (revision 98ef18a2)
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(
5     missing_docs,
6     trivial_numeric_casts,
7     unused_extern_crates,
8     unstable_features
9 )]
10 #![warn(unused_import_braces)]
11 #![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))]
12 #![cfg_attr(feature = "cargo-clippy", allow(clippy::new_without_default))]
13 #![cfg_attr(
14     feature = "cargo-clippy",
15     warn(
16         clippy::float_arithmetic,
17         clippy::mut_mut,
18         clippy::nonminimal_bool,
19         clippy::map_unwrap_or,
20         clippy::clippy::print_stdout,
21         clippy::unicode_not_nfc,
22         clippy::use_self
23     )
24 )]
25 
26 use cranelift_codegen::isa;
27 use target_lexicon::Triple;
28 
29 /// Return an `isa` builder configured for the current host
30 /// machine, or `Err(())` if the host machine is not supported
31 /// in the current configuration.
32 pub fn builder() -> Result<isa::Builder, &'static str> {
33     builder_with_options(true)
34 }
35 
36 /// Return an `isa` builder configured for the current host
37 /// machine, or `Err(())` if the host machine is not supported
38 /// in the current configuration.
39 ///
40 /// Selects the given backend variant specifically; this is
41 /// useful when more than oen backend exists for a given target
42 /// (e.g., on x86-64).
43 pub fn builder_with_options(infer_native_flags: bool) -> Result<isa::Builder, &'static str> {
44     let mut isa_builder = isa::lookup(Triple::host()).map_err(|err| match err {
45         isa::LookupError::SupportDisabled => "support for architecture disabled at compile time",
46         isa::LookupError::Unsupported => "unsupported architecture",
47     })?;
48 
49     #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
50     {
51         use cranelift_codegen::settings::Configurable;
52 
53         if !std::is_x86_feature_detected!("sse2") {
54             return Err("x86 support requires SSE2");
55         }
56 
57         if !infer_native_flags {
58             return Ok(isa_builder);
59         }
60 
61         if std::is_x86_feature_detected!("sse3") {
62             isa_builder.enable("has_sse3").unwrap();
63         }
64         if std::is_x86_feature_detected!("ssse3") {
65             isa_builder.enable("has_ssse3").unwrap();
66         }
67         if std::is_x86_feature_detected!("sse4.1") {
68             isa_builder.enable("has_sse41").unwrap();
69         }
70         if std::is_x86_feature_detected!("sse4.2") {
71             isa_builder.enable("has_sse42").unwrap();
72         }
73         if std::is_x86_feature_detected!("popcnt") {
74             isa_builder.enable("has_popcnt").unwrap();
75         }
76         if std::is_x86_feature_detected!("avx") {
77             isa_builder.enable("has_avx").unwrap();
78         }
79         if std::is_x86_feature_detected!("avx2") {
80             isa_builder.enable("has_avx2").unwrap();
81         }
82         if std::is_x86_feature_detected!("bmi1") {
83             isa_builder.enable("has_bmi1").unwrap();
84         }
85         if std::is_x86_feature_detected!("bmi2") {
86             isa_builder.enable("has_bmi2").unwrap();
87         }
88         if std::is_x86_feature_detected!("avx512bitalg") {
89             isa_builder.enable("has_avx512bitalg").unwrap();
90         }
91         if std::is_x86_feature_detected!("avx512dq") {
92             isa_builder.enable("has_avx512dq").unwrap();
93         }
94         if std::is_x86_feature_detected!("avx512f") {
95             isa_builder.enable("has_avx512f").unwrap();
96         }
97         if std::is_x86_feature_detected!("avx512vl") {
98             isa_builder.enable("has_avx512vl").unwrap();
99         }
100         if std::is_x86_feature_detected!("avx512vbmi") {
101             isa_builder.enable("has_avx512vbmi").unwrap();
102         }
103         if std::is_x86_feature_detected!("lzcnt") {
104             isa_builder.enable("has_lzcnt").unwrap();
105         }
106     }
107 
108     // `stdsimd` is necessary for std::is_aarch64_feature_detected!().
109     #[cfg(all(target_arch = "aarch64", feature = "stdsimd"))]
110     {
111         use cranelift_codegen::settings::Configurable;
112 
113         if !infer_native_flags {
114             return Ok(isa_builder);
115         }
116 
117         if std::is_aarch64_feature_detected!("lse") {
118             isa_builder.enable("has_lse").unwrap();
119         }
120     }
121 
122     // There is no is_s390x_feature_detected macro yet, so for now
123     // we use getauxval from the libc crate directly.
124     #[cfg(all(target_arch = "s390x", target_os = "linux"))]
125     {
126         use cranelift_codegen::settings::Configurable;
127 
128         if !infer_native_flags {
129             return Ok(isa_builder);
130         }
131 
132         let v = unsafe { libc::getauxval(libc::AT_HWCAP) };
133         const HWCAP_S390X_VXRS_EXT2: libc::c_ulong = 32768;
134         if (v & HWCAP_S390X_VXRS_EXT2) != 0 {
135             isa_builder.enable("has_vxrs_ext2").unwrap();
136             // There is no separate HWCAP bit for mie2, so assume
137             // that any machine with vxrs_ext2 also has mie2.
138             isa_builder.enable("has_mie2").unwrap();
139         }
140     }
141 
142     // squelch warnings about unused mut/variables on some platforms.
143     drop(&mut isa_builder);
144     drop(infer_native_flags);
145 
146     Ok(isa_builder)
147 }
148 
149 #[cfg(test)]
150 mod tests {
151     use super::builder;
152     use cranelift_codegen::isa::CallConv;
153     use cranelift_codegen::settings;
154 
155     #[test]
156     fn test() {
157         if let Ok(isa_builder) = builder() {
158             let flag_builder = settings::builder();
159             let isa = isa_builder
160                 .finish(settings::Flags::new(flag_builder))
161                 .unwrap();
162 
163             if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
164                 assert_eq!(isa.default_call_conv(), CallConv::AppleAarch64);
165             } else if cfg!(any(unix, target_os = "nebulet")) {
166                 assert_eq!(isa.default_call_conv(), CallConv::SystemV);
167             } else if cfg!(windows) {
168                 assert_eq!(isa.default_call_conv(), CallConv::WindowsFastcall);
169             }
170 
171             if cfg!(target_pointer_width = "64") {
172                 assert_eq!(isa.pointer_bits(), 64);
173             } else if cfg!(target_pointer_width = "32") {
174                 assert_eq!(isa.pointer_bits(), 32);
175             } else if cfg!(target_pointer_width = "16") {
176                 assert_eq!(isa.pointer_bits(), 16);
177             }
178         }
179     }
180 }
181 
182 /// Version number of this crate.
183 pub const VERSION: &str = env!("CARGO_PKG_VERSION");
184