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)]
|
|
|
|
#![warn(unused_lifetimes)]
|
2020-11-03 11:00:04 +01:00
|
|
|
#![warn(unreachable_pub)]
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2022-08-24 18:40:58 +02:00
|
|
|
extern crate jobserver;
|
2020-08-20 16:51:01 +02:00
|
|
|
#[macro_use]
|
2020-03-31 13:20:19 +02:00
|
|
|
extern crate rustc_middle;
|
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-07-07 11:14:20 +02:00
|
|
|
extern crate rustc_interface;
|
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;
|
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;
|
2022-08-24 18:40:58 +02:00
|
|
|
use std::cell::{Cell, RefCell};
|
|
|
|
use std::sync::Arc;
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2020-08-28 12:10:48 +02:00
|
|
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
|
|
|
use rustc_codegen_ssa::CodegenResults;
|
2022-08-24 18:40:58 +02:00
|
|
|
use rustc_data_structures::profiling::SelfProfilerRef;
|
2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
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::config::OutputFilenames;
|
|
|
|
use rustc_session::Session;
|
2021-12-20 18:56:35 +01:00
|
|
|
use rustc_span::Symbol;
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
use cranelift_codegen::isa::TargetIsa;
|
2020-06-20 18:44:49 +02:00
|
|
|
use cranelift_codegen::settings::{self, Configurable};
|
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;
|
2018-11-09 18:38:30 +01:00
|
|
|
mod archive;
|
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;
|
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 {
|
2021-12-20 18:56:35 +01:00
|
|
|
pub(crate) use rustc_span::{FileNameDisplayPreference, Span};
|
2020-03-27 12:14:45 +01:00
|
|
|
|
|
|
|
pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
2020-08-28 12:10:48 +02:00
|
|
|
pub(crate) use rustc_middle::bug;
|
2020-03-31 13:20:19 +02:00
|
|
|
pub(crate) use rustc_middle::mir::{self, *};
|
2021-08-30 17:38:27 +03:00
|
|
|
pub(crate) use rustc_middle::ty::layout::{self, LayoutOf, TyAndLayout};
|
2020-03-31 13:20:19 +02:00
|
|
|
pub(crate) use rustc_middle::ty::{
|
2021-02-01 10:11:46 +01:00
|
|
|
self, FloatTy, Instance, InstanceDef, IntTy, ParamEnv, Ty, TyCtxt, TypeAndMut,
|
2023-02-22 02:18:40 +00:00
|
|
|
TypeFoldable, TypeVisitableExt, UintTy,
|
2018-06-17 18:05:11 +02:00
|
|
|
};
|
2023-03-28 12:32:57 -07:00
|
|
|
pub(crate) use rustc_target::abi::{Abi, FieldIdx, Scalar, Size, VariantIdx, FIRST_VARIANT};
|
2019-10-03 17:22:01 +02:00
|
|
|
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) use rustc_data_structures::fx::FxHashMap;
|
|
|
|
|
2023-04-19 10:57:17 +00:00
|
|
|
pub(crate) use rustc_index::Idx;
|
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;
|
|
|
|
pub(crate) use cranelift_codegen::ir::types;
|
2020-08-28 12:10:48 +02:00
|
|
|
pub(crate) use cranelift_codegen::ir::{
|
2022-10-23 16:22:55 +02:00
|
|
|
AbiParam, Block, FuncRef, Inst, InstBuilder, MemFlags, Signature, SourceLoc, StackSlot,
|
|
|
|
StackSlotData, StackSlotKind, TrapCode, Type, Value,
|
2020-08-28 12:10:48 +02:00
|
|
|
};
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) use cranelift_codegen::isa::{self, CallConv};
|
2020-08-28 12:10:48 +02:00
|
|
|
pub(crate) use cranelift_codegen::Context;
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
|
2021-06-20 17:43:25 +08:00
|
|
|
pub(crate) use cranelift_module::{self, DataContext, FuncId, Linkage, Module};
|
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 {
|
|
|
|
profiler: SelfProfilerRef,
|
|
|
|
output_filenames: Arc<OutputFilenames>,
|
|
|
|
should_write_ir: bool,
|
2020-07-09 19:24:53 +02:00
|
|
|
global_asm: String,
|
2021-12-20 18:56:35 +01:00
|
|
|
inline_asm_index: Cell<usize>,
|
2022-08-24 18:40:58 +02:00
|
|
|
debug_context: Option<DebugContext>,
|
2021-04-30 14:49:58 +02:00
|
|
|
unwind_context: UnwindContext,
|
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 {
|
2021-03-05 19:12:59 +01:00
|
|
|
fn new(
|
2022-08-24 18:40:58 +02:00
|
|
|
tcx: TyCtxt<'_>,
|
2021-03-05 19:12:59 +01:00
|
|
|
backend_config: BackendConfig,
|
2021-04-30 14:49:58 +02:00
|
|
|
isa: &dyn TargetIsa,
|
2021-03-05 19:12:59 +01:00
|
|
|
debug_info: bool,
|
2021-12-20 18:56:35 +01:00
|
|
|
cgu_name: Symbol,
|
2021-03-05 19:12:59 +01:00
|
|
|
) -> Self {
|
2021-04-30 14:49:58 +02:00
|
|
|
assert_eq!(pointer_ty(tcx), isa.pointer_type());
|
|
|
|
|
|
|
|
let unwind_context =
|
2021-12-30 14:53:41 +01:00
|
|
|
UnwindContext::new(isa, matches!(backend_config.codegen_mode, CodegenMode::Aot));
|
2022-07-25 16:07:57 +02:00
|
|
|
let debug_context = if debug_info && !tcx.sess.target.options.is_like_windows {
|
|
|
|
Some(DebugContext::new(tcx, isa))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-12-18 18:28:02 +01:00
|
|
|
CodegenCx {
|
2022-08-24 18:40:58 +02:00
|
|
|
profiler: tcx.prof.clone(),
|
|
|
|
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(),
|
2021-12-20 18:56:35 +01:00
|
|
|
inline_asm_index: Cell::new(0),
|
2019-01-17 18:07:27 +01:00
|
|
|
debug_context,
|
2020-05-01 19:21:29 +02:00
|
|
|
unwind_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 {
|
2022-08-24 18:40:58 +02:00
|
|
|
pub config: RefCell<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) {
|
2021-03-29 10:45:09 +02:00
|
|
|
use rustc_session::config::Lto;
|
|
|
|
match sess.lto() {
|
|
|
|
Lto::No | Lto::ThinLocal => {}
|
|
|
|
Lto::Thin | Lto::Fat => sess.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
|
|
|
|
|
|
|
let mut config = self.config.borrow_mut();
|
|
|
|
if config.is_none() {
|
|
|
|
let new_config = BackendConfig::from_opts(&sess.opts.cg.llvm_args)
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
.unwrap_or_else(|err| sess.fatal(err));
|
2022-08-24 18:40:58 +02:00
|
|
|
*config = Some(new_config);
|
|
|
|
}
|
2020-01-17 20:33:27 +01:00
|
|
|
}
|
2018-06-17 18:05:11 +02:00
|
|
|
|
2022-07-11 14:26:58 +01:00
|
|
|
fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<rustc_span::Symbol> {
|
2020-08-15 21:04:33 +02:00
|
|
|
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> {
|
2021-04-30 14:49:58 +02:00
|
|
|
tcx.sess.abort_if_errors();
|
2022-08-24 18:40:58 +02:00
|
|
|
let config = self.config.borrow().clone().unwrap();
|
2021-04-30 14:49:58 +02: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 16:55:21 +01:00
|
|
|
driver::jit::run_jit(tcx, config);
|
2021-04-30 14:49:58 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "jit"))]
|
|
|
|
tcx.sess.fatal("jit support was disabled when compiling rustc_codegen_cranelift");
|
|
|
|
}
|
|
|
|
}
|
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,
|
2021-12-13 00:00:00 +00:00
|
|
|
_outputs: &OutputFilenames,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
|
2022-08-24 18:40:58 +02:00
|
|
|
Ok(ongoing_codegen
|
|
|
|
.downcast::<driver::aot::OngoingCodegen>()
|
|
|
|
.unwrap()
|
|
|
|
.join(sess, self.config.borrow().as_ref().unwrap()))
|
2020-02-07 13:49:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn link(
|
|
|
|
&self,
|
|
|
|
sess: &Session,
|
2020-10-15 10:34:13 +02:00
|
|
|
codegen_results: CodegenResults,
|
2018-06-17 18:05:11 +02:00
|
|
|
outputs: &OutputFilenames,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
2019-04-01 19:34:26 +02:00
|
|
|
use rustc_codegen_ssa::back::link::link_binary;
|
|
|
|
|
2022-07-28 09:07:49 +00:00
|
|
|
link_binary(sess, &crate::archive::ArArchiveBuilderBuilder, &codegen_results, outputs)
|
2018-06-17 18:05:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 12:30:21 +02:00
|
|
|
fn target_triple(sess: &Session) -> target_lexicon::Triple {
|
2021-08-06 16:26:56 +02:00
|
|
|
match sess.target.llvm_target.parse() {
|
|
|
|
Ok(triple) => triple,
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
Err(err) => sess.fatal(format!("target not recognized: {}", err)),
|
2021-08-06 16:26:56 +02:00
|
|
|
}
|
2019-05-25 12:30:21 +02:00
|
|
|
}
|
|
|
|
|
2023-03-15 14:41:48 +00:00
|
|
|
fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Arc<dyn isa::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();
|
2021-04-30 14:49:58 +02:00
|
|
|
let enable_verifier = if backend_config.enable_verifier { "true" } else { "false" };
|
|
|
|
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
|
|
|
|
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();
|
|
|
|
|
2020-03-27 20:55:54 +01:00
|
|
|
flags_builder.set("enable_simd", "true").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
|
|
|
}
|
|
|
|
OptLevel::Less | OptLevel::Default => {}
|
2021-03-29 10:45:09 +02:00
|
|
|
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
|
|
|
|
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() {
|
|
|
|
Some("native") => {
|
2021-12-20 18:56:35 +01:00
|
|
|
let builder = cranelift_native::builder_with_options(true).unwrap();
|
2021-04-30 14:49:58 +02:00
|
|
|
builder
|
|
|
|
}
|
|
|
|
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| {
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
sess.fatal(format!("can't compile for {}: {}", target_triple, err));
|
2021-12-20 18:56:35 +01:00
|
|
|
});
|
2021-04-30 14:49:58 +02:00
|
|
|
if let Err(_) = builder.enable(value) {
|
2021-08-06 16:26:56 +02:00
|
|
|
sess.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| {
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
sess.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 {
|
|
|
|
// 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 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,
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
Err(err) => sess.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> {
|
2022-08-24 18:40:58 +02:00
|
|
|
Box::new(CraneliftCodegenBackend { config: RefCell::new(None) })
|
2018-06-18 18:39:07 +02:00
|
|
|
}
|