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

Commit 8a336a2

Browse files
committed
Move cached_context out of CodegenCx
1 parent a10da0f commit 8a336a2

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

src/base.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,23 @@ struct CodegenedFunction<'tcx> {
2525

2626
pub(crate) fn codegen_and_compile_fn<'tcx>(
2727
cx: &mut crate::CodegenCx<'tcx>,
28+
cached_context: &mut Context,
2829
module: &mut dyn Module,
2930
instance: Instance<'tcx>,
3031
) {
3132
let tcx = cx.tcx;
3233
let _inst_guard =
3334
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
3435

35-
let codegened_func = codegen_fn(cx, module, instance);
36+
let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
37+
let codegened_func = codegen_fn(cx, cached_func, module, instance);
3638

37-
compile_fn(cx, module, codegened_func);
39+
compile_fn(cx, cached_context, module, codegened_func);
3840
}
3941

4042
fn codegen_fn<'tcx>(
4143
cx: &mut crate::CodegenCx<'tcx>,
44+
cached_func: Function,
4245
module: &mut dyn Module,
4346
instance: Instance<'tcx>,
4447
) -> CodegenedFunction<'tcx> {
@@ -61,11 +64,10 @@ fn codegen_fn<'tcx>(
6164
let sig = get_function_sig(tcx, module.isa().triple(), instance);
6265
let func_id = module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap();
6366

64-
cx.cached_context.clear();
65-
6667
// Make the FunctionBuilder
6768
let mut func_ctx = FunctionBuilderContext::new();
68-
let mut func = std::mem::replace(&mut cx.cached_context.func, Function::new());
69+
let mut func = cached_func;
70+
func.clear();
6971
func.name = ExternalName::user(0, func_id.as_u32());
7072
func.signature = sig;
7173
func.collect_debug_info();
@@ -140,6 +142,7 @@ fn codegen_fn<'tcx>(
140142

141143
fn compile_fn<'tcx>(
142144
cx: &mut crate::CodegenCx<'tcx>,
145+
cached_context: &mut Context,
143146
module: &mut dyn Module,
144147
codegened_func: CodegenedFunction<'tcx>,
145148
) {
@@ -148,7 +151,7 @@ fn compile_fn<'tcx>(
148151
let mut clif_comments = codegened_func.clif_comments;
149152

150153
// Store function in context
151-
let context = &mut cx.cached_context;
154+
let context = cached_context;
152155
context.clear();
153156
context.func = codegened_func.func;
154157

src/driver/aot.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,17 @@ fn module_codegen(
128128
cgu_name,
129129
);
130130
super::predefine_mono_items(tcx, &mut module, &mono_items);
131+
let mut cached_context = Context::new();
131132
for (mono_item, _) in mono_items {
132133
match mono_item {
133134
MonoItem::Fn(inst) => {
134135
cx.tcx.sess.time("codegen fn", || {
135-
crate::base::codegen_and_compile_fn(&mut cx, &mut module, inst)
136+
crate::base::codegen_and_compile_fn(
137+
&mut cx,
138+
&mut cached_context,
139+
&mut module,
140+
inst,
141+
)
136142
});
137143
}
138144
MonoItem::Static(def_id) => crate::constant::codegen_static(tcx, &mut module, def_id),

src/driver/jit.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
111111
&backend_config,
112112
matches!(backend_config.codegen_mode, CodegenMode::JitLazy),
113113
);
114+
let mut cached_context = Context::new();
114115

115116
let (_, cgus) = tcx.collect_and_partition_mono_items(());
116117
let mono_items = cgus
@@ -129,10 +130,17 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
129130
CodegenMode::Aot => unreachable!(),
130131
CodegenMode::Jit => {
131132
cx.tcx.sess.time("codegen fn", || {
132-
crate::base::codegen_and_compile_fn(&mut cx, &mut jit_module, inst)
133+
crate::base::codegen_and_compile_fn(
134+
&mut cx,
135+
&mut cached_context,
136+
&mut jit_module,
137+
inst,
138+
)
133139
});
134140
}
135-
CodegenMode::JitLazy => codegen_shim(&mut cx, &mut jit_module, inst),
141+
CodegenMode::JitLazy => {
142+
codegen_shim(&mut cx, &mut cached_context, &mut jit_module, inst)
143+
}
136144
},
137145
MonoItem::Static(def_id) => {
138146
crate::constant::codegen_static(tcx, &mut jit_module, def_id);
@@ -260,7 +268,12 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) ->
260268
Symbol::intern("dummy_cgu_name"),
261269
);
262270
tcx.sess.time("codegen fn", || {
263-
crate::base::codegen_and_compile_fn(&mut cx, jit_module, instance)
271+
crate::base::codegen_and_compile_fn(
272+
&mut cx,
273+
&mut Context::new(),
274+
jit_module,
275+
instance,
276+
)
264277
});
265278

266279
assert!(cx.global_asm.is_empty());
@@ -336,7 +349,12 @@ fn load_imported_symbols_for_jit(
336349
imported_symbols
337350
}
338351

339-
fn codegen_shim<'tcx>(cx: &mut CodegenCx<'tcx>, module: &mut JITModule, inst: Instance<'tcx>) {
352+
fn codegen_shim<'tcx>(
353+
cx: &mut CodegenCx<'tcx>,
354+
cached_context: &mut Context,
355+
module: &mut JITModule,
356+
inst: Instance<'tcx>,
357+
) {
340358
let tcx = cx.tcx;
341359

342360
let pointer_type = module.target_config().pointer_type();
@@ -359,8 +377,9 @@ fn codegen_shim<'tcx>(cx: &mut CodegenCx<'tcx>, module: &mut JITModule, inst: In
359377
)
360378
.unwrap();
361379

362-
cx.cached_context.clear();
363-
let trampoline = &mut cx.cached_context.func;
380+
let context = cached_context;
381+
context.clear();
382+
let trampoline = &mut context.func;
364383
trampoline.signature = sig.clone();
365384

366385
let mut builder_ctx = FunctionBuilderContext::new();
@@ -383,5 +402,5 @@ fn codegen_shim<'tcx>(cx: &mut CodegenCx<'tcx>, module: &mut JITModule, inst: In
383402
let ret_vals = trampoline_builder.func.dfg.inst_results(call_inst).to_vec();
384403
trampoline_builder.ins().return_(&ret_vals);
385404

386-
module.define_function(func_id, &mut cx.cached_context).unwrap();
405+
module.define_function(func_id, context).unwrap();
387406
}

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ struct CodegenCx<'tcx> {
123123
tcx: TyCtxt<'tcx>,
124124
global_asm: String,
125125
inline_asm_index: Cell<usize>,
126-
cached_context: Context,
127126
debug_context: Option<DebugContext<'tcx>>,
128127
unwind_context: UnwindContext,
129128
cgu_name: Symbol,
@@ -150,7 +149,6 @@ impl<'tcx> CodegenCx<'tcx> {
150149
tcx,
151150
global_asm: String::new(),
152151
inline_asm_index: Cell::new(0),
153-
cached_context: Context::new(),
154152
debug_context,
155153
unwind_context,
156154
cgu_name,

0 commit comments

Comments
 (0)