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(isa::BackendVariant::Any, 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( 44 variant: isa::BackendVariant, 45 infer_native_flags: bool, 46 ) -> Result<isa::Builder, &'static str> { 47 let mut isa_builder = 48 isa::lookup_variant(Triple::host(), variant).map_err(|err| match err { 49 isa::LookupError::SupportDisabled => { 50 "support for architecture disabled at compile time" 51 } 52 isa::LookupError::Unsupported => "unsupported architecture", 53 })?; 54 55 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 56 { 57 use cranelift_codegen::settings::Configurable; 58 59 if !std::is_x86_feature_detected!("sse2") { 60 return Err("x86 support requires SSE2"); 61 } 62 63 if !infer_native_flags { 64 return Ok(isa_builder); 65 } 66 67 if std::is_x86_feature_detected!("sse3") { 68 isa_builder.enable("has_sse3").unwrap(); 69 } 70 if std::is_x86_feature_detected!("ssse3") { 71 isa_builder.enable("has_ssse3").unwrap(); 72 } 73 if std::is_x86_feature_detected!("sse4.1") { 74 isa_builder.enable("has_sse41").unwrap(); 75 } 76 if std::is_x86_feature_detected!("sse4.2") { 77 isa_builder.enable("has_sse42").unwrap(); 78 } 79 if std::is_x86_feature_detected!("popcnt") { 80 isa_builder.enable("has_popcnt").unwrap(); 81 } 82 if std::is_x86_feature_detected!("avx") { 83 isa_builder.enable("has_avx").unwrap(); 84 } 85 if std::is_x86_feature_detected!("avx2") { 86 isa_builder.enable("has_avx2").unwrap(); 87 } 88 if std::is_x86_feature_detected!("bmi1") { 89 isa_builder.enable("has_bmi1").unwrap(); 90 } 91 if std::is_x86_feature_detected!("bmi2") { 92 isa_builder.enable("has_bmi2").unwrap(); 93 } 94 if std::is_x86_feature_detected!("avx512bitalg") { 95 isa_builder.enable("has_avx512bitalg").unwrap(); 96 } 97 if std::is_x86_feature_detected!("avx512dq") { 98 isa_builder.enable("has_avx512dq").unwrap(); 99 } 100 if std::is_x86_feature_detected!("avx512f") { 101 isa_builder.enable("has_avx512f").unwrap(); 102 } 103 if std::is_x86_feature_detected!("avx512vl") { 104 isa_builder.enable("has_avx512vl").unwrap(); 105 } 106 if std::is_x86_feature_detected!("avx512vbmi") { 107 isa_builder.enable("has_avx512vbmi").unwrap(); 108 } 109 if std::is_x86_feature_detected!("lzcnt") { 110 isa_builder.enable("has_lzcnt").unwrap(); 111 } 112 } 113 114 // `stdsimd` is necessary for std::is_aarch64_feature_detected!(). 115 #[cfg(all(target_arch = "aarch64", feature = "stdsimd"))] 116 { 117 use cranelift_codegen::settings::Configurable; 118 119 if !infer_native_flags { 120 return Ok(isa_builder); 121 } 122 123 if std::is_aarch64_feature_detected!("lse") { 124 isa_builder.enable("has_lse").unwrap(); 125 } 126 } 127 128 // There is no is_s390x_feature_detected macro yet, so for now 129 // we use getauxval from the libc crate directly. 130 #[cfg(all(target_arch = "s390x", target_os = "linux"))] 131 { 132 use cranelift_codegen::settings::Configurable; 133 134 if !infer_native_flags { 135 return Ok(isa_builder); 136 } 137 138 let v = unsafe { libc::getauxval(libc::AT_HWCAP) }; 139 const HWCAP_S390X_VXRS_EXT2: libc::c_ulong = 32768; 140 if (v & HWCAP_S390X_VXRS_EXT2) != 0 { 141 isa_builder.enable("has_vxrs_ext2").unwrap(); 142 // There is no separate HWCAP bit for mie2, so assume 143 // that any machine with vxrs_ext2 also has mie2. 144 isa_builder.enable("has_mie2").unwrap(); 145 } 146 } 147 148 // squelch warnings about unused mut/variables on some platforms. 149 drop(&mut isa_builder); 150 drop(infer_native_flags); 151 152 Ok(isa_builder) 153 } 154 155 #[cfg(test)] 156 mod tests { 157 use super::builder; 158 use cranelift_codegen::isa::CallConv; 159 use cranelift_codegen::settings; 160 161 #[test] 162 fn test() { 163 if let Ok(isa_builder) = builder() { 164 let flag_builder = settings::builder(); 165 let isa = isa_builder.finish(settings::Flags::new(flag_builder)); 166 167 if cfg!(all(target_os = "macos", target_arch = "aarch64")) { 168 assert_eq!(isa.default_call_conv(), CallConv::AppleAarch64); 169 } else if cfg!(any(unix, target_os = "nebulet")) { 170 assert_eq!(isa.default_call_conv(), CallConv::SystemV); 171 } else if cfg!(windows) { 172 assert_eq!(isa.default_call_conv(), CallConv::WindowsFastcall); 173 } 174 175 if cfg!(target_pointer_width = "64") { 176 assert_eq!(isa.pointer_bits(), 64); 177 } else if cfg!(target_pointer_width = "32") { 178 assert_eq!(isa.pointer_bits(), 32); 179 } else if cfg!(target_pointer_width = "16") { 180 assert_eq!(isa.pointer_bits(), 16); 181 } 182 } 183 } 184 } 185 186 /// Version number of this crate. 187 pub const VERSION: &str = env!("CARGO_PKG_VERSION"); 188