rustc_codegen_llvm/
llvm_util.rs

1use std::collections::VecDeque;
2use std::ffi::{CStr, CString};
3use std::fmt::Write;
4use std::path::Path;
5use std::sync::Once;
6use std::{ptr, slice, str};
7
8use libc::c_int;
9use rustc_codegen_ssa::base::wants_wasm_eh;
10use rustc_codegen_ssa::target_features::cfg_target_feature;
11use rustc_codegen_ssa::{TargetConfig, target_features};
12use rustc_data_structures::fx::FxHashSet;
13use rustc_data_structures::small_c_str::SmallCStr;
14use rustc_fs_util::path_to_c_string;
15use rustc_middle::bug;
16use rustc_session::Session;
17use rustc_session::config::{PrintKind, PrintRequest};
18use rustc_target::spec::{
19    Abi, Arch, Env, MergeFunctions, Os, PanicStrategy, SmallDataThresholdSupport,
20};
21use smallvec::{SmallVec, smallvec};
22
23use crate::back::write::create_informational_target_machine;
24use crate::{errors, llvm};
25
26static INIT: Once = Once::new();
27
28pub(crate) fn init(sess: &Session) {
29    unsafe {
30        // Before we touch LLVM, make sure that multithreading is enabled.
31        if !llvm::LLVMIsMultithreaded().is_true() {
32            bug!("LLVM compiled without support for threads");
33        }
34        INIT.call_once(|| {
35            configure_llvm(sess);
36        });
37    }
38}
39
40fn require_inited() {
41    if !INIT.is_completed() {
42        bug!("LLVM is not initialized");
43    }
44}
45
46unsafe 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    unsafe {
52        llvm::LLVMRustInstallErrorHandlers();
53    }
54    // On Windows, an LLVM assertion will open an Abort/Retry/Ignore dialog
55    // box for the purpose of launching a debugger. However, on CI this will
56    // cause it to hang until it times out, which can take several hours.
57    if std::env::var_os("CI").is_some() {
58        unsafe {
59            llvm::LLVMRustDisableSystemDialogsOnCrash();
60        }
61    }
62
63    fn llvm_arg_to_arg_name(full_arg: &str) -> &str {
64        full_arg.trim().split(|c: char| c == '=' || c.is_whitespace()).next().unwrap_or("")
65    }
66
67    let cg_opts = sess.opts.cg.llvm_args.iter().map(AsRef::as_ref);
68    let tg_opts = sess.target.llvm_args.iter().map(AsRef::as_ref);
69    let sess_args = cg_opts.chain(tg_opts);
70
71    let user_specified_args: FxHashSet<_> =
72        sess_args.clone().map(|s| llvm_arg_to_arg_name(s)).filter(|s| !s.is_empty()).collect();
73
74    {
75        // This adds the given argument to LLVM. Unless `force` is true
76        // user specified arguments are *not* overridden.
77        let mut add = |arg: &str, force: bool| {
78            if force || !user_specified_args.contains(llvm_arg_to_arg_name(arg)) {
79                let s = CString::new(arg).unwrap();
80                llvm_args.push(s.as_ptr());
81                llvm_c_strs.push(s);
82            }
83        };
84        // Set the llvm "program name" to make usage and invalid argument messages more clear.
85        add("rustc -Cllvm-args=\"...\" with", true);
86        if sess.opts.unstable_opts.time_llvm_passes {
87            add("-time-passes", false);
88        }
89        if sess.opts.unstable_opts.print_llvm_passes {
90            add("-debug-pass=Structure", false);
91        }
92        if sess.target.generate_arange_section
93            && !sess.opts.unstable_opts.no_generate_arange_section
94        {
95            add("-generate-arange-section", false);
96        }
97
98        match sess.opts.unstable_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 wants_wasm_eh(sess) {
106            add("-wasm-enable-eh", false);
107        }
108
109        if sess.target.os == Os::Emscripten
110            && !sess.opts.unstable_opts.emscripten_wasm_eh
111            && sess.panic_strategy().unwinds()
112        {
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        if sess.print_llvm_stats() {
124            add("-stats", false);
125        }
126
127        for arg in sess_args {
128            add(&(*arg), true);
129        }
130
131        match (
132            sess.opts.unstable_opts.small_data_threshold,
133            sess.target.small_data_threshold_support(),
134        ) {
135            // Set up the small-data optimization limit for architectures that use
136            // an LLVM argument to control this.
137            (Some(threshold), SmallDataThresholdSupport::LlvmArg(arg)) => {
138                add(&format!("--{arg}={threshold}"), false)
139            }
140            _ => (),
141        };
142    }
143
144    if sess.opts.unstable_opts.llvm_time_trace {
145        unsafe { llvm::LLVMRustTimeTraceProfilerInitialize() };
146    }
147
148    rustc_llvm::initialize_available_targets();
149
150    unsafe { llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr()) };
151}
152
153pub(crate) fn time_trace_profiler_finish(file_name: &Path) {
154    unsafe {
155        let file_name = path_to_c_string(file_name);
156        llvm::LLVMRustTimeTraceProfilerFinish(file_name.as_ptr());
157    }
158}
159
160enum TargetFeatureFoldStrength<'a> {
161    // The feature is only tied when enabling the feature, disabling
162    // this feature shouldn't disable the tied feature.
163    EnableOnly(&'a str),
164    // The feature is tied for both enabling and disabling this feature.
165    Both(&'a str),
166}
167
168impl<'a> TargetFeatureFoldStrength<'a> {
169    fn as_str(&self) -> &'a str {
170        match self {
171            TargetFeatureFoldStrength::EnableOnly(feat) => feat,
172            TargetFeatureFoldStrength::Both(feat) => feat,
173        }
174    }
175}
176
177pub(crate) struct LLVMFeature<'a> {
178    llvm_feature_name: &'a str,
179    dependencies: SmallVec<[TargetFeatureFoldStrength<'a>; 1]>,
180}
181
182impl<'a> LLVMFeature<'a> {
183    fn new(llvm_feature_name: &'a str) -> Self {
184        Self { llvm_feature_name, dependencies: SmallVec::new() }
185    }
186
187    fn with_dependencies(
188        llvm_feature_name: &'a str,
189        dependencies: SmallVec<[TargetFeatureFoldStrength<'a>; 1]>,
190    ) -> Self {
191        Self { llvm_feature_name, dependencies }
192    }
193}
194
195impl<'a> IntoIterator for LLVMFeature<'a> {
196    type Item = &'a str;
197    type IntoIter = impl Iterator<Item = &'a str>;
198
199    fn into_iter(self) -> Self::IntoIter {
200        let dependencies = self.dependencies.into_iter().map(|feat| feat.as_str());
201        std::iter::once(self.llvm_feature_name).chain(dependencies)
202    }
203}
204
205/// Convert a Rust feature name to an LLVM feature name. Returning `None` means the
206/// feature should be skipped, usually because it is not supported by the current
207/// LLVM version.
208///
209/// WARNING: the features after applying `to_llvm_features` must be known
210/// to LLVM or the feature detection code will walk past the end of the feature
211/// array, leading to crashes.
212///
213/// To find a list of LLVM's names, see llvm-project/llvm/lib/Target/{ARCH}/*.td
214/// where `{ARCH}` is the architecture name. Look for instances of `SubtargetFeature`.
215///
216/// Check the current rustc fork of LLVM in the repo at
217/// <https://github.com/rust-lang/llvm-project/>. The commit in use can be found via the
218/// `llvm-project` submodule in <https://github.com/rust-lang/rust/tree/HEAD/src> Though note that
219/// Rust can also be build with an external precompiled version of LLVM which might lead to failures
220/// if the oldest tested / supported LLVM version doesn't yet support the relevant intrinsics.
221pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFeature<'a>> {
222    let (major, _, _) = get_version();
223    match sess.target.arch {
224        Arch::AArch64 | Arch::Arm64EC => {
225            match s {
226                "rcpc2" => Some(LLVMFeature::new("rcpc-immo")),
227                "dpb" => Some(LLVMFeature::new("ccpp")),
228                "dpb2" => Some(LLVMFeature::new("ccdp")),
229                "frintts" => Some(LLVMFeature::new("fptoint")),
230                "fcma" => Some(LLVMFeature::new("complxnum")),
231                "pmuv3" => Some(LLVMFeature::new("perfmon")),
232                "paca" => Some(LLVMFeature::new("pauth")),
233                "pacg" => Some(LLVMFeature::new("pauth")),
234                "flagm2" => Some(LLVMFeature::new("altnzcv")),
235                // Rust ties fp and neon together.
236                "neon" => Some(LLVMFeature::with_dependencies(
237                    "neon",
238                    smallvec![TargetFeatureFoldStrength::Both("fp-armv8")],
239                )),
240                // In LLVM neon implicitly enables fp, but we manually enable
241                // neon when a feature only implicitly enables fp
242                "fhm" => Some(LLVMFeature::new("fp16fml")),
243                "fp16" => Some(LLVMFeature::new("fullfp16")),
244                // Filter out features that are not supported by the current LLVM version
245                "fpmr" => None, // only existed in 18
246                // Withdrawn by ARM; removed from LLVM in 22
247                "tme" if major >= 22 => None,
248                s => Some(LLVMFeature::new(s)),
249            }
250        }
251        Arch::Arm => match s {
252            "fp16" => Some(LLVMFeature::new("fullfp16")),
253            s => Some(LLVMFeature::new(s)),
254        },
255
256        // Filter out features that are not supported by the current LLVM version
257        Arch::LoongArch32 | Arch::LoongArch64 => match s {
258            "32s" if major < 21 => None,
259            s => Some(LLVMFeature::new(s)),
260        },
261        Arch::PowerPC | Arch::PowerPC64 => match s {
262            "power8-crypto" => Some(LLVMFeature::new("crypto")),
263            s => Some(LLVMFeature::new(s)),
264        },
265        Arch::RiscV32 | Arch::RiscV64 => match s {
266            // Filter out Rust-specific *virtual* target feature
267            "zkne_or_zknd" => None,
268            s => Some(LLVMFeature::new(s)),
269        },
270        Arch::Sparc | Arch::Sparc64 => match s {
271            "leoncasa" => Some(LLVMFeature::new("hasleoncasa")),
272            s => Some(LLVMFeature::new(s)),
273        },
274        Arch::Wasm32 | Arch::Wasm64 => match s {
275            "gc" if major < 22 => None,
276            s => Some(LLVMFeature::new(s)),
277        },
278        Arch::X86 | Arch::X86_64 => {
279            match s {
280                "sse4.2" => Some(LLVMFeature::with_dependencies(
281                    "sse4.2",
282                    smallvec![TargetFeatureFoldStrength::EnableOnly("crc32")],
283                )),
284                "pclmulqdq" => Some(LLVMFeature::new("pclmul")),
285                "rdrand" => Some(LLVMFeature::new("rdrnd")),
286                "bmi1" => Some(LLVMFeature::new("bmi")),
287                "cmpxchg16b" => Some(LLVMFeature::new("cx16")),
288                "lahfsahf" => Some(LLVMFeature::new("sahf")),
289                // Enable the evex512 target feature if an avx512 target feature is enabled.
290                s if s.starts_with("avx512") => Some(LLVMFeature::with_dependencies(
291                    s,
292                    smallvec![TargetFeatureFoldStrength::EnableOnly("evex512")],
293                )),
294                "avx10.1" => Some(LLVMFeature::new("avx10.1-512")),
295                "avx10.2" => Some(LLVMFeature::new("avx10.2-512")),
296                "apxf" => Some(LLVMFeature::with_dependencies(
297                    "egpr",
298                    smallvec![
299                        TargetFeatureFoldStrength::Both("push2pop2"),
300                        TargetFeatureFoldStrength::Both("ppx"),
301                        TargetFeatureFoldStrength::Both("ndd"),
302                        TargetFeatureFoldStrength::Both("ccmp"),
303                        TargetFeatureFoldStrength::Both("cf"),
304                        TargetFeatureFoldStrength::Both("nf"),
305                        TargetFeatureFoldStrength::Both("zu"),
306                    ],
307                )),
308                s => Some(LLVMFeature::new(s)),
309            }
310        }
311        _ => Some(LLVMFeature::new(s)),
312    }
313}
314
315/// Used to generate cfg variables and apply features.
316/// Must express features in the way Rust understands them.
317///
318/// We do not have to worry about RUSTC_SPECIFIC_FEATURES here, those are handled outside codegen.
319pub(crate) fn target_config(sess: &Session) -> TargetConfig {
320    let target_machine = create_informational_target_machine(sess, true);
321
322    let (unstable_target_features, target_features) = cfg_target_feature(
323        sess,
324        |feature| {
325            to_llvm_features(sess, feature)
326                .map(|f| SmallVec::<[&str; 2]>::from_iter(f.into_iter()))
327                .unwrap_or_default()
328        },
329        |feature| {
330            // This closure determines whether the target CPU has the feature according to LLVM. We do
331            // *not* consider the `-Ctarget-feature`s here, as that will be handled later in
332            // `cfg_target_feature`.
333            if let Some(feat) = to_llvm_features(sess, feature) {
334                // All the LLVM features this expands to must be enabled.
335                for llvm_feature in feat {
336                    let cstr = SmallCStr::new(llvm_feature);
337                    // `LLVMRustHasFeature` is moderately expensive. On targets with many
338                    // features (e.g. x86) these calls take a non-trivial fraction of runtime
339                    // when compiling very small programs.
340                    if !unsafe { llvm::LLVMRustHasFeature(target_machine.raw(), cstr.as_ptr()) } {
341                        return false;
342                    }
343                }
344                true
345            } else {
346                false
347            }
348        },
349    );
350
351    let mut cfg = TargetConfig {
352        target_features,
353        unstable_target_features,
354        has_reliable_f16: true,
355        has_reliable_f16_math: true,
356        has_reliable_f128: true,
357        has_reliable_f128_math: true,
358    };
359
360    update_target_reliable_float_cfg(sess, &mut cfg);
361    cfg
362}
363
364/// Determine whether or not experimental float types are reliable based on known bugs.
365fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
366    let target_arch = &sess.target.arch;
367    let target_os = &sess.target.options.os;
368    let target_env = &sess.target.options.env;
369    let target_abi = &sess.target.options.abi;
370    let target_pointer_width = sess.target.pointer_width;
371    let version = get_version();
372    let (major, _, _) = version;
373
374    cfg.has_reliable_f16 = match (target_arch, target_os) {
375        // LLVM crash without neon <https://github.com/llvm/llvm-project/issues/129394> (fixed in LLVM 20.1.1)
376        (Arch::AArch64, _)
377            if !cfg.target_features.iter().any(|f| f.as_str() == "neon")
378                && version < (20, 1, 1) =>
379        {
380            false
381        }
382        // Unsupported <https://github.com/llvm/llvm-project/issues/94434> (fixed in llvm22)
383        (Arch::Arm64EC, _) if major < 22 => false,
384        // Selection failure <https://github.com/llvm/llvm-project/issues/50374> (fixed in llvm21)
385        (Arch::S390x, _) if major < 21 => false,
386        // MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
387        (Arch::X86_64, Os::Windows) if *target_env == Env::Gnu && *target_abi != Abi::Llvm => false,
388        // Infinite recursion <https://github.com/llvm/llvm-project/issues/97981>
389        (Arch::CSky, _) if major < 22 => false, // (fixed in llvm22)
390        (Arch::Hexagon, _) if major < 21 => false, // (fixed in llvm21)
391        (Arch::LoongArch32 | Arch::LoongArch64, _) if major < 21 => false, // (fixed in llvm21)
392        (Arch::PowerPC | Arch::PowerPC64, _) if major < 22 => false, // (fixed in llvm22)
393        (Arch::Sparc | Arch::Sparc64, _) if major < 22 => false, // (fixed in llvm22)
394        (Arch::Wasm32 | Arch::Wasm64, _) if major < 22 => false, // (fixed in llvm22)
395        // `f16` support only requires that symbols converting to and from `f32` are available. We
396        // provide these in `compiler-builtins`, so `f16` should be available on all platforms that
397        // do not have other ABI issues or LLVM crashes.
398        _ => true,
399    };
400
401    cfg.has_reliable_f128 = match (target_arch, target_os) {
402        // Unsupported https://github.com/llvm/llvm-project/issues/121122
403        (Arch::AmdGpu, _) => false,
404        // Unsupported <https://github.com/llvm/llvm-project/issues/94434>
405        (Arch::Arm64EC, _) => false,
406        // Selection bug <https://github.com/llvm/llvm-project/issues/96432> (fixed in LLVM 20.1.0)
407        (Arch::Mips64 | Arch::Mips64r6, _) if version < (20, 1, 0) => false,
408        // Selection bug <https://github.com/llvm/llvm-project/issues/95471>. This issue is closed
409        // but basic math still does not work.
410        (Arch::Nvptx64, _) => false,
411        // ABI bugs <https://github.com/rust-lang/rust/issues/125109> et al. (full
412        // list at <https://github.com/rust-lang/rust/issues/116909>)
413        (Arch::PowerPC | Arch::PowerPC64, _) => false,
414        // ABI unsupported  <https://github.com/llvm/llvm-project/issues/41838>
415        (Arch::Sparc, _) => false,
416        // Stack alignment bug <https://github.com/llvm/llvm-project/issues/77401>. NB: tests may
417        // not fail if our compiler-builtins is linked. (fixed in llvm21)
418        (Arch::X86, _) if major < 21 => false,
419        // MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
420        (Arch::X86_64, Os::Windows) if *target_env == Env::Gnu && *target_abi != Abi::Llvm => false,
421        // There are no known problems on other platforms, so the only requirement is that symbols
422        // are available. `compiler-builtins` provides all symbols required for core `f128`
423        // support, so this should work for everything else.
424        _ => true,
425    };
426
427    // Assume that working `f16` means working `f16` math for most platforms, since
428    // operations just go through `f32`.
429    cfg.has_reliable_f16_math = cfg.has_reliable_f16;
430
431    cfg.has_reliable_f128_math = match (target_arch, target_os) {
432        // LLVM lowers `fp128` math to `long double` symbols even on platforms where
433        // `long double` is not IEEE binary128. See
434        // <https://github.com/llvm/llvm-project/issues/44744>.
435        //
436        // This rules out anything that doesn't have `long double` = `binary128`; <= 32 bits
437        // (ld is `f64`), anything other than Linux (Windows and MacOS use `f64`), and `x86`
438        // (ld is 80-bit extended precision).
439        //
440        // musl does not implement the symbols required for f128 math at all.
441        _ if *target_env == Env::Musl => false,
442        (Arch::X86_64, _) => false,
443        (_, Os::Linux) if target_pointer_width == 64 => true,
444        _ => false,
445    } && cfg.has_reliable_f128;
446}
447
448pub(crate) fn print_version() {
449    let (major, minor, patch) = get_version();
450    println!("LLVM version: {major}.{minor}.{patch}");
451}
452
453pub(crate) fn get_version() -> (u32, u32, u32) {
454    // Can be called without initializing LLVM
455    unsafe {
456        (llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor(), llvm::LLVMRustVersionPatch())
457    }
458}
459
460pub(crate) fn print_passes() {
461    // Can be called without initializing LLVM
462    unsafe {
463        llvm::LLVMRustPrintPasses();
464    }
465}
466
467fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
468    let len = unsafe { llvm::LLVMRustGetTargetFeaturesCount(tm) };
469    let mut ret = Vec::with_capacity(len);
470    for i in 0..len {
471        unsafe {
472            let mut feature = ptr::null();
473            let mut desc = ptr::null();
474            llvm::LLVMRustGetTargetFeature(tm, i, &mut feature, &mut desc);
475            if feature.is_null() || desc.is_null() {
476                bug!("LLVM returned a `null` target feature string");
477            }
478            let feature = CStr::from_ptr(feature).to_str().unwrap_or_else(|e| {
479                bug!("LLVM returned a non-utf8 feature string: {}", e);
480            });
481            let desc = CStr::from_ptr(desc).to_str().unwrap_or_else(|e| {
482                bug!("LLVM returned a non-utf8 feature string: {}", e);
483            });
484            ret.push((feature, desc));
485        }
486    }
487    ret
488}
489
490pub(crate) fn print(req: &PrintRequest, out: &mut String, sess: &Session) {
491    require_inited();
492    let tm = create_informational_target_machine(sess, false);
493    match req.kind {
494        PrintKind::TargetCPUs => print_target_cpus(sess, tm.raw(), out),
495        PrintKind::TargetFeatures => print_target_features(sess, tm.raw(), out),
496        _ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req),
497    }
498}
499
500fn print_target_cpus(sess: &Session, tm: &llvm::TargetMachine, out: &mut String) {
501    let cpu_names = llvm::build_string(|s| unsafe {
502        llvm::LLVMRustPrintTargetCPUs(&tm, s);
503    })
504    .unwrap();
505
506    struct Cpu<'a> {
507        cpu_name: &'a str,
508        remark: String,
509    }
510    // Compare CPU against current target to label the default.
511    let target_cpu = handle_native(&sess.target.cpu);
512    let make_remark = |cpu_name| {
513        if cpu_name == target_cpu {
514            // FIXME(#132514): This prints the LLVM target string, which can be
515            // different from the Rust target string. Is that intended?
516            let target = &sess.target.llvm_target;
517            format!(
518                " - This is the default target CPU for the current build target (currently {target})."
519            )
520        } else {
521            "".to_owned()
522        }
523    };
524    let mut cpus = cpu_names
525        .lines()
526        .map(|cpu_name| Cpu { cpu_name, remark: make_remark(cpu_name) })
527        .collect::<VecDeque<_>>();
528
529    // Only print the "native" entry when host and target are the same arch,
530    // since otherwise it could be wrong or misleading.
531    if sess.host.arch == sess.target.arch {
532        let host = get_host_cpu_name();
533        cpus.push_front(Cpu {
534            cpu_name: "native",
535            remark: format!(" - Select the CPU of the current host (currently {host})."),
536        });
537    }
538
539    let max_name_width = cpus.iter().map(|cpu| cpu.cpu_name.len()).max().unwrap_or(0);
540    writeln!(out, "Available CPUs for this target:").unwrap();
541    for Cpu { cpu_name, remark } in cpus {
542        // Only pad the CPU name if there's a remark to print after it.
543        let width = if remark.is_empty() { 0 } else { max_name_width };
544        writeln!(out, "    {cpu_name:<width$}{remark}").unwrap();
545    }
546}
547
548fn print_target_features(sess: &Session, tm: &llvm::TargetMachine, out: &mut String) {
549    let mut llvm_target_features = llvm_target_features(tm);
550    let mut known_llvm_target_features = FxHashSet::<&'static str>::default();
551    let mut rustc_target_features = sess
552        .target
553        .rust_target_features()
554        .iter()
555        .filter_map(|(feature, gate, _implied)| {
556            if !gate.in_cfg() {
557                // Only list (experimentally) supported features.
558                return None;
559            }
560            // LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these
561            // strings.
562            let llvm_feature = to_llvm_features(sess, *feature)?.llvm_feature_name;
563            let desc =
564                match llvm_target_features.binary_search_by_key(&llvm_feature, |(f, _d)| f).ok() {
565                    Some(index) => {
566                        known_llvm_target_features.insert(llvm_feature);
567                        llvm_target_features[index].1
568                    }
569                    None => "",
570                };
571
572            Some((*feature, desc))
573        })
574        .collect::<Vec<_>>();
575
576    // Since we add this at the end ...
577    rustc_target_features.extend_from_slice(&[(
578        "crt-static",
579        "Enables C Run-time Libraries to be statically linked",
580    )]);
581    // ... we need to sort the list again.
582    rustc_target_features.sort();
583
584    llvm_target_features.retain(|(f, _d)| !known_llvm_target_features.contains(f));
585
586    let max_feature_len = llvm_target_features
587        .iter()
588        .chain(rustc_target_features.iter())
589        .map(|(feature, _desc)| feature.len())
590        .max()
591        .unwrap_or(0);
592
593    writeln!(out, "Features supported by rustc for this target:").unwrap();
594    for (feature, desc) in &rustc_target_features {
595        writeln!(out, "    {feature:max_feature_len$} - {desc}.").unwrap();
596    }
597    writeln!(out, "\nCode-generation features supported by LLVM for this target:").unwrap();
598    for (feature, desc) in &llvm_target_features {
599        writeln!(out, "    {feature:max_feature_len$} - {desc}.").unwrap();
600    }
601    if llvm_target_features.is_empty() {
602        writeln!(out, "    Target features listing is not supported by this LLVM version.")
603            .unwrap();
604    }
605    writeln!(out, "\nUse +feature to enable a feature, or -feature to disable it.").unwrap();
606    writeln!(out, "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n")
607        .unwrap();
608    writeln!(out, "Code-generation features cannot be used in cfg or #[target_feature],").unwrap();
609    writeln!(out, "and may be renamed or removed in a future version of LLVM or rustc.\n").unwrap();
610}
611
612/// Returns the host CPU name, according to LLVM.
613fn get_host_cpu_name() -> &'static str {
614    let mut len = 0;
615    // SAFETY: The underlying C++ global function returns a `StringRef` that
616    // isn't tied to any particular backing buffer, so it must be 'static.
617    let slice: &'static [u8] = unsafe {
618        let ptr = llvm::LLVMRustGetHostCPUName(&mut len);
619        assert!(!ptr.is_null());
620        slice::from_raw_parts(ptr, len)
621    };
622    str::from_utf8(slice).expect("host CPU name should be UTF-8")
623}
624
625/// If the given string is `"native"`, returns the host CPU name according to
626/// LLVM. Otherwise, the string is returned as-is.
627fn handle_native(cpu_name: &str) -> &str {
628    match cpu_name {
629        "native" => get_host_cpu_name(),
630        _ => cpu_name,
631    }
632}
633
634pub(crate) fn target_cpu(sess: &Session) -> &str {
635    let cpu_name = sess.opts.cg.target_cpu.as_deref().unwrap_or_else(|| &sess.target.cpu);
636    handle_native(cpu_name)
637}
638
639/// The target features for compiler flags other than `-Ctarget-features`.
640fn llvm_features_by_flags(sess: &Session, features: &mut Vec<String>) {
641    if wants_wasm_eh(sess) && sess.panic_strategy() == PanicStrategy::Unwind {
642        features.push("+exception-handling".into());
643    }
644
645    target_features::retpoline_features_by_flags(sess, features);
646
647    // -Zfixed-x18
648    if sess.opts.unstable_opts.fixed_x18 {
649        if sess.target.arch != Arch::AArch64 {
650            sess.dcx().emit_fatal(errors::FixedX18InvalidArch { arch: sess.target.arch.desc() });
651        } else {
652            features.push("+reserve-x18".into());
653        }
654    }
655}
656
657/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
658/// `--target` and similar).
659pub(crate) fn global_llvm_features(sess: &Session, only_base_features: bool) -> Vec<String> {
660    // Features that come earlier are overridden by conflicting features later in the string.
661    // Typically we'll want more explicit settings to override the implicit ones, so:
662    //
663    // * Features from -Ctarget-cpu=*; are overridden by [^1]
664    // * Features implied by --target; are overridden by
665    // * Features from -Ctarget-feature; are overridden by
666    // * function specific features.
667    //
668    // [^1]: target-cpu=native is handled here, other target-cpu values are handled implicitly
669    // through LLVM TargetMachine implementation.
670    //
671    // FIXME(nagisa): it isn't clear what's the best interaction between features implied by
672    // `-Ctarget-cpu` and `--target` are. On one hand, you'd expect CLI arguments to always
673    // override anything that's implicit, so e.g. when there's no `--target` flag, features implied
674    // the host target are overridden by `-Ctarget-cpu=*`. On the other hand, what about when both
675    // `--target` and `-Ctarget-cpu=*` are specified? Both then imply some target features and both
676    // flags are specified by the user on the CLI. It isn't as clear-cut which order of precedence
677    // should be taken in cases like these.
678    let mut features = vec![];
679
680    // -Ctarget-cpu=native
681    match sess.opts.cg.target_cpu {
682        Some(ref s) if s == "native" => {
683            // We have already figured out the actual CPU name with `LLVMRustGetHostCPUName` and set
684            // that for LLVM, so the features implied by that CPU name will be available everywhere.
685            // However, that is not sufficient: e.g. `skylake` alone is not sufficient to tell if
686            // some of the instructions are available or not. So we have to also explicitly ask for
687            // the exact set of features available on the host, and enable all of them.
688            let features_string = unsafe {
689                let ptr = llvm::LLVMGetHostCPUFeatures();
690                let features_string = if !ptr.is_null() {
691                    CStr::from_ptr(ptr)
692                        .to_str()
693                        .unwrap_or_else(|e| {
694                            bug!("LLVM returned a non-utf8 features string: {}", e);
695                        })
696                        .to_owned()
697                } else {
698                    bug!("could not allocate host CPU features, LLVM returned a `null` string");
699                };
700
701                llvm::LLVMDisposeMessage(ptr);
702
703                features_string
704            };
705            features.extend(features_string.split(',').map(String::from));
706        }
707        Some(_) | None => {}
708    };
709
710    let mut extend_backend_features = |feature: &str, enable: bool| {
711        let enable_disable = if enable { '+' } else { '-' };
712        // We run through `to_llvm_features` when
713        // passing requests down to LLVM. This means that all in-language
714        // features also work on the command line instead of having two
715        // different names when the LLVM name and the Rust name differ.
716        let Some(llvm_feature) = to_llvm_features(sess, feature) else { return };
717
718        features.extend(
719            std::iter::once(format!("{}{}", enable_disable, llvm_feature.llvm_feature_name)).chain(
720                llvm_feature.dependencies.into_iter().filter_map(move |feat| {
721                    match (enable, feat) {
722                        (_, TargetFeatureFoldStrength::Both(f))
723                        | (true, TargetFeatureFoldStrength::EnableOnly(f)) => {
724                            Some(format!("{enable_disable}{f}"))
725                        }
726                        _ => None,
727                    }
728                }),
729            ),
730        );
731    };
732
733    // Features implied by an implicit or explicit `--target`.
734    target_features::target_spec_to_backend_features(sess, &mut extend_backend_features);
735
736    // -Ctarget-features
737    if !only_base_features {
738        target_features::flag_to_backend_features(sess, extend_backend_features);
739    }
740
741    // We add this in the "base target" so that these show up in `sess.unstable_target_features`.
742    llvm_features_by_flags(sess, &mut features);
743
744    features
745}
746
747pub(crate) fn tune_cpu(sess: &Session) -> Option<&str> {
748    let name = sess.opts.unstable_opts.tune_cpu.as_ref()?;
749    Some(handle_native(name))
750}