@@ -23,6 +23,33 @@ thread_local! {
23
23
static LAZY_JIT_STATE : RefCell <Option <JitState >> = RefCell :: new( None ) ;
24
24
}
25
25
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
+
26
53
pub ( crate ) fn run_jit ( tcx : TyCtxt < ' _ > , backend_config : BackendConfig ) -> ! {
27
54
if !tcx. sess . opts . output_types . should_codegen ( ) {
28
55
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) -> ! {
32
59
tcx. sess . fatal ( "can't jit non-executable crate" ) ;
33
60
}
34
61
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
+ ) ;
44
67
45
68
let ( _, cgus) = tcx. collect_and_partition_mono_items ( LOCAL_CRATE ) ;
46
69
let mono_items = cgus
@@ -51,8 +74,6 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
51
74
. into_iter ( )
52
75
. collect :: < Vec < ( _ , ( _ , _ ) ) > > ( ) ;
53
76
54
- let mut cx = crate :: CodegenCx :: new ( tcx, backend_config. clone ( ) , jit_module. isa ( ) , false ) ;
55
-
56
77
super :: time ( tcx, backend_config. display_cg_time , "codegen mono items" , || {
57
78
super :: predefine_mono_items ( tcx, & mut jit_module, & mono_items) ;
58
79
for ( mono_item, _) in mono_items {
@@ -77,20 +98,10 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
77
98
}
78
99
} ) ;
79
100
80
- jit_module. finalize_definitions ( ) ;
81
-
82
101
if !cx. global_asm . is_empty ( ) {
83
102
tcx. sess . fatal ( "Inline asm is not supported in JIT mode" ) ;
84
103
}
85
104
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
-
94
105
tcx. sess . abort_if_errors ( ) ;
95
106
96
107
jit_module. finalize_definitions ( ) ;
0 commit comments