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