Extract create_jit_module function
This commit is contained in:
parent
d4d270d503
commit
eed9aaa268
@ -107,7 +107,6 @@ fn module_codegen(
|
|||||||
|
|
||||||
let isa = crate::build_isa(tcx.sess, &backend_config);
|
let isa = crate::build_isa(tcx.sess, &backend_config);
|
||||||
let mut module = crate::backend::make_module(tcx.sess, isa, cgu_name.as_str().to_string());
|
let mut module = crate::backend::make_module(tcx.sess, isa, cgu_name.as_str().to_string());
|
||||||
assert_eq!(pointer_ty(tcx), module.target_config().pointer_type());
|
|
||||||
|
|
||||||
let mut cx = crate::CodegenCx::new(
|
let mut cx = crate::CodegenCx::new(
|
||||||
tcx,
|
tcx,
|
||||||
|
@ -23,6 +23,33 @@ thread_local! {
|
|||||||
static LAZY_JIT_STATE: RefCell<Option<JitState>> = RefCell::new(None);
|
static LAZY_JIT_STATE: RefCell<Option<JitState>> = RefCell::new(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_jit_module<'tcx>(
|
||||||
|
tcx: TyCtxt<'tcx>,
|
||||||
|
backend_config: &BackendConfig,
|
||||||
|
hotswap: bool,
|
||||||
|
) -> (JITModule, CodegenCx<'tcx>) {
|
||||||
|
let imported_symbols = load_imported_symbols_for_jit(tcx);
|
||||||
|
|
||||||
|
let isa = crate::build_isa(tcx.sess, backend_config);
|
||||||
|
let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
|
||||||
|
jit_builder.hotswap(hotswap);
|
||||||
|
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
|
||||||
|
jit_builder.symbols(imported_symbols);
|
||||||
|
let mut jit_module = JITModule::new(jit_builder);
|
||||||
|
|
||||||
|
let mut cx = crate::CodegenCx::new(tcx, backend_config.clone(), jit_module.isa(), false);
|
||||||
|
|
||||||
|
crate::allocator::codegen(tcx, &mut jit_module, &mut cx.unwind_context);
|
||||||
|
crate::main_shim::maybe_create_entry_wrapper(
|
||||||
|
tcx,
|
||||||
|
&mut jit_module,
|
||||||
|
&mut cx.unwind_context,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
(jit_module, cx)
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
||||||
if !tcx.sess.opts.output_types.should_codegen() {
|
if !tcx.sess.opts.output_types.should_codegen() {
|
||||||
tcx.sess.fatal("JIT mode doesn't work with `cargo check`");
|
tcx.sess.fatal("JIT mode doesn't work with `cargo check`");
|
||||||
@ -32,15 +59,11 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
|||||||
tcx.sess.fatal("can't jit non-executable crate");
|
tcx.sess.fatal("can't jit non-executable crate");
|
||||||
}
|
}
|
||||||
|
|
||||||
let imported_symbols = load_imported_symbols_for_jit(tcx);
|
let (mut jit_module, mut cx) = create_jit_module(
|
||||||
|
tcx,
|
||||||
let isa = crate::build_isa(tcx.sess, &backend_config);
|
&backend_config,
|
||||||
let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
|
matches!(backend_config.codegen_mode, CodegenMode::JitLazy),
|
||||||
jit_builder.hotswap(matches!(backend_config.codegen_mode, CodegenMode::JitLazy));
|
);
|
||||||
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
|
|
||||||
jit_builder.symbols(imported_symbols);
|
|
||||||
let mut jit_module = JITModule::new(jit_builder);
|
|
||||||
assert_eq!(pointer_ty(tcx), jit_module.target_config().pointer_type());
|
|
||||||
|
|
||||||
let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
|
let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
|
||||||
let mono_items = cgus
|
let mono_items = cgus
|
||||||
@ -51,8 +74,6 @@ pub(crate) 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(), 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(tcx, &mut jit_module, &mono_items);
|
super::predefine_mono_items(tcx, &mut jit_module, &mono_items);
|
||||||
for (mono_item, _) in mono_items {
|
for (mono_item, _) in mono_items {
|
||||||
@ -77,20 +98,10 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
jit_module.finalize_definitions();
|
|
||||||
|
|
||||||
if !cx.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 cx.unwind_context);
|
|
||||||
crate::main_shim::maybe_create_entry_wrapper(
|
|
||||||
tcx,
|
|
||||||
&mut jit_module,
|
|
||||||
&mut cx.unwind_context,
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
|
|
||||||
tcx.sess.abort_if_errors();
|
tcx.sess.abort_if_errors();
|
||||||
|
|
||||||
jit_module.finalize_definitions();
|
jit_module.finalize_definitions();
|
||||||
|
@ -134,6 +134,8 @@ impl<'tcx> CodegenCx<'tcx> {
|
|||||||
isa: &dyn TargetIsa,
|
isa: &dyn TargetIsa,
|
||||||
debug_info: bool,
|
debug_info: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
assert_eq!(pointer_ty(tcx), isa.pointer_type());
|
||||||
|
|
||||||
let unwind_context =
|
let unwind_context =
|
||||||
UnwindContext::new(tcx, isa, matches!(backend_config.codegen_mode, CodegenMode::Aot));
|
UnwindContext::new(tcx, isa, matches!(backend_config.codegen_mode, CodegenMode::Aot));
|
||||||
let debug_context = if debug_info { Some(DebugContext::new(tcx, isa)) } else { None };
|
let debug_context = if debug_info { Some(DebugContext::new(tcx, isa)) } else { None };
|
||||||
|
@ -10,7 +10,7 @@ pub(crate) fn maybe_create_entry_wrapper(
|
|||||||
tcx: TyCtxt<'_>,
|
tcx: TyCtxt<'_>,
|
||||||
module: &mut impl Module,
|
module: &mut impl Module,
|
||||||
unwind_context: &mut UnwindContext,
|
unwind_context: &mut UnwindContext,
|
||||||
ignore_lang_start_wrapper: bool,
|
is_jit: bool,
|
||||||
) {
|
) {
|
||||||
let (main_def_id, is_main_fn) = match tcx.entry_fn(LOCAL_CRATE) {
|
let (main_def_id, is_main_fn) = match tcx.entry_fn(LOCAL_CRATE) {
|
||||||
Some((def_id, entry_ty)) => (
|
Some((def_id, entry_ty)) => (
|
||||||
@ -24,18 +24,11 @@ pub(crate) fn maybe_create_entry_wrapper(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let instance = Instance::mono(tcx, main_def_id).polymorphize(tcx);
|
let instance = Instance::mono(tcx, main_def_id).polymorphize(tcx);
|
||||||
if module.get_name(&*tcx.symbol_name(instance).name).is_none() {
|
if !is_jit && module.get_name(&*tcx.symbol_name(instance).name).is_none() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
create_entry_fn(
|
create_entry_fn(tcx, module, unwind_context, main_def_id, is_jit, is_main_fn);
|
||||||
tcx,
|
|
||||||
module,
|
|
||||||
unwind_context,
|
|
||||||
main_def_id,
|
|
||||||
ignore_lang_start_wrapper,
|
|
||||||
is_main_fn,
|
|
||||||
);
|
|
||||||
|
|
||||||
fn create_entry_fn(
|
fn create_entry_fn(
|
||||||
tcx: TyCtxt<'_>,
|
tcx: TyCtxt<'_>,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user