Skip to content

Commit 761d366

Browse files
committed
Reuse metadata file from work products.
1 parent c7ee3a5 commit 761d366

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
@@ -4133,6 +4133,7 @@ dependencies = [
41334133
"rustc_fs_util",
41344134
"rustc_hir",
41354135
"rustc_hir_pretty",
4136+
"rustc_incremental",
41364137
"rustc_index",
41374138
"rustc_macros",
41384139
"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
@@ -2386,7 +2386,43 @@ impl<D: Decoder> Decodable<D> for EncodedMetadata {
23862386
}
23872387
}
23882388

2389+
#[instrument(level = "trace", skip(tcx))]
23892390
pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
2391+
if let Some(ref_path) = ref_path {
2392+
let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata_stub");
2393+
2394+
with_encode_metadata_header(tcx, ref_path, |ecx| {
2395+
let header: LazyValue<CrateHeader> = ecx.lazy(CrateHeader {
2396+
name: tcx.crate_name(LOCAL_CRATE),
2397+
triple: tcx.sess.opts.target_triple.clone(),
2398+
hash: tcx.crate_hash(LOCAL_CRATE),
2399+
is_proc_macro_crate: false,
2400+
is_stub: true,
2401+
});
2402+
header.position.get()
2403+
});
2404+
}
2405+
2406+
let dep_node = tcx.metadata_dep_node();
2407+
2408+
if tcx.dep_graph.is_fully_enabled()
2409+
&& let work_product_id = &rustc_middle::dep_graph::WorkProductId::from_cgu_name("metadata")
2410+
&& let Some(work_product) = tcx.dep_graph.previous_work_product(work_product_id)
2411+
&& tcx.try_mark_green(&dep_node)
2412+
{
2413+
let saved_path = &work_product.saved_files["rmeta"];
2414+
let incr_comp_session_dir = tcx.sess.incr_comp_session_dir_opt().unwrap();
2415+
let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, saved_path);
2416+
debug!("copying preexisting metadata from {source_file:?} to {path:?}");
2417+
match rustc_fs_util::link_or_copy(&source_file, path) {
2418+
Ok(_) => {}
2419+
Err(err) => {
2420+
tcx.dcx().emit_fatal(FailCreateFileEncoder { err });
2421+
}
2422+
};
2423+
return;
2424+
};
2425+
23902426
let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata");
23912427

23922428
// Since encoding metadata is not in a query, and nothing is cached,
@@ -2406,35 +2442,30 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
24062442
);
24072443
}
24082444

2409-
with_encode_metadata_header(tcx, path, |ecx| {
2410-
// Encode all the entries and extra information in the crate,
2411-
// culminating in the `CrateRoot` which points to all of it.
2412-
let root = ecx.encode_crate_root();
2413-
2414-
// Flush buffer to ensure backing file has the correct size.
2415-
ecx.opaque.flush();
2416-
// Record metadata size for self-profiling
2417-
tcx.prof.artifact_size(
2418-
"crate_metadata",
2419-
"crate_metadata",
2420-
ecx.opaque.file().metadata().unwrap().len(),
2421-
);
2422-
2423-
root.position.get()
2424-
});
2445+
tcx.dep_graph.with_task(
2446+
dep_node,
2447+
tcx,
2448+
path,
2449+
|tcx, path| {
2450+
with_encode_metadata_header(tcx, path, |ecx| {
2451+
// Encode all the entries and extra information in the crate,
2452+
// culminating in the `CrateRoot` which points to all of it.
2453+
let root = ecx.encode_crate_root();
2454+
2455+
// Flush buffer to ensure backing file has the correct size.
2456+
ecx.opaque.flush();
2457+
// Record metadata size for self-profiling
2458+
tcx.prof.artifact_size(
2459+
"crate_metadata",
2460+
"crate_metadata",
2461+
ecx.opaque.file().metadata().unwrap().len(),
2462+
);
24252463

2426-
if let Some(ref_path) = ref_path {
2427-
with_encode_metadata_header(tcx, ref_path, |ecx| {
2428-
let header: LazyValue<CrateHeader> = ecx.lazy(CrateHeader {
2429-
name: tcx.crate_name(LOCAL_CRATE),
2430-
triple: tcx.sess.opts.target_triple.clone(),
2431-
hash: tcx.crate_hash(LOCAL_CRATE),
2432-
is_proc_macro_crate: false,
2433-
is_stub: true,
2464+
root.position.get()
24342465
});
2435-
header.position.get()
2436-
});
2437-
}
2466+
},
2467+
None,
2468+
);
24382469
}
24392470

24402471
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
@@ -3364,6 +3364,10 @@ impl<'tcx> TyCtxt<'tcx> {
33643364
self.resolver_for_lowering_raw(()).0
33653365
}
33663366

3367+
pub fn metadata_dep_node(self) -> crate::dep_graph::DepNode {
3368+
crate::dep_graph::make_metadata(self)
3369+
}
3370+
33673371
/// Given an `impl_id`, return the trait it implements.
33683372
/// Return `None` if this is an inherent impl.
33693373
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)