Remove CodegenCx::finalize and pass Module separately from CodegenCx

This commit is contained in:
bjorn3 2021-04-08 22:01:39 +02:00
parent 3e2bdb94ec
commit d9e9fedfe5
13 changed files with 102 additions and 111 deletions

View File

@ -71,8 +71,8 @@ pub(crate) fn import_function<'tcx>(
impl<'tcx> FunctionCx<'_, '_, 'tcx> { impl<'tcx> FunctionCx<'_, '_, 'tcx> {
/// Instance must be monomorphized /// Instance must be monomorphized
pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef { pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
let func_id = import_function(self.tcx, self.cx.module, inst); let func_id = import_function(self.tcx, self.module, inst);
let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func); let func_ref = self.module.declare_func_in_func(func_id, &mut self.bcx.func);
if self.clif_comments.enabled() { if self.clif_comments.enabled() {
self.add_comment(func_ref, format!("{:?}", inst)); self.add_comment(func_ref, format!("{:?}", inst));
@ -89,8 +89,8 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
args: &[Value], args: &[Value],
) -> &[Value] { ) -> &[Value] {
let sig = Signature { params, returns, call_conv: CallConv::triple_default(self.triple()) }; let sig = Signature { params, returns, call_conv: CallConv::triple_default(self.triple()) };
let func_id = self.cx.module.declare_function(&name, Linkage::Import, &sig).unwrap(); let func_id = self.module.declare_function(&name, Linkage::Import, &sig).unwrap();
let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func); let func_ref = self.module.declare_func_in_func(func_id, &mut self.bcx.func);
let call_inst = self.bcx.ins().call(func_ref, args); let call_inst = self.bcx.ins().call(func_ref, args);
if self.clif_comments.enabled() { if self.clif_comments.enabled() {
self.add_comment(call_inst, format!("easy_call {}", name)); self.add_comment(call_inst, format!("easy_call {}", name));

View File

@ -9,7 +9,11 @@ use rustc_target::abi::call::FnAbi;
use crate::constant::ConstantCx; use crate::constant::ConstantCx;
use crate::prelude::*; use crate::prelude::*;
pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: Instance<'tcx>) { pub(crate) fn codegen_fn<'tcx>(
cx: &mut crate::CodegenCx<'tcx>,
module: &mut dyn Module,
instance: Instance<'tcx>,
) {
let tcx = cx.tcx; let tcx = cx.tcx;
let _inst_guard = let _inst_guard =
@ -20,8 +24,8 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
// Declare function // Declare function
let symbol_name = tcx.symbol_name(instance); let symbol_name = tcx.symbol_name(instance);
let sig = get_function_sig(tcx, cx.module.isa().triple(), instance); let sig = get_function_sig(tcx, module.isa().triple(), instance);
let func_id = cx.module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap(); let func_id = module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap();
cx.cached_context.clear(); cx.cached_context.clear();
@ -40,11 +44,12 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
(0..mir.basic_blocks().len()).map(|_| bcx.create_block()).collect(); (0..mir.basic_blocks().len()).map(|_| bcx.create_block()).collect();
// Make FunctionCx // Make FunctionCx
let pointer_type = cx.module.target_config().pointer_type(); let pointer_type = module.target_config().pointer_type();
let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance); let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance);
let mut fx = FunctionCx { let mut fx = FunctionCx {
cx, cx,
module,
tcx, tcx,
pointer_type, pointer_type,
vtables: FxHashMap::default(), vtables: FxHashMap::default(),
@ -94,7 +99,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
let source_info_set = fx.source_info_set; let source_info_set = fx.source_info_set;
let local_map = fx.local_map; let local_map = fx.local_map;
fx.constants_cx.finalize(fx.tcx, &mut *fx.cx.module); fx.constants_cx.finalize(fx.tcx, &mut *fx.module);
// Store function in context // Store function in context
let context = &mut cx.cached_context; let context = &mut cx.cached_context;
@ -114,8 +119,8 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
// instruction, which doesn't have an encoding. // instruction, which doesn't have an encoding.
context.compute_cfg(); context.compute_cfg();
context.compute_domtree(); context.compute_domtree();
context.eliminate_unreachable_code(cx.module.isa()).unwrap(); context.eliminate_unreachable_code(module.isa()).unwrap();
context.dce(cx.module.isa()).unwrap(); context.dce(module.isa()).unwrap();
// Some Cranelift optimizations expect the domtree to not yet be computed and as such don't // Some Cranelift optimizations expect the domtree to not yet be computed and as such don't
// invalidate it when it would change. // invalidate it when it would change.
context.domtree.clear(); context.domtree.clear();
@ -123,7 +128,6 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
context.want_disasm = crate::pretty_clif::should_write_ir(tcx); context.want_disasm = crate::pretty_clif::should_write_ir(tcx);
// Define function // Define function
let module = &mut cx.module;
tcx.sess.time("define function", || { tcx.sess.time("define function", || {
module module
.define_function(func_id, context, &mut NullTrapSink {}, &mut NullStackMapSink {}) .define_function(func_id, context, &mut NullTrapSink {}, &mut NullStackMapSink {})
@ -134,7 +138,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
crate::pretty_clif::write_clif_file( crate::pretty_clif::write_clif_file(
tcx, tcx,
"opt", "opt",
Some(cx.module.isa()), Some(module.isa()),
instance, instance,
&context, &context,
&clif_comments, &clif_comments,
@ -149,7 +153,7 @@ pub(crate) fn codegen_fn<'tcx>(cx: &mut crate::CodegenCx<'_, 'tcx>, instance: In
} }
// Define debuginfo for function // Define debuginfo for function
let isa = cx.module.isa(); let isa = module.isa();
let debug_context = &mut cx.debug_context; let debug_context = &mut cx.debug_context;
let unwind_context = &mut cx.unwind_context; let unwind_context = &mut cx.unwind_context;
tcx.sess.time("generate debug info", || { tcx.sess.time("generate debug info", || {
@ -654,7 +658,7 @@ fn codegen_stmt<'tcx>(
// FIXME use emit_small_memset where possible // FIXME use emit_small_memset where possible
let addr = lval.to_ptr().get_addr(fx); let addr = lval.to_ptr().get_addr(fx);
let val = operand.load_scalar(fx); let val = operand.load_scalar(fx);
fx.bcx.call_memset(fx.cx.module.target_config(), addr, val, times); fx.bcx.call_memset(fx.module.target_config(), addr, val, times);
} else { } else {
let loop_block = fx.bcx.create_block(); let loop_block = fx.bcx.create_block();
let loop_block2 = fx.bcx.create_block(); let loop_block2 = fx.bcx.create_block();
@ -834,7 +838,7 @@ fn codegen_stmt<'tcx>(
let elem_size: u64 = pointee.size.bytes(); let elem_size: u64 = pointee.size.bytes();
let bytes = let bytes =
if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count }; if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count };
fx.bcx.call_memcpy(fx.cx.module.target_config(), dst, src, bytes); fx.bcx.call_memcpy(fx.module.target_config(), dst, src, bytes);
} }
} }
} }

View File

@ -228,8 +228,9 @@ pub(crate) fn type_sign(ty: Ty<'_>) -> bool {
} }
} }
pub(crate) struct FunctionCx<'m, 'clif, 'tcx> { pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
pub(crate) cx: &'clif mut crate::CodegenCx<'m, 'tcx>, pub(crate) cx: &'clif mut crate::CodegenCx<'tcx>,
pub(crate) module: &'m mut dyn Module,
pub(crate) tcx: TyCtxt<'tcx>, pub(crate) tcx: TyCtxt<'tcx>,
pub(crate) pointer_type: Type, // Cached from module pub(crate) pointer_type: Type, // Cached from module
pub(crate) vtables: FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>, pub(crate) vtables: FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
@ -341,7 +342,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
} }
pub(crate) fn triple(&self) -> &target_lexicon::Triple { pub(crate) fn triple(&self) -> &target_lexicon::Triple {
self.cx.module.isa().triple() self.module.isa().triple()
} }
pub(crate) fn anonymous_str(&mut self, prefix: &str, msg: &str) -> Value { pub(crate) fn anonymous_str(&mut self, prefix: &str, msg: &str) -> Value {
@ -354,15 +355,14 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
let mut data_ctx = DataContext::new(); let mut data_ctx = DataContext::new();
data_ctx.define(msg.as_bytes().to_vec().into_boxed_slice()); data_ctx.define(msg.as_bytes().to_vec().into_boxed_slice());
let msg_id = self let msg_id = self
.cx
.module .module
.declare_data(&format!("__{}_{:08x}", prefix, msg_hash), Linkage::Local, false, false) .declare_data(&format!("__{}_{:08x}", prefix, msg_hash), Linkage::Local, false, false)
.unwrap(); .unwrap();
// Ignore DuplicateDefinition error, as the data will be the same // Ignore DuplicateDefinition error, as the data will be the same
let _ = self.cx.module.define_data(msg_id, &data_ctx); let _ = self.module.define_data(msg_id, &data_ctx);
let local_msg_id = self.cx.module.declare_data_in_func(msg_id, self.bcx.func); let local_msg_id = self.module.declare_data_in_func(msg_id, self.bcx.func);
if self.clif_comments.enabled() { if self.clif_comments.enabled() {
self.add_comment(local_msg_id, msg); self.add_comment(local_msg_id, msg);
} }

View File

@ -13,7 +13,7 @@ use rustc_middle::ty::ConstKind;
use cranelift_codegen::ir::GlobalValueData; use cranelift_codegen::ir::GlobalValueData;
use cranelift_module::*; use cranelift_module::*;
use crate::{prelude::*, CodegenCx}; use crate::prelude::*;
pub(crate) struct ConstantCx { pub(crate) struct ConstantCx {
todo: Vec<TodoItem>, todo: Vec<TodoItem>,
@ -78,10 +78,10 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
all_constants_ok all_constants_ok
} }
pub(crate) fn codegen_static(cx: &mut CodegenCx<'_, '_>, def_id: DefId) { pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) {
let mut constants_cx = ConstantCx::new(); let mut constants_cx = ConstantCx::new();
constants_cx.todo.push(TodoItem::Static(def_id)); constants_cx.todo.push(TodoItem::Static(def_id));
constants_cx.finalize(cx.tcx, &mut *cx.module); constants_cx.finalize(tcx, module);
} }
pub(crate) fn codegen_tls_ref<'tcx>( pub(crate) fn codegen_tls_ref<'tcx>(
@ -89,8 +89,8 @@ pub(crate) fn codegen_tls_ref<'tcx>(
def_id: DefId, def_id: DefId,
layout: TyAndLayout<'tcx>, layout: TyAndLayout<'tcx>,
) -> CValue<'tcx> { ) -> CValue<'tcx> {
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false); let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() { if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("tls {:?}", def_id)); fx.add_comment(local_data_id, format!("tls {:?}", def_id));
} }
@ -103,8 +103,8 @@ fn codegen_static_ref<'tcx>(
def_id: DefId, def_id: DefId,
layout: TyAndLayout<'tcx>, layout: TyAndLayout<'tcx>,
) -> CPlace<'tcx> { ) -> CPlace<'tcx> {
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false); let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() { if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("{:?}", def_id)); fx.add_comment(local_data_id, format!("{:?}", def_id));
} }
@ -191,29 +191,28 @@ pub(crate) fn codegen_const_value<'tcx>(
fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id)); fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
let data_id = data_id_for_alloc_id( let data_id = data_id_for_alloc_id(
&mut fx.constants_cx, &mut fx.constants_cx,
fx.cx.module, fx.module,
ptr.alloc_id, ptr.alloc_id,
alloc.mutability, alloc.mutability,
); );
let local_data_id = let local_data_id =
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() { if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id)); fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id));
} }
fx.bcx.ins().global_value(fx.pointer_type, local_data_id) fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
} }
Some(GlobalAlloc::Function(instance)) => { Some(GlobalAlloc::Function(instance)) => {
let func_id = let func_id = crate::abi::import_function(fx.tcx, fx.module, instance);
crate::abi::import_function(fx.tcx, fx.cx.module, instance);
let local_func_id = let local_func_id =
fx.cx.module.declare_func_in_func(func_id, &mut fx.bcx.func); fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id) fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
} }
Some(GlobalAlloc::Static(def_id)) => { Some(GlobalAlloc::Static(def_id)) => {
assert!(fx.tcx.is_static(def_id)); assert!(fx.tcx.is_static(def_id));
let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false); let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
let local_data_id = let local_data_id =
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() { if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("{:?}", def_id)); fx.add_comment(local_data_id, format!("{:?}", def_id));
} }
@ -255,9 +254,9 @@ fn pointer_for_allocation<'tcx>(
let alloc_id = fx.tcx.create_memory_alloc(alloc); let alloc_id = fx.tcx.create_memory_alloc(alloc);
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id)); fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
let data_id = let data_id =
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.cx.module, alloc_id, alloc.mutability); data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, alloc.mutability);
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() { if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("{:?}", alloc_id)); fx.add_comment(local_data_id, format!("{:?}", alloc_id));
} }

