From 429e75fe4007835e60e76bff38ed5d97cd134af3 Mon Sep 17 00:00:00 2001 From: CohenArthur Date: Sat, 22 Aug 2020 15:49:16 +0200 Subject: [PATCH] FunctionCx: WIP: Remove more common fields from CodegenCx --- src/abi/mod.rs | 2 +- src/base.rs | 26 +++++++++++--------------- src/common.rs | 3 --- src/constant.rs | 16 ++++++++-------- src/debuginfo/line_info.rs | 4 ++-- src/debuginfo/mod.rs | 8 ++++---- src/debuginfo/unwind.rs | 4 ++-- src/driver/mod.rs | 10 +++++----- src/lib.rs | 4 ++-- src/vtable.rs | 4 ++-- 10 files changed, 37 insertions(+), 44 deletions(-) diff --git a/src/abi/mod.rs b/src/abi/mod.rs index ca6680f1e14..65613754d06 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -226,7 +226,7 @@ pub(crate) fn import_function<'tcx>( impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> { /// Instance must be monomorphized pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef { - let func_id = import_function(self.codegen_cx.tcx, self.codegen_cx.module, inst); + let func_id = import_function(self.codegen_cx.tcx, &mut self.codegen_cx.module, inst); let func_ref = self .codegen_cx.module .declare_func_in_func(func_id, &mut self.bcx.func); diff --git a/src/base.rs b/src/base.rs index bdcb931b827..92d910dd998 100644 --- a/src/base.rs +++ b/src/base.rs @@ -8,13 +8,13 @@ pub(crate) fn trans_fn<'tcx, B: Backend + 'static>( instance: Instance<'tcx>, linkage: Linkage, ) { - let tcx = cx.codegen_cx.tcx; + let tcx = cx.tcx; let mir = tcx.instance_mir(instance.def); // Declare function - let (name, sig) = get_function_name_and_sig(tcx, cx.codegen_cx.module.isa().triple(), instance, false); - let func_id = cx.codegen_cx.module.declare_function(&name, linkage, &sig).unwrap(); + let (name, sig) = get_function_name_and_sig(tcx, cx.module.isa().triple(), instance, false); + let func_id = cx.module.declare_function(&name, linkage, &sig).unwrap(); // Make FunctionBuilder let context = &mut cx.cached_context; @@ -30,13 +30,11 @@ pub(crate) fn trans_fn<'tcx, B: Backend + 'static>( let block_map: IndexVec = (0..mir.basic_blocks().len()).map(|_| bcx.create_block()).collect(); // Make FunctionCx - let pointer_type = cx.codegen_cx.module.target_config().pointer_type(); + let pointer_type = cx.module.target_config().pointer_type(); let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance); let mut fx = FunctionCx { - tcx, - module: &mut cx.codegen_cx.module, - global_asm: &mut cx.global_asm, + codegen_cx: cx, pointer_type, instance, @@ -49,8 +47,6 @@ pub(crate) fn trans_fn<'tcx, B: Backend + 'static>( cold_blocks: EntitySet::new(), clif_comments, - constants_cx: &mut cx.constants_cx, - vtables: &mut cx.vtables, source_info_set: indexmap::IndexSet::new(), next_ssa_var: 0, @@ -78,7 +74,7 @@ pub(crate) fn trans_fn<'tcx, B: Backend + 'static>( let cold_blocks = fx.cold_blocks; crate::pretty_clif::write_clif_file( - cx.codegen_cx.tcx, + cx.tcx, "unopt", None, instance, @@ -98,10 +94,10 @@ pub(crate) fn trans_fn<'tcx, B: Backend + 'static>( // instruction, which doesn't have an encoding. context.compute_cfg(); context.compute_domtree(); - context.eliminate_unreachable_code(cx.codegen_cx.module.isa()).unwrap(); + context.eliminate_unreachable_code(cx.module.isa()).unwrap(); // Define function - let module = &mut cx.codegen_cx.module; + let module = &mut cx.module; tcx.sess.time( "define function", || module.define_function( @@ -113,16 +109,16 @@ pub(crate) fn trans_fn<'tcx, B: Backend + 'static>( // Write optimized function to file for debugging crate::pretty_clif::write_clif_file( - cx.codegen_cx.tcx, + cx.tcx, "opt", - Some(cx.codegen_cx.module.isa()), + Some(cx.module.isa()), instance, &context, &clif_comments, ); // Define debuginfo for function - let isa = cx.codegen_cx.module.isa(); + let isa = cx.module.isa(); let debug_context = &mut cx.debug_context; let unwind_context = &mut cx.unwind_context; tcx.sess.time("generate debug info", || { diff --git a/src/common.rs b/src/common.rs index df91668c1e9..bf7fc570818 100644 --- a/src/common.rs +++ b/src/common.rs @@ -266,7 +266,6 @@ pub(crate) fn type_sign(ty: Ty<'_>) -> bool { pub(crate) struct FunctionCx<'clif, 'tcx, B: Backend + 'static> { pub(crate) codegen_cx: &'clif mut crate::CodegenCx<'tcx, B>, - pub(crate) global_asm: &'clif mut String, pub(crate) pointer_type: Type, // Cached from module pub(crate) instance: Instance<'tcx>, @@ -283,8 +282,6 @@ pub(crate) struct FunctionCx<'clif, 'tcx, B: Backend + 'static> { pub(crate) cold_blocks: EntitySet, pub(crate) clif_comments: crate::pretty_clif::CommentWriter, - pub(crate) vtables: &'clif mut FxHashMap<(Ty<'tcx>, Option>), DataId>, - pub(crate) source_info_set: indexmap::IndexSet, /// This should only be accessed by `CPlace::new_var`. diff --git a/src/constant.rs b/src/constant.rs index 3c05e4aa793..7f047b04c9d 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -67,7 +67,7 @@ pub(crate) fn codegen_tls_ref<'tcx>( def_id: DefId, layout: TyAndLayout<'tcx>, ) -> CValue<'tcx> { - let data_id = data_id_for_static(fx.codegen_cx.tcx, fx.codegen_cx.module, def_id, false); + let data_id = data_id_for_static(fx.codegen_cx.tcx, &mut fx.codegen_cx.module, def_id, false); let local_data_id = fx.codegen_cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); #[cfg(debug_assertions)] fx.add_comment(local_data_id, format!("tls {:?}", def_id)); @@ -80,7 +80,7 @@ fn codegen_static_ref<'tcx>( def_id: DefId, layout: TyAndLayout<'tcx>, ) -> CPlace<'tcx> { - let data_id = data_id_for_static(fx.codegen_cx.tcx, fx.codegen_cx.module, def_id, false); + let data_id = data_id_for_static(fx.codegen_cx.tcx, &mut fx.codegen_cx.module, def_id, false); let local_data_id = fx.codegen_cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); #[cfg(debug_assertions)] fx.add_comment(local_data_id, format!("{:?}", def_id)); @@ -167,21 +167,21 @@ pub(crate) fn trans_const_value<'tcx>( let alloc_kind = fx.codegen_cx.tcx.get_global_alloc(ptr.alloc_id); let base_addr = match alloc_kind { Some(GlobalAlloc::Memory(alloc)) => { - fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id)); - let data_id = data_id_for_alloc_id(fx.codegen_cx.module, ptr.alloc_id, alloc.align, alloc.mutability); + fx.codegen_cx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id)); + let data_id = data_id_for_alloc_id(&mut fx.codegen_cx.module, ptr.alloc_id, alloc.align, alloc.mutability); let local_data_id = fx.codegen_cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); #[cfg(debug_assertions)] fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id)); fx.bcx.ins().global_value(fx.pointer_type, local_data_id) } Some(GlobalAlloc::Function(instance)) => { - let func_id = crate::abi::import_function(fx.codegen_cx.tcx, fx.codegen_cx.module, instance); + let func_id = crate::abi::import_function(fx.codegen_cx.tcx, &mut fx.codegen_cx.module, instance); let local_func_id = fx.codegen_cx.module.declare_func_in_func(func_id, &mut fx.bcx.func); fx.bcx.ins().func_addr(fx.pointer_type, local_func_id) } Some(GlobalAlloc::Static(def_id)) => { assert!(fx.codegen_cx.tcx.is_static(def_id)); - let data_id = data_id_for_static(fx.codegen_cx.tcx, fx.codegen_cx.module, def_id, false); + let data_id = data_id_for_static(fx.codegen_cx.tcx, &mut fx.codegen_cx.module, def_id, false); let local_data_id = fx.codegen_cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); #[cfg(debug_assertions)] fx.add_comment(local_data_id, format!("{:?}", def_id)); @@ -216,8 +216,8 @@ fn pointer_for_allocation<'tcx>( alloc: &'tcx Allocation, ) -> crate::pointer::Pointer { let alloc_id = fx.codegen_cx.tcx.create_memory_alloc(alloc); - fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id)); - let data_id = data_id_for_alloc_id(fx.codegen_cx.module, alloc_id, alloc.align, alloc.mutability); + fx.codegen_cx.constants_cx.todo.push(TodoItem::Alloc(alloc_id)); + let data_id = data_id_for_alloc_id(&mut fx.codegen_cx.module, alloc_id, alloc.align, alloc.mutability); let local_data_id = fx.codegen_cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); #[cfg(debug_assertions)] diff --git a/src/debuginfo/line_info.rs b/src/debuginfo/line_info.rs index 6167fb8a56d..89dccb8545f 100644 --- a/src/debuginfo/line_info.rs +++ b/src/debuginfo/line_info.rs @@ -95,7 +95,7 @@ fn line_program_add_file( impl<'tcx> DebugContext<'tcx> { pub(super) fn emit_location(&mut self, entry_id: UnitEntryId, span: Span) { - let loc = self.codegen_cx.tcx.sess.source_map().lookup_char_pos(span.lo()); + let loc = self.tcx.sess.source_map().lookup_char_pos(span.lo()); let file_id = line_program_add_file( &mut self.dwarf.unit.line_program, @@ -129,7 +129,7 @@ impl<'tcx> DebugContext<'tcx> { function_span: Span, source_info_set: &indexmap::IndexSet, ) -> CodeOffset { - let tcx = self.codegen_cx.tcx; + let tcx = self.tcx; let line_program = &mut self.dwarf.unit.line_program; let func = &context.func; diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs index 54d51e4d41a..2c92fd0e47c 100644 --- a/src/debuginfo/mod.rs +++ b/src/debuginfo/mod.rs @@ -165,7 +165,7 @@ impl<'tcx> DebugContext<'tcx> { }; let name = format!("{}", ty); - let layout = self.codegen_cx.tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap(); + let layout = self.tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap(); let type_id = match ty.kind { ty::Bool => primitive(&mut self.dwarf, gimli::DW_ATE_boolean), @@ -203,7 +203,7 @@ impl<'tcx> DebugContext<'tcx> { for (field_idx, field_def) in variant.fields.iter().enumerate() { let field_offset = layout.fields.offset(field_idx); let field_layout = layout.field(&layout::LayoutCx { - tcx: self.codegen_cx.tcx, + tcx: self.tcx, param_env: ParamEnv::reveal_all(), }, field_idx).unwrap(); @@ -261,7 +261,7 @@ impl<'tcx> DebugContext<'tcx> { local_map: FxHashMap>, ) { let symbol = func_id.as_u32() as usize; - let mir = self.codegen_cx.tcx.instance_mir(instance.def); + let mir = self.tcx.instance_mir(instance.def); // FIXME: add to appropriate scope instead of root let scope = self.dwarf.unit.root(); @@ -336,7 +336,7 @@ impl<'tcx> DebugContext<'tcx> { let value_labels_ranges = context.build_value_labels_ranges(isa).unwrap(); for (local, _local_decl) in mir.local_decls.iter_enumerated() { - let ty = self.codegen_cx.tcx.subst_and_normalize_erasing_regions( + let ty = self.tcx.subst_and_normalize_erasing_regions( instance.substs, ty::ParamEnv::reveal_all(), &mir.local_decls[local].ty, diff --git a/src/debuginfo/unwind.rs b/src/debuginfo/unwind.rs index 0c3686f4bb0..4bc3859652d 100644 --- a/src/debuginfo/unwind.rs +++ b/src/debuginfo/unwind.rs @@ -54,7 +54,7 @@ impl<'tcx> UnwindContext<'tcx> { } pub(crate) fn emit(self, product: &mut P) { - let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(super::target_endian(self.codegen_cx.tcx))); + let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(super::target_endian(self.tcx))); self.frame_table.write_eh_frame(&mut eh_frame).unwrap(); if !eh_frame.0.writer.slice().is_empty() { @@ -74,7 +74,7 @@ impl<'tcx> UnwindContext<'tcx> { self, jit_module: &mut Module, ) -> Option { - let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(super::target_endian(self.codegen_cx.tcx))); + let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(super::target_endian(self.tcx))); self.frame_table.write_eh_frame(&mut eh_frame).unwrap(); if eh_frame.0.writer.slice().is_empty() { diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 797039d0fd2..1d81efb880d 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -33,14 +33,14 @@ fn codegen_mono_items<'tcx>( cx: &mut crate::CodegenCx<'tcx, impl Backend + 'static>, mono_items: Vec<(MonoItem<'tcx>, (RLinkage, Visibility))>, ) { - cx.codegen_cx.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(cx.codegen_cx.tcx, cx.codegen_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.codegen_cx.module.declare_function(&name, linkage, &sig).unwrap(); + cx.module.declare_function(&name, linkage, &sig).unwrap(); } MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {} } @@ -58,7 +58,7 @@ fn trans_mono_item<'tcx, B: Backend + 'static>( mono_item: MonoItem<'tcx>, linkage: Linkage, ) { - let tcx = cx.codegen_cx.tcx; + let tcx = cx.tcx; match mono_item { MonoItem::Fn(inst) => { let _inst_guard = @@ -85,7 +85,7 @@ fn trans_mono_item<'tcx, B: Backend + 'static>( } }); - cx.codegen_cx.tcx.sess.time("codegen fn", || crate::base::trans_fn(cx, inst, linkage)); + cx.tcx.sess.time("codegen fn", || crate::base::trans_fn(cx, inst, linkage)); } MonoItem::Static(def_id) => { crate::constant::codegen_static(&mut cx.constants_cx, def_id); diff --git a/src/lib.rs b/src/lib.rs index b9acc27efdf..6394937605a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -160,8 +160,8 @@ impl<'tcx, B: Backend + 'static> CodegenCx<'tcx, B> { } fn finalize(mut self) -> (Module, String, Option>, UnwindContext<'tcx>) { - self.constants_cx.finalize(self.codegen_cx.tcx, &mut self.codegen_cx.module); - (self.codegen_cx.module, self.global_asm, self.debug_context, self.unwind_context) + self.constants_cx.finalize(self.tcx, &mut self.module); + (self.module, self.global_asm, self.debug_context, self.unwind_context) } } diff --git a/src/vtable.rs b/src/vtable.rs index 6a179b3dfeb..f4d3a303bd3 100644 --- a/src/vtable.rs +++ b/src/vtable.rs @@ -93,7 +93,7 @@ fn build_vtable<'tcx>( let usize_size = fx.layout_of(fx.codegen_cx.tcx.types.usize).size.bytes() as usize; let drop_in_place_fn = - import_function(tcx, fx.codegen_cx.module, Instance::resolve_drop_in_place(tcx, layout.ty).polymorphize(fx.codegen_cx.tcx)); + import_function(tcx, &mut fx.codegen_cx.module, Instance::resolve_drop_in_place(tcx, layout.ty).polymorphize(fx.codegen_cx.tcx)); let mut components: Vec<_> = vec![Some(drop_in_place_fn), None, None]; @@ -108,7 +108,7 @@ fn build_vtable<'tcx>( opt_mth.map_or(None, |(def_id, substs)| { Some(import_function( tcx, - fx.codegen_cx.module, + &mut fx.codegen_cx.module, Instance::resolve_for_vtable(tcx, ParamEnv::reveal_all(), def_id, substs).unwrap().polymorphize(fx.codegen_cx.tcx), )) })