]> git.lizzy.rs Git - rust.git/commitdiff
Preparing the generalization of base:compile_coodegen_unit
authorDenis Merigoux <denis.merigoux@gmail.com>
Wed, 26 Sep 2018 15:00:01 +0000 (17:00 +0200)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Fri, 16 Nov 2018 12:38:15 +0000 (14:38 +0200)
src/librustc_codegen_llvm/base.rs
src/librustc_codegen_llvm/consts.rs
src/librustc_codegen_llvm/context.rs
src/librustc_codegen_llvm/debuginfo/mod.rs
src/librustc_codegen_llvm/interfaces/backend.rs
src/librustc_codegen_llvm/interfaces/debuginfo.rs
src/librustc_codegen_llvm/interfaces/misc.rs
src/librustc_codegen_llvm/interfaces/mod.rs
src/librustc_codegen_llvm/interfaces/statics.rs
src/librustc_codegen_llvm/lib.rs
src/librustc_codegen_llvm/mono_item.rs

index 5fd413b4c7babf1d33bb5ec4fa3376c696e97393..74432870705a0bd7e8b8c4ee8138e50031ef6dc7 100644 (file)
@@ -27,6 +27,7 @@
 use super::ModuleCodegen;
 use super::ModuleKind;
 use super::CachedModuleCodegen;
+use super::LlvmCodegenBackend;
 
 use abi;
 use back::write;
@@ -53,8 +54,6 @@
 use callee;
 use rustc_mir::monomorphize::item::DefPathBasedNames;
 use common::{self, IntPredicate, RealPredicate, TypeKind};
-use context::CodegenCx;
-use debuginfo;
 use meth;
 use mir;
 use monomorphize::Instance;
@@ -968,7 +967,7 @@ fn drop(&mut self) {
     }
 }
 
-fn assert_and_save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
+fn assert_and_save_dep_graph<'ll, 'tcx>(tcx: TyCtxt<'ll, 'tcx, 'tcx>) {
     time(tcx.sess,
          "assert dep graph",
          || rustc_incremental::assert_dep_graph(tcx));
@@ -1067,7 +1066,7 @@ fn load_wasm_imports(&mut self, tcx: TyCtxt, cnum: CrateNum) {
     }
 }
 
-fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
+fn compile_codegen_unit<'ll, 'tcx>(tcx: TyCtxt<'ll, 'tcx, 'tcx>,
                                   cgu_name: InternedString)
                                   -> Stats {
     let start_time = Instant::now();
@@ -1089,26 +1088,26 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                                            cost);
     return stats;
 
