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

Commit 86530f8

Browse files
committed
Use maybe_create_entry_wrapper again in jit mode
This simplifies the jit driver a lot
1 parent ba8e610 commit 86530f8

File tree

3 files changed

+42
-60
lines changed

3 files changed

+42
-60
lines changed

src/driver/aot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn module_codegen(
135135
}
136136
}
137137
}
138-
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut cx.unwind_context);
138+
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut cx.unwind_context, false);
139139

140140
let codegen_result = emit_module(
141141
tcx,

src/driver/jit.rs

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::os::raw::{c_char, c_int};
88
use cranelift_codegen::binemit::{NullStackMapSink, NullTrapSink};
99
use rustc_codegen_ssa::CrateInfo;
1010
use rustc_middle::mir::mono::MonoItem;
11-
use rustc_session::config::EntryFnType;
1211

1312
use cranelift_jit::{JITBuilder, JITModule};
1413

@@ -81,6 +80,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
8180
}
8281

8382
crate::allocator::codegen(tcx, &mut jit_module, &mut cx.unwind_context);
83+
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, &mut cx.unwind_context, true);
8484

8585
tcx.sess.abort_if_errors();
8686

@@ -105,57 +105,24 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
105105
assert!(tls_backend_config.borrow_mut().replace(backend_config).is_none())
106106
});
107107

108-
let (main_def_id, entry_ty) = tcx.entry_fn(LOCAL_CRATE).unwrap();
109-
let instance = Instance::mono(tcx, main_def_id.to_def_id()).polymorphize(tcx);
110-
111-
match entry_ty {
112-
EntryFnType::Main => {
113-
// FIXME set program arguments somehow
114-
115-
let main_sig = Signature {
116-
params: vec![],
117-
returns: vec![],
118-
call_conv: CallConv::triple_default(&crate::target_triple(tcx.sess)),
119-
};
120-
let main_func_id = jit_module
121-
.declare_function(tcx.symbol_name(instance).name, Linkage::Import, &main_sig)
122-
.unwrap();
123-
let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id);
124-
125-
CURRENT_MODULE.with(|current_module| {
126-
assert!(current_module.borrow_mut().replace(jit_module).is_none())
127-
});
128-
129-
let f: extern "C" fn() = unsafe { ::std::mem::transmute(finalized_main) };
130-
f();
131-
std::process::exit(0);
132-
}
133-
EntryFnType::Start => {
134-
let start_sig = Signature {
135-
params: vec![
136-
AbiParam::new(jit_module.target_config().pointer_type()),
137-
AbiParam::new(jit_module.target_config().pointer_type()),
138-
],
139-
returns: vec![AbiParam::new(
140-
jit_module.target_config().pointer_type(), /*isize*/
141-
)],
142-
call_conv: CallConv::triple_default(&crate::target_triple(tcx.sess)),
143-
};
144-
let start_func_id = jit_module
145-
.declare_function(tcx.symbol_name(instance).name, Linkage::Import, &start_sig)
146-
.unwrap();
147-
let finalized_start: *const u8 = jit_module.get_finalized_function(start_func_id);
148-
149-
CURRENT_MODULE.with(|current_module| {
150-
assert!(current_module.borrow_mut().replace(jit_module).is_none())
151-
});
152-
153-
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
154-
unsafe { ::std::mem::transmute(finalized_start) };
155-
let ret = f(args.len() as c_int, argv.as_ptr());
156-
std::process::exit(ret);
157-
}
158-
}
108+
let start_sig = Signature {
109+
params: vec![
110+
AbiParam::new(jit_module.target_config().pointer_type()),
111+
AbiParam::new(jit_module.target_config().pointer_type()),
112+
],
113+
returns: vec![AbiParam::new(jit_module.target_config().pointer_type() /*isize*/)],
114+
call_conv: CallConv::triple_default(&crate::target_triple(tcx.sess)),
115+
};
116+
let start_func_id = jit_module.declare_function("main", Linkage::Import, &start_sig).unwrap();
117+
let finalized_start: *const u8 = jit_module.get_finalized_function(start_func_id);
118+
119+
CURRENT_MODULE
120+
.with(|current_module| assert!(current_module.borrow_mut().replace(jit_module).is_none()));
121+
122+
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
123+
unsafe { ::std::mem::transmute(finalized_start) };
124+
let ret = f(args.len() as c_int, argv.as_ptr());
125+
std::process::exit(ret);
159126
}
160127

161128
#[no_mangle]

src/main_shim.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ pub(crate) fn maybe_create_entry_wrapper(
1010
tcx: TyCtxt<'_>,
1111
module: &mut impl Module,
1212
unwind_context: &mut UnwindContext,
13+
ignore_lang_start_wrapper: bool,
1314
) {
14-
let (main_def_id, use_start_lang_item) = match tcx.entry_fn(LOCAL_CRATE) {
15+
let (main_def_id, is_main_fn) = match tcx.entry_fn(LOCAL_CRATE) {
1516
Some((def_id, entry_ty)) => (
1617
def_id.to_def_id(),
1718
match entry_ty {
@@ -27,14 +28,22 @@ pub(crate) fn maybe_create_entry_wrapper(
2728
return;
2829
}
2930

30-
create_entry_fn(tcx, module, unwind_context, main_def_id, use_start_lang_item);
31+
create_entry_fn(
32+
tcx,
33+
module,
34+
unwind_context,
35+
main_def_id,
36+
ignore_lang_start_wrapper,
37+
is_main_fn,
38+
);
3139

3240
fn create_entry_fn(
3341
tcx: TyCtxt<'_>,
3442
m: &mut impl Module,
3543
unwind_context: &mut UnwindContext,
3644
rust_main_def_id: DefId,
37-
use_start_lang_item: bool,
45+
ignore_lang_start_wrapper: bool,
46+
is_main_fn: bool,
3847
) {
3948
let main_ret_ty = tcx.fn_sig(rust_main_def_id).output();
4049
// Given that `main()` has no arguments,
@@ -74,7 +83,12 @@ pub(crate) fn maybe_create_entry_wrapper(
7483

7584
let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
7685

77-
let call_inst = if use_start_lang_item {
86+
let result = if is_main_fn && ignore_lang_start_wrapper {
87+
// regular main fn, but ignoring #[lang = "start"] as we are running in the jit
88+
// FIXME set program arguments somehow
89+
bcx.ins().call(main_func_ref, &[]);
90+
bcx.ins().iconst(m.target_config().pointer_type(), 0)
91+
} else if is_main_fn {
7892
let start_def_id = tcx.require_lang_item(LangItem::Start, None);
7993
let start_instance = Instance::resolve(
8094
tcx,
@@ -90,13 +104,14 @@ pub(crate) fn maybe_create_entry_wrapper(
90104
let main_val = bcx.ins().func_addr(m.target_config().pointer_type(), main_func_ref);
91105

92106
let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
93-
bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv])
107+
let call_inst = bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv]);
108+
bcx.inst_results(call_inst)[0]
94109
} else {
95110
// using user-defined start fn
96-
bcx.ins().call(main_func_ref, &[arg_argc, arg_argv])
111+
let call_inst = bcx.ins().call(main_func_ref, &[arg_argc, arg_argv]);
112+
bcx.inst_results(call_inst)[0]
97113
};
98114

99-
let result = bcx.inst_results(call_inst)[0];
100115
bcx.ins().return_(&[result]);
101116
bcx.seal_all_blocks();
102117
bcx.finalize();

0 commit comments

Comments
 (0)