1
Fork 0

Remove all non-tcx references from CodegenCx

This commit is contained in:
bjorn3 2020-06-12 21:15:13 +02:00
parent ba7cdf21be
commit 16b5dac463
6 changed files with 39 additions and 46 deletions

View file

@ -3,8 +3,8 @@ use rustc_index::vec::IndexVec;
use crate::prelude::*; use crate::prelude::*;
pub(crate) fn trans_fn<'clif, 'tcx, B: Backend + 'static>( pub(crate) fn trans_fn<'tcx, B: Backend + 'static>(
cx: &mut crate::CodegenCx<'clif, 'tcx, B>, cx: &mut crate::CodegenCx<'tcx, B>,
instance: Instance<'tcx>, instance: Instance<'tcx>,
linkage: Linkage, linkage: Linkage,
) { ) {
@ -39,7 +39,7 @@ pub(crate) fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
let mut fx = FunctionCx { let mut fx = FunctionCx {
tcx, tcx,
module: cx.module, module: &mut cx.module,
pointer_type, pointer_type,
instance, instance,

View file

@ -15,12 +15,12 @@ pub(crate) struct UnwindContext<'tcx> {
impl<'tcx> UnwindContext<'tcx> { impl<'tcx> UnwindContext<'tcx> {
pub(crate) fn new( pub(crate) fn new(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
module: &mut Module<impl Backend>, isa: &dyn TargetIsa,
) -> Self { ) -> Self {
let mut frame_table = FrameTable::default(); 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)) Some(frame_table.add_cie(cie))
} else { } else {
None None

View file

@ -108,21 +108,11 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege
let cgu = tcx.codegen_unit(cgu_name); let cgu = tcx.codegen_unit(cgu_name);
let mono_items = cgu.items_in_deterministic_order(tcx); 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 mut cx = CodegenCx::new(tcx, module, tcx.sess.opts.debuginfo != DebugInfo::None);
let debug = DebugContext::new( super::codegen_mono_items(&mut cx, mono_items);
tcx, let (mut module, debug, mut unwind_context) = tcx.sess.time("finalize CodegenCx", || cx.finalize());
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);
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context); crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context);
emit_module( emit_module(
@ -185,7 +175,7 @@ pub(super) fn run_aot(
tcx.sess.abort_if_errors(); tcx.sess.abort_if_errors();
let mut allocator_module = new_module(tcx, "allocator_shim".to_string()); 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( let created_alloc_shim = crate::allocator::codegen(
tcx, tcx,
&mut allocator_module, &mut allocator_module,

View file

@ -52,10 +52,11 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>) -> ! {
.into_iter() .into_iter()
.collect::<Vec<(_, (_, _))>>(); .collect::<Vec<(_, (_, _))>>();
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", || { let (mut jit_module, _debug, mut unwind_context) = super::time(tcx, "codegen mono items", || {
super::codegen_mono_items(tcx, &mut jit_module, None, &mut unwind_context, 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::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, &mut unwind_context);
crate::allocator::codegen(tcx, &mut jit_module, &mut unwind_context); crate::allocator::codegen(tcx, &mut jit_module, &mut unwind_context);

View file

@ -30,20 +30,15 @@ pub(crate) fn codegen_crate(
} }
fn codegen_mono_items<'tcx>( fn codegen_mono_items<'tcx>(
tcx: TyCtxt<'tcx>, cx: &mut CodegenCx<'tcx, impl Backend + 'static>,
module: &mut Module<impl Backend + 'static>,
debug_context: Option<&mut DebugContext<'tcx>>,
unwind_context: &mut UnwindContext<'tcx>,
mono_items: Vec<(MonoItem<'tcx>, (RLinkage, Visibility))>, mono_items: Vec<(MonoItem<'tcx>, (RLinkage, Visibility))>,
) { ) {
let mut cx = CodegenCx::new(tcx, module, debug_context, unwind_context); cx.tcx.sess.time("predefine functions", || {
tcx.sess.time("predefine functions", || {
for &(mono_item, (linkage, visibility)) in &mono_items { for &(mono_item, (linkage, visibility)) in &mono_items {
match mono_item { match mono_item {
MonoItem::Fn(instance) => { MonoItem::Fn(instance) => {
let (name, sig) = 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); let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
cx.module.declare_function(&name, linkage, &sig).unwrap(); 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 { for (mono_item, (linkage, visibility)) in mono_items {
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility); 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>( fn trans_mono_item<'tcx, B: Backend + 'static>(
cx: &mut crate::CodegenCx<'clif, 'tcx, B>, cx: &mut crate::CodegenCx<'tcx, B>,
mono_item: MonoItem<'tcx>, mono_item: MonoItem<'tcx>,
linkage: Linkage, linkage: Linkage,
) { ) {

View file

@ -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>, tcx: TyCtxt<'tcx>,
module: &'clif mut Module<B>, module: Module<B>,
constants_cx: ConstantCx, constants_cx: ConstantCx,
cached_context: Context, cached_context: Context,
vtables: FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>, vtables: FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
debug_context: Option<&'clif mut DebugContext<'tcx>>, debug_context: Option<DebugContext<'tcx>>,
unwind_context: &'clif mut UnwindContext<'tcx>, unwind_context: UnwindContext<'tcx>,
} }
impl<'clif, 'tcx, B: Backend + 'static> CodegenCx<'clif, 'tcx, B> { impl<'tcx, B: Backend + 'static> CodegenCx<'tcx, B> {
fn new( fn new(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
module: &'clif mut Module<B>, module: Module<B>,
debug_context: Option<&'clif mut DebugContext<'tcx>>, debug_info: bool,
unwind_context: &'clif mut UnwindContext<'tcx>,
) -> Self { ) -> Self {
let unwind_context = UnwindContext::new(tcx, module.isa());
let debug_context = if debug_info {
Some(DebugContext::new(
tcx,
module.isa(),
))
} else {
None
};
CodegenCx { CodegenCx {
tcx, tcx,
module, module,
@ -154,8 +162,9 @@ impl<'clif, 'tcx, B: Backend + 'static> CodegenCx<'clif, 'tcx, B> {
} }
} }
fn finalize(self) { fn finalize(mut self) -> (Module<B>, Option<DebugContext<'tcx>>, UnwindContext<'tcx>) {
self.constants_cx.finalize(self.tcx, self.module); self.constants_cx.finalize(self.tcx, &mut self.module);
(self.module, self.debug_context, self.unwind_context)
} }
} }