Re-use cranelift_codegen::ir::Function for every function

Fixes #844
This commit is contained in:
bjorn3 2020-01-04 17:58:38 +01:00
parent c5a7fca527
commit 1bb848d4ac
4 changed files with 17 additions and 29 deletions

View File

@ -21,10 +21,13 @@ pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
.map(|debug_context| FunctionDebugContext::new(debug_context, instance, func_id, &name));
// Make FunctionBuilder
let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
func.collect_debug_info();
let context = &mut cx.cached_context;
context.clear();
context.func.name = ExternalName::user(0, func_id.as_u32());
context.func.signature = sig;
context.func.collect_debug_info();
let mut func_ctx = FunctionBuilderContext::new();
let mut bcx = FunctionBuilder::new(&mut func, &mut func_ctx);
let mut bcx = FunctionBuilder::new(&mut context.func, &mut func_ctx);
// Predefine ebb's
let start_ebb = bcx.create_ebb();
@ -48,7 +51,7 @@ pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
clif_comments,
constants_cx: &mut cx.constants_cx,
caches: &mut cx.caches,
vtables: &mut cx.vtables,
source_info_set: indexmap::IndexSet::new(),
};
@ -69,13 +72,10 @@ pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
let local_map = fx.local_map;
#[cfg(debug_assertions)]
crate::pretty_clif::write_clif_file(cx.tcx, "unopt", instance, &func, &clif_comments, None);
crate::pretty_clif::write_clif_file(cx.tcx, "unopt", instance, &context.func, &clif_comments, None);
// Verify function
verify_func(tcx, &clif_comments, &func);
let context = &mut cx.caches.context;
context.func = func;
verify_func(tcx, &clif_comments, &context.func);
// Perform rust specific optimizations
crate::optimize::optimize_function(cx.tcx, instance, context, &mut clif_comments);

View File

@ -269,7 +269,7 @@ pub struct FunctionCx<'clif, 'tcx, B: Backend + 'static> {
pub clif_comments: crate::pretty_clif::CommentWriter,
pub constants_cx: &'clif mut crate::constant::ConstantCx,
pub caches: &'clif mut Caches<'tcx>,
pub vtables: &'clif mut HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
pub source_info_set: indexmap::IndexSet<SourceInfo>,
}

View File

@ -114,7 +114,7 @@ mod prelude {
pub use crate::trap::*;
pub use crate::unimpl::unimpl;
pub use crate::value_and_place::{CPlace, CPlaceInner, CValue};
pub use crate::{Caches, CodegenCx};
pub use crate::CodegenCx;
pub struct PrintOnPanic<F: Fn() -> String>(pub F);
impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
@ -126,25 +126,12 @@ mod prelude {
}
}
pub struct Caches<'tcx> {
pub context: Context,
pub vtables: HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
}
impl Default for Caches<'_> {
fn default() -> Self {
Caches {
context: Context::new(),
vtables: HashMap::new(),
}
}
}
pub struct CodegenCx<'clif, 'tcx, B: Backend + 'static> {
tcx: TyCtxt<'tcx>,
module: &'clif mut Module<B>,
constants_cx: ConstantCx,
caches: Caches<'tcx>,
cached_context: Context,
vtables: HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
debug_context: Option<&'clif mut DebugContext<'tcx>>,
}
@ -158,7 +145,8 @@ impl<'clif, 'tcx, B: Backend + 'static> CodegenCx<'clif, 'tcx, B> {
tcx,
module,
constants_cx: ConstantCx::default(),
caches: Caches::default(),
cached_context: Context::new(),
vtables: HashMap::new(),
debug_context,
}
}

View File

@ -63,11 +63,11 @@ pub fn get_vtable<'tcx>(
ty: Ty<'tcx>,
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
) -> Value {
let data_id = if let Some(data_id) = fx.caches.vtables.get(&(ty, trait_ref)) {
let data_id = if let Some(data_id) = fx.vtables.get(&(ty, trait_ref)) {
*data_id
} else {
let data_id = build_vtable(fx, ty, trait_ref);
fx.caches.vtables.insert((ty, trait_ref), data_id);
fx.vtables.insert((ty, trait_ref), data_id);
data_id
};