View File

@ -112,16 +112,18 @@ fn module_codegen(
let mut cx = crate::CodegenCx::new( let mut cx = crate::CodegenCx::new(
tcx, tcx,
backend_config.clone(), backend_config.clone(),
&mut module, module.isa(),
tcx.sess.opts.debuginfo != DebugInfo::None, tcx.sess.opts.debuginfo != DebugInfo::None,
); );
super::predefine_mono_items(&mut cx, &mono_items); super::predefine_mono_items(tcx, &mut module, &mono_items);
for (mono_item, _) in mono_items { for (mono_item, _) in mono_items {
match mono_item { match mono_item {
MonoItem::Fn(inst) => { MonoItem::Fn(inst) => {
cx.tcx.sess.time("codegen fn", || crate::base::codegen_fn(&mut cx, inst)); cx.tcx
.sess
.time("codegen fn", || crate::base::codegen_fn(&mut cx, &mut module, inst));
} }
MonoItem::Static(def_id) => crate::constant::codegen_static(&mut cx, def_id), MonoItem::Static(def_id) => crate::constant::codegen_static(tcx, &mut module, def_id),
MonoItem::GlobalAsm(item_id) => { MonoItem::GlobalAsm(item_id) => {
let item = cx.tcx.hir().item(item_id); let item = cx.tcx.hir().item(item_id);
if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind { if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind {
@ -133,9 +135,7 @@ fn module_codegen(
} }
} }
} }
let (global_asm, debug, mut unwind_context) = crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut cx.unwind_context);
tcx.sess.time("finalize CodegenCx", || cx.finalize());
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context);
let codegen_result = emit_module( let codegen_result = emit_module(
tcx, tcx,
@ -143,11 +143,11 @@ fn module_codegen(
cgu.name().as_str().to_string(), cgu.name().as_str().to_string(),
ModuleKind::Regular, ModuleKind::Regular,
module, module,
debug, cx.debug_context,
unwind_context, cx.unwind_context,
); );
codegen_global_asm(tcx, &cgu.name().as_str(), &global_asm); codegen_global_asm(tcx, &cgu.name().as_str(), &cx.global_asm);
codegen_result codegen_result
} }

View File

@ -44,44 +44,44 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
.into_iter() .into_iter()
.collect::<Vec<(_, (_, _))>>(); .collect::<Vec<(_, (_, _))>>();
let mut cx = crate::CodegenCx::new(tcx, backend_config.clone(), &mut jit_module, false); let mut cx = crate::CodegenCx::new(tcx, backend_config.clone(), jit_module.isa(), false);
super::time(tcx, backend_config.display_cg_time, "codegen mono items", || { super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
super::predefine_mono_items(&mut cx, &mono_items); super::predefine_mono_items(tcx, &mut jit_module, &mono_items);
for (mono_item, _) in mono_items { for (mono_item, _) in mono_items {
match mono_item { match mono_item {
MonoItem::Fn(inst) => match backend_config.codegen_mode { MonoItem::Fn(inst) => match backend_config.codegen_mode {
CodegenMode::Aot => unreachable!(), CodegenMode::Aot => unreachable!(),
CodegenMode::Jit => { CodegenMode::Jit => {
cx.tcx.sess.time("codegen fn", || crate::base::codegen_fn(&mut cx, inst)); cx.tcx.sess.time("codegen fn", || {
crate::base::codegen_fn(&mut cx, &mut jit_module, inst)
});
} }
CodegenMode::JitLazy => codegen_shim(&mut cx, inst), CodegenMode::JitLazy => codegen_shim(&mut cx, &mut jit_module, inst),
}, },
MonoItem::Static(def_id) => { MonoItem::Static(def_id) => {
crate::constant::codegen_static(&mut cx, def_id); crate::constant::codegen_static(tcx, &mut jit_module, def_id);
} }
MonoItem::GlobalAsm(item_id) => { MonoItem::GlobalAsm(item_id) => {
let item = cx.tcx.hir().item(item_id); let item = tcx.hir().item(item_id);
tcx.sess.span_fatal(item.span, "Global asm is not supported in JIT mode"); tcx.sess.span_fatal(item.span, "Global asm is not supported in JIT mode");
} }
} }
} }
}); });
let (global_asm, _debug, mut unwind_context) =
tcx.sess.time("finalize CodegenCx", || cx.finalize());
jit_module.finalize_definitions(); jit_module.finalize_definitions();
if !global_asm.is_empty() { if !cx.global_asm.is_empty() {
tcx.sess.fatal("Inline asm is not supported in JIT mode"); tcx.sess.fatal("Inline asm is not supported in JIT mode");
} }
crate::allocator::codegen(tcx, &mut jit_module, &mut unwind_context); crate::allocator::codegen(tcx, &mut jit_module, &mut cx.unwind_context);
tcx.sess.abort_if_errors(); tcx.sess.abort_if_errors();
jit_module.finalize_definitions(); jit_module.finalize_definitions();
let _unwind_register_guard = unsafe { unwind_context.register_jit(&jit_module) }; let _unwind_register_guard = unsafe { cx.unwind_context.register_jit(&jit_module) };
println!( println!(
"Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed" "Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed"
@ -171,13 +171,12 @@ extern "C" fn __clif_jit_fn(instance_ptr: *const Instance<'static>) -> *const u8
let func_id = jit_module.declare_function(&name, Linkage::Export, &sig).unwrap(); let func_id = jit_module.declare_function(&name, Linkage::Export, &sig).unwrap();
jit_module.prepare_for_function_redefine(func_id).unwrap(); jit_module.prepare_for_function_redefine(func_id).unwrap();
let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false); let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module.isa(), false);
tcx.sess.time("codegen fn", || crate::base::codegen_fn(&mut cx, instance)); tcx.sess.time("codegen fn", || crate::base::codegen_fn(&mut cx, jit_module, instance));
let (global_asm, _debug_context, unwind_context) = cx.finalize(); assert!(cx.global_asm.is_empty());
assert!(global_asm.is_empty());
jit_module.finalize_definitions(); jit_module.finalize_definitions();
std::mem::forget(unsafe { unwind_context.register_jit(&jit_module) }); std::mem::forget(unsafe { cx.unwind_context.register_jit(&jit_module) });
jit_module.get_finalized_function(func_id) jit_module.get_finalized_function(func_id)
}) })
}) })
@ -247,24 +246,23 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> {
imported_symbols imported_symbols
} }
fn codegen_shim<'tcx>(cx: &mut CodegenCx<'_, 'tcx>, inst: Instance<'tcx>) { fn codegen_shim<'tcx>(cx: &mut CodegenCx<'tcx>, module: &mut JITModule, inst: Instance<'tcx>) {
let tcx = cx.tcx; let tcx = cx.tcx;
let pointer_type = cx.module.target_config().pointer_type(); let pointer_type = module.target_config().pointer_type();
let name = tcx.symbol_name(inst).name.to_string(); let name = tcx.symbol_name(inst).name.to_string();
let sig = crate::abi::get_function_sig(tcx, cx.module.isa().triple(), inst); let sig = crate::abi::get_function_sig(tcx, module.isa().triple(), inst);
let func_id = cx.module.declare_function(&name, Linkage::Export, &sig).unwrap(); let func_id = module.declare_function(&name, Linkage::Export, &sig).unwrap();
let instance_ptr = Box::into_raw(Box::new(inst)); let instance_ptr = Box::into_raw(Box::new(inst));
let jit_fn = cx let jit_fn = module
.module
.declare_function( .declare_function(
"__clif_jit_fn", "__clif_jit_fn",
Linkage::Import, Linkage::Import,
&Signature { &Signature {
call_conv: cx.module.target_config().default_call_conv, call_conv: module.target_config().default_call_conv,
params: vec![AbiParam::new(pointer_type)], params: vec![AbiParam::new(pointer_type)],
returns: vec![AbiParam::new(pointer_type)], returns: vec![AbiParam::new(pointer_type)],
}, },
@ -278,7 +276,7 @@ fn codegen_shim<'tcx>(cx: &mut CodegenCx<'_, 'tcx>, inst: Instance<'tcx>) {
let mut builder_ctx = FunctionBuilderContext::new(); let mut builder_ctx = FunctionBuilderContext::new();
let mut trampoline_builder = FunctionBuilder::new(trampoline, &mut builder_ctx); let mut trampoline_builder = FunctionBuilder::new(trampoline, &mut builder_ctx);
let jit_fn = cx.module.declare_func_in_func(jit_fn, trampoline_builder.func); let jit_fn = module.declare_func_in_func(jit_fn, trampoline_builder.func);
let sig_ref = trampoline_builder.func.import_signature(sig); let sig_ref = trampoline_builder.func.import_signature(sig);
let entry_block = trampoline_builder.create_block(); let entry_block = trampoline_builder.create_block();
@ -293,7 +291,7 @@ fn codegen_shim<'tcx>(cx: &mut CodegenCx<'_, 'tcx>, inst: Instance<'tcx>) {
let ret_vals = trampoline_builder.func.dfg.inst_results(call_inst).to_vec(); let ret_vals = trampoline_builder.func.dfg.inst_results(call_inst).to_vec();
trampoline_builder.ins().return_(&ret_vals); trampoline_builder.ins().return_(&ret_vals);
cx.module module
.define_function( .define_function(
func_id, func_id,
&mut cx.cached_context, &mut cx.cached_context,

View File

@ -40,24 +40,25 @@ pub(crate) fn codegen_crate(
} }
fn predefine_mono_items<'tcx>( fn predefine_mono_items<'tcx>(
cx: &mut crate::CodegenCx<'_, 'tcx>, tcx: TyCtxt<'tcx>,
module: &mut dyn Module,
mono_items: &[(MonoItem<'tcx>, (RLinkage, Visibility))], mono_items: &[(MonoItem<'tcx>, (RLinkage, Visibility))],
) { ) {
cx.tcx.sess.time("predefine functions", || { tcx.sess.time("predefine functions", || {
let is_compiler_builtins = cx.tcx.is_compiler_builtins(LOCAL_CRATE); let is_compiler_builtins = tcx.is_compiler_builtins(LOCAL_CRATE);
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 = cx.tcx.symbol_name(instance).name.to_string(); let name = tcx.symbol_name(instance).name.to_string();
let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name)); let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name));
let sig = get_function_sig(cx.tcx, cx.module.isa().triple(), instance); let sig = get_function_sig(tcx, module.isa().triple(), instance);
let linkage = crate::linkage::get_clif_linkage( let linkage = crate::linkage::get_clif_linkage(
mono_item, mono_item,
linkage, linkage,
visibility, visibility,
is_compiler_builtins, is_compiler_builtins,
); );
cx.module.declare_function(&name, linkage, &sig).unwrap(); module.declare_function(&name, linkage, &sig).unwrap();
} }
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {} MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {}
} }

View File

@ -201,7 +201,6 @@ fn call_inline_asm<'tcx>(
} }
let inline_asm_func = fx let inline_asm_func = fx
.cx
.module .module
.declare_function( .declare_function(
asm_name, asm_name,
@ -213,7 +212,7 @@ fn call_inline_asm<'tcx>(
}, },
) )
.unwrap(); .unwrap();
let inline_asm_func = fx.cx.module.declare_func_in_func(inline_asm_func, &mut fx.bcx.func); let inline_asm_func = fx.module.declare_func_in_func(inline_asm_func, &mut fx.bcx.func);
if fx.clif_comments.enabled() { if fx.clif_comments.enabled() {
fx.add_comment(inline_asm_func, asm_name); fx.add_comment(inline_asm_func, asm_name);
} }

