From 7f2a887c838754c0fa392c690ff8321d5dfeb0bb Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 15 Aug 2018 14:45:32 +0200 Subject: [PATCH] Use either SimpleJIT or faerie, but not both --- src/abi.rs | 6 ++++-- src/lib.rs | 48 ++++++++++++++++++++++++++++++------------------ src/metadata.rs | 12 ++++++++---- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/abi.rs b/src/abi.rs index 9c8d1a13a2e..55fcc277a9a 100644 --- a/src/abi.rs +++ b/src/abi.rs @@ -336,7 +336,8 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>( .unwrap() .contains(crate::analyze::Flags::NOT_SSA) { - fx.bcx.declare_var(mir_var(local), fx.cton_type(ty).unwrap()); + fx.bcx + .declare_var(mir_var(local), fx.cton_type(ty).unwrap()); match get_pass_mode(fx.tcx, fx.self_sig().abi, ty, false) { PassMode::NoPass => unimplemented!("pass mode nopass"), PassMode::ByVal(_) => fx.bcx.def_var(mir_var(local), ebb_param), @@ -401,7 +402,8 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>( }); CPlace::from_stack_slot(fx, stack_slot, ty) } else { - fx.bcx.declare_var(mir_var(local), fx.cton_type(ty).unwrap()); + fx.bcx + .declare_var(mir_var(local), fx.cton_type(ty).unwrap()); CPlace::Var(local, layout) }; diff --git a/src/lib.rs b/src/lib.rs index 22089a1241f..1cf5abd873d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,8 +60,8 @@ mod analyze; mod base; mod common; mod constant; -mod pretty_clif; mod metadata; +mod pretty_clif; mod prelude { pub use std::any::Any; @@ -222,22 +222,31 @@ impl CodegenBackend for CraneliftCodegenBackend { let isa = cranelift::codegen::isa::lookup(target_lexicon::Triple::host()) .unwrap() .finish(flags); - let mut jit_module: Module = Module::new(SimpleJITBuilder::new()); - let mut faerie_module: Module = Module::new( - FaerieBuilder::new( - isa, - "some_file.o".to_string(), - FaerieTrapCollection::Disabled, - FaerieBuilder::default_libcall_names(), - ).unwrap(), - ); + let (mut jit_module, mut faerie_module): ( + Option>, + Option>, + ) = if std::env::var("SHOULD_RUN").is_ok() { + (Some(Module::new(SimpleJITBuilder::new())), None) + } else { + ( + None, + Some(Module::new( + FaerieBuilder::new( + isa, + "some_file.o".to_string(), + FaerieTrapCollection::Disabled, + FaerieBuilder::default_libcall_names(), + ).unwrap(), + )), + ) + }; let defined_functions = { use std::io::Write; let mut cx = CodegenCx { tcx, - jit: Some((ConstantCx::default(), &mut jit_module)), - faerie: Some((ConstantCx::default(), &mut faerie_module)), + jit: jit_module.as_mut().map(|m| (ConstantCx::default(), m)), + faerie: faerie_module.as_mut().map(|m| (ConstantCx::default(), m)), defined_functions: Vec::new(), context: Context::new(), @@ -294,7 +303,7 @@ impl CodegenBackend for CraneliftCodegenBackend { tcx.sess.warn("Compiled everything"); // TODO: this doesn't work most of the time - if std::env::var("SHOULD_RUN").is_ok() { + if let Some(mut jit_module) = jit_module { tcx.sess.warn("Rustc codegen cranelift will JIT run the executable, because the SHOULD_RUN env var is set"); let start_wrapper = tcx.lang_items().start_fn().expect("no start lang item"); @@ -320,14 +329,13 @@ impl CodegenBackend for CraneliftCodegenBackend { jit_module.finish(); ::std::process::exit(0); } else if should_codegen(tcx.sess) { - jit_module.finalize_all(); - faerie_module.finalize_all(); + faerie_module.as_mut().map(|m| m.finalize_all()); tcx.sess.warn("Finalized everything"); } Box::new(OngoingCodegen { - product: faerie_module.finish(), + product: faerie_module.unwrap().finish(), metadata: metadata.raw_data, crate_name: tcx.crate_name(LOCAL_CRATE), crate_hash: tcx.crate_hash(LOCAL_CRATE), @@ -348,7 +356,8 @@ impl CodegenBackend for CraneliftCodegenBackend { let mut artifact = ongoing_codegen.product.artifact; let metadata = ongoing_codegen.metadata; - let metadata_name = ".rustc.clif_metadata".to_string() + &ongoing_codegen.crate_hash.to_string(); + let metadata_name = + ".rustc.clif_metadata".to_string() + &ongoing_codegen.crate_hash.to_string(); artifact .declare_with( &metadata_name, @@ -373,7 +382,10 @@ impl CodegenBackend for CraneliftCodegenBackend { let mut builder = ar::Builder::new(file); builder .append( - &ar::Header::new(metadata_name.as_bytes().to_vec(), metadata.len() as u64), + &ar::Header::new( + metadata_name.as_bytes().to_vec(), + metadata.len() as u64, + ), ::std::io::Cursor::new(metadata.clone()), ).unwrap(); if should_codegen(sess) { diff --git a/src/metadata.rs b/src/metadata.rs index 384f59f171a..29bb1c8e325 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -1,7 +1,7 @@ -use std::path::Path; -use std::fs::File; -use rustc_data_structures::owning_ref::{self, OwningRef}; use rustc::middle::cstore::MetadataLoader; +use rustc_data_structures::owning_ref::{self, OwningRef}; +use std::fs::File; +use std::path::Path; pub struct CraneliftMetadataLoader; @@ -15,7 +15,11 @@ impl MetadataLoader for CraneliftMetadataLoader { // Iterate over all entries in the archive: while let Some(entry_result) = archive.next_entry() { let mut entry = entry_result.map_err(|e| format!("{:?}", e))?; - if entry.header().identifier().starts_with(b".rustc.clif_metadata") { + if entry + .header() + .identifier() + .starts_with(b".rustc.clif_metadata") + { let mut buf = Vec::new(); ::std::io::copy(&mut entry, &mut buf).map_err(|e| format!("{:?}", e))?; let buf: OwningRef, [u8]> = OwningRef::new(buf).into();