Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 13:49:36 +10:00
|
|
|
// tidy-alphabetical-start
|
|
|
|
#![allow(rustc::diagnostic_outside_of_impl)]
|
|
|
|
#![allow(rustc::untranslatable_diagnostic)]
|
2023-12-22 11:14:11 +01:00
|
|
|
#![cfg_attr(doc, allow(internal_features))]
|
|
|
|
#![cfg_attr(doc, doc(rust_logo))]
|
Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 13:49:36 +10:00
|
|
|
#![cfg_attr(doc, feature(rustdoc_internals))]
|
|
|
|
// Note: please avoid adding other feature gates where possible
|
2022-03-20 16:55:21 +01:00
|
|
|
#![feature(rustc_private)]
|
|
|
|
// Note: please avoid adding other feature gates where possible
|
2020-04-05 13:48:26 +02:00
|
|
|
#![warn(rust_2018_idioms)]
|
2020-11-03 11:00:04 +01:00
|
|
|
#![warn(unreachable_pub)]
|
Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 13:49:36 +10:00
|
|
|
#![warn(unused_lifetimes)]
|
|
|
|
// tidy-alphabetical-end
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2020-08-20 16:51:01 +02:00
|
|
|
#[macro_use]
|
2020-03-31 13:20:19 +02:00
|
|
|
extern crate rustc_middle;
|
2024-10-08 17:26:42 -07:00
|
|
|
extern crate rustc_abi;
|
2020-08-28 12:10:48 +02:00
|
|
|
extern crate rustc_ast;
|
2018-11-24 11:23:49 +01:00
|
|
|
extern crate rustc_codegen_ssa;
|
2018-11-24 12:47:53 +01:00
|
|
|
extern crate rustc_data_structures;
|
2020-04-03 11:54:18 +02:00
|
|
|
extern crate rustc_errors;
|
2018-11-24 12:47:53 +01:00
|
|
|
extern crate rustc_fs_util;
|
2020-01-09 17:43:10 +01:00
|
|
|
extern crate rustc_hir;
|
2018-06-17 18:05:11 +02:00
|
|
|
extern crate rustc_incremental;
|
2019-10-03 17:22:01 +02:00
|
|
|
extern crate rustc_index;
|
2021-05-29 22:49:59 +02:00
|
|
|
extern crate rustc_metadata;
|
2019-12-31 16:43:24 +01:00
|
|
|
extern crate rustc_session;
|
2020-01-06 20:11:03 +01:00
|
|
|
extern crate rustc_span;
|
2018-07-31 12:25:16 +02:00
|
|
|
extern crate rustc_target;
|
2024-12-18 17:53:16 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate tracing;
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2020-04-05 13:48:26 +02:00
|
|
|
// This prevents duplicating functions and statics that are already part of the host rustc process.
|
|
|
|
#[allow(unused_extern_crates)]
|
|
|
|
extern crate rustc_driver;
|
|
|
|
|
2018-07-23 11:17:39 +02:00
|
|
|
use std::any::Any;
|
2024-12-06 12:10:30 +00:00
|
|
|
use std::env;
|
2022-08-24 18:40:58 +02:00
|
|
|
use std::sync::Arc;
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2023-10-09 08:52:46 +00:00
|
|
|
use cranelift_codegen::isa::TargetIsa;
|
|
|
|
use cranelift_codegen::settings::{self, Configurable};
|
2020-08-28 12:10:48 +02:00
|
|
|
use rustc_codegen_ssa::CodegenResults;
|
2024-11-01 17:07:18 +01:00
|
|
|
use rustc_codegen_ssa::back::versioned_llvm_target;
|
2024-09-22 19:05:04 -04:00
|
|
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
2021-09-24 18:15:36 +02:00
|
|
|
use rustc_metadata::EncodedMetadata;
|
2020-10-15 10:34:13 +02:00
|
|
|
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
2020-08-28 12:10:48 +02:00
|
|
|
use rustc_session::Session;
|
2024-09-22 19:05:04 -04:00
|
|
|
use rustc_session::config::OutputFilenames;
|
|
|
|
use rustc_span::{Symbol, sym};
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
pub use crate::config::*;
|
2018-11-10 15:12:00 +01:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2018-07-19 19:33:42 +02:00
|
|
|
mod abi;
|
2018-11-05 18:29:15 +01:00
|
|
|
mod allocator;
|
2018-08-09 10:46:56 +02:00
|
|
|
mod analyze;
|
2020-08-28 12:10:48 +02:00
|
|
|
mod base;
|
2019-07-31 09:45:11 +02:00
|
|
|
mod cast;
|
2019-07-07 18:08:38 +02:00
|
|
|
mod codegen_i128;
|
2018-06-22 19:18:53 +02:00
|
|
|
mod common;
|
2021-03-29 10:45:09 +02:00
|
|
|
mod compiler_builtins;
|
2022-08-24 18:40:58 +02:00
|
|
|
mod concurrency_limiter;
|
2021-04-30 14:49:58 +02:00
|
|
|
mod config;
|
2018-07-31 12:25:16 +02:00
|
|
|
mod constant;
|
2019-01-17 18:07:27 +01:00
|
|
|
mod debuginfo;
|
2019-08-14 12:01:41 +02:00
|
|
|
mod discriminant;
|
2019-05-04 16:54:25 +02:00
|
|
|
mod driver;
|
2022-08-24 18:40:58 +02:00
|
|
|
mod global_asm;
|
2020-07-10 14:45:45 +02:00
|
|
|
mod inline_asm;
|
2018-10-03 18:21:52 +02:00
|
|
|
mod intrinsics;
|
2019-03-11 20:36:29 +01:00
|
|
|
mod linkage;
|
2018-10-20 18:41:26 +02:00
|
|
|
mod main_shim;
|
2019-08-14 11:52:39 +02:00
|
|
|
mod num;
|
2019-12-26 13:37:10 +01:00
|
|
|
mod optimize;
|
2019-12-20 16:02:47 +01:00
|
|
|
mod pointer;
|
2018-08-15 14:45:32 +02:00
|
|
|
mod pretty_clif;
|
2020-07-09 18:55:46 +02:00
|
|
|
mod toolchain;
|
2018-11-16 17:35:47 +01:00
|
|
|
mod trap;
|
2018-12-29 15:33:34 +01:00
|
|
|
mod unsize;
|
2024-06-30 11:28:14 +00:00
|
|
|
mod unwind_module;
|
2019-06-11 15:43:22 +02:00
|
|
|
mod value_and_place;
|
2018-09-08 18:00:06 +02:00
|
|
|
mod vtable;
|
2018-06-17 18:05:11 +02:00
|
|
|
|
|
|
|
mod prelude {
|
2024-09-22 19:05:04 -04:00
|
|
|
pub(crate) use cranelift_codegen::Context;
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
|
|
|
|
pub(crate) use cranelift_codegen::ir::function::Function;
|
2020-08-28 12:10:48 +02:00
|
|
|
pub(crate) use cranelift_codegen::ir::{
|
2024-09-22 19:05:04 -04:00
|
|
|
AbiParam, Block, FuncRef, Inst, InstBuilder, MemFlags, Signature, SourceLoc, StackSlot,
|
|
|
|
StackSlotData, StackSlotKind, TrapCode, Type, Value, types,
|
2020-08-28 12:10:48 +02:00
|
|
|
};
|
2023-06-15 17:56:01 +00:00
|
|
|
pub(crate) use cranelift_module::{self, DataDescription, FuncId, Linkage, Module};
|
2024-10-29 13:37:59 -07:00
|
|
|
pub(crate) use rustc_abi::{BackendRepr, FIRST_VARIANT, FieldIdx, Scalar, Size, VariantIdx};
|
2023-10-09 08:52:46 +00:00
|
|
|
pub(crate) use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
|
|
|
|
pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
|
|
|
pub(crate) use rustc_index::Idx;
|
|
|
|
pub(crate) use rustc_middle::mir::{self, *};
|
2024-04-23 09:37:28 +00:00
|
|
|
pub(crate) use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
|
2023-10-09 08:52:46 +00:00
|
|
|
pub(crate) use rustc_middle::ty::{
|
2024-11-15 13:53:31 +01:00
|
|
|
self, FloatTy, Instance, InstanceKind, IntTy, Ty, TyCtxt, UintTy,
|
2023-10-09 08:52:46 +00:00
|
|
|
};
|
2024-04-23 09:37:28 +00:00
|
|
|
pub(crate) use rustc_span::Span;
|
2018-06-22 19:18:53 +02:00
|
|
|
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) use crate::abi::*;
|
2020-11-03 11:00:04 +01:00
|
|
|
pub(crate) use crate::base::{codegen_operand, codegen_place};
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) use crate::cast::*;
|
|
|
|
pub(crate) use crate::common::*;
|
2020-06-13 17:03:34 +02:00
|
|
|
pub(crate) use crate::debuginfo::{DebugContext, UnwindContext};
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) use crate::pointer::Pointer;
|
2023-04-29 12:00:43 +00:00
|
|
|
pub(crate) use crate::value_and_place::{CPlace, CValue};
|
2020-06-20 18:44:49 +02:00
|
|
|
}
|
2020-03-17 16:26:56 +01:00
|
|
|
|
2020-06-20 18:44:49 +02:00
|
|
|
struct PrintOnPanic<F: Fn() -> String>(F);
|
|
|
|
impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if ::std::thread::panicking() {
|
|
|
|
println!("{}", (self.0)());
|
|
|
|
}
|
2020-03-17 16:26:56 +01:00
|
|
|
}
|
2018-06-17 18:05:11 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
/// The codegen context holds any information shared between the codegen of individual functions
|
|
|
|
/// inside a single codegen unit with the exception of the Cranelift [`Module`](cranelift_module::Module).
|
2022-08-24 18:40:58 +02:00
|
|
|
struct CodegenCx {
|
|
|
|
output_filenames: Arc<OutputFilenames>,
|
|
|
|
should_write_ir: bool,
|
2020-07-09 19:24:53 +02:00
|
|
|
global_asm: String,
|
2024-12-06 12:10:30 +00:00
|
|
|
inline_asm_index: usize,
|
2022-08-24 18:40:58 +02:00
|
|
|
debug_context: Option<DebugContext>,
|
2021-12-20 18:56:35 +01:00
|
|
|
cgu_name: Symbol,
|
2018-12-18 18:28:02 +01:00
|
|
|
}
|
|
|
|
|
2022-08-24 18:40:58 +02:00
|
|
|
impl CodegenCx {
|
2024-06-30 11:28:14 +00:00
|
|
|
fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa, debug_info: bool, cgu_name: Symbol) -> Self {
|
2021-04-30 14:49:58 +02:00
|
|
|
assert_eq!(pointer_ty(tcx), isa.pointer_type());
|
|
|
|
|
2022-07-25 16:07:57 +02:00
|
|
|
let debug_context = if debug_info && !tcx.sess.target.options.is_like_windows {
|
2024-03-28 11:43:35 +00:00
|
|
|
Some(DebugContext::new(tcx, isa, cgu_name.as_str()))
|
2022-07-25 16:07:57 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-12-18 18:28:02 +01:00
|
|
|
CodegenCx {
|
2022-08-24 18:40:58 +02:00
|
|
|
output_filenames: tcx.output_filenames(()).clone(),
|
|
|
|
should_write_ir: crate::pretty_clif::should_write_ir(tcx),
|
2020-07-09 19:24:53 +02:00
|
|
|
global_asm: String::new(),
|
2024-12-06 12:10:30 +00:00
|
|
|
inline_asm_index: 0,
|
2019-01-17 18:07:27 +01:00
|
|
|
debug_context,
|
2021-12-20 18:56:35 +01:00
|
|
|
cgu_name,
|
2018-12-18 18:28:02 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-29 18:41:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CraneliftCodegenBackend {
|
2024-12-06 12:10:30 +00:00
|
|
|
pub config: Option<BackendConfig>,
|
2020-09-29 18:41:59 +02:00
|
|
|
}
|
2018-07-23 11:17:39 +02:00
|
|
|
|
2018-07-14 11:59:42 +02:00
|
|
|
impl CodegenBackend for CraneliftCodegenBackend {
|
2022-10-17 14:11:26 +01:00
|
|
|
fn locale_resource(&self) -> &'static str {
|
|
|
|
// FIXME(rust-lang/rust#100717) - cranelift codegen backend is not yet translated
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
2020-01-17 20:33:27 +01:00
|
|
|
fn init(&self, sess: &Session) {
|
2024-06-30 11:28:14 +00:00
|
|
|
use rustc_session::config::{InstrumentCoverage, Lto};
|
2021-03-29 10:45:09 +02:00
|
|
|
match sess.lto() {
|
|
|
|
Lto::No | Lto::ThinLocal => {}
|
2023-12-18 22:21:37 +11:00
|
|
|
Lto::Thin | Lto::Fat => {
|
|
|
|
sess.dcx().warn("LTO is not supported. You may get a linker error.")
|
|
|
|
}
|
2020-01-17 20:33:27 +01:00
|
|
|
}
|
2022-08-24 18:40:58 +02:00
|
|
|
|
2024-06-30 11:28:14 +00:00
|
|
|
if sess.opts.cg.instrument_coverage() != InstrumentCoverage::No {
|
|
|
|
sess.dcx()
|
|
|
|
.fatal("`-Cinstrument-coverage` is LLVM specific and not supported by Cranelift");
|
|
|
|
}
|
2020-01-17 20:33:27 +01:00
|
|
|
}
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2024-11-16 10:00:16 +01:00
|
|
|
fn target_features_cfg(
|
|
|
|
&self,
|
|
|
|
sess: &Session,
|
|
|
|
_allow_unstable: bool,
|
|
|
|
) -> Vec<rustc_span::Symbol> {
|
2024-01-26 18:33:45 +00:00
|
|
|
// FIXME return the actually used target features. this is necessary for #[cfg(target_feature)]
|
|
|
|
if sess.target.arch == "x86_64" && sess.target.os != "none" {
|
|
|
|
// x86_64 mandates SSE2 support
|
2024-11-27 17:52:57 +00:00
|
|
|
vec![sym::fsxr, sym::sse, sym::sse2]
|
2024-08-09 17:18:46 +00:00
|
|
|
} else if sess.target.arch == "aarch64" {
|
|
|
|
match &*sess.target.os {
|
|
|
|
"none" => vec![],
|
|
|
|
// On macOS the aes, sha2 and sha3 features are enabled by default and ring
|
|
|
|
// fails to compile on macOS when they are not present.
|
2024-11-27 17:52:57 +00:00
|
|
|
"macos" => vec![sym::neon, sym::aes, sym::sha2, sym::sha3],
|
2024-08-09 17:18:46 +00:00
|
|
|
// AArch64 mandates Neon support
|
|
|
|
_ => vec![sym::neon],
|
|
|
|
}
|
2024-01-26 18:33:45 +00:00
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
2020-06-20 16:22:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-29 15:14:05 +02:00
|
|
|
fn print_version(&self) {
|
|
|
|
println!("Cranelift version: {}", cranelift_codegen::VERSION);
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:45:09 +02:00
|
|
|
fn codegen_crate(
|
2018-06-17 18:05:11 +02:00
|
|
|
&self,
|
2021-03-29 10:45:09 +02:00
|
|
|
tcx: TyCtxt<'_>,
|
2019-05-04 14:57:41 +02:00
|
|
|
metadata: EncodedMetadata,
|
2019-05-04 16:54:25 +02:00
|
|
|
need_metadata_module: bool,
|
2018-11-17 18:23:52 +01:00
|
|
|
) -> Box<dyn Any> {
|
2023-12-21 16:26:09 +11:00
|
|
|
tcx.dcx().abort_if_errors();
|
2024-12-18 17:53:16 -05:00
|
|
|
info!("codegen crate {}", tcx.crate_name(LOCAL_CRATE));
|
2024-12-06 12:10:30 +00:00
|
|
|
let config = self.config.clone().unwrap_or_else(|| {
|
|
|
|
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
|
|
|
|
.unwrap_or_else(|err| tcx.sess.dcx().fatal(err))
|
|
|
|
});
|
2021-04-30 14:49:58 +02:00
|
|
|
match config.codegen_mode {
|
2024-12-06 12:10:30 +00:00
|
|
|
CodegenMode::Aot => driver::aot::run_aot(tcx, metadata, need_metadata_module),
|
2021-04-30 14:49:58 +02:00
|
|
|
CodegenMode::Jit | CodegenMode::JitLazy => {
|
|
|
|
#[cfg(feature = "jit")]
|
2024-12-06 12:10:30 +00:00
|
|
|
driver::jit::run_jit(tcx, config.codegen_mode, config.jit_args);
|
2021-04-30 14:49:58 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "jit"))]
|
2023-12-18 22:21:37 +11:00
|
|
|
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
|
2021-04-30 14:49:58 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-17 18:05:11 +02:00
|
|
|
}
|
|
|
|
|
2020-02-07 13:49:48 +01:00
|
|
|
fn join_codegen(
|
2018-06-17 18:05:11 +02:00
|
|
|
&self,
|
2020-02-07 13:49:48 +01:00
|
|
|
ongoing_codegen: Box<dyn Any>,
|
2022-08-24 18:40:58 +02:00
|
|
|
sess: &Session,
|
2024-03-28 11:43:35 +00:00
|
|
|
outputs: &OutputFilenames,
|
2024-02-17 10:51:35 +11:00
|
|
|
) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>) {
|
2024-12-06 12:10:30 +00:00
|
|
|
ongoing_codegen.downcast::<driver::aot::OngoingCodegen>().unwrap().join(sess, outputs)
|
2020-02-07 13:49:48 +01:00
|
|
|
}
|
2018-06-17 18:05:11 +02:00
|
|
|
}
|
|
|
|
|
2024-12-06 12:10:30 +00:00
|
|
|
/// Determine if the Cranelift ir verifier should run.
|
|
|
|
///
|
|
|
|
/// Returns true when `-Zverify-llvm-ir` is passed, the `CG_CLIF_ENABLE_VERIFIER` env var is set to
|
|
|
|
/// 1 or when cg_clif is compiled with debug assertions enabled or false otherwise.
|
|
|
|
fn enable_verifier(sess: &Session) -> bool {
|
|
|
|
sess.verify_llvm_ir()
|
|
|
|
|| cfg!(debug_assertions)
|
|
|
|
|| env::var("CG_CLIF_ENABLE_VERIFIER").as_deref() == Ok("1")
|
|
|
|
}
|
|
|
|
|
2019-05-25 12:30:21 +02:00
|
|
|
fn target_triple(sess: &Session) -> target_lexicon::Triple {
|
2024-11-01 17:07:18 +01:00
|
|
|
// FIXME(madsmtm): Use `sess.target.llvm_target` once target-lexicon supports unversioned macOS.
|
|
|
|
// See <https://github.com/bytecodealliance/target-lexicon/pull/113>
|
|
|
|
match versioned_llvm_target(sess).parse() {
|
2021-08-06 16:26:56 +02:00
|
|
|
Ok(triple) => triple,
|
2023-12-18 22:21:37 +11:00
|
|
|
Err(err) => sess.dcx().fatal(format!("target not recognized: {}", err)),
|
2021-08-06 16:26:56 +02:00
|
|
|
}
|
2019-05-25 12:30:21 +02:00
|
|
|
}
|
|
|
|
|
2024-12-06 12:10:30 +00:00
|
|
|
fn build_isa(sess: &Session) -> Arc<dyn TargetIsa + 'static> {
|
2019-10-27 16:55:35 +01:00
|
|
|
use target_lexicon::BinaryFormat;
|
|
|
|
|
|
|
|
let target_triple = crate::target_triple(sess);
|
|
|
|
|
2018-12-12 15:11:15 +01:00
|
|
|
let mut flags_builder = settings::builder();
|
2020-12-27 10:30:38 +01:00
|
|
|
flags_builder.enable("is_pic").unwrap();
|
2024-12-06 12:10:30 +00:00
|
|
|
let enable_verifier = if enable_verifier(sess) { "true" } else { "false" };
|
2021-04-30 14:49:58 +02:00
|
|
|
flags_builder.set("enable_verifier", enable_verifier).unwrap();
|
2022-07-25 16:07:57 +02:00
|
|
|
flags_builder.set("regalloc_checker", enable_verifier).unwrap();
|
2018-12-12 15:11:15 +01:00
|
|
|
|
2024-05-04 16:47:18 -07:00
|
|
|
let mut frame_ptr = sess.target.options.frame_pointer.clone();
|
|
|
|
frame_ptr.ratchet(sess.opts.cg.force_frame_pointers);
|
|
|
|
let preserve_frame_pointer = frame_ptr != rustc_target::spec::FramePointer::MayOmit;
|
2023-10-09 08:52:46 +00:00
|
|
|
flags_builder
|
|
|
|
.set("preserve_frame_pointers", if preserve_frame_pointer { "true" } else { "false" })
|
|
|
|
.unwrap();
|
2023-08-09 18:20:12 +00:00
|
|
|
|
2019-10-27 16:55:35 +01:00
|
|
|
let tls_model = match target_triple.binary_format {
|
|
|
|
BinaryFormat::Elf => "elf_gd",
|
|
|
|
BinaryFormat::Macho => "macho",
|
|
|
|
BinaryFormat::Coff => "coff",
|
|
|
|
_ => "none",
|
|
|
|
};
|
|
|
|
flags_builder.set("tls_model", tls_model).unwrap();
|
|
|
|
|
2021-03-29 10:45:09 +02:00
|
|
|
flags_builder.set("enable_llvm_abi_extensions", "true").unwrap();
|
|
|
|
|
2020-03-24 13:09:44 +01:00
|
|
|
use rustc_session::config::OptLevel;
|
2019-05-04 16:54:25 +02:00
|
|
|
match sess.opts.optimize {
|
2018-12-12 15:11:15 +01:00
|
|
|
OptLevel::No => {
|
2020-03-31 14:13:03 +02:00
|
|
|
flags_builder.set("opt_level", "none").unwrap();
|
2018-12-12 15:11:15 +01:00
|
|
|
}
|
2024-11-02 14:53:30 +00:00
|
|
|
OptLevel::Less
|
|
|
|
| OptLevel::Default
|
|
|
|
| OptLevel::Size
|
|
|
|
| OptLevel::SizeMin
|
|
|
|
| OptLevel::Aggressive => {
|
2020-03-31 14:13:03 +02:00
|
|
|
flags_builder.set("opt_level", "speed_and_size").unwrap();
|
2018-12-12 15:11:15 +01:00
|
|
|
}
|
2020-12-27 10:30:38 +01:00
|
|
|
}
|
2018-12-12 15:11:15 +01:00
|
|
|
|
2024-12-06 12:10:30 +00:00
|
|
|
if let target_lexicon::OperatingSystem::Windows = target_triple.operating_system {
|
|
|
|
// FIXME remove dependency on this from the Rust ABI. cc bytecodealliance/wasmtime#9510
|
|
|
|
flags_builder.enable("enable_multi_ret_implicit_sret").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let target_lexicon::Architecture::S390x = target_triple.architecture {
|
|
|
|
// FIXME remove dependency on this from the Rust ABI. cc bytecodealliance/wasmtime#9510
|
|
|
|
flags_builder.enable("enable_multi_ret_implicit_sret").unwrap();
|
|
|
|
}
|
|
|
|
|
2023-03-15 14:41:48 +00:00
|
|
|
if let target_lexicon::Architecture::Aarch64(_)
|
|
|
|
| target_lexicon::Architecture::Riscv64(_)
|
|
|
|
| target_lexicon::Architecture::X86_64 = target_triple.architecture
|
2023-01-24 18:56:42 +01:00
|
|
|
{
|
2023-03-15 14:41:48 +00:00
|
|
|
// Windows depends on stack probes to grow the committed part of the stack.
|
|
|
|
// On other platforms it helps prevents stack smashing.
|
2022-10-23 16:22:55 +02:00
|
|
|
flags_builder.enable("enable_probestack").unwrap();
|
|
|
|
flags_builder.set("probestack_strategy", "inline").unwrap();
|
|
|
|
} else {
|
2023-03-15 14:41:48 +00:00
|
|
|
// __cranelift_probestack is not provided and inline stack probes are only supported on
|
|
|
|
// AArch64, Riscv64 and x86_64.
|
2022-10-23 16:22:55 +02:00
|
|
|
flags_builder.set("enable_probestack", "false").unwrap();
|
|
|
|
}
|
|
|
|
|
2018-12-12 15:11:15 +01:00
|
|
|
let flags = settings::Flags::new(flags_builder);
|
2020-03-27 20:55:54 +01:00
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
let isa_builder = match sess.opts.cg.target_cpu.as_deref() {
|
2024-01-26 18:33:45 +00:00
|
|
|
Some("native") => cranelift_native::builder_with_options(true).unwrap(),
|
2021-04-30 14:49:58 +02:00
|
|
|
Some(value) => {
|
2021-05-27 13:08:14 +02:00
|
|
|
let mut builder =
|
2021-12-20 18:56:35 +01:00
|
|
|
cranelift_codegen::isa::lookup(target_triple.clone()).unwrap_or_else(|err| {
|
2023-12-18 22:21:37 +11:00
|
|
|
sess.dcx().fatal(format!("can't compile for {}: {}", target_triple, err));
|
2021-12-20 18:56:35 +01:00
|
|
|
});
|
2024-01-26 18:33:45 +00:00
|
|
|
if builder.enable(value).is_err() {
|
2023-12-18 22:21:37 +11:00
|
|
|
sess.dcx()
|
|
|
|
.fatal("the specified target cpu isn't currently supported by Cranelift.");
|
2021-04-30 14:49:58 +02:00
|
|
|
}
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
None => {
|
2021-05-27 13:08:14 +02:00
|
|
|
let mut builder =
|
2021-12-20 18:56:35 +01:00
|
|
|
cranelift_codegen::isa::lookup(target_triple.clone()).unwrap_or_else(|err| {
|
2023-12-18 22:21:37 +11:00
|
|
|
sess.dcx().fatal(format!("can't compile for {}: {}", target_triple, err));
|
2021-12-20 18:56:35 +01:00
|
|
|
});
|
2021-07-07 11:14:20 +02:00
|
|
|
if target_triple.architecture == target_lexicon::Architecture::X86_64 {
|
2024-05-13 13:26:33 +00:00
|
|
|
// Only set the target cpu on x86_64 as Cranelift is missing
|
|
|
|
// the target cpu list for most other targets.
|
|
|
|
builder.enable(sess.target.cpu.as_ref()).unwrap();
|
2021-07-07 11:14:20 +02:00
|
|
|
}
|
2021-04-30 14:49:58 +02:00
|
|
|
builder
|
|
|
|
}
|
|
|
|
};
|
2021-05-27 13:08:14 +02:00
|
|
|
|
2022-03-20 16:55:21 +01:00
|
|
|
match isa_builder.finish(flags) {
|
|
|
|
Ok(target_isa) => target_isa,
|
2023-12-18 22:21:37 +11:00
|
|
|
Err(err) => sess.dcx().fatal(format!("failed to build TargetIsa: {}", err)),
|
2022-03-20 16:55:21 +01:00
|
|
|
}
|
2018-12-12 15:11:15 +01:00
|
|
|
}
|
|
|
|
|
2018-07-14 11:59:42 +02:00
|
|
|
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
|
2018-06-17 18:05:11 +02:00
|
|
|
#[no_mangle]
|
2018-11-17 18:23:52 +01:00
|
|
|
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
2024-12-06 12:10:30 +00:00
|
|
|
Box::new(CraneliftCodegenBackend { config: None })
|
2018-06-18 18:39:07 +02:00
|
|
|
}
|