3 use std::os::raw::{c_char, c_int};
5 use rustc::dep_graph::{WorkProduct, WorkProductFileKind, WorkProductId};
6 use rustc::middle::cstore::EncodedMetadata;
7 use rustc::mir::mono::{CodegenUnit, Linkage as RLinkage, Visibility};
8 use rustc::session::config::{DebugInfo, OutputType};
9 use rustc_session::cgu_reuse_tracker::CguReuse;
10 use rustc_codegen_ssa::back::linker::LinkerInfo;
11 use rustc_codegen_ssa::CrateInfo;
13 use crate::prelude::*;
15 use crate::backend::{Emit, WriteDebugInfo};
19 metadata: EncodedMetadata,
20 need_metadata_module: bool,
22 tcx.sess.abort_if_errors();
24 if std::env::var("CG_CLIF_JIT").is_ok()
25 && tcx.sess.crate_types.get().contains(&CrateType::Executable)
27 #[cfg(not(target_arch = "wasm32"))]
28 let _: ! = run_jit(tcx);
30 #[cfg(target_arch = "wasm32")]
31 panic!("jit not supported on wasm");
34 run_aot(tcx, metadata, need_metadata_module)
37 #[cfg(not(target_arch = "wasm32"))]
38 fn run_jit(tcx: TyCtxt<'_>) -> ! {
39 use cranelift_simplejit::{SimpleJITBackend, SimpleJITBuilder};
41 // Rustc opens us without the RTLD_GLOBAL flag, so __cg_clif_global_atomic_mutex will not be
42 // exported. We fix this by opening ourself again as global.
43 // FIXME remove once atomic_shim is gone
44 let cg_dylib = std::ffi::OsString::from(&tcx.sess.opts.debugging_opts.codegen_backend.as_ref().unwrap());
45 std::mem::forget(libloading::os::unix::Library::open(Some(cg_dylib), libc::RTLD_NOW | libc::RTLD_GLOBAL).unwrap());
48 let imported_symbols = load_imported_symbols_for_jit(tcx);
50 let mut jit_builder = SimpleJITBuilder::with_isa(
51 crate::build_isa(tcx.sess, false),
52 cranelift_module::default_libcall_names(),
54 jit_builder.symbols(imported_symbols);
55 let mut jit_module: Module<SimpleJITBackend> = Module::new(jit_builder);
56 assert_eq!(pointer_ty(tcx), jit_module.target_config().pointer_type());
60 AbiParam::new(jit_module.target_config().pointer_type()),
61 AbiParam::new(jit_module.target_config().pointer_type()),
63 returns: vec![AbiParam::new(
64 jit_module.target_config().pointer_type(), /*isize*/
66 call_conv: CallConv::triple_default(&crate::target_triple(tcx.sess)),
68 let main_func_id = jit_module
69 .declare_function("main", Linkage::Import, &sig)
72 let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
75 .map(|cgu| cgu.items_in_deterministic_order(tcx).into_iter())
77 .collect::<FxHashMap<_, (_, _)>>()
79 .collect::<Vec<(_, (_, _))>>();
81 time(tcx, "codegen mono items", || {
82 codegen_mono_items(tcx, &mut jit_module, None, mono_items);
84 crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module);
85 crate::allocator::codegen(tcx, &mut jit_module);
87 jit_module.finalize_definitions();
89 tcx.sess.abort_if_errors();
91 let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id);
93 println!("Rustc codegen cranelift will JIT run the executable, because the CG_CLIF_JIT env var is set");
95 let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
96 unsafe { ::std::mem::transmute(finalized_main) };
98 let args = ::std::env::var("CG_CLIF_JIT_ARGS").unwrap_or_else(|_| String::new());
101 .chain(Some(&*tcx.crate_name(LOCAL_CRATE).as_str().to_string()))
102 .map(|arg| CString::new(arg).unwrap())
103 .collect::<Vec<_>>();
104 let argv = args.iter().map(|arg| arg.as_ptr()).collect::<Vec<_>>();
105 // TODO: Rust doesn't care, but POSIX argv has a NULL sentinel at the end
107 let ret = f(args.len() as c_int, argv.as_ptr());
110 std::process::exit(ret);
113 fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> {
114 use rustc::middle::dependency_format::Linkage;
116 let mut dylib_paths = Vec::new();
118 let crate_info = CrateInfo::new(tcx);
119 let formats = tcx.dependency_formats(LOCAL_CRATE);
122 .find(|(crate_type, _data)| *crate_type == CrateType::Executable)
125 for &(cnum, _) in &crate_info.used_crates_dynamic {
126 let src = &crate_info.used_crate_source[&cnum];
127 match data[cnum.as_usize() - 1] {
128 Linkage::NotLinked | Linkage::IncludedFromDylib => {}
130 let name = tcx.crate_name(cnum);
133 .struct_err(&format!("Can't load static lib {}", name.as_str()));
134 err.note("rustc_codegen_cranelift can only load dylibs in JIT mode.");
137 Linkage::Dynamic => {
138 dylib_paths.push(src.dylib.as_ref().unwrap().0.clone());
143 let mut imported_symbols = Vec::new();
144 for path in dylib_paths {
146 let lib = libloading::Library::new(&path).unwrap();
147 let obj = std::fs::read(path).unwrap();
148 let obj = object::File::parse(&obj).unwrap();
149 imported_symbols.extend(obj.dynamic_symbols().filter_map(|(_idx, symbol)| {
150 let name = symbol.name().unwrap().to_string();
151 if name.is_empty() || !symbol.is_global() || symbol.is_undefined() {
154 let dlsym_name = if cfg!(target_os = "macos") {
155 // On macOS `dlsym` expects the name without leading `_`.
156 assert!(name.starts_with("_"), "{:?}", name);
161 let symbol: libloading::Symbol<*const u8> =
162 unsafe { lib.get(dlsym_name.as_bytes()) }.unwrap();
163 Some((name, *symbol))
165 std::mem::forget(lib)
168 tcx.sess.abort_if_errors();
175 metadata: EncodedMetadata,
176 need_metadata_module: bool,
177 ) -> Box<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>)> {
178 let mut work_products = FxHashMap::default();
180 fn new_module(tcx: TyCtxt<'_>, name: String) -> Module<crate::backend::Backend> {
181 let module = crate::backend::make_module(tcx.sess, name);
182 assert_eq!(pointer_ty(tcx), module.target_config().pointer_type());
186 struct ModuleCodegenResult(CompiledModule, Option<(WorkProductId, WorkProduct)>);
188 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
190 impl<HCX> HashStable<HCX> for ModuleCodegenResult {
191 fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
196 fn emit_module<B: Backend>(
200 mut module: Module<B>,
201 debug: Option<DebugContext>,
202 ) -> ModuleCodegenResult
203 where B::Product: Emit + WriteDebugInfo,
205 module.finalize_definitions();
206 let mut product = module.finish();
208 if let Some(mut debug) = debug {
209 debug.emit(&mut product);
213 .output_filenames(LOCAL_CRATE)
214 .temp_path(OutputType::Object, Some(&name));
215 let obj = product.emit();
216 std::fs::write(&tmp_file, obj).unwrap();
218 let work_product = if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() {
221 rustc_incremental::copy_cgu_workproducts_to_incr_comp_cache_dir(
224 &[(WorkProductFileKind::Object, tmp_file.clone())],
232 object: Some(tmp_file),
234 bytecode_compressed: None,
240 let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
242 if tcx.dep_graph.is_fully_enabled() {
244 tcx.codegen_unit(cgu.name());
248 let modules = time(tcx, "codegen mono items", || {
249 cgus.iter().map(|cgu| {
250 let cgu_reuse = determine_cgu_reuse(tcx, cgu);
251 tcx.sess.cgu_reuse_tracker.set_actual_reuse(&cgu.name().as_str(), cgu_reuse);
254 _ if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() => {}
256 CguReuse::PreLto => {
257 let incr_comp_session_dir = tcx.sess.incr_comp_session_dir();
258 let mut object = None;
259 let work_product = cgu.work_product(tcx);
260 for (kind, saved_file) in &work_product.saved_files {
261 let obj_out = match kind {
262 WorkProductFileKind::Object => {
263 let path = tcx.output_filenames(LOCAL_CRATE).temp_path(OutputType::Object, Some(&cgu.name().as_str()));
264 object = Some(path.clone());
267 WorkProductFileKind::Bytecode | WorkProductFileKind::BytecodeCompressed => {
268 panic!("cg_clif doesn't use bytecode");
271 let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, &saved_file);
272 if let Err(err) = rustc_fs_util::link_or_copy(&source_file, &obj_out) {
273 tcx.sess.err(&format!(
274 "unable to copy {} to {}: {}",
275 source_file.display(),
282 work_products.insert(cgu.work_product_id(), work_product);
284 return CompiledModule {
285 name: cgu.name().to_string(),
286 kind: ModuleKind::Regular,
289 bytecode_compressed: None,
292 CguReuse::PostLto => unreachable!(),
295 let dep_node = cgu.codegen_dep_node(tcx);
296 let (ModuleCodegenResult(module, work_product), _) =
297 tcx.dep_graph.with_task(dep_node, tcx, cgu.name(), module_codegen, rustc::dep_graph::hash_result);
299 fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodegenResult {
300 let cgu = tcx.codegen_unit(cgu_name);
301 let mono_items = cgu.items_in_deterministic_order(tcx);
303 let mut module = new_module(tcx, cgu_name.as_str().to_string());
305 let mut debug = if tcx.sess.opts.debuginfo != DebugInfo::None {
306 let debug = DebugContext::new(
308 module.target_config().pointer_type().bytes() as u8,
315 codegen_mono_items(tcx, &mut module, debug.as_mut(), mono_items);
316 crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module);
320 cgu.name().as_str().to_string(),
327 if let Some((id, product)) = work_product {
328 work_products.insert(id, product);
332 }).collect::<Vec<_>>()
335 tcx.sess.abort_if_errors();
337 let mut allocator_module = new_module(tcx, "allocator_shim".to_string());
338 let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module);
340 let allocator_module = if created_alloc_shim {
341 let ModuleCodegenResult(module, work_product) = emit_module(
343 "allocator_shim".to_string(),
344 ModuleKind::Allocator,
348 if let Some((id, product)) = work_product {
349 work_products.insert(id, product);
356 rustc_incremental::assert_dep_graph(tcx);
357 rustc_incremental::save_dep_graph(tcx);
359 let metadata_module = if need_metadata_module {
360 let _timer = tcx.prof.generic_activity("codegen crate metadata");
361 let (metadata_cgu_name, tmp_file) = tcx.sess.time("write compressed metadata", || {
362 use rustc::mir::mono::CodegenUnitNameBuilder;
364 let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
365 let metadata_cgu_name = cgu_name_builder
366 .build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata"))
371 .output_filenames(LOCAL_CRATE)
372 .temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
374 let obj = crate::backend::with_object(tcx.sess, &metadata_cgu_name, |object| {
375 crate::metadata::write_metadata(tcx, object);
378 std::fs::write(&tmp_file, obj).unwrap();
380 (metadata_cgu_name, tmp_file)
383 Some(CompiledModule {
384 name: metadata_cgu_name,
385 kind: ModuleKind::Metadata,
386 object: Some(tmp_file),
388 bytecode_compressed: None,
394 Box::new((CodegenResults {
395 crate_name: tcx.crate_name(LOCAL_CRATE),
399 crate_hash: tcx.crate_hash(LOCAL_CRATE),
401 windows_subsystem: None, // Windows is not yet supported
402 linker_info: LinkerInfo::new(tcx),
403 crate_info: CrateInfo::new(tcx),
407 fn codegen_mono_items<'tcx>(
409 module: &mut Module<impl Backend + 'static>,
410 debug_context: Option<&mut DebugContext<'tcx>>,
411 mono_items: Vec<(MonoItem<'tcx>, (RLinkage, Visibility))>,
413 let mut cx = CodegenCx::new(tcx, module, debug_context);
415 tcx.sess.time("predefine functions", || {
416 for &(mono_item, (linkage, visibility)) in &mono_items {
418 MonoItem::Fn(instance) => {
420 get_function_name_and_sig(tcx, cx.module.isa().triple(), instance, false);
421 let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
422 cx.module.declare_function(&name, linkage, &sig).unwrap();
424 MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {}
429 for (mono_item, (linkage, visibility)) in mono_items {
430 crate::unimpl::try_unimpl(tcx, || {
431 let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
432 trans_mono_item(&mut cx, mono_item, linkage);
436 tcx.sess.time("finalize CodegenCx", || cx.finalize());
439 fn trans_mono_item<'clif, 'tcx, B: Backend + 'static>(
440 cx: &mut crate::CodegenCx<'clif, 'tcx, B>,
441 mono_item: MonoItem<'tcx>,
446 MonoItem::Fn(inst) => {
448 PrintOnPanic(|| format!("{:?} {}", inst, tcx.symbol_name(inst).name.as_str()));
449 debug_assert!(!inst.substs.needs_infer());
450 let _mir_guard = PrintOnPanic(|| {
453 | InstanceDef::DropGlue(_, _)
454 | InstanceDef::Virtual(_, _) => {
455 let mut mir = ::std::io::Cursor::new(Vec::new());
456 crate::rustc_mir::util::write_mir_pretty(
462 String::from_utf8(mir.into_inner()).unwrap()
465 // FIXME fix write_mir_pretty for these instances
466 format!("{:#?}", tcx.instance_mir(inst.def))
471 cx.tcx.sess.time("codegen fn", || crate::base::trans_fn(cx, inst, linkage));
473 MonoItem::Static(def_id) => {
474 crate::constant::codegen_static(&mut cx.constants_cx, def_id);
476 MonoItem::GlobalAsm(hir_id) => {
477 let item = tcx.hir().expect_item(hir_id);
478 if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind {
479 // FIXME implement global asm using an external assembler
480 if asm.as_str().contains("__rust_probestack") {
485 .fatal(&format!("Unimplemented global asm mono item \"{}\"", asm));
488 bug!("Expected GlobalAsm found {:?}", item);
494 fn time<R>(tcx: TyCtxt<'_>, name: &'static str, f: impl FnOnce() -> R) -> R {
495 if std::env::var("CG_CLIF_DISPLAY_CG_TIME").is_ok() {
496 println!("[{:<30}: {}] start", tcx.crate_name(LOCAL_CRATE), name);
497 let before = std::time::Instant::now();
498 let res = tcx.sess.time(name, f);
499 let after = std::time::Instant::now();
500 println!("[{:<30}: {}] end time: {:?}", tcx.crate_name(LOCAL_CRATE), name, after - before);
503 tcx.sess.time(name, f)
507 // Adapted from https://github.com/rust-lang/rust/blob/303d8aff6092709edd4dbd35b1c88e9aa40bf6d8/src/librustc_codegen_ssa/base.rs#L922-L953
508 fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguReuse {
509 if !tcx.dep_graph.is_fully_enabled() {
513 let work_product_id = &cgu.work_product_id();
514 if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
515 // We don't have anything cached for this CGU. This can happen
516 // if the CGU did not exist in the previous session.
520 // Try to mark the CGU as green. If it we can do so, it means that nothing
521 // affecting the LLVM module has changed and we can re-use a cached version.
522 // If we compile with any kind of LTO, this means we can re-use the bitcode
523 // of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
524 // know that later). If we are not doing LTO, there is only one optimized
525 // version of each module, so we re-use that.
526 let dep_node = cgu.codegen_dep_node(tcx);
528 !tcx.dep_graph.dep_node_exists(&dep_node),
529 "CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
533 if tcx.dep_graph.try_mark_green(tcx, &dep_node).is_some() {