-    fn module_codegen<'a, 'tcx>(
-        tcx: TyCtxt<'a, 'tcx, 'tcx>,
+    fn module_codegen<'ll, 'tcx>(
+        tcx: TyCtxt<'ll, 'tcx, 'tcx>,
         cgu_name: InternedString)
         -> (Stats, ModuleCodegen<ModuleLlvm>)
     {
+        let backend = LlvmCodegenBackend(());
         let cgu = tcx.codegen_unit(cgu_name);
-
         // Instantiate monomorphizations without filling out definitions yet...
-        let llvm_module = ModuleLlvm::new(tcx.sess, &cgu_name.as_str());
+        let llvm_module = backend.new_metadata(tcx.sess, &cgu_name.as_str());
         let stats = {
-            let cx = CodegenCx::new(tcx, cgu, &llvm_module);
+            let cx = backend.new_codegen_context(tcx, cgu, &llvm_module);
             let mono_items = cx.codegen_unit
                                .items_in_deterministic_order(cx.tcx);
             for &(mono_item, (linkage, visibility)) in &mono_items {
-                mono_item.predefine(&cx, linkage, visibility);
+                mono_item.predefine::<Builder<&Value>>(&cx, linkage, visibility);
             }
 
             // ... and now that we have everything pre-defined, fill out those definitions.
             for &(mono_item, _) in &mono_items {
-                mono_item.define(&cx);
+                mono_item.define::<Builder<&Value>>(&cx);
             }
 
             // If this codegen unit contains the main function, also create the
@@ -1116,40 +1115,22 @@ fn module_codegen<'a, 'tcx>(
             maybe_create_entry_wrapper::<Builder<&Value>>(&cx);
 
             // Run replace-all-uses-with for statics that need it
-            for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
-                unsafe {
-                    let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
-                    llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
-                    llvm::LLVMDeleteGlobal(old_g);
-                }
+            for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
+                cx.static_replace_all_uses(old_g, new_g)
             }
 
             // Create the llvm.used variable
             // This variable has type [N x i8*] and is stored in the llvm.metadata section
-            if !cx.used_statics.borrow().is_empty() {
-                let name = const_cstr!("llvm.used");
-                let section = const_cstr!("llvm.metadata");
-                let array = cx.const_array(
-                    &cx.type_ptr_to(cx.type_i8()),
-                    &*cx.used_statics.borrow()
-                );
-
-                unsafe {
-                    let g = llvm::LLVMAddGlobal(cx.llmod,
-                                                cx.val_ty(array),
-                                                name.as_ptr());
-                    llvm::LLVMSetInitializer(g, array);
-                    llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
-                    llvm::LLVMSetSection(g, section.as_ptr());
-                }
+            if !cx.used_statics().borrow().is_empty() {
+                cx.create_used_variable()
             }
 
             // Finalize debuginfo
             if cx.sess().opts.debuginfo != DebugInfo::None {
-                debuginfo::finalize(&cx);
+                cx.debuginfo_finalize();
             }
 
-            cx.stats.into_inner()
+            cx.consume_stats().into_inner()
         };
 
         (stats, ModuleCodegen {
index aef13a208772b1c9c938b84a99df21857300d45e..cbbda28994b2f8fb1d650245e5840540ec8b6907 100644 (file)
@@ -443,4 +443,11 @@ fn codegen_static(
             }
         }
     }
+    fn static_replace_all_uses(&self, old_g: &'ll Value, new_g: &'ll Value) {
+        unsafe {
+            let bitcast = llvm::LLVMConstPointerCast(new_g, self.val_ty(old_g));
+            llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
+            llvm::LLVMDeleteGlobal(old_g);
+        }
+    }
 }
index af75c15e26020e1f6ae6cd50975ed29892800bc4..b0d153f8efc165c3caf799c75ef4d8ade39c96c3 100644 (file)
@@ -421,10 +421,22 @@ fn stats(&self) -> &RefCell<Stats> {
         &self.stats
     }
 
+    fn consume_stats(self) -> RefCell<Stats> {
+        self.stats
+    }
+
     fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>> {
         &self.codegen_unit
     }
 
+    fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
+        &self.statics_to_rauw
+    }
+
+    fn used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
+        &self.used_statics
+    }
+
     fn set_frame_pointer_elimination(&self, llfn: &'ll Value) {
         attributes::set_frame_pointer_elimination(self, llfn)
     }
@@ -432,6 +444,25 @@ fn set_frame_pointer_elimination(&self, llfn: &'ll Value) {
     fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
         attributes::apply_target_cpu_attr(self, llfn)
     }
+
+
+    fn create_used_variable(&self) {
+        let name = const_cstr!("llvm.used");
+        let section = const_cstr!("llvm.metadata");
+        let array = self.const_array(
+            &self.type_ptr_to(self.type_i8()),
+            &*self.used_statics.borrow()
+        );
+
+        unsafe {
+            let g = llvm::LLVMAddGlobal(self.llmod,
+                                        self.val_ty(array),
+                                        name.as_ptr());
+            llvm::LLVMSetInitializer(g, array);
+            llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
+            llvm::LLVMSetSection(g, section.as_ptr());
+        }
+    }
 }
 
 impl IntrinsicDeclarationMethods<'tcx> for CodegenCx<'b, 'tcx> {
index eef3ddb24a7f32a4b42d02fda81bd4d5a00288f8..8ef7350747d4f183617189bf770e312278047a52 100644 (file)
@@ -585,4 +585,8 @@ fn extend_scope_to_file(
     ) -> &'ll DILexicalBlock {
         metadata::extend_scope_to_file(&self, scope_metadata, file, defining_crate)
     }
+
+    fn debuginfo_finalize(&self) {
+        finalize(self)
+    }
 }
index a5df7e5af6ff5a04980143690e08f592a5d0d527..b34bb00682f6b7c082afc92e2159e8f50f254b03 100644 (file)
 use rustc::ty::layout::{HasTyCtxt, LayoutOf, TyLayout};
 use rustc::ty::Ty;
 
-use super::CodegenObject;
+use super::{CodegenMethods, CodegenObject};
+use monomorphize::partitioning::CodegenUnit;
 use rustc::middle::allocator::AllocatorKind;
 use rustc::middle::cstore::EncodedMetadata;
 use rustc::session::Session;
 use rustc::ty::TyCtxt;
 use std::any::Any;
 use std::sync::mpsc::Receiver;
+use std::sync::Arc;
 use time_graph::TimeGraph;
 use ModuleCodegen;
 
