]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/llvm_util.rs
Rollup merge of #85182 - CDirkx:available_concurrency, r=JohnTitor
[rust.git] / compiler / rustc_codegen_llvm / src / llvm_util.rs
1 use crate::back::write::create_informational_target_machine;
2 use crate::{llvm, llvm_util};
3 use libc::c_int;
4 use rustc_codegen_ssa::target_features::supported_target_features;
5 use rustc_data_structures::fx::FxHashSet;
6 use rustc_middle::bug;
7 use rustc_session::config::PrintRequest;
8 use rustc_session::Session;
9 use rustc_span::symbol::Symbol;
10 use rustc_target::spec::{MergeFunctions, PanicStrategy};
11 use std::ffi::{CStr, CString};
12
13 use std::ptr;
14 use std::slice;
15 use std::str;
16 use std::sync::atomic::{AtomicBool, Ordering};
17 use std::sync::Once;
18
19 static POISONED: AtomicBool = AtomicBool::new(false);
20 static INIT: Once = Once::new();
21
22 pub(crate) fn init(sess: &Session) {
23     unsafe {
24         // Before we touch LLVM, make sure that multithreading is enabled.
25         INIT.call_once(|| {
26             if llvm::LLVMStartMultithreaded() != 1 {
27                 // use an extra bool to make sure that all future usage of LLVM
28                 // cannot proceed despite the Once not running more than once.
29                 POISONED.store(true, Ordering::SeqCst);
30             }
31
32             configure_llvm(sess);
33         });
34
35         if POISONED.load(Ordering::SeqCst) {
36             bug!("couldn't enable multi-threaded LLVM");
37         }
38     }
39 }
40
41 fn require_inited() {
42     INIT.call_once(|| bug!("llvm is not initialized"));
43     if POISONED.load(Ordering::SeqCst) {
44         bug!("couldn't enable multi-threaded LLVM");
45     }
46 }
47
48 unsafe fn configure_llvm(sess: &Session) {
49     let n_args = sess.opts.cg.llvm_args.len() + sess.target.llvm_args.len();
50     let mut llvm_c_strs = Vec::with_capacity(n_args + 1);
51     let mut llvm_args = Vec::with_capacity(n_args + 1);
52
53     llvm::LLVMRustInstallFatalErrorHandler();
54
55     fn llvm_arg_to_arg_name(full_arg: &str) -> &str {
56         full_arg.trim().split(|c: char| c == '=' || c.is_whitespace()).next().unwrap_or("")
57     }
58
59     let cg_opts = sess.opts.cg.llvm_args.iter();
60     let tg_opts = sess.target.llvm_args.iter();
61     let sess_args = cg_opts.chain(tg_opts);
62
63     let user_specified_args: FxHashSet<_> =
64         sess_args.clone().map(|s| llvm_arg_to_arg_name(s)).filter(|s| !s.is_empty()).collect();
65
66     {
67         // This adds the given argument to LLVM. Unless `force` is true
68         // user specified arguments are *not* overridden.
69         let mut add = |arg: &str, force: bool| {
70             if force || !user_specified_args.contains(llvm_arg_to_arg_name(arg)) {
71                 let s = CString::new(arg).unwrap();
72                 llvm_args.push(s.as_ptr());
73                 llvm_c_strs.push(s);
74             }
75         };
76         // Set the llvm "program name" to make usage and invalid argument messages more clear.
77         add("rustc -Cllvm-args=\"...\" with", true);
78         if sess.time_llvm_passes() {
79             add("-time-passes", false);
80         }
81         if sess.print_llvm_passes() {
82             add("-debug-pass=Structure", false);
83         }
84         if !sess.opts.debugging_opts.no_generate_arange_section {
85             add("-generate-arange-section", false);
86         }
87
88         // FIXME(nagisa): disable the machine outliner by default in LLVM versions 11, where it was
89         // introduced and up.
90         //
91         // This should remain in place until https://reviews.llvm.org/D103167 is fixed. If LLVM
92         // has been upgraded since, consider adjusting the version check below to contain an upper
93         // bound.
94         if llvm_util::get_version() >= (11, 0, 0) {
95             add("-enable-machine-outliner=never", false);
96         }
97
98         match sess.opts.debugging_opts.merge_functions.unwrap_or(sess.target.merge_functions) {
99             MergeFunctions::Disabled | MergeFunctions::Trampolines => {}
100             MergeFunctions::Aliases => {
101                 add("-mergefunc-use-aliases", false);
102             }
103         }
104
105         if sess.target.os == "emscripten" && sess.panic_strategy() == PanicStrategy::Unwind {
106             add("-enable-emscripten-cxx-exceptions", false);
107         }
108
109         // HACK(eddyb) LLVM inserts `llvm.assume` calls to preserve align attributes
110         // during inlining. Unfortunately these may block other optimizations.
111         add("-preserve-alignment-assumptions-during-inlining=false", false);
112
113         // Use non-zero `import-instr-limit` multiplier for cold callsites.
114         add("-import-cold-multiplier=0.1", false);
115
116         for arg in sess_args {
117             add(&(*arg), true);
118         }
119     }
120
121     if sess.opts.debugging_opts.llvm_time_trace {
122         // time-trace is not thread safe and running it in parallel will cause seg faults.
123         if !sess.opts.debugging_opts.no_parallel_llvm {
124             bug!("`-Z llvm-time-trace` requires `-Z no-parallel-llvm")
125         }
126
127         llvm::LLVMTimeTraceProfilerInitialize();
128     }
129
130     llvm::LLVMInitializePasses();
131
132     rustc_llvm::initialize_available_targets();
133
134     llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());
135 }
136
137 pub fn time_trace_profiler_finish(file_name: &str) {
138     unsafe {
139         let file_name = CString::new(file_name).unwrap();
140         llvm::LLVMTimeTraceProfilerFinish(file_name.as_ptr());
141     }
142 }
143
144 // WARNING: the features after applying `to_llvm_feature` must be known
145 // to LLVM or the feature detection code will walk past the end of the feature
146 // array, leading to crashes.
147 // To find a list of LLVM's names, check llvm-project/llvm/include/llvm/Support/*TargetParser.def
148 // where the * matches the architecture's name
149 // Beware to not use the llvm github project for this, but check the git submodule
150 // found in src/llvm-project
151 // Though note that Rust can also be build with an external precompiled version of LLVM
152 // which might lead to failures if the oldest tested / supported LLVM version
153 // doesn't yet support the relevant intrinsics
154 pub fn to_llvm_feature<'a>(sess: &Session, s: &'a str) -> &'a str {
155     let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch };
156     match (arch, s) {
157         ("x86", "pclmulqdq") => "pclmul",
158         ("x86", "rdrand") => "rdrnd",
159         ("x86", "bmi1") => "bmi",
160         ("x86", "cmpxchg16b") => "cx16",
161         ("x86", "avx512vaes") => "vaes",
162         ("x86", "avx512gfni") => "gfni",
163         ("x86", "avx512vpclmulqdq") => "vpclmulqdq",
164         ("aarch64", "fp") => "fp-armv8",
165         ("aarch64", "fp16") => "fullfp16",
166         ("aarch64", "fhm") => "fp16fml",
167         ("aarch64", "rcpc2") => "rcpc-immo",
168         ("aarch64", "dpb") => "ccpp",
169         ("aarch64", "dpb2") => "ccdp",
170         ("aarch64", "frintts") => "fptoint",
171         ("aarch64", "fcma") => "complxnum",
172         (_, s) => s,
173     }
174 }
175
176 pub fn target_features(sess: &Session) -> Vec<Symbol> {
177     let target_machine = create_informational_target_machine(sess);
178     supported_target_features(sess)
179         .iter()
180         .filter_map(
181             |&(feature, gate)| {
182                 if sess.is_nightly_build() || gate.is_none() { Some(feature) } else { None }
183             },
184         )
185         .filter(|feature| {
186             let llvm_feature = to_llvm_feature(sess, feature);
187             let cstr = CString::new(llvm_feature).unwrap();
188             unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) }
189         })
190         .map(|feature| Symbol::intern(feature))
191         .collect()
192 }
193
194 pub fn print_version() {
195     let (major, minor, patch) = get_version();
196     println!("LLVM version: {}.{}.{}", major, minor, patch);
197 }
198
199 pub fn get_version() -> (u32, u32, u32) {
200     // Can be called without initializing LLVM
201     unsafe {
202         (llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor(), llvm::LLVMRustVersionPatch())
203     }
204 }
205
206 pub fn print_passes() {
207     // Can be called without initializing LLVM
208     unsafe {
209         llvm::LLVMRustPrintPasses();
210     }
211 }
212
213 fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
214     let len = unsafe { llvm::LLVMRustGetTargetFeaturesCount(tm) };
215     let mut ret = Vec::with_capacity(len);
216     for i in 0..len {
217         unsafe {
218             let mut feature = ptr::null();
219             let mut desc = ptr::null();
220             llvm::LLVMRustGetTargetFeature(tm, i, &mut feature, &mut desc);
221             if feature.is_null() || desc.is_null() {
222                 bug!("LLVM returned a `null` target feature string");
223             }
224             let feature = CStr::from_ptr(feature).to_str().unwrap_or_else(|e| {
225                 bug!("LLVM returned a non-utf8 feature string: {}", e);
226             });
227             let desc = CStr::from_ptr(desc).to_str().unwrap_or_else(|e| {
228                 bug!("LLVM returned a non-utf8 feature string: {}", e);
229             });
230             ret.push((feature, desc));
231         }
232     }
233     ret
234 }
235
236 fn print_target_features(sess: &Session, tm: &llvm::TargetMachine) {
237     let mut target_features = llvm_target_features(tm);
238     let mut rustc_target_features = supported_target_features(sess)
239         .iter()
240         .filter_map(|(feature, _gate)| {
241             let llvm_feature = to_llvm_feature(sess, *feature);
242             // LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these strings.
243             target_features.binary_search_by_key(&llvm_feature, |(f, _d)| *f).ok().map(|index| {
244                 let (_f, desc) = target_features.remove(index);
245                 (*feature, desc)
246             })
247         })
248         .collect::<Vec<_>>();
249     rustc_target_features.extend_from_slice(&[(
250         "crt-static",
251         "Enables C Run-time Libraries to be statically linked",
252     )]);
253     let max_feature_len = target_features
254         .iter()
255         .chain(rustc_target_features.iter())
256         .map(|(feature, _desc)| feature.len())
257         .max()
258         .unwrap_or(0);
259
260     println!("Features supported by rustc for this target:");
261     for (feature, desc) in &rustc_target_features {
262         println!("    {1:0$} - {2}.", max_feature_len, feature, desc);
263     }
264     println!("\nCode-generation features supported by LLVM for this target:");
265     for (feature, desc) in &target_features {
266         println!("    {1:0$} - {2}.", max_feature_len, feature, desc);
267     }
268     if target_features.len() == 0 {
269         println!("    Target features listing is not supported by this LLVM version.");
270     }
271     println!("\nUse +feature to enable a feature, or -feature to disable it.");
272     println!("For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n");
273     println!("Code-generation features cannot be used in cfg or #[target_feature],");
274     println!("and may be renamed or removed in a future version of LLVM or rustc.\n");
275 }
276
277 pub(crate) fn print(req: PrintRequest, sess: &Session) {
278     require_inited();
279     let tm = create_informational_target_machine(sess);
280     match req {
281         PrintRequest::TargetCPUs => unsafe { llvm::LLVMRustPrintTargetCPUs(tm) },
282         PrintRequest::TargetFeatures => print_target_features(sess, tm),
283         _ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req),
284     }
285 }
286
287 fn handle_native(name: &str) -> &str {
288     if name != "native" {
289         return name;
290     }
291
292     unsafe {
293         let mut len = 0;
294         let ptr = llvm::LLVMRustGetHostCPUName(&mut len);
295         str::from_utf8(slice::from_raw_parts(ptr as *const u8, len)).unwrap()
296     }
297 }
298
299 pub fn target_cpu(sess: &Session) -> &str {
300     let name = sess.opts.cg.target_cpu.as_ref().unwrap_or(&sess.target.cpu);
301     handle_native(name)
302 }
303
304 /// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
305 /// `--target` and similar).
306 // FIXME(nagisa): Cache the output of this somehow? Maybe make this a query? We're calling this
307 // for every function that has `#[target_feature]` on it. The global features won't change between
308 // the functions; only crates, maybe…
309 pub fn llvm_global_features(sess: &Session) -> Vec<String> {
310     // FIXME(nagisa): this should definitely be available more centrally and to other codegen backends.
311     /// These features control behaviour of rustc rather than llvm.
312     const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
313
314     // Features that come earlier are overriden by conflicting features later in the string.
315     // Typically we'll want more explicit settings to override the implicit ones, so:
316     //
317     // * Features from -Ctarget-cpu=*; are overriden by [^1]
318     // * Features implied by --target; are overriden by
319     // * Features from -Ctarget-feature; are overriden by
320     // * function specific features.
321     //
322     // [^1]: target-cpu=native is handled here, other target-cpu values are handled implicitly
323     // through LLVM TargetMachine implementation.
324     //
325     // FIXME(nagisa): it isn't clear what's the best interaction between features implied by
326     // `-Ctarget-cpu` and `--target` are. On one hand, you'd expect CLI arguments to always
327     // override anything that's implicit, so e.g. when there's no `--target` flag, features implied
328     // the host target are overriden by `-Ctarget-cpu=*`. On the other hand, what about when both
329     // `--target` and `-Ctarget-cpu=*` are specified? Both then imply some target features and both
330     // flags are specified by the user on the CLI. It isn't as clear-cut which order of precedence
331     // should be taken in cases like these.
332     let mut features = vec![];
333
334     // -Ctarget-cpu=native
335     match sess.opts.cg.target_cpu {
336         Some(ref s) if s == "native" => {
337             let features_string = unsafe {
338                 let ptr = llvm::LLVMGetHostCPUFeatures();
339                 let features_string = if !ptr.is_null() {
340                     CStr::from_ptr(ptr)
341                         .to_str()
342                         .unwrap_or_else(|e| {
343                             bug!("LLVM returned a non-utf8 features string: {}", e);
344                         })
345                         .to_owned()
346                 } else {
347                     bug!("could not allocate host CPU features, LLVM returned a `null` string");
348                 };
349
350                 llvm::LLVMDisposeMessage(ptr);
351
352                 features_string
353             };
354             features.extend(features_string.split(",").map(String::from));
355         }
356         Some(_) | None => {}
357     };
358
359     let filter = |s: &str| {
360         if s.is_empty() {
361             return None;
362         }
363         let feature = if s.starts_with("+") || s.starts_with("-") {
364             &s[1..]
365         } else {
366             return Some(s.to_string());
367         };
368         // Rustc-specific feature requests like `+crt-static` or `-crt-static`
369         // are not passed down to LLVM.
370         if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
371             return None;
372         }
373         // ... otherwise though we run through `to_llvm_feature` feature when
374         // passing requests down to LLVM. This means that all in-language
375         // features also work on the command line instead of having two
376         // different names when the LLVM name and the Rust name differ.
377         Some(format!("{}{}", &s[..1], to_llvm_feature(sess, feature)))
378     };
379
380     // Features implied by an implicit or explicit `--target`.
381     features.extend(sess.target.features.split(',').filter_map(&filter));
382
383     // -Ctarget-features
384     features.extend(sess.opts.cg.target_feature.split(',').filter_map(&filter));
385
386     features
387 }
388
389 pub fn tune_cpu(sess: &Session) -> Option<&str> {
390     let name = sess.opts.debugging_opts.tune_cpu.as_ref()?;
391     Some(handle_native(name))
392 }