2021-04-30 12:49:58 +00:00
|
|
|
//! The JIT driver uses [`cranelift_jit`] to JIT execute programs without writing any object
|
2020-09-23 13:13:49 +00:00
|
|
|
//! files.
|
|
|
|
|
2020-03-12 10:44:27 +00:00
|
|
|
use std::ffi::CString;
|
|
|
|
use std::os::raw::{c_char, c_int};
|
|
|
|
|
2023-10-09 08:52:46 +00:00
|
|
|
use cranelift_jit::{JITBuilder, JITModule};
|
2020-03-12 10:44:27 +00:00
|
|
|
use rustc_codegen_ssa::CrateInfo;
|
2020-12-27 09:30:38 +00:00
|
|
|
use rustc_middle::mir::mono::MonoItem;
|
2021-07-07 09:14:20 +00:00
|
|
|
use rustc_session::Session;
|
2024-12-06 12:10:30 +00:00
|
|
|
use rustc_span::sym;
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2025-03-05 15:12:57 +00:00
|
|
|
use crate::CodegenCx;
|
2024-03-28 11:43:35 +00:00
|
|
|
use crate::debuginfo::TypeDebugContext;
|
2024-07-28 22:13:50 +00:00
|
|
|
use crate::prelude::*;
|
2024-06-30 11:28:14 +00:00
|
|
|
use crate::unwind_module::UnwindModule;
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2025-03-05 15:35:51 +00:00
|
|
|
fn create_jit_module(tcx: TyCtxt<'_>) -> (UnwindModule<JITModule>, CodegenCx) {
|
2021-07-07 09:14:20 +00:00
|
|
|
let crate_info = CrateInfo::new(tcx, "dummy_target_cpu".to_string());
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2025-03-06 15:02:29 +00:00
|
|
|
let isa = crate::build_isa(tcx.sess, true);
|
2021-04-30 12:49:58 +00:00
|
|
|
let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
|
2021-03-29 08:45:09 +00:00
|
|
|
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
|
2022-10-23 14:22:55 +00:00
|
|
|
jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info));
|
2024-06-30 11:28:14 +00:00
|
|
|
let mut jit_module = UnwindModule::new(JITModule::new(jit_builder), false);
|
2021-04-30 12:49:58 +00:00
|
|
|
|
2024-11-27 17:52:57 +00:00
|
|
|
let cx = crate::CodegenCx::new(tcx, jit_module.isa(), false, sym::dummy_cgu_name);
|
2021-04-30 12:49:58 +00:00
|
|
|
|
2024-06-30 11:28:14 +00:00
|
|
|
crate::allocator::codegen(tcx, &mut jit_module);
|
2021-04-30 12:49:58 +00:00
|
|
|
|
|
|
|
(jit_module, cx)
|
|
|
|
}
|
|
|
|
|
2025-03-05 15:35:51 +00:00
|
|
|
pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_args: Vec<String>) -> ! {
|
2021-04-30 12:49:58 +00:00
|
|
|
if !tcx.sess.opts.output_types.should_codegen() {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().fatal("JIT mode doesn't work with `cargo check`");
|
2021-04-30 12:49:58 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 10:28:20 +00:00
|
|
|
if !tcx.crate_types().contains(&rustc_session::config::CrateType::Executable) {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().fatal("can't jit non-executable crate");
|
2021-04-30 12:49:58 +00:00
|
|
|
}
|
|
|
|
|
2025-03-05 15:35:51 +00:00
|
|
|
let (mut jit_module, mut cx) = create_jit_module(tcx);
|
2022-08-24 16:40:58 +00:00
|
|
|
let mut cached_context = Context::new();
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2025-02-07 20:58:27 +00:00
|
|
|
let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
|
2020-03-12 10:44:27 +00:00
|
|
|
let mono_items = cgus
|
|
|
|
.iter()
|
|
|
|
.map(|cgu| cgu.items_in_deterministic_order(tcx).into_iter())
|
|
|
|
.flatten()
|
2023-07-22 13:32:34 +00:00
|
|
|
.collect::<FxHashMap<_, _>>()
|
2020-03-12 10:44:27 +00:00
|
|
|
.into_iter()
|
2023-07-22 13:32:34 +00:00
|
|
|
.collect::<Vec<(_, _)>>();
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2023-02-09 11:38:16 +00:00
|
|
|
tcx.sess.time("codegen mono items", || {
|
2021-04-30 12:49:58 +00:00
|
|
|
super::predefine_mono_items(tcx, &mut jit_module, &mono_items);
|
2021-03-29 08:45:09 +00:00
|
|
|
for (mono_item, _) in mono_items {
|
2020-12-27 09:30:38 +00:00
|
|
|
match mono_item {
|
2025-03-05 15:12:57 +00:00
|
|
|
MonoItem::Fn(inst) => {
|
2025-03-05 15:35:51 +00:00
|
|
|
codegen_and_compile_fn(
|
|
|
|
tcx,
|
|
|
|
&mut cx,
|
|
|
|
&mut cached_context,
|
|
|
|
&mut jit_module,
|
|
|
|
inst,
|
|
|
|
);
|
2025-03-05 15:12:57 +00:00
|
|
|
}
|
2020-12-27 09:30:38 +00:00
|
|
|
MonoItem::Static(def_id) => {
|
2021-04-30 12:49:58 +00:00
|
|
|
crate::constant::codegen_static(tcx, &mut jit_module, def_id);
|
2020-12-27 09:30:38 +00:00
|
|
|
}
|
2021-01-30 18:18:48 +00:00
|
|
|
MonoItem::GlobalAsm(item_id) => {
|
2025-02-02 23:45:49 +00:00
|
|
|
let item = tcx.hir_item(item_id);
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().span_fatal(item.span, "Global asm is not supported in JIT mode");
|
2020-12-27 09:30:38 +00:00
|
|
|
}
|
2020-11-27 19:48:53 +00:00
|
|
|
}
|
2020-12-27 09:30:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
if !cx.global_asm.is_empty() {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().fatal("Inline asm is not supported in JIT mode");
|
2020-07-09 17:24:53 +00:00
|
|
|
}
|
2020-12-27 09:30:38 +00:00
|
|
|
|
2024-06-30 11:28:14 +00:00
|
|
|
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, true, true);
|
2024-05-13 13:26:33 +00:00
|
|
|
|
2023-12-21 05:26:09 +00:00
|
|
|
tcx.dcx().abort_if_errors();
|
2020-10-11 09:31:36 +00:00
|
|
|
|
2024-06-30 11:28:14 +00:00
|
|
|
jit_module.finalize_definitions();
|
2020-05-01 18:57:51 +00:00
|
|
|
|
2021-03-05 18:12:59 +00:00
|
|
|
println!(
|
|
|
|
"Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed"
|
|
|
|
);
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2020-06-20 10:01:24 +00:00
|
|
|
let args = std::iter::once(&*tcx.crate_name(LOCAL_CRATE).as_str().to_string())
|
2024-12-06 12:10:30 +00:00
|
|
|
.chain(jit_args.iter().map(|arg| &**arg))
|
2020-03-12 10:44:27 +00:00
|
|
|
.map(|arg| CString::new(arg).unwrap())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
let start_sig = Signature {
|
|
|
|
params: vec![
|
|
|
|
AbiParam::new(jit_module.target_config().pointer_type()),
|
|
|
|
AbiParam::new(jit_module.target_config().pointer_type()),
|
|
|
|
],
|
|
|
|
returns: vec![AbiParam::new(jit_module.target_config().pointer_type() /*isize*/)],
|
2021-07-07 09:14:20 +00:00
|
|
|
call_conv: jit_module.target_config().default_call_conv,
|
2021-04-30 12:49:58 +00:00
|
|
|
};
|
|
|
|
let start_func_id = jit_module.declare_function("main", Linkage::Import, &start_sig).unwrap();
|
2024-06-30 11:28:14 +00:00
|
|
|
let finalized_start: *const u8 = jit_module.module.get_finalized_function(start_func_id);
|
2021-04-30 12:49:58 +00:00
|
|
|
|
2021-04-30 13:27:05 +00:00
|
|
|
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
|
|
|
|
unsafe { ::std::mem::transmute(finalized_start) };
|
2021-07-07 09:14:20 +00:00
|
|
|
|
2025-03-05 15:35:51 +00:00
|
|
|
let mut argv = args.iter().map(|arg| arg.as_ptr()).collect::<Vec<_>>();
|
2021-07-07 09:14:20 +00:00
|
|
|
|
2025-03-05 15:35:51 +00:00
|
|
|
// Push a null pointer as a terminating argument. This is required by POSIX and
|
|
|
|
// useful as some dynamic linkers use it as a marker to jump over.
|
|
|
|
argv.push(std::ptr::null());
|
2021-07-07 09:14:20 +00:00
|
|
|
|
2025-03-05 15:35:51 +00:00
|
|
|
let ret = f(args.len() as c_int, argv.as_ptr());
|
|
|
|
std::process::exit(ret);
|
2020-03-12 10:44:27 +00:00
|
|
|
}
|
|
|
|
|
2025-04-08 18:41:42 +00:00
|
|
|
fn codegen_and_compile_fn<'tcx>(
|
2023-02-09 11:38:16 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
cx: &mut crate::CodegenCx,
|
|
|
|
cached_context: &mut Context,
|
|
|
|
module: &mut dyn Module,
|
|
|
|
instance: Instance<'tcx>,
|
|
|
|
) {
|
2023-04-29 12:00:43 +00:00
|
|
|
cranelift_codegen::timing::set_thread_profiler(Box::new(super::MeasuremeProfiler(
|
2024-12-06 12:10:30 +00:00
|
|
|
tcx.prof.clone(),
|
2023-04-29 12:00:43 +00:00
|
|
|
)));
|
|
|
|
|
2023-02-09 11:38:16 +00:00
|
|
|
tcx.prof.generic_activity("codegen and compile fn").run(|| {
|
|
|
|
let _inst_guard =
|
|
|
|
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
|
|
|
|
|
|
|
|
let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
|
2024-05-13 13:26:33 +00:00
|
|
|
if let Some(codegened_func) = crate::base::codegen_fn(
|
2024-03-28 11:43:35 +00:00
|
|
|
tcx,
|
|
|
|
cx,
|
|
|
|
&mut TypeDebugContext::default(),
|
|
|
|
cached_func,
|
|
|
|
module,
|
|
|
|
instance,
|
2024-05-13 13:26:33 +00:00
|
|
|
) {
|
2024-12-06 12:10:30 +00:00
|
|
|
crate::base::compile_fn(cx, &tcx.prof, cached_context, module, codegened_func);
|
2024-05-13 13:26:33 +00:00
|
|
|
}
|
2023-02-09 11:38:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-23 14:22:55 +00:00
|
|
|
fn dep_symbol_lookup_fn(
|
2021-07-07 09:14:20 +00:00
|
|
|
sess: &Session,
|
|
|
|
crate_info: CrateInfo,
|
2024-06-30 11:28:14 +00:00
|
|
|
) -> Box<dyn Fn(&str) -> Option<*const u8> + Send> {
|
2020-03-31 11:20:19 +00:00
|
|
|
use rustc_middle::middle::dependency_format::Linkage;
|
2020-03-12 10:44:27 +00:00
|
|
|
|
|
|
|
let mut dylib_paths = Vec::new();
|
|
|
|
|
2025-01-05 15:44:46 +00:00
|
|
|
let data = &crate_info.dependency_formats[&rustc_session::config::CrateType::Executable];
|
2023-03-15 14:41:48 +00:00
|
|
|
// `used_crates` is in reverse postorder in terms of dependencies. Reverse the order here to
|
|
|
|
// get a postorder which ensures that all dependencies of a dylib are loaded before the dylib
|
|
|
|
// itself. This helps the dynamic linker to find dylibs not in the regular dynamic library
|
|
|
|
// search path.
|
|
|
|
for &cnum in crate_info.used_crates.iter().rev() {
|
2020-03-12 10:44:27 +00:00
|
|
|
let src = &crate_info.used_crate_source[&cnum];
|
2024-12-12 14:55:36 +00:00
|
|
|
match data[cnum] {
|
2020-03-12 10:44:27 +00:00
|
|
|
Linkage::NotLinked | Linkage::IncludedFromDylib => {}
|
|
|
|
Linkage::Static => {
|
2022-04-05 12:52:53 +00:00
|
|
|
let name = crate_info.crate_name[&cnum];
|
2024-01-26 18:33:45 +00:00
|
|
|
let mut diag = sess.dcx().struct_err(format!("Can't load static lib {}", name));
|
|
|
|
diag.note("rustc_codegen_cranelift can only load dylibs in JIT mode.");
|
|
|
|
diag.emit();
|
2020-03-12 10:44:27 +00:00
|
|
|
}
|
|
|
|
Linkage::Dynamic => {
|
|
|
|
dylib_paths.push(src.dylib.as_ref().unwrap().0.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 14:22:55 +00:00
|
|
|
let imported_dylibs = Box::leak(
|
|
|
|
dylib_paths
|
|
|
|
.into_iter()
|
|
|
|
.map(|path| unsafe { libloading::Library::new(&path).unwrap() })
|
|
|
|
.collect::<Box<[_]>>(),
|
|
|
|
);
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2023-12-21 05:26:09 +00:00
|
|
|
sess.dcx().abort_if_errors();
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2022-10-23 14:22:55 +00:00
|
|
|
Box::new(move |sym_name| {
|
|
|
|
for dylib in &*imported_dylibs {
|
|
|
|
if let Ok(sym) = unsafe { dylib.get::<*const u8>(sym_name.as_bytes()) } {
|
|
|
|
return Some(*sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
2020-03-12 10:44:27 +00:00
|
|
|
}
|