Skip to content

Commit a48e472

Browse files
committed
Reuse metadata file from work products.
1 parent 4a8ecda commit a48e472

File tree

7 files changed

+47
-2
lines changed

7 files changed

+47
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4057,6 +4057,7 @@ dependencies = [
40574057
"rustc_fs_util",
40584058
"rustc_hir",
40594059
"rustc_hir_pretty",
4060+
"rustc_incremental",
40604061
"rustc_index",
40614062
"rustc_macros",
40624063
"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
@@ -2181,15 +2181,36 @@ impl<D: Decoder> Decodable<D> for EncodedMetadata {
21812181
}
21822182
}
21832183

2184+
#[instrument(level = "trace", skip(tcx))]
21842185
pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
2186+
let dep_node = tcx.metadata_dep_node();
2187+
2188+
if tcx.dep_graph.is_fully_enabled()
2189+
&& let work_product_id = &rustc_middle::dep_graph::WorkProductId::from_cgu_name("metadata")
2190+
&& let Some(work_product) = tcx.dep_graph.previous_work_product(work_product_id)
2191+
&& tcx.try_mark_green(&dep_node)
2192+
{
2193+
let saved_path = &work_product.saved_files["rmeta"];
2194+
let incr_comp_session_dir = tcx.sess.incr_comp_session_dir_opt().unwrap();
2195+
let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, saved_path);
2196+
debug!("copying preexisting metadata from {source_file:?} to {path:?}");
2197+
match rustc_fs_util::link_or_copy(&source_file, path) {
2198+
Ok(_) => {}
2199+
Err(err) => {
2200+
tcx.sess.emit_fatal(FailCreateFileEncoder { err });
2201+
}
2202+
};
2203+
return;
2204+
};
2205+
21852206
let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata");
21862207

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

21912212
join(
2192-
|| encode_metadata_impl(tcx, path),
2213+
|| tcx.dep_graph.with_task(dep_node, tcx, path, encode_metadata_impl, None),
21932214
|| {
21942215
if tcx.sess.threads() == 1 {
21952216
return;

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 7 additions & 0 deletions
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

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

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

1919
pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepsType>;
2020

compiler/rustc_middle/src/ty/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,10 @@ impl<'tcx> TyCtxt<'tcx> {
21492149
pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] {
21502150
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
21512151
}
2152+
2153+
pub fn metadata_dep_node(self) -> crate::dep_graph::DepNode {
2154+
crate::dep_graph::make_metadata(self)
2155+
}
21522156
}
21532157

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

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,17 @@ macro_rules! define_queries {
792792
}
793793
}
794794

795+
pub fn Metadata<'tcx>() -> DepKindStruct<'tcx> {
796+
DepKindStruct {
797+
is_anon: false,
798+
is_eval_always: false,
799+
fingerprint_style: FingerprintStyle::Unit,
800+
force_from_dep_node: None,
801+
try_load_from_on_disk_cache: None,
802+
name: &"Metadata",
803+
}
804+
}
805+
795806
$(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
796807
$crate::plumbing::query_callback::<query_impl::$name::QueryType<'tcx>>(
797808
is_anon!([$($modifiers)*]),

0 commit comments

Comments
 (0)