]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/llvm_util.rs
Remove Config::stderr
[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 libloading::Library;
5 use rustc_codegen_ssa::target_features::{supported_target_features, tied_target_features};
6 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7 use rustc_fs_util::path_to_c_string;
8 use rustc_middle::bug;
9 use rustc_session::config::PrintRequest;
10 use rustc_session::Session;
11 use rustc_span::symbol::Symbol;
12 use rustc_target::spec::{MergeFunctions, PanicStrategy};
13 use std::ffi::{CStr, CString};
14 use tracing::debug;
15
16 use std::mem;
17 use std::path::Path;
18 use std::ptr;
19 use std::slice;
20 use std::str;
21 use std::sync::Once;
22
23 static INIT: Once = Once::new();
24
25 pub(crate) fn init(sess: &Session) {
26     unsafe {
27         // Before we touch LLVM, make sure that multithreading is enabled.
28         if llvm::LLVMIsMultithreaded() != 1 {
29             bug!("LLVM compiled without support for threads");
30         }
31         INIT.call_once(|| {
32             configure_llvm(sess);
33         });
34     }
35 }
36
37 fn require_inited() {
38     if !INIT.is_completed() {
39         bug!("LLVM is not initialized");
40     }
41 }
42
43 unsafe fn configure_llvm(sess: &Session) {
44     let n_args = sess.opts.cg.llvm_args.len() + sess.target.llvm_args.len();
45     let mut llvm_c_strs = Vec::with_capacity(n_args + 1);
46     let mut llvm_args = Vec::with_capacity(n_args + 1);
47
48     llvm::LLVMRustInstallFatalErrorHandler();
49     // On Windows, an LLVM assertion will open an Abort/Retry/Ignore dialog
50     // box for the purpose of launching a debugger. However, on CI this will
51     // cause it to hang until it times out, which can take several hours.
52     if std::env::var_os("CI").is_some() {
53         llvm::LLVMRustDisableSystemDialogsOnCrash();
54     }
55
56     fn llvm_arg_to_arg_name(full_arg: &str) -> &str {
57         full_arg.trim().split(|c: char| c == '=' || c.is_whitespace()).next().unwrap_or("")
58     }
59
60     let cg_opts = sess.opts.cg.llvm_args.iter();
61     let tg_opts = sess.target.llvm_args.iter();
62     let sess_args = cg_opts.chain(tg_opts);
63
64     let user_specified_args: FxHashSet<_> =
65         sess_args.clone().map(|s| llvm_arg_to_arg_name(s)).filter(|s| !s.is_empty()).collect();
66
67     {
68         // This adds the given argument to LLVM. Unless `force` is true
69         // user specified arguments are *not* overridden.
70         let mut add = |arg: &str, force: bool| {
71             if force || !user_specified_args.contains(llvm_arg_to_arg_name(arg)) {
72                 let s = CString::new(arg).unwrap();
73                 llvm_args.push(s.as_ptr());
74                 llvm_c_strs.push(s);
75             }
76         };
77         // Set the llvm "program name" to make usage and invalid argument messages more clear.
78         add("rustc -Cllvm-args=\"...\" with", true);
79         if sess.time_llvm_passes() {
80             add("-time-passes", false);
81         }
82         if sess.print_llvm_passes() {
83             add("-debug-pass=Structure", false);
84         }
85         if sess.target.generate_arange_section
86             && !sess.opts.debugging_opts.no_generate_arange_section
87         {
88             add("-generate-arange-section", false);
89         }
90
91         // Disable the machine outliner by default in LLVM versions 11 and LLVM
92         // version 12, where it leads to miscompilation.
93         //
94         // Ref:
95         // - https://github.com/rust-lang/rust/issues/85351
96         // - https://reviews.llvm.org/D103167
97         if llvm_util::get_version() < (13, 0, 0) {
98             add("-enable-machine-outliner=never", false);
99         }
100
101         match sess.opts.debugging_opts.merge_functions.unwrap_or(sess.target.merge_functions) {
102             MergeFunctions::Disabled | MergeFunctions::Trampolines => {}
103             MergeFunctions::Aliases => {
104                 add("-mergefunc-use-aliases", false);
105             }
106         }
107
108         if sess.target.os == "emscripten" && sess.panic_strategy() == PanicStrategy::Unwind {
109             add("-enable-emscripten-cxx-exceptions", false);
110         }
111
112         // HACK(eddyb) LLVM inserts `llvm.assume` calls to preserve align attributes
113         // during inlining. Unfortunately these may block other optimizations.
114         add("-preserve-alignment-assumptions-during-inlining=false", false);
115
116         // Use non-zero `import-instr-limit` multiplier for cold callsites.
117         add("-import-cold-multiplier=0.1", false);
118
119         for arg in sess_args {
120             add(&(*arg), true);
121         }
122     }
123
124     if sess.opts.debugging_opts.llvm_time_trace {
125         llvm::LLVMTimeTraceProfilerInitialize();
126     }
127
128     llvm::LLVMInitializePasses();
129
130     // Use the legacy plugin registration if we don't use the new pass manager
131     if !should_use_new_llvm_pass_manager(
132         &sess.opts.debugging_opts.new_llvm_pass_manager,
133         &sess.target.arch,
134     ) {
135         // Register LLVM plugins by loading them into the compiler process.
136         for plugin in &sess.opts.debugging_opts.llvm_plugins {
137             let lib = Library::new(plugin).unwrap_or_else(|e| bug!("couldn't load plugin: {}", e));
138             debug!("LLVM plugin loaded successfully {:?} ({})", lib, plugin);
139
140             // Intentionally leak the dynamic library. We can't ever unload it
141             // since the library can make things that will live arbitrarily long.
142             mem::forget(lib);
143         }
144     }
145
146     rustc_llvm::initialize_available_targets();
147
148     llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());
149 }
150
151 pub fn time_trace_profiler_finish(file_name: &Path) {
152     unsafe {
153         let file_name = path_to_c_string(file_name);
154         llvm::LLVMTimeTraceProfilerFinish(file_name.as_ptr());
155     }
156 }
157
158 // WARNING: the features after applying `to_llvm_feature` must be known
159 // to LLVM or the feature detection code will walk past the end of the feature
160 // array, leading to crashes.
161 // To find a list of LLVM's names, check llvm-project/llvm/include/llvm/Support/*TargetParser.def
162 // where the * matches the architecture's name
163 // Beware to not use the llvm github project for this, but check the git submodule
164 // found in src/llvm-project
165 // Though note that Rust can also be build with an external precompiled version of LLVM
166 // which might lead to failures if the oldest tested / supported LLVM version
167 // doesn't yet support the relevant intrinsics
168 pub fn to_llvm_feature<'a>(sess: &Session, s: &'a str) -> Vec<&'a str> {
169     let arch = if sess.target.arch == "x86_64" { "x86" } else { &*sess.target.arch };
170     match (arch, s) {
171         ("x86", "sse4.2") => {
172             if get_version() >= (14, 0, 0) {
173                 vec!["sse4.2", "crc32"]
174             } else {
175                 vec!["sse4.2"]
176             }
177         }
178         ("x86", "pclmulqdq") => vec!["pclmul"],
179         ("x86", "rdrand") => vec!["rdrnd"],
180         ("x86", "bmi1") => vec!["bmi"],
181         ("x86", "cmpxchg16b") => vec!["cx16"],
182         ("x86", "avx512vaes") => vec!["vaes"],
183         ("x86", "avx512gfni") => vec!["gfni"],
184         ("x86", "avx512vpclmulqdq") => vec!["vpclmulqdq"],
185         ("aarch64", "fp") => vec!["fp-armv8"],
186         ("aarch64", "fp16") => vec!["fullfp16"],
187         ("aarch64", "fhm") => vec!["fp16fml"],
188         ("aarch64", "rcpc2") => vec!["rcpc-immo"],
189         ("aarch64", "dpb") => vec!["ccpp"],
190         ("aarch64", "dpb2") => vec!["ccdp"],
191         ("aarch64", "frintts") => vec!["fptoint"],
192         ("aarch64", "fcma") => vec!["complxnum"],
193         ("aarch64", "pmuv3") => vec!["perfmon"],
194         ("aarch64", "paca") => vec!["pauth"],
195         ("aarch64", "pacg") => vec!["pauth"],
196         (_, s) => vec![s],
197     }
198 }
199
200 // Given a map from target_features to whether they are enabled or disabled,
201 // ensure only valid combinations are allowed.
202 pub fn check_tied_features(
203     sess: &Session,
204     features: &FxHashMap<&str, bool>,
205 ) -> Option<&'static [&'static str]> {
206     for tied in tied_target_features(sess) {
207         // Tied features must be set to the same value, or not set at all
208         let mut tied_iter = tied.iter();
209         let enabled = features.get(tied_iter.next().unwrap());
210
211         if tied_iter.any(|f| enabled != features.get(f)) {
212             return Some(tied);
213         }
214     }
215     None
216 }
217
218 pub fn target_features(sess: &Session) -> Vec<Symbol> {
219     let target_machine = create_informational_target_machine(sess);
220     supported_target_features(sess)
221         .iter()
222         .filter_map(
223             |&(feature, gate)| {
224                 if sess.is_nightly_build() || gate.is_none() { Some(feature) } else { None }
225             },
226         )
227         .filter(|feature| {
228             for llvm_feature in to_llvm_feature(sess, feature) {
229                 let cstr = CString::new(llvm_feature).unwrap();
230                 if unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) } {
231                     return true;
232                 }
233             }
234             false
235         })
236         .map(|feature| Symbol::intern(feature))
237         .collect()
238 }
239
240 pub fn print_version() {
241     let (major, minor, patch) = get_version();
242     println!("LLVM version: {}.{}.{}", major, minor, patch);
243 }
244
245 pub fn get_version() -> (u32, u32, u32) {
246     // Can be called without initializing LLVM
247     unsafe {
248         (llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor(), llvm::LLVMRustVersionPatch())
249     }
250 }
251
252 /// Returns `true` if this LLVM is Rust's bundled LLVM (and not system LLVM).
253 pub fn is_rust_llvm() -> bool {
254     // Can be called without initializing LLVM
255     unsafe { llvm::LLVMRustIsRustLLVM() }
256 }
257
258 pub fn print_passes() {
259     // Can be called without initializing LLVM
260     unsafe {
261         llvm::LLVMRustPrintPasses();
262     }
263 }
264
265 fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
266     let len = unsafe { llvm::LLVMRustGetTargetFeaturesCount(tm) };
267     let mut ret = Vec::with_capacity(len);
268     for i in 0..len {
269         unsafe {
270             let mut feature = ptr::null();
271             let mut desc = ptr::null();
272             llvm::LLVMRustGetTargetFeature(tm, i, &mut feature, &mut desc);
273             if feature.is_null() || desc.is_null() {
274                 bug!("LLVM returned a `null` target feature string");
275             }
276             let feature = CStr::from_ptr(feature).to_str().unwrap_or_else(|e| {
277                 bug!("LLVM returned a non-utf8 feature string: {}", e);
278             });
279             let desc = CStr::from_ptr(desc).to_str().unwrap_or_else(|e| {
280                 bug!("LLVM returned a non-utf8 feature string: {}", e);
281             });
282             ret.push((feature, desc));
283         }
284     }
285     ret
286 }
287
288 fn print_target_features(sess: &Session, tm: &llvm::TargetMachine) {
289     let mut target_features = llvm_target_features(tm);
290     let mut rustc_target_features = supported_target_features(sess)
291         .iter()
292         .filter_map(|(feature, _gate)| {
293             for llvm_feature in to_llvm_feature(sess, *feature) {
294                 // LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these strings.
295                 match target_features.binary_search_by_key(&llvm_feature, |(f, _d)| (*f)).ok().map(
296                     |index| {
297                         let (_f, desc) = target_features.remove(index);
298                         (*feature, desc)
299                     },
300                 ) {
301                     Some(v) => return Some(v),
302                     None => {}
303                 }
304             }
305             None
306         })
307         .collect::<Vec<_>>();
308     rustc_target_features.extend_from_slice(&[(
309         "crt-static",
310         "Enables C Run-time Libraries to be statically linked",
311     )]);
312     let max_feature_len = target_features
313         .iter()
314         .chain(rustc_target_features.iter())
315         .map(|(feature, _desc)| feature.len())
316         .max()
317         .unwrap_or(0);
318
319     println!("Features supported by rustc for this target:");
320     for (feature, desc) in &rustc_target_features {
321         println!("    {1:0$} - {2}.", max_feature_len, feature, desc);
322     }
323     println!("\nCode-generation features supported by LLVM for this target:");
324     for (feature, desc) in &target_features {
325         println!("    {1:0$} - {2}.", max_feature_len, feature, desc);
326     }
327     if target_features.is_empty() {
328         println!("    Target features listing is not supported by this LLVM version.");
329     }
330     println!("\nUse +feature to enable a feature, or -feature to disable it.");
331     println!("For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n");
332     println!("Code-generation features cannot be used in cfg or #[target_feature],");
333     println!("and may be renamed or removed in a future version of LLVM or rustc.\n");
334 }
335
336 pub(crate) fn print(req: PrintRequest, sess: &Session) {
337     require_inited();
338     let tm = create_informational_target_machine(sess);
339     match req {
340         PrintRequest::TargetCPUs => unsafe { llvm::LLVMRustPrintTargetCPUs(tm) },
341         PrintRequest::TargetFeatures => print_target_features(sess, tm),
342         _ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req),
343     }
344 }
345
346 fn handle_native(name: &str) -> &str {
347     if name != "native" {
348         return name;
349     }
350
351     unsafe {
352         let mut len = 0;
353         let ptr = llvm::LLVMRustGetHostCPUName(&mut len);
354         str::from_utf8(slice::from_raw_parts(ptr as *const u8, len)).unwrap()
355     }
356 }
357
358 pub fn target_cpu(sess: &Session) -> &str {
359     let name = sess.opts.cg.target_cpu.as_ref().unwrap_or(&sess.target.cpu);
360     handle_native(name)
361 }
362
363 /// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
364 /// `--target` and similar).
365 // FIXME(nagisa): Cache the output of this somehow? Maybe make this a query? We're calling this
366 // for every function that has `#[target_feature]` on it. The global features won't change between
367 // the functions; only crates, maybe…
368 pub fn llvm_global_features(sess: &Session) -> Vec<String> {
369     // FIXME(nagisa): this should definitely be available more centrally and to other codegen backends.
370     /// These features control behaviour of rustc rather than llvm.
371     const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
372
373     // Features that come earlier are overriden by conflicting features later in the string.
374     // Typically we'll want more explicit settings to override the implicit ones, so:
375     //
376     // * Features from -Ctarget-cpu=*; are overriden by [^1]
377     // * Features implied by --target; are overriden by
378     // * Features from -Ctarget-feature; are overriden by
379     // * function specific features.
380     //
381     // [^1]: target-cpu=native is handled here, other target-cpu values are handled implicitly
382     // through LLVM TargetMachine implementation.
383     //
384     // FIXME(nagisa): it isn't clear what's the best interaction between features implied by
385     // `-Ctarget-cpu` and `--target` are. On one hand, you'd expect CLI arguments to always
386     // override anything that's implicit, so e.g. when there's no `--target` flag, features implied
387     // the host target are overriden by `-Ctarget-cpu=*`. On the other hand, what about when both
388     // `--target` and `-Ctarget-cpu=*` are specified? Both then imply some target features and both
389     // flags are specified by the user on the CLI. It isn't as clear-cut which order of precedence
390     // should be taken in cases like these.
391     let mut features = vec![];
392
393     // -Ctarget-cpu=native
394     match sess.opts.cg.target_cpu {
395         Some(ref s) if s == "native" => {
396             let features_string = unsafe {
397                 let ptr = llvm::LLVMGetHostCPUFeatures();
398                 let features_string = if !ptr.is_null() {
399                     CStr::from_ptr(ptr)
400                         .to_str()
401                         .unwrap_or_else(|e| {
402                             bug!("LLVM returned a non-utf8 features string: {}", e);
403                         })
404                         .to_owned()
405                 } else {
406                     bug!("could not allocate host CPU features, LLVM returned a `null` string");
407                 };
408
409                 llvm::LLVMDisposeMessage(ptr);
410
411                 features_string
412             };
413             features.extend(features_string.split(',').map(String::from));
414         }
415         Some(_) | None => {}
416     };
417
418     fn strip(s: &str) -> &str {
419         s.strip_prefix(&['+', '-']).unwrap_or(s)
420     }
421
422     let filter = |s: &str| {
423         if s.is_empty() {
424             return vec![];
425         }
426         let feature = strip(s);
427         if feature == s {
428             return vec![s.to_string()];
429         }
430
431         // Rustc-specific feature requests like `+crt-static` or `-crt-static`
432         // are not passed down to LLVM.
433         if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
434             return vec![];
435         }
436         // ... otherwise though we run through `to_llvm_feature` feature when
437         // passing requests down to LLVM. This means that all in-language
438         // features also work on the command line instead of having two
439         // different names when the LLVM name and the Rust name differ.
440         to_llvm_feature(sess, feature).iter().map(|f| format!("{}{}", &s[..1], f)).collect()
441     };
442
443     // Features implied by an implicit or explicit `--target`.
444     features.extend(sess.target.features.split(',').flat_map(&filter));
445
446     // -Ctarget-features
447     let feats: Vec<&str> = sess.opts.cg.target_feature.split(',').collect();
448     // LLVM enables based on the last occurence of a feature
449     if let Some(f) =
450         check_tied_features(sess, &feats.iter().map(|f| (strip(f), !f.starts_with("-"))).collect())
451     {
452         sess.err(&format!(
453             "Target features {} must all be enabled or disabled together",
454             f.join(", ")
455         ));
456     }
457     features.extend(feats.iter().flat_map(|&f| filter(f)));
458     features
459 }
460
461 pub fn tune_cpu(sess: &Session) -> Option<&str> {
462     let name = sess.opts.debugging_opts.tune_cpu.as_ref()?;
463     Some(handle_native(name))
464 }
465
466 pub(crate) fn should_use_new_llvm_pass_manager(user_opt: &Option<bool>, target_arch: &str) -> bool {
467     // The new pass manager is enabled by default for LLVM >= 13.
468     // This matches Clang, which also enables it since Clang 13.
469
470     // FIXME: There are some perf issues with the new pass manager
471     // when targeting s390x, so it is temporarily disabled for that
472     // arch, see https://github.com/rust-lang/rust/issues/89609
473     user_opt.unwrap_or_else(|| target_arch != "s390x" && llvm_util::get_version() >= (13, 0, 0))
474 }