Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions compiler/rustc_codegen_gcc/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ use object::read::archive::ArchiveFile;
use rustc_codegen_ssa::back::lto::SerializedModule;
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput, SharedEmitter};
use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind, looks_like_rust_object_file};
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_log::tracing::info;
use rustc_session::config::Lto;
use tempfile::{TempDir, tempdir};

use crate::back::write::save_temp_bitcode;
use crate::back::write::{codegen, save_temp_bitcode};
use crate::errors::LtoBitcodeFromRlib;
use crate::{GccCodegenBackend, GccContext, LtoMode, to_gcc_opt_level};

Expand Down Expand Up @@ -112,7 +112,7 @@ pub(crate) fn run_fat(
shared_emitter: &SharedEmitter,
each_linked_rlib_for_lto: &[PathBuf],
modules: Vec<FatLtoInput<GccCodegenBackend>>,
) -> ModuleCodegen<GccContext> {
) -> CompiledModule {
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
let dcx = dcx.handle();
let lto_data = prepare_lto(cgcx, each_linked_rlib_for_lto, dcx);
Expand All @@ -132,12 +132,12 @@ pub(crate) fn run_fat(
fn fat_lto(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
_dcx: DiagCtxtHandle<'_>,
dcx: DiagCtxtHandle<'_>,
modules: Vec<FatLtoInput<GccCodegenBackend>>,
mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
tmp_path: TempDir,
//symbols_below_threshold: &[String],
) -> ModuleCodegen<GccContext> {
) -> CompiledModule {
let _timer = prof.generic_activity("GCC_fat_lto_build_monolithic_module");
info!("going for a fat lto");

Expand Down Expand Up @@ -260,7 +260,7 @@ fn fat_lto(
// of now.
module.module_llvm.temp_dir = Some(tmp_path);

module
codegen(cgcx, prof, dcx, module, &cgcx.module_config)
}

pub struct ModuleBuffer(PathBuf);
Expand Down
11 changes: 3 additions & 8 deletions compiler/rustc_codegen_gcc/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ use std::{env, fs};

use gccjit::{Context, OutputKind};
use rustc_codegen_ssa::back::link::ensure_removed;
use rustc_codegen_ssa::back::write::{
BitcodeSection, CodegenContext, EmitObj, ModuleConfig, SharedEmitter,
};
use rustc_codegen_ssa::back::write::{BitcodeSection, CodegenContext, EmitObj, ModuleConfig};
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::DiagCtxt;
use rustc_errors::DiagCtxtHandle;
use rustc_fs_util::link_or_copy;
use rustc_log::tracing::debug;
use rustc_session::config::OutputType;
Expand All @@ -20,13 +18,10 @@ use crate::{GccContext, LtoMode};
pub(crate) fn codegen(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
shared_emitter: &SharedEmitter,
dcx: DiagCtxtHandle<'_>,
module: ModuleCodegen<GccContext>,
config: &ModuleConfig,
) -> CompiledModule {
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
let dcx = dcx.handle();

let _timer = prof.generic_activity_with_arg("GCC_module_codegen", &*module.name);
{
let context = &module.module_llvm.context;
Expand Down
42 changes: 18 additions & 24 deletions compiler/rustc_codegen_gcc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen, TargetCon
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::IntoDynSyncSend;
use rustc_errors::DiagCtxtHandle;
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::ty::TyCtxt;
use rustc_middle::util::Providers;
Expand Down Expand Up @@ -369,16 +369,6 @@ impl ExtraBackendMethods for GccCodegenBackend {
self.lto_supported.load(Ordering::SeqCst),
)
}

fn target_machine_factory(
&self,
_sess: &Session,
_opt_level: OptLevel,
_features: &[String],
) -> TargetMachineFactoryFn<Self> {
// TODO(antoyo): set opt level.
Arc::new(|_, _| ())
}
}

#[derive(Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -427,7 +417,17 @@ impl WriteBackendMethods for GccCodegenBackend {
type ModuleBuffer = ModuleBuffer;
type ThinData = ();

fn run_and_optimize_fat_lto(
fn target_machine_factory(
&self,
_sess: &Session,
_opt_level: OptLevel,
_features: &[String],
) -> TargetMachineFactoryFn<Self> {
// TODO(antoyo): set opt level.
Arc::new(|_, _| ())
}

fn optimize_and_codegen_fat_lto(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
shared_emitter: &SharedEmitter,
Expand All @@ -436,7 +436,7 @@ impl WriteBackendMethods for GccCodegenBackend {
_exported_symbols_for_lto: &[String],
each_linked_rlib_for_lto: &[PathBuf],
modules: Vec<FatLtoInput<Self>>,
) -> ModuleCodegen<Self::Module> {
) -> CompiledModule {
back::lto::run_fat(cgcx, prof, shared_emitter, each_linked_rlib_for_lto, modules)
}

Expand All @@ -453,14 +453,6 @@ impl WriteBackendMethods for GccCodegenBackend {
unreachable!()
}

fn print_pass_timings(&self) {
unimplemented!();
}

fn print_statistics(&self) {
unimplemented!()
}

fn optimize(
_cgcx: &CodegenContext,
_prof: &SelfProfilerRef,
Expand All @@ -471,13 +463,13 @@ impl WriteBackendMethods for GccCodegenBackend {
module.module_llvm.context.set_optimization_level(to_gcc_opt_level(config.opt_level));
}

fn optimize_thin(
fn optimize_and_codegen_thin(
_cgcx: &CodegenContext,
_prof: &SelfProfilerRef,
_shared_emitter: &SharedEmitter,
_tm_factory: TargetMachineFactoryFn<Self>,
_thin: ThinModule<Self>,
) -> ModuleCodegen<Self::Module> {
) -> CompiledModule {
unreachable!()
}

Expand All @@ -488,7 +480,9 @@ impl WriteBackendMethods for GccCodegenBackend {
module: ModuleCodegen<Self::Module>,
config: &ModuleConfig,
) -> CompiledModule {
back::write::codegen(cgcx, prof, shared_emitter, module, config)
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
let dcx = dcx.handle();
back::write::codegen(cgcx, prof, dcx, module, config)
}

fn serialize_module(_module: Self::Module, _is_thin: bool) -> Self::ModuleBuffer {
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_codegen_llvm/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_codegen_ssa::back::write::{
CodegenContext, FatLtoInput, SharedEmitter, TargetMachineFactoryFn,
};
use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind, looks_like_rust_object_file};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::profiling::SelfProfilerRef;
Expand All @@ -24,7 +24,8 @@ use rustc_session::config::{self, Lto};
use tracing::{debug, info};

use crate::back::write::{
self, CodegenDiagnosticsStage, DiagnosticHandlers, bitcode_section_name, save_temp_bitcode,
self, CodegenDiagnosticsStage, DiagnosticHandlers, bitcode_section_name, codegen,
save_temp_bitcode,
};
use crate::errors::{LlvmError, LtoBitcodeFromRlib};
use crate::llvm::{self, build_string};
Expand Down Expand Up @@ -709,13 +710,13 @@ impl ModuleBufferMethods for ModuleBuffer {
}
}

pub(crate) fn optimize_thin_module(
pub(crate) fn optimize_and_codegen_thin_module(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
shared_emitter: &SharedEmitter,
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
thin_module: ThinModule<LlvmCodegenBackend>,
) -> ModuleCodegen<ModuleLlvm> {
) -> CompiledModule {
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
let dcx = dcx.handle();

Expand Down Expand Up @@ -794,7 +795,7 @@ pub(crate) fn optimize_thin_module(
save_temp_bitcode(cgcx, &module, "thin-lto-after-pm");
}
}
module
codegen(cgcx, prof, shared_emitter, module, &cgcx.module_config)
}

/// Maps LLVM module identifiers to their corresponding LLVM LTO cache keys
Expand Down
15 changes: 5 additions & 10 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,13 @@ pub(crate) fn save_temp_bitcode(
&module.name,
cgcx.invocation_temp.as_deref(),
);
write_bitcode_to_file(module, &path)
write_bitcode_to_file(&module.module_llvm, &path)
}

fn write_bitcode_to_file(module: &ModuleCodegen<ModuleLlvm>, path: &Path) {
fn write_bitcode_to_file(module: &ModuleLlvm, path: &Path) {
unsafe {
let path = path_to_c_string(&path);
let llmod = module.module_llvm.llmod();
let llmod = module.llmod();
llvm::LLVMWriteBitcodeToFile(llmod, path.as_ptr());
}
}
Expand Down Expand Up @@ -905,13 +905,8 @@ pub(crate) fn optimize(
let _handlers =
DiagnosticHandlers::new(cgcx, shared_emitter, llcx, module, CodegenDiagnosticsStage::Opt);

if config.emit_no_opt_bc {
let out = cgcx.output_filenames.temp_path_ext_for_cgu(
"no-opt.bc",
&module.name,
cgcx.invocation_temp.as_deref(),
);
write_bitcode_to_file(module, &out)
if module.kind == ModuleKind::Regular {
save_temp_bitcode(cgcx, module, "no-opt");
}

// FIXME(ZuseZ4): support SanitizeHWAddress and prevent illegal/unsupported opts
Expand Down
77 changes: 30 additions & 47 deletions compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,18 @@ pub(crate) use macros::TryFromU32;
#[derive(Clone)]
pub struct LlvmCodegenBackend(());

struct TimeTraceProfiler {
enabled: bool,
}
struct TimeTraceProfiler {}

impl TimeTraceProfiler {
fn new(enabled: bool) -> Self {
if enabled {
unsafe { llvm::LLVMRustTimeTraceProfilerInitialize() }
}
TimeTraceProfiler { enabled }
fn new() -> Self {
unsafe { llvm::LLVMRustTimeTraceProfilerInitialize() }
TimeTraceProfiler {}
}
}

impl Drop for TimeTraceProfiler {
fn drop(&mut self) {
if self.enabled {
unsafe { llvm::LLVMRustTimeTraceProfilerFinishThread() }
}
unsafe { llvm::LLVMRustTimeTraceProfilerFinishThread() }
}
}

Expand All @@ -122,54 +116,33 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
) -> (ModuleCodegen<ModuleLlvm>, u64) {
base::compile_codegen_unit(tcx, cgu_name)
}
fn target_machine_factory(
&self,
sess: &Session,
optlvl: OptLevel,
target_features: &[String],
) -> TargetMachineFactoryFn<Self> {
back::write::target_machine_factory(sess, optlvl, target_features)
}

fn spawn_named_thread<F, T>(
time_trace: bool,
name: String,
f: F,
) -> std::io::Result<std::thread::JoinHandle<T>>
where
F: FnOnce() -> T,
F: Send + 'static,
T: Send + 'static,
{
std::thread::Builder::new().name(name).spawn(move || {
let _profiler = TimeTraceProfiler::new(time_trace);
f()
})
}
}

impl WriteBackendMethods for LlvmCodegenBackend {
type Module = ModuleLlvm;
type ModuleBuffer = back::lto::ModuleBuffer;
type TargetMachine = OwnedTargetMachine;
type ThinData = back::lto::ThinData;
fn print_pass_timings(&self) {
let timings = llvm::build_string(|s| unsafe { llvm::LLVMRustPrintPassTimings(s) }).unwrap();
print!("{timings}");
fn thread_profiler() -> Box<dyn Any> {
Box::new(TimeTraceProfiler::new())
}
fn print_statistics(&self) {
let stats = llvm::build_string(|s| unsafe { llvm::LLVMRustPrintStatistics(s) }).unwrap();
print!("{stats}");
fn target_machine_factory(
&self,
sess: &Session,
optlvl: OptLevel,
target_features: &[String],
) -> TargetMachineFactoryFn<Self> {
back::write::target_machine_factory(sess, optlvl, target_features)
}
fn run_and_optimize_fat_lto(
fn optimize_and_codegen_fat_lto(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
shared_emitter: &SharedEmitter,
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
exported_symbols_for_lto: &[String],
each_linked_rlib_for_lto: &[PathBuf],
modules: Vec<FatLtoInput<Self>>,
) -> ModuleCodegen<Self::Module> {
) -> CompiledModule {
let mut module = back::lto::run_fat(
cgcx,
prof,
Expand All @@ -184,7 +157,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
let dcx = dcx.handle();
back::lto::run_pass_manager(cgcx, prof, dcx, &mut module, false);

module
back::write::codegen(cgcx, prof, shared_emitter, module, &cgcx.module_config)
}
fn run_thin_lto(
cgcx: &CodegenContext,
Expand Down Expand Up @@ -214,14 +187,14 @@ impl WriteBackendMethods for LlvmCodegenBackend {
) {
back::write::optimize(cgcx, prof, shared_emitter, module, config)
}
fn optimize_thin(
fn optimize_and_codegen_thin(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
shared_emitter: &SharedEmitter,
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
thin: ThinModule<Self>,
) -> ModuleCodegen<Self::Module> {
back::lto::optimize_thin_module(cgcx, prof, shared_emitter, tm_factory, thin)
) -> CompiledModule {
back::lto::optimize_and_codegen_thin_module(cgcx, prof, shared_emitter, tm_factory, thin)
}
fn codegen(
cgcx: &CodegenContext,
Expand Down Expand Up @@ -389,6 +362,16 @@ impl CodegenBackend for LlvmCodegenBackend {
(codegen_results, work_products)
}

fn print_pass_timings(&self) {
let timings = llvm::build_string(|s| unsafe { llvm::LLVMRustPrintPassTimings(s) }).unwrap();
print!("{timings}");
}

fn print_statistics(&self) {
let stats = llvm::build_string(|s| unsafe { llvm::LLVMRustPrintStatistics(s) }).unwrap();
print!("{stats}");
}

fn link(
&self,
sess: &Session,
Expand Down
Loading
Loading