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        // Enable the evex512 target feature if an avx512 target feature is enabled.
285        ("x86", s) if s.starts_with("avx512") => {
286            Some(LLVMFeature::with_dependency(s, TargetFeatureFoldStrength::EnableOnly("evex512")))
287        }
288        // Support for `wide-arithmetic` will first land in LLVM 20 as part of
289        // llvm/llvm-project#111598
290        ("wasm32" | "wasm64", "wide-arithmetic") if get_version() < (20, 0, 0) => None,
291        ("sparc", "leoncasa") => Some(LLVMFeature::new("hasleoncasa")),
292        // In LLVM 19, there is no `v8plus` feature and `v9` means "SPARC-V9 instruction available and SPARC-V8+ ABI used".
293        // https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp#L27-L28
294        // Before LLVM 19, there is no `v8plus` feature and `v9` means "SPARC-V9 instruction available".
295        // https://github.com/llvm/llvm-project/blob/llvmorg-18.1.0/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp#L26
296        ("sparc", "v8plus") if get_version().0 == 19 => Some(LLVMFeature::new("v9")),
297        ("sparc", "v8plus") if get_version().0 < 19 => None,
298        ("powerpc", "power8-crypto") => Some(LLVMFeature::new("crypto")),
299        (_, s) => Some(LLVMFeature::new(s)),
300    }
301}
302
303/// Used to generate cfg variables and apply features.
304/// Must express features in the way Rust understands them.
305///
306/// We do not have to worry about RUSTC_SPECIFIC_FEATURES here, those are handled outside codegen.
307pub(crate) fn target_features_cfg(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
308    let mut features: FxHashSet<Symbol> = Default::default();
309
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) by
315    // 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.
318    // We only consider "supported" features; "forbidden" features are not reflected in `cfg` as of now.
319    features.extend(
320        sess.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                // check that all features in a given smallvec are enabled
329                if let Some(feat) = to_llvm_features(sess, feature) {
330                    for llvm_feature in feat {
331                        let cstr = SmallCStr::new(llvm_feature);
332                        if !unsafe { llvm::LLVMRustHasFeature(&target_machine, cstr.as_ptr()) } {
333                            return false;
334                        }
335                    }
336                    true
337                } else {
338                    false
339                }
340            })
341            .map(|(feature, _, _)| Symbol::intern(feature)),
342    );
343
344    // Add enabled features
345    for (enabled, feature) in
346        sess.opts.cg.target_feature.split(',').filter_map(|s| match s.chars().next() {
347            Some('+') => Some((true, Symbol::intern(&s[1..]))),
348            Some('-') => Some((false, Symbol::intern(&s[1..]))),
349            _ => None,
350        })
351    {
352        if enabled {
353            // Also add all transitively implied features.
354
355            // We don't care about the order in `features` since the only thing we use it for is the
356            // `features.contains` below.
357            #[allow(rustc::potential_query_instability)]
358            features.extend(
359                sess.target
360                    .implied_target_features(std::iter::once(feature.as_str()))
361                    .iter()
362                    .map(|s| Symbol::intern(s)),
363            );
364        } else {
365            // Remove transitively reverse-implied features.
366
367            // We don't care about the order in `features` since the only thing we use it for is the
368            // `features.contains` below.
369            #[allow(rustc::potential_query_instability)]
370            features.retain(|f| {
371                if sess
372                    .target
373                    .implied_target_features(std::iter::once(f.as_str()))
374                    .contains(&feature.as_str())
375                {
376                    // If `f` if implies `feature`, then `!feature` implies `!f`, so we have to
377                    // remove `f`. (This is the standard logical contraposition principle.)
378                    false
379                } else {
380                    // We can keep `f`.
381                    true
382                }
383            });
384        }
385    }
386
387    // Filter enabled features based on feature gates
388    sess.target
389        .rust_target_features()
390        .iter()
391        .filter_map(|(feature, gate, _)| {
392            // The `allow_unstable` set is used by rustc internally to determined which target
393            // features are truly available, so we want to return even perma-unstable "forbidden"
394            // features.
395            if allow_unstable
396                || (gate.in_cfg() && (sess.is_nightly_build() || gate.requires_nightly().is_none()))
397            {
398                Some(*feature)
399            } else {
400                None
401            }
402        })
403        .filter(|feature| features.contains(&Symbol::intern(feature)))
404        .map(|feature| Symbol::intern(feature))
405        .collect()
406}
407
408pub(crate) fn print_version() {
409    let (major, minor, patch) = get_version();
410    println!("LLVM version: {major}.{minor}.{patch}");
411}
412
413pub(crate) fn get_version() -> (u32, u32, u32) {
414    // Can be called without initializing LLVM
415    unsafe {
416        (llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor(), llvm::LLVMRustVersionPatch())
417    }
418}
419
420pub(crate) fn print_passes() {
421    // Can be called without initializing LLVM
422    unsafe {
423        llvm::LLVMRustPrintPasses();
424    }
425}
426
427fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
428    let len = unsafe { llvm::LLVMRustGetTargetFeaturesCount(tm) };
429    let mut ret = Vec::with_capacity(len);
430    for i in 0..len {
431        unsafe {
432            let mut feature = ptr::null();
433            let mut desc = ptr::null();
434            llvm::LLVMRustGetTargetFeature(tm, i, &mut feature, &mut desc);
435            if feature.is_null() || desc.is_null() {
436                bug!("LLVM returned a `null` target feature string");
437            }
438            let feature = CStr::from_ptr(feature).to_str().unwrap_or_else(|e| {
439                bug!("LLVM returned a non-utf8 feature string: {}", e);
440            });
441            let desc = CStr::from_ptr(desc).to_str().unwrap_or_else(|e| {
442                bug!("LLVM returned a non-utf8 feature string: {}", e);
443            });
444            ret.push((feature, desc));
445        }
446    }
447    ret
448}
449
450pub(crate) fn print(req: &PrintRequest, out: &mut String, sess: &Session) {
451    require_inited();
452    let tm = create_informational_target_machine(sess, false);
453    match req.kind {
454        PrintKind::TargetCPUs => print_target_cpus(sess, &tm, out),
455        PrintKind::TargetFeatures => print_target_features(sess, &tm, out),
456        _ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req),
457    }
458}
459
460fn print_target_cpus(sess: &Session, tm: &llvm::TargetMachine, out: &mut String) {
461    let cpu_names = llvm::build_string(|s| unsafe {
462        llvm::LLVMRustPrintTargetCPUs(&tm, s);
463    })
464    .unwrap();
465
466    struct Cpu<'a> {
467        cpu_name: &'a str,
468        remark: String,
469    }
470    // Compare CPU against current target to label the default.
471    let target_cpu = handle_native(&sess.target.cpu);
472    let make_remark = |cpu_name| {
473        if cpu_name == target_cpu {
474            // FIXME(#132514): This prints the LLVM target string, which can be
475            // different from the Rust target string. Is that intended?
476            let target = &sess.target.llvm_target;
477            format!(
478                " - This is the default target CPU for the current build target (currently {target})."
479            )
480        } else {
481            "".to_owned()
482        }
483    };
484    let mut cpus = cpu_names
485        .lines()
486        .map(|cpu_name| Cpu { cpu_name, remark: make_remark(cpu_name) })
487        .collect::<VecDeque<_>>();
488
489    // Only print the "native" entry when host and target are the same arch,
490    // since otherwise it could be wrong or misleading.
491    if sess.host.arch == sess.target.arch {
492        let host = get_host_cpu_name();
493        cpus.push_front(Cpu {
494            cpu_name: "native",
495            remark: format!(" - Select the CPU of the current host (currently {host})."),
496        });
497    }
498
499    let max_name_width = cpus.iter().map(|cpu| cpu.cpu_name.len()).max().unwrap_or(0);
500    writeln!(out, "Available CPUs for this target:").unwrap();
501    for Cpu { cpu_name, remark } in cpus {
502        // Only pad the CPU name if there's a remark to print after it.
503        let width = if remark.is_empty() { 0 } else { max_name_width };
504        writeln!(out, "    {cpu_name:<width$}{remark}").unwrap();
505    }
506}
507
508fn print_target_features(sess: &Session, tm: &llvm::TargetMachine, out: &mut String) {
509    let mut llvm_target_features = llvm_target_features(tm);
510    let mut known_llvm_target_features = FxHashSet::<&'static str>::default();
511    let mut rustc_target_features = sess
512        .target
513        .rust_target_features()
514        .iter()
515        .filter_map(|(feature, gate, _implied)| {
516            if !gate.in_cfg() {
517                // Only list (experimentally) supported features.
518                return None;
519            }
520            // LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these
521            // strings.
522            let llvm_feature = to_llvm_features(sess, *feature)?.llvm_feature_name;
523            let desc =
524                match llvm_target_features.binary_search_by_key(&llvm_feature, |(f, _d)| f).ok() {
525                    Some(index) => {
526                        known_llvm_target_features.insert(llvm_feature);
527                        llvm_target_features[index].1
528                    }
529                    None => "",
530                };
531
532            Some((*feature, desc))
533        })
534        .collect::<Vec<_>>();
535
536    // Since we add this at the end ...
537    rustc_target_features.extend_from_slice(&[(
538        "crt-static",
539        "Enables C Run-time Libraries to be statically linked",
540    )]);
541    // ... we need to sort the list again.
542    rustc_target_features.sort();
543
544    llvm_target_features.retain(|(f, _d)| !known_llvm_target_features.contains(f));
545
546    let max_feature_len = llvm_target_features
547        .iter()
548        .chain(rustc_target_features.iter())
549        .map(|(feature, _desc)| feature.len())
550        .max()
551        .unwrap_or(0);
552
553    writeln!(out, "Features supported by rustc for this target:").unwrap();
554    for (feature, desc) in &rustc_target_features {
555        writeln!(out, "    {feature:max_feature_len$} - {desc}.").unwrap();
556    }
557    writeln!(out, "\nCode-generation features supported by LLVM for this target:").unwrap();
558    for (feature, desc) in &llvm_target_features {
559        writeln!(out, "    {feature:max_feature_len$} - {desc}.").unwrap();
560    }
561    if llvm_target_features.is_empty() {
562        writeln!(out, "    Target features listing is not supported by this LLVM version.")
563            .unwrap();
564    }
565    writeln!(out, "\nUse +feature to enable a feature, or -feature to disable it.").unwrap();
566    writeln!(out, "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n")
567        .unwrap();
568    writeln!(out, "Code-generation features cannot be used in cfg or #[target_feature],").unwrap();
569    writeln!(out, "and may be renamed or removed in a future version of LLVM or rustc.\n").unwrap();
570}
571
572/// Returns the host CPU name, according to LLVM.
573fn get_host_cpu_name() -> &'static str {
574    let mut len = 0;
575    // SAFETY: The underlying C++ global function returns a `StringRef` that
576    // isn't tied to any particular backing buffer, so it must be 'static.
577    let slice: &'static [u8] = unsafe {
578        let ptr = llvm::LLVMRustGetHostCPUName(&mut len);
579        assert!(!ptr.is_null());
580        slice::from_raw_parts(ptr, len)
581    };
582    str::from_utf8(slice).expect("host CPU name should be UTF-8")
583}
584
585/// If the given string is `"native"`, returns the host CPU name according to
586/// LLVM. Otherwise, the string is returned as-is.
587fn handle_native(cpu_name: &str) -> &str {
588    match cpu_name {
589        "native" => get_host_cpu_name(),
590        _ => cpu_name,
591    }
592}
593
594pub(crate) fn target_cpu(sess: &Session) -> &str {
595    let cpu_name = sess.opts.cg.target_cpu.as_deref().unwrap_or_else(|| &sess.target.cpu);
596    handle_native(cpu_name)
597}
598
599/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
600/// `--target` and similar).
601pub(crate) fn global_llvm_features(
602    sess: &Session,
603    diagnostics: bool,
604    only_base_features: bool,
605) -> Vec<String> {
606    // Features that come earlier are overridden by conflicting features later in the string.
607    // Typically we'll want more explicit settings to override the implicit ones, so:
608    //
609    // * Features from -Ctarget-cpu=*; are overridden by [^1]
610    // * Features implied by --target; are overridden by
611    // * Features from -Ctarget-feature; are overridden by
612    // * function specific features.
613    //
614    // [^1]: target-cpu=native is handled here, other target-cpu values are handled implicitly
615    // through LLVM TargetMachine implementation.
616    //
617    // FIXME(nagisa): it isn't clear what's the best interaction between features implied by
618    // `-Ctarget-cpu` and `--target` are. On one hand, you'd expect CLI arguments to always
619    // override anything that's implicit, so e.g. when there's no `--target` flag, features implied
620    // the host target are overridden by `-Ctarget-cpu=*`. On the other hand, what about when both
621    // `--target` and `-Ctarget-cpu=*` are specified? Both then imply some target features and both
622    // flags are specified by the user on the CLI. It isn't as clear-cut which order of precedence
623    // should be taken in cases like these.
624    let mut features = vec![];
625
626    // -Ctarget-cpu=native
627    match sess.opts.cg.target_cpu {
628        Some(ref s) if s == "native" => {
629            // We have already figured out the actual CPU name with `LLVMRustGetHostCPUName` and set
630            // that for LLVM, so the features implied by that CPU name will be available everywhere.
631            // However, that is not sufficient: e.g. `skylake` alone is not sufficient to tell if
632            // some of the instructions are available or not. So we have to also explicitly ask for
633            // the exact set of features available on the host, and enable all of them.
634            let features_string = unsafe {
635                let ptr = llvm::LLVMGetHostCPUFeatures();
636                let features_string = if !ptr.is_null() {
637                    CStr::from_ptr(ptr)
638                        .to_str()
639                        .unwrap_or_else(|e| {
640                            bug!("LLVM returned a non-utf8 features string: {}", e);
641                        })
642                        .to_owned()
643                } else {
644                    bug!("could not allocate host CPU features, LLVM returned a `null` string");
645                };
646
647                llvm::LLVMDisposeMessage(ptr);
648
649                features_string
650            };
651            features.extend(features_string.split(',').map(String::from));
652        }
653        Some(_) | None => {}
654    };
655
656    // Features implied by an implicit or explicit `--target`.
657    features.extend(
658        sess.target
659            .features
660            .split(',')
661            .filter(|v| !v.is_empty())
662            // Drop +v8plus feature introduced in LLVM 20.
663            .filter(|v| *v != "+v8plus" || get_version() >= (20, 0, 0))
664            .map(String::from),
665    );
666
667    if wants_wasm_eh(sess) && sess.panic_strategy() == PanicStrategy::Unwind {
668        features.push("+exception-handling".into());
669    }
670
671    // -Ctarget-features
672    if !only_base_features {
673        let known_features = sess.target.rust_target_features();
674        // Will only be filled when `diagnostics` is set!
675        let mut featsmap = FxHashMap::default();
676
677        // Compute implied features
678        let mut all_rust_features = vec![];
679        for feature in sess.opts.cg.target_feature.split(',') {
680            if let Some(feature) = feature.strip_prefix('+') {
681                all_rust_features.extend(
682                    UnordSet::from(sess.target.implied_target_features(std::iter::once(feature)))
683                        .to_sorted_stable_ord()
684                        .iter()
685                        .map(|&&s| (true, s)),
686                )
687            } else if let Some(feature) = feature.strip_prefix('-') {
688                // FIXME: Why do we not remove implied features on "-" here?
689                // We do the equivalent above in `target_features_cfg`.
690                // See <https://github.com/rust-lang/rust/issues/134792>.
691                all_rust_features.push((false, feature));
692            } else if !feature.is_empty() {
693                if diagnostics {
694                    sess.dcx().emit_warn(UnknownCTargetFeaturePrefix { feature });
695                }
696            }
697        }
698        // Remove features that are meant for rustc, not LLVM.
699        all_rust_features.retain(|(_, feature)| {
700            // Retain if it is not a rustc feature
701            !RUSTC_SPECIFIC_FEATURES.contains(feature)
702        });
703
704        // Check feature validity.
705        if diagnostics {
706            for &(enable, feature) in &all_rust_features {
707                let feature_state = known_features.iter().find(|&&(v, _, _)| v == feature);
708                match feature_state {
709                    None => {
710                        let rust_feature =
711                            known_features.iter().find_map(|&(rust_feature, _, _)| {
712                                let llvm_features = to_llvm_features(sess, rust_feature)?;
713                                if llvm_features.contains(feature)
714                                    && !llvm_features.contains(rust_feature)
715                                {
716                                    Some(rust_feature)
717                                } else {
718                                    None
719                                }
720                            });
721                        let unknown_feature = if let Some(rust_feature) = rust_feature {
722                            UnknownCTargetFeature {
723                                feature,
724                                rust_feature: PossibleFeature::Some { rust_feature },
725                            }
726                        } else {
727                            UnknownCTargetFeature { feature, rust_feature: PossibleFeature::None }
728                        };
729                        sess.dcx().emit_warn(unknown_feature);
730                    }
731                    Some((_, stability, _)) => {
732                        if let Err(reason) = stability.toggle_allowed() {
733                            sess.dcx().emit_warn(ForbiddenCTargetFeature {
734                                feature,
735                                enabled: if enable { "enabled" } else { "disabled" },
736                                reason,
737                            });
738                        } else if stability.requires_nightly().is_some() {
739                            // An unstable feature. Warn about using it. It makes little sense
740                            // to hard-error here since we just warn about fully unknown
741                            // features above.
742                            sess.dcx().emit_warn(UnstableCTargetFeature { feature });
743                        }
744                    }
745                }
746
747                // FIXME(nagisa): figure out how to not allocate a full hashset here.
748                featsmap.insert(feature, enable);
749            }
750        }
751
752        // Translate this into LLVM features.
753        let feats = all_rust_features
754            .iter()
755            .filter_map(|&(enable, feature)| {
756                let enable_disable = if enable { '+' } else { '-' };
757                // We run through `to_llvm_features` when
758                // passing requests down to LLVM. This means that all in-language
759                // features also work on the command line instead of having two
760                // different names when the LLVM name and the Rust name differ.
761                let llvm_feature = to_llvm_features(sess, feature)?;
762
763                Some(
764                    std::iter::once(format!(
765                        "{}{}",
766                        enable_disable, llvm_feature.llvm_feature_name
767                    ))
768                    .chain(llvm_feature.dependency.into_iter().filter_map(
769                        move |feat| match (enable, feat) {
770                            (_, TargetFeatureFoldStrength::Both(f))
771                            | (true, TargetFeatureFoldStrength::EnableOnly(f)) => {
772                                Some(format!("{enable_disable}{f}"))
773                            }
774                            _ => None,
775                        },
776                    )),
777                )
778            })
779            .flatten();
780        features.extend(feats);
781
782        if diagnostics && let Some(f) = check_tied_features(sess, &featsmap) {
783            sess.dcx().emit_err(rustc_codegen_ssa::errors::TargetFeatureDisableOrEnable {
784                features: f,
785                span: None,
786                missing_features: None,
787            });
788        }
789    }
790
791    // -Zfixed-x18
792    if sess.opts.unstable_opts.fixed_x18 {
793        if sess.target.arch != "aarch64" {
794            sess.dcx().emit_fatal(FixedX18InvalidArch { arch: &sess.target.arch });
795        } else {
796            features.push("+reserve-x18".into());
797        }
798    }
799
800    features
801}
802
803pub(crate) fn tune_cpu(sess: &Session) -> Option<&str> {
804    let name = sess.opts.unstable_opts.tune_cpu.as_ref()?;
805    Some(handle_native(name))
806}