]> git.lizzy.rs Git - rust.git/commitdiff
Re-use cranelift_codegen::ir::Function for every function
authorbjorn3 <bjorn3@users.noreply.github.com>
Sat, 4 Jan 2020 16:58:38 +0000 (17:58 +0100)
committerbjorn3 <bjorn3@users.noreply.github.com>
Sat, 4 Jan 2020 16:58:38 +0000 (17:58 +0100)
Fixes #844

src/base.rs
src/common.rs
src/lib.rs
src/vtable.rs

index b0b92a72cf87fc934ebdc5050cab56a4ff3911c4..0d71438cee92a1c05dffe90fd78f5f66ad05e998 100644 (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);
index e5b932947ccc5b4398e9421e658e1bd2485ea6ac..3543df9cb865e20705671d30d41e63543fae098d 100644 (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>,
 }
index c0a2433ceeb4bc421e933d1ce96bf123ffc4e0b1..c318b868ff5046b6cc659ffa88a7098f8f28e986 100644 (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 @@ fn drop(&mut self) {
     }
 }
 
-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 @@ fn new(
             tcx,
             module,
             constants_cx: ConstantCx::default(),
-            caches: Caches::default(),
+            cached_context: Context::new(),
+            vtables: HashMap::new(),
             debug_context,
         }
     }
index c12dff3cdc6b3a6c01515ec02c2072a2a111517d..913bb712da3cb9ab47e9334d1d53c60c6c7685dc 100644 (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
     };