Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler/rustc_codegen_cranelift/src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ impl WriteBackendMethods for AotDriver {
&self,
_sess: &Session,
_opt_level: OptLevel,
_target_features: &[String],
) -> TargetMachineFactoryFn<Self> {
Arc::new(|_, _| ())
}
Expand Down
51 changes: 27 additions & 24 deletions compiler/rustc_codegen_cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ extern crate rustc_target;
extern crate rustc_driver;

use std::any::Any;
use std::cell::OnceCell;
use std::env;
use std::sync::Arc;

Expand All @@ -43,7 +42,7 @@ use rustc_data_structures::unord::UnordSet;
use rustc_log::tracing::info;
use rustc_middle::dep_graph::WorkProductMap;
use rustc_session::config::{NATIVE_CPU, OutputFilenames};
use rustc_session::{IncrCompSession, Session};
use rustc_session::{CodegenBackendInit, EarlySession, IncrCompSession, Session};
use rustc_span::{Symbol, sym};
use rustc_target::spec::{Arch, CfgAbi, Env, Os};

Expand Down Expand Up @@ -118,43 +117,51 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
}

pub struct CraneliftCodegenBackend {
pub config: OnceCell<BackendConfig>,
// Set by `init` if not already set. (E.g. by cg_clif.)
pub config: Option<BackendConfig>,
}

impl CodegenBackend for CraneliftCodegenBackend {
fn name(&self) -> &'static str {
"cranelift"
}

