diff --git a/src/base.rs b/src/base.rs index 34c20204726..acdfba4323a 100644 --- a/src/base.rs +++ b/src/base.rs @@ -3,8 +3,8 @@ use rustc_index::vec::IndexVec; use crate::prelude::*; -pub(crate) fn trans_fn<'clif, 'tcx, B: Backend + 'static>( - cx: &mut crate::CodegenCx<'clif, 'tcx, B>, +pub(crate) fn trans_fn<'tcx, B: Backend + 'static>( + cx: &mut crate::CodegenCx<'tcx, B>, instance: Instance<'tcx>, linkage: Linkage, ) { @@ -39,7 +39,7 @@ pub(crate) fn trans_fn<'clif, 'tcx, B: Backend + 'static>( let mut fx = FunctionCx { tcx, - module: cx.module, + module: &mut cx.module, pointer_type, instance, diff --git a/src/debuginfo/unwind.rs b/src/debuginfo/unwind.rs index a0f933d7c62..a8e52f99b67 100644 --- a/src/debuginfo/unwind.rs +++ b/src/debuginfo/unwind.rs @@ -15,12 +15,12 @@ pub(crate) struct UnwindContext<'tcx> { impl<'tcx> UnwindContext<'tcx> { pub(crate) fn new( tcx: TyCtxt<'tcx>, - module: &mut Module, + isa: &dyn TargetIsa, ) -> Self { let mut frame_table = FrameTable::default(); - let cie_id = if let Some(cie) = module.isa().create_systemv_cie() { + let cie_id = if let Some(cie) = isa.create_systemv_cie() { Some(frame_table.add_cie(cie)) } else { None diff --git a/src/driver/aot.rs b/src/driver/aot.rs index 27c76fbee1d..303031c0c03 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -108,21 +108,11 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege let cgu = tcx.codegen_unit(cgu_name); let mono_items = cgu.items_in_deterministic_order(tcx); - let mut module = new_module(tcx, cgu_name.as_str().to_string()); + let module = new_module(tcx, cgu_name.as_str().to_string()); - let mut debug = if tcx.sess.opts.debuginfo != DebugInfo::None { - let debug = DebugContext::new( - tcx, - module.isa(), - ); - Some(debug) - } else { - None - }; - - let mut unwind_context = UnwindContext::new(tcx, &mut module); - - super::codegen_mono_items(tcx, &mut module, debug.as_mut(), &mut unwind_context, mono_items); + let mut cx = CodegenCx::new(tcx, module, tcx.sess.opts.debuginfo != DebugInfo::None); + super::codegen_mono_items(&mut cx, mono_items); + let (mut module, debug, mut unwind_context) = tcx.sess.time("finalize CodegenCx", || cx.finalize()); crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context); emit_module( @@ -185,7 +175,7 @@ pub(super) fn run_aot( tcx.sess.abort_if_errors(); let mut allocator_module = new_module(tcx, "allocator_shim".to_string()); - let mut allocator_unwind_context = UnwindContext::new(tcx, &mut allocator_module); + let mut allocator_unwind_context = UnwindContext::new(tcx, allocator_module.isa()); let created_alloc_shim = crate::allocator::codegen( tcx, &mut allocator_module, diff --git a/src/driver/jit.rs b/src/driver/jit.rs index fa88029ceb9..ab6c9da25ad 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -52,10 +52,11 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>) -> ! { .into_iter() .collect::>(); - let mut unwind_context = UnwindContext::new(tcx, &mut jit_module); + let mut cx = CodegenCx::new(tcx, jit_module, false); - super::time(tcx, "codegen mono items", || { - super::codegen_mono_items(tcx, &mut jit_module, None, &mut unwind_context, mono_items); + let (mut jit_module, _debug, mut unwind_context) = super::time(tcx, "codegen mono items", || { + super::codegen_mono_items(&mut cx, mono_items); + tcx.sess.time("finalize CodegenCx", || cx.finalize()) }); crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, &mut unwind_context); crate::allocator::codegen(tcx, &mut jit_module, &mut unwind_context); diff --git a/src/driver/mod.rs b/src/driver/mod.rs index e72e15a3279..ded3bbe1e2c 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -30,20 +30,15 @@ pub(crate) fn codegen_crate( } fn codegen_mono_items<'tcx>( - tcx: TyCtxt<'tcx>, - module: &mut Module, - debug_context: Option<&mut DebugContext<'tcx>>, - unwind_context: &mut UnwindContext<'tcx>, + cx: &mut CodegenCx<'tcx, impl Backend + 'static>, mono_items: Vec<(MonoItem<'tcx>, (RLinkage, Visibility))>, ) { - let mut cx = CodegenCx::new(tcx, module, debug_context, unwind_context); - - tcx.sess.time("predefine functions", || { + cx.tcx.sess.time("predefine functions", || { for &(mono_item, (linkage, visibility)) in &mono_items { match mono_item { MonoItem::Fn(instance) => { let (name, sig) = - get_function_name_and_sig(tcx, cx.module.isa().triple(), instance, false); + get_function_name_and_sig(cx.tcx, cx.module.isa().triple(), instance, false); let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility); cx.module.declare_function(&name, linkage, &sig).unwrap(); } @@ -54,14 +49,12 @@ fn codegen_mono_items<'tcx>( for (mono_item, (linkage, visibility)) in mono_items { let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility); - trans_mono_item(&mut cx, mono_item, linkage); + trans_mono_item(cx, mono_item, linkage); } - - tcx.sess.time("finalize CodegenCx", || cx.finalize()); } -fn trans_mono_item<'clif, 'tcx, B: Backend + 'static>( - cx: &mut crate::CodegenCx<'clif, 'tcx, B>, +fn trans_mono_item<'tcx, B: Backend + 'static>( + cx: &mut crate::CodegenCx<'tcx, B>, mono_item: MonoItem<'tcx>, linkage: Linkage, ) { diff --git a/src/lib.rs b/src/lib.rs index 4e3fbc3eaaa..afaeb6d1e25 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -126,23 +126,31 @@ mod prelude { } } -pub(crate) struct CodegenCx<'clif, 'tcx, B: Backend + 'static> { +pub(crate) struct CodegenCx<'tcx, B: Backend + 'static> { tcx: TyCtxt<'tcx>, - module: &'clif mut Module, + module: Module, constants_cx: ConstantCx, cached_context: Context, vtables: FxHashMap<(Ty<'tcx>, Option>), DataId>, - debug_context: Option<&'clif mut DebugContext<'tcx>>, - unwind_context: &'clif mut UnwindContext<'tcx>, + debug_context: Option>, + unwind_context: UnwindContext<'tcx>, } -impl<'clif, 'tcx, B: Backend + 'static> CodegenCx<'clif, 'tcx, B> { +impl<'tcx, B: Backend + 'static> CodegenCx<'tcx, B> { fn new( tcx: TyCtxt<'tcx>, - module: &'clif mut Module, - debug_context: Option<&'clif mut DebugContext<'tcx>>, - unwind_context: &'clif mut UnwindContext<'tcx>, + module: Module, + debug_info: bool, ) -> Self { + let unwind_context = UnwindContext::new(tcx, module.isa()); + let debug_context = if debug_info { + Some(DebugContext::new( + tcx, + module.isa(), + )) + } else { + None + }; CodegenCx { tcx, module, @@ -154,8 +162,9 @@ impl<'clif, 'tcx, B: Backend + 'static> CodegenCx<'clif, 'tcx, B> { } } - fn finalize(self) { - self.constants_cx.finalize(self.tcx, self.module); + fn finalize(mut self) -> (Module, Option>, UnwindContext<'tcx>) { + self.constants_cx.finalize(self.tcx, &mut self.module); + (self.module, self.debug_context, self.unwind_context) } }