Skip to content

Commit 6819e6e

Browse files
denismerigouxeddyb
authored andcommitted
Preparing the generalization of base:compile_coodegen_unit
1 parent 91a2a80 commit 6819e6e

File tree

11 files changed

+114
-57
lines changed

11 files changed

+114
-57
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use super::ModuleLlvm;
2727
use super::ModuleCodegen;
2828
use super::ModuleKind;
2929
use super::CachedModuleCodegen;
30+
use super::LlvmCodegenBackend;
3031

3132
use abi;
3233
use back::write;
@@ -53,8 +54,6 @@ use builder::{Builder, MemFlags};
5354
use callee;
5455
use rustc_mir::monomorphize::item::DefPathBasedNames;
5556
use common::{self, IntPredicate, RealPredicate, TypeKind};
56-
use context::CodegenCx;
57-
use debuginfo;
5857
use meth;
5958
use mir;
6059
use monomorphize::Instance;
@@ -968,7 +967,7 @@ impl<B: BackendMethods> Drop for AbortCodegenOnDrop<B> {
968967
}
969968
}
970969

971-
fn assert_and_save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
970+
fn assert_and_save_dep_graph<'ll, 'tcx>(tcx: TyCtxt<'ll, 'tcx, 'tcx>) {
972971
time(tcx.sess,
973972
"assert dep graph",
974973
|| rustc_incremental::assert_dep_graph(tcx));
@@ -1067,7 +1066,7 @@ impl CrateInfo {
10671066
}
10681067
}
10691068

1070-
fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1069+
fn compile_codegen_unit<'ll, 'tcx>(tcx: TyCtxt<'ll, 'tcx, 'tcx>,
10711070
cgu_name: InternedString)
10721071
-> Stats {
10731072
let start_time = Instant::now();
@@ -1089,67 +1088,49 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10891088
cost);
10901089
return stats;
10911090

1092-
fn module_codegen<'a, 'tcx>(
1093-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
1091+
fn module_codegen<'ll, 'tcx>(
1092+
tcx: TyCtxt<'ll, 'tcx, 'tcx>,
10941093
cgu_name: InternedString)
10951094
-> (Stats, ModuleCodegen<ModuleLlvm>)
10961095
{
1096+
let backend = LlvmCodegenBackend(());
10971097
let cgu = tcx.codegen_unit(cgu_name);
1098-
10991098
// Instantiate monomorphizations without filling out definitions yet...
1100-
let llvm_module = ModuleLlvm::new(tcx.sess, &cgu_name.as_str());
1099+
let llvm_module = backend.new_metadata(tcx.sess, &cgu_name.as_str());
11011100
let stats = {
1102-
let cx = CodegenCx::new(tcx, cgu, &llvm_module);
1101+
let cx = backend.new_codegen_context(tcx, cgu, &llvm_module);
11031102
let mono_items = cx.codegen_unit
11041103
.items_in_deterministic_order(cx.tcx);
11051104
for &(mono_item, (linkage, visibility)) in &mono_items {
1106-
mono_item.predefine(&cx, linkage, visibility);
1105+
mono_item.predefine::<Builder<&Value>>(&cx, linkage, visibility);
11071106
}
11081107

11091108
// ... and now that we have everything pre-defined, fill out those definitions.
11101109
for &(mono_item, _) in &mono_items {
1111-
mono_item.define(&cx);
1110+
mono_item.define::<Builder<&Value>>(&cx);
11121111
}
11131112

11141113
// If this codegen unit contains the main function, also create the
11151114
// wrapper here
11161115
maybe_create_entry_wrapper::<Builder<&Value>>(&cx);
11171116

11181117
// Run replace-all-uses-with for statics that need it
1119-
for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
1120-
unsafe {
1121-
let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
1122-
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
1123-
llvm::LLVMDeleteGlobal(old_g);
1124-
}
1118+
for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
1119+
cx.static_replace_all_uses(old_g, new_g)
11251120
}
11261121

11271122
// Create the llvm.used variable
11281123
// This variable has type [N x i8*] and is stored in the llvm.metadata section
1129-
if !cx.used_statics.borrow().is_empty() {
1130-
let name = const_cstr!("llvm.used");
1131-
let section = const_cstr!("llvm.metadata");
1132-
let array = cx.const_array(
1133-
&cx.type_ptr_to(cx.type_i8()),
1134-
&*cx.used_statics.borrow()
1135-
);
1136-
1137-
unsafe {
1138-
let g = llvm::LLVMAddGlobal(cx.llmod,
1139-
cx.val_ty(array),
1140-
name.as_ptr());
1141-
llvm::LLVMSetInitializer(g, array);
1142-
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
1143-
llvm::LLVMSetSection(g, section.as_ptr());
1144-
}
1124+
if !cx.used_statics().borrow().is_empty() {
1125+
cx.create_used_variable()
11451126
}
11461127

11471128
// Finalize debuginfo
11481129
if cx.sess().opts.debuginfo != DebugInfo::None {
1149-
debuginfo::finalize(&cx);
1130+
cx.debuginfo_finalize();
11501131
}
11511132

1152-
cx.stats.into_inner()
1133+
cx.consume_stats().into_inner()
11531134
};
11541135

11551136
(stats, ModuleCodegen {

src/librustc_codegen_llvm/consts.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,11 @@ impl StaticMethods<'tcx> for CodegenCx<'ll, 'tcx> {
443443
}
444444
}
445445
}
446+
fn static_replace_all_uses(&self, old_g: &'ll Value, new_g: &'ll Value) {
447+
unsafe {
448+
let bitcast = llvm::LLVMConstPointerCast(new_g, self.val_ty(old_g));
449+
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
450+
llvm::LLVMDeleteGlobal(old_g);
451+
}
452+
}
446453
}

