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 #[cfg(target_arch = "aarch64")] 109 { 110 use cranelift_codegen::settings::Configurable; 111 112 if !infer_native_flags { 113 return Ok(isa_builder); 114 } 115 116 if std::arch::is_aarch64_feature_detected!("lse") { 117 isa_builder.enable("has_lse").unwrap(); 118 } 119 } 120 121 // There is no is_s390x_feature_detected macro yet, so for now 122 // we use getauxval from the libc crate directly. 123 #[cfg(all(target_arch = "s390x", target_os = "linux"))] 124 { 125 use cranelift_codegen::settings::Configurable; 126 127 if !infer_native_flags { 128 return Ok(isa_builder); 129 } 130 131 let v = unsafe { libc::getauxval(libc::AT_HWCAP) }; 132 const HWCAP_S390X_VXRS_EXT2: libc::c_ulong = 32768; 133 if (v & HWCAP_S390X_VXRS_EXT2) != 0 { 134 isa_builder.enable("has_vxrs_ext2").unwrap(); 135 // There is no separate HWCAP bit for mie2, so assume 136 // that any machine with vxrs_ext2 also has mie2. 137 isa_builder.enable("has_mie2").unwrap(); 138 } 139 } 140 141 // squelch warnings about unused mut/variables on some platforms. 142 drop(&mut isa_builder); 143 drop(infer_native_flags); 144 145 Ok(isa_builder) 146 } 147 148 #[cfg(test)] 149 mod tests { 150 use super::builder; 151 use cranelift_codegen::isa::CallConv; 152 use cranelift_codegen::settings; 153 154 #[test] 155 fn test() { 156 if let Ok(isa_builder) = builder() { 157 let flag_builder = settings::builder(); 158 let isa = isa_builder 159 .finish(settings::Flags::new(flag_builder)) 160 .unwrap(); 161 162 if cfg!(all(target_os = "macos", target_arch = "aarch64")) { 163 assert_eq!(isa.default_call_conv(), CallConv::AppleAarch64); 164 } else if cfg!(any(unix, target_os = "nebulet")) { 165 assert_eq!(isa.default_call_conv(), CallConv::SystemV); 166 } else if cfg!(windows) { 167 assert_eq!(isa.default_call_conv(), CallConv::WindowsFastcall); 168 } 169 170 if cfg!(target_pointer_width = "64") { 171 assert_eq!(isa.pointer_bits(), 64); 172 } else if cfg!(target_pointer_width = "32") { 173 assert_eq!(isa.pointer_bits(), 32); 174 } else if cfg!(target_pointer_width = "16") { 175 assert_eq!(isa.pointer_bits(), 16); 176 } 177 } 178 } 179 } 180 181 /// Version number of this crate. 182 pub const VERSION: &str = env!("CARGO_PKG_VERSION"); 183