Skip to content

Commit d4bd159

Browse files
committed
Replace spawn_named_thread method with thread_profiler
1 parent 14701f8 commit d4bd159

3 files changed

Lines changed: 29 additions & 45 deletions

File tree

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,18 @@ pub(crate) use macros::TryFromU32;
7979
#[derive(Clone)]
8080
pub struct LlvmCodegenBackend(());
8181

82-
struct TimeTraceProfiler {
83-
enabled: bool,
84-
}
82+
struct TimeTraceProfiler {}
8583

8684
impl TimeTraceProfiler {
87-
fn new(enabled: bool) -> Self {
88-
if enabled {
89-
unsafe { llvm::LLVMRustTimeTraceProfilerInitialize() }
90-
}
91-
TimeTraceProfiler { enabled }
85+
fn new() -> Self {
86+
unsafe { llvm::LLVMRustTimeTraceProfilerInitialize() }
87+
TimeTraceProfiler {}
9288
}
9389
}
9490

9591
impl Drop for TimeTraceProfiler {
9692
fn drop(&mut self) {
97-
if self.enabled {
98-
unsafe { llvm::LLVMRustTimeTraceProfilerFinishThread() }
99-
}
93+
unsafe { llvm::LLVMRustTimeTraceProfilerFinishThread() }
10094
}
10195
}
10296

@@ -131,20 +125,8 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
131125
back::write::target_machine_factory(sess, optlvl, target_features)
132126
}
133127

134-
fn spawn_named_thread<F, T>(
135-
time_trace: bool,
136-
name: String,
137-
f: F,
138-
) -> std::io::Result<std::thread::JoinHandle<T>>
139-
where
140-
F: FnOnce() -> T,
141-
F: Send + 'static,
142-
T: Send + 'static,
143-
{
144-
std::thread::Builder::new().name(name).spawn(move || {
145-
let _profiler = TimeTraceProfiler::new(time_trace);
146-
f()
147-
})
128+
fn thread_profiler() -> Box<dyn Any> {
129+
Box::new(TimeTraceProfiler::new())
148130
}
149131
}
150132

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,9 @@ fn start_executing_work<B: ExtraBackendMethods>(
14881488
// Each LLVM module is automatically sent back to the coordinator for LTO if
14891489
// necessary. There's already optimizations in place to avoid sending work
14901490
// back to the coordinator if LTO isn't requested.
1491-
return B::spawn_named_thread(cgcx.time_trace, "coordinator".to_string(), move || {
1491+
let f = move || {
1492+
let _profiler = if cgcx.time_trace { B::thread_profiler() } else { Box::new(()) };
1493+
14921494
// This is where we collect codegen units that have gone all the way
14931495
// through codegen and LLVM.
14941496
let mut compiled_modules = vec![];
@@ -1829,8 +1831,11 @@ fn start_executing_work<B: ExtraBackendMethods>(
18291831
B::codegen(&cgcx, &prof, &shared_emitter, allocator_module, &allocator_config)
18301832
}),
18311833
})
1832-
})
1833-
.expect("failed to spawn coordinator thread");
1834+
};
1835+
return std::thread::Builder::new()
1836+
.name("coordinator".to_owned())
1837+
.spawn(f)
1838+
.expect("failed to spawn coordinator thread");
18341839

18351840
// A heuristic that determines if we have enough LLVM WorkItems in the
18361841
// queue so that the main thread can do LLVM work instead of codegen
@@ -1909,7 +1914,10 @@ fn spawn_work<'a, B: ExtraBackendMethods>(
19091914
let cgcx = cgcx.clone();
19101915
let prof = prof.clone();
19111916

1912-
B::spawn_named_thread(cgcx.time_trace, work.short_description(), move || {
1917+
let name = work.short_description();
1918+
let f = move || {
1919+
let _profiler = if cgcx.time_trace { B::thread_profiler() } else { Box::new(()) };
1920+
19131921
let result = std::panic::catch_unwind(AssertUnwindSafe(|| match work {
19141922
WorkItem::Optimize(m) => execute_optimize_work_item(&cgcx, &prof, shared_emitter, m),
19151923
WorkItem::CopyPostLtoArtifacts(m) => WorkItemResult::Finished(
@@ -1930,8 +1938,8 @@ fn spawn_work<'a, B: ExtraBackendMethods>(
19301938
Err(_) => Message::WorkItem::<B> { result: Err(None) },
19311939
};
19321940
drop(coordinator_send.send(msg));
1933-
})
1934-
.expect("failed to spawn work thread");
1941+
};
1942+
std::thread::Builder::new().name(name).spawn(f).expect("failed to spawn work thread");
19351943
}
19361944

19371945
fn spawn_thin_lto_work<B: ExtraBackendMethods>(
@@ -1945,7 +1953,10 @@ fn spawn_thin_lto_work<B: ExtraBackendMethods>(
19451953
let cgcx = cgcx.clone();
19461954
let prof = prof.clone();
19471955

1948-
B::spawn_named_thread(cgcx.time_trace, work.short_description(), move || {
1956+
let name = work.short_description();
1957+
let f = move || {
1958+
let _profiler = if cgcx.time_trace { B::thread_profiler() } else { Box::new(()) };
1959+
19491960
let result = std::panic::catch_unwind(AssertUnwindSafe(|| match work {
19501961
ThinLtoWorkItem::CopyPostLtoArtifacts(m) => {
19511962
execute_copy_from_cache_work_item(&cgcx, &prof, shared_emitter, m)
@@ -1968,8 +1979,8 @@ fn spawn_thin_lto_work<B: ExtraBackendMethods>(
19681979
Err(_) => ThinLtoMessage::WorkItem { result: Err(None) },
19691980
};
19701981
drop(coordinator_send.send(msg));
1971-
})
1972-
.expect("failed to spawn work thread");
1982+
};
1983+
std::thread::Builder::new().name(name).spawn(f).expect("failed to spawn work thread");
19731984
}
19741985

19751986
enum SharedEmitterMessage {

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,8 @@ pub trait ExtraBackendMethods:
161161
target_features: &[String],
162162
) -> TargetMachineFactoryFn<Self>;
163163

164-
fn spawn_named_thread<F, T>(
165-
_time_trace: bool,
166-
name: String,
167-
f: F,
168-
) -> std::io::Result<std::thread::JoinHandle<T>>
169-
where
170-
F: FnOnce() -> T,
171-
F: Send + 'static,
172-
T: Send + 'static,
173-
{
174-
std::thread::Builder::new().name(name).spawn(f)
164+
fn thread_profiler() -> Box<dyn Any> {
165+
Box::new(())
175166
}
176167

177168
/// Returns `true` if this backend can be safely called from multiple threads.

0 commit comments

Comments
 (0)