Skip to content

Commit cc05efe

Browse files
committed
Introduce UnwindModule wrapper around a Module and UnwindContext
This way all UnwindContext::add_function calls can be done automatically in a single place.
1 parent 94c2e7a commit cc05efe

File tree

9 files changed

+148
-98
lines changed

9 files changed

+148
-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: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc_session::Session;
2626
use crate::concurrency_limiter::{ConcurrencyLimiter, ConcurrencyLimiterToken};
2727
use crate::debuginfo::TypeDebugContext;
2828
use crate::global_asm::GlobalAsmConfig;
29+
use crate::unwind_module::UnwindModule;
2930
use crate::{prelude::*, BackendConfig};
3031

3132
struct ModuleCodegenResult {
@@ -318,7 +319,11 @@ fn produce_final_output_artifacts(
318319
// These are used in linking steps and will be cleaned up afterward.
319320
}
320321

321-
fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) -> ObjectModule {
322+
fn make_module(
323+
sess: &Session,
324+
backend_config: &BackendConfig,
325+
name: String,
326+
) -> UnwindModule<ObjectModule> {
322327
let isa = crate::build_isa(sess, backend_config);
323328

324329
let mut builder =
@@ -327,16 +332,15 @@ fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) ->
327332
// is important, while cg_clif cares more about compilation times. Enabling -Zfunction-sections
328333
// can easily double the amount of time necessary to perform linking.
329334
builder.per_function_section(sess.opts.unstable_opts.function_sections.unwrap_or(false));
330-
ObjectModule::new(builder)
335+
UnwindModule::new(ObjectModule::new(builder), true)
331336
}
332337

333338
fn emit_cgu(
334339
output_filenames: &OutputFilenames,
335340
prof: &SelfProfilerRef,
336341
name: String,
337-
module: ObjectModule,
342+
module: UnwindModule<ObjectModule>,
338343
debug: Option<DebugContext>,
339-
unwind_context: UnwindContext,
340344
global_asm_object_file: Option<PathBuf>,
341345
producer: &str,
342346
) -> Result<ModuleCodegenResult, String> {
@@ -346,8 +350,6 @@ fn emit_cgu(
346350
debug.emit(&mut product);
347351
}
348352

349-
unwind_context.emit(&mut product);
350-
351353
let module_regular = emit_module(
352354
output_filenames,
353355
prof,
@@ -494,7 +496,6 @@ fn module_codegen(
494496

495497
let mut cx = crate::CodegenCx::new(
496498
tcx,
497-
backend_config.clone(),
498499
module.isa(),
499500
tcx.sess.opts.debuginfo != DebugInfo::None,
500501
cgu_name,
@@ -531,13 +532,7 @@ fn module_codegen(
531532
}
532533
}
533534
}
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-
);
535+
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, false, cgu.is_primary());
541536

542537
let cgu_name = cgu.name().as_str().to_owned();
543538

@@ -571,7 +566,6 @@ fn module_codegen(
571566
cgu_name,
572567
module,
573568
cx.debug_context,
574-
cx.unwind_context,
575569
global_asm_object_file,
576570
&producer,
577571
)
@@ -665,13 +659,10 @@ pub(crate) fn run_aot(
665659
});
666660

667661
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);
662+
let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module);
671663

