2023-12-22 04:14:11 -06:00
|
|
|
#![cfg_attr(doc, allow(internal_features))]
|
|
|
|
#![cfg_attr(doc, feature(rustdoc_internals))]
|
|
|
|
#![cfg_attr(doc, doc(rust_logo))]
|
2022-03-20 10:55:21 -05:00
|
|
|
#![feature(rustc_private)]
|
|
|
|
// Note: please avoid adding other feature gates where possible
|
2024-02-05 16:51:39 -06:00
|
|
|
#![allow(rustc::diagnostic_outside_of_impl)]
|
|
|
|
#![allow(rustc::untranslatable_diagnostic)]
|
2020-04-05 06:48:26 -05:00
|
|
|
#![warn(rust_2018_idioms)]
|
|
|
|
#![warn(unused_lifetimes)]
|
2020-11-03 04:00:04 -06:00
|
|
|
#![warn(unreachable_pub)]
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
extern crate jobserver;
|
2020-08-20 09:51:01 -05:00
|
|
|
#[macro_use]
|
2020-03-31 06:20:19 -05:00
|
|
|
extern crate rustc_middle;
|
2020-08-28 05:10:48 -05:00
|
|
|
extern crate rustc_ast;
|
2018-11-24 04:23:49 -06:00
|
|
|
extern crate rustc_codegen_ssa;
|
2018-11-24 05:47:53 -06:00
|
|
|
extern crate rustc_data_structures;
|
2020-04-03 04:54:18 -05:00
|
|
|
extern crate rustc_errors;
|
2018-11-24 05:47:53 -06:00
|
|
|
extern crate rustc_fs_util;
|
2020-01-09 10:43:10 -06:00
|
|
|
extern crate rustc_hir;
|
2018-06-17 11:05:11 -05:00
|
|
|
extern crate rustc_incremental;
|
2019-10-03 10:22:01 -05:00
|
|
|
extern crate rustc_index;
|
2021-05-29 15:49:59 -05:00
|
|
|
extern crate rustc_metadata;
|
2019-12-31 09:43:24 -06:00
|
|
|
extern crate rustc_session;
|
2020-01-06 13:11:03 -06:00
|
|
|
extern crate rustc_span;
|
2018-07-31 05:25:16 -05:00
|
|
|
extern crate rustc_target;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2020-04-05 06:48:26 -05: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 04:17:39 -05:00
|
|
|
use std::any::Any;
|
2022-08-24 11:40:58 -05:00
|
|
|
use std::cell::{Cell, RefCell};
|
|
|
|
use std::sync::Arc;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2023-10-09 03:52:46 -05:00
|
|
|
use cranelift_codegen::isa::TargetIsa;
|
|
|
|
use cranelift_codegen::settings::{self, Configurable};
|
2020-08-28 05:10:48 -05:00
|
|
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
|
|
|
use rustc_codegen_ssa::CodegenResults;
|
2022-08-24 11:40:58 -05:00
|
|
|
use rustc_data_structures::profiling::SelfProfilerRef;
|
2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2021-09-24 11:15:36 -05:00
|
|
|
use rustc_metadata::EncodedMetadata;
|
2020-10-15 03:34:13 -05:00
|
|
|
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
2020-08-28 05:10:48 -05:00
|
|
|
use rustc_session::config::OutputFilenames;
|
|
|
|
use rustc_session::Session;
|
2024-01-26 12:33:45 -06:00
|
|
|
use rustc_span::{sym, Symbol};
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
pub use crate::config::*;
|
2018-11-10 08:12:00 -06:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2018-07-19 12:33:42 -05:00
|
|
|
mod abi;
|
2018-11-05 11:29:15 -06:00
|
|
|
mod allocator;
|
2018-08-09 03:46:56 -05:00
|
|
|
mod analyze;
|
2018-11-09 11:38:30 -06:00
|
|
|
mod archive;
|
2020-08-28 05:10:48 -05:00
|
|
|
mod base;
|
2019-07-31 02:45:11 -05:00
|
|
|
mod cast;
|
2019-07-07 11:08:38 -05:00
|
|
|
mod codegen_i128;
|
2018-06-22 12:18:53 -05:00
|
|
|
mod common;
|
2021-03-29 03:45:09 -05:00
|
|
|
mod compiler_builtins;
|
2022-08-24 11:40:58 -05:00
|
|
|
mod concurrency_limiter;
|
2021-04-30 07:49:58 -05:00
|
|
|
mod config;
|
2018-07-31 05:25:16 -05:00
|
|
|
mod constant;
|
2019-01-17 11:07:27 -06:00
|
|
|
mod debuginfo;
|
2019-08-14 05:01:41 -05:00
|
|
|
mod discriminant;
|
2019-05-04 09:54:25 -05:00
|
|
|
mod driver;
|
2022-08-24 11:40:58 -05:00
|
|
|
mod global_asm;
|
2020-07-10 07:45:45 -05:00
|
|
|
mod inline_asm;
|
2018-10-03 11:21:52 -05:00
|
|
|
mod intrinsics;
|
2019-03-11 14:36:29 -05:00
|
|
|
mod linkage;
|
2018-10-20 11:41:26 -05:00
|
|
|
mod main_shim;
|
2019-08-14 04:52:39 -05:00
|
|
|
mod num;
|
2019-12-26 06:37:10 -06:00
|
|
|
mod optimize;
|
2019-12-20 09:02:47 -06:00
|
|
|
mod pointer;
|
2018-08-15 07:45:32 -05:00
|
|
|
mod pretty_clif;
|
2020-07-09 11:55:46 -05:00
|
|
|
mod toolchain;
|
2018-11-16 10:35:47 -06:00
|
|
|
mod trap;
|
2018-12-29 08:33:34 -06:00
|
|
|
mod unsize;
|
2019-06-11 08:43:22 -05:00
|
|
|
mod value_and_place;
|
2018-09-08 11:00:06 -05:00
|
|
|
mod vtable;
|
2018-06-17 11:05:11 -05:00
|
|
|
|
|
|
|
mod prelude {
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
|
|
|
|
pub(crate) use cranelift_codegen::ir::function::Function;
|
|
|
|
pub(crate) use cranelift_codegen::ir::types;
|
2020-08-28 05:10:48 -05:00
|
|
|
pub(crate) use cranelift_codegen::ir::{
|
2022-10-23 09:22:55 -05:00
|
|
|
AbiParam, Block, FuncRef, Inst, InstBuilder, MemFlags, Signature, SourceLoc, StackSlot,
|
|
|
|
StackSlotData, StackSlotKind, TrapCode, Type, Value,
|
2020-08-28 05:10:48 -05:00
|
|
|
};
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) use cranelift_codegen::isa::{self, CallConv};
|
2020-08-28 05:10:48 -05:00
|
|
|
pub(crate) use cranelift_codegen::Context;
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
|
2023-06-15 12:56:01 -05:00
|
|
|
pub(crate) use cranelift_module::{self, DataDescription, FuncId, Linkage, Module};
|
2023-10-09 03:52:46 -05: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::bug;
|
|
|
|
pub(crate) use rustc_middle::mir::{self, *};
|
|
|
|
pub(crate) use rustc_middle::ty::layout::{self, LayoutOf, TyAndLayout};
|
|
|
|
pub(crate) use rustc_middle::ty::{
|
|
|
|
self, FloatTy, Instance, InstanceDef, IntTy, ParamEnv, Ty, TyCtxt, TypeAndMut,
|
|
|
|
TypeFoldable, TypeVisitableExt, UintTy,
|
|
|
|
};
|
|
|
|
pub(crate) use rustc_span::{FileNameDisplayPreference, Span};
|
|
|
|
pub(crate) use rustc_target::abi::{Abi, FieldIdx, Scalar, Size, VariantIdx, FIRST_VARIANT};
|
2018-06-22 12:18:53 -05:00
|
|
|
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) use crate::abi::*;
|
2020-11-03 04:00:04 -06:00
|
|
|
pub(crate) use crate::base::{codegen_operand, codegen_place};
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) use crate::cast::*;
|
|
|
|
pub(crate) use crate::common::*;
|
2020-06-13 10:03:34 -05:00
|
|
|
pub(crate) use crate::debuginfo::{DebugContext, UnwindContext};
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) use crate::pointer::Pointer;
|
2023-04-29 07:00:43 -05:00
|
|
|
pub(crate) use crate::value_and_place::{CPlace, CValue};
|
2020-06-20 11:44:49 -05:00
|
|
|
}
|
2020-03-17 10:26:56 -05:00
|
|
|
|
2020-06-20 11:44:49 -05: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 10:26:56 -05:00
|
|
|
}
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
|
2021-04-30 07:49:58 -05: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 11:40:58 -05:00
|
|
|
struct CodegenCx {
|
|
|
|
profiler: SelfProfilerRef,
|
|
|
|
output_filenames: Arc<OutputFilenames>,
|
|
|
|
should_write_ir: bool,
|
2020-07-09 12:24:53 -05:00
|
|
|
global_asm: String,
|
2021-12-20 11:56:35 -06:00
|
|
|
inline_asm_index: Cell<usize>,
|
2022-08-24 11:40:58 -05:00
|
|
|
debug_context: Option<DebugContext>,
|
2021-04-30 07:49:58 -05:00
|
|
|
unwind_context: UnwindContext,
|
2021-12-20 11:56:35 -06:00
|
|
|
cgu_name: Symbol,
|
2018-12-18 11:28:02 -06:00
|
|
|
}
|
|
|
|
|
2022-08-24 11:40:58 -05:00
|
|
|
impl CodegenCx {
|
2021-03-05 12:12:59 -06:00
|
|
|
fn new(
|
2022-08-24 11:40:58 -05:00
|
|
|
tcx: TyCtxt<'_>,
|
2021-03-05 12:12:59 -06:00
|
|
|
backend_config: BackendConfig,
|
2021-04-30 07:49:58 -05:00
|
|
|
isa: &dyn TargetIsa,
|
2021-03-05 12:12:59 -06:00
|
|
|
debug_info: bool,
|
2021-12-20 11:56:35 -06:00
|
|
|
cgu_name: Symbol,
|
2021-03-05 12:12:59 -06:00
|
|
|
) -> Self {
|
2021-04-30 07:49:58 -05:00
|
|
|
assert_eq!(pointer_ty(tcx), isa.pointer_type());
|
|
|
|
|
|
|
|
let unwind_context =
|
2021-12-30 07:53:41 -06:00
|
|
|
UnwindContext::new(isa, matches!(backend_config.codegen_mode, CodegenMode::Aot));
|
2022-07-25 09:07:57 -05:00
|
|
|
let debug_context = if debug_info && !tcx.sess.target.options.is_like_windows {
|
|
|
|
Some(DebugContext::new(tcx, isa))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-12-18 11:28:02 -06:00
|
|
|
CodegenCx {
|
2022-08-24 11:40:58 -05:00
|
|
|
profiler: tcx.prof.clone(),
|
|
|
|
output_filenames: tcx.output_filenames(()).clone(),
|
|
|
|
should_write_ir: crate::pretty_clif::should_write_ir(tcx),
|
2020-07-09 12:24:53 -05:00
|
|
|
global_asm: String::new(),
|
2021-12-20 11:56:35 -06:00
|
|
|
inline_asm_index: Cell::new(0),
|
2019-01-17 11:07:27 -06:00
|
|
|
debug_context,
|
2020-05-01 12:21:29 -05:00
|
|
|
unwind_context,
|
2021-12-20 11:56:35 -06:00
|
|
|
cgu_name,
|
2018-12-18 11:28:02 -06:00
|
|
|
}
|
|
|
|
}
|
2020-09-29 11:41:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CraneliftCodegenBackend {
|
2022-08-24 11:40:58 -05:00
|
|
|
pub config: RefCell<Option<BackendConfig>>,
|
2020-09-29 11:41:59 -05:00
|
|
|
}
|
2018-07-23 04:17:39 -05:00
|
|
|
|
2018-07-14 04:59:42 -05:00
|
|
|
impl CodegenBackend for CraneliftCodegenBackend {
|
2022-10-17 08:11:26 -05:00
|
|
|
fn locale_resource(&self) -> &'static str {
|
|
|
|
// FIXME(rust-lang/rust#100717) - cranelift codegen backend is not yet translated
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
2020-01-17 13:33:27 -06:00
|
|
|
fn init(&self, sess: &Session) {
|
2021-03-29 03:45:09 -05:00
|
|
|
use rustc_session::config::Lto;
|
|
|
|
match sess.lto() {
|
|
|
|
Lto::No | Lto::ThinLocal => {}
|
2023-12-18 05:21:37 -06:00
|
|
|
Lto::Thin | Lto::Fat => {
|
|
|
|
sess.dcx().warn("LTO is not supported. You may get a linker error.")
|
|
|
|
}
|
2020-01-17 13:33:27 -06:00
|
|
|
}
|
2022-08-24 11:40:58 -05:00
|
|
|
|
|
|
|
let mut config = self.config.borrow_mut();
|
|
|
|
if config.is_none() {
|
|
|
|
let new_config = BackendConfig::from_opts(&sess.opts.cg.llvm_args)
|
2023-12-18 05:21:37 -06:00
|
|
|
.unwrap_or_else(|err| sess.dcx().fatal(err));
|
2022-08-24 11:40:58 -05:00
|
|
|
*config = Some(new_config);
|
|
|
|
}
|
2020-01-17 13:33:27 -06:00
|
|
|
}
|
2018-06-17 11:05:11 -05:00
|
|
|
|
2024-01-26 12:33:45 -06:00
|
|
|
fn target_features(&self, sess: &Session, _allow_unstable: bool) -> Vec<rustc_span::Symbol> {
|
|
|
|
// 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
|
|
|
|
vec![Symbol::intern("fxsr"), sym::sse, Symbol::intern("sse2")]
|
|
|
|
} else if sess.target.arch == "aarch64" && sess.target.os != "none" {
|
|
|
|
// AArch64 mandates Neon support
|
|
|
|
vec![sym::neon]
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
2020-06-20 09:22:03 -05:00
|
|
|
}
|
|
|
|
|
2021-05-29 08:14:05 -05:00
|
|
|
fn print_version(&self) {
|
|
|
|
println!("Cranelift version: {}", cranelift_codegen::VERSION);
|
|
|
|
}
|
|
|
|
|
2021-03-29 03:45:09 -05:00
|
|
|
fn codegen_crate(
|
2018-06-17 11:05:11 -05:00
|
|
|
&self,
|
2021-03-29 03:45:09 -05:00
|
|
|
tcx: TyCtxt<'_>,
|
2019-05-04 07:57:41 -05:00
|
|
|
metadata: EncodedMetadata,
|
2019-05-04 09:54:25 -05:00
|
|
|
need_metadata_module: bool,
|
2018-11-17 11:23:52 -06:00
|
|
|
) -> Box<dyn Any> {
|
2023-12-20 23:26:09 -06:00
|
|
|
tcx.dcx().abort_if_errors();
|
2022-08-24 11:40:58 -05:00
|
|
|
let config = self.config.borrow().clone().unwrap();
|
2021-04-30 07:49:58 -05:00
|
|
|
match config.codegen_mode {
|
|
|
|
CodegenMode::Aot => driver::aot::run_aot(tcx, config, metadata, need_metadata_module),
|
|
|
|
CodegenMode::Jit | CodegenMode::JitLazy => {
|
|
|
|
#[cfg(feature = "jit")]
|
2022-03-20 10:55:21 -05:00
|
|
|
driver::jit::run_jit(tcx, config);
|
2021-04-30 07:49:58 -05:00
|
|
|
|
|
|
|
#[cfg(not(feature = "jit"))]
|
2023-12-18 05:21:37 -06:00
|
|
|
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
|
2021-04-30 07:49:58 -05:00
|
|
|
}
|
|
|
|
}
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
|
2020-02-07 06:49:48 -06:00
|
|
|
fn join_codegen(
|
2018-06-17 11:05:11 -05:00
|
|
|
&self,
|
2020-02-07 06:49:48 -06:00
|
|
|
ongoing_codegen: Box<dyn Any>,
|
2022-08-24 11:40:58 -05:00
|
|
|
sess: &Session,
|
2021-12-12 18:00:00 -06:00
|
|
|
_outputs: &OutputFilenames,
|
2024-02-16 17:51:35 -06:00
|
|
|
) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>) {
|
|
|
|
ongoing_codegen
|
2022-08-24 11:40:58 -05:00
|
|
|
.downcast::<driver::aot::OngoingCodegen>()
|
|
|
|
.unwrap()
|
2024-02-16 17:51:35 -06:00
|
|
|
.join(sess, self.config.borrow().as_ref().unwrap())
|
2020-02-07 06:49:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn link(
|
|
|
|
&self,
|
|
|
|
sess: &Session,
|
2020-10-15 03:34:13 -05:00
|
|
|
codegen_results: CodegenResults,
|
2018-06-17 11:05:11 -05:00
|
|
|
outputs: &OutputFilenames,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
2019-04-01 12:34:26 -05:00
|
|
|
use rustc_codegen_ssa::back::link::link_binary;
|
|
|
|
|
2022-07-28 04:07:49 -05:00
|
|
|
link_binary(sess, &crate::archive::ArArchiveBuilderBuilder, &codegen_results, outputs)
|
2018-06-17 11:05:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 05:30:21 -05:00
|
|
|
fn target_triple(sess: &Session) -> target_lexicon::Triple {
|
2021-08-06 09:26:56 -05:00
|
|
|
match sess.target.llvm_target.parse() {
|
|
|
|
Ok(triple) => triple,
|
2023-12-18 05:21:37 -06:00
|
|
|
Err(err) => sess.dcx().fatal(format!("target not recognized: {}", err)),
|
2021-08-06 09:26:56 -05:00
|
|
|
}
|
2019-05-25 05:30:21 -05:00
|
|
|
}
|
|
|
|
|
2023-03-15 09:41:48 -05:00
|
|
|
fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Arc<dyn isa::TargetIsa + 'static> {
|
2019-10-27 10:55:35 -05:00
|
|
|
use target_lexicon::BinaryFormat;
|
|
|
|
|
|
|
|
let target_triple = crate::target_triple(sess);
|
|
|
|
|
2018-12-12 08:11:15 -06:00
|
|
|
let mut flags_builder = settings::builder();
|
2020-12-27 03:30:38 -06:00
|
|
|
flags_builder.enable("is_pic").unwrap();
|
2021-04-30 07:49:58 -05:00
|
|
|
let enable_verifier = if backend_config.enable_verifier { "true" } else { "false" };
|
|
|
|
flags_builder.set("enable_verifier", enable_verifier).unwrap();
|
2022-07-25 09:07:57 -05:00
|
|
|
flags_builder.set("regalloc_checker", enable_verifier).unwrap();
|
2018-12-12 08:11:15 -06:00
|
|
|
|
2023-08-09 13:20:12 -05:00
|
|
|
let preserve_frame_pointer = sess.target.options.frame_pointer
|
|
|
|
!= rustc_target::spec::FramePointer::MayOmit
|
|
|
|
|| matches!(sess.opts.cg.force_frame_pointers, Some(true));
|
2023-10-09 03:52:46 -05:00
|
|
|
flags_builder
|
|
|
|
.set("preserve_frame_pointers", if preserve_frame_pointer { "true" } else { "false" })
|
|
|
|
.unwrap();
|
2023-08-09 13:20:12 -05:00
|
|
|
|
2019-10-27 10:55:35 -05: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 03:45:09 -05:00
|
|
|
flags_builder.set("enable_llvm_abi_extensions", "true").unwrap();
|
|
|
|
|
2020-03-24 07:09:44 -05:00
|
|
|
use rustc_session::config::OptLevel;
|
2019-05-04 09:54:25 -05:00
|
|
|
match sess.opts.optimize {
|
2018-12-12 08:11:15 -06:00
|
|
|
OptLevel::No => {
|
2020-03-31 07:13:03 -05:00
|
|
|
flags_builder.set("opt_level", "none").unwrap();
|
2018-12-12 08:11:15 -06:00
|
|
|
}
|
|
|
|
OptLevel::Less | OptLevel::Default => {}
|
2021-03-29 03:45:09 -05:00
|
|
|
OptLevel::Size | OptLevel::SizeMin | OptLevel::Aggressive => {
|
2020-03-31 07:13:03 -05:00
|
|
|
flags_builder.set("opt_level", "speed_and_size").unwrap();
|
2018-12-12 08:11:15 -06:00
|
|
|
}
|
2020-12-27 03:30:38 -06:00
|
|
|
}
|
2018-12-12 08:11:15 -06:00
|
|
|
|
2023-03-15 09:41:48 -05:00
|
|
|
if let target_lexicon::Architecture::Aarch64(_)
|
|
|
|
| target_lexicon::Architecture::Riscv64(_)
|
|
|
|
| target_lexicon::Architecture::X86_64 = target_triple.architecture
|
2023-01-24 11:56:42 -06:00
|
|
|
{
|
2023-03-15 09:41:48 -05: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 09:22:55 -05:00
|
|
|
flags_builder.enable("enable_probestack").unwrap();
|
|
|
|
flags_builder.set("probestack_strategy", "inline").unwrap();
|
|
|
|
} else {
|
2023-03-15 09:41:48 -05:00
|
|
|
// __cranelift_probestack is not provided and inline stack probes are only supported on
|
|
|
|
// AArch64, Riscv64 and x86_64.
|
2022-10-23 09:22:55 -05:00
|
|
|
flags_builder.set("enable_probestack", "false").unwrap();
|
|
|
|
}
|
|
|
|
|
2018-12-12 08:11:15 -06:00
|
|
|
let flags = settings::Flags::new(flags_builder);
|
2020-03-27 14:55:54 -05:00
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
let isa_builder = match sess.opts.cg.target_cpu.as_deref() {
|
2024-01-26 12:33:45 -06:00
|
|
|
Some("native") => cranelift_native::builder_with_options(true).unwrap(),
|
2021-04-30 07:49:58 -05:00
|
|
|
Some(value) => {
|
2021-05-27 06:08:14 -05:00
|
|
|
let mut builder =
|
2021-12-20 11:56:35 -06:00
|
|
|
cranelift_codegen::isa::lookup(target_triple.clone()).unwrap_or_else(|err| {
|
2023-12-18 05:21:37 -06:00
|
|
|
sess.dcx().fatal(format!("can't compile for {}: {}", target_triple, err));
|
2021-12-20 11:56:35 -06:00
|
|
|
});
|
2024-01-26 12:33:45 -06:00
|
|
|
if builder.enable(value).is_err() {
|
2023-12-18 05:21:37 -06:00
|
|
|
sess.dcx()
|
|
|
|
.fatal("the specified target cpu isn't currently supported by Cranelift.");
|
2021-04-30 07:49:58 -05:00
|
|
|
}
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
None => {
|
2021-05-27 06:08:14 -05:00
|
|
|
let mut builder =
|
2021-12-20 11:56:35 -06:00
|
|
|
cranelift_codegen::isa::lookup(target_triple.clone()).unwrap_or_else(|err| {
|
2023-12-18 05:21:37 -06:00
|
|
|
sess.dcx().fatal(format!("can't compile for {}: {}", target_triple, err));
|
2021-12-20 11:56:35 -06:00
|
|
|
});
|
2021-07-07 04:14:20 -05:00
|
|
|
if target_triple.architecture == target_lexicon::Architecture::X86_64 {
|
|
|
|
// Don't use "haswell" as the default, as it implies `has_lzcnt`.
|
|
|
|
// macOS CI is still at Ivy Bridge EP, so `lzcnt` is interpreted as `bsr`.
|
|
|
|
builder.enable("nehalem").unwrap();
|
|
|
|
}
|
2021-04-30 07:49:58 -05:00
|
|
|
builder
|
|
|
|
}
|
|
|
|
};
|
2021-05-27 06:08:14 -05:00
|
|
|
|
2022-03-20 10:55:21 -05:00
|
|
|
match isa_builder.finish(flags) {
|
|
|
|
Ok(target_isa) => target_isa,
|
2023-12-18 05:21:37 -06:00
|
|
|
Err(err) => sess.dcx().fatal(format!("failed to build TargetIsa: {}", err)),
|
2022-03-20 10:55:21 -05:00
|
|
|
}
|
2018-12-12 08:11:15 -06:00
|
|
|
}
|
|
|
|
|
2018-07-14 04:59:42 -05:00
|
|
|
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
|
2018-06-17 11:05:11 -05:00
|
|
|
#[no_mangle]
|
2018-11-17 11:23:52 -06:00
|
|
|
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
2022-08-24 11:40:58 -05:00
|
|
|
Box::new(CraneliftCodegenBackend { config: RefCell::new(None) })
|
2018-06-18 11:39:07 -05:00
|
|
|
}
|