FunctionCx: WIP: Remove more common fields from CodegenCx
MODIFY
src/abi/mod.rs
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);

MODIFY
src/base.rs
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<BasicBlock, Block> = (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", || {

MODIFY
src/common.rs
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<Block>,
pub(crate) clif_comments: crate::pretty_clif::CommentWriter,
- pub(crate) vtables: &'clif mut FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
-
pub(crate) source_info_set: indexmap::IndexSet<SourceInfo>,
/// This should only be accessed by `CPlace::new_var`.

MODIFY
src/constant.rs
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)]

MODIFY
src/debuginfo/line_info.rs
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 @@ pub(super) fn create_debug_lines(
function_span: Span,
source_info_set: &indexmap::IndexSet<SourceInfo>,
) -> CodeOffset {
- let tcx = self.codegen_cx.tcx;
+ let tcx = self.tcx;
let line_program = &mut self.dwarf.unit.line_program;
let func = &context.func;

MODIFY
src/debuginfo/mod.rs
src/debuginfo/mod.rs
@@ -165,7 +165,7 @@ fn dwarf_ty(&mut self, ty: Ty<'tcx>) -> UnitEntryId {
};
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 @@ fn dwarf_ty(&mut self, ty: Ty<'tcx>) -> UnitEntryId {
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 @@ pub(crate) fn define_function(
local_map: FxHashMap<mir::Local, CPlace<'tcx>>,
) {
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 @@ pub(crate) fn define_function(
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,

MODIFY
src/debuginfo/unwind.rs
src/debuginfo/unwind.rs
@@ -54,7 +54,7 @@ pub(crate) fn add_function(&mut self, func_id: FuncId, context: &Context, isa: &
}
pub(crate) fn emit<P: WriteDebugInfo>(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 @@ pub(crate) unsafe fn register_jit(
self,
jit_module: &mut Module<cranelift_simplejit::SimpleJITBackend>,
) -> Option<UnwindRegistry> {
- 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() {

MODIFY
src/driver/mod.rs
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);

MODIFY
src/lib.rs
src/lib.rs
@@ -160,8 +160,8 @@ fn new(
}
fn finalize(mut self) -> (Module<B>, String, Option<DebugContext<'tcx>>, 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)
}
}

MODIFY
src/vtable.rs
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),
))
})

FunctionCx: WIP: Remove more common fields from CodegenCx