Skip to content

Commit 25b794f

Browse files
committed
Reuse metadata file from work products.
1 parent 80221b4 commit 25b794f

File tree

7 files changed

+46
-2
lines changed

7 files changed

+46
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3932,6 +3932,7 @@ dependencies = [
39323932
"rustc_fs_util",
39333933
"rustc_hir",
39343934
"rustc_hir_pretty",
3935+
"rustc_incremental",
39353936
"rustc_index",
39363937
"rustc_macros",
39373938
"rustc_middle",

compiler/rustc_metadata/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rustc_fluent_macro = { path = "../rustc_fluent_macro" }
2121
rustc_fs_util = { path = "../rustc_fs_util" }
2222
rustc_hir = { path = "../rustc_hir" }
2323
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
24+
rustc_incremental = { path = "../rustc_incremental" }
2425
rustc_target = { path = "../rustc_target" }
2526
rustc_index = { path = "../rustc_index" }
2627
rustc_macros = { path = "../rustc_macros" }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2163,15 +2163,36 @@ impl<D: Decoder> Decodable<D> for EncodedMetadata {
21632163
}
21642164
}
21652165

2166+
#[instrument(level = "trace", skip(tcx))]
21662167
pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
2168+
let dep_node = tcx.metadata_dep_node();
2169+
2170+
if tcx.dep_graph.is_fully_enabled()
2171+
&& let work_product_id = &rustc_middle::dep_graph::WorkProductId::from_cgu_name("metadata")
2172+
&& let Some(work_product) = tcx.dep_graph.previous_work_product(work_product_id)
2173+
&& tcx.try_mark_green(&dep_node)
2174+
{
2175+
let saved_path = &work_product.saved_files["rmeta"];
2176+
let incr_comp_session_dir = tcx.sess.incr_comp_session_dir_opt().unwrap();
2177+
let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, saved_path);
2178+
debug!("copying preexisting metadata from {source_file:?} to {path:?}");
2179+
match rustc_fs_util::link_or_copy(&source_file, path) {
2180+
Ok(_) => {}
2181+
Err(err) => {
2182+
tcx.sess.emit_fatal(FailCreateFileEncoder { err });
2183+
}
2184+
};
2185+
return;
2186+
};
2187+
21672188
let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata");
21682189

21692190
// Since encoding metadata is not in a query, and nothing is cached,
21702191
// there's no need to do dep-graph tracking for any of it.
21712192
tcx.dep_graph.assert_ignored();
21722193

21732194
join(
2174-
|| encode_metadata_impl(tcx, path),
2195+
|| tcx.dep_graph.with_task(dep_node, tcx, path, encode_metadata_impl, None),
21752196
|| {
21762197
if tcx.sess.threads() == 1 {
21772198
return;

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ rustc_query_append!(define_dep_nodes![
112112
[] fn TraitSelect() -> (),
113113
[] fn CompileCodegenUnit() -> (),
114114
[] fn CompileMonoItem() -> (),
115+
[] fn Metadata() -> (),
115116
]);
116117

117118
// WARNING: `construct` is generic and does not know that `CompileCodegenUnit` takes `Symbol`s as keys.
@@ -129,6 +130,12 @@ pub(crate) fn make_compile_mono_item<'tcx>(
129130
DepNode::construct(tcx, DepKind::CompileMonoItem, mono_item)
130131
}
131132

133+
// WARNING: `construct` is generic and does not know that `Metadata` takes `()`s as keys.
134+
// Be very careful changing this type signature!
135+
pub(crate) fn make_metadata(tcx: TyCtxt<'_>) -> DepNode {
136+
DepNode::construct(tcx, DepKind::Metadata, &())
137+
}
138+
132139
pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
133140

134141
// We keep a lot of `DepNode`s in memory during compilation. It's not

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use rustc_query_system::dep_graph::{
1212
};
1313

1414
pub use dep_node::{label_strs, DepKind, DepNode, DepNodeExt};
15-
pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
15+
pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item, make_metadata};
1616

1717
pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
1818

compiler/rustc_middle/src/ty/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,10 @@ impl<'tcx> TyCtxt<'tcx> {
21262126
pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] {
21272127
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
21282128
}
2129+
2130+
pub fn metadata_dep_node(self) -> crate::dep_graph::DepNode {
2131+
crate::dep_graph::make_metadata(self)
2132+
}
21292133
}
21302134

21312135
/// Parameter attributes that can only be determined by examining the body of a function instead

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,16 @@ macro_rules! define_queries {
782782
}
783783
}
784784

785+
pub fn Metadata<'tcx>() -> DepKindStruct<'tcx> {
786+
DepKindStruct {
787+
is_anon: false,
788+
is_eval_always: false,
789+
fingerprint_style: FingerprintStyle::Unit,
790+
force_from_dep_node: None,
791+
try_load_from_on_disk_cache: None,
792+
}
793+
}
794+
785795
$(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
786796
$crate::plumbing::query_callback::<query_impl::$name::QueryType<'tcx>>(
787797
is_anon!([$($modifiers)*]),

0 commit comments

Comments
 (0)