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