Skip to content

Commit 8d530db

Browse files
denismerigouxeddyb
authored andcommitted
Generalized base:codegen_crate
1 parent 97825a3 commit 8d530db

File tree

6 files changed

+160
-66
lines changed

6 files changed

+160
-66
lines changed

src/librustc_codegen_llvm/back/lto.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use back::write::{ModuleConfig, with_llvm_pmb, CodegenContext};
1313
use back::write::{self, DiagnosticHandlers, pre_lto_bitcode_filename};
1414
use errors::{FatalError, Handler};
1515
use llvm::archive_ro::ArchiveRO;
16-
use llvm::{True, False};
17-
use llvm;
16+
use llvm::{self, True, False};
1817
use memmap;
1918
use rustc::dep_graph::WorkProduct;
2019
use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
@@ -49,7 +48,7 @@ pub fn crate_type_allows_lto(crate_type: config::CrateType) -> bool {
4948

5049
pub(crate) enum LtoModuleCodegen {
5150
Fat {
52-
module: Option<ModuleCodegen>,
51+
module: Option<ModuleCodegen<ModuleLlvm>>,
5352
_serialized_bitcode: Vec<SerializedModule>,
5453
},
5554

@@ -73,7 +72,7 @@ impl LtoModuleCodegen {
7372
pub(crate) unsafe fn optimize(&mut self,
7473
cgcx: &CodegenContext,
7574
timeline: &mut Timeline)
76-
-> Result<ModuleCodegen, FatalError>
75+
-> Result<ModuleCodegen<ModuleLlvm>, FatalError>
7776
{
7877
match *self {
7978
LtoModuleCodegen::Fat { ref mut module, .. } => {
@@ -108,7 +107,7 @@ impl LtoModuleCodegen {
108107
/// the need optimization and another for modules that can simply be copied over
109108
/// from the incr. comp. cache.
110109
pub(crate) fn run(cgcx: &CodegenContext,
111-
modules: Vec<ModuleCodegen>,
110+
modules: Vec<ModuleCodegen<ModuleLlvm>>,
112111
cached_modules: Vec<(SerializedModule, WorkProduct)>,
113112
timeline: &mut Timeline)
114113
-> Result<(Vec<LtoModuleCodegen>, Vec<WorkProduct>), FatalError>
@@ -232,7 +231,7 @@ pub(crate) fn run(cgcx: &CodegenContext,
232231

233232
fn fat_lto(cgcx: &CodegenContext,
234233
diag_handler: &Handler,
235-
mut modules: Vec<ModuleCodegen>,
234+
mut modules: Vec<ModuleCodegen<ModuleLlvm>>,
236235
mut serialized_modules: Vec<(SerializedModule, CString)>,
237236
symbol_white_list: &[*const libc::c_char],
238237
timeline: &mut Timeline)
@@ -388,7 +387,7 @@ impl Drop for Linker<'a> {
388387
/// they all go out of scope.
389388
fn thin_lto(cgcx: &CodegenContext,
390389
diag_handler: &Handler,
391-
modules: Vec<ModuleCodegen>,
390+
modules: Vec<ModuleCodegen<ModuleLlvm>>,
392391
serialized_modules: Vec<(SerializedModule, CString)>,
393392
cached_modules: Vec<(SerializedModule, WorkProduct)>,
394393
symbol_white_list: &[*const libc::c_char],
@@ -740,7 +739,7 @@ impl ThinModule {
740739
}
741740

742741
unsafe fn optimize(&mut self, cgcx: &CodegenContext, timeline: &mut Timeline)
743-
-> Result<ModuleCodegen, FatalError>
742+
-> Result<ModuleCodegen<ModuleLlvm>, FatalError>
744743
{
745744
let diag_handler = cgcx.create_diag_handler();
746745
let tm = (cgcx.tm_factory)().map_err(|e| {

src/librustc_codegen_llvm/back/write.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc::util::nodemap::FxHashMap;
2626
use time_graph::{self, TimeGraph, Timeline};
2727
use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic};
2828
use llvm_util;
29-
use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, // ModuleLlvm,
29+
use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, ModuleLlvm,
3030
CachedModuleCodegen};
3131
use CrateInfo;
3232
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
@@ -408,7 +408,7 @@ impl CodegenContext {
408408
}
409409
}
410410

411-
pub(crate) fn save_temp_bitcode(&self, module: &ModuleCodegen, name: &str) {
411+
pub(crate) fn save_temp_bitcode(&self, module: &ModuleCodegen<ModuleLlvm>, name: &str) {
412412
if !self.save_temps {
413413
return
414414
}
@@ -515,7 +515,7 @@ unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void
515515
// Unsafe due to LLVM calls.
516516
unsafe fn optimize(cgcx: &CodegenContext,
517517
diag_handler: &Handler,
518-
module: &ModuleCodegen,
518+
module: &ModuleCodegen<ModuleLlvm>,
519519
config: &ModuleConfig,
520520
timeline: &mut Timeline)
521521
-> Result<(), FatalError>
@@ -646,7 +646,7 @@ unsafe fn optimize(cgcx: &CodegenContext,
646646
}
647647

648648
fn generate_lto_work(cgcx: &CodegenContext,
649-
modules: Vec<ModuleCodegen>,
649+
modules: Vec<ModuleCodegen<ModuleLlvm>>,
650650
import_only_modules: Vec<(SerializedModule, WorkProduct)>)
651651
-> Vec<(WorkItem, u64)>
652652
{
@@ -675,7 +675,7 @@ fn generate_lto_work(cgcx: &CodegenContext,
675675

676676
unsafe fn codegen(cgcx: &CodegenContext,
677677
diag_handler: &Handler,
678-
module: ModuleCodegen,
678+
module: ModuleCodegen<ModuleLlvm>,
679679
config: &ModuleConfig,
680680
timeline: &mut Timeline)
681681
-> Result<CompiledModule, FatalError>
@@ -1284,7 +1284,7 @@ pub(crate) fn dump_incremental_data(_codegen_results: &CodegenResults) {
12841284

12851285
enum WorkItem {
12861286
/// Optimize a newly codegened, totally unoptimized module.
1287-
Optimize(ModuleCodegen),
1287+
Optimize(ModuleCodegen<ModuleLlvm>),
12881288
/// Copy the post-LTO artifacts from the incremental cache to the output
12891289
/// directory.
12901290
CopyPostLtoArtifacts(CachedModuleCodegen),
@@ -1312,7 +1312,7 @@ impl WorkItem {
13121312

13131313
enum WorkItemResult {
13141314
Compiled(CompiledModule),
1315-
NeedsLTO(ModuleCodegen),
1315+
NeedsLTO(ModuleCodegen<ModuleLlvm>),
13161316
}
13171317

13181318
fn execute_work_item(cgcx: &CodegenContext,
@@ -1336,7 +1336,7 @@ fn execute_work_item(cgcx: &CodegenContext,
13361336
}
13371337

13381338
fn execute_optimize_work_item(cgcx: &CodegenContext,
1339-
module: ModuleCodegen,
1339+
module: ModuleCodegen<ModuleLlvm>,
13401340
module_config: &ModuleConfig,
13411341
timeline: &mut Timeline)
13421342
-> Result<WorkItemResult, FatalError>
@@ -1480,7 +1480,7 @@ fn execute_lto_work_item(cgcx: &CodegenContext,
14801480
enum Message {
14811481
Token(io::Result<Acquired>),
14821482
NeedsLTO {
1483-
result: ModuleCodegen,
1483+
result: ModuleCodegen<ModuleLlvm>,
14841484
worker_id: usize,
14851485
},
14861486
Done {
@@ -2445,7 +2445,7 @@ impl OngoingCodegen {
24452445

24462446
pub(crate) fn submit_pre_codegened_module_to_llvm(&self,
24472447
tcx: TyCtxt,
2448-
module: ModuleCodegen) {
2448+
module: ModuleCodegen<ModuleLlvm>) {
24492449
self.wait_for_signal_to_codegen_item();
24502450
self.check_for_errors(tcx.sess);
24512451

@@ -2497,7 +2497,7 @@ impl OngoingCodegen {
24972497
// }
24982498

24992499
pub(crate) fn submit_codegened_module_to_llvm(tcx: TyCtxt,
2500-
module: ModuleCodegen,
2500+
module: ModuleCodegen<ModuleLlvm>,
25012501
cost: u64) {
25022502
let llvm_work_item = WorkItem::Optimize(module);
25032503
drop(tcx.tx_to_llvm_workers.lock().send(Box::new(Message::CodegenDone {

src/librustc_codegen_llvm/base.rs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use super::ModuleKind;
2929
use super::CachedModuleCodegen;
3030

3131
use abi;
32-
use back::write::{self, OngoingCodegen};
32+
use back::write;
3333
use llvm;
3434
use metadata;
3535
use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
@@ -48,7 +48,6 @@ use rustc::util::profiling::ProfileCategory;
4848
use rustc::session::config::{self, DebugInfo, EntryFnType, Lto};
4949
use rustc::session::Session;
5050
use rustc_incremental;
51-
use allocator;
5251
use mir::place::PlaceRef;
5352
use builder::{Builder, MemFlags};
5453
use callee;
@@ -584,9 +583,10 @@ fn maybe_create_entry_wrapper<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
584583
}
585584
}
586585

587-
fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
588-
llvm_module: &ModuleLlvm)
589-
-> EncodedMetadata {
586+
pub(crate) fn write_metadata<'a, 'gcx>(
587+
tcx: TyCtxt<'a, 'gcx, 'gcx>,
588+
llvm_module: &ModuleLlvm
589+
) -> EncodedMetadata {
590590
use std::io::Write;
591591
use flate2::Compression;
592592
use flate2::write::DeflateEncoder;
@@ -713,10 +713,12 @@ fn determine_cgu_reuse<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
713713
}
714714
}
715715

716-
pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
717-
rx: mpsc::Receiver<Box<dyn Any + Send>>)
718-
-> OngoingCodegen
719-
{
716+
pub fn codegen_crate<'a, 'tcx, B: BackendMethods>(
717+
backend: B,
718+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
719+
rx: mpsc::Receiver<Box<dyn Any + Send>>
720+
) -> B::OngoingCodegen {
721+
720722
check_for_rustc_errors_attr(tcx);
721723

722724
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
@@ -728,9 +730,9 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
728730
&["crate"],
729731
Some("metadata")).as_str()
730732
.to_string();
731-
let metadata_llvm_module = ModuleLlvm::new(tcx.sess, &metadata_cgu_name);
733+
let metadata_llvm_module = backend.new_metadata(tcx.sess, &metadata_cgu_name);
732734
let metadata = time(tcx.sess, "write metadata", || {
733-
write_metadata(tcx, &metadata_llvm_module)
735+
backend.write_metadata(tcx, &metadata_llvm_module)
734736
});
735737
tcx.sess.profiler(|p| p.end_activity(ProfileCategory::Codegen));
736738

@@ -749,19 +751,19 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
749751
// Skip crate items and just output metadata in -Z no-codegen mode.
750752
if tcx.sess.opts.debugging_opts.no_codegen ||
751753
!tcx.sess.opts.output_types.should_codegen() {
752-
let ongoing_codegen = write::start_async_codegen(
754+
let ongoing_codegen = backend.start_async_codegen(
753755
tcx,
754756
time_graph,
755757
metadata,
756758
rx,
757759
1);
758760

759-
ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
760-
ongoing_codegen.codegen_finished(tcx);
761+
backend.submit_pre_codegened_module_to_llvm(&ongoing_codegen, tcx, metadata_module);
762+
backend.codegen_finished(&ongoing_codegen, tcx);
761763

762764
assert_and_save_dep_graph(tcx);
763765

764-
ongoing_codegen.check_for_errors(tcx.sess);
766+
backend.check_for_errors(&ongoing_codegen, tcx.sess);
765767

766768
return ongoing_codegen;
767769
}
@@ -782,13 +784,13 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
782784
}
783785
}
784786

785-
let ongoing_codegen = write::start_async_codegen(
787+
let ongoing_codegen = backend.start_async_codegen(
786788
tcx,
787789
time_graph.clone(),
788790
metadata,
789791
rx,
790792
codegen_units.len());
791-
let ongoing_codegen = AbortCodegenOnDrop(Some(ongoing_codegen));
793+
let ongoing_codegen = AbortCodegenOnDrop::<B>(Some(ongoing_codegen));
792794

793795
// Codegen an allocator shim, if necessary.
794796
//
@@ -811,11 +813,9 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
811813
&["crate"],
812814
Some("allocator")).as_str()
813815
.to_string();
814-
let modules = ModuleLlvm::new(tcx.sess, &llmod_id);
816+
let modules = backend.new_metadata(tcx.sess, &llmod_id);
815817
time(tcx.sess, "write allocator module", || {
816-
unsafe {
817-
allocator::codegen(tcx, &modules, kind)
818-
}
818+
backend.codegen_allocator(tcx, &modules, kind)
819819
});
820820

821821
Some(ModuleCodegen {
@@ -828,10 +828,10 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
828828
};
829829

830830
if let Some(allocator_module) = allocator_module {
831-
ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, allocator_module);
831+
backend.submit_pre_codegened_module_to_llvm(&ongoing_codegen, tcx, allocator_module);
832832
}
833833

834-
ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
834+
backend.submit_pre_codegened_module_to_llvm(&ongoing_codegen, tcx, metadata_module);
835835

836836
// We sort the codegen units by size. This way we can schedule work for LLVM
837837
// a bit more efficiently.
@@ -845,8 +845,8 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
845845
let mut all_stats = Stats::default();
846846

847847
for cgu in codegen_units.into_iter() {
848-
ongoing_codegen.wait_for_signal_to_codegen_item();
849-
ongoing_codegen.check_for_errors(tcx.sess);
848+
backend.wait_for_signal_to_codegen_item(&ongoing_codegen);
849+
backend.check_for_errors(&ongoing_codegen, tcx.sess);
850850

851851
let cgu_reuse = determine_cgu_reuse(tcx, &cgu);
852852
tcx.sess.cgu_reuse_tracker.set_actual_reuse(&cgu.name().as_str(), cgu_reuse);
@@ -881,7 +881,7 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
881881
};
882882
}
883883

884-
ongoing_codegen.codegen_finished(tcx);
884+
backend.codegen_finished(&ongoing_codegen, tcx);
885885

886886
// Since the main thread is sometimes blocked during codegen, we keep track
887887
// -Ztime-passes output manually.
@@ -915,7 +915,7 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
915915
}
916916
}
917917

918-
ongoing_codegen.check_for_errors(tcx.sess);
918+
backend.check_for_errors(&ongoing_codegen, tcx.sess);
919919

920920
assert_and_save_dep_graph(tcx);
921921
ongoing_codegen.into_inner()
@@ -938,32 +938,32 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
938938
/// If you see this comment in the code, then it means that this workaround
939939
/// worked! We may yet one day track down the mysterious cause of that
940940
/// segfault...
941-
struct AbortCodegenOnDrop(Option<OngoingCodegen>);
941+
struct AbortCodegenOnDrop<B: BackendMethods>(Option<B::OngoingCodegen>);
942942

943-
impl AbortCodegenOnDrop {
944-
fn into_inner(mut self) -> OngoingCodegen {
943+
impl<B: BackendMethods> AbortCodegenOnDrop<B> {
944+
fn into_inner(mut self) -> B::OngoingCodegen {
945945
self.0.take().unwrap()
946946
}
947947
}
948948

949-
impl Deref for AbortCodegenOnDrop {
950-
type Target = OngoingCodegen;
949+
impl<B: BackendMethods> Deref for AbortCodegenOnDrop<B> {
950+
type Target = B::OngoingCodegen;
951951

952-
fn deref(&self) -> &OngoingCodegen {
952+
fn deref(&self) -> &B::OngoingCodegen {
953953
self.0.as_ref().unwrap()
954954
}
955955
}
956956

957-
impl DerefMut for AbortCodegenOnDrop {
958-
fn deref_mut(&mut self) -> &mut OngoingCodegen {
957+
impl<B: BackendMethods> DerefMut for AbortCodegenOnDrop<B> {
958+
fn deref_mut(&mut self) -> &mut B::OngoingCodegen {
959959
self.0.as_mut().unwrap()
960960
}
961961
}
962962

963-
impl Drop for AbortCodegenOnDrop {
963+
impl<B: BackendMethods> Drop for AbortCodegenOnDrop<B> {
964964
fn drop(&mut self) {
965965
if let Some(codegen) = self.0.take() {
966-
codegen.codegen_aborted();
966+
B::codegen_aborted(codegen);
967967
}
968968
}
969969
}
@@ -1092,7 +1092,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10921092
fn module_codegen<'a, 'tcx>(
10931093
tcx: TyCtxt<'a, 'tcx, 'tcx>,
10941094
cgu_name: InternedString)
1095-
-> (Stats, ModuleCodegen)
1095+
-> (Stats, ModuleCodegen<ModuleLlvm>)
10961096
{
10971097
let cgu = tcx.codegen_unit(cgu_name);
10981098

@@ -1226,9 +1226,9 @@ pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
12261226
mod temp_stable_hash_impls {
12271227
use rustc_data_structures::stable_hasher::{StableHasherResult, StableHasher,
12281228
HashStable};
1229-
use ModuleCodegen;
1229+
use {ModuleCodegen, ModuleLlvm};
12301230

1231-
impl<HCX> HashStable<HCX> for ModuleCodegen {
1231+
impl<HCX> HashStable<HCX> for ModuleCodegen<ModuleLlvm> {
12321232
fn hash_stable<W: StableHasherResult>(&self,
12331233
_: &mut HCX,
12341234
_: &mut StableHasher<W>) {

0 commit comments

Comments
 (0)