fn init(&self, sess: &Session) {
use rustc_session::config::{InstrumentCoverage, Lto};
match sess.lto() {
Lto::No | Lto::ThinLocal => {}
Lto::Thin | Lto::Fat => {
sess.dcx().fatal("LTO is not supported by rustc_codegen_cranelift");
fn init(&mut self, sess: &EarlySession) -> CodegenBackendInit {
use rustc_session::config::{InstrumentCoverage, LtoCli};

match (sess.target.requires_lto, sess.early_lto()) {
(true, _) | (false, LtoCli::Yes | LtoCli::Fat | LtoCli::NoParam | LtoCli::Thin) => {
sess.dcx().fatal("LTO is not supported by rustc_codegen_cranelift")
}
(false, LtoCli::Unspecified | LtoCli::No) => {}
}

if sess.opts.cg.instrument_coverage() != InstrumentCoverage::No {
sess.dcx()
.fatal("`-Cinstrument-coverage` is LLVM specific and not supported by Cranelift");
}

let config = self.config.get_or_init(|| {
BackendConfig::from_opts(&sess.opts.cg.llvm_args)
.unwrap_or_else(|err| sess.dcx().fatal(err))
});
// Set `config` if not already set.
if self.config.is_none() {
self.config = Some(
BackendConfig::from_opts(&sess.opts.cg.llvm_args)
.unwrap_or_else(|err| sess.dcx().fatal(err)),
);
}

if config.jit_mode && !sess.opts.output_types.should_codegen() {
if self.config.as_ref().unwrap().jit_mode && !sess.opts.output_types.should_codegen() {
sess.dcx().fatal("JIT mode doesn't work with `cargo check`");
}
}

fn thin_lto_supported(&self) -> bool {
false
CodegenBackendInit {
global_backend_features: vec![],
replaced_intrinsics: vec![],
fallback_intrinsics: vec![sym::type_id_eq],
thin_lto_supported: false,
}
}

fn target_config(&self, sess: &Session) -> TargetConfig {
fn target_config(&self, sess: &EarlySession) -> TargetConfig {
// FIXME return the actually used target features. this is necessary for #[cfg(target_feature)]
let target_features = match sess.target.arch {
Arch::X86_64 if sess.target.os != Os::None => {
Expand Down Expand Up @@ -215,7 +222,7 @@ impl CodegenBackend for CraneliftCodegenBackend {

fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
info!("codegen crate {}", tcx.crate_name(LOCAL_CRATE));
let config = self.config.get().unwrap();
let config = self.config.as_ref().unwrap();
if config.jit_mode {
#[cfg(feature = "jit")]
driver::jit::run_jit(tcx, self.target_cpu(tcx.sess), config.jit_args.clone());
Expand All @@ -240,10 +247,6 @@ impl CodegenBackend for CraneliftCodegenBackend {
.unwrap()
.join(sess, incr_comp_session, crate_info)
}

fn fallback_intrinsics(&self) -> Vec<Symbol> {
vec![sym::type_id_eq]
}
}

/// Determine if the Cranelift ir verifier should run.
Expand Down Expand Up @@ -375,5 +378,5 @@ fn build_isa(sess: &Session, jit: bool) -> Arc<dyn TargetIsa + 'static> {
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
#[unsafe(no_mangle)]
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
Box::new(CraneliftCodegenBackend { config: OnceCell::new() })
Box::new(CraneliftCodegenBackend { config: None })
}
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_gcc/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub fn from_fn_attrs<'gcc, 'tcx>(
.target_features
.iter()
.map(|features| features.name.as_str())
.flat_map(|feat| to_gcc_features(cx.tcx.sess, feat).into_iter())
.flat_map(|feat| to_gcc_features(&cx.tcx.sess.target, feat).into_iter())
.chain(codegen_fn_attrs.instruction_set.iter().map(|x| match *x {
InstructionSetAttr::ArmA32 => "-thumb-mode", // FIXME(antoyo): support removing feature.
InstructionSetAttr::ArmT32 => "thumb-mode",
Expand All @@ -133,7 +133,7 @@ pub fn from_fn_attrs<'gcc, 'tcx>(

// FIXME(antoyo): cg_llvm adds global features to each function so that LTO keep them.
// Check if GCC requires the same.
let mut global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str());
let mut global_features = cx.tcx.sess.global_backend_features.iter().map(|s| s.as_str());
function_features.extend(&mut global_features);
let target_features = function_features
.iter()
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_gcc/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc_target::spec::{Arch, RelocModel};

use crate::builder::Builder;
use crate::context::CodegenCx;
use crate::{GccContext, LockedTargetInfo, LtoMode, SyncContext, gcc_util, new_context};
use crate::{GccContext, LtoMode, SharedTargetInfo, SyncContext, gcc_util, new_context};

#[cfg(feature = "master")]
pub fn visibility_to_gcc(visibility: Visibility) -> gccjit::Visibility {
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn linkage_to_gcc(linkage: Linkage) -> FunctionType {
pub fn compile_codegen_unit(
tcx: TyCtxt<'_>,
cgu_name: Symbol,
target_info: LockedTargetInfo,
target_info: SharedTargetInfo,
lto_supported: bool,
) -> (ModuleCodegen<GccContext>, u64) {
let prof_timer = tcx.prof.generic_activity("codegen_module");
Expand All @@ -96,7 +96,7 @@ pub fn compile_codegen_unit(
fn module_codegen(
tcx: TyCtxt<'_>,
cgu_name: Symbol,
target_info: LockedTargetInfo,
target_info: SharedTargetInfo,
lto_supported: bool,
) -> ModuleCodegen<GccContext> {
let cgu = tcx.codegen_unit(cgu_name);
Expand Down Expand Up @@ -162,7 +162,7 @@ pub fn compile_codegen_unit(

add_pic_option(&context, tcx.sess.relocation_model());

let target_cpu = gcc_util::target_cpu(tcx.sess);
let target_cpu = gcc_util::target_cpu(&tcx.sess);
if target_cpu != "generic" {
context.add_command_line_option(format!("-march={}", target_cpu));
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_gcc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
}
let tcx = self.tcx;
let func = match tcx.lang_items().eh_personality() {
Some(def_id) if !wants_msvc_seh(self.sess()) => {
Some(def_id) if !wants_msvc_seh(&self.sess().target) => {
let instance = ty::Instance::expect_resolve(
tcx,
self.typing_env(),
Expand All @@ -466,7 +466,7 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
self.declare_fn(symbol_name, fn_abi)
}
_ => {
let name = if wants_msvc_seh(self.sess()) {
let name = if wants_msvc_seh(&self.sess().target) {
"__CxxFrameHandler3"
} else {
"rust_eh_personality"
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_codegen_gcc/src/gcc_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
use gccjit::Context;
use rustc_codegen_ssa::target_features;
use rustc_data_structures::smallvec::{SmallVec, smallvec};
use rustc_session::Session;
use rustc_session::EarlySession;
use rustc_session::config::NATIVE_CPU;
use rustc_target::spec::Arch;
use rustc_target::spec::{Arch, Target};

fn gcc_features_by_flags(sess: &Session, features: &mut Vec<String>) {
fn gcc_features_by_flags(sess: &EarlySession, features: &mut Vec<String>) {
target_features::retpoline_features_by_flags(sess, features);
// FIXME: LLVM also sets +reserve-x18 here under some conditions.
}

/// The list of GCC features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
/// `--target` and similar).
pub(crate) fn global_gcc_features(sess: &Session) -> Vec<String> {
pub(crate) fn global_gcc_features(sess: &EarlySession) -> Vec<String> {
// Features that come earlier are overridden by conflicting features later in the string.
// Typically we'll want more explicit settings to override the implicit ones, so:
//
Expand All @@ -40,9 +40,9 @@ pub(crate) fn global_gcc_features(sess: &Session) -> Vec<String> {
// features also work on the command line instead of having two
// different names when the GCC name and the Rust name differ.
features.extend(
to_gcc_features(sess, feature)
to_gcc_features(&sess.target, feature)
.iter()
.flat_map(|feat| to_gcc_features(sess, feat).into_iter())
.flat_map(|feat| to_gcc_features(&sess.target, feat).into_iter())
.map(|feature| if !enable { format!("-{}", feature) } else { feature.to_string() }),
);
};
Expand All @@ -59,9 +59,9 @@ pub(crate) fn global_gcc_features(sess: &Session) -> Vec<String> {
}

// To find a list of GCC's names, check https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
pub fn to_gcc_features<'a>(sess: &Session, s: &'a str) -> SmallVec<[&'a str; 2]> {
pub fn to_gcc_features<'a>(target: &Target, s: &'a str) -> SmallVec<[&'a str; 2]> {
// cSpell:disable
match (&sess.target.arch, s) {
match (&target.arch, s) {
// FIXME: seems like x87 does not exist?
(&Arch::X86 | &Arch::X86_64, "x87") => smallvec![],
(&Arch::X86 | &Arch::X86_64, "sse4.2") => smallvec!["sse4.2", "crc32"],
Expand Down Expand Up @@ -130,7 +130,7 @@ fn handle_native(name: &str) -> &str {
unimplemented!();
}

pub fn target_cpu(sess: &Session) -> &str {
pub fn target_cpu(sess: &EarlySession) -> &str {
match sess.opts.cg.target_cpu {
Some(ref name) => handle_native(name),
None => handle_native(sess.target.cpu.as_ref()),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/src/intrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ fn try_intrinsic<'a, 'b, 'gcc, 'tcx>(
// we can never unwind.
OperandValue::Immediate(bx.const_bool(false)).store(bx, dest);
} else {
if wants_msvc_seh(bx.sess()) {
if wants_msvc_seh(&bx.sess().target) {
unimplemented!();
}
#[cfg(feature = "master")]
Expand Down
Loading
Loading