@@ -2163,15 +2163,36 @@ impl<D: Decoder> Decodable<D> for EncodedMetadata {
21632163 }
21642164}
21652165
2166+ #[ instrument( level = "trace" , skip( tcx) ) ]
21662167pub fn encode_metadata ( tcx : TyCtxt < ' _ > , path : & Path ) {
2168+ let dep_node = tcx. metadata_dep_node ( ) ;
2169+
2170+ if tcx. dep_graph . is_fully_enabled ( )
2171+ && let work_product_id = & rustc_middle:: dep_graph:: WorkProductId :: from_cgu_name ( "metadata" )
2172+ && let Some ( work_product) = tcx. dep_graph . previous_work_product ( work_product_id)
2173+ && tcx. try_mark_green ( & dep_node)
2174+ {
2175+ let saved_path = & work_product. saved_files [ "rmeta" ] ;
2176+ let incr_comp_session_dir = tcx. sess . incr_comp_session_dir_opt ( ) . unwrap ( ) ;
2177+ let source_file = rustc_incremental:: in_incr_comp_dir ( & incr_comp_session_dir, saved_path) ;
2178+ debug ! ( "copying preexisting metadata from {source_file:?} to {path:?}" ) ;
2179+ match rustc_fs_util:: link_or_copy ( & source_file, path) {
2180+ Ok ( _) => { }
2181+ Err ( err) => {
2182+ tcx. sess . emit_fatal ( FailCreateFileEncoder { err } ) ;
2183+ }
2184+ } ;
2185+ return ;
2186+ } ;
2187+
21672188 let _prof_timer = tcx. prof . verbose_generic_activity ( "generate_crate_metadata" ) ;
21682189
21692190 // Since encoding metadata is not in a query, and nothing is cached,
21702191 // there's no need to do dep-graph tracking for any of it.
21712192 tcx. dep_graph . assert_ignored ( ) ;
21722193
21732194 join (
2174- || encode_metadata_impl ( tcx, path) ,
2195+ || tcx . dep_graph . with_task ( dep_node , tcx, path, encode_metadata_impl , None ) ,
21752196 || {
21762197 if tcx. sess . threads ( ) == 1 {
21772198 return ;
0 commit comments