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