From 94aac0af59d66bc0007149c6eb55039b8d440a54 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 12 Feb 2021 15:39:58 +0000 Subject: [PATCH] Pass around BackendConfig --- src/driver/aot.rs | 12 ++++++++---- src/driver/jit.rs | 21 +++++++++++++++------ src/driver/mod.rs | 8 ++++---- src/lib.rs | 8 ++++++-- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/driver/aot.rs b/src/driver/aot.rs index df89883f0bb..4ae7b42fa66 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -14,7 +14,7 @@ use rustc_session::config::{DebugInfo, OutputType}; use cranelift_object::{ObjectModule, ObjectProduct}; -use crate::prelude::*; +use crate::{prelude::*, BackendConfig}; use crate::backend::AddConstructor; @@ -117,7 +117,10 @@ fn reuse_workproduct_for_cgu( } } -fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodegenResult { +fn module_codegen( + tcx: TyCtxt<'_>, + (backend_config, cgu_name): (BackendConfig, rustc_span::Symbol), +) -> ModuleCodegenResult { let cgu = tcx.codegen_unit(cgu_name); let mono_items = cgu.items_in_deterministic_order(tcx); @@ -148,9 +151,9 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege let mut cx = crate::CodegenCx::new( tcx, + backend_config, module, tcx.sess.opts.debuginfo != DebugInfo::None, - true, ); super::predefine_mono_items(&mut cx, &mono_items); for (mono_item, (linkage, visibility)) in mono_items { @@ -202,6 +205,7 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege pub(super) fn run_aot( tcx: TyCtxt<'_>, + backend_config: BackendConfig, metadata: EncodedMetadata, need_metadata_module: bool, ) -> Box<(CodegenResults, FxHashMap)> { @@ -242,7 +246,7 @@ pub(super) fn run_aot( let (ModuleCodegenResult(module, work_product), _) = tcx.dep_graph.with_task( dep_node, tcx, - cgu.name(), + (backend_config, cgu.name()), module_codegen, rustc_middle::dep_graph::hash_result, ); diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 2d14ff2c022..841b496dd1c 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -10,14 +10,15 @@ use rustc_middle::mir::mono::MonoItem; use cranelift_jit::{JITBuilder, JITModule}; -use crate::prelude::*; +use crate::{prelude::*, BackendConfig}; use crate::{CodegenCx, CodegenMode}; thread_local! { + pub static BACKEND_CONFIG: RefCell> = RefCell::new(None); pub static CURRENT_MODULE: RefCell> = RefCell::new(None); } -pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! { +pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { if !tcx.sess.opts.output_types.should_codegen() { tcx.sess.fatal("JIT mode doesn't work with `cargo check`."); } @@ -46,7 +47,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! { crate::build_isa(tcx.sess), cranelift_module::default_libcall_names(), ); - jit_builder.hotswap(matches!(codegen_mode, CodegenMode::JitLazy)); + jit_builder.hotswap(matches!(backend_config.codegen_mode, CodegenMode::JitLazy)); jit_builder.symbols(imported_symbols); let mut jit_module = JITModule::new(jit_builder); assert_eq!(pointer_ty(tcx), jit_module.target_config().pointer_type()); @@ -74,14 +75,14 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! { .into_iter() .collect::>(); - let mut cx = crate::CodegenCx::new(tcx, jit_module, false, false); + let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false); super::time(tcx, "codegen mono items", || { super::predefine_mono_items(&mut cx, &mono_items); for (mono_item, (linkage, visibility)) in mono_items { let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility); match mono_item { - MonoItem::Fn(inst) => match codegen_mode { + MonoItem::Fn(inst) => match backend_config.codegen_mode { CodegenMode::Aot => unreachable!(), CodegenMode::Jit => { cx.tcx.sess.time("codegen fn", || { @@ -137,6 +138,12 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! { // useful as some dynamic linkers use it as a marker to jump over. argv.push(std::ptr::null()); + BACKEND_CONFIG.with(|tls_backend_config| { + assert!(tls_backend_config + .borrow_mut() + .replace(backend_config) + .is_none()) + }); CURRENT_MODULE .with(|current_module| assert!(current_module.borrow_mut().replace(jit_module).is_none())); @@ -154,7 +161,9 @@ extern "C" fn __clif_jit_fn(instance_ptr: *const Instance<'static>) -> *const u8 CURRENT_MODULE.with(|jit_module| { let mut jit_module = jit_module.borrow_mut(); let jit_module = jit_module.as_mut().unwrap(); - let mut cx = crate::CodegenCx::new(tcx, jit_module, false, false); + let backend_config = + BACKEND_CONFIG.with(|backend_config| backend_config.borrow().clone().unwrap()); + let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false); let name = tcx.symbol_name(instance).name.to_string(); let sig = crate::abi::get_function_sig(tcx, cx.module.isa().triple(), instance); diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 2497f9dfdfb..360810cb58e 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -17,12 +17,12 @@ pub(crate) fn codegen_crate( tcx: TyCtxt<'_>, metadata: EncodedMetadata, need_metadata_module: bool, - config: crate::BackendConfig, + backend_config: crate::BackendConfig, ) -> Box { tcx.sess.abort_if_errors(); - match config.codegen_mode { - CodegenMode::Aot => aot::run_aot(tcx, metadata, need_metadata_module), + match backend_config.codegen_mode { + CodegenMode::Aot => aot::run_aot(tcx, backend_config, metadata, need_metadata_module), CodegenMode::Jit | CodegenMode::JitLazy => { let is_executable = tcx .sess @@ -33,7 +33,7 @@ pub(crate) fn codegen_crate( } #[cfg(feature = "jit")] - let _: ! = jit::run_jit(tcx, config.codegen_mode); + let _: ! = jit::run_jit(tcx, backend_config); #[cfg(not(feature = "jit"))] tcx.sess diff --git a/src/lib.rs b/src/lib.rs index 170750461ca..119716268c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -142,8 +142,12 @@ struct CodegenCx<'tcx, M: Module> { } impl<'tcx, M: Module> CodegenCx<'tcx, M> { - fn new(tcx: TyCtxt<'tcx>, module: M, debug_info: bool, pic_eh_frame: bool) -> Self { - let unwind_context = UnwindContext::new(tcx, module.isa(), pic_eh_frame); + fn new(tcx: TyCtxt<'tcx>, backend_config: BackendConfig, module: M, debug_info: bool) -> Self { + let unwind_context = UnwindContext::new( + tcx, + module.isa(), + matches!(backend_config.codegen_mode, CodegenMode::Aot), + ); let debug_context = if debug_info { Some(DebugContext::new(tcx, module.isa())) } else {