Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ef07e8e

Browse files
committed
Integrate better with Cranelift's profiling infrastructure
1 parent 517b5c1 commit ef07e8e

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

src/base.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -172,27 +172,6 @@ pub(crate) fn compile_fn(
172172
cx.profiler.generic_activity("define function").run(|| {
173173
context.want_disasm = cx.should_write_ir;
174174
module.define_function(codegened_func.func_id, context).unwrap();
175-
176-
if cx.profiler.enabled() {
177-
let mut recording_args = false;
178-
cx.profiler
179-
.generic_activity_with_arg_recorder(
180-
"define function (clif pass timings)",
181-
|recorder| {
182-
let pass_times = cranelift_codegen::timing::take_current();
183-
// Replace newlines with | as measureme doesn't allow control characters like
184-
// newlines inside strings.
185-
recorder.record_arg(format!("{}", pass_times).replace('\n', " | "));
186-
recording_args = true;
187-
},
188-
)
189-
.run(|| {
190-
if recording_args {
191-
// Wait a tiny bit to ensure chrome's profiler doesn't hide the event
192-
std::thread::sleep(std::time::Duration::from_nanos(2))
193-
}
194-
});
195-
}
196175
});
197176

198177
if cx.should_write_ir {

src/driver/aot.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ fn module_codegen(
324324
OngoingModuleCodegen::Async(std::thread::spawn(move || {
325325
cx.profiler.clone().verbose_generic_activity_with_arg("compile functions", &*cgu_name).run(
326326
|| {
327+
cranelift_codegen::timing::set_thread_profiler(Box::new(super::MeasuremeProfiler(
328+
cx.profiler.clone(),
329+
)));
330+
327331
let mut cached_context = Context::new();
328332
for codegened_func in codegened_functions {
329333
crate::base::compile_fn(

src/driver/jit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ pub(crate) fn codegen_and_compile_fn<'tcx>(
224224
module: &mut dyn Module,
225225
instance: Instance<'tcx>,
226226
) {
227+
cranelift_codegen::timing::set_thread_profiler(Box::new(super::MeasuremeProfiler(
228+
cx.profiler.clone(),
229+
)));
230+
227231
tcx.prof.generic_activity("codegen and compile fn").run(|| {
228232
let _inst_guard =
229233
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));

src/driver/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! [`codegen_fn`]: crate::base::codegen_fn
55
//! [`codegen_static`]: crate::constant::codegen_static
66
7+
use rustc_data_structures::profiling::SelfProfilerRef;
78
use rustc_middle::mir::mono::{Linkage as RLinkage, MonoItem, Visibility};
89

910
use crate::prelude::*;
@@ -39,3 +40,31 @@ fn predefine_mono_items<'tcx>(
3940
}
4041
});
4142
}
43+
44+
struct MeasuremeProfiler(SelfProfilerRef);
45+
46+
struct TimingGuard {
47+
profiler: std::mem::ManuallyDrop<SelfProfilerRef>,
48+
inner: Option<rustc_data_structures::profiling::TimingGuard<'static>>,
49+
}
50+
51+
impl Drop for TimingGuard {
52+
fn drop(&mut self) {
53+
self.inner.take();
54+
unsafe {
55+
std::mem::ManuallyDrop::drop(&mut self.profiler);
56+
}
57+
}
58+
}
59+
60+
impl cranelift_codegen::timing::Profiler for MeasuremeProfiler {
61+
fn start_pass(&self, pass: cranelift_codegen::timing::Pass) -> Box<dyn std::any::Any> {
62+
let mut timing_guard =
63+
TimingGuard { profiler: std::mem::ManuallyDrop::new(self.0.clone()), inner: None };
64+
timing_guard.inner = Some(
65+
unsafe { &*(&*timing_guard.profiler as &SelfProfilerRef as *const SelfProfilerRef) }
66+
.generic_activity(pass.description()),
67+
);
68+
Box::new(timing_guard)
69+
}
70+
}

0 commit comments

Comments
 (0)