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
42 changes: 20 additions & 22 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,40 +117,43 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
}

pub struct CraneliftCodegenBackend {
pub config: OnceCell<BackendConfig>,
// `None` before `init`, `Some` after.

@bjorn3 bjorn3 Aug 21, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// `None` before `init`, `Some` after.
// Set by `init` if not yet set.

and appropriate changes to init. This is intended to allow setting the config when building CraneliftCodegenBackend already.

View changes since the review

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))
});
let config = 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() {
sess.dcx().fatal("JIT mode doesn't work with `cargo check`");
}
}
self.config = Some(config);

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

fn target_config(&self, sess: &Session) -> TargetConfig {
Expand Down Expand Up @@ -215,7 +217,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 +242,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 +373,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 })
}
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.opts, &tcx.sess.target);
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
10 changes: 5 additions & 5 deletions compiler/rustc_codegen_gcc/src/gcc_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use gccjit::Context;
use rustc_codegen_ssa::target_features;
use rustc_data_structures::smallvec::{SmallVec, smallvec};
use rustc_session::Session;
use rustc_session::config::NATIVE_CPU;
use rustc_target::spec::Arch;
use rustc_session::config::{NATIVE_CPU, Options};
use rustc_target::spec::{Arch, Target};

fn gcc_features_by_flags(sess: &Session, features: &mut Vec<String>) {
target_features::retpoline_features_by_flags(sess, features);
Expand Down Expand Up @@ -130,9 +130,9 @@ fn handle_native(name: &str) -> &str {
unimplemented!();
}

pub fn target_cpu(sess: &Session) -> &str {
match sess.opts.cg.target_cpu {
pub fn target_cpu<'a>(sopts: &'a Options, target: &'a Target) -> &'a str {
match sopts.cg.target_cpu {
Some(ref name) => handle_native(name),
None => handle_native(sess.target.cpu.as_ref()),
None => handle_native(target.cpu.as_ref()),
}
}
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 @@ -1353,7 +1353,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