@@ -40,16 +42,16 @@ impl<'tcx, T> Backend<'tcx> for T where
 {}
 
 pub trait BackendMethods {
-    type Metadata;
+    type Module;
     type OngoingCodegen;
 
-    fn new_metadata(&self, sess: &Session, mod_name: &str) -> Self::Metadata;
-    fn write_metadata<'a, 'gcx>(
+    fn new_metadata(&self, sess: &Session, mod_name: &str) -> Self::Module;
+    fn write_metadata<'b, 'gcx>(
         &self,
-        tcx: TyCtxt<'a, 'gcx, 'gcx>,
-        metadata: &Self::Metadata,
+        tcx: TyCtxt<'b, 'gcx, 'gcx>,
+        metadata: &Self::Module,
     ) -> EncodedMetadata;
-    fn codegen_allocator(&self, tcx: TyCtxt, mods: &Self::Metadata, kind: AllocatorKind);
+    fn codegen_allocator(&self, tcx: TyCtxt, mods: &Self::Module, kind: AllocatorKind);
 
     fn start_async_codegen(
         &self,
@@ -63,10 +65,21 @@ fn submit_pre_codegened_module_to_llvm(
         &self,
         codegen: &Self::OngoingCodegen,
         tcx: TyCtxt,
-        module: ModuleCodegen<Self::Metadata>,
+        module: ModuleCodegen<Self::Module>,
     );
     fn codegen_aborted(codegen: Self::OngoingCodegen);
     fn codegen_finished(&self, codegen: &Self::OngoingCodegen, tcx: TyCtxt);
     fn check_for_errors(&self, codegen: &Self::OngoingCodegen, sess: &Session);
     fn wait_for_signal_to_codegen_item(&self, codegen: &Self::OngoingCodegen);
 }
+
+pub trait BackendCodegenCxMethods<'a, 'tcx: 'a>: BackendMethods {
+    type CodegenCx: CodegenMethods<'tcx>;
+
+    fn new_codegen_context(
+        &self,
+        tcx: TyCtxt<'a, 'tcx, 'tcx>,
+        codegen_unit: Arc<CodegenUnit<'tcx>>,
+        llvm_module: &'a Self::Module,
+    ) -> Self::CodegenCx;
+}
index eca60e9c9cee1c1ef48de55e4a1ae3317a8cb98c..4e24e12bc6861e27eca89e0d8b574b6b25461716 100644 (file)
@@ -47,6 +47,7 @@ fn extend_scope_to_file(
         file: &SourceFile,
         defining_crate: CrateNum,
     ) -> Self::DIScope;
+    fn debuginfo_finalize(&self);
 }
 
 pub trait DebugInfoBuilderMethods<'tcx>: HasCodegen<'tcx> {
index 7c5108500f13318bd2d15857b0a914346d6dfbe1..2557b51b76de294ee0bb6330cf43d478648329b9 100644 (file)
@@ -30,7 +30,11 @@ fn vtables(
     fn eh_unwind_resume(&self) -> Self::Value;
     fn sess(&self) -> &Session;
     fn stats(&self) -> &RefCell<Stats>;
+    fn consume_stats(self) -> RefCell<Stats>;
     fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>>;
+    fn statics_to_rauw(&self) -> &RefCell<Vec<(Self::Value, Self::Value)>>;
+    fn used_statics(&self) -> &RefCell<Vec<Self::Value>>;
     fn set_frame_pointer_elimination(&self, llfn: Self::Value);
     fn apply_target_cpu_attr(&self, llfn: Self::Value);
+    fn create_used_variable(&self);
 }
index e0ce05d0a8479f43dbb91eeb998be59c14f06c44..019c4410e67b36a42aef9fa964eb314d129434f3 100644 (file)
@@ -22,7 +22,7 @@
 
 pub use self::abi::{AbiBuilderMethods, AbiMethods};
 pub use self::asm::{AsmBuilderMethods, AsmMethods};
-pub use self::backend::{Backend, BackendMethods, BackendTypes};
+pub use self::backend::{Backend, BackendCodegenCxMethods, BackendMethods, BackendTypes};
 pub use self::builder::BuilderMethods;
 pub use self::consts::ConstMethods;
 pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods};
index a61087812316c8130fb6911496a72e460a15bf34..0feb9d5255f5ce748cf78eb7c5839b977969977d 100644 (file)
@@ -19,4 +19,5 @@ pub trait StaticMethods<'tcx>: Backend<'tcx> {
     fn static_addr_of(&self, cv: Self::Value, align: Align, kind: Option<&str>) -> Self::Value;
     fn get_static(&self, def_id: DefId) -> Self::Value;
     fn codegen_static(&self, def_id: DefId, is_mutable: bool);
+    fn static_replace_all_uses(&self, old_g: Self::Value, new_g: Self::Value);
 }
