2019-05-04 09:54:25 -05:00
|
|
|
use std::any::Any;
|
|
|
|
use std::ffi::CString;
|
|
|
|
use std::os::raw::{c_char, c_int};
|
|
|
|
|
2020-03-11 09:18:01 -05:00
|
|
|
use rustc::dep_graph::{WorkProduct, WorkProductFileKind, WorkProductId};
|
2019-05-04 09:54:25 -05:00
|
|
|
use rustc::middle::cstore::EncodedMetadata;
|
2020-03-11 09:18:01 -05:00
|
|
|
use rustc::mir::mono::{CodegenUnit, Linkage as RLinkage, Visibility};
|
2019-05-04 09:54:25 -05:00
|
|
|
use rustc::session::config::{DebugInfo, OutputType};
|
2020-03-11 09:18:01 -05:00
|
|
|
use rustc_session::cgu_reuse_tracker::CguReuse;
|
2019-05-04 09:54:25 -05:00
|
|
|
use rustc_codegen_ssa::back::linker::LinkerInfo;
|
|
|
|
use rustc_codegen_ssa::CrateInfo;
|
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-10-18 10:19:13 -05:00
|
|
|
use crate::backend::{Emit, WriteDebugInfo};
|
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
pub fn codegen_crate(
|
|
|
|
tcx: TyCtxt<'_>,
|
2019-05-04 09:54:25 -05:00
|
|
|
metadata: EncodedMetadata,
|
2019-05-11 05:23:40 -05:00
|
|
|
need_metadata_module: bool,
|
2019-05-04 09:54:25 -05:00
|
|
|
) -> Box<dyn Any> {
|
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
|
2019-08-11 10:33:52 -05:00
|
|
|
if std::env::var("SHOULD_RUN").is_ok()
|
|
|
|
&& tcx.sess.crate_types.get().contains(&CrateType::Executable)
|
|
|
|
{
|
2019-05-11 05:23:40 -05:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2019-10-06 10:31:46 -05:00
|
|
|
let _: ! = run_jit(tcx);
|
2019-05-11 05:23:40 -05:00
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
panic!("jit not supported on wasm");
|
|
|
|
}
|
|
|
|
|
2019-10-06 10:31:46 -05:00
|
|
|
run_aot(tcx, metadata, need_metadata_module)
|
2019-05-11 05:23:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2019-10-06 10:31:46 -05:00
|
|
|
fn run_jit(tcx: TyCtxt<'_>) -> ! {
|
2019-05-11 05:23:40 -05:00
|
|
|
use cranelift_simplejit::{SimpleJITBackend, SimpleJITBuilder};
|
|
|
|
|
2020-01-18 05:43:31 -06:00
|
|
|
// Rustc opens us without the RTLD_GLOBAL flag, so __cg_clif_global_atomic_mutex will not be
|
|
|
|
// exported. We fix this by opening ourself again as global.
|
|
|
|
// FIXME remove once atomic_shim is gone
|
|
|
|
let cg_dylib = std::ffi::OsString::from(&tcx.sess.opts.debugging_opts.codegen_backend.as_ref().unwrap());
|
|
|
|
std::mem::forget(libloading::os::unix::Library::open(Some(cg_dylib), libc::RTLD_NOW | libc::RTLD_GLOBAL).unwrap());
|
|
|
|
|
|
|
|
|
2019-08-11 11:17:51 -05:00
|
|
|
let imported_symbols = load_imported_symbols_for_jit(tcx);
|
2019-08-10 09:50:23 -05:00
|
|
|
|
2019-08-11 10:06:18 -05:00
|
|
|
let mut jit_builder = SimpleJITBuilder::with_isa(
|
|
|
|
crate::build_isa(tcx.sess, false),
|
2019-06-08 13:41:15 -05:00
|
|
|
cranelift_module::default_libcall_names(),
|
2019-08-10 09:50:23 -05:00
|
|
|
);
|
|
|
|
jit_builder.symbols(imported_symbols);
|
|
|
|
let mut jit_module: Module<SimpleJITBackend> = Module::new(jit_builder);
|
2019-05-11 05:23:40 -05:00
|
|
|
assert_eq!(pointer_ty(tcx), jit_module.target_config().pointer_type());
|
|
|
|
|
|
|
|
let 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*/
|
|
|
|
)],
|
2019-12-17 08:03:32 -06:00
|
|
|
call_conv: CallConv::triple_default(&crate::target_triple(tcx.sess)),
|
2019-05-11 05:23:40 -05:00
|
|
|
};
|
|
|
|
let main_func_id = jit_module
|
|
|
|
.declare_function("main", Linkage::Import, &sig)
|
|
|
|
.unwrap();
|
|
|
|
|
2020-03-07 04:54:35 -06:00
|
|
|
let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
|
|
|
|
let mono_items = cgus
|
|
|
|
.iter()
|
|
|
|
.map(|cgu| cgu.items_in_deterministic_order(tcx).into_iter())
|
|
|
|
.flatten()
|
2020-03-09 05:21:40 -05:00
|
|
|
.collect::<FxHashMap<_, (_, _)>>()
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<(_, (_, _))>>();
|
2020-03-07 04:54:35 -06:00
|
|
|
|
2020-03-07 05:16:32 -06:00
|
|
|
time(tcx.sess, "codegen mono items", || {
|
|
|
|
codegen_mono_items(tcx, &mut jit_module, None, mono_items);
|
|
|
|
});
|
2020-03-07 04:54:35 -06:00
|
|
|
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module);
|
2019-09-28 04:13:40 -05:00
|
|
|
crate::allocator::codegen(tcx, &mut jit_module);
|
2020-03-07 04:54:35 -06:00
|
|
|
|
2019-05-11 05:23:40 -05:00
|
|
|
jit_module.finalize_definitions();
|
|
|
|
|
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
|
|
|
|
let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id);
|
|
|
|
|
|
|
|
println!("Rustc codegen cranelift will JIT run the executable, because the SHOULD_RUN env var is set");
|
|
|
|
|
|
|
|
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
|
|
|
|
unsafe { ::std::mem::transmute(finalized_main) };
|
|
|
|
|
|
|
|
let args = ::std::env::var("JIT_ARGS").unwrap_or_else(|_| String::new());
|
|
|
|
let args = args
|
|
|
|
.split(" ")
|
|
|
|
.chain(Some(&*tcx.crate_name(LOCAL_CRATE).as_str().to_string()))
|
|
|
|
.map(|arg| CString::new(arg).unwrap())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let argv = args.iter().map(|arg| arg.as_ptr()).collect::<Vec<_>>();
|
|
|
|
// TODO: Rust doesn't care, but POSIX argv has a NULL sentinel at the end
|
|
|
|
|
|
|
|
let ret = f(args.len() as c_int, argv.as_ptr());
|
|
|
|
|
|
|
|
jit_module.finish();
|
|
|
|
std::process::exit(ret);
|
|
|
|
}
|
|
|
|
|
2019-08-11 11:17:51 -05:00
|
|
|
fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> {
|
|
|
|
use rustc::middle::dependency_format::Linkage;
|
|
|
|
|
|
|
|
let mut dylib_paths = Vec::new();
|
|
|
|
|
|
|
|
let crate_info = CrateInfo::new(tcx);
|
2019-09-28 04:13:40 -05:00
|
|
|
let formats = tcx.dependency_formats(LOCAL_CRATE);
|
|
|
|
let data = &formats
|
|
|
|
.iter()
|
|
|
|
.find(|(crate_type, _data)| *crate_type == CrateType::Executable)
|
|
|
|
.unwrap()
|
|
|
|
.1;
|
2019-08-11 11:17:51 -05:00
|
|
|
for &(cnum, _) in &crate_info.used_crates_dynamic {
|
|
|
|
let src = &crate_info.used_crate_source[&cnum];
|
|
|
|
match data[cnum.as_usize() - 1] {
|
|
|
|
Linkage::NotLinked | Linkage::IncludedFromDylib => {}
|
|
|
|
Linkage::Static => {
|
|
|
|
let name = tcx.crate_name(cnum);
|
2019-08-31 12:28:09 -05:00
|
|
|
let mut err = tcx
|
|
|
|
.sess
|
|
|
|
.struct_err(&format!("Can't load static lib {}", name.as_str()));
|
2019-08-11 11:17:51 -05:00
|
|
|
err.note("rustc_codegen_cranelift can only load dylibs in JIT mode.");
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
Linkage::Dynamic => {
|
|
|
|
dylib_paths.push(src.dylib.as_ref().unwrap().0.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut imported_symbols = Vec::new();
|
|
|
|
for path in dylib_paths {
|
|
|
|
use object::Object;
|
|
|
|
let lib = libloading::Library::new(&path).unwrap();
|
|
|
|
let obj = std::fs::read(path).unwrap();
|
|
|
|
let obj = object::File::parse(&obj).unwrap();
|
|
|
|
imported_symbols.extend(obj.dynamic_symbols().filter_map(|(_idx, symbol)| {
|
|
|
|
let name = symbol.name().unwrap().to_string();
|
|
|
|
if name.is_empty() || !symbol.is_global() || symbol.is_undefined() {
|
|
|
|
return None;
|
|
|
|
}
|
2020-02-22 08:57:53 -06:00
|
|
|
let dlsym_name = if cfg!(target_os = "macos") {
|
|
|
|
// On macOS `dlsym` expects the name without leading `_`.
|
|
|
|
assert!(name.starts_with("_"), "{:?}", name);
|
|
|
|
&name[1..]
|
|
|
|
} else {
|
|
|
|
&name
|
|
|
|
};
|
2019-08-11 11:17:51 -05:00
|
|
|
let symbol: libloading::Symbol<*const u8> =
|
2020-02-22 08:57:53 -06:00
|
|
|
unsafe { lib.get(dlsym_name.as_bytes()) }.unwrap();
|
2019-08-11 11:17:51 -05:00
|
|
|
Some((name, *symbol))
|
|
|
|
}));
|
|
|
|
std::mem::forget(lib)
|
|
|
|
}
|
|
|
|
|
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
|
|
|
|
imported_symbols
|
|
|
|
}
|
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
fn run_aot(
|
|
|
|
tcx: TyCtxt<'_>,
|
2019-05-11 05:23:40 -05:00
|
|
|
metadata: EncodedMetadata,
|
2019-05-04 08:26:26 -05:00
|
|
|
need_metadata_module: bool,
|
2020-03-11 09:18:01 -05:00
|
|
|
) -> Box<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>)> {
|
|
|
|
let mut work_products = FxHashMap::default();
|
|
|
|
|
|
|
|
fn new_module(tcx: TyCtxt<'_>, name: String) -> Module<crate::backend::Backend> {
|
2019-10-18 10:19:13 -05:00
|
|
|
let module = crate::backend::make_module(tcx.sess, name);
|
2019-05-11 05:23:40 -05:00
|
|
|
assert_eq!(pointer_ty(tcx), module.target_config().pointer_type());
|
|
|
|
module
|
|
|
|
};
|
|
|
|
|
2020-03-11 09:18:01 -05:00
|
|
|
struct ModuleCodegenResult(CompiledModule, Option<(WorkProductId, WorkProduct)>);
|
|
|
|
|
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
|
|
|
|
|
|
|
impl<HCX> HashStable<HCX> for ModuleCodegenResult {
|
|
|
|
fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 10:19:13 -05:00
|
|
|
fn emit_module<B: Backend>(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
name: String,
|
|
|
|
kind: ModuleKind,
|
|
|
|
mut module: Module<B>,
|
|
|
|
debug: Option<DebugContext>,
|
2020-03-11 09:18:01 -05:00
|
|
|
) -> ModuleCodegenResult
|
2019-10-18 10:19:13 -05:00
|
|
|
where B::Product: Emit + WriteDebugInfo,
|
|
|
|
{
|
2019-09-28 04:13:40 -05:00
|
|
|
module.finalize_definitions();
|
2019-10-18 10:19:13 -05:00
|
|
|
let mut product = module.finish();
|
2019-05-11 05:23:40 -05:00
|
|
|
|
2019-09-28 04:13:40 -05:00
|
|
|
if let Some(mut debug) = debug {
|
2019-10-18 10:19:13 -05:00
|
|
|
debug.emit(&mut product);
|
2019-09-28 04:13:40 -05:00
|
|
|
}
|
2019-05-11 05:23:40 -05:00
|
|
|
|
2019-09-28 04:13:40 -05:00
|
|
|
let tmp_file = tcx
|
|
|
|
.output_filenames(LOCAL_CRATE)
|
2019-10-18 10:19:13 -05:00
|
|
|
.temp_path(OutputType::Object, Some(&name));
|
|
|
|
let obj = product.emit();
|
2019-09-28 04:13:40 -05:00
|
|
|
std::fs::write(&tmp_file, obj).unwrap();
|
2020-03-11 09:18:01 -05:00
|
|
|
|
|
|
|
let work_product = if std::env::var("CG_CLIF_INCR_CACHE").is_ok() {
|
|
|
|
rustc_incremental::copy_cgu_workproducts_to_incr_comp_cache_dir(
|
|
|
|
tcx.sess,
|
|
|
|
&name,
|
|
|
|
&[(WorkProductFileKind::Object, tmp_file.clone())],
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
ModuleCodegenResult(
|
|
|
|
CompiledModule {
|
|
|
|
name,
|
|
|
|
kind,
|
|
|
|
object: Some(tmp_file),
|
|
|
|
bytecode: None,
|
|
|
|
bytecode_compressed: None,
|
|
|
|
},
|
|
|
|
work_product,
|
|
|
|
)
|
2019-09-28 04:13:40 -05:00
|
|
|
};
|
2019-05-11 05:23:40 -05:00
|
|
|
|
2020-03-07 04:54:35 -06:00
|
|
|
let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
|
|
|
|
|
2020-03-11 09:18:01 -05:00
|
|
|
if tcx.dep_graph.is_fully_enabled() {
|
|
|
|
for cgu in &*cgus {
|
|
|
|
tcx.codegen_unit(cgu.name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let modules = time(tcx.sess, "codegen mono items", || {
|
2020-03-09 05:21:40 -05:00
|
|
|
cgus.iter().map(|cgu| {
|
2020-03-11 09:18:01 -05:00
|
|
|
let cgu_reuse = determine_cgu_reuse(tcx, cgu);
|
|
|
|
tcx.sess.cgu_reuse_tracker.set_actual_reuse(&cgu.name().as_str(), cgu_reuse);
|
|
|
|
|
|
|
|
match cgu_reuse {
|
|
|
|
CguReuse::No => {}
|
|
|
|
CguReuse::PreLto => {
|
|
|
|
let incr_comp_session_dir = tcx.sess.incr_comp_session_dir();
|
|
|
|
let mut object = None;
|
|
|
|
let work_product = cgu.work_product(tcx);
|
|
|
|
for (kind, saved_file) in &work_product.saved_files {
|
|
|
|
let obj_out = match kind {
|
|
|
|
WorkProductFileKind::Object => {
|
|
|
|
let path = tcx.output_filenames(LOCAL_CRATE).temp_path(OutputType::Object, Some(&cgu.name().as_str()));
|
|
|
|
object = Some(path.clone());
|
|
|
|
path
|
|
|
|
}
|
|
|
|
WorkProductFileKind::Bytecode | WorkProductFileKind::BytecodeCompressed => {
|
|
|
|
panic!("cg_clif doesn't use bytecode");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, &saved_file);
|
|
|
|
if let Err(err) = rustc_fs_util::link_or_copy(&source_file, &obj_out) {
|
|
|
|
tcx.sess.err(&format!(
|
|
|
|
"unable to copy {} to {}: {}",
|
|
|
|
source_file.display(),
|
|
|
|
obj_out.display(),
|
|
|
|
err
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
work_products.insert(cgu.work_product_id(), work_product);
|
2019-05-11 05:23:40 -05:00
|
|
|
|
2020-03-11 09:18:01 -05:00
|
|
|
return CompiledModule {
|
|
|
|
name: cgu.name().to_string(),
|
|
|
|
kind: ModuleKind::Regular,
|
|
|
|
object,
|
|
|
|
bytecode: None,
|
|
|
|
bytecode_compressed: None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
CguReuse::PostLto => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
let dep_node = cgu.codegen_dep_node(tcx);
|
|
|
|
let (ModuleCodegenResult(module, work_product), _) =
|
|
|
|
tcx.dep_graph.with_task(dep_node, tcx, cgu.name(), module_codegen, rustc::dep_graph::hash_result);
|
2020-03-07 05:45:55 -06:00
|
|
|
|
2020-03-11 09:18:01 -05:00
|
|
|
fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodegenResult {
|
|
|
|
let cgu = tcx.codegen_unit(cgu_name);
|
|
|
|
let mono_items = cgu.items_in_deterministic_order(tcx);
|
|
|
|
|
|
|
|
let mut module = new_module(tcx, cgu_name.as_str().to_string());
|
|
|
|
|
|
|
|
let mut debug = if tcx.sess.opts.debuginfo != DebugInfo::None {
|
|
|
|
let debug = DebugContext::new(
|
|
|
|
tcx,
|
|
|
|
module.target_config().pointer_type().bytes() as u8,
|
|
|
|
);
|
|
|
|
Some(debug)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
codegen_mono_items(tcx, &mut module, debug.as_mut(), mono_items);
|
|
|
|
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module);
|
|
|
|
|
|
|
|
emit_module(
|
2020-03-09 05:21:40 -05:00
|
|
|
tcx,
|
2020-03-11 09:18:01 -05:00
|
|
|
cgu.name().as_str().to_string(),
|
|
|
|
ModuleKind::Regular,
|
|
|
|
module,
|
|
|
|
debug,
|
|
|
|
)
|
|
|
|
}
|
2020-03-08 07:27:43 -05:00
|
|
|
|
2020-03-11 09:18:01 -05:00
|
|
|
if let Some((id, product)) = work_product {
|
|
|
|
work_products.insert(id, product);
|
|
|
|
}
|
2020-03-09 05:21:40 -05:00
|
|
|
|
2020-03-11 09:18:01 -05:00
|
|
|
module
|
2020-03-09 05:21:40 -05:00
|
|
|
}).collect::<Vec<_>>()
|
|
|
|
});
|
2020-03-07 05:45:05 -06:00
|
|
|
|
2019-05-11 05:23:40 -05:00
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
|
2020-03-11 09:18:01 -05:00
|
|
|
let mut allocator_module = new_module(tcx, "allocator_shim".to_string());
|
2019-09-28 04:13:40 -05:00
|
|
|
let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module);
|
2019-05-11 05:23:40 -05:00
|
|
|
|
2020-03-11 09:18:01 -05:00
|
|
|
let allocator_module = if created_alloc_shim {
|
|
|
|
let ModuleCodegenResult(module, work_product) = emit_module(
|
|
|
|
tcx,
|
|
|
|
"allocator_shim".to_string(),
|
|
|
|
ModuleKind::Allocator,
|
|
|
|
allocator_module,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
if let Some((id, product)) = work_product {
|
|
|
|
work_products.insert(id, product);
|
|
|
|
}
|
|
|
|
Some(module)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2019-05-11 05:23:40 -05:00
|
|
|
rustc_incremental::assert_dep_graph(tcx);
|
|
|
|
rustc_incremental::save_dep_graph(tcx);
|
|
|
|
|
2019-05-04 08:26:26 -05:00
|
|
|
let metadata_module = if need_metadata_module {
|
2019-10-03 10:22:01 -05:00
|
|
|
let _timer = tcx.prof.generic_activity("codegen crate metadata");
|
2020-01-06 13:11:03 -06:00
|
|
|
let (metadata_cgu_name, tmp_file) = tcx.sess.time("write compressed metadata", || {
|
2019-09-22 09:47:45 -05:00
|
|
|
use rustc::mir::mono::CodegenUnitNameBuilder;
|
|
|
|
|
|
|
|
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
|
|
|
|
let metadata_cgu_name = cgu_name_builder
|
|
|
|
.build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata"))
|
|
|
|
.as_str()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let tmp_file = tcx
|
|
|
|
.output_filenames(LOCAL_CRATE)
|
|
|
|
.temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
|
|
|
|
|
2019-10-18 10:19:13 -05:00
|
|
|
let obj = crate::backend::with_object(tcx.sess, &metadata_cgu_name, |object| {
|
|
|
|
crate::metadata::write_metadata(tcx, object);
|
|
|
|
});
|
|
|
|
|
2019-09-22 09:47:45 -05:00
|
|
|
std::fs::write(&tmp_file, obj).unwrap();
|
|
|
|
|
|
|
|
(metadata_cgu_name, tmp_file)
|
|
|
|
});
|
2019-03-27 13:24:42 -05:00
|
|
|
|
2019-05-04 08:26:26 -05:00
|
|
|
Some(CompiledModule {
|
2019-03-27 13:24:42 -05:00
|
|
|
name: metadata_cgu_name,
|
|
|
|
kind: ModuleKind::Metadata,
|
|
|
|
object: Some(tmp_file),
|
|
|
|
bytecode: None,
|
|
|
|
bytecode_compressed: None,
|
2019-05-04 08:26:26 -05:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
2019-03-27 13:24:42 -05:00
|
|
|
};
|
|
|
|
|
2020-03-11 09:18:01 -05:00
|
|
|
Box::new((CodegenResults {
|
2019-05-11 05:23:40 -05:00
|
|
|
crate_name: tcx.crate_name(LOCAL_CRATE),
|
2020-03-07 05:45:05 -06:00
|
|
|
modules,
|
2020-03-11 09:18:01 -05:00
|
|
|
allocator_module,
|
2019-05-04 08:26:26 -05:00
|
|
|
metadata_module,
|
2019-05-11 05:23:40 -05:00
|
|
|
crate_hash: tcx.crate_hash(LOCAL_CRATE),
|
|
|
|
metadata,
|
|
|
|
windows_subsystem: None, // Windows is not yet supported
|
|
|
|
linker_info: LinkerInfo::new(tcx),
|
|
|
|
crate_info: CrateInfo::new(tcx),
|
2020-03-11 09:18:01 -05:00
|
|
|
}, work_products))
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
fn codegen_mono_items<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-05-04 09:54:25 -05:00
|
|
|
module: &mut Module<impl Backend + 'static>,
|
|
|
|
debug_context: Option<&mut DebugContext<'tcx>>,
|
2020-03-09 05:21:40 -05:00
|
|
|
mono_items: Vec<(MonoItem<'tcx>, (RLinkage, Visibility))>,
|
2019-05-04 09:54:25 -05:00
|
|
|
) {
|
|
|
|
let mut cx = CodegenCx::new(tcx, module, debug_context);
|
2019-10-04 07:39:14 -05:00
|
|
|
|
2020-03-07 05:16:32 -06:00
|
|
|
tcx.sess.time("predefine functions", || {
|
2020-03-09 05:21:40 -05:00
|
|
|
for &(mono_item, (linkage, visibility)) in &mono_items {
|
2020-03-07 05:16:32 -06:00
|
|
|
match mono_item {
|
|
|
|
MonoItem::Fn(instance) => {
|
|
|
|
let (name, sig) =
|
|
|
|
get_function_name_and_sig(tcx, cx.module.isa().triple(), instance, false);
|
|
|
|
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
|
|
|
|
cx.module.declare_function(&name, linkage, &sig).unwrap();
|
2019-10-04 07:39:14 -05:00
|
|
|
}
|
2020-03-07 05:16:32 -06:00
|
|
|
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {}
|
2019-10-04 07:39:14 -05:00
|
|
|
}
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
|
|
|
});
|
2020-03-07 05:16:32 -06:00
|
|
|
|
|
|
|
for (mono_item, (linkage, visibility)) in mono_items {
|
|
|
|
crate::unimpl::try_unimpl(tcx, || {
|
|
|
|
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
|
|
|
|
trans_mono_item(&mut cx, mono_item, linkage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tcx.sess.time("finalize CodegenCx", || cx.finalize());
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
fn trans_mono_item<'clif, 'tcx, B: Backend + 'static>(
|
2019-06-13 13:44:40 -05:00
|
|
|
cx: &mut crate::CodegenCx<'clif, 'tcx, B>,
|
2019-05-04 09:54:25 -05:00
|
|
|
mono_item: MonoItem<'tcx>,
|
|
|
|
linkage: Linkage,
|
|
|
|
) {
|
|
|
|
let tcx = cx.tcx;
|
|
|
|
match mono_item {
|
|
|
|
MonoItem::Fn(inst) => {
|
|
|
|
let _inst_guard =
|
2019-09-07 03:51:00 -05:00
|
|
|
PrintOnPanic(|| format!("{:?} {}", inst, tcx.symbol_name(inst).name.as_str()));
|
2019-05-04 09:54:25 -05:00
|
|
|
debug_assert!(!inst.substs.needs_infer());
|
|
|
|
let _mir_guard = PrintOnPanic(|| {
|
|
|
|
match inst.def {
|
|
|
|
InstanceDef::Item(_)
|
|
|
|
| InstanceDef::DropGlue(_, _)
|
2019-06-08 13:41:15 -05:00
|
|
|
| InstanceDef::Virtual(_, _) => {
|
2019-05-04 09:54:25 -05:00
|
|
|
let mut mir = ::std::io::Cursor::new(Vec::new());
|
|
|
|
crate::rustc_mir::util::write_mir_pretty(
|
|
|
|
tcx,
|
|
|
|
Some(inst.def_id()),
|
|
|
|
&mut mir,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
String::from_utf8(mir.into_inner()).unwrap()
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// FIXME fix write_mir_pretty for these instances
|
|
|
|
format!("{:#?}", tcx.instance_mir(inst.def))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-10 07:15:14 -06:00
|
|
|
cx.tcx.sess.time("codegen fn", || crate::base::trans_fn(cx, inst, linkage));
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
|
|
|
MonoItem::Static(def_id) => {
|
2019-08-18 09:52:07 -05:00
|
|
|
crate::constant::codegen_static(&mut cx.constants_cx, def_id);
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
2020-01-25 10:24:45 -06:00
|
|
|
MonoItem::GlobalAsm(hir_id) => {
|
|
|
|
let item = tcx.hir().expect_item(hir_id);
|
|
|
|
if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind {
|
|
|
|
// FIXME implement global asm using an external assembler
|
|
|
|
if asm.as_str().contains("__rust_probestack") {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
tcx
|
|
|
|
.sess
|
|
|
|
.fatal(&format!("Unimplemented global asm mono item \"{}\"", asm));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bug!("Expected GlobalAsm found {:?}", item);
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 07:27:26 -06:00
|
|
|
fn time<R>(sess: &Session, name: &'static str, f: impl FnOnce() -> R) -> R {
|
2019-05-04 09:54:25 -05:00
|
|
|
println!("[{}] start", name);
|
|
|
|
let before = std::time::Instant::now();
|
2020-01-10 07:15:14 -06:00
|
|
|
let res = sess.time(name, f);
|
2019-05-04 09:54:25 -05:00
|
|
|
let after = std::time::Instant::now();
|
|
|
|
println!("[{}] end time: {:?}", name, after - before);
|
|
|
|
res
|
|
|
|
}
|
2020-03-11 09:18:01 -05:00
|
|
|
|
|
|
|
// Adapted from https://github.com/rust-lang/rust/blob/303d8aff6092709edd4dbd35b1c88e9aa40bf6d8/src/librustc_codegen_ssa/base.rs#L922-L953
|
|
|
|
fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguReuse {
|
|
|
|
if !tcx.dep_graph.is_fully_enabled() {
|
|
|
|
return CguReuse::No;
|
|
|
|
}
|
|
|
|
|
|
|
|
let work_product_id = &cgu.work_product_id();
|
|
|
|
if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
|
|
|
|
// We don't have anything cached for this CGU. This can happen
|
|
|
|
// if the CGU did not exist in the previous session.
|
|
|
|
return CguReuse::No;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to mark the CGU as green. If it we can do so, it means that nothing
|
|
|
|
// affecting the LLVM module has changed and we can re-use a cached version.
|
|
|
|
// If we compile with any kind of LTO, this means we can re-use the bitcode
|
|
|
|
// of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
|
|
|
|
// know that later). If we are not doing LTO, there is only one optimized
|
|
|
|
// version of each module, so we re-use that.
|
|
|
|
let dep_node = cgu.codegen_dep_node(tcx);
|
|
|
|
assert!(
|
|
|
|
!tcx.dep_graph.dep_node_exists(&dep_node),
|
|
|
|
"CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
|
|
|
|
cgu.name()
|
|
|
|
);
|
|
|
|
|
|
|
|
if tcx.dep_graph.try_mark_green(tcx, &dep_node).is_some() {
|
|
|
|
CguReuse::PreLto
|
|
|
|
} else {
|
|
|
|
CguReuse::No
|
|
|
|
}
|
|
|
|
}
|