Support simplejit and faerie at the same time

This commit is contained in:
bjorn3 2018-08-14 20:58:24 +02:00
parent 178aa32b0c
commit 0f26781a86
4 changed files with 49 additions and 20 deletions

View file

@ -26,6 +26,8 @@ fn start(_main: *const u8, i: isize, _: *const *const u8) -> isize {
puts(ptr);
}
//panic(&("panic msg", "abc.rs", 0, 43));
unsafe {
NUM = 6 * 7 + 1 + (1u8 == 1u8) as u8; // 44
*NUM_REF as isize

View file

@ -1,10 +1,8 @@
use crate::prelude::*;
pub fn trans_mono_item<'a, 'tcx: 'a>(
cx: &mut CodegenCx<'a, 'tcx, CurrentBackend>,
mono_item: MonoItem<'tcx>,
) {
pub fn trans_mono_item<'a, 'tcx: 'a>(cx: &mut CodegenCx<'a, 'tcx>, mono_item: MonoItem<'tcx>) {
let tcx = cx.tcx;
let context = &mut cx.context;
match mono_item {
MonoItem::Fn(inst) => match inst {
@ -20,8 +18,10 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
String::from_utf8_lossy(&mir.into_inner())
));
let func_id = trans_fn(cx.tcx, cx.module, &mut cx.constants, &mut cx.context, inst);
let res = each_module!(cx, |(ccx, m)| trans_fn(tcx, *m, ccx, context, inst));
if let Some(func_id) = res.jit {
cx.defined_functions.push(func_id);
};
}
Instance {
def: InstanceDef::DropGlue(_, _),
@ -30,7 +30,9 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
inst => unimpl!("Unimplemented instance {:?}", inst),
},
MonoItem::Static(def_id) => {
crate::constant::codegen_static(&mut cx.constants, def_id);
each_module!(cx, |(ccx, _m)| {
crate::constant::codegen_static(ccx, def_id);
});
}
MonoItem::GlobalAsm(node_id) => cx
.tcx
@ -41,7 +43,7 @@ pub fn trans_mono_item<'a, 'tcx: 'a>(
fn trans_fn<'a, 'tcx: 'a>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
module: &mut Module<SimpleJITBackend>,
module: &mut Module<impl Backend>,
constants: &mut crate::constant::ConstantCx,
context: &mut Context,
instance: Instance<'tcx>,

View file

@ -6,8 +6,6 @@ use cranelift_module::Module;
use crate::prelude::*;
pub type CurrentBackend = ::cranelift_simplejit::SimpleJITBackend;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Variable(pub Local);

View file

@ -47,6 +47,15 @@ macro_rules! unimpl {
};
}
macro_rules! each_module {
($cx:expr, |$p:pat| $res:expr) => {
ModuleTup {
jit: $cx.jit.as_mut().map(|$p| $res),
faerie: $cx.faerie.as_mut().map(|$p| $res),
}
};
}
mod abi;
mod analyze;
mod base;
@ -88,7 +97,7 @@ mod prelude {
pub use crate::common::Variable;
pub use crate::common::*;
pub use crate::CodegenCx;
pub use crate::{CodegenCx, ModuleTup};
pub fn should_codegen(tcx: TyCtxt) -> bool {
::std::env::var("SHOULD_CODEGEN").is_ok()
@ -96,18 +105,24 @@ mod prelude {
}
}
use crate::constant::ConstantCx;
use crate::prelude::*;
pub struct CodegenCx<'a, 'tcx: 'a, B: Backend + 'a> {
pub struct CodegenCx<'a, 'tcx: 'a> {
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
pub module: &'a mut Module<B>,
pub constants: crate::constant::ConstantCx,
pub jit: Option<(ConstantCx, &'a mut Module<SimpleJITBackend>)>,
pub faerie: Option<(ConstantCx, &'a mut Module<FaerieBackend>)>,
pub defined_functions: Vec<FuncId>,
// Cache
pub context: Context,
}
pub struct ModuleTup<T> {
jit: Option<T>,
faerie: Option<T>,
}
struct CraneliftMetadataLoader;
impl MetadataLoader for CraneliftMetadataLoader {
@ -265,8 +280,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
use std::io::Write;
let mut cx = CodegenCx {
tcx,
module: &mut jit_module,
constants: Default::default(),
jit: Some((ConstantCx::default(), &mut jit_module)),
faerie: Some((ConstantCx::default(), &mut faerie_module)),
defined_functions: Vec::new(),
context: Context::new(),
@ -299,7 +314,18 @@ impl CodegenBackend for CraneliftCodegenBackend {
}
}
cx.constants.finalize(tcx, &mut cx.module);
match cx {
CodegenCx {
tcx,
jit,
faerie,
defined_functions: _,
context: _,
} => {
jit.map(|jit| jit.0.finalize(tcx, jit.1));
faerie.map(|faerie| faerie.0.finalize(tcx, faerie.1));
}
}
let after = ::std::time::Instant::now();
println!("time: {:?}", after - before);
@ -307,6 +333,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
cx.defined_functions
};
tcx.sess.abort_if_errors();
tcx.sess.warn("Compiled everything");
// TODO: this doesn't work most of the time
@ -334,9 +362,8 @@ impl CodegenBackend for CraneliftCodegenBackend {
jit_module.finish();
} else if should_codegen(tcx) {
for func_id in defined_functions {
jit_module.finalize_function(func_id);
}
jit_module.finalize_all();
faerie_module.finalize_all();
tcx.sess.warn("Finalized everything");
}