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