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