@@ -2181,15 +2181,36 @@ impl<D: Decoder> Decodable<D> for EncodedMetadata {
21812181 }
21822182}
21832183
2184+ #[ instrument( level = "trace" , skip( tcx) ) ]
21842185pub fn encode_metadata ( tcx : TyCtxt < ' _ > , path : & Path ) {
2186+ let dep_node = tcx. metadata_dep_node ( ) ;
2187+
2188+ if tcx. dep_graph . is_fully_enabled ( )
2189+ && let work_product_id = & rustc_middle:: dep_graph:: WorkProductId :: from_cgu_name ( "metadata" )
2190+ && let Some ( work_product) = tcx. dep_graph . previous_work_product ( work_product_id)
2191+ && tcx. try_mark_green ( & dep_node)
2192+ {
2193+ let saved_path = & work_product. saved_files [ "rmeta" ] ;
2194+ let incr_comp_session_dir = tcx. sess . incr_comp_session_dir_opt ( ) . unwrap ( ) ;
2195+ let source_file = rustc_incremental:: in_incr_comp_dir ( & incr_comp_session_dir, saved_path) ;
2196+ debug ! ( "copying preexisting metadata from {source_file:?} to {path:?}" ) ;
2197+ match rustc_fs_util:: link_or_copy ( & source_file, path) {
2198+ Ok ( _) => { }
2199+ Err ( err) => {
2200+ tcx. sess . emit_fatal ( FailCreateFileEncoder { err } ) ;
2201+ }
2202+ } ;
2203+ return ;
2204+ } ;
2205+
21852206 let _prof_timer = tcx. prof . verbose_generic_activity ( "generate_crate_metadata" ) ;
21862207
21872208 // Since encoding metadata is not in a query, and nothing is cached,
21882209 // there's no need to do dep-graph tracking for any of it.
21892210 tcx. dep_graph . assert_ignored ( ) ;
21902211
21912212 join (
2192- || encode_metadata_impl ( tcx, path) ,
2213+ || tcx . dep_graph . with_task ( dep_node , tcx, path, encode_metadata_impl , None ) ,
21932214 || {
21942215 if tcx. sess . threads ( ) == 1 {
21952216 return ;
0 commit comments