src/librustc_codegen_llvm/context.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,17 +421,48 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
421421
&self.stats
422422
}
423423

424+
fn consume_stats(self) -> RefCell<Stats> {
425+
self.stats
426+
}
427+
424428
fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>> {
425429
&self.codegen_unit
426430
}
427431

432+
fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
433+
&self.statics_to_rauw
434+
}
435+
436+
fn used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
437+
&self.used_statics
438+
}
439+
428440
fn set_frame_pointer_elimination(&self, llfn: &'ll Value) {
429441
attributes::set_frame_pointer_elimination(self, llfn)
430442
}
431443

432444
fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
433445
attributes::apply_target_cpu_attr(self, llfn)
434446
}
447+
448+
449+
fn create_used_variable(&self) {
450+
let name = const_cstr!("llvm.used");
451+
let section = const_cstr!("llvm.metadata");
452+
let array = self.const_array(
453+
&self.type_ptr_to(self.type_i8()),
454+
&*self.used_statics.borrow()
455+
);
456+
457+
unsafe {
458+
let g = llvm::LLVMAddGlobal(self.llmod,
459+
self.val_ty(array),
460+
name.as_ptr());
461+
llvm::LLVMSetInitializer(g, array);
462+
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
463+
llvm::LLVMSetSection(g, section.as_ptr());
464+
}
465+
}
435466
}
436467

437468
impl IntrinsicDeclarationMethods<'tcx> for CodegenCx<'b, 'tcx> {

src/librustc_codegen_llvm/debuginfo/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,4 +585,8 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
585585
) -> &'ll DILexicalBlock {
586586
metadata::extend_scope_to_file(&self, scope_metadata, file, defining_crate)
587587
}
588+
589+
fn debuginfo_finalize(&self) {
590+
finalize(self)
591+
}
588592
}

src/librustc_codegen_llvm/interfaces/backend.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
use rustc::ty::layout::{HasTyCtxt, LayoutOf, TyLayout};
1212
use rustc::ty::Ty;
1313

14-
use super::CodegenObject;
14+
use super::{CodegenMethods, CodegenObject};
15+
use monomorphize::partitioning::CodegenUnit;
1516
use rustc::middle::allocator::AllocatorKind;
1617
use rustc::middle::cstore::EncodedMetadata;
1718
use rustc::session::Session;
1819
use rustc::ty::TyCtxt;
1920
use std::any::Any;
2021
use std::sync::mpsc::Receiver;
22+
use std::sync::Arc;
2123
use time_graph::TimeGraph;
2224
use ModuleCodegen;
2325

@@ -40,16 +42,16 @@ impl<'tcx, T> Backend<'tcx> for T where
4042
{}
4143

4244
pub trait BackendMethods {
43-
type Metadata;
45+
type Module;
4446
type OngoingCodegen;
4547

46-
fn new_metadata(&self, sess: &Session, mod_name: &str) -> Self::Metadata;
47-
fn write_metadata<'a, 'gcx>(
48+
fn new_metadata(&self, sess: &Session, mod_name: &str) -> Self::Module;
49+
fn write_metadata<'b, 'gcx>(
4850
&self,
49-
tcx: TyCtxt<'a, 'gcx, 'gcx>,
50-
metadata: &Self::Metadata,
51+
tcx: TyCtxt<'b, 'gcx, 'gcx>,
52+
metadata: &Self::Module,
5153
) -> EncodedMetadata;
52-
fn codegen_allocator(&self, tcx: TyCtxt, mods: &Self::Metadata, kind: AllocatorKind);
54+
fn codegen_allocator(&self, tcx: TyCtxt, mods: &Self::Module, kind: AllocatorKind);
5355

5456
fn start_async_codegen(
5557
&self,
@@ -63,10 +65,21 @@ pub trait BackendMethods {
6365
&self,
6466
codegen: &Self::OngoingCodegen,
6567
tcx: TyCtxt,
66-
module: ModuleCodegen<Self::Metadata>,
68+
module: ModuleCodegen<Self::Module>,
6769
);
6870
fn codegen_aborted(codegen: Self::OngoingCodegen);
6971
fn codegen_finished(&self, codegen: &Self::OngoingCodegen, tcx: TyCtxt);
7072
fn check_for_errors(&self, codegen: &Self::OngoingCodegen, sess: &Session);
7173
fn wait_for_signal_to_codegen_item(&self, codegen: &Self::OngoingCodegen);
7274
}
75+
76+
pub trait BackendCodegenCxMethods<'a, 'tcx: 'a>: BackendMethods {
77+
type CodegenCx: CodegenMethods<'tcx>;
78+
79+
fn new_codegen_context(
80+
&self,
81+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
82+
codegen_unit: Arc<CodegenUnit<'tcx>>,
83+
llvm_module: &'a Self::Module,
84+
) -> Self::CodegenCx;
85+
}

src/librustc_codegen_llvm/interfaces/debuginfo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub trait DebugInfoMethods<'tcx>: Backend<'tcx> {
4747
file: &SourceFile,
4848
defining_crate: CrateNum,
4949
) -> Self::DIScope;
50+
fn debuginfo_finalize(&self);
5051
}
5152

5253
pub trait DebugInfoBuilderMethods<'tcx>: HasCodegen<'tcx> {

src/librustc_codegen_llvm/interfaces/misc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ pub trait MiscMethods<'tcx>: Backend<'tcx> {
3030
fn eh_unwind_resume(&self) -> Self::Value;
3131
fn sess(&self) -> &Session;
3232
fn stats(&self) -> &RefCell<Stats>;
33+
fn consume_stats(self) -> RefCell<Stats>;
3334
fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>>;
35+
fn statics_to_rauw(&self) -> &RefCell<Vec<(Self::Value, Self::Value)>>;
36+
fn used_statics(&self) -> &RefCell<Vec<Self::Value>>;
3437
fn set_frame_pointer_elimination(&self, llfn: Self::Value);
3538
fn apply_target_cpu_attr(&self, llfn: Self::Value);
39+
fn create_used_variable(&self);
3640
}

src/librustc_codegen_llvm/interfaces/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mod type_;
2222

2323
pub use self::abi::{AbiBuilderMethods, AbiMethods};
2424
pub use self::asm::{AsmBuilderMethods, AsmMethods};
25-
pub use self::backend::{Backend, BackendMethods, BackendTypes};
25+
pub use self::backend::{Backend, BackendCodegenCxMethods, BackendMethods, BackendTypes};
2626
pub use self::builder::BuilderMethods;
2727
pub use self::consts::ConstMethods;
2828
pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods};

src/librustc_codegen_llvm/interfaces/statics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ pub trait StaticMethods<'tcx>: Backend<'tcx> {
1919
fn static_addr_of(&self, cv: Self::Value, align: Align, kind: Option<&str>) -> Self::Value;
2020
fn get_static(&self, def_id: DefId) -> Self::Value;
2121
fn codegen_static(&self, def_id: DefId, is_mutable: bool);
22+
fn static_replace_all_uses(&self, old_g: Self::Value, new_g: Self::Value);
2223
}

src/librustc_codegen_llvm/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ use interfaces::*;
7272
use time_graph::TimeGraph;
7373
use std::sync::mpsc::Receiver;
7474
use back::write::{self, OngoingCodegen};
75+
use context::CodegenCx;
76+
use monomorphize::partitioning::CodegenUnit;
7577

7678
pub use llvm_util::target_features;
7779
use std::any::Any;
80+
use std::sync::Arc;
7881
use std::sync::mpsc;
7982
use rustc_data_structures::sync::Lrc;
8083

@@ -139,15 +142,15 @@ mod value;
139142
pub struct LlvmCodegenBackend(());
140143

141144
impl BackendMethods for LlvmCodegenBackend {
142-
type Metadata = ModuleLlvm;
145+
type Module = ModuleLlvm;
143146
type OngoingCodegen = OngoingCodegen;
144147

145148
fn new_metadata(&self, sess: &Session, mod_name: &str) -> ModuleLlvm {
146149
ModuleLlvm::new(sess, mod_name)
147150
}
148-
fn write_metadata<'a, 'gcx>(
151+
fn write_metadata<'b, 'gcx>(
149152
&self,
150-
tcx: TyCtxt<'a, 'gcx, 'gcx>,
153+
tcx: TyCtxt<'b, 'gcx, 'gcx>,
151154
metadata: &ModuleLlvm
152155
) -> EncodedMetadata {
153156
base::write_metadata(tcx, metadata)
@@ -187,6 +190,19 @@ impl BackendMethods for LlvmCodegenBackend {
187190
}
188191
}
189192

193+
impl<'a, 'tcx: 'a> BackendCodegenCxMethods<'a, 'tcx> for LlvmCodegenBackend {
194+
type CodegenCx = CodegenCx<'a, 'tcx>;
195+
196+
fn new_codegen_context(
197+
&self,
198+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
199+
codegen_unit: Arc<CodegenUnit<'tcx>>,
200+
llvm_module: &'a ModuleLlvm
201+
) -> CodegenCx<'a, 'tcx> {
202+
CodegenCx::new(tcx, codegen_unit, llvm_module)
203+
}
204+
}
205+
190206

191207
impl !Send for LlvmCodegenBackend {} // Llvm is on a per-thread basis
192208
impl !Sync for LlvmCodegenBackend {}

0 commit comments

Comments
 (0)