672664
let allocator_module = if created_alloc_shim {
673-
let mut product = allocator_module.finish();
674-
allocator_unwind_context.emit(&mut product);
665+
let product = allocator_module.finish();
675666

676667
match emit_module(
677668
tcx.output_filenames(()),

src/driver/jit.rs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ use rustc_session::Session;
1414
use rustc_span::Symbol;
1515

1616
use crate::debuginfo::TypeDebugContext;
17+
use crate::unwind_module::UnwindModule;
1718
use crate::{prelude::*, BackendConfig};
1819
use crate::{CodegenCx, CodegenMode};
1920

2021
struct JitState {
21-
backend_config: BackendConfig,
22-
jit_module: JITModule,
22+
jit_module: UnwindModule<JITModule>,
2323
}
2424

2525
thread_local! {
@@ -63,7 +63,7 @@ fn create_jit_module(
6363
tcx: TyCtxt<'_>,
6464
backend_config: &BackendConfig,
6565
hotswap: bool,
66-
) -> (JITModule, CodegenCx) {
66+
) -> (UnwindModule<JITModule>, CodegenCx) {
6767
let crate_info = CrateInfo::new(tcx, "dummy_target_cpu".to_string());
6868

6969
let isa = crate::build_isa(tcx.sess, backend_config);
@@ -72,17 +72,11 @@ fn create_jit_module(
7272
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
7373
jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info));
7474
jit_builder.symbol("__clif_jit_fn", clif_jit_fn as *const u8);
75-
let mut jit_module = JITModule::new(jit_builder);
75+
let mut jit_module = UnwindModule::new(JITModule::new(jit_builder), false);
7676

77-
let mut cx = crate::CodegenCx::new(
78-
tcx,
79-
backend_config.clone(),
80-
jit_module.isa(),
81-
false,
82-
Symbol::intern("dummy_cgu_name"),
83-
);
77+
let cx = crate::CodegenCx::new(tcx, jit_module.isa(), false, Symbol::intern("dummy_cgu_name"));
8478

85-
crate::allocator::codegen(tcx, &mut jit_module, &mut cx.unwind_context);
79+
crate::allocator::codegen(tcx, &mut jit_module);
8680

8781
(jit_module, cx)
8882
}
@@ -128,7 +122,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
128122
);
129123
}
130124
CodegenMode::JitLazy => {
131-
codegen_shim(tcx, &mut cx, &mut cached_context, &mut jit_module, inst)
125+
codegen_shim(tcx, &mut cached_context, &mut jit_module, inst)
132126
}
133127
},
134128
MonoItem::Static(def_id) => {
@@ -146,18 +140,11 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
146140
tcx.dcx().fatal("Inline asm is not supported in JIT mode");
147141
}
148142

149-
crate::main_shim::maybe_create_entry_wrapper(
150-
tcx,
151-
&mut jit_module,
152-
&mut cx.unwind_context,
153-
true,
154-
true,
155-
);
143+
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, true, true);
156144

157145
tcx.dcx().abort_if_errors();
158146

159-
jit_module.finalize_definitions().unwrap();
160-
unsafe { cx.unwind_context.register_jit(&jit_module) };
147+
jit_module.finalize_definitions();
161148

162149
println!(
163150
"Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed"
@@ -177,12 +164,12 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
177164
call_conv: jit_module.target_config().default_call_conv,
178165
};
179166
let start_func_id = jit_module.declare_function("main", Linkage::Import, &start_sig).unwrap();
180-
let finalized_start: *const u8 = jit_module.get_finalized_function(start_func_id);
167+
let finalized_start: *const u8 = jit_module.module.get_finalized_function(start_func_id);
181168

182169
LAZY_JIT_STATE.with(|lazy_jit_state| {
183170
let mut lazy_jit_state = lazy_jit_state.borrow_mut();
184171
assert!(lazy_jit_state.is_none());
185-
*lazy_jit_state = Some(JitState { backend_config, jit_module });
172+
*lazy_jit_state = Some(JitState { jit_module });
186173
});
187174

188175
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
@@ -268,7 +255,6 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) ->
268255
let mut lazy_jit_state = lazy_jit_state.borrow_mut();
269256
let lazy_jit_state = lazy_jit_state.as_mut().unwrap();
270257
let jit_module = &mut lazy_jit_state.jit_module;
271-
let backend_config = lazy_jit_state.backend_config.clone();
272258

273259
let name = tcx.symbol_name(instance).name;
274260
let sig = crate::abi::get_function_sig(
@@ -278,7 +264,7 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) ->
278264
);
279265
let func_id = jit_module.declare_function(name, Linkage::Export, &sig).unwrap();
280266

281-
let current_ptr = jit_module.read_got_entry(func_id);
267+
let current_ptr = jit_module.module.read_got_entry(func_id);
282268

283269
// If the function's GOT entry has already been updated to point at something other
284270
// than the shim trampoline, don't re-jit but just return the new pointer instead.
@@ -288,21 +274,19 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) ->
288274
return current_ptr;
289275
}
290276

291-
jit_module.prepare_for_function_redefine(func_id).unwrap();
277+
jit_module.module.prepare_for_function_redefine(func_id).unwrap();
292278

