Skip to content

Commit fab583b

Browse files
authored
Rollup merge of #66335 - Mark-Simulacrum:self-profile-to-data, r=michaelwoerister
Move self-profile infrastructure to data structures The single dependency on queries (QueryName) can be fairly easily abstracted via a trait and this further decouples Session from librustc (the primary goal). This is intended as a precursor to moving Session out of librustc, but since that involves lots of smaller steps that move around code I'm splitting it up into separate PRs.
2 parents f735cd2 + 2fd5454 commit fab583b

File tree

13 files changed

+47
-22
lines changed

13 files changed

+47
-22
lines changed

Cargo.lock

+2-1
Original file line numberDiff line numberDiff line change
@@ -3120,7 +3120,6 @@ dependencies = [
31203120
"graphviz",
31213121
"jobserver",
31223122
"log",
3123-
"measureme",
31243123
"num_cpus",
31253124
"parking_lot 0.9.0",
31263125
"polonius-engine",
@@ -3470,6 +3469,7 @@ dependencies = [
34703469
name = "rustc_data_structures"
34713470
version = "0.0.0"
34723471
dependencies = [
3472+
"bitflags",
34733473
"cfg-if",
34743474
"crossbeam-utils 0.6.5",
34753475
"ena",
@@ -3478,6 +3478,7 @@ dependencies = [
34783478
"jobserver",
34793479
"lazy_static 1.3.0",
34803480
"log",
3481+
"measureme",
34813482
"parking_lot 0.9.0",
34823483
"rustc-hash",
34833484
"rustc-rayon 0.3.0",

src/librustc/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,3 @@ byteorder = { version = "1.3" }
4040
chalk-engine = { version = "0.9.0", default-features=false }
4141
rustc_fs_util = { path = "../librustc_fs_util" }
4242
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
43-
measureme = "0.4"

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ pub mod util {
124124
pub mod captures;
125125
pub mod common;
126126
pub mod nodemap;
127-
pub mod profiling;
128127
pub mod bug;
129128
}
130129

src/librustc/session/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ use syntax::source_map;
2727
use syntax::sess::{ParseSess, ProcessCfgMod};
2828
use syntax::symbol::Symbol;
2929
use syntax_pos::{MultiSpan, Span};
30-
use crate::util::profiling::{SelfProfiler, SelfProfilerRef};
3130

3231
use rustc_target::spec::{PanicStrategy, RelroLevel, Target, TargetTriple};
3332
use rustc_data_structures::flock;
3433
use rustc_data_structures::jobserver;
34+
use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
3535
use ::jobserver::Client;
3636

3737
use std;
@@ -1091,7 +1091,6 @@ fn build_session_(
10911091
);
10921092
match profiler {
10931093
Ok(profiler) => {
1094-
crate::ty::query::QueryName::register_with_profiler(&profiler);
10951094
Some(Arc::new(profiler))
10961095
},
10971096
Err(e) => {

src/librustc/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ use crate::ty::CanonicalPolyFnSig;
4646
use crate::util::common::ErrorReported;
4747
use crate::util::nodemap::{DefIdMap, DefIdSet, ItemLocalMap, ItemLocalSet, NodeMap};
4848
use crate::util::nodemap::{FxHashMap, FxHashSet};
49-
use crate::util::profiling::SelfProfilerRef;
5049

5150
use errors::DiagnosticBuilder;
5251
use arena::SyncDroplessArena;
5352
use smallvec::SmallVec;
53+
use rustc_data_structures::profiling::SelfProfilerRef;
5454
use rustc_data_structures::stable_hasher::{
5555
HashStable, StableHasher, StableVec, hash_stable_hashmap,
5656
};

src/librustc/ty/query/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::ty::query::queries;
66
use crate::ty::query::{Query, QueryName};
77
use crate::ty::query::QueryCache;
88
use crate::ty::query::plumbing::CycleError;
9-
use crate::util::profiling::ProfileCategory;
9+
use rustc_data_structures::profiling::ProfileCategory;
1010

1111
use std::borrow::Cow;
1212
use std::hash::Hash;

src/librustc/ty/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::ty::util::NeedsDrop;
3939
use crate::ty::subst::SubstsRef;
4040
use crate::util::nodemap::{DefIdSet, DefIdMap};
4141
use crate::util::common::ErrorReported;
42-
use crate::util::profiling::ProfileCategory::*;
42+
use rustc_data_structures::profiling::ProfileCategory::*;
4343

4444
use rustc_data_structures::svh::Svh;
4545
use rustc_index::vec::IndexVec;

src/librustc/ty/query/plumbing.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ macro_rules! define_queries_inner {
672672
rustc_data_structures::stable_hasher::StableHasher,
673673
ich::StableHashingContext
674674
};
675-
use crate::util::profiling::ProfileCategory;
675+
use rustc_data_structures::profiling::ProfileCategory;
676676

677677
define_queries_struct! {
678678
tcx: $tcx,
@@ -816,8 +816,20 @@ macro_rules! define_queries_inner {
816816
$($name),*
817817
}
818818

819+
impl rustc_data_structures::profiling::QueryName for QueryName {
820+
fn discriminant(self) -> std::mem::Discriminant<QueryName> {
821+
std::mem::discriminant(&self)
822+
}
823+
824+
fn as_str(self) -> &'static str {
825+
QueryName::as_str(&self)
826+
}
827+
}
828+
819829
impl QueryName {
820-
pub fn register_with_profiler(profiler: &crate::util::profiling::SelfProfiler) {
830+
pub fn register_with_profiler(
831+
profiler: &rustc_data_structures::profiling::SelfProfiler,
832+
) {
821833
$(profiler.register_query_name(QueryName::$name);)*
822834
}
823835

src/librustc_codegen_ssa/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc::util::nodemap::FxHashMap;
1919
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
2020
use rustc::ty::TyCtxt;
2121
use rustc::util::common::{time_depth, set_time_depth, print_time_passes_entry};
22-
use rustc::util::profiling::SelfProfilerRef;
22+
use rustc_data_structures::profiling::SelfProfilerRef;
2323
use rustc_fs_util::link_or_copy;
2424
use rustc_data_structures::svh::Svh;
2525
use rustc_data_structures::sync::Lrc;

src/librustc_data_structures/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ rayon-core = { version = "0.3.0", package = "rustc-rayon-core" }
2525
rustc-hash = "1.0.1"
2626
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
2727
rustc_index = { path = "../librustc_index", package = "rustc_index" }
28+
bitflags = "1.2.1"
29+
measureme = "0.4"
2830

2931
[dependencies.parking_lot]
3032
version = "0.9"

src/librustc_data_structures/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub use ena::unify;
9494
pub mod vec_linked_list;
9595
pub mod work_queue;
9696
pub mod fingerprint;
97+
pub mod profiling;
9798

9899
pub struct OnDrop<F: Fn()>(pub F);
99100

src/librustc/util/profiling.rs renamed to src/librustc_data_structures/profiling.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use std::sync::Arc;
77
use std::thread::ThreadId;
88
use std::u32;
99

10-
use crate::ty::query::QueryName;
11-
1210
use measureme::{StringId, TimestampKind};
1311

1412
/// MmapSerializatioSink is faster on macOS and Linux
@@ -20,6 +18,10 @@ type SerializationSink = measureme::FileSerializationSink;
2018

2119
type Profiler = measureme::Profiler<SerializationSink>;
2220

21+
pub trait QueryName: Sized + Copy {
22+
fn discriminant(self) -> Discriminant<Self>;
23+
fn as_str(self) -> &'static str;
24+
}
2325

2426
#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)]
2527
pub enum ProfileCategory {
@@ -32,7 +34,7 @@ pub enum ProfileCategory {
3234
Other,
3335
}
3436

35-
bitflags! {
37+
bitflags::bitflags! {
3638
struct EventFilter: u32 {
3739
const GENERIC_ACTIVITIES = 1 << 0;
3840
const QUERY_PROVIDERS = 1 << 1;
@@ -137,7 +139,7 @@ impl SelfProfilerRef {
137139
/// Start profiling a query provider. Profiling continues until the
138140
/// TimingGuard returned from this call is dropped.
139141
#[inline(always)]
140-
pub fn query_provider(&self, query_name: QueryName) -> TimingGuard<'_> {
142+
pub fn query_provider(&self, query_name: impl QueryName) -> TimingGuard<'_> {
141143
self.exec(EventFilter::QUERY_PROVIDERS, |profiler| {
142144
let event_id = SelfProfiler::get_query_name_string_id(query_name);
143145
TimingGuard::start(profiler, profiler.query_event_kind, event_id)
@@ -146,7 +148,7 @@ impl SelfProfilerRef {
146148

147149
/// Record a query in-memory cache hit.
148150
#[inline(always)]
149-
pub fn query_cache_hit(&self, query_name: QueryName) {
151+
pub fn query_cache_hit(&self, query_name: impl QueryName) {
150152
self.non_guard_query_event(
151153
|profiler| profiler.query_cache_hit_event_kind,
152154
query_name,
@@ -159,7 +161,7 @@ impl SelfProfilerRef {
159161
/// Profiling continues until the TimingGuard returned from this call is
160162
/// dropped.
161163
#[inline(always)]
162-
pub fn query_blocked(&self, query_name: QueryName) -> TimingGuard<'_> {
164+
pub fn query_blocked(&self, query_name: impl QueryName) -> TimingGuard<'_> {
163165
self.exec(EventFilter::QUERY_BLOCKED, |profiler| {
164166
let event_id = SelfProfiler::get_query_name_string_id(query_name);
165167
TimingGuard::start(profiler, profiler.query_blocked_event_kind, event_id)
@@ -170,7 +172,7 @@ impl SelfProfilerRef {
170172
/// incremental compilation on-disk cache. Profiling continues until the
171173
/// TimingGuard returned from this call is dropped.
172174
#[inline(always)]
173-
pub fn incr_cache_loading(&self, query_name: QueryName) -> TimingGuard<'_> {
175+
pub fn incr_cache_loading(&self, query_name: impl QueryName) -> TimingGuard<'_> {
174176
self.exec(EventFilter::INCR_CACHE_LOADS, |profiler| {
175177
let event_id = SelfProfiler::get_query_name_string_id(query_name);
176178
TimingGuard::start(
@@ -185,7 +187,7 @@ impl SelfProfilerRef {
185187
fn non_guard_query_event(
186188
&self,
187189
event_kind: fn(&SelfProfiler) -> StringId,
188-
query_name: QueryName,
190+
query_name: impl QueryName,
189191
event_filter: EventFilter,
190192
timestamp_kind: TimestampKind
191193
) {
@@ -203,6 +205,12 @@ impl SelfProfilerRef {
203205
TimingGuard::none()
204206
}));
205207
}
208+
209+
pub fn register_queries(&self, f: impl FnOnce(&SelfProfiler)) {
210+
if let Some(profiler) = &self.profiler {
211+
f(&profiler)
212+
}
213+
}
206214
}
207215

208216
pub struct SelfProfiler {
@@ -274,15 +282,15 @@ impl SelfProfiler {
274282
})
275283
}
276284

277-
fn get_query_name_string_id(query_name: QueryName) -> StringId {
285+
fn get_query_name_string_id(query_name: impl QueryName) -> StringId {
278286
let discriminant = unsafe {
279-
mem::transmute::<Discriminant<QueryName>, u64>(mem::discriminant(&query_name))
287+
mem::transmute::<Discriminant<_>, u64>(query_name.discriminant())
280288
};
281289

282290
StringId::reserved(discriminant as u32)
283291
}
284292

285-
pub fn register_query_name(&self, query_name: QueryName) {
293+
pub fn register_query_name(&self, query_name: impl QueryName) {
286294
let id = SelfProfiler::get_query_name_string_id(query_name);
287295
self.profiler.alloc_string_with_reserved_id(id, query_name.as_str());
288296
}

src/librustc_interface/util.rs

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ pub fn create_session(
108108
process_configure_mod,
109109
);
110110

111+
sess.prof.register_queries(|profiler| {
112+
rustc::ty::query::QueryName::register_with_profiler(&profiler);
113+
});
114+
111115
let codegen_backend = get_codegen_backend(&sess);
112116

113117
let mut cfg = config::build_configuration(&sess, config::to_crate_config(cfg));

0 commit comments

Comments
 (0)