Skip to content

Commit

Permalink
Move SelfProfilerRef out of CodegenCx
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Dec 5, 2024
1 parent 357deaa commit 623a6da
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use cranelift_module::ModuleError;
use rustc_ast::InlineAsmOptions;
use rustc_codegen_ssa::base::is_call_from_compiler_builtins_to_upstream_monomorphization;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_index::IndexVec;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mir::InlineAsmMacro;
Expand Down Expand Up @@ -170,12 +171,13 @@ pub(crate) fn codegen_fn<'tcx>(

pub(crate) fn compile_fn(
cx: &mut crate::CodegenCx,
profiler: &SelfProfilerRef,
cached_context: &mut Context,
module: &mut dyn Module,
codegened_func: CodegenedFunction,
) {
let _timer =
cx.profiler.generic_activity_with_arg("compile function", &*codegened_func.symbol_name);
profiler.generic_activity_with_arg("compile function", &*codegened_func.symbol_name);

let clif_comments = codegened_func.clif_comments;

Expand Down Expand Up @@ -213,7 +215,7 @@ pub(crate) fn compile_fn(
};

// Define function
cx.profiler.generic_activity("define function").run(|| {
profiler.generic_activity("define function").run(|| {
context.want_disasm = cx.should_write_ir;
match module.define_function(codegened_func.func_id, context) {
Ok(()) => {}
Expand Down Expand Up @@ -254,7 +256,7 @@ pub(crate) fn compile_fn(

// Define debuginfo for function
let debug_context = &mut cx.debug_context;
cx.profiler.generic_activity("generate debug info").run(|| {
profiler.generic_activity("generate debug info").run(|| {
if let Some(debug_context) = debug_context {
codegened_func.func_debug_cx.unwrap().finalize(
debug_context,
Expand Down
20 changes: 14 additions & 6 deletions src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,28 +548,36 @@ fn module_codegen(

let producer = crate::debuginfo::producer(tcx.sess);

let profiler = tcx.prof.clone();

OngoingModuleCodegen::Async(std::thread::spawn(move || {
cx.profiler.clone().generic_activity_with_arg("compile functions", &*cgu_name).run(|| {
profiler.clone().generic_activity_with_arg("compile functions", &*cgu_name).run(|| {
cranelift_codegen::timing::set_thread_profiler(Box::new(super::MeasuremeProfiler(
cx.profiler.clone(),
profiler.clone(),
)));

let mut cached_context = Context::new();
for codegened_func in codegened_functions {
crate::base::compile_fn(&mut cx, &mut cached_context, &mut module, codegened_func);
crate::base::compile_fn(
&mut cx,
&profiler,
&mut cached_context,
&mut module,
codegened_func,
);
}
});

let global_asm_object_file =
cx.profiler.generic_activity_with_arg("compile assembly", &*cgu_name).run(|| {
profiler.generic_activity_with_arg("compile assembly", &*cgu_name).run(|| {
crate::global_asm::compile_global_asm(&global_asm_config, &cgu_name, &cx.global_asm)
})?;

let codegen_result =
cx.profiler.generic_activity_with_arg("write object file", &*cgu_name).run(|| {
profiler.generic_activity_with_arg("write object file", &*cgu_name).run(|| {
emit_cgu(
&global_asm_config.output_filenames,
&cx.profiler,
&profiler,
cgu_name,
module,
cx.debug_context,
Expand Down
4 changes: 2 additions & 2 deletions src/driver/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub(crate) fn codegen_and_compile_fn<'tcx>(
instance: Instance<'tcx>,
) {
cranelift_codegen::timing::set_thread_profiler(Box::new(super::MeasuremeProfiler(
cx.profiler.clone(),
tcx.prof.clone(),
)));

tcx.prof.generic_activity("codegen and compile fn").run(|| {
Expand All @@ -220,7 +220,7 @@ pub(crate) fn codegen_and_compile_fn<'tcx>(
module,
instance,
) {
crate::base::compile_fn(cx, cached_context, module, codegened_func);
crate::base::compile_fn(cx, &tcx.prof, cached_context, module, codegened_func);
}
});
}
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ use cranelift_codegen::settings::{self, Configurable};
use rustc_codegen_ssa::CodegenResults;
use rustc_codegen_ssa::back::versioned_llvm_target;
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_session::Session;
Expand Down Expand Up @@ -123,7 +122,6 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
/// The codegen context holds any information shared between the codegen of individual functions
/// inside a single codegen unit with the exception of the Cranelift [`Module`](cranelift_module::Module).
struct CodegenCx {
profiler: SelfProfilerRef,
output_filenames: Arc<OutputFilenames>,
should_write_ir: bool,
global_asm: String,
Expand All @@ -142,7 +140,6 @@ impl CodegenCx {
None
};
CodegenCx {
profiler: tcx.prof.clone(),
output_filenames: tcx.output_filenames(()).clone(),
should_write_ir: crate::pretty_clif::should_write_ir(tcx),
global_asm: String::new(),
Expand Down

0 comments on commit 623a6da

Please sign in to comment.