Skip to content

Commit 8429494

Browse files
committed
Create JITModule and ObjectModule wrappers and move UnwindContext calls into them
1 parent 94c2e7a commit 8429494

File tree

8 files changed

+210
-98
lines changed

8 files changed

+210
-98
lines changed

src/allocator.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@ use rustc_session::config::OomStrategy;
1111
use crate::prelude::*;
1212

1313
/// Returns whether an allocator shim was created
14-
pub(crate) fn codegen(
15-
tcx: TyCtxt<'_>,
16-
module: &mut impl Module,
17-
unwind_context: &mut UnwindContext,
18-
) -> bool {
14+
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut impl Module) -> bool {
1915
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
2016
codegen_inner(
2117
module,
22-
unwind_context,
2318
kind,
2419
tcx.alloc_error_handler_kind(()).unwrap(),
2520
tcx.sess.opts.unstable_opts.oom,
@@ -29,7 +24,6 @@ pub(crate) fn codegen(
2924

3025
fn codegen_inner(
3126
module: &mut impl Module,
32-
unwind_context: &mut UnwindContext,
3327
kind: AllocatorKind,
3428
alloc_error_handler_kind: AllocatorKind,
3529
oom_strategy: OomStrategy,
@@ -67,7 +61,6 @@ fn codegen_inner(
6761
};
6862
crate::common::create_wrapper_function(
6963
module,
70-
unwind_context,
7164
sig,
7265
&global_fn_name(method.name),
7366
&default_fn_name(method.name),
@@ -82,7 +75,6 @@ fn codegen_inner(
8275
};
8376
crate::common::create_wrapper_function(
8477
module,
85-
unwind_context,
8678
sig,
8779
"__rust_alloc_error_handler",
8880
&alloc_error_handler_name(alloc_error_handler_kind),

src/base.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,7 @@ pub(crate) fn compile_fn(
249249
}
250250

251251
// Define debuginfo for function
252-
let isa = module.isa();
253252
let debug_context = &mut cx.debug_context;
254-
let unwind_context = &mut cx.unwind_context;
255253
cx.profiler.generic_activity("generate debug info").run(|| {
256254
if let Some(debug_context) = debug_context {
257255
codegened_func.func_debug_cx.unwrap().finalize(
@@ -260,7 +258,6 @@ pub(crate) fn compile_fn(
260258
context,
261259
);
262260
}
263-
unwind_context.add_function(codegened_func.func_id, &context, isa);
264261
});
265262
}
266263

src/common.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ pub(crate) fn type_sign(ty: Ty<'_>) -> bool {
247247

248248
pub(crate) fn create_wrapper_function(
249249
module: &mut dyn Module,
250-
unwind_context: &mut UnwindContext,
251250
sig: Signature,
252251
wrapper_name: &str,
253252
callee_name: &str,
@@ -280,7 +279,6 @@ pub(crate) fn create_wrapper_function(
280279
bcx.finalize();
281280
}
282281
module.define_function(wrapper_func_id, &mut ctx).unwrap();
283-
unwind_context.add_function(wrapper_func_id, &ctx, module.isa());
284282
}
285283

286284
pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {

src/driver/aot.rs

Lines changed: 98 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ use std::path::{Path, PathBuf};
66
use std::sync::Arc;
77
use std::thread::JoinHandle;
88

9-
use cranelift_object::{ObjectBuilder, ObjectModule};
9+
use cranelift_codegen::control::ControlPlane;
10+
use cranelift_codegen::ir::Signature;
11+
use cranelift_codegen::isa::{TargetFrontendConfig, TargetIsa};
12+
use cranelift_codegen::FinalizedMachReloc;
13+
use cranelift_module::ModuleResult;
14+
use cranelift_object::{ObjectBuilder, ObjectModule, ObjectProduct};
1015
use rustc_codegen_ssa::assert_module_sources::CguReuse;
1116
use rustc_codegen_ssa::back::link::ensure_removed;
1217
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
@@ -318,7 +323,91 @@ fn produce_final_output_artifacts(
318323
// These are used in linking steps and will be cleaned up afterward.
319324
}
320325

321-
fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) -> ObjectModule {
326+
struct AOTModule {
327+
aot_module: ObjectModule,
328+
unwind_context: UnwindContext,
329+
}
330+
331+
impl AOTModule {
332+
fn finish(self) -> ObjectProduct {
333+
let mut product = self.aot_module.finish();
334+
self.unwind_context.emit(&mut product);
335+
product
336+
}
337+
}
338+
339+
impl Module for AOTModule {
340+
fn isa(&self) -> &dyn TargetIsa {
341+
self.aot_module.isa()
342+
}
343+
344+
fn declarations(&self) -> &cranelift_module::ModuleDeclarations {
345+
self.aot_module.declarations()
346+
}
347+
348+
fn get_name(&self, name: &str) -> Option<cranelift_module::FuncOrDataId> {
349+
self.aot_module.get_name(name)
350+
}
351+
352+
fn target_config(&self) -> TargetFrontendConfig {
353+
self.aot_module.target_config()
354+
}
355+
356+
fn declare_function(
357+
&mut self,
358+
name: &str,
359+
linkage: Linkage,
360+
signature: &Signature,
361+
) -> ModuleResult<FuncId> {
362+
self.aot_module.declare_function(name, linkage, signature)
363+
}
364+
365+
fn declare_anonymous_function(&mut self, signature: &Signature) -> ModuleResult<FuncId> {
366+
self.aot_module.declare_anonymous_function(signature)
367+
}
368+
369+
fn declare_data(
370+
&mut self,
371+
name: &str,
372+
linkage: Linkage,
373+
writable: bool,
374+
tls: bool,
375+
) -> ModuleResult<DataId> {
376+
self.aot_module.declare_data(name, linkage, writable, tls)
377+
}
378+
379+
fn declare_anonymous_data(&mut self, writable: bool, tls: bool) -> ModuleResult<DataId> {
380+
self.aot_module.declare_anonymous_data(writable, tls)
381+
}
382+
383+
fn define_function_with_control_plane(
384+
&mut self,
385+
func: FuncId,
386+
ctx: &mut Context,
387+
ctrl_plane: &mut ControlPlane,
388+
) -> ModuleResult<()> {
389+
self.aot_module.define_function_with_control_plane(func, ctx, ctrl_plane)?;
390+
self.unwind_context.add_function(func, ctx, self.aot_module.isa());
391+
Ok(())
392+
}
393+
394+
fn define_function_bytes(
395+
&mut self,
396+
_func_id: FuncId,
397+
_func: &Function,
398+
_alignment: u64,
399+
_bytes: &[u8],
400+
_relocs: &[FinalizedMachReloc],
401+
) -> ModuleResult<()> {
402+
unimplemented!()
403+
}
404+
405+
fn define_data(&mut self, data_id: DataId, data: &DataDescription) -> ModuleResult<()> {
406+
self.aot_module.define_data(data_id, data)
407+
}
408+
}
409+
410+
fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) -> AOTModule {
322411
let isa = crate::build_isa(sess, backend_config);
323412

324413
let mut builder =
@@ -327,16 +416,17 @@ fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) ->
327416
// is important, while cg_clif cares more about compilation times. Enabling -Zfunction-sections
328417
// can easily double the amount of time necessary to perform linking.
329418
builder.per_function_section(sess.opts.unstable_opts.function_sections.unwrap_or(false));
330-
ObjectModule::new(builder)
419+
let aot_module = ObjectModule::new(builder);
420+
let unwind_context = UnwindContext::new(aot_module.isa(), true);
421+
AOTModule { aot_module, unwind_context }
331422
}
332423

333424
fn emit_cgu(
334425
output_filenames: &OutputFilenames,
335426
prof: &SelfProfilerRef,
336427
name: String,
337-
module: ObjectModule,
428+
module: AOTModule,
338429
debug: Option<DebugContext>,
339-
unwind_context: UnwindContext,
340430
global_asm_object_file: Option<PathBuf>,
341431
producer: &str,
342432
) -> Result<ModuleCodegenResult, String> {
@@ -346,8 +436,6 @@ fn emit_cgu(
346436
debug.emit(&mut product);
347437
}
348438

349-
unwind_context.emit(&mut product);
350-
351439
let module_regular = emit_module(
352440
output_filenames,
353441
prof,
@@ -494,7 +582,6 @@ fn module_codegen(
494582

495583
let mut cx = crate::CodegenCx::new(
496584
tcx,
497-
backend_config.clone(),
498585
module.isa(),
499586
tcx.sess.opts.debuginfo != DebugInfo::None,
500587
cgu_name,
@@ -531,13 +618,7 @@ fn module_codegen(
531618
}
532619
}
533620
}
534-
crate::main_shim::maybe_create_entry_wrapper(
535-
tcx,
536-
&mut module,
537-
&mut cx.unwind_context,
538-
false,
539-
cgu.is_primary(),
540-
);
621+
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, false, cgu.is_primary());
541622

542623
let cgu_name = cgu.name().as_str().to_owned();
543624

@@ -571,7 +652,6 @@ fn module_codegen(
571652
cgu_name,
572653
module,
573654
cx.debug_context,
574-
cx.unwind_context,
575655
global_asm_object_file,
576656
&producer,
577657
)
@@ -665,13 +745,10 @@ pub(crate) fn run_aot(
665745
});
666746

667747
let mut allocator_module = make_module(tcx.sess, &backend_config, "allocator_shim".to_string());
668-
let mut allocator_unwind_context = UnwindContext::new(allocator_module.isa(), true);
669-
let created_alloc_shim =
670-
crate::allocator::codegen(tcx, &mut allocator_module, &mut allocator_unwind_context);
748+
let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module);
671749

672750
let allocator_module = if created_alloc_shim {
673-
let mut product = allocator_module.finish();
674-
allocator_unwind_context.emit(&mut product);
751+
let product = allocator_module.finish();
675752

676753
match emit_module(
677754
tcx.output_filenames(()),

0 commit comments

Comments
 (0)