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

Commit a10da0f

Browse files
committed
Split non-compile parts of codegen_fn out into a separate function
The new codegen_and_compile_fn function only calls codegen_fn and then compile_fn. This makes it possible for both parts to be called separately by the driver.
1 parent a7443a6 commit a10da0f

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

src/base.rs

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,47 @@ use rustc_index::vec::IndexVec;
55
use rustc_middle::ty::adjustment::PointerCast;
66
use rustc_middle::ty::layout::FnAbiOf;
77
use rustc_middle::ty::print::with_no_trimmed_paths;
8+
use rustc_middle::ty::SymbolName;
89

910
use indexmap::IndexSet;
1011

1112
use crate::constant::ConstantCx;
1213
use crate::prelude::*;
1314
use crate::pretty_clif::CommentWriter;
1415

15-
pub(crate) fn codegen_fn<'tcx>(
16+
struct CodegenedFunction<'tcx> {
17+
instance: Instance<'tcx>,
18+
symbol_name: SymbolName<'tcx>,
19+
func_id: FuncId,
20+
func: Function,
21+
clif_comments: CommentWriter,
22+
source_info_set: IndexSet<SourceInfo>,
23+
local_map: IndexVec<mir::Local, CPlace<'tcx>>,
24+
}
25+
26+
pub(crate) fn codegen_and_compile_fn<'tcx>(
1627
cx: &mut crate::CodegenCx<'tcx>,
1728
module: &mut dyn Module,
1829
instance: Instance<'tcx>,
1930
) {
2031
let tcx = cx.tcx;
21-
2232
let _inst_guard =
2333
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
34+
35+
let codegened_func = codegen_fn(cx, module, instance);
36+
37+
compile_fn(cx, module, codegened_func);
38+
}
39+
40+
fn codegen_fn<'tcx>(
41+
cx: &mut crate::CodegenCx<'tcx>,
42+
module: &mut dyn Module,
43+
instance: Instance<'tcx>,
44+
) -> CodegenedFunction<'tcx> {
2445
debug_assert!(!instance.substs.needs_infer());
2546

47+
let tcx = cx.tcx;
48+
2649
let mir = tcx.instance_mir(instance.def);
2750
let _mir_guard = crate::PrintOnPanic(|| {
2851
let mut buf = Vec::new();
@@ -104,36 +127,30 @@ pub(crate) fn codegen_fn<'tcx>(
104127
// Verify function
105128
verify_func(tcx, &clif_comments, &func);
106129

107-
compile_fn(
108-
cx,
109-
module,
130+
CodegenedFunction {
110131
instance,
111-
symbol_name.name,
132+
symbol_name,
112133
func_id,
113134
func,
114135
clif_comments,
115136
source_info_set,
116137
local_map,
117-
);
138+
}
118139
}
119140

120141
fn compile_fn<'tcx>(
121142
cx: &mut crate::CodegenCx<'tcx>,
122143
module: &mut dyn Module,
123-
instance: Instance<'tcx>,
124-
symbol_name: &str,
125-
func_id: FuncId,
126-
func: Function,
127-
mut clif_comments: CommentWriter,
128-
source_info_set: IndexSet<SourceInfo>,
129-
local_map: IndexVec<mir::Local, CPlace<'tcx>>,
144+
codegened_func: CodegenedFunction<'tcx>,
130145
) {
131146
let tcx = cx.tcx;
132147

148+
let mut clif_comments = codegened_func.clif_comments;
149+
133150
// Store function in context
134151
let context = &mut cx.cached_context;
135152
context.clear();
136-
context.func = func;
153+
context.func = codegened_func.func;
137154

138155
// If the return block is not reachable, then the SSA builder may have inserted an `iconst.i128`
139156
// instruction, which doesn't have an encoding.
@@ -150,7 +167,7 @@ fn compile_fn<'tcx>(
150167
crate::optimize::optimize_function(
151168
tcx,
152169
module.isa(),
153-
instance,
170+
codegened_func.instance,
154171
context,
155172
&mut clif_comments,
156173
);
@@ -186,23 +203,23 @@ fn compile_fn<'tcx>(
186203
// Define function
187204
tcx.sess.time("define function", || {
188205
context.want_disasm = crate::pretty_clif::should_write_ir(tcx);
189-
module.define_function(func_id, context).unwrap();
206+
module.define_function(codegened_func.func_id, context).unwrap();
190207
});
191208

192209
// Write optimized function to file for debugging
193210
crate::pretty_clif::write_clif_file(
194211
tcx,
195212
"opt",
196213
module.isa(),
197-
instance,
214+
codegened_func.instance,
198215
&context.func,
199216
&clif_comments,
200217
);
201218

202219
if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
203220
crate::pretty_clif::write_ir_file(
204221
tcx,
205-
|| format!("{}.vcode", tcx.symbol_name(instance).name),
222+
|| format!("{}.vcode", tcx.symbol_name(codegened_func.instance).name),
206223
|file| file.write_all(disasm.as_bytes()),
207224
)
208225
}
@@ -214,16 +231,16 @@ fn compile_fn<'tcx>(
214231
tcx.sess.time("generate debug info", || {
215232
if let Some(debug_context) = debug_context {
216233
debug_context.define_function(
217-
instance,
218-
func_id,
219-
symbol_name,
234+
codegened_func.instance,
235+
codegened_func.func_id,
236+
codegened_func.symbol_name.name,
220237
isa,
221238
context,
222-
&source_info_set,
223-
local_map,
239+
&codegened_func.source_info_set,
240+
codegened_func.local_map,
224241
);
225242
}
226-
unwind_context.add_function(func_id, &context, isa);
243+
unwind_context.add_function(codegened_func.func_id, &context, isa);
227244
});
228245
}
229246

src/driver/aot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ fn module_codegen(
131131
for (mono_item, _) in mono_items {
132132
match mono_item {
133133
MonoItem::Fn(inst) => {
134-
cx.tcx
135-
.sess
136-
.time("codegen fn", || crate::base::codegen_fn(&mut cx, &mut module, inst));
134+
cx.tcx.sess.time("codegen fn", || {
135+
crate::base::codegen_and_compile_fn(&mut cx, &mut module, inst)
136+
});
137137
}
138138
MonoItem::Static(def_id) => crate::constant::codegen_static(tcx, &mut module, def_id),
139139
MonoItem::GlobalAsm(item_id) => {

src/driver/jit.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
129129
CodegenMode::Aot => unreachable!(),
130130
CodegenMode::Jit => {
131131
cx.tcx.sess.time("codegen fn", || {
132-
crate::base::codegen_fn(&mut cx, &mut jit_module, inst)
132+
crate::base::codegen_and_compile_fn(&mut cx, &mut jit_module, inst)
133133
});
134134
}
135135
CodegenMode::JitLazy => codegen_shim(&mut cx, &mut jit_module, inst),
@@ -259,7 +259,9 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) ->
259259
false,
260260
Symbol::intern("dummy_cgu_name"),
261261
);
262-
tcx.sess.time("codegen fn", || crate::base::codegen_fn(&mut cx, jit_module, instance));
262+
tcx.sess.time("codegen fn", || {
263+
crate::base::codegen_and_compile_fn(&mut cx, jit_module, instance)
264+
});
263265

264266
assert!(cx.global_asm.is_empty());
265267
jit_module.finalize_definitions();

0 commit comments

Comments
 (0)