xref: /wasmtime-44.0.1/cranelift/native/src/lib.rs (revision 7fa89c4a)
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         // These are temporarily enabled by default (see #3810 for
62         // more) so that a default-constructed `Flags` can work with
63         // default Wasmtime features. Otherwise, the user must
64         // explicitly use native flags or turn these on when on x86-64
65         // platforms to avoid a configuration panic. In order for the
66         // "enable if detected" logic below to work, we must turn them
67         // *off* (differing from the default) and then re-enable below
68         // if present.
69         isa_builder.set("has_sse3", "false").unwrap();
70         isa_builder.set("has_ssse3", "false").unwrap();
71         isa_builder.set("has_sse41", "false").unwrap();
72         isa_builder.set("has_sse42", "false").unwrap();
73 
74         if std::is_x86_feature_detected!("sse3") {
75             isa_builder.enable("has_sse3").unwrap();
76         }
77         if std::is_x86_feature_detected!("ssse3") {
78             isa_builder.enable("has_ssse3").unwrap();
79         }
80         if std::is_x86_feature_detected!("sse4.1") {
81             isa_builder.enable("has_sse41").unwrap();
82         }
83         if std::is_x86_feature_detected!("sse4.2") {
84             isa_builder.enable("has_sse42").unwrap();
85         }
86         if std::is_x86_feature_detected!("popcnt") {
87             isa_builder.enable("has_popcnt").unwrap();
88         }
89         if std::is_x86_feature_detected!("avx") {
90             isa_builder.enable("has_avx").unwrap();
91         }
92         if std::is_x86_feature_detected!("avx2") {
93             isa_builder.enable("has_avx2").unwrap();
94         }
95         if std::is_x86_feature_detected!("fma") {
96             isa_builder.enable("has_fma").unwrap();
97         }
98         if std::is_x86_feature_detected!("bmi1") {
99             isa_builder.enable("has_bmi1").unwrap();
100         }
101         if std::is_x86_feature_detected!("bmi2") {
102             isa_builder.enable("has_bmi2").unwrap();
103         }
104         if std::is_x86_feature_detected!("avx512bitalg") {
105             isa_builder.enable("has_avx512bitalg").unwrap();
106         }
107         if std::is_x86_feature_detected!("avx512dq") {
108             isa_builder.enable("has_avx512dq").unwrap();
109         }
110         if std::is_x86_feature_detected!("avx512f") {
111             isa_builder.enable("has_avx512f").unwrap();
112         }
113         if std::is_x86_feature_detected!("avx512vl") {
114             isa_builder.enable("has_avx512vl").unwrap();
115         }
116         if std::is_x86_feature_detected!("avx512vbmi") {
117             isa_builder.enable("has_avx512vbmi").unwrap();
118         }
119         if std::is_x86_feature_detected!("lzcnt") {
120             isa_builder.enable("has_lzcnt").unwrap();
121         }
122     }
123 
124     #[cfg(target_arch = "aarch64")]
125     {
126         use cranelift_codegen::settings::Configurable;
127 
128         if !infer_native_flags {
129             return Ok(isa_builder);
130         }
131 
132         if std::arch::is_aarch64_feature_detected!("lse") {
133             isa_builder.enable("has_lse").unwrap();
134         }
135 
136         if std::arch::is_aarch64_feature_detected!("paca") {
137             isa_builder.enable("has_pauth").unwrap();
138         }
139 
140         if cfg!(target_os = "macos") {
141             isa_builder.enable("sign_return_address_with_bkey").unwrap();
142         }
143     }
144 
145     // There is no is_s390x_feature_detected macro yet, so for now
146     // we use getauxval from the libc crate directly.
147     #[cfg(all(target_arch = "s390x", target_os = "linux"))]
148     {
149         use cranelift_codegen::settings::Configurable;
150 
151         if !infer_native_flags {
152             return Ok(isa_builder);
153         }
154 
155         let v = unsafe { libc::getauxval(libc::AT_HWCAP) };
156         const HWCAP_S390X_VXRS_EXT2: libc::c_ulong = 32768;
157         if (v & HWCAP_S390X_VXRS_EXT2) != 0 {
158             isa_builder.enable("has_vxrs_ext2").unwrap();
159             // There is no separate HWCAP bit for mie2, so assume
160             // that any machine with vxrs_ext2 also has mie2.
161             isa_builder.enable("has_mie2").unwrap();
162         }
163     }
164 
165     // squelch warnings about unused mut/variables on some platforms.
166     drop(&mut isa_builder);
167     drop(infer_native_flags);
168 
169     Ok(isa_builder)
170 }
171 
172 #[cfg(test)]
173 mod tests {
174     use super::builder;
175     use cranelift_codegen::isa::CallConv;
176     use cranelift_codegen::settings;
177 
178     #[test]
179     fn test() {
180         if let Ok(isa_builder) = builder() {
181             let flag_builder = settings::builder();
182             let isa = isa_builder
183                 .finish(settings::Flags::new(flag_builder))
184                 .unwrap();
185 
186             if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
187                 assert_eq!(isa.default_call_conv(), CallConv::AppleAarch64);
188             } else if cfg!(any(unix, target_os = "nebulet")) {
189                 assert_eq!(isa.default_call_conv(), CallConv::SystemV);
190             } else if cfg!(windows) {
191                 assert_eq!(isa.default_call_conv(), CallConv::WindowsFastcall);
192             }
193 
194             if cfg!(target_pointer_width = "64") {
195                 assert_eq!(isa.pointer_bits(), 64);
196             } else if cfg!(target_pointer_width = "32") {
197                 assert_eq!(isa.pointer_bits(), 32);
198             } else if cfg!(target_pointer_width = "16") {
199                 assert_eq!(isa.pointer_bits(), 16);
200             }
201         }
202     }
203 }
204 
205 /// Version number of this crate.
206 pub const VERSION: &str = env!("CARGO_PKG_VERSION");
207