Skip to content

Commit 69c2ea4

Browse files
committed
Reuse metadata file from work products.
1 parent 9c84ea5 commit 69c2ea4

File tree

7 files changed

+50
-1
lines changed

7 files changed

+50
-1
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4003,6 +4003,7 @@ dependencies = [
40034003
"rustc_fs_util",
40044004
"rustc_hir",
40054005
"rustc_hir_pretty",
4006+
"rustc_incremental",
40064007
"rustc_index",
40074008
"rustc_macros",
40084009
"rustc_middle",

compiler/rustc_metadata/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1818
rustc_fs_util = { path = "../rustc_fs_util" }
1919
rustc_hir = { path = "../rustc_hir" }
2020
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
21+
rustc_incremental = { path = "../rustc_incremental" }
2122
rustc_index = { path = "../rustc_index" }
2223
rustc_macros = { path = "../rustc_macros" }
2324
rustc_middle = { path = "../rustc_middle" }

compiler/rustc_metadata/src/rmeta/encoder.rs

+25
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,28 @@ impl<D: Decoder> Decodable<D> for EncodedMetadata {
22502250
}
22512251
}
22522252

2253+
#[instrument(level = "trace", skip(tcx))]
22532254
pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
2255+
let dep_node = tcx.metadata_dep_node();
2256+
2257+
if tcx.dep_graph.is_fully_enabled()
2258+
&& let work_product_id = &rustc_middle::dep_graph::WorkProductId::from_cgu_name("metadata")
2259+
&& let Some(work_product) = tcx.dep_graph.previous_work_product(work_product_id)
2260+
&& tcx.try_mark_green(&dep_node)
2261+
{
2262+
let saved_path = &work_product.saved_files["rmeta"];
2263+
let incr_comp_session_dir = tcx.sess.incr_comp_session_dir_opt().unwrap();
2264+
let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, saved_path);
2265+
debug!("copying preexisting metadata from {source_file:?} to {path:?}");
2266+
match rustc_fs_util::link_or_copy(&source_file, path) {
2267+
Ok(_) => {}
2268+
Err(err) => {
2269+
tcx.dcx().emit_fatal(FailCreateFileEncoder { err });
2270+
}
2271+
};
2272+
return;
2273+
};
2274+
22542275
let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata");
22552276

22562277
// Since encoding metadata is not in a query, and nothing is cached,
@@ -2264,6 +2285,10 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
22642285
join(|| prefetch_mir(tcx), || tcx.exported_symbols(LOCAL_CRATE));
22652286
}
22662287

2288+
tcx.dep_graph.with_task(dep_node, tcx, path, encode_metadata_impl, None);
2289+
}
2290+
2291+
fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
22672292
let mut encoder = opaque::FileEncoder::new(path)
22682293
.unwrap_or_else(|err| tcx.dcx().emit_fatal(FailCreateFileEncoder { err }));
22692294
encoder.emit_raw_bytes(METADATA_HEADER);

compiler/rustc_middle/src/dep_graph/dep_node.rs

+7
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ rustc_query_append!(define_dep_nodes![
140140
[] fn TraitSelect() -> (),
141141
[] fn CompileCodegenUnit() -> (),
142142
[] fn CompileMonoItem() -> (),
143+
[] fn Metadata() -> (),
143144
]);
144145

145146
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
@@ -157,6 +158,12 @@ pub(crate) fn make_compile_mono_item<'tcx>(
157158
DepNode::construct(tcx, dep_kinds::CompileMonoItem, mono_item)
158159
}
159160

161+
// WARNING: `construct` is generic and does not know that `Metadata` takes `()`s as keys.
162+
// Be very careful changing this type signature!
163+
pub(crate) fn make_metadata(tcx: TyCtxt<'_>) -> DepNode {
164+
DepNode::construct(tcx, dep_kinds::Metadata, &())
165+
}
166+
160167
pub trait DepNodeExt: Sized {
161168
/// Extracts the DefId corresponding to this DepNode. This will work
162169
/// if two conditions are met:

compiler/rustc_middle/src/dep_graph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::ty::{self, TyCtxt};
88
mod dep_node;
99

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

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3155,6 +3155,10 @@ impl<'tcx> TyCtxt<'tcx> {
31553155
self.resolver_for_lowering_raw(()).0
31563156
}
31573157

3158+
pub fn metadata_dep_node(self) -> crate::dep_graph::DepNode {
3159+
crate::dep_graph::make_metadata(self)
3160+
}
3161+
31583162
/// Given an `impl_id`, return the trait it implements.
31593163
/// Return `None` if this is an inherent impl.
31603164
pub fn impl_trait_ref(

compiler/rustc_query_impl/src/plumbing.rs

+11
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,17 @@ macro_rules! define_queries {
859859
}
860860
}
861861

862+
pub(crate) fn Metadata<'tcx>() -> DepKindStruct<'tcx> {
863+
DepKindStruct {
864+
is_anon: false,
865+
is_eval_always: false,
866+
fingerprint_style: FingerprintStyle::Unit,
867+
force_from_dep_node: None,
868+
try_load_from_on_disk_cache: None,
869+
name: &"Metadata",
870+
}
871+
}
872+
862873
$(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
863874
$crate::plumbing::query_callback::<query_impl::$name::QueryType<'tcx>>(
864875
is_anon!([$($modifiers)*]),

0 commit comments

Comments
 (0)