@@ -2347,7 +2347,43 @@ impl<D: Decoder> Decodable<D> for EncodedMetadata {
23472347 }
23482348}
23492349
2350+ #[ instrument( level = "trace" , skip( tcx) ) ]
23502351pub fn encode_metadata ( tcx : TyCtxt < ' _ > , path : & Path , ref_path : Option < & Path > ) {
2352+ if let Some ( ref_path) = ref_path {
2353+ let _prof_timer = tcx. prof . verbose_generic_activity ( "generate_crate_metadata_stub" ) ;
2354+
2355+ with_encode_metadata_header ( tcx, ref_path, |ecx| {
2356+ let header: LazyValue < CrateHeader > = ecx. lazy ( CrateHeader {
2357+ name : tcx. crate_name ( LOCAL_CRATE ) ,
2358+ triple : tcx. sess . opts . target_triple . clone ( ) ,
2359+ hash : tcx. crate_hash ( LOCAL_CRATE ) ,
2360+ is_proc_macro_crate : false ,
2361+ is_stub : true ,
2362+ } ) ;
2363+ header. position . get ( )
2364+ } ) ;
2365+ }
2366+
2367+ let dep_node = tcx. metadata_dep_node ( ) ;
2368+
2369+ if tcx. dep_graph . is_fully_enabled ( )
2370+ && let work_product_id = & rustc_middle:: dep_graph:: WorkProductId :: from_cgu_name ( "metadata" )
2371+ && let Some ( work_product) = tcx. dep_graph . previous_work_product ( work_product_id)
2372+ && tcx. try_mark_green ( & dep_node)
2373+ {
2374+ let saved_path = & work_product. saved_files [ "rmeta" ] ;
2375+ let incr_comp_session_dir = tcx. sess . incr_comp_session_dir_opt ( ) . unwrap ( ) ;
2376+ let source_file = rustc_incremental:: in_incr_comp_dir ( & incr_comp_session_dir, saved_path) ;
2377+ debug ! ( "copying preexisting metadata from {source_file:?} to {path:?}" ) ;
2378+ match rustc_fs_util:: link_or_copy ( & source_file, path) {
2379+ Ok ( _) => { }
2380+ Err ( err) => {
2381+ tcx. dcx ( ) . emit_fatal ( FailCreateFileEncoder { err } ) ;
2382+ }
2383+ } ;
2384+ return ;
2385+ } ;
2386+
23512387 let _prof_timer = tcx. prof . verbose_generic_activity ( "generate_crate_metadata" ) ;
23522388
23532389 // Since encoding metadata is not in a query, and nothing is cached,
@@ -2361,35 +2397,30 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
23612397 join ( || prefetch_mir ( tcx) , || tcx. exported_symbols ( LOCAL_CRATE ) ) ;
23622398 }
23632399
2364- with_encode_metadata_header ( tcx, path, |ecx| {
2365- // Encode all the entries and extra information in the crate,
2366- // culminating in the `CrateRoot` which points to all of it.
2367- let root = ecx. encode_crate_root ( ) ;
2368-
2369- // Flush buffer to ensure backing file has the correct size.
2370- ecx. opaque . flush ( ) ;
2371- // Record metadata size for self-profiling
2372- tcx. prof . artifact_size (
2373- "crate_metadata" ,
2374- "crate_metadata" ,
2375- ecx. opaque . file ( ) . metadata ( ) . unwrap ( ) . len ( ) ,
2376- ) ;
2377-
2378- root. position . get ( )
2379- } ) ;
2400+ tcx. dep_graph . with_task (
2401+ dep_node,
2402+ tcx,
2403+ path,
2404+ |tcx, path| {
2405+ with_encode_metadata_header ( tcx, path, |ecx| {
2406+ // Encode all the entries and extra information in the crate,
2407+ // culminating in the `CrateRoot` which points to all of it.
2408+ let root = ecx. encode_crate_root ( ) ;
2409+
2410+ // Flush buffer to ensure backing file has the correct size.
2411+ ecx. opaque . flush ( ) ;
2412+ // Record metadata size for self-profiling
2413+ tcx. prof . artifact_size (
2414+ "crate_metadata" ,
2415+ "crate_metadata" ,
2416+ ecx. opaque . file ( ) . metadata ( ) . unwrap ( ) . len ( ) ,
2417+ ) ;
23802418
2381- if let Some ( ref_path) = ref_path {
2382- with_encode_metadata_header ( tcx, ref_path, |ecx| {
2383- let header: LazyValue < CrateHeader > = ecx. lazy ( CrateHeader {
2384- name : tcx. crate_name ( LOCAL_CRATE ) ,
2385- triple : tcx. sess . opts . target_triple . clone ( ) ,
2386- hash : tcx. crate_hash ( LOCAL_CRATE ) ,
2387- is_proc_macro_crate : false ,
2388- is_stub : true ,
2419+ root. position . get ( )
23892420 } ) ;
2390- header . position . get ( )
2391- } ) ;
2392- }
2421+ } ,
2422+ None ,
2423+ ) ;
23932424}
23942425
23952426fn with_encode_metadata_header (
0 commit comments