index d1be81f1224846bbeba58723829878eb6018cda7..8633722204ddbd752630d7cf05b6a2a763c70c52 100644 (file)
 use time_graph::TimeGraph;
 use std::sync::mpsc::Receiver;
 use back::write::{self, OngoingCodegen};
+use context::CodegenCx;
+use monomorphize::partitioning::CodegenUnit;
 
 pub use llvm_util::target_features;
 use std::any::Any;
+use std::sync::Arc;
 use std::sync::mpsc;
 use rustc_data_structures::sync::Lrc;
 
@@ -139,15 +142,15 @@ mod back {
 pub struct LlvmCodegenBackend(());
 
 impl BackendMethods for LlvmCodegenBackend {
-    type Metadata = ModuleLlvm;
+    type Module = ModuleLlvm;
     type OngoingCodegen = OngoingCodegen;
 
     fn new_metadata(&self, sess: &Session, mod_name: &str) -> ModuleLlvm {
         ModuleLlvm::new(sess, mod_name)
     }
-    fn write_metadata<'a, 'gcx>(
+    fn write_metadata<'b, 'gcx>(
         &self,
-        tcx: TyCtxt<'a, 'gcx, 'gcx>,
+        tcx: TyCtxt<'b, 'gcx, 'gcx>,
         metadata: &ModuleLlvm
     ) -> EncodedMetadata {
         base::write_metadata(tcx, metadata)
@@ -187,6 +190,19 @@ fn wait_for_signal_to_codegen_item(&self, codegen: &OngoingCodegen) {
     }
 }
 
+impl<'a, 'tcx: 'a> BackendCodegenCxMethods<'a, 'tcx> for LlvmCodegenBackend {
+    type CodegenCx = CodegenCx<'a, 'tcx>;
+
+    fn new_codegen_context(
+        &self,
+        tcx: TyCtxt<'a, 'tcx, 'tcx>,
+        codegen_unit: Arc<CodegenUnit<'tcx>>,
+        llvm_module: &'a ModuleLlvm
+    ) -> CodegenCx<'a, 'tcx> {
+        CodegenCx::new(tcx, codegen_unit, llvm_module)
+    }
+}
+
 
 impl !Send for LlvmCodegenBackend {} // Llvm is on a per-thread basis
 impl !Sync for LlvmCodegenBackend {}
index 041cfbf00c6fe5648997a7d63788fc35b7e39036..1defb2c16f8a7f23b85fe8103871796d52d66d9a 100644 (file)
 use rustc::ty::TypeFoldable;
 use rustc::ty::layout::{LayoutOf, HasTyCtxt};
 use std::fmt;
-use builder::Builder;
 use interfaces::*;
 
 pub use rustc::mir::mono::MonoItem;
 
 pub use rustc_mir::monomorphize::item::MonoItemExt as BaseMonoItemExt;
 
-pub trait MonoItemExt<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> :
-    fmt::Debug + BaseMonoItemExt<'a, 'tcx>
-{
-    fn define(&self, cx: &'a Bx::CodegenCx) {
+pub trait MonoItemExt<'a, 'tcx: 'a>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
+    fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx) {
         debug!("BEGIN IMPLEMENTING '{} ({})' in cgu {}",
                self.to_string(cx.tcx()),
                self.to_raw_string(),
@@ -76,10 +73,12 @@ fn define(&self, cx: &'a Bx::CodegenCx) {
                cx.codegen_unit().name());
     }
 
-    fn predefine(&self,
-                 cx: &'a Bx::CodegenCx,
-                 linkage: Linkage,
-                 visibility: Visibility) {
+    fn predefine<Bx: BuilderMethods<'a, 'tcx>>(
+        &self,
+        cx: &'a Bx::CodegenCx,
+        linkage: Linkage,
+        visibility: Visibility
+    ) {
         debug!("BEGIN PREDEFINING '{} ({})' in cgu {}",
                self.to_string(cx.tcx()),
                self.to_raw_string(),
@@ -122,7 +121,7 @@ fn to_raw_string(&self) -> String {
     }
 }
 
-impl MonoItemExt<'a, 'tcx, Builder<'a, 'll, 'tcx>> for MonoItem<'tcx> {}
+impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {}
 
 impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
     fn predefine_static(&self,