2024-11-02 12:40:28 +11:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
use std::ffi::{CStr, CString};
|
2019-02-18 03:58:58 +09:00
|
|
|
use std::fmt::Write;
|
2022-10-30 19:26:12 +01:00
|
|
|
use std::path::Path;
|
2022-08-25 15:42:20 +02:00
|
|
|
use std::sync::Once;
|
2023-07-13 16:56:29 -07:00
|
|
|
use std::{ptr, slice, str};
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2017-04-30 20:33:25 +02:00
|
|
|
use libc::c_int;
|
2024-02-21 17:32:58 -06:00
|
|
|
use rustc_codegen_ssa::base::wants_wasm_eh;
|
2025-05-11 12:50:48 +02:00
|
|
|
use rustc_codegen_ssa::target_features::cfg_target_feature;
|
2025-05-10 22:53:38 +02:00
|
|
|
use rustc_codegen_ssa::{TargetConfig, target_features};
|
2025-05-23 08:07:42 +02:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2021-07-23 16:19:22 +03:00
|
|
|
use rustc_data_structures::small_c_str::SmallCStr;
|
2021-12-13 00:00:00 +00:00
|
|
|
use rustc_fs_util::path_to_c_string;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::bug;
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_session::Session;
|
2023-07-16 17:20:28 -07:00
|
|
|
use rustc_session::config::{PrintKind, PrintRequest};
|
2024-09-10 12:19:16 -07:00
|
|
|
use rustc_target::spec::{MergeFunctions, PanicStrategy, SmallDataThresholdSupport};
|
2025-04-09 16:40:50 +05:30
|
|
|
use smallvec::{SmallVec, smallvec};
|
2017-04-30 20:33:25 +02:00
|
|
|
|
2024-03-30 13:19:58 +00:00
|
|
|
use crate::back::write::create_informational_target_machine;
|
2025-05-23 08:07:42 +02:00
|
|
|
use crate::{errors, llvm};
|
2017-05-14 20:33:37 +02:00
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
|
|
pub(crate) fn init(sess: &Session) {
|
2017-04-30 20:33:25 +02:00
|
|
|
unsafe {
|
|
|
|
|
// Before we touch LLVM, make sure that multithreading is enabled.
|
2021-10-12 00:00:00 +00:00
|
|
|
if llvm::LLVMIsMultithreaded() != 1 {
|
|
|
|
|
bug!("LLVM compiled without support for threads");
|
|
|
|
|
}
|
2017-04-30 20:33:25 +02:00
|
|
|
INIT.call_once(|| {
|
|
|
|
|
configure_llvm(sess);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
fn require_inited() {
|
2021-10-12 00:00:00 +00:00
|
|
|
if !INIT.is_completed() {
|
|
|
|
|
bug!("LLVM is not initialized");
|
2017-10-30 18:42:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-30 20:33:25 +02:00
|
|
|
unsafe fn configure_llvm(sess: &Session) {
|
2020-11-08 14:27:51 +03:00
|
|
|
let n_args = sess.opts.cg.llvm_args.len() + sess.target.llvm_args.len();
|
2018-10-08 16:55:04 +02:00
|
|
|
let mut llvm_c_strs = Vec::with_capacity(n_args + 1);
|
|
|
|
|
let mut llvm_args = Vec::with_capacity(n_args + 1);
|
2017-04-30 20:33:25 +02:00
|
|
|
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMRustInstallErrorHandlers();
|
|
|
|
|
}
|
2022-02-03 07:03:44 -08:00
|
|
|
// On Windows, an LLVM assertion will open an Abort/Retry/Ignore dialog
|
|
|
|
|
// box for the purpose of launching a debugger. However, on CI this will
|
|
|
|
|
// cause it to hang until it times out, which can take several hours.
|
|
|
|
|
if std::env::var_os("CI").is_some() {
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMRustDisableSystemDialogsOnCrash();
|
|
|
|
|
}
|
2022-02-03 07:03:44 -08:00
|
|
|
}
|
2018-10-12 15:35:55 -04:00
|
|
|
|
2019-12-18 14:19:03 +01:00
|
|
|
fn llvm_arg_to_arg_name(full_arg: &str) -> &str {
|
|
|
|
|
full_arg.trim().split(|c: char| c == '=' || c.is_whitespace()).next().unwrap_or("")
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 11:43:05 +01:00
|
|
|
let cg_opts = sess.opts.cg.llvm_args.iter().map(AsRef::as_ref);
|
|
|
|
|
let tg_opts = sess.target.llvm_args.iter().map(AsRef::as_ref);
|
2020-01-17 16:11:52 -08:00
|
|
|
let sess_args = cg_opts.chain(tg_opts);
|
2020-01-09 16:40:40 +01:00
|
|
|
|
|
|
|
|
let user_specified_args: FxHashSet<_> =
|
2020-02-28 14:20:33 +01:00
|
|
|
sess_args.clone().map(|s| llvm_arg_to_arg_name(s)).filter(|s| !s.is_empty()).collect();
|
2019-12-18 14:19:03 +01:00
|
|
|
|
2017-04-30 20:33:25 +02:00
|
|
|
{
|
2019-12-18 14:19:03 +01:00
|
|
|
// This adds the given argument to LLVM. Unless `force` is true
|
|
|
|
|
// user specified arguments are *not* overridden.
|
|
|
|
|
let mut add = |arg: &str, force: bool| {
|
|
|
|
|
if force || !user_specified_args.contains(llvm_arg_to_arg_name(arg)) {
|
|
|
|
|
let s = CString::new(arg).unwrap();
|
|
|
|
|
llvm_args.push(s.as_ptr());
|
|
|
|
|
llvm_c_strs.push(s);
|
|
|
|
|
}
|
2017-04-30 20:33:25 +02:00
|
|
|
};
|
2020-08-12 15:30:41 -07:00
|
|
|
// Set the llvm "program name" to make usage and invalid argument messages more clear.
|
|
|
|
|
add("rustc -Cllvm-args=\"...\" with", true);
|
2022-12-20 15:02:15 +01:00
|
|
|
if sess.opts.unstable_opts.time_llvm_passes {
|
2019-12-18 14:19:03 +01:00
|
|
|
add("-time-passes", false);
|
|
|
|
|
}
|
2022-12-20 15:02:15 +01:00
|
|
|
if sess.opts.unstable_opts.print_llvm_passes {
|
2019-12-18 14:19:03 +01:00
|
|
|
add("-debug-pass=Structure", false);
|
|
|
|
|
}
|
2021-11-10 10:47:00 -08:00
|
|
|
if sess.target.generate_arange_section
|
2022-07-06 07:44:47 -05:00
|
|
|
&& !sess.opts.unstable_opts.no_generate_arange_section
|
2021-11-01 14:16:25 -07:00
|
|
|
{
|
2019-12-18 14:19:03 +01:00
|
|
|
add("-generate-arange-section", false);
|
2019-11-18 15:05:01 -08:00
|
|
|
}
|
2021-06-05 14:35:19 +03:00
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
match sess.opts.unstable_opts.merge_functions.unwrap_or(sess.target.merge_functions) {
|
2020-04-14 12:10:58 -07:00
|
|
|
MergeFunctions::Disabled | MergeFunctions::Trampolines => {}
|
|
|
|
|
MergeFunctions::Aliases => {
|
|
|
|
|
add("-mergefunc-use-aliases", false);
|
2018-12-31 10:58:13 -08:00
|
|
|
}
|
2018-11-29 23:05:23 +01:00
|
|
|
}
|
2017-04-30 20:33:25 +02:00
|
|
|
|
2024-02-22 16:52:48 -06:00
|
|
|
if wants_wasm_eh(sess) {
|
|
|
|
|
add("-wasm-enable-eh", false);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 13:00:35 +02:00
|
|
|
if sess.target.os == "emscripten"
|
|
|
|
|
&& !sess.opts.unstable_opts.emscripten_wasm_eh
|
|
|
|
|
&& sess.panic_strategy() == PanicStrategy::Unwind
|
|
|
|
|
{
|
2019-12-18 14:19:03 +01:00
|
|
|
add("-enable-emscripten-cxx-exceptions", false);
|
2019-10-18 14:47:54 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-21 00:30:35 +01:00
|
|
|
// HACK(eddyb) LLVM inserts `llvm.assume` calls to preserve align attributes
|
|
|
|
|
// during inlining. Unfortunately these may block other optimizations.
|
2019-12-18 14:19:03 +01:00
|
|
|
add("-preserve-alignment-assumptions-during-inlining=false", false);
|
2018-12-21 00:30:35 +01:00
|
|
|
|
Import small cold functions
The Rust code is often written under an assumption that for generic
methods inline attribute is mostly unnecessary, since for optimized
builds using ThinLTO, a method will be generated in at least one CGU and
available for import.
For example, deref implementations for Box, Vec, MutexGuard, and
MutexGuard are not currently marked as inline, neither is identity
implementation of From trait.
In PGO builds, when functions are determined to be cold, the default
multiplier of zero will stop the import, even for completely trivial
functions.
Increase slightly the default multiplier from 0 to 0.1 to import them
regardless.
2021-03-11 00:00:00 +00:00
|
|
|
// Use non-zero `import-instr-limit` multiplier for cold callsites.
|
|
|
|
|
add("-import-cold-multiplier=0.1", false);
|
2018-12-21 00:30:35 +01:00
|
|
|
|
2022-11-05 01:08:57 -07:00
|
|
|
if sess.print_llvm_stats() {
|
|
|
|
|
add("-stats", false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 16:11:52 -08:00
|
|
|
for arg in sess_args {
|
2019-12-18 14:19:03 +01:00
|
|
|
add(&(*arg), true);
|
2017-04-30 20:33:25 +02:00
|
|
|
}
|
2024-09-10 12:19:16 -07:00
|
|
|
|
|
|
|
|
match (
|
|
|
|
|
sess.opts.unstable_opts.small_data_threshold,
|
|
|
|
|
sess.target.small_data_threshold_support(),
|
|
|
|
|
) {
|
|
|
|
|
// Set up the small-data optimization limit for architectures that use
|
|
|
|
|
// an LLVM argument to control this.
|
|
|
|
|
(Some(threshold), SmallDataThresholdSupport::LlvmArg(arg)) => {
|
|
|
|
|
add(&format!("--{arg}={threshold}"), false)
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
};
|
2017-04-30 20:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
if sess.opts.unstable_opts.llvm_time_trace {
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe { llvm::LLVMRustTimeTraceProfilerInitialize() };
|
2020-01-31 18:58:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-10-13 10:17:05 +02:00
|
|
|
rustc_llvm::initialize_available_targets();
|
2017-04-30 20:33:25 +02:00
|
|
|
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe { llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr()) };
|
2017-04-30 20:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) fn time_trace_profiler_finish(file_name: &Path) {
|
2020-01-31 18:58:28 -05:00
|
|
|
unsafe {
|
2021-12-13 00:00:00 +00:00
|
|
|
let file_name = path_to_c_string(file_name);
|
2023-11-21 13:43:11 -05:00
|
|
|
llvm::LLVMRustTimeTraceProfilerFinish(file_name.as_ptr());
|
2020-01-31 18:58:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 22:26:42 +10:00
|
|
|
enum TargetFeatureFoldStrength<'a> {
|
2023-05-22 14:46:40 +01:00
|
|
|
// The feature is only tied when enabling the feature, disabling
|
|
|
|
|
// this feature shouldn't disable the tied feature.
|
|
|
|
|
EnableOnly(&'a str),
|
|
|
|
|
// The feature is tied for both enabling and disabling this feature.
|
|
|
|
|
Both(&'a str),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> TargetFeatureFoldStrength<'a> {
|
|
|
|
|
fn as_str(&self) -> &'a str {
|
|
|
|
|
match self {
|
|
|
|
|
TargetFeatureFoldStrength::EnableOnly(feat) => feat,
|
|
|
|
|
TargetFeatureFoldStrength::Both(feat) => feat,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) struct LLVMFeature<'a> {
|
|
|
|
|
llvm_feature_name: &'a str,
|
2025-04-09 16:40:50 +05:30
|
|
|
dependencies: SmallVec<[TargetFeatureFoldStrength<'a>; 1]>,
|
2023-05-22 14:46:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> LLVMFeature<'a> {
|
2024-07-06 22:26:42 +10:00
|
|
|
fn new(llvm_feature_name: &'a str) -> Self {
|
2025-04-09 16:40:50 +05:30
|
|
|
Self { llvm_feature_name, dependencies: SmallVec::new() }
|
2023-05-22 14:46:40 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-09 16:40:50 +05:30
|
|
|
fn with_dependencies(
|
2023-05-22 14:46:40 +01:00
|
|
|
llvm_feature_name: &'a str,
|
2025-04-09 16:40:50 +05:30
|
|
|
dependencies: SmallVec<[TargetFeatureFoldStrength<'a>; 1]>,
|
2023-05-22 14:46:40 +01:00
|
|
|
) -> Self {
|
2025-04-09 16:40:50 +05:30
|
|
|
Self { llvm_feature_name, dependencies }
|
2023-05-22 14:46:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> IntoIterator for LLVMFeature<'a> {
|
|
|
|
|
type Item = &'a str;
|
|
|
|
|
type IntoIter = impl Iterator<Item = &'a str>;
|
|
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
2025-04-09 16:40:50 +05:30
|
|
|
let dependencies = self.dependencies.into_iter().map(|feat| feat.as_str());
|
2023-05-22 14:46:40 +01:00
|
|
|
std::iter::once(self.llvm_feature_name).chain(dependencies)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-23 08:07:42 +02:00
|
|
|
/// Convert a Rust feature name to an LLVM feature name. Returning `None` means the
|
|
|
|
|
/// feature should be skipped, usually because it is not supported by the current
|
|
|
|
|
/// LLVM version.
|
|
|
|
|
///
|
|
|
|
|
/// WARNING: the features after applying `to_llvm_features` must be known
|
|
|
|
|
/// to LLVM or the feature detection code will walk past the end of the feature
|
|
|
|
|
/// array, leading to crashes.
|
|
|
|
|
///
|
|
|
|
|
/// To find a list of LLVM's names, see llvm-project/llvm/lib/Target/{ARCH}/*.td
|
|
|
|
|
/// where `{ARCH}` is the architecture name. Look for instances of `SubtargetFeature`.
|
|
|
|
|
///
|
2025-06-12 14:04:39 +02:00
|
|
|
/// Check the current rustc fork of LLVM in the repo at
|
|
|
|
|
/// <https://github.com/rust-lang/llvm-project/>. The commit in use can be found via the
|
|
|
|
|
/// `llvm-project` submodule in <https://github.com/rust-lang/rust/tree/master/src> Though note that
|
|
|
|
|
/// Rust can also be build with an external precompiled version of LLVM which might lead to failures
|
|
|
|
|
/// if the oldest tested / supported LLVM version doesn't yet support the relevant intrinsics.
|
2024-08-09 16:41:43 +01:00
|
|
|
pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFeature<'a>> {
|
Add arm64ec-pc-windows-msvc target
Introduces the `arm64ec-pc-windows-msvc` target for building Arm64EC ("Emulation Compatible") binaries for Windows.
For more information about Arm64EC see <https://learn.microsoft.com/en-us/windows/arm/arm64ec>.
Tier 3 policy:
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
I will be the maintainer for this target.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
Target uses the `arm64ec` architecture to match LLVM and MSVC, and the `-pc-windows-msvc` suffix to indicate that it targets Windows via the MSVC environment.
> Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
Target name exactly specifies the type of code that will be produced.
> If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.
Done.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
> The target must not introduce license incompatibilities.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
Understood.
> The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
> Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
> "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
> This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
Understood, I am not a member of the Rust team.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
Both `core` and `alloc` are supported.
Support for `std` dependends on making changes to the standard library, `stdarch` and `backtrace` which cannot be done yet as the bootstrapping compiler raises a warning ("unexpected `cfg` condition value") for `target_arch = "arm64ec"`.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation is provided in src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
> Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
> In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.
Understood.
2023-12-15 16:46:34 -08:00
|
|
|
let arch = if sess.target.arch == "x86_64" {
|
|
|
|
|
"x86"
|
|
|
|
|
} else if sess.target.arch == "arm64ec" {
|
|
|
|
|
"aarch64"
|
2024-11-09 03:17:24 +09:00
|
|
|
} else if sess.target.arch == "sparc64" {
|
|
|
|
|
"sparc"
|
2024-12-09 00:41:35 +05:30
|
|
|
} else if sess.target.arch == "powerpc64" {
|
|
|
|
|
"powerpc"
|
Add arm64ec-pc-windows-msvc target
Introduces the `arm64ec-pc-windows-msvc` target for building Arm64EC ("Emulation Compatible") binaries for Windows.
For more information about Arm64EC see <https://learn.microsoft.com/en-us/windows/arm/arm64ec>.
Tier 3 policy:
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
I will be the maintainer for this target.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
Target uses the `arm64ec` architecture to match LLVM and MSVC, and the `-pc-windows-msvc` suffix to indicate that it targets Windows via the MSVC environment.
> Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
Target name exactly specifies the type of code that will be produced.
> If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.
Done.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
> The target must not introduce license incompatibilities.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
Understood.
> The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
> Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
> "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
> This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
Understood, I am not a member of the Rust team.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
Both `core` and `alloc` are supported.
Support for `std` dependends on making changes to the standard library, `stdarch` and `backtrace` which cannot be done yet as the bootstrapping compiler raises a warning ("unexpected `cfg` condition value") for `target_arch = "arm64ec"`.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation is provided in src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
> Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
> In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.
Understood.
2023-12-15 16:46:34 -08:00
|
|
|
} else {
|
|
|
|
|
&*sess.target.arch
|
|
|
|
|
};
|
2018-02-27 02:05:58 +00:00
|
|
|
match (arch, s) {
|
2025-04-09 16:40:50 +05:30
|
|
|
("x86", "sse4.2") => Some(LLVMFeature::with_dependencies(
|
2024-08-09 16:41:43 +01:00
|
|
|
"sse4.2",
|
2025-04-09 16:40:50 +05:30
|
|
|
smallvec![TargetFeatureFoldStrength::EnableOnly("crc32")],
|
2024-08-09 16:41:43 +01:00
|
|
|
)),
|
|
|
|
|
("x86", "pclmulqdq") => Some(LLVMFeature::new("pclmul")),
|
|
|
|
|
("x86", "rdrand") => Some(LLVMFeature::new("rdrnd")),
|
|
|
|
|
("x86", "bmi1") => Some(LLVMFeature::new("bmi")),
|
|
|
|
|
("x86", "cmpxchg16b") => Some(LLVMFeature::new("cx16")),
|
|
|
|
|
("x86", "lahfsahf") => Some(LLVMFeature::new("sahf")),
|
|
|
|
|
("aarch64", "rcpc2") => Some(LLVMFeature::new("rcpc-immo")),
|
|
|
|
|
("aarch64", "dpb") => Some(LLVMFeature::new("ccpp")),
|
|
|
|
|
("aarch64", "dpb2") => Some(LLVMFeature::new("ccdp")),
|
|
|
|
|
("aarch64", "frintts") => Some(LLVMFeature::new("fptoint")),
|
|
|
|
|
("aarch64", "fcma") => Some(LLVMFeature::new("complxnum")),
|
|
|
|
|
("aarch64", "pmuv3") => Some(LLVMFeature::new("perfmon")),
|
|
|
|
|
("aarch64", "paca") => Some(LLVMFeature::new("pauth")),
|
|
|
|
|
("aarch64", "pacg") => Some(LLVMFeature::new("pauth")),
|
2024-09-03 13:51:54 +01:00
|
|
|
// Before LLVM 20 those two features were packaged together as b16b16
|
|
|
|
|
("aarch64", "sve-b16b16") if get_version().0 < 20 => Some(LLVMFeature::new("b16b16")),
|
|
|
|
|
("aarch64", "sme-b16b16") if get_version().0 < 20 => Some(LLVMFeature::new("b16b16")),
|
2024-08-09 16:41:43 +01:00
|
|
|
("aarch64", "flagm2") => Some(LLVMFeature::new("altnzcv")),
|
2023-01-17 15:49:20 +00:00
|
|
|
// Rust ties fp and neon together.
|
2025-04-09 16:40:50 +05:30
|
|
|
("aarch64", "neon") => Some(LLVMFeature::with_dependencies(
|
|
|
|
|
"neon",
|
|
|
|
|
smallvec![TargetFeatureFoldStrength::Both("fp-armv8")],
|
|
|
|
|
)),
|
2023-01-17 15:49:20 +00:00
|
|
|
// In LLVM neon implicitly enables fp, but we manually enable
|
|
|
|
|
// neon when a feature only implicitly enables fp
|
2024-08-09 16:41:43 +01:00
|
|
|
("aarch64", "fhm") => Some(LLVMFeature::new("fp16fml")),
|
|
|
|
|
("aarch64", "fp16") => Some(LLVMFeature::new("fullfp16")),
|
|
|
|
|
// Filter out features that are not supported by the current LLVM version
|
2025-04-02 12:08:01 -07:00
|
|
|
("aarch64", "fpmr") => None, // only existed in 18
|
2025-02-07 18:08:19 +00:00
|
|
|
("arm", "fp16") => Some(LLVMFeature::new("fullfp16")),
|
2025-02-26 20:06:25 -07:00
|
|
|
// NVPTX targets added in LLVM 20
|
|
|
|
|
("nvptx64", "sm_100") if get_version().0 < 20 => None,
|
|
|
|
|
("nvptx64", "sm_100a") if get_version().0 < 20 => None,
|
|
|
|
|
("nvptx64", "sm_101") if get_version().0 < 20 => None,
|
|
|
|
|
("nvptx64", "sm_101a") if get_version().0 < 20 => None,
|
|
|
|
|
("nvptx64", "sm_120") if get_version().0 < 20 => None,
|
|
|
|
|
("nvptx64", "sm_120a") if get_version().0 < 20 => None,
|
|
|
|
|
("nvptx64", "ptx86") if get_version().0 < 20 => None,
|
|
|
|
|
("nvptx64", "ptx87") if get_version().0 < 20 => None,
|
2024-09-27 10:44:04 +09:00
|
|
|
// Filter out features that are not supported by the current LLVM version
|
2025-04-29 22:12:27 +08:00
|
|
|
("loongarch64", "div32" | "lam-bh" | "lamcas" | "ld-seq-sa" | "scq")
|
|
|
|
|
if get_version().0 < 20 =>
|
|
|
|
|
{
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
// Filter out features that are not supported by the current LLVM version
|
2025-02-22 16:15:14 +09:00
|
|
|
("riscv32" | "riscv64", "zacas") if get_version().0 < 20 => None,
|
2025-05-19 15:14:38 +02:00
|
|
|
(
|
|
|
|
|
"s390x",
|
|
|
|
|
"message-security-assist-extension12"
|
|
|
|
|
| "concurrent-functions"
|
|
|
|
|
| "miscellaneous-extensions-4"
|
|
|
|
|
| "vector-enhancements-3"
|
|
|
|
|
| "vector-packed-decimal-enhancement-3",
|
|
|
|
|
) if get_version().0 < 20 => None,
|
2024-09-17 12:37:45 -07:00
|
|
|
// Enable the evex512 target feature if an avx512 target feature is enabled.
|
2025-04-09 16:40:50 +05:30
|
|
|
("x86", s) if s.starts_with("avx512") => Some(LLVMFeature::with_dependencies(
|
|
|
|
|
s,
|
|
|
|
|
smallvec![TargetFeatureFoldStrength::EnableOnly("evex512")],
|
|
|
|
|
)),
|
2024-10-23 11:26:45 -07:00
|
|
|
// Support for `wide-arithmetic` will first land in LLVM 20 as part of
|
|
|
|
|
// llvm/llvm-project#111598
|
|
|
|
|
("wasm32" | "wasm64", "wide-arithmetic") if get_version() < (20, 0, 0) => None,
|
2024-11-09 03:17:24 +09:00
|
|
|
("sparc", "leoncasa") => Some(LLVMFeature::new("hasleoncasa")),
|
2024-11-09 03:22:09 +09:00
|
|
|
// In LLVM 19, there is no `v8plus` feature and `v9` means "SPARC-V9 instruction available and SPARC-V8+ ABI used".
|
|
|
|
|
// https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp#L27-L28
|
2025-04-02 12:08:01 -07:00
|
|
|
// Before LLVM 19, there was no `v8plus` feature and `v9` means "SPARC-V9 instruction available".
|
2024-11-09 03:22:09 +09:00
|
|
|
// https://github.com/llvm/llvm-project/blob/llvmorg-18.1.0/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp#L26
|
|
|
|
|
("sparc", "v8plus") if get_version().0 == 19 => Some(LLVMFeature::new("v9")),
|
2024-12-09 00:41:35 +05:30
|
|
|
("powerpc", "power8-crypto") => Some(LLVMFeature::new("crypto")),
|
2025-03-04 20:07:25 +05:30
|
|
|
// These new `amx` variants and `movrs` were introduced in LLVM20
|
|
|
|
|
("x86", "amx-avx512" | "amx-fp8" | "amx-movrs" | "amx-tf32" | "amx-transpose")
|
|
|
|
|
if get_version().0 < 20 =>
|
|
|
|
|
{
|
|
|
|
|
None
|
|
|
|
|
}
|
2025-03-04 20:08:28 +05:30
|
|
|
("x86", "movrs") if get_version().0 < 20 => None,
|
2025-04-11 19:57:05 +05:30
|
|
|
("x86", "avx10.1") => Some(LLVMFeature::new("avx10.1-512")),
|
|
|
|
|
("x86", "avx10.2") if get_version().0 < 20 => None,
|
|
|
|
|
("x86", "avx10.2") if get_version().0 >= 20 => Some(LLVMFeature::new("avx10.2-512")),
|
2025-04-21 16:44:42 +05:30
|
|
|
("x86", "apxf") => Some(LLVMFeature::with_dependencies(
|
|
|
|
|
"egpr",
|
|
|
|
|
smallvec![
|
|
|
|
|
TargetFeatureFoldStrength::Both("push2pop2"),
|
|
|
|
|
TargetFeatureFoldStrength::Both("ppx"),
|
|
|
|
|
TargetFeatureFoldStrength::Both("ndd"),
|
|
|
|
|
TargetFeatureFoldStrength::Both("ccmp"),
|
|
|
|
|
TargetFeatureFoldStrength::Both("cf"),
|
|
|
|
|
TargetFeatureFoldStrength::Both("nf"),
|
|
|
|
|
TargetFeatureFoldStrength::Both("zu"),
|
|
|
|
|
],
|
|
|
|
|
)),
|
2024-08-09 16:41:43 +01:00
|
|
|
(_, s) => Some(LLVMFeature::new(s)),
|
2023-01-23 13:15:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 11:45:59 +02:00
|
|
|
/// Used to generate cfg variables and apply features.
|
|
|
|
|
/// Must express features in the way Rust understands them.
|
|
|
|
|
///
|
|
|
|
|
/// We do not have to worry about RUSTC_SPECIFIC_FEATURES here, those are handled outside codegen.
|
2025-04-24 22:11:23 +00:00
|
|
|
pub(crate) fn target_config(sess: &Session) -> TargetConfig {
|
2024-08-03 23:51:37 -04:00
|
|
|
let target_machine = create_informational_target_machine(sess, true);
|
2025-05-10 22:53:38 +02:00
|
|
|
|
2025-05-11 12:50:48 +02:00
|
|
|
let (unstable_target_features, target_features) = cfg_target_feature(sess, |feature| {
|
target-feature: enable rust target features implied by target-cpu
Normally LLVM and rustc agree about what features are implied by
target-cpu, but for NVPTX, LLVM considers sm_* and ptx* features to be
exclusive, which makes sense for codegen purposes. But in Rust, we want
to think of them as:
sm_{sver} means that the target supports the hardware features of sver
ptx{pver} means the driver supports PTX ISA pver
Intrinsics usually require a minimum sm_{sver} and ptx{pver}.
Prior to this commit, -Ctarget-cpu=sm_70 would activate only sm_70 and
ptx60 (the minimum PTX version that supports sm_70, which maximizes
driver compatibility). With this commit, it also activates all the
implied target features (sm_20, ..., sm_62; ptx32, ..., ptx50).
2025-05-21 22:08:51 -06:00
|
|
|
// This closure determines whether the target CPU has the feature according to LLVM. We do
|
|
|
|
|
// *not* consider the `-Ctarget-feature`s here, as that will be handled later in
|
|
|
|
|
// `cfg_target_feature`.
|
2025-05-11 12:50:48 +02:00
|
|
|
if let Some(feat) = to_llvm_features(sess, feature) {
|
|
|
|
|
// All the LLVM features this expands to must be enabled.
|
|
|
|
|
for llvm_feature in feat {
|
|
|
|
|
let cstr = SmallCStr::new(llvm_feature);
|
|
|
|
|
// `LLVMRustHasFeature` is moderately expensive. On targets with many
|
|
|
|
|
// features (e.g. x86) these calls take a non-trivial fraction of runtime
|
|
|
|
|
// when compiling very small programs.
|
|
|
|
|
if !unsafe { llvm::LLVMRustHasFeature(target_machine.raw(), cstr.as_ptr()) } {
|
|
|
|
|
return false;
|
2024-08-03 04:45:48 -04:00
|
|
|
}
|
2025-02-25 16:27:09 +11:00
|
|
|
}
|
2025-05-11 12:50:48 +02:00
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-02-25 15:25:54 +11:00
|
|
|
|
2025-04-24 22:11:23 +00:00
|
|
|
let mut cfg = TargetConfig {
|
|
|
|
|
target_features,
|
|
|
|
|
unstable_target_features,
|
|
|
|
|
has_reliable_f16: true,
|
|
|
|
|
has_reliable_f16_math: true,
|
|
|
|
|
has_reliable_f128: true,
|
|
|
|
|
has_reliable_f128_math: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
update_target_reliable_float_cfg(sess, &mut cfg);
|
|
|
|
|
cfg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Determine whether or not experimental float types are reliable based on known bugs.
|
|
|
|
|
fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
|
|
|
|
|
let target_arch = sess.target.arch.as_ref();
|
|
|
|
|
let target_os = sess.target.options.os.as_ref();
|
|
|
|
|
let target_env = sess.target.options.env.as_ref();
|
|
|
|
|
let target_abi = sess.target.options.abi.as_ref();
|
|
|
|
|
let target_pointer_width = sess.target.pointer_width;
|
2025-06-27 20:24:01 -05:00
|
|
|
let version = get_version();
|
2025-08-06 08:57:14 +00:00
|
|
|
let lt_20_1_1 = version < (20, 1, 1);
|
|
|
|
|
let lt_21_0_0 = version < (21, 0, 0);
|
2025-04-24 22:11:23 +00:00
|
|
|
|
|
|
|
|
cfg.has_reliable_f16 = match (target_arch, target_os) {
|
2025-08-06 08:57:14 +00:00
|
|
|
// LLVM crash without neon <https://github.com/llvm/llvm-project/issues/129394> (fixed in llvm20)
|
2025-06-27 20:24:01 -05:00
|
|
|
("aarch64", _)
|
2025-08-06 08:57:14 +00:00
|
|
|
if !cfg.target_features.iter().any(|f| f.as_str() == "neon") && lt_20_1_1 =>
|
2025-06-27 20:24:01 -05:00
|
|
|
{
|
|
|
|
|
false
|
|
|
|
|
}
|
2025-04-24 22:11:23 +00:00
|
|
|
// Unsupported <https://github.com/llvm/llvm-project/issues/94434>
|
|
|
|
|
("arm64ec", _) => false,
|
2025-08-06 08:57:14 +00:00
|
|
|
// Selection failure <https://github.com/llvm/llvm-project/issues/50374> (fixed in llvm21)
|
|
|
|
|
("s390x", _) if lt_21_0_0 => false,
|
2025-04-24 22:11:23 +00:00
|
|
|
// MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
|
|
|
|
|
("x86_64", "windows") if target_env == "gnu" && target_abi != "llvm" => false,
|
|
|
|
|
// Infinite recursion <https://github.com/llvm/llvm-project/issues/97981>
|
|
|
|
|
("csky", _) => false,
|
2025-08-06 08:57:14 +00:00
|
|
|
("hexagon", _) if lt_21_0_0 => false, // (fixed in llvm21)
|
2025-04-24 22:11:23 +00:00
|
|
|
("powerpc" | "powerpc64", _) => false,
|
|
|
|
|
("sparc" | "sparc64", _) => false,
|
|
|
|
|
("wasm32" | "wasm64", _) => false,
|
|
|
|
|
// `f16` support only requires that symbols converting to and from `f32` are available. We
|
|
|
|
|
// provide these in `compiler-builtins`, so `f16` should be available on all platforms that
|
|
|
|
|
// do not have other ABI issues or LLVM crashes.
|
|
|
|
|
_ => true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cfg.has_reliable_f128 = match (target_arch, target_os) {
|
|
|
|
|
// Unsupported <https://github.com/llvm/llvm-project/issues/94434>
|
|
|
|
|
("arm64ec", _) => false,
|
2025-08-06 08:57:14 +00:00
|
|
|
// Selection bug <https://github.com/llvm/llvm-project/issues/96432> (fixed in llvm20)
|
|
|
|
|
("mips64" | "mips64r6", _) if lt_20_1_1 => false,
|
|
|
|
|
// Selection bug <https://github.com/llvm/llvm-project/issues/95471>. This issue is closed
|
|
|
|
|
// but basic math still does not work.
|
2025-04-24 22:11:23 +00:00
|
|
|
("nvptx64", _) => false,
|
2025-07-23 16:49:39 -07:00
|
|
|
// Unsupported https://github.com/llvm/llvm-project/issues/121122
|
|
|
|
|
("amdgpu", _) => false,
|
2025-04-24 22:11:23 +00:00
|
|
|
// ABI bugs <https://github.com/rust-lang/rust/issues/125109> et al. (full
|
|
|
|
|
// list at <https://github.com/rust-lang/rust/issues/116909>)
|
|
|
|
|
("powerpc" | "powerpc64", _) => false,
|
|
|
|
|
// ABI unsupported <https://github.com/llvm/llvm-project/issues/41838>
|
|
|
|
|
("sparc", _) => false,
|
|
|
|
|
// Stack alignment bug <https://github.com/llvm/llvm-project/issues/77401>. NB: tests may
|
2025-08-06 08:57:14 +00:00
|
|
|
// not fail if our compiler-builtins is linked. (fixed in llvm21)
|
|
|
|
|
("x86", _) if lt_21_0_0 => false,
|
2025-04-24 22:11:23 +00:00
|
|
|
// MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
|
|
|
|
|
("x86_64", "windows") if target_env == "gnu" && target_abi != "llvm" => false,
|
|
|
|
|
// There are no known problems on other platforms, so the only requirement is that symbols
|
|
|
|
|
// are available. `compiler-builtins` provides all symbols required for core `f128`
|
|
|
|
|
// support, so this should work for everything else.
|
|
|
|
|
_ => true,
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-11 08:08:53 +00:00
|
|
|
// Assume that working `f16` means working `f16` math for most platforms, since
|
|
|
|
|
// operations just go through `f32`.
|
|
|
|
|
cfg.has_reliable_f16_math = cfg.has_reliable_f16;
|
2025-04-24 22:11:23 +00:00
|
|
|
|
|
|
|
|
cfg.has_reliable_f128_math = match (target_arch, target_os) {
|
|
|
|
|
// LLVM lowers `fp128` math to `long double` symbols even on platforms where
|
|
|
|
|
// `long double` is not IEEE binary128. See
|
|
|
|
|
// <https://github.com/llvm/llvm-project/issues/44744>.
|
|
|
|
|
//
|
|
|
|
|
// This rules out anything that doesn't have `long double` = `binary128`; <= 32 bits
|
|
|
|
|
// (ld is `f64`), anything other than Linux (Windows and MacOS use `f64`), and `x86`
|
|
|
|
|
// (ld is 80-bit extended precision).
|
2025-07-25 01:45:42 +00:00
|
|
|
//
|
|
|
|
|
// musl does not implement the symbols required for f128 math at all.
|
|
|
|
|
_ if target_env == "musl" => false,
|
2025-04-24 22:11:23 +00:00
|
|
|
("x86_64", _) => false,
|
|
|
|
|
(_, "linux") if target_pointer_width == 64 => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
} && cfg.has_reliable_f128;
|
2018-01-05 13:26:26 -08:00
|
|
|
}
|
2017-04-30 20:33:25 +02:00
|
|
|
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) fn print_version() {
|
2020-10-12 22:33:27 -04:00
|
|
|
let (major, minor, patch) = get_version();
|
2023-07-25 23:04:01 +02:00
|
|
|
println!("LLVM version: {major}.{minor}.{patch}");
|
2020-10-12 22:33:27 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) fn get_version() -> (u32, u32, u32) {
|
2017-10-30 18:42:21 +01:00
|
|
|
// Can be called without initializing LLVM
|
2017-04-30 20:33:25 +02:00
|
|
|
unsafe {
|
2020-10-12 22:33:27 -04:00
|
|
|
(llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor(), llvm::LLVMRustVersionPatch())
|
2017-04-30 20:33:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) fn print_passes() {
|
2017-10-30 18:42:21 +01:00
|
|
|
// Can be called without initializing LLVM
|
2017-04-30 20:33:25 +02:00
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMRustPrintPasses();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 00:00:47 -05:00
|
|
|
fn llvm_target_features(tm: &llvm::TargetMachine) -> Vec<(&str, &str)> {
|
|
|
|
|
let len = unsafe { llvm::LLVMRustGetTargetFeaturesCount(tm) };
|
|
|
|
|
let mut ret = Vec::with_capacity(len);
|
|
|
|
|
for i in 0..len {
|
|
|
|
|
unsafe {
|
|
|
|
|
let mut feature = ptr::null();
|
|
|
|
|
let mut desc = ptr::null();
|
|
|
|
|
llvm::LLVMRustGetTargetFeature(tm, i, &mut feature, &mut desc);
|
|
|
|
|
if feature.is_null() || desc.is_null() {
|
|
|
|
|
bug!("LLVM returned a `null` target feature string");
|
|
|
|
|
}
|
|
|
|
|
let feature = CStr::from_ptr(feature).to_str().unwrap_or_else(|e| {
|
|
|
|
|
bug!("LLVM returned a non-utf8 feature string: {}", e);
|
|
|
|
|
});
|
|
|
|
|
let desc = CStr::from_ptr(desc).to_str().unwrap_or_else(|e| {
|
|
|
|
|
bug!("LLVM returned a non-utf8 feature string: {}", e);
|
|
|
|
|
});
|
|
|
|
|
ret.push((feature, desc));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ret
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 12:40:28 +11:00
|
|
|
pub(crate) fn print(req: &PrintRequest, out: &mut String, sess: &Session) {
|
|
|
|
|
require_inited();
|
|
|
|
|
let tm = create_informational_target_machine(sess, false);
|
|
|
|
|
match req.kind {
|
2025-02-04 12:20:09 +00:00
|
|
|
PrintKind::TargetCPUs => print_target_cpus(sess, tm.raw(), out),
|
|
|
|
|
PrintKind::TargetFeatures => print_target_features(sess, tm.raw(), out),
|
2024-11-02 12:40:28 +11:00
|
|
|
_ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn print_target_cpus(sess: &Session, tm: &llvm::TargetMachine, out: &mut String) {
|
|
|
|
|
let cpu_names = llvm::build_string(|s| unsafe {
|
|
|
|
|
llvm::LLVMRustPrintTargetCPUs(&tm, s);
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
struct Cpu<'a> {
|
|
|
|
|
cpu_name: &'a str,
|
|
|
|
|
remark: String,
|
|
|
|
|
}
|
|
|
|
|
// Compare CPU against current target to label the default.
|
|
|
|
|
let target_cpu = handle_native(&sess.target.cpu);
|
|
|
|
|
let make_remark = |cpu_name| {
|
|
|
|
|
if cpu_name == target_cpu {
|
|
|
|
|
// FIXME(#132514): This prints the LLVM target string, which can be
|
|
|
|
|
// different from the Rust target string. Is that intended?
|
|
|
|
|
let target = &sess.target.llvm_target;
|
|
|
|
|
format!(
|
|
|
|
|
" - This is the default target CPU for the current build target (currently {target})."
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
"".to_owned()
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let mut cpus = cpu_names
|
|
|
|
|
.lines()
|
|
|
|
|
.map(|cpu_name| Cpu { cpu_name, remark: make_remark(cpu_name) })
|
|
|
|
|
.collect::<VecDeque<_>>();
|
|
|
|
|
|
|
|
|
|
// Only print the "native" entry when host and target are the same arch,
|
|
|
|
|
// since otherwise it could be wrong or misleading.
|
|
|
|
|
if sess.host.arch == sess.target.arch {
|
|
|
|
|
let host = get_host_cpu_name();
|
|
|
|
|
cpus.push_front(Cpu {
|
|
|
|
|
cpu_name: "native",
|
|
|
|
|
remark: format!(" - Select the CPU of the current host (currently {host})."),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let max_name_width = cpus.iter().map(|cpu| cpu.cpu_name.len()).max().unwrap_or(0);
|
|
|
|
|
writeln!(out, "Available CPUs for this target:").unwrap();
|
|
|
|
|
for Cpu { cpu_name, remark } in cpus {
|
|
|
|
|
// Only pad the CPU name if there's a remark to print after it.
|
|
|
|
|
let width = if remark.is_empty() { 0 } else { max_name_width };
|
|
|
|
|
writeln!(out, " {cpu_name:<width$}{remark}").unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn print_target_features(sess: &Session, tm: &llvm::TargetMachine, out: &mut String) {
|
2022-11-19 21:22:17 -05:00
|
|
|
let mut llvm_target_features = llvm_target_features(tm);
|
|
|
|
|
let mut known_llvm_target_features = FxHashSet::<&'static str>::default();
|
2023-12-12 21:47:20 +01:00
|
|
|
let mut rustc_target_features = sess
|
|
|
|
|
.target
|
2024-09-02 11:45:59 +02:00
|
|
|
.rust_target_features()
|
2021-04-08 00:00:47 -05:00
|
|
|
.iter()
|
2024-09-02 11:45:59 +02:00
|
|
|
.filter_map(|(feature, gate, _implied)| {
|
2024-11-16 10:00:16 +01:00
|
|
|
if !gate.in_cfg() {
|
2024-09-02 11:45:59 +02:00
|
|
|
// Only list (experimentally) supported features.
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2024-09-18 13:34:49 +10:00
|
|
|
// LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these
|
|
|
|
|
// strings.
|
2024-08-09 16:41:43 +01:00
|
|
|
let llvm_feature = to_llvm_features(sess, *feature)?.llvm_feature_name;
|
2023-05-22 14:46:40 +01:00
|
|
|
let desc =
|
2022-11-19 21:22:17 -05:00
|
|
|
match llvm_target_features.binary_search_by_key(&llvm_feature, |(f, _d)| f).ok() {
|
|
|
|
|
Some(index) => {
|
|
|
|
|
known_llvm_target_features.insert(llvm_feature);
|
|
|
|
|
llvm_target_features[index].1
|
|
|
|
|
}
|
|
|
|
|
None => "",
|
2023-05-22 14:46:40 +01:00
|
|
|
};
|
|
|
|
|
|
2024-08-09 16:41:43 +01:00
|
|
|
Some((*feature, desc))
|
2021-04-08 00:00:47 -05:00
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
2024-06-13 20:42:12 +02:00
|
|
|
|
|
|
|
|
// Since we add this at the end ...
|
2021-04-08 00:00:47 -05:00
|
|
|
rustc_target_features.extend_from_slice(&[(
|
|
|
|
|
"crt-static",
|
|
|
|
|
"Enables C Run-time Libraries to be statically linked",
|
|
|
|
|
)]);
|
2024-06-13 20:42:12 +02:00
|
|
|
// ... we need to sort the list again.
|
|
|
|
|
rustc_target_features.sort();
|
|
|
|
|
|
2022-11-19 21:22:17 -05:00
|
|
|
llvm_target_features.retain(|(f, _d)| !known_llvm_target_features.contains(f));
|
|
|
|
|
|
|
|
|
|
let max_feature_len = llvm_target_features
|
2021-04-08 00:00:47 -05:00
|
|
|
.iter()
|
|
|
|
|
.chain(rustc_target_features.iter())
|
|
|
|
|
.map(|(feature, _desc)| feature.len())
|
|
|
|
|
.max()
|
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
2024-03-30 13:19:58 +00:00
|
|
|
writeln!(out, "Features supported by rustc for this target:").unwrap();
|
2021-04-08 00:00:47 -05:00
|
|
|
for (feature, desc) in &rustc_target_features {
|
2024-03-30 13:19:58 +00:00
|
|
|
writeln!(out, " {feature:max_feature_len$} - {desc}.").unwrap();
|
2021-04-08 00:00:47 -05:00
|
|
|
}
|
2024-03-30 13:19:58 +00:00
|
|
|
writeln!(out, "\nCode-generation features supported by LLVM for this target:").unwrap();
|
2022-11-19 21:22:17 -05:00
|
|
|
for (feature, desc) in &llvm_target_features {
|
2024-03-30 13:19:58 +00:00
|
|
|
writeln!(out, " {feature:max_feature_len$} - {desc}.").unwrap();
|
2021-04-08 00:00:47 -05:00
|
|
|
}
|
2022-11-19 21:22:17 -05:00
|
|
|
if llvm_target_features.is_empty() {
|
2024-03-30 13:19:58 +00:00
|
|
|
writeln!(out, " Target features listing is not supported by this LLVM version.")
|
|
|
|
|
.unwrap();
|
2021-04-08 00:00:47 -05:00
|
|
|
}
|
2024-03-30 13:19:58 +00:00
|
|
|
writeln!(out, "\nUse +feature to enable a feature, or -feature to disable it.").unwrap();
|
|
|
|
|
writeln!(out, "For example, rustc -C target-cpu=mycpu -C target-feature=+feature1,-feature2\n")
|
|
|
|
|
.unwrap();
|
|
|
|
|
writeln!(out, "Code-generation features cannot be used in cfg or #[target_feature],").unwrap();
|
|
|
|
|
writeln!(out, "and may be renamed or removed in a future version of LLVM or rustc.\n").unwrap();
|
2021-04-08 00:00:47 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-02 11:44:15 +11:00
|
|
|
/// Returns the host CPU name, according to LLVM.
|
|
|
|
|
fn get_host_cpu_name() -> &'static str {
|
|
|
|
|
let mut len = 0;
|
|
|
|
|
// SAFETY: The underlying C++ global function returns a `StringRef` that
|
|
|
|
|
// isn't tied to any particular backing buffer, so it must be 'static.
|
|
|
|
|
let slice: &'static [u8] = unsafe {
|
2018-08-23 11:03:22 -07:00
|
|
|
let ptr = llvm::LLVMRustGetHostCPUName(&mut len);
|
2024-11-02 11:44:15 +11:00
|
|
|
assert!(!ptr.is_null());
|
|
|
|
|
slice::from_raw_parts(ptr, len)
|
|
|
|
|
};
|
|
|
|
|
str::from_utf8(slice).expect("host CPU name should be UTF-8")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// If the given string is `"native"`, returns the host CPU name according to
|
|
|
|
|
/// LLVM. Otherwise, the string is returned as-is.
|
|
|
|
|
fn handle_native(cpu_name: &str) -> &str {
|
|
|
|
|
match cpu_name {
|
|
|
|
|
"native" => get_host_cpu_name(),
|
|
|
|
|
_ => cpu_name,
|
2018-08-23 11:03:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-17 17:39:26 +08:00
|
|
|
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) fn target_cpu(sess: &Session) -> &str {
|
2024-11-02 11:44:15 +11:00
|
|
|
let cpu_name = sess.opts.cg.target_cpu.as_deref().unwrap_or_else(|| &sess.target.cpu);
|
|
|
|
|
handle_native(cpu_name)
|
2020-09-17 17:39:26 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-23 08:07:42 +02:00
|
|
|
/// The target features for compiler flags other than `-Ctarget-features`.
|
|
|
|
|
fn llvm_features_by_flags(sess: &Session, features: &mut Vec<String>) {
|
|
|
|
|
target_features::retpoline_features_by_flags(sess, features);
|
2025-06-14 14:11:00 +02:00
|
|
|
|
|
|
|
|
// -Zfixed-x18
|
|
|
|
|
if sess.opts.unstable_opts.fixed_x18 {
|
|
|
|
|
if sess.target.arch != "aarch64" {
|
|
|
|
|
sess.dcx().emit_fatal(errors::FixedX18InvalidArch { arch: &sess.target.arch });
|
|
|
|
|
} else {
|
|
|
|
|
features.push("+reserve-x18".into());
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-15 20:43:02 +07:00
|
|
|
}
|
|
|
|
|
|
2021-03-13 15:29:39 +02:00
|
|
|
/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
|
|
|
|
|
/// `--target` and similar).
|
2024-08-03 23:51:37 -04:00
|
|
|
pub(crate) fn global_llvm_features(
|
|
|
|
|
sess: &Session,
|
|
|
|
|
diagnostics: bool,
|
|
|
|
|
only_base_features: bool,
|
|
|
|
|
) -> Vec<String> {
|
2022-03-03 19:47:23 +08:00
|
|
|
// Features that come earlier are overridden by conflicting features later in the string.
|
2021-03-13 15:29:39 +02:00
|
|
|
// Typically we'll want more explicit settings to override the implicit ones, so:
|
|
|
|
|
//
|
2022-03-03 19:47:23 +08:00
|
|
|
// * Features from -Ctarget-cpu=*; are overridden by [^1]
|
|
|
|
|
// * Features implied by --target; are overridden by
|
|
|
|
|
// * Features from -Ctarget-feature; are overridden by
|
2021-03-13 15:29:39 +02:00
|
|
|
// * function specific features.
|
|
|
|
|
//
|
|
|
|
|
// [^1]: target-cpu=native is handled here, other target-cpu values are handled implicitly
|
|
|
|
|
// through LLVM TargetMachine implementation.
|
|
|
|
|
//
|
|
|
|
|
// FIXME(nagisa): it isn't clear what's the best interaction between features implied by
|
|
|
|
|
// `-Ctarget-cpu` and `--target` are. On one hand, you'd expect CLI arguments to always
|
|
|
|
|
// override anything that's implicit, so e.g. when there's no `--target` flag, features implied
|
2022-03-03 19:47:23 +08:00
|
|
|
// the host target are overridden by `-Ctarget-cpu=*`. On the other hand, what about when both
|
2021-03-13 15:29:39 +02:00
|
|
|
// `--target` and `-Ctarget-cpu=*` are specified? Both then imply some target features and both
|
|
|
|
|
// flags are specified by the user on the CLI. It isn't as clear-cut which order of precedence
|
|
|
|
|
// should be taken in cases like these.
|
|
|
|
|
let mut features = vec![];
|
|
|
|
|
|
|
|
|
|
// -Ctarget-cpu=native
|
2021-01-06 03:23:54 -05:00
|
|
|
match sess.opts.cg.target_cpu {
|
2021-03-13 15:29:39 +02:00
|
|
|
Some(ref s) if s == "native" => {
|
2024-09-29 12:16:35 +02:00
|
|
|
// We have already figured out the actual CPU name with `LLVMRustGetHostCPUName` and set
|
|
|
|
|
// that for LLVM, so the features implied by that CPU name will be available everywhere.
|
|
|
|
|
// However, that is not sufficient: e.g. `skylake` alone is not sufficient to tell if
|
|
|
|
|
// some of the instructions are available or not. So we have to also explicitly ask for
|
|
|
|
|
// the exact set of features available on the host, and enable all of them.
|
2021-01-08 11:50:21 -05:00
|
|
|
let features_string = unsafe {
|
|
|
|
|
let ptr = llvm::LLVMGetHostCPUFeatures();
|
|
|
|
|
let features_string = if !ptr.is_null() {
|
|
|
|
|
CStr::from_ptr(ptr)
|
|
|
|
|
.to_str()
|
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
|
bug!("LLVM returned a non-utf8 features string: {}", e);
|
|
|
|
|
})
|
|
|
|
|
.to_owned()
|
|
|
|
|
} else {
|
|
|
|
|
bug!("could not allocate host CPU features, LLVM returned a `null` string");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
llvm::LLVMDisposeMessage(ptr);
|
|
|
|
|
|
|
|
|
|
features_string
|
|
|
|
|
};
|
2021-07-17 23:35:57 +02:00
|
|
|
features.extend(features_string.split(',').map(String::from));
|
2021-01-06 03:23:54 -05:00
|
|
|
}
|
2021-03-13 15:29:39 +02:00
|
|
|
Some(_) | None => {}
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-23 16:19:22 +03:00
|
|
|
// Features implied by an implicit or explicit `--target`.
|
|
|
|
|
features.extend(
|
|
|
|
|
sess.target
|
|
|
|
|
.features
|
|
|
|
|
.split(',')
|
2024-12-26 18:32:22 +01:00
|
|
|
.filter(|v| !v.is_empty())
|
2024-11-09 03:22:09 +09:00
|
|
|
// Drop +v8plus feature introduced in LLVM 20.
|
2025-05-23 08:07:42 +02:00
|
|
|
// (Hard-coded target features do not go through `to_llvm_feature` since they already
|
|
|
|
|
// are LLVM feature names, hence we need a special case here.)
|
2024-11-09 03:22:09 +09:00
|
|
|
.filter(|v| *v != "+v8plus" || get_version() >= (20, 0, 0))
|
2021-07-23 16:19:22 +03:00
|
|
|
.map(String::from),
|
|
|
|
|
);
|
2022-01-31 13:04:27 +00:00
|
|
|
|
2024-02-21 17:32:58 -06:00
|
|
|
if wants_wasm_eh(sess) && sess.panic_strategy() == PanicStrategy::Unwind {
|
|
|
|
|
features.push("+exception-handling".into());
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 16:19:22 +03:00
|
|
|
// -Ctarget-features
|
2024-08-03 23:51:37 -04:00
|
|
|
if !only_base_features {
|
2025-05-23 08:07:42 +02:00
|
|
|
target_features::flag_to_backend_features(
|
|
|
|
|
sess,
|
|
|
|
|
diagnostics,
|
|
|
|
|
|feature| {
|
|
|
|
|
to_llvm_features(sess, feature)
|
|
|
|
|
.map(|f| SmallVec::<[&str; 2]>::from_iter(f.into_iter()))
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
},
|
|
|
|
|
|feature, enable| {
|
2024-12-26 18:32:22 +01:00
|
|
|
let enable_disable = if enable { '+' } else { '-' };
|
2024-09-02 11:45:59 +02:00
|
|
|
// We run through `to_llvm_features` when
|
2024-08-03 23:51:37 -04:00
|
|
|
// passing requests down to LLVM. This means that all in-language
|
|
|
|
|
// features also work on the command line instead of having two
|
|
|
|
|
// different names when the LLVM name and the Rust name differ.
|
2025-05-23 08:07:42 +02:00
|
|
|
let Some(llvm_feature) = to_llvm_features(sess, feature) else { return };
|
2024-08-03 23:51:37 -04:00
|
|
|
|
2025-05-23 08:07:42 +02:00
|
|
|
features.extend(
|
2024-08-03 23:51:37 -04:00
|
|
|
std::iter::once(format!(
|
|
|
|
|
"{}{}",
|
|
|
|
|
enable_disable, llvm_feature.llvm_feature_name
|
|
|
|
|
))
|
2025-04-09 16:40:50 +05:30
|
|
|
.chain(llvm_feature.dependencies.into_iter().filter_map(
|
2024-12-26 18:32:22 +01:00
|
|
|
move |feat| match (enable, feat) {
|
|
|
|
|
(_, TargetFeatureFoldStrength::Both(f))
|
|
|
|
|
| (true, TargetFeatureFoldStrength::EnableOnly(f)) => {
|
2023-07-25 23:04:01 +02:00
|
|
|
Some(format!("{enable_disable}{f}"))
|
2023-05-22 14:46:40 +01:00
|
|
|
}
|
|
|
|
|
_ => None,
|
2024-08-03 23:51:37 -04:00
|
|
|
},
|
|
|
|
|
)),
|
|
|
|
|
)
|
2025-05-23 08:07:42 +02:00
|
|
|
},
|
|
|
|
|
);
|
2024-08-03 23:51:37 -04:00
|
|
|
}
|
2022-06-06 18:05:07 +08:00
|
|
|
|
2025-06-14 14:11:00 +02:00
|
|
|
// We add this in the "base target" so that these show up in `sess.unstable_target_features`.
|
|
|
|
|
llvm_features_by_flags(sess, &mut features);
|
2024-05-03 14:32:01 +02:00
|
|
|
|
2021-07-23 16:19:22 +03:00
|
|
|
features
|
|
|
|
|
}
|
2021-03-13 15:29:39 +02:00
|
|
|
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) fn tune_cpu(sess: &Session) -> Option<&str> {
|
2022-07-06 07:44:47 -05:00
|
|
|
let name = sess.opts.unstable_opts.tune_cpu.as_ref()?;
|
2020-12-30 18:22:41 +01:00
|
|
|
Some(handle_native(name))
|
2020-09-17 17:39:26 +08:00
|
|
|
}
|