Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit eed9aaa

Browse files
committed
Extract create_jit_module function
1 parent d4d270d commit eed9aaa

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

src/driver/aot.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ fn module_codegen(
107107

108108
let isa = crate::build_isa(tcx.sess, &backend_config);
109109
let mut module = crate::backend::make_module(tcx.sess, isa, cgu_name.as_str().to_string());
110-
assert_eq!(pointer_ty(tcx), module.target_config().pointer_type());
111110

112111
let mut cx = crate::CodegenCx::new(
113112
tcx,

src/driver/jit.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,33 @@ thread_local! {
2323
static LAZY_JIT_STATE: RefCell<Option<JitState>> = RefCell::new(None);
2424
}
2525

26+
fn create_jit_module<'tcx>(
27+
tcx: TyCtxt<'tcx>,
28+
backend_config: &BackendConfig,
29+
hotswap: bool,
30+
) -> (JITModule, CodegenCx<'tcx>) {
31+
let imported_symbols = load_imported_symbols_for_jit(tcx);
32+
33+
let isa = crate::build_isa(tcx.sess, backend_config);
34+
let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
35+
jit_builder.hotswap(hotswap);
36+
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
37+
jit_builder.symbols(imported_symbols);
38+
let mut jit_module = JITModule::new(jit_builder);
39+
40+
let mut cx = crate::CodegenCx::new(tcx, backend_config.clone(), jit_module.isa(), false);
41+
42+
crate::allocator::codegen(tcx, &mut jit_module, &mut cx.unwind_context);
43+
crate::main_shim::maybe_create_entry_wrapper(
44+
tcx,
45+
&mut jit_module,
46+
&mut cx.unwind_context,
47+
true,
48+
);
49+
50+
(jit_module, cx)
51+
}
52+
2653
pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
2754
if !tcx.sess.opts.output_types.should_codegen() {
2855
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) -> ! {
3259
tcx.sess.fatal("can't jit non-executable crate");
3360
}
3461

35-
let imported_symbols = load_imported_symbols_for_jit(tcx);
36-
37-
let isa = crate::build_isa(tcx.sess, &backend_config);
38-
let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
39-
jit_builder.hotswap(matches!(backend_config.codegen_mode, CodegenMode::JitLazy));
40-
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
41-
jit_builder.symbols(imported_symbols);
42-
let mut jit_module = JITModule::new(jit_builder);
43-
assert_eq!(pointer_ty(tcx), jit_module.target_config().pointer_type());
62+
let (mut jit_module, mut cx) = create_jit_module(
63+
tcx,
64+
&backend_config,
65+
matches!(backend_config.codegen_mode, CodegenMode::JitLazy),
66+
);
4467

4568
let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
4669
let mono_items = cgus
@@ -51,8 +74,6 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
5174
.into_iter()
5275
.collect::<Vec<(_, (_, _))>>();
5376

54-
let mut cx = crate::CodegenCx::new(tcx, backend_config.clone(), jit_module.isa(), false);
55-
5677
super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
5778
super::predefine_mono_items(tcx, &mut jit_module, &mono_items);
5879
for (mono_item, _) in mono_items {
@@ -77,20 +98,10 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
7798
}
7899
});
79100

80-
jit_module.finalize_definitions();
81-
82101
if !cx.global_asm.is_empty() {
83102
tcx.sess.fatal("Inline asm is not supported in JIT mode");
84103
}
85104

86-
crate::allocator::codegen(tcx, &mut jit_module, &mut cx.unwind_context);
87-
crate::main_shim::maybe_create_entry_wrapper(
88-
tcx,
89-
&mut jit_module,
90-
&mut cx.unwind_context,
91-
true,
92-
);
93-
94105
tcx.sess.abort_if_errors();
95106

96107
jit_module.finalize_definitions();

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ impl<'tcx> CodegenCx<'tcx> {
134134
isa: &dyn TargetIsa,
135135
debug_info: bool,
136136
) -> Self {
137+
assert_eq!(pointer_ty(tcx), isa.pointer_type());
138+
137139
let unwind_context =
138140
UnwindContext::new(tcx, isa, matches!(backend_config.codegen_mode, CodegenMode::Aot));
139141
let debug_context = if debug_info { Some(DebugContext::new(tcx, isa)) } else { None };

src/main_shim.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) fn maybe_create_entry_wrapper(
1010
tcx: TyCtxt<'_>,
1111
module: &mut impl Module,
1212
unwind_context: &mut UnwindContext,
13-
ignore_lang_start_wrapper: bool,
13+
is_jit: bool,
1414
) {
1515
let (main_def_id, is_main_fn) = match tcx.entry_fn(LOCAL_CRATE) {
1616
Some((def_id, entry_ty)) => (
@@ -24,18 +24,11 @@ pub(crate) fn maybe_create_entry_wrapper(
2424
};
2525

2626
let instance = Instance::mono(tcx, main_def_id).polymorphize(tcx);
27-
if module.get_name(&*tcx.symbol_name(instance).name).is_none() {
27+
if !is_jit && module.get_name(&*tcx.symbol_name(instance).name).is_none() {
2828
return;
2929
}
3030

31-
create_entry_fn(
32-
tcx,
33-
module,
34-
unwind_context,
35-
main_def_id,
36-
ignore_lang_start_wrapper,
37-
is_main_fn,
38-
);
31+
create_entry_fn(tcx, module, unwind_context, main_def_id, is_jit, is_main_fn);
3932

4033
fn create_entry_fn(
4134
tcx: TyCtxt<'_>,

0 commit comments

Comments
 (0)