Skip to content

Commit

Permalink
Generalized base:codegen_crate
Browse files Browse the repository at this point in the history
  • Loading branch information
denismerigoux authored and eddyb committed Nov 16, 2018
1 parent 97825a3 commit 8d530db
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 66 deletions.
15 changes: 7 additions & 8 deletions src/librustc_codegen_llvm/back/lto.rs
Expand Up @@ -13,8 +13,7 @@ use back::write::{ModuleConfig, with_llvm_pmb, CodegenContext};
use back::write::{self, DiagnosticHandlers, pre_lto_bitcode_filename};
use errors::{FatalError, Handler};
use llvm::archive_ro::ArchiveRO;
use llvm::{True, False};
use llvm;
use llvm::{self, True, False};
use memmap;
use rustc::dep_graph::WorkProduct;
use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
Expand Down Expand Up @@ -49,7 +48,7 @@ pub fn crate_type_allows_lto(crate_type: config::CrateType) -> bool {

pub(crate) enum LtoModuleCodegen {
Fat {
module: Option<ModuleCodegen>,
module: Option<ModuleCodegen<ModuleLlvm>>,
_serialized_bitcode: Vec<SerializedModule>,
},

Expand All @@ -73,7 +72,7 @@ impl LtoModuleCodegen {
pub(crate) unsafe fn optimize(&mut self,
cgcx: &CodegenContext,
timeline: &mut Timeline)
-> Result<ModuleCodegen, FatalError>
-> Result<ModuleCodegen<ModuleLlvm>, FatalError>
{
match *self {
LtoModuleCodegen::Fat { ref mut module, .. } => {
Expand Down Expand Up @@ -108,7 +107,7 @@ impl LtoModuleCodegen {
/// the need optimization and another for modules that can simply be copied over
/// from the incr. comp. cache.
pub(crate) fn run(cgcx: &CodegenContext,
modules: Vec<ModuleCodegen>,
modules: Vec<ModuleCodegen<ModuleLlvm>>,
cached_modules: Vec<(SerializedModule, WorkProduct)>,
timeline: &mut Timeline)
-> Result<(Vec<LtoModuleCodegen>, Vec<WorkProduct>), FatalError>
Expand Down Expand Up @@ -232,7 +231,7 @@ pub(crate) fn run(cgcx: &CodegenContext,

fn fat_lto(cgcx: &CodegenContext,
diag_handler: &Handler,
mut modules: Vec<ModuleCodegen>,
mut modules: Vec<ModuleCodegen<ModuleLlvm>>,
mut serialized_modules: Vec<(SerializedModule, CString)>,
symbol_white_list: &[*const libc::c_char],
timeline: &mut Timeline)
Expand Down Expand Up @@ -388,7 +387,7 @@ impl Drop for Linker<'a> {
/// they all go out of scope.
fn thin_lto(cgcx: &CodegenContext,
diag_handler: &Handler,
modules: Vec<ModuleCodegen>,
modules: Vec<ModuleCodegen<ModuleLlvm>>,
serialized_modules: Vec<(SerializedModule, CString)>,
cached_modules: Vec<(SerializedModule, WorkProduct)>,
symbol_white_list: &[*const libc::c_char],
Expand Down Expand Up @@ -740,7 +739,7 @@ impl ThinModule {
}

unsafe fn optimize(&mut self, cgcx: &CodegenContext, timeline: &mut Timeline)
-> Result<ModuleCodegen, FatalError>
-> Result<ModuleCodegen<ModuleLlvm>, FatalError>
{
let diag_handler = cgcx.create_diag_handler();
let tm = (cgcx.tm_factory)().map_err(|e| {
Expand Down
22 changes: 11 additions & 11 deletions src/librustc_codegen_llvm/back/write.rs
Expand Up @@ -26,7 +26,7 @@ use rustc::util::nodemap::FxHashMap;
use time_graph::{self, TimeGraph, Timeline};
use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic};
use llvm_util;
use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, // ModuleLlvm,
use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, ModuleLlvm,
CachedModuleCodegen};
use CrateInfo;
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
Expand Down Expand Up @@ -408,7 +408,7 @@ impl CodegenContext {
}
}

pub(crate) fn save_temp_bitcode(&self, module: &ModuleCodegen, name: &str) {
pub(crate) fn save_temp_bitcode(&self, module: &ModuleCodegen<ModuleLlvm>, name: &str) {
if !self.save_temps {
return
}
Expand Down Expand Up @@ -515,7 +515,7 @@ unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void
// Unsafe due to LLVM calls.
unsafe fn optimize(cgcx: &CodegenContext,
diag_handler: &Handler,
module: &ModuleCodegen,
module: &ModuleCodegen<ModuleLlvm>,
config: &ModuleConfig,
timeline: &mut Timeline)
-> Result<(), FatalError>
Expand Down Expand Up @@ -646,7 +646,7 @@ unsafe fn optimize(cgcx: &CodegenContext,
}

fn generate_lto_work(cgcx: &CodegenContext,
modules: Vec<ModuleCodegen>,
modules: Vec<ModuleCodegen<ModuleLlvm>>,
import_only_modules: Vec<(SerializedModule, WorkProduct)>)
-> Vec<(WorkItem, u64)>
{
Expand Down Expand Up @@ -675,7 +675,7 @@ fn generate_lto_work(cgcx: &CodegenContext,

unsafe fn codegen(cgcx: &CodegenContext,
diag_handler: &Handler,
module: ModuleCodegen,
module: ModuleCodegen<ModuleLlvm>,
config: &ModuleConfig,
timeline: &mut Timeline)
-> Result<CompiledModule, FatalError>
Expand Down Expand Up @@ -1284,7 +1284,7 @@ pub(crate) fn dump_incremental_data(_codegen_results: &CodegenResults) {

enum WorkItem {
/// Optimize a newly codegened, totally unoptimized module.
Optimize(ModuleCodegen),
Optimize(ModuleCodegen<ModuleLlvm>),
/// Copy the post-LTO artifacts from the incremental cache to the output
/// directory.
CopyPostLtoArtifacts(CachedModuleCodegen),
Expand Down Expand Up @@ -1312,7 +1312,7 @@ impl WorkItem {

enum WorkItemResult {
Compiled(CompiledModule),
NeedsLTO(ModuleCodegen),
NeedsLTO(ModuleCodegen<ModuleLlvm>),
}

fn execute_work_item(cgcx: &CodegenContext,
Expand All @@ -1336,7 +1336,7 @@ fn execute_work_item(cgcx: &CodegenContext,
}

fn execute_optimize_work_item(cgcx: &CodegenContext,
module: ModuleCodegen,
module: ModuleCodegen<ModuleLlvm>,
module_config: &ModuleConfig,
timeline: &mut Timeline)
-> Result<WorkItemResult, FatalError>
Expand Down Expand Up @@ -1480,7 +1480,7 @@ fn execute_lto_work_item(cgcx: &CodegenContext,
enum Message {
Token(io::Result<Acquired>),
NeedsLTO {
result: ModuleCodegen,
result: ModuleCodegen<ModuleLlvm>,
worker_id: usize,
},
Done {
Expand Down Expand Up @@ -2445,7 +2445,7 @@ impl OngoingCodegen {

pub(crate) fn submit_pre_codegened_module_to_llvm(&self,
tcx: TyCtxt,
module: ModuleCodegen) {
module: ModuleCodegen<ModuleLlvm>) {
self.wait_for_signal_to_codegen_item();
self.check_for_errors(tcx.sess);

Expand Down Expand Up @@ -2497,7 +2497,7 @@ impl OngoingCodegen {
// }

pub(crate) fn submit_codegened_module_to_llvm(tcx: TyCtxt,
module: ModuleCodegen,
module: ModuleCodegen<ModuleLlvm>,
cost: u64) {
let llvm_work_item = WorkItem::Optimize(module);
drop(tcx.tx_to_llvm_workers.lock().send(Box::new(Message::CodegenDone {
Expand Down
80 changes: 40 additions & 40 deletions src/librustc_codegen_llvm/base.rs
Expand Up @@ -29,7 +29,7 @@ use super::ModuleKind;
use super::CachedModuleCodegen;

use abi;
use back::write::{self, OngoingCodegen};
use back::write;
use llvm;
use metadata;
use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
Expand All @@ -48,7 +48,6 @@ use rustc::util::profiling::ProfileCategory;
use rustc::session::config::{self, DebugInfo, EntryFnType, Lto};
use rustc::session::Session;
use rustc_incremental;
use allocator;
use mir::place::PlaceRef;
use builder::{Builder, MemFlags};
use callee;
Expand Down Expand Up @@ -584,9 +583,10 @@ fn maybe_create_entry_wrapper<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
}
}

fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
llvm_module: &ModuleLlvm)
-> EncodedMetadata {
pub(crate) fn write_metadata<'a, 'gcx>(
tcx: TyCtxt<'a, 'gcx, 'gcx>,
llvm_module: &ModuleLlvm
) -> EncodedMetadata {
use std::io::Write;
use flate2::Compression;
use flate2::write::DeflateEncoder;
Expand Down Expand Up @@ -713,10 +713,12 @@ fn determine_cgu_reuse<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}
}

pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
rx: mpsc::Receiver<Box<dyn Any + Send>>)
-> OngoingCodegen
{
pub fn codegen_crate<'a, 'tcx, B: BackendMethods>(
backend: B,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
rx: mpsc::Receiver<Box<dyn Any + Send>>
) -> B::OngoingCodegen {

check_for_rustc_errors_attr(tcx);

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

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

ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
ongoing_codegen.codegen_finished(tcx);
backend.submit_pre_codegened_module_to_llvm(&ongoing_codegen, tcx, metadata_module);
backend.codegen_finished(&ongoing_codegen, tcx);

assert_and_save_dep_graph(tcx);

ongoing_codegen.check_for_errors(tcx.sess);
backend.check_for_errors(&ongoing_codegen, tcx.sess);

return ongoing_codegen;
}
Expand All @@ -782,13 +784,13 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}
}

let ongoing_codegen = write::start_async_codegen(
let ongoing_codegen = backend.start_async_codegen(
tcx,
time_graph.clone(),
metadata,
rx,
codegen_units.len());
let ongoing_codegen = AbortCodegenOnDrop(Some(ongoing_codegen));
let ongoing_codegen = AbortCodegenOnDrop::<B>(Some(ongoing_codegen));

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

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

if let Some(allocator_module) = allocator_module {
ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, allocator_module);
backend.submit_pre_codegened_module_to_llvm(&ongoing_codegen, tcx, allocator_module);
}

ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
backend.submit_pre_codegened_module_to_llvm(&ongoing_codegen, tcx, metadata_module);

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

for cgu in codegen_units.into_iter() {
ongoing_codegen.wait_for_signal_to_codegen_item();
ongoing_codegen.check_for_errors(tcx.sess);
backend.wait_for_signal_to_codegen_item(&ongoing_codegen);
backend.check_for_errors(&ongoing_codegen, tcx.sess);

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

ongoing_codegen.codegen_finished(tcx);
backend.codegen_finished(&ongoing_codegen, tcx);

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

ongoing_codegen.check_for_errors(tcx.sess);
backend.check_for_errors(&ongoing_codegen, tcx.sess);

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

impl AbortCodegenOnDrop {
fn into_inner(mut self) -> OngoingCodegen {
impl<B: BackendMethods> AbortCodegenOnDrop<B> {
fn into_inner(mut self) -> B::OngoingCodegen {
self.0.take().unwrap()
}
}

impl Deref for AbortCodegenOnDrop {
type Target = OngoingCodegen;
impl<B: BackendMethods> Deref for AbortCodegenOnDrop<B> {
type Target = B::OngoingCodegen;

fn deref(&self) -> &OngoingCodegen {
fn deref(&self) -> &B::OngoingCodegen {
self.0.as_ref().unwrap()
}
}

impl DerefMut for AbortCodegenOnDrop {
fn deref_mut(&mut self) -> &mut OngoingCodegen {
impl<B: BackendMethods> DerefMut for AbortCodegenOnDrop<B> {
fn deref_mut(&mut self) -> &mut B::OngoingCodegen {
self.0.as_mut().unwrap()
}
}

impl Drop for AbortCodegenOnDrop {
impl<B: BackendMethods> Drop for AbortCodegenOnDrop<B> {
fn drop(&mut self) {
if let Some(codegen) = self.0.take() {
codegen.codegen_aborted();
B::codegen_aborted(codegen);
}
}
}
Expand Down Expand Up @@ -1092,7 +1092,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
fn module_codegen<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
cgu_name: InternedString)
-> (Stats, ModuleCodegen)
-> (Stats, ModuleCodegen<ModuleLlvm>)
{
let cgu = tcx.codegen_unit(cgu_name);

Expand Down Expand Up @@ -1226,9 +1226,9 @@ pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
mod temp_stable_hash_impls {
use rustc_data_structures::stable_hasher::{StableHasherResult, StableHasher,
HashStable};
use ModuleCodegen;
use {ModuleCodegen, ModuleLlvm};

impl<HCX> HashStable<HCX> for ModuleCodegen {
impl<HCX> HashStable<HCX> for ModuleCodegen<ModuleLlvm> {
fn hash_stable<W: StableHasherResult>(&self,
_: &mut HCX,
_: &mut StableHasher<W>) {
Expand Down

0 comments on commit 8d530db

Please sign in to comment.