View File

@ -498,10 +498,10 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
if intrinsic.contains("nonoverlapping") { if intrinsic.contains("nonoverlapping") {
// FIXME emit_small_memcpy // FIXME emit_small_memcpy
fx.bcx.call_memcpy(fx.cx.module.target_config(), dst, src, byte_amount); fx.bcx.call_memcpy(fx.module.target_config(), dst, src, byte_amount);
} else { } else {
// FIXME emit_small_memmove // FIXME emit_small_memmove
fx.bcx.call_memmove(fx.cx.module.target_config(), dst, src, byte_amount); fx.bcx.call_memmove(fx.module.target_config(), dst, src, byte_amount);
} }
}; };
// NOTE: the volatile variants have src and dst swapped // NOTE: the volatile variants have src and dst swapped
@ -517,10 +517,10 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
// FIXME make the copy actually volatile when using emit_small_mem{cpy,move} // FIXME make the copy actually volatile when using emit_small_mem{cpy,move}
if intrinsic.contains("nonoverlapping") { if intrinsic.contains("nonoverlapping") {
// FIXME emit_small_memcpy // FIXME emit_small_memcpy
fx.bcx.call_memcpy(fx.cx.module.target_config(), dst, src, byte_amount); fx.bcx.call_memcpy(fx.module.target_config(), dst, src, byte_amount);
} else { } else {
// FIXME emit_small_memmove // FIXME emit_small_memmove
fx.bcx.call_memmove(fx.cx.module.target_config(), dst, src, byte_amount); fx.bcx.call_memmove(fx.module.target_config(), dst, src, byte_amount);
} }
}; };
size_of_val, <T> (c ptr) { size_of_val, <T> (c ptr) {
@ -670,7 +670,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
let dst_ptr = dst.load_scalar(fx); let dst_ptr = dst.load_scalar(fx);
// FIXME make the memset actually volatile when switching to emit_small_memset // FIXME make the memset actually volatile when switching to emit_small_memset
// FIXME use emit_small_memset // FIXME use emit_small_memset
fx.bcx.call_memset(fx.cx.module.target_config(), dst_ptr, val, count); fx.bcx.call_memset(fx.module.target_config(), dst_ptr, val, count);
}; };
ctlz | ctlz_nonzero, <T> (v arg) { ctlz | ctlz_nonzero, <T> (v arg) {
// FIXME trap on `ctlz_nonzero` with zero arg. // FIXME trap on `ctlz_nonzero` with zero arg.

View File

@ -33,6 +33,7 @@ use rustc_middle::ty::query::Providers;
use rustc_session::config::OutputFilenames; use rustc_session::config::OutputFilenames;
use rustc_session::Session; use rustc_session::Session;
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::settings::{self, Configurable}; use cranelift_codegen::settings::{self, Configurable};
pub use crate::config::*; pub use crate::config::*;
@ -118,42 +119,32 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
} }
} }
struct CodegenCx<'m, 'tcx: 'm> { struct CodegenCx<'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
module: &'m mut dyn Module,
global_asm: String, global_asm: String,
cached_context: Context, cached_context: Context,
debug_context: Option<DebugContext<'tcx>>, debug_context: Option<DebugContext<'tcx>>,
unwind_context: UnwindContext, unwind_context: UnwindContext,
} }
impl<'m, 'tcx> CodegenCx<'m, 'tcx> { impl<'tcx> CodegenCx<'tcx> {
fn new( fn new(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
backend_config: BackendConfig, backend_config: BackendConfig,
module: &'m mut dyn Module, isa: &dyn TargetIsa,
debug_info: bool, debug_info: bool,
) -> Self { ) -> Self {
let unwind_context = UnwindContext::new( let unwind_context =
tcx, UnwindContext::new(tcx, isa, matches!(backend_config.codegen_mode, CodegenMode::Aot));
module.isa(), let debug_context = if debug_info { Some(DebugContext::new(tcx, isa)) } else { None };
matches!(backend_config.codegen_mode, CodegenMode::Aot),
);
let debug_context =
if debug_info { Some(DebugContext::new(tcx, module.isa())) } else { None };
CodegenCx { CodegenCx {
tcx, tcx,
module,
global_asm: String::new(), global_asm: String::new(),
cached_context: Context::new(), cached_context: Context::new(),
debug_context, debug_context,
unwind_context, unwind_context,
} }
} }
fn finalize(self) -> (String, Option<DebugContext<'tcx>>, UnwindContext) {
(self.global_asm, self.debug_context, self.unwind_context)
}
} }
pub struct CraneliftCodegenBackend { pub struct CraneliftCodegenBackend {

View File

@ -4,7 +4,6 @@ use crate::prelude::*;
fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) { fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) {
let puts = fx let puts = fx
.cx
.module .module
.declare_function( .declare_function(
"puts", "puts",
@ -16,7 +15,7 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) {
}, },
) )
.unwrap(); .unwrap();
let puts = fx.cx.module.declare_func_in_func(puts, &mut fx.bcx.func); let puts = fx.module.declare_func_in_func(puts, &mut fx.bcx.func);
if fx.clif_comments.enabled() { if fx.clif_comments.enabled() {
fx.add_comment(puts, "puts"); fx.add_comment(puts, "puts");
} }