293279
let mut cx = crate::CodegenCx::new(
294280
tcx,
295-
backend_config,
296281
jit_module.isa(),
297282
false,
298283
Symbol::intern("dummy_cgu_name"),
299284
);
300285
codegen_and_compile_fn(tcx, &mut cx, &mut Context::new(), jit_module, instance);
301286

302287
assert!(cx.global_asm.is_empty());
303-
jit_module.finalize_definitions().unwrap();
304-
unsafe { cx.unwind_context.register_jit(&jit_module) };
305-
jit_module.get_finalized_function(func_id)
288+
jit_module.finalize_definitions();
289+
jit_module.module.get_finalized_function(func_id)
306290
})
307291
})
308292
}
@@ -362,9 +346,8 @@ fn dep_symbol_lookup_fn(
362346

363347
fn codegen_shim<'tcx>(
364348
tcx: TyCtxt<'tcx>,
365-
cx: &mut CodegenCx,
366349
cached_context: &mut Context,
367-
module: &mut JITModule,
350+
module: &mut UnwindModule<JITModule>,
368351
inst: Instance<'tcx>,
369352
) {
370353
let pointer_type = module.target_config().pointer_type();
@@ -413,5 +396,4 @@ fn codegen_shim<'tcx>(
413396
trampoline_builder.ins().return_(&ret_vals);
414397

415398
module.define_function(func_id, context).unwrap();
416-
cx.unwind_context.add_function(func_id, context, module.isa());
417399
}

src/inline_asm.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,7 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>(
113113
);
114114
let sig =
115115
get_function_sig(fx.tcx, fx.target_config.default_call_conv, instance);
116-
create_wrapper_function(
117-
fx.module,
118-
&mut fx.cx.unwind_context,
119-
sig,
120-
&wrapper_name,
121-
symbol.name,
122-
);
116+
create_wrapper_function(fx.module, sig, &wrapper_name, symbol.name);
123117

124118
CInlineAsmOperand::Symbol { symbol: wrapper_name }
125119
} else {
@@ -283,13 +277,7 @@ pub(crate) fn codegen_naked_asm<'tcx>(
283277
);
284278
let sig =
285279
get_function_sig(tcx, module.target_config().default_call_conv, instance);
286-
create_wrapper_function(
287-
module,
288-
&mut cx.unwind_context,
289-
sig,
290-
&wrapper_name,
291-
symbol.name,
292-
);
280+
create_wrapper_function(module, sig, &wrapper_name, symbol.name);
293281

294282
CInlineAsmOperand::Symbol { symbol: wrapper_name }
295283
} else {

src/lib.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ mod pretty_clif;
7979
mod toolchain;
8080
mod trap;
8181
mod unsize;
82+
mod unwind_module;
8283
mod value_and_place;
8384
mod vtable;
8485

@@ -130,22 +131,13 @@ struct CodegenCx {
130131
global_asm: String,
131132
inline_asm_index: Cell<usize>,
132133
debug_context: Option<DebugContext>,
133-
unwind_context: UnwindContext,
134134
cgu_name: Symbol,
135135
}
136136

137137
impl CodegenCx {
138-
fn new(
139-
tcx: TyCtxt<'_>,
140-
backend_config: BackendConfig,
141-
isa: &dyn TargetIsa,
142-
debug_info: bool,
143-
cgu_name: Symbol,
144-
) -> Self {
138+
fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa, debug_info: bool, cgu_name: Symbol) -> Self {
145139
assert_eq!(pointer_ty(tcx), isa.pointer_type());
146140

147-
let unwind_context =
148-
UnwindContext::new(isa, matches!(backend_config.codegen_mode, CodegenMode::Aot));
149141
let debug_context = if debug_info && !tcx.sess.target.options.is_like_windows {
150142
Some(DebugContext::new(tcx, isa, cgu_name.as_str()))
151143
} else {
@@ -158,7 +150,6 @@ impl CodegenCx {
158150
global_asm: String::new(),
159151
inline_asm_index: Cell::new(0),
160152
debug_context,
161-
unwind_context,
162153
cgu_name,
163154
}
164155
}

0 commit comments

Comments
 (0)