Skip to content

Commit 0c10974

Browse files
committed
Preparing the generalization of base:compile_coodegen_unit
1 parent 440f2fb commit 0c10974

File tree

10 files changed

+112
-56
lines changed

10 files changed

+112
-56
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 20 additions & 39 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;
@@ -54,8 +55,6 @@ use callee;
5455
use rustc_mir::monomorphize::collector::{self, MonoItemCollectionMode};
5556
use rustc_mir::monomorphize::item::DefPathBasedNames;
5657
use common::{self, IntPredicate, RealPredicate, TypeKind};
57-
use context::CodegenCx;
58-
use debuginfo;
5958
use meth;
6059
use mir;
6160
use monomorphize::Instance;
@@ -720,9 +719,9 @@ fn determine_cgu_reuse<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
720719
}
721720
}
722721

723-
pub fn codegen_crate<'a, 'tcx, B : BackendMethods>(
722+
pub fn codegen_crate<'a, 'll: 'a, 'tcx: 'll, B : BackendMethods<'a, 'll, 'tcx>>(
724723
backend: B,
725-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
724+
tcx: TyCtxt<'ll, 'tcx, 'tcx>,
726725
rx: mpsc::Receiver<Box<dyn Any + Send>>
727726
) -> B::OngoingCodegen {
728727

@@ -940,7 +939,7 @@ pub fn codegen_crate<'a, 'tcx, B : BackendMethods>(
940939
ongoing_codegen
941940
}
942941

943-
fn assert_and_save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
942+
fn assert_and_save_dep_graph<'ll, 'tcx>(tcx: TyCtxt<'ll, 'tcx, 'tcx>) {
944943
time(tcx.sess,
945944
"assert dep graph",
946945
|| rustc_incremental::assert_dep_graph(tcx));
@@ -950,8 +949,8 @@ fn assert_and_save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
950949
|| rustc_incremental::save_dep_graph(tcx));
951950
}
952951

953-
fn collect_and_partition_mono_items<'a, 'tcx>(
954-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
952+
fn collect_and_partition_mono_items<'ll, 'tcx>(
953+
tcx: TyCtxt<'ll, 'tcx, 'tcx>,
955954
cnum: CrateNum,
956955
) -> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>)
957956
{
@@ -1167,7 +1166,7 @@ fn is_codegened_item(tcx: TyCtxt, id: DefId) -> bool {
11671166
all_mono_items.contains(&id)
11681167
}
11691168

1170-
fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1169+
fn compile_codegen_unit<'ll, 'tcx>(tcx: TyCtxt<'ll, 'tcx, 'tcx>,
11711170
cgu_name: InternedString)
11721171
-> Stats {
11731172
let start_time = Instant::now();
@@ -1189,67 +1188,49 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11891188
cost);
11901189
return stats;
11911190

1192-
fn module_codegen<'a, 'tcx>(
1193-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
1191+
fn module_codegen<'ll, 'tcx>(
1192+
tcx: TyCtxt<'ll, 'tcx, 'tcx>,
11941193
cgu_name: InternedString)
11951194
-> (Stats, ModuleCodegen<ModuleLlvm>)
11961195
{
1196+
let backend = LlvmCodegenBackend(());
11971197
let cgu = tcx.codegen_unit(cgu_name);
1198-
11991198
// Instantiate monomorphizations without filling out definitions yet...
1200-
let llvm_module = ModuleLlvm::new(tcx.sess, &cgu_name.as_str());
1199+
let llvm_module = backend.new_metadata(tcx.sess, &cgu_name.as_str());
12011200
let stats = {
1202-
let cx = CodegenCx::new(tcx, cgu, &llvm_module);
1201+
let cx = backend.new_codegen_context(tcx, cgu, &llvm_module);
12031202
let mono_items = cx.codegen_unit
12041203
.items_in_deterministic_order(cx.tcx);
12051204
for &(mono_item, (linkage, visibility)) in &mono_items {
1206-
mono_item.predefine(&cx, linkage, visibility);
1205+
mono_item.predefine::<Builder<&Value>>(&cx, linkage, visibility);
12071206
}
12081207

12091208
// ... and now that we have everything pre-defined, fill out those definitions.
12101209
for &(mono_item, _) in &mono_items {
1211-
mono_item.define(&cx);
1210+
mono_item.define::<Builder<&Value>>(&cx);
12121211
}
12131212

12141213
// If this codegen unit contains the main function, also create the
12151214
// wrapper here
12161215
maybe_create_entry_wrapper::<Builder<&Value>>(&cx);
12171216

12181217
// Run replace-all-uses-with for statics that need it
1219-
for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
1220-
unsafe {
1221-
let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
1222-
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
1223-
llvm::LLVMDeleteGlobal(old_g);
1224-
}
1218+
for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
1219+
cx.static_replace_all_uses(old_g, new_g)
12251220
}
12261221

12271222
// Create the llvm.used variable
12281223
// This variable has type [N x i8*] and is stored in the llvm.metadata section
1229-
if !cx.used_statics.borrow().is_empty() {
1230-
let name = const_cstr!("llvm.used");
1231-
let section = const_cstr!("llvm.metadata");
1232-
let array = cx.const_array(
1233-
&cx.type_ptr_to(cx.type_i8()),
1234-
&*cx.used_statics.borrow()
1235-
);
1236-
1237-
unsafe {
1238-
let g = llvm::LLVMAddGlobal(cx.llmod,
1239-
cx.val_ty(array),
1240-
name.as_ptr());
1241-
llvm::LLVMSetInitializer(g, array);
1242-
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
1243-
llvm::LLVMSetSection(g, section.as_ptr());
1244-
}
1224+
if !cx.used_statics().borrow().is_empty() {
1225+
cx.create_used_variable()
12451226
}
12461227

12471228
// Finalize debuginfo
12481229
if cx.sess().opts.debuginfo != DebugInfo::None {
1249-
debuginfo::finalize(&cx);
1230+
cx.debuginfo_finalize();
12501231
}
12511232

1252-
cx.stats.into_inner()
1233+
cx.consume_stats().into_inner()
12531234
};
12541235

12551236
(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<'ll> for CodegenCx<'ll, 'tcx, &'ll Value> {
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
@@ -422,17 +422,48 @@ impl MiscMethods<'ll, 'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {
422422
&self.stats
423423
}
424424

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

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

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

438469
impl<'ll, 'tcx: 'll> CodegenMethods<'ll, 'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {}

src/librustc_codegen_llvm/debuginfo/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,8 @@ impl<'ll, 'tcx: 'll> DebugInfoMethods<'ll, 'tcx> for CodegenCx<'ll, 'tcx, &'ll V
592592
) -> &'ll DILexicalBlock {
593593
metadata::extend_scope_to_file(&self, scope_metadata, file, defining_crate)
594594
}
595+
596+
fn debuginfo_finalize(&self) {
597+
finalize(self)
598+
}
595599
}

src/librustc_codegen_llvm/interfaces/backend.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
// except according to those terms.
1010

1111
use super::CodegenObject;
12+
use super::builder::{HasCodegen, BuilderMethods};
1213
use ModuleCodegen;
1314
use rustc::session::Session;
1415
use rustc::middle::cstore::EncodedMetadata;
1516
use rustc::middle::allocator::AllocatorKind;
17+
use monomorphize::partitioning::CodegenUnit;
1618
use rustc::ty::TyCtxt;
1719
use time_graph::TimeGraph;
1820
use std::sync::mpsc::Receiver;
1921
use std::any::Any;
22+
use std::sync::Arc;
2023

2124
pub trait Backend<'ll> {
2225
type Value : 'll + CodegenObject;
@@ -25,16 +28,17 @@ pub trait Backend<'ll> {
2528
type Context;
2629
}
2730

28-
pub trait BackendMethods {
31+
pub trait BackendMethods<'a, 'll: 'a, 'tcx: 'll> {
2932
type Metadata;
3033
type OngoingCodegen;
34+
type Builder : BuilderMethods<'a, 'll, 'tcx>;
3135

3236
fn thin_lto_available(&self) -> bool;
3337
fn pgo_available(&self) -> bool;
3438
fn new_metadata(&self, sess: &Session, mod_name: &str) -> Self::Metadata;
35-
fn write_metadata<'a, 'gcx>(
39+
fn write_metadata<'b, 'gcx>(
3640
&self,
37-
tcx: TyCtxt<'a, 'gcx, 'gcx>,
41+
tcx: TyCtxt<'b, 'gcx, 'gcx>,
3842
metadata: &Self::Metadata
3943
) -> EncodedMetadata;
4044
fn codegen_allocator(&self, tcx: TyCtxt, mods: &Self::Metadata, kind: AllocatorKind);
@@ -56,4 +60,10 @@ pub trait BackendMethods {
5660
fn codegen_finished(&self, codegen: &Self::OngoingCodegen, tcx: TyCtxt);
5761
fn check_for_errors(&self, codegen: &Self::OngoingCodegen, sess: &Session);
5862
fn wait_for_signal_to_codegen_item(&self, codegen: &Self::OngoingCodegen);
63+
fn new_codegen_context(
64+
&self,
65+
tcx: TyCtxt<'ll, 'tcx, 'tcx>,
66+
codegen_unit: Arc<CodegenUnit<'tcx>>,
67+
llvm_module: &'ll Self::Metadata
68+
) -> <Self::Builder as HasCodegen<'a, 'll, 'tcx>>::CodegenCx;
5969
}

src/librustc_codegen_llvm/interfaces/debuginfo.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub trait DebugInfoMethods<'ll, 'tcx: 'll> : Backend<'ll> {
4141
llfn: Self::Value,
4242
mir: &mir::Mir,
4343
) -> FunctionDebugContext<'ll>;
44-
44+
4545
fn create_mir_scopes(
4646
&self,
4747
mir: &mir::Mir,
@@ -53,6 +53,7 @@ pub trait DebugInfoMethods<'ll, 'tcx: 'll> : Backend<'ll> {
5353
file: &syntax_pos::SourceFile,
5454
defining_crate: CrateNum,
5555
) -> Self::DIScope;
56+
fn debuginfo_finalize(&self);
5657
}
5758

5859
pub trait DebugInfoBuilderMethods<'a, 'll: 'a, 'tcx: 'll> : HasCodegen<'a, 'll, 'tcx> {

src/librustc_codegen_llvm/interfaces/misc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ pub trait MiscMethods<'ll, 'tcx: 'll> : Backend<'ll> {
2929
fn eh_unwind_resume(&self) -> Self::Value;
3030
fn sess(&self) -> &Session;
3131
fn stats(&self) -> &RefCell<Stats>;
32+
fn consume_stats(self) -> RefCell<Stats>;
3233
fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>>;
34+
fn statics_to_rauw(&self) -> &RefCell<Vec<(Self::Value, Self::Value)>>;
35+
fn used_statics(&self) -> &RefCell<Vec<Self::Value>>;
3336
fn set_frame_pointer_elimination(&self, llfn: Self::Value);
3437
fn apply_target_cpu_attr(&self, llfn: Self::Value);
38+
fn create_used_variable(&self);
3539
}

src/librustc_codegen_llvm/interfaces/statics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ pub trait StaticMethods<'ll> : Backend<'ll> {
3333
def_id: DefId,
3434
is_mutable: bool,
3535
);
36+
fn static_replace_all_uses(&self, old_g: Self::Value, new_g: Self::Value);
3637
}

src/librustc_codegen_llvm/lib.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ use interfaces::*;
7272
use time_graph::TimeGraph;
7373
use std::sync::mpsc::Receiver;
7474
use back::write::{self, OngoingCodegen};
75+
use builder::Builder;
76+
use value::Value;
77+
use context::CodegenCx;
78+
use monomorphize::partitioning::CodegenUnit;
7579

7680
pub use llvm_util::target_features;
7781
use std::any::Any;
82+
use std::sync::Arc;
7883
use std::path::{PathBuf};
7984
use std::sync::mpsc;
8085
use rustc_data_structures::sync::Lrc;
@@ -142,9 +147,10 @@ mod value;
142147

143148
pub struct LlvmCodegenBackend(());
144149

145-
impl BackendMethods for LlvmCodegenBackend {
150+
impl<'a, 'll: 'a, 'tcx: 'll> BackendMethods<'a, 'll, 'tcx> for LlvmCodegenBackend {
146151
type Metadata = ModuleLlvm;
147152
type OngoingCodegen = OngoingCodegen;
153+
type Builder = Builder<'a, 'll, 'tcx, &'ll Value>;
148154

149155
fn thin_lto_available(&self) -> bool {
150156
unsafe { !llvm::LLVMRustThinLTOAvailable() }
@@ -155,9 +161,9 @@ impl BackendMethods for LlvmCodegenBackend {
155161
fn new_metadata(&self, sess: &Session, mod_name: &str) -> ModuleLlvm {
156162
ModuleLlvm::new(sess, mod_name)
157163
}
158-
fn write_metadata<'a, 'gcx>(
164+
fn write_metadata<'b, 'gcx>(
159165
&self,
160-
tcx: TyCtxt<'a, 'gcx, 'gcx>,
166+
tcx: TyCtxt<'b, 'gcx, 'gcx>,
161167
metadata: &ModuleLlvm
162168
) -> EncodedMetadata {
163169
base::write_metadata(tcx, metadata)
@@ -192,6 +198,14 @@ impl BackendMethods for LlvmCodegenBackend {
192198
fn wait_for_signal_to_codegen_item(&self, codegen: &OngoingCodegen) {
193199
codegen.wait_for_signal_to_codegen_item()
194200
}
201+
fn new_codegen_context(
202+
&self,
203+
tcx: TyCtxt<'ll, 'tcx, 'tcx>,
204+
codegen_unit: Arc<CodegenUnit<'tcx>>,
205+
llvm_module: &'ll ModuleLlvm
206+
) -> CodegenCx<'ll, 'tcx, &'ll Value> {
207+
CodegenCx::new(tcx, codegen_unit, llvm_module)
208+
}
195209
}
196210

197211

src/librustc_codegen_llvm/mono_item.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,17 @@ use rustc::ty::{TypeFoldable, Ty};
2828
use rustc::ty::layout::{LayoutOf, HasTyCtxt, TyLayout};
2929
use std::fmt;
3030
use value::Value;
31-
use builder::Builder;
3231
use interfaces::*;
3332

3433
pub use rustc::mir::mono::MonoItem;
3534

3635
pub use rustc_mir::monomorphize::item::MonoItemExt as BaseMonoItemExt;
3736

38-
pub trait MonoItemExt<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a, 'll, 'tcx>> :
39-
fmt::Debug + BaseMonoItemExt<'ll, 'tcx> where
40-
&'a Bx::CodegenCx : LayoutOf<Ty = Ty<'tcx>, TyLayout=TyLayout<'tcx>> + HasTyCtxt<'tcx>
37+
pub trait MonoItemExt<'a, 'll: 'a, 'tcx: 'll> : fmt::Debug + BaseMonoItemExt<'ll, 'tcx>
4138
{
42-
fn define(&self, cx: &'a Bx::CodegenCx) {
39+
fn define<Bx: BuilderMethods<'a, 'll, 'tcx>>(&self, cx: &'a Bx::CodegenCx) where
40+
&'a Bx::CodegenCx : LayoutOf<Ty = Ty<'tcx>, TyLayout=TyLayout<'tcx>> + HasTyCtxt<'tcx>
41+
{
4342
debug!("BEGIN IMPLEMENTING '{} ({})' in cgu {}",
4443
self.to_string(*cx.tcx()),
4544
self.to_raw_string(),
@@ -78,10 +77,14 @@ pub trait MonoItemExt<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a, 'll, 'tcx>>
7877
cx.codegen_unit().name());
7978
}
8079

81-
fn predefine(&self,
82-
cx: &'a Bx::CodegenCx,
83-
linkage: Linkage,
84-
visibility: Visibility) {
80+
fn predefine<Bx: BuilderMethods<'a, 'll, 'tcx>>(
81+
&self,
82+
cx: &'a Bx::CodegenCx,
83+
linkage: Linkage,
84+
visibility: Visibility
85+
) where
86+
&'a Bx::CodegenCx : LayoutOf<Ty = Ty<'tcx>, TyLayout=TyLayout<'tcx>> + HasTyCtxt<'tcx>
87+
{
8588
debug!("BEGIN PREDEFINING '{} ({})' in cgu {}",
8689
self.to_string(*cx.tcx()),
8790
self.to_raw_string(),
@@ -124,7 +127,7 @@ pub trait MonoItemExt<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a, 'll, 'tcx>>
124127
}
125128
}
126129

127-
impl<'a, 'll:'a, 'tcx: 'll> MonoItemExt<'a, 'll, 'tcx, Builder<'a, 'll, 'tcx, &'ll Value>>
130+
impl<'a, 'll:'a, 'tcx: 'll> MonoItemExt<'a, 'll, 'tcx>
128131
for MonoItem<'tcx> {}
129132

130133
impl<'ll, 'tcx: 'll> PreDefineMethods<'ll, 'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {

0 commit comments

Comments
 (0)