View File

@ -554,7 +554,7 @@ impl<'tcx> CPlace<'tcx> {
let src_align = src_layout.align.abi.bytes() as u8; let src_align = src_layout.align.abi.bytes() as u8;
let dst_align = dst_layout.align.abi.bytes() as u8; let dst_align = dst_layout.align.abi.bytes() as u8;
fx.bcx.emit_small_memory_copy( fx.bcx.emit_small_memory_copy(
fx.cx.module.target_config(), fx.module.target_config(),
to_addr, to_addr,
from_addr, from_addr,
size, size,

View File

@ -80,7 +80,7 @@ pub(crate) fn get_vtable<'tcx>(
data_id data_id
}; };
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
fx.bcx.ins().global_value(fx.pointer_type, local_data_id) fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
} }
@ -94,7 +94,7 @@ fn build_vtable<'tcx>(
let drop_in_place_fn = import_function( let drop_in_place_fn = import_function(
tcx, tcx,
fx.cx.module, fx.module,
Instance::resolve_drop_in_place(tcx, layout.ty).polymorphize(fx.tcx), Instance::resolve_drop_in_place(tcx, layout.ty).polymorphize(fx.tcx),
); );
@ -111,7 +111,7 @@ fn build_vtable<'tcx>(
opt_mth.map(|(def_id, substs)| { opt_mth.map(|(def_id, substs)| {
import_function( import_function(
tcx, tcx,
fx.cx.module, fx.module,
Instance::resolve_for_vtable(tcx, ParamEnv::reveal_all(), def_id, substs) Instance::resolve_for_vtable(tcx, ParamEnv::reveal_all(), def_id, substs)
.unwrap() .unwrap()
.polymorphize(fx.tcx), .polymorphize(fx.tcx),
@ -132,16 +132,16 @@ fn build_vtable<'tcx>(
for (i, component) in components.into_iter().enumerate() { for (i, component) in components.into_iter().enumerate() {
if let Some(func_id) = component { if let Some(func_id) = component {
let func_ref = fx.cx.module.declare_func_in_data(func_id, &mut data_ctx); let func_ref = fx.module.declare_func_in_data(func_id, &mut data_ctx);
data_ctx.write_function_addr((i * usize_size) as u32, func_ref); data_ctx.write_function_addr((i * usize_size) as u32, func_ref);
} }
} }
data_ctx.set_align(fx.tcx.data_layout.pointer_align.pref.bytes()); data_ctx.set_align(fx.tcx.data_layout.pointer_align.pref.bytes());
let data_id = fx.cx.module.declare_anonymous_data(false, false).unwrap(); let data_id = fx.module.declare_anonymous_data(false, false).unwrap();
fx.cx.module.define_data(data_id, &data_ctx).unwrap(); fx.module.define_data(data_id, &data_ctx).unwrap();
data_id data_id
} }