@@ -95,7 +95,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
9595 if let Some ( ( _, value) ) =
9696 lock. results . raw_entry ( ) . from_key_hashed_nocheck ( key_hash, key)
9797 {
98- tcx. prof . query_cache_hit ( Q :: NAME ) ;
98+ tcx. prof . query_cache_hit ( value . index . into ( ) ) ;
9999 let result = ( value. value . clone ( ) , value. index ) ;
100100 #[ cfg( debug_assertions) ]
101101 {
@@ -347,7 +347,7 @@ impl<'tcx> TyCtxt<'tcx> {
347347
348348 #[ inline( never) ]
349349 pub ( super ) fn get_query < Q : QueryDescription < ' tcx > > ( self , span : Span , key : Q :: Key ) -> Q :: Value {
350- debug ! ( "ty::query::get_query<{}>(key={:?}, span={:?})" , Q :: NAME . as_str ( ) , key, span) ;
350+ debug ! ( "ty::query::get_query<{}>(key={:?}, span={:?})" , Q :: NAME , key, span) ;
351351
352352 let job = match JobOwner :: try_get ( self , span, & key) {
353353 TryGetJob :: NotYetStarted ( job) => job,
@@ -366,15 +366,15 @@ impl<'tcx> TyCtxt<'tcx> {
366366 }
367367
368368 if Q :: ANON {
369- let prof_timer = self . prof . query_provider ( Q :: NAME ) ;
369+ let prof_timer = self . prof . query_provider ( ) ;
370370
371371 let ( ( result, dep_node_index) , diagnostics) = with_diagnostics ( |diagnostics| {
372372 self . start_query ( job. job . clone ( ) , diagnostics, |tcx| {
373373 tcx. dep_graph . with_anon_task ( Q :: dep_kind ( ) , || Q :: compute ( tcx, key) )
374374 } )
375375 } ) ;
376376
377- drop ( prof_timer) ;
377+ prof_timer. finish_with_query_invocation_id ( dep_node_index . into ( ) ) ;
378378
379379 self . dep_graph . read_index ( dep_node_index) ;
380380
@@ -436,8 +436,9 @@ impl<'tcx> TyCtxt<'tcx> {
436436 let result = if Q :: cache_on_disk ( self , key. clone ( ) , None )
437437 && self . sess . opts . debugging_opts . incremental_queries
438438 {
439- let _prof_timer = self . prof . incr_cache_loading ( Q :: NAME ) ;
439+ let prof_timer = self . prof . incr_cache_loading ( ) ;
440440 let result = Q :: try_load_from_disk ( self , prev_dep_node_index) ;
441+ prof_timer. finish_with_query_invocation_id ( dep_node_index. into ( ) ) ;
441442
442443 // We always expect to find a cached result for things that
443444 // can be forced from `DepNode`.
@@ -457,11 +458,13 @@ impl<'tcx> TyCtxt<'tcx> {
457458 } else {
458459 // We could not load a result from the on-disk cache, so
459460 // recompute.
460- let _prof_timer = self . prof . query_provider ( Q :: NAME ) ;
461+ let prof_timer = self . prof . query_provider ( ) ;
461462
462463 // The dep-graph for this computation is already in-place.
463464 let result = self . dep_graph . with_ignore ( || Q :: compute ( self , key) ) ;
464465
466+ prof_timer. finish_with_query_invocation_id ( dep_node_index. into ( ) ) ;
467+
465468 result
466469 } ;
467470
@@ -523,7 +526,7 @@ impl<'tcx> TyCtxt<'tcx> {
523526 dep_node
524527 ) ;
525528
526- let prof_timer = self . prof . query_provider ( Q :: NAME ) ;
529+ let prof_timer = self . prof . query_provider ( ) ;
527530
528531 let ( ( result, dep_node_index) , diagnostics) = with_diagnostics ( |diagnostics| {
529532 self . start_query ( job. job . clone ( ) , diagnostics, |tcx| {
@@ -541,7 +544,7 @@ impl<'tcx> TyCtxt<'tcx> {
541544 } )
542545 } ) ;
543546
544- drop ( prof_timer) ;
547+ prof_timer. finish_with_query_invocation_id ( dep_node_index . into ( ) ) ;
545548
546549 if unlikely ! ( !diagnostics. is_empty( ) ) {
547550 if dep_node. kind != crate :: dep_graph:: DepKind :: Null {
@@ -572,17 +575,19 @@ impl<'tcx> TyCtxt<'tcx> {
572575
573576 let dep_node = Q :: to_dep_node ( self , & key) ;
574577
575- if self . dep_graph . try_mark_green_and_read ( self , & dep_node) . is_none ( ) {
576- // A None return from `try_mark_green_and_read` means that this is either
577- // a new dep node or that the dep node has already been marked red.
578- // Either way, we can't call `dep_graph.read()` as we don't have the
579- // DepNodeIndex. We must invoke the query itself. The performance cost
580- // this introduces should be negligible as we'll immediately hit the
581- // in-memory cache, or another query down the line will.
582-
583- let _ = self . get_query :: < Q > ( DUMMY_SP , key) ;
584- } else {
585- self . prof . query_cache_hit ( Q :: NAME ) ;
578+ match self . dep_graph . try_mark_green_and_read ( self , & dep_node) {
579+ None => {
580+ // A None return from `try_mark_green_and_read` means that this is either
581+ // a new dep node or that the dep node has already been marked red.
582+ // Either way, we can't call `dep_graph.read()` as we don't have the
583+ // DepNodeIndex. We must invoke the query itself. The performance cost
584+ // this introduces should be negligible as we'll immediately hit the
585+ // in-memory cache, or another query down the line will.
586+ let _ = self . get_query :: < Q > ( DUMMY_SP , key) ;
587+ }
588+ Some ( ( _, dep_node_index) ) => {
589+ self . prof . query_cache_hit ( dep_node_index. into ( ) ) ;
590+ }
586591 }
587592 }
588593
@@ -696,6 +701,42 @@ macro_rules! define_queries_inner {
696701 }
697702 }
698703
704+ /// All self-profiling events generated by the query engine use a
705+ /// virtual `StringId`s for their `event_id`. This method makes all
706+ /// those virtual `StringId`s point to actual strings.
707+ ///
708+ /// If we are recording only summary data, the ids will point to
709+ /// just the query names. If we are recording query keys too, we
710+ /// allocate the corresponding strings here. (The latter is not yet
711+ /// implemented.)
712+ pub fn allocate_self_profile_query_strings(
713+ & self ,
714+ profiler: & rustc_data_structures:: profiling:: SelfProfiler
715+ ) {
716+ // Walk the entire query cache and allocate the appropriate
717+ // string representation. Each cache entry is uniquely
718+ // identified by its dep_node_index.
719+ $( {
720+ let query_name_string_id =
721+ profiler. get_or_alloc_cached_string( stringify!( $name) ) ;
722+
723+ let result_cache = self . $name. lock_shards( ) ;
724+
725+ for shard in result_cache. iter( ) {
726+ let query_invocation_ids = shard
727+ . results
728+ . values( )
729+ . map( |v| v. index)
730+ . map( |dep_node_index| dep_node_index. into( ) ) ;
731+
732+ profiler. bulk_map_query_invocation_id_to_single_string(
733+ query_invocation_ids,
734+ query_name_string_id
735+ ) ;
736+ }
737+ } ) *
738+ }
739+
699740 #[ cfg( parallel_compiler) ]
700741 pub fn collect_active_jobs( & self ) -> Vec <Lrc <QueryJob <$tcx>>> {
701742 let mut jobs = Vec :: new( ) ;
@@ -813,36 +854,6 @@ macro_rules! define_queries_inner {
813854 }
814855 }
815856
816- #[ allow( nonstandard_style) ]
817- #[ derive( Clone , Copy ) ]
818- pub enum QueryName {
819- $( $name) ,*
820- }
821-
822- impl rustc_data_structures:: profiling:: QueryName for QueryName {
823- fn discriminant( self ) -> std:: mem:: Discriminant <QueryName > {
824- std:: mem:: discriminant( & self )
825- }
826-
827- fn as_str( self ) -> & ' static str {
828- QueryName :: as_str( & self )
829- }
830- }
831-
832- impl QueryName {
833- pub fn register_with_profiler(
834- profiler: & rustc_data_structures:: profiling:: SelfProfiler ,
835- ) {
836- $( profiler. register_query_name( QueryName :: $name) ; ) *
837- }
838-
839- pub fn as_str( & self ) -> & ' static str {
840- match self {
841- $( QueryName :: $name => stringify!( $name) , ) *
842- }
843- }
844- }
845-
846857 #[ allow( nonstandard_style) ]
847858 #[ derive( Clone , Debug ) ]
848859 pub enum Query <$tcx> {
@@ -883,12 +894,6 @@ macro_rules! define_queries_inner {
883894 $( Query :: $name( key) => key. default_span( tcx) , ) *
884895 }
885896 }
886-
887- pub fn query_name( & self ) -> QueryName {
888- match self {
889- $( Query :: $name( _) => QueryName :: $name, ) *
890- }
891- }
892897 }
893898
894899 impl <' a, $tcx> HashStable <StableHashingContext <' a>> for Query <$tcx> {
@@ -923,7 +928,7 @@ macro_rules! define_queries_inner {
923928 type Key = $K;
924929 type Value = $V;
925930
926- const NAME : QueryName = QueryName :: $name;
931+ const NAME : & ' static str = stringify! ( $name) ;
927932 const CATEGORY : ProfileCategory = $category;
928933 }
929934
0 commit comments