Skip to content

Commit a478ce6

Browse files
committed
Preparing the generalization of base:compile_coodegen_unit
1 parent fdf915b commit a478ce6

File tree

10 files changed

+192
-53
lines changed

10 files changed

+192
-53
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 101 additions & 36 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

@@ -941,6 +940,7 @@ pub fn codegen_crate<'a, 'tcx, B : BackendMethods>(
941940
ongoing_codegen.into_inner()
942941
}
943942

943+
=======
944944
/// A curious wrapper structure whose only purpose is to call `codegen_aborted`
945945
/// when it's dropped abnormally.
946946
///
@@ -968,6 +968,83 @@ impl AbortCodegenOnDrop {
968968

969969
impl Deref for AbortCodegenOnDrop {
970970
type Target = OngoingCodegen;
971+
}
972+
973+
fn assert_and_save_dep_graph<'ll, 'tcx>(tcx: TyCtxt<'ll, 'tcx, 'tcx>) {
974+
time(tcx.sess,
975+
"assert dep graph",
976+
|| rustc_incremental::assert_dep_graph(tcx));
977+
978+
time(tcx.sess,
979+
"serialize dep graph",
980+
|| rustc_incremental::save_dep_graph(tcx));
981+
}
982+
983+
fn collect_and_partition_mono_items<'ll, 'tcx>(
984+
tcx: TyCtxt<'ll, 'tcx, 'tcx>,
985+
cnum: CrateNum,
986+
) -> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>)
987+
{
988+
assert_eq!(cnum, LOCAL_CRATE);
989+
990+
let collection_mode = match tcx.sess.opts.debugging_opts.print_mono_items {
991+
Some(ref s) => {
992+
let mode_string = s.to_lowercase();
993+
let mode_string = mode_string.trim();
994+
if mode_string == "eager" {
995+
MonoItemCollectionMode::Eager
996+
} else {
997+
if mode_string != "lazy" {
998+
let message = format!("Unknown codegen-item collection mode '{}'. \
999+
Falling back to 'lazy' mode.",
1000+
mode_string);
1001+
tcx.sess.warn(&message);
1002+
}
1003+
1004+
MonoItemCollectionMode::Lazy
1005+
}
1006+
}
1007+
None => {
1008+
if tcx.sess.opts.cg.link_dead_code {
1009+
MonoItemCollectionMode::Eager
1010+
} else {
1011+
MonoItemCollectionMode::Lazy
1012+
}
1013+
}
1014+
};
1015+
1016+
let (items, inlining_map) =
1017+
time(tcx.sess, "monomorphization collection", || {
1018+
collector::collect_crate_mono_items(tcx, collection_mode)
1019+
});
1020+
1021+
tcx.sess.abort_if_errors();
1022+
1023+
::rustc_mir::monomorphize::assert_symbols_are_distinct(tcx, items.iter());
1024+
1025+
let strategy = if tcx.sess.opts.incremental.is_some() {
1026+
PartitioningStrategy::PerModule
1027+
} else {
1028+
PartitioningStrategy::FixedUnitCount(tcx.sess.codegen_units())
1029+
};
1030+
1031+
let codegen_units = time(tcx.sess, "codegen unit partitioning", || {
1032+
partitioning::partition(tcx,
1033+
items.iter().cloned(),
1034+
strategy,
1035+
&inlining_map)
1036+
.into_iter()
1037+
.map(Arc::new)
1038+
.collect::<Vec<_>>()
1039+
});
1040+
1041+
let mono_items: DefIdSet = items.iter().filter_map(|mono_item| {
1042+
match *mono_item {
1043+
MonoItem::Fn(ref instance) => Some(instance.def_id()),
1044+
MonoItem::Static(def_id) => Some(def_id),
1045+
_ => None,
1046+
}
1047+
}).collect();
9711048

9721049
fn deref(&self) -> &OngoingCodegen {
9731050
self.0.as_ref().unwrap()
@@ -1087,7 +1164,13 @@ impl CrateInfo {
10871164
}
10881165
}
10891166

1090-
fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1167+
fn is_codegened_item(tcx: TyCtxt, id: DefId) -> bool {
1168+
let (all_mono_items, _) =
1169+
tcx.collect_and_partition_mono_items(LOCAL_CRATE);
1170+
all_mono_items.contains(&id)
1171+
}
1172+
1173+
fn compile_codegen_unit<'ll, 'tcx>(tcx: TyCtxt<'ll, 'tcx, 'tcx>,
10911174
cgu_name: InternedString)
10921175
-> Stats {
10931176
let start_time = Instant::now();
@@ -1109,67 +1192,49 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11091192
cost);
11101193
return stats;
11111194

1112-
fn module_codegen<'a, 'tcx>(
1113-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
1195+
fn module_codegen<'ll, 'tcx>(
1196+
tcx: TyCtxt<'ll, 'tcx, 'tcx>,
11141197
cgu_name: InternedString)
11151198
-> (Stats, ModuleCodegen<ModuleLlvm>)
11161199
{
1200+
let backend = LlvmCodegenBackend(());
11171201
let cgu = tcx.codegen_unit(cgu_name);
1118-
11191202
// Instantiate monomorphizations without filling out definitions yet...
1120-
let llvm_module = ModuleLlvm::new(tcx.sess, &cgu_name.as_str());
1203+
let llvm_module = backend.new_metadata(tcx.sess, &cgu_name.as_str());
11211204
let stats = {
1122-
let cx = CodegenCx::new(tcx, cgu, &llvm_module);
1205+
let cx = backend.new_codegen_context(tcx, cgu, &llvm_module);
11231206
let mono_items = cx.codegen_unit
11241207
.items_in_deterministic_order(cx.tcx);
11251208
for &(mono_item, (linkage, visibility)) in &mono_items {
1126-
mono_item.predefine(&cx, linkage, visibility);
1209+
mono_item.predefine::<Builder<&Value>>(&cx, linkage, visibility);
11271210
}
11281211

11291212
// ... and now that we have everything pre-defined, fill out those definitions.
11301213
for &(mono_item, _) in &mono_items {
1131-
mono_item.define(&cx);
1214+
mono_item.define::<Builder<&Value>>(&cx);
11321215
}
11331216

11341217
// If this codegen unit contains the main function, also create the
11351218
// wrapper here
11361219
maybe_create_entry_wrapper::<Builder<&Value>>(&cx);
11371220

11381221
// Run replace-all-uses-with for statics that need it
1139-
for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
1140-
unsafe {
1141-
let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
1142-
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
1143-
llvm::LLVMDeleteGlobal(old_g);
1144-
}
1222+
for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
1223+
cx.static_replace_all_uses(old_g, new_g)
11451224
}
11461225

11471226
// Create the llvm.used variable
11481227
// This variable has type [N x i8*] and is stored in the llvm.metadata section
1149-
if !cx.used_statics.borrow().is_empty() {
1150-
let name = const_cstr!("llvm.used");
1151-
let section = const_cstr!("llvm.metadata");
1152-
let array = cx.const_array(
1153-
&cx.type_ptr_to(cx.type_i8()),
1154-
&*cx.used_statics.borrow()
1155-
);
1156-
1157-
unsafe {
1158-
let g = llvm::LLVMAddGlobal(cx.llmod,
1159-
cx.val_ty(array),
1160-
name.as_ptr());
1161-
llvm::LLVMSetInitializer(g, array);
1162-
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
1163-
llvm::LLVMSetSection(g, section.as_ptr());
1164-
}
1228+
if !cx.used_statics().borrow().is_empty() {
1229+
cx.create_used_variable()
11651230
}
11661231

11671232
// Finalize debuginfo
11681233
if cx.sess().opts.debuginfo != DebugInfo::None {
1169-
debuginfo::finalize(&cx);
1234+
cx.debuginfo_finalize();
11701235
}
11711236

1172-
cx.stats.into_inner()
1237+
cx.consume_stats().into_inner()
11731238
};
11741239

11751240
(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
}

0 commit comments

Comments
 (0)