Skip to content

Commit 841e08e

Browse files
committed
Reuse metadata file from work products.
1 parent 3da0e99 commit 841e08e

File tree

7 files changed

+83
-28
lines changed

7 files changed

+83
-28
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4058,6 +4058,7 @@ dependencies = [
40584058
"rustc_fs_util",
40594059
"rustc_hir",
40604060
"rustc_hir_pretty",
4061+
"rustc_incremental",
40614062
"rustc_index",
40624063
"rustc_macros",
40634064
"rustc_middle",

compiler/rustc_metadata/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rustc_fluent_macro = { path = "../rustc_fluent_macro" }
2020
rustc_fs_util = { path = "../rustc_fs_util" }
2121
rustc_hir = { path = "../rustc_hir" }
2222
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
23+
rustc_incremental = { path = "../rustc_incremental" }
2324
rustc_index = { path = "../rustc_index" }
2425
rustc_macros = { path = "../rustc_macros" }
2526
rustc_middle = { path = "../rustc_middle" }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,43 @@ impl<D: Decoder> Decodable<D> for EncodedMetadata {
23472347
}
23482348
}
23492349

2350+
#[instrument(level = "trace", skip(tcx))]
23502351
pub 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

23952426
fn with_encode_metadata_header(

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ rustc_with_all_queries!(define_dep_nodes![
9898
[] fn TraitSelect() -> (),
9999
[] fn CompileCodegenUnit() -> (),
100100
[] fn CompileMonoItem() -> (),
101+
[] fn Metadata() -> (),
101102
]);
102103

103104
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
@@ -115,6 +116,12 @@ pub(crate) fn make_compile_mono_item<'tcx>(
115116
DepNode::construct(tcx, dep_kinds::CompileMonoItem, mono_item)
116117
}
117118

119+
// WARNING: `construct` is generic and does not know that `Metadata` takes `()`s as keys.
120+
// Be very careful changing this type signature!
121+
pub(crate) fn make_metadata(tcx: TyCtxt<'_>) -> DepNode {
122+
DepNode::construct(tcx, dep_kinds::Metadata, &())
123+
}
124+
118125
pub trait DepNodeExt: Sized {
119126
fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>;
120127

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::ty::{self, TyCtxt};
99
mod dep_node;
1010

1111
pub use dep_node::{DepKind, DepNode, DepNodeExt, dep_kinds, label_strs};
12-
pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
12+
pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item, make_metadata};
1313
pub use rustc_query_system::dep_graph::debug::{DepNodeFilter, EdgeFilter};
1414
pub use rustc_query_system::dep_graph::{
1515
DepContext, DepGraphQuery, DepNodeIndex, Deps, SerializedDepGraph, SerializedDepNodeIndex,

compiler/rustc_middle/src/ty/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3388,6 +3388,10 @@ impl<'tcx> TyCtxt<'tcx> {
33883388
self.resolver_for_lowering_raw(()).0
33893389
}
33903390

3391+
pub fn metadata_dep_node(self) -> crate::dep_graph::DepNode {
3392+
crate::dep_graph::make_metadata(self)
3393+
}
3394+
33913395
/// Given an `impl_id`, return the trait it implements.
33923396
/// Return `None` if this is an inherent impl.
33933397
pub fn impl_trait_ref(

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,17 @@ macro_rules! define_queries {
923923
}
924924
}
925925

926+
pub(crate) fn Metadata<'tcx>() -> DepKindStruct<'tcx> {
927+
DepKindStruct {
928+
is_anon: false,
929+
is_eval_always: false,
930+
fingerprint_style: FingerprintStyle::Unit,
931+
force_from_dep_node: None,
932+
try_load_from_on_disk_cache: None,
933+
name: &"Metadata",
934+
}
935+
}
936+
926937
$(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
927938
$crate::plumbing::query_callback::<query_impl::$name::QueryType<'tcx>>(
928939
is_anon!([$($modifiers)*]),

0 commit comments

Comments
 (0)