about summary refs log tree commit diff
path: root/compiler/rustc_incremental/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_incremental/src')
-rw-r--r--compiler/rustc_incremental/src/assert_dep_graph.rs440
-rw-r--r--compiler/rustc_incremental/src/errors.rs314
-rw-r--r--compiler/rustc_incremental/src/lib.rs25
-rw-r--r--compiler/rustc_incremental/src/persist/README.md3
-rw-r--r--compiler/rustc_incremental/src/persist/data.rs13
-rw-r--r--compiler/rustc_incremental/src/persist/dirty_clean.rs469
-rw-r--r--compiler/rustc_incremental/src/persist/file_format.rs178
-rw-r--r--compiler/rustc_incremental/src/persist/fs.rs927
-rw-r--r--compiler/rustc_incremental/src/persist/fs/tests.rs77
-rw-r--r--compiler/rustc_incremental/src/persist/load.rs231
-rw-r--r--compiler/rustc_incremental/src/persist/mod.rs21
-rw-r--r--compiler/rustc_incremental/src/persist/save.rs184
-rw-r--r--compiler/rustc_incremental/src/persist/work_product.rs57
13 files changed, 2939 insertions, 0 deletions
diff --git a/compiler/rustc_incremental/src/assert_dep_graph.rs b/compiler/rustc_incremental/src/assert_dep_graph.rs
new file mode 100644
index 00000000000..41caa5d4765
--- /dev/null
+++ b/compiler/rustc_incremental/src/assert_dep_graph.rs
@@ -0,0 +1,440 @@
+//! This pass is only used for the UNIT TESTS and DEBUGGING NEEDS
+//! around dependency graph construction. It serves two purposes; it
+//! will dump graphs in graphviz form to disk, and it searches for
+//! `#[rustc_if_this_changed]` and `#[rustc_then_this_would_need]`
+//! annotations. These annotations can be used to test whether paths
+//! exist in the graph. These checks run after codegen, so they view the
+//! the final state of the dependency graph. Note that there are
+//! similar assertions found in `persist::dirty_clean` which check the
+//! **initial** state of the dependency graph, just after it has been
+//! loaded from disk.
+//!
+//! In this code, we report errors on each `rustc_if_this_changed`
+//! annotation. If a path exists in all cases, then we would report
+//! "all path(s) exist". Otherwise, we report: "no path to `foo`" for
+//! each case where no path exists. `ui` tests can then be
+//! used to check when paths exist or do not.
+//!
+//! The full form of the `rustc_if_this_changed` annotation is
+//! `#[rustc_if_this_changed("foo")]`, which will report a
+//! source node of `foo(def_id)`. The `"foo"` is optional and
+//! defaults to `"Hir"` if omitted.
+//!
+//! Example:
+//!
+//! ```ignore (needs flags)
+//! #[rustc_if_this_changed(Hir)]
+//! fn foo() { }
+//!
+//! #[rustc_then_this_would_need(codegen)] //~ ERROR no path from `foo`
+//! fn bar() { }
+//!
+//! #[rustc_then_this_would_need(codegen)] //~ ERROR OK
+//! fn baz() { foo(); }
+//! ```
+
+use crate::errors;
+use rustc_ast as ast;
+use rustc_data_structures::fx::FxIndexSet;
+use rustc_data_structures::graph::implementation::{Direction, NodeIndex, INCOMING, OUTGOING};
+use rustc_graphviz as dot;
+use rustc_hir as hir;
+use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
+use rustc_hir::intravisit::{self, Visitor};
+use rustc_middle::dep_graph::{
+    dep_kinds, DepGraphQuery, DepKind, DepNode, DepNodeExt, DepNodeFilter, EdgeFilter,
+};
+use rustc_middle::hir::nested_filter;
+use rustc_middle::ty::TyCtxt;
+use rustc_middle::{bug, span_bug};
+use rustc_span::symbol::{sym, Symbol};
+use rustc_span::Span;
+use std::env;
+use std::fs::{self, File};
+use std::io::{BufWriter, Write};
+use tracing::debug;
+
+#[allow(missing_docs)]
+pub fn assert_dep_graph(tcx: TyCtxt<'_>) {
+    tcx.dep_graph.with_ignore(|| {
+        if tcx.sess.opts.unstable_opts.dump_dep_graph {
+            tcx.dep_graph.with_query(dump_graph);
+        }
+
+        if !tcx.sess.opts.unstable_opts.query_dep_graph {
+            return;
+        }
+
+        // if the `rustc_attrs` feature is not enabled, then the
+        // attributes we are interested in cannot be present anyway, so
+        // skip the walk.
+        if !tcx.features().rustc_attrs {
+            return;
+        }
+
+        // Find annotations supplied by user (if any).
+        let (if_this_changed, then_this_would_need) = {
+            let mut visitor =
+                IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] };
+            visitor.process_attrs(CRATE_DEF_ID);
+            tcx.hir().visit_all_item_likes_in_crate(&mut visitor);
+            (visitor.if_this_changed, visitor.then_this_would_need)
+        };
+
+        if !if_this_changed.is_empty() || !then_this_would_need.is_empty() {
+            assert!(
+                tcx.sess.opts.unstable_opts.query_dep_graph,
+                "cannot use the `#[{}]` or `#[{}]` annotations \
+                    without supplying `-Z query-dep-graph`",
+                sym::rustc_if_this_changed,
+                sym::rustc_then_this_would_need
+            );
+        }
+
+        // Check paths.
+        check_paths(tcx, &if_this_changed, &then_this_would_need);
+    })
+}
+
+type Sources = Vec<(Span, DefId, DepNode)>;
+type Targets = Vec<(Span, Symbol, hir::HirId, DepNode)>;
+
+struct IfThisChanged<'tcx> {
+    tcx: TyCtxt<'tcx>,
+    if_this_changed: Sources,
+    then_this_would_need: Targets,
+}
+
+impl<'tcx> IfThisChanged<'tcx> {
+    fn argument(&self, attr: &ast::Attribute) -> Option<Symbol> {
+        let mut value = None;
+        for list_item in attr.meta_item_list().unwrap_or_default() {
+            match list_item.ident() {
+                Some(ident) if list_item.is_word() && value.is_none() => value = Some(ident.name),
+                _ =>
+                // FIXME better-encapsulate meta_item (don't directly access `node`)
+                {
+                    span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item)
+                }
+            }
+        }
+        value
+    }
+
+    fn process_attrs(&mut self, def_id: LocalDefId) {
+        let def_path_hash = self.tcx.def_path_hash(def_id.to_def_id());
+        let hir_id = self.tcx.local_def_id_to_hir_id(def_id);
+        let attrs = self.tcx.hir().attrs(hir_id);
+        for attr in attrs {
+            if attr.has_name(sym::rustc_if_this_changed) {
+                let dep_node_interned = self.argument(attr);
+                let dep_node = match dep_node_interned {
+                    None => DepNode::from_def_path_hash(
+                        self.tcx,
+                        def_path_hash,
+                        dep_kinds::opt_hir_owner_nodes,
+                    ),
+                    Some(n) => {
+                        match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {
+                            Ok(n) => n,
+                            Err(()) => self.tcx.dcx().emit_fatal(errors::UnrecognizedDepNode {
+                                span: attr.span,
+                                name: n,
+                            }),
+                        }
+                    }
+                };
+                self.if_this_changed.push((attr.span, def_id.to_def_id(), dep_node));
+            } else if attr.has_name(sym::rustc_then_this_would_need) {
+                let dep_node_interned = self.argument(attr);
+                let dep_node = match dep_node_interned {
+                    Some(n) => {
+                        match DepNode::from_label_string(self.tcx, n.as_str(), def_path_hash) {
+                            Ok(n) => n,
+                            Err(()) => self.tcx.dcx().emit_fatal(errors::UnrecognizedDepNode {
+                                span: attr.span,
+                                name: n,
+                            }),
+                        }
+                    }
+                    None => {
+                        self.tcx.dcx().emit_fatal(errors::MissingDepNode { span: attr.span });
+                    }
+                };
+                self.then_this_would_need.push((
+                    attr.span,
+                    dep_node_interned.unwrap(),
+                    hir_id,
+                    dep_node,
+                ));
+            }
+        }
+    }
+}
+
+impl<'tcx> Visitor<'tcx> for IfThisChanged<'tcx> {
+    type NestedFilter = nested_filter::OnlyBodies;
+
+    fn nested_visit_map(&mut self) -> Self::Map {
+        self.tcx.hir()
+    }
+
+    fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
+        self.process_attrs(item.owner_id.def_id);
+        intravisit::walk_item(self, item);
+    }
+
+    fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
+        self.process_attrs(trait_item.owner_id.def_id);
+        intravisit::walk_trait_item(self, trait_item);
+    }
+
+    fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
+        self.process_attrs(impl_item.owner_id.def_id);
+        intravisit::walk_impl_item(self, impl_item);
+    }
+
+    fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
+        self.process_attrs(s.def_id);
+        intravisit::walk_field_def(self, s);
+    }
+}
+
+fn check_paths<'tcx>(tcx: TyCtxt<'tcx>, if_this_changed: &Sources, then_this_would_need: &Targets) {
+    // Return early here so as not to construct the query, which is not cheap.
+    if if_this_changed.is_empty() {
+        for &(target_span, _, _, _) in then_this_would_need {
+            tcx.dcx().emit_err(errors::MissingIfThisChanged { span: target_span });
+        }
+        return;
+    }
+    tcx.dep_graph.with_query(|query| {
+        for &(_, source_def_id, ref source_dep_node) in if_this_changed {
+            let dependents = query.transitive_predecessors(source_dep_node);
+            for &(target_span, ref target_pass, _, ref target_dep_node) in then_this_would_need {
+                if !dependents.contains(&target_dep_node) {
+                    tcx.dcx().emit_err(errors::NoPath {
+                        span: target_span,
+                        source: tcx.def_path_str(source_def_id),
+                        target: *target_pass,
+                    });
+                } else {
+                    tcx.dcx().emit_err(errors::Ok { span: target_span });
+                }
+            }
+        }
+    });
+}
+
+fn dump_graph(query: &DepGraphQuery) {
+    let path: String = env::var("RUST_DEP_GRAPH").unwrap_or_else(|_| "dep_graph".to_string());
+
+    let nodes = match env::var("RUST_DEP_GRAPH_FILTER") {
+        Ok(string) => {
+            // Expect one of: "-> target", "source -> target", or "source ->".
+            let edge_filter =
+                EdgeFilter::new(&string).unwrap_or_else(|e| bug!("invalid filter: {}", e));
+            let sources = node_set(query, &edge_filter.source);
+            let targets = node_set(query, &edge_filter.target);
+            filter_nodes(query, &sources, &targets)
+        }
+        Err(_) => query.nodes().into_iter().map(|n| n.kind).collect(),
+    };
+    let edges = filter_edges(query, &nodes);
+
+    {
+        // dump a .txt file with just the edges:
+        let txt_path = format!("{path}.txt");
+        let mut file = BufWriter::new(File::create(&txt_path).unwrap());
+        for (source, target) in &edges {
+            write!(file, "{source:?} -> {target:?}\n").unwrap();
+        }
+    }
+
+    {
+        // dump a .dot file in graphviz format:
+        let dot_path = format!("{path}.dot");
+        let mut v = Vec::new();
+        dot::render(&GraphvizDepGraph(nodes, edges), &mut v).unwrap();
+        fs::write(dot_path, v).unwrap();
+    }
+}
+
+#[allow(missing_docs)]
+pub struct GraphvizDepGraph(FxIndexSet<DepKind>, Vec<(DepKind, DepKind)>);
+
+impl<'a> dot::GraphWalk<'a> for GraphvizDepGraph {
+    type Node = DepKind;
+    type Edge = (DepKind, DepKind);
+    fn nodes(&self) -> dot::Nodes<'_, DepKind> {
+        let nodes: Vec<_> = self.0.iter().cloned().collect();
+        nodes.into()
+    }
+    fn edges(&self) -> dot::Edges<'_, (DepKind, DepKind)> {
+        self.1[..].into()
+    }
+    fn source(&self, edge: &(DepKind, DepKind)) -> DepKind {
+        edge.0
+    }
+    fn target(&self, edge: &(DepKind, DepKind)) -> DepKind {
+        edge.1
+    }
+}
+
+impl<'a> dot::Labeller<'a> for GraphvizDepGraph {
+    type Node = DepKind;
+    type Edge = (DepKind, DepKind);
+    fn graph_id(&self) -> dot::Id<'_> {
+        dot::Id::new("DependencyGraph").unwrap()
+    }
+    fn node_id(&self, n: &DepKind) -> dot::Id<'_> {
+        let s: String = format!("{n:?}")
+            .chars()
+            .map(|c| if c == '_' || c.is_alphanumeric() { c } else { '_' })
+            .collect();
+        debug!("n={:?} s={:?}", n, s);
+        dot::Id::new(s).unwrap()
+    }
+    fn node_label(&self, n: &DepKind) -> dot::LabelText<'_> {
+        dot::LabelText::label(format!("{n:?}"))
+    }
+}
+
+// Given an optional filter like `"x,y,z"`, returns either `None` (no
+// filter) or the set of nodes whose labels contain all of those
+// substrings.
+fn node_set<'q>(
+    query: &'q DepGraphQuery,
+    filter: &DepNodeFilter,
+) -> Option<FxIndexSet<&'q DepNode>> {
+    debug!("node_set(filter={:?})", filter);
+
+    if filter.accepts_all() {
+        return None;
+    }
+
+    Some(query.nodes().into_iter().filter(|n| filter.test(n)).collect())
+}
+
+fn filter_nodes<'q>(
+    query: &'q DepGraphQuery,
+    sources: &Option<FxIndexSet<&'q DepNode>>,
+    targets: &Option<FxIndexSet<&'q DepNode>>,
+) -> FxIndexSet<DepKind> {
+    if let Some(sources) = sources {
+        if let Some(targets) = targets {
+            walk_between(query, sources, targets)
+        } else {
+            walk_nodes(query, sources, OUTGOING)
+        }
+    } else if let Some(targets) = targets {
+        walk_nodes(query, targets, INCOMING)
+    } else {
+        query.nodes().into_iter().map(|n| n.kind).collect()
+    }
+}
+
+fn walk_nodes<'q>(
+    query: &'q DepGraphQuery,
+    starts: &FxIndexSet<&'q DepNode>,
+    direction: Direction,
+) -> FxIndexSet<DepKind> {
+    let mut set = FxIndexSet::default();
+    for &start in starts {
+        debug!("walk_nodes: start={:?} outgoing?={:?}", start, direction == OUTGOING);
+        if set.insert(start.kind) {
+            let mut stack = vec![query.indices[start]];
+            while let Some(index) = stack.pop() {
+                for (_, edge) in query.graph.adjacent_edges(index, direction) {
+                    let neighbor_index = edge.source_or_target(direction);
+                    let neighbor = query.graph.node_data(neighbor_index);
+                    if set.insert(neighbor.kind) {
+                        stack.push(neighbor_index);
+                    }
+                }
+            }
+        }
+    }
+    set
+}
+
+fn walk_between<'q>(
+    query: &'q DepGraphQuery,
+    sources: &FxIndexSet<&'q DepNode>,
+    targets: &FxIndexSet<&'q DepNode>,
+) -> FxIndexSet<DepKind> {
+    // This is a bit tricky. We want to include a node only if it is:
+    // (a) reachable from a source and (b) will reach a target. And we
+    // have to be careful about cycles etc. Luckily efficiency is not
+    // a big concern!
+
+    #[derive(Copy, Clone, PartialEq)]
+    enum State {
+        Undecided,
+        Deciding,
+        Included,
+        Excluded,
+    }
+
+    let mut node_states = vec![State::Undecided; query.graph.len_nodes()];
+
+    for &target in targets {
+        node_states[query.indices[target].0] = State::Included;
+    }
+
+    for source in sources.iter().map(|&n| query.indices[n]) {
+        recurse(query, &mut node_states, source);
+    }
+
+    return query
+        .nodes()
+        .into_iter()
+        .filter(|&n| {
+            let index = query.indices[n];
+            node_states[index.0] == State::Included
+        })
+        .map(|n| n.kind)
+        .collect();
+
+    fn recurse(query: &DepGraphQuery, node_states: &mut [State], node: NodeIndex) -> bool {
+        match node_states[node.0] {
+            // known to reach a target
+            State::Included => return true,
+
+            // known not to reach a target
+            State::Excluded => return false,
+
+            // backedge, not yet known, say false
+            State::Deciding => return false,
+
+            State::Undecided => {}
+        }
+
+        node_states[node.0] = State::Deciding;
+
+        for neighbor_index in query.graph.successor_nodes(node) {
+            if recurse(query, node_states, neighbor_index) {
+                node_states[node.0] = State::Included;
+            }
+        }
+
+        // if we didn't find a path to target, then set to excluded
+        if node_states[node.0] == State::Deciding {
+            node_states[node.0] = State::Excluded;
+            false
+        } else {
+            assert!(node_states[node.0] == State::Included);
+            true
+        }
+    }
+}
+
+fn filter_edges(query: &DepGraphQuery, nodes: &FxIndexSet<DepKind>) -> Vec<(DepKind, DepKind)> {
+    let uniq: FxIndexSet<_> = query
+        .edges()
+        .into_iter()
+        .map(|(s, t)| (s.kind, t.kind))
+        .filter(|(source, target)| nodes.contains(source) && nodes.contains(target))
+        .collect();
+    uniq.into_iter().collect()
+}
diff --git a/compiler/rustc_incremental/src/errors.rs b/compiler/rustc_incremental/src/errors.rs
new file mode 100644
index 00000000000..e94a7fb876b
--- /dev/null
+++ b/compiler/rustc_incremental/src/errors.rs
@@ -0,0 +1,314 @@
+use rustc_macros::Diagnostic;
+use rustc_span::{symbol::Ident, Span, Symbol};
+use std::path::{Path, PathBuf};
+
+#[derive(Diagnostic)]
+#[diag(incremental_unrecognized_depnode)]
+pub struct UnrecognizedDepNode {
+    #[primary_span]
+    pub span: Span,
+    pub name: Symbol,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_missing_depnode)]
+pub struct MissingDepNode {
+    #[primary_span]
+    pub span: Span,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_missing_if_this_changed)]
+pub struct MissingIfThisChanged {
+    #[primary_span]
+    pub span: Span,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_ok)]
+pub struct Ok {
+    #[primary_span]
+    pub span: Span,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_no_path)]
+pub struct NoPath {
+    #[primary_span]
+    pub span: Span,
+    pub target: Symbol,
+    pub source: String,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_assertion_auto)]
+pub struct AssertionAuto<'a> {
+    #[primary_span]
+    pub span: Span,
+    pub name: &'a str,
+    pub e: &'a str,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_undefined_clean_dirty_assertions_item)]
+pub struct UndefinedCleanDirtyItem {
+    #[primary_span]
+    pub span: Span,
+    pub kind: String,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_undefined_clean_dirty_assertions)]
+pub struct UndefinedCleanDirty {
+    #[primary_span]
+    pub span: Span,
+    pub kind: String,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_repeated_depnode_label)]
+pub struct RepeatedDepNodeLabel<'a> {
+    #[primary_span]
+    pub span: Span,
+    pub label: &'a str,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_unrecognized_depnode_label)]
+pub struct UnrecognizedDepNodeLabel<'a> {
+    #[primary_span]
+    pub span: Span,
+    pub label: &'a str,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_not_dirty)]
+pub struct NotDirty<'a> {
+    #[primary_span]
+    pub span: Span,
+    pub dep_node_str: &'a str,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_not_clean)]
+pub struct NotClean<'a> {
+    #[primary_span]
+    pub span: Span,
+    pub dep_node_str: &'a str,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_not_loaded)]
+pub struct NotLoaded<'a> {
+    #[primary_span]
+    pub span: Span,
+    pub dep_node_str: &'a str,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_unknown_item)]
+pub struct UnknownItem {
+    #[primary_span]
+    pub span: Span,
+    pub name: Symbol,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_no_cfg)]
+pub struct NoCfg {
+    #[primary_span]
+    pub span: Span,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_associated_value_expected_for)]
+pub struct AssociatedValueExpectedFor {
+    #[primary_span]
+    pub span: Span,
+    pub ident: Ident,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_associated_value_expected)]
+pub struct AssociatedValueExpected {
+    #[primary_span]
+    pub span: Span,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_unchecked_clean)]
+pub struct UncheckedClean {
+    #[primary_span]
+    pub span: Span,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_delete_old)]
+pub struct DeleteOld<'a> {
+    pub name: &'a str,
+    pub path: PathBuf,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_create_new)]
+pub struct CreateNew<'a> {
+    pub name: &'a str,
+    pub path: PathBuf,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_write_new)]
+pub struct WriteNew<'a> {
+    pub name: &'a str,
+    pub path: PathBuf,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_canonicalize_path)]
+pub struct CanonicalizePath {
+    pub path: PathBuf,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_create_incr_comp_dir)]
+pub struct CreateIncrCompDir<'a> {
+    pub tag: &'a str,
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_create_lock)]
+pub struct CreateLock<'a> {
+    pub lock_err: std::io::Error,
+    pub session_dir: &'a Path,
+    #[note(incremental_lock_unsupported)]
+    pub is_unsupported_lock: Option<()>,
+    #[help(incremental_cargo_help_1)]
+    #[help(incremental_cargo_help_2)]
+    pub is_cargo: Option<()>,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_delete_lock)]
+pub struct DeleteLock<'a> {
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_hard_link_failed)]
+pub struct HardLinkFailed<'a> {
+    pub path: &'a Path,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_delete_partial)]
+pub struct DeletePartial<'a> {
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_delete_full)]
+pub struct DeleteFull<'a> {
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_finalize)]
+pub struct Finalize<'a> {
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_invalid_gc_failed)]
+pub struct InvalidGcFailed<'a> {
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_finalized_gc_failed)]
+pub struct FinalizedGcFailed<'a> {
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_session_gc_failed)]
+pub struct SessionGcFailed<'a> {
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_assert_not_loaded)]
+pub struct AssertNotLoaded;
+
+#[derive(Diagnostic)]
+#[diag(incremental_assert_loaded)]
+pub struct AssertLoaded;
+
+#[derive(Diagnostic)]
+#[diag(incremental_delete_incompatible)]
+pub struct DeleteIncompatible {
+    pub path: PathBuf,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_load_dep_graph)]
+pub struct LoadDepGraph {
+    pub path: PathBuf,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_write_dep_graph)]
+pub struct WriteDepGraph<'a> {
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_move_dep_graph)]
+pub struct MoveDepGraph<'a> {
+    pub from: &'a Path,
+    pub to: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_create_dep_graph)]
+pub struct CreateDepGraph<'a> {
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_copy_workproduct_to_cache)]
+pub struct CopyWorkProductToCache<'a> {
+    pub from: &'a Path,
+    pub to: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_delete_workproduct)]
+pub struct DeleteWorkProduct<'a> {
+    pub path: &'a Path,
+    pub err: std::io::Error,
+}
+
+#[derive(Diagnostic)]
+#[diag(incremental_corrupt_file)]
+pub struct CorruptFile<'a> {
+    pub path: &'a Path,
+}
diff --git a/compiler/rustc_incremental/src/lib.rs b/compiler/rustc_incremental/src/lib.rs
new file mode 100644
index 00000000000..76e3c0682de
--- /dev/null
+++ b/compiler/rustc_incremental/src/lib.rs
@@ -0,0 +1,25 @@
+//! Support for serializing the dep-graph and reloading it.
+
+// tidy-alphabetical-start
+#![allow(internal_features)]
+#![deny(missing_docs)]
+#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
+#![doc(rust_logo)]
+#![feature(rustdoc_internals)]
+// tidy-alphabetical-end
+
+mod assert_dep_graph;
+mod errors;
+mod persist;
+
+pub use persist::copy_cgu_workproduct_to_incr_comp_cache_dir;
+pub use persist::finalize_session_directory;
+pub use persist::in_incr_comp_dir;
+pub use persist::in_incr_comp_dir_sess;
+pub use persist::load_query_result_cache;
+pub use persist::save_dep_graph;
+pub use persist::save_work_product_index;
+pub use persist::setup_dep_graph;
+pub use persist::LoadResult;
+
+rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
diff --git a/compiler/rustc_incremental/src/persist/README.md b/compiler/rustc_incremental/src/persist/README.md
new file mode 100644
index 00000000000..b01fe219e1e
--- /dev/null
+++ b/compiler/rustc_incremental/src/persist/README.md
@@ -0,0 +1,3 @@
+For info on how the incremental compilation works, see the [rustc dev guide].
+
+[rustc dev guide]: https://rustc-dev-guide.rust-lang.org/query.html
diff --git a/compiler/rustc_incremental/src/persist/data.rs b/compiler/rustc_incremental/src/persist/data.rs
new file mode 100644
index 00000000000..81e5410978d
--- /dev/null
+++ b/compiler/rustc_incremental/src/persist/data.rs
@@ -0,0 +1,13 @@
+//! The data that we will serialize and deserialize.
+
+use rustc_macros::{Decodable, Encodable};
+use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
+
+#[derive(Debug, Encodable, Decodable)]
+pub struct SerializedWorkProduct {
+    /// node that produced the work-product
+    pub id: WorkProductId,
+
+    /// work-product data itself
+    pub work_product: WorkProduct,
+}
diff --git a/compiler/rustc_incremental/src/persist/dirty_clean.rs b/compiler/rustc_incremental/src/persist/dirty_clean.rs
new file mode 100644
index 00000000000..2a0d681fa37
--- /dev/null
+++ b/compiler/rustc_incremental/src/persist/dirty_clean.rs
@@ -0,0 +1,469 @@
+//! Debugging code to test fingerprints computed for query results. For each node marked with
+//! `#[rustc_clean]` we will compare the fingerprint from the current and from the previous
+//! compilation session as appropriate:
+//!
+//! - `#[rustc_clean(cfg="rev2", except="typeck")]` if we are
+//!   in `#[cfg(rev2)]`, then the fingerprints associated with
+//!   `DepNode::typeck(X)` must be DIFFERENT (`X` is the `DefId` of the
+//!   current node).
+//! - `#[rustc_clean(cfg="rev2")]` same as above, except that the
+//!   fingerprints must be the SAME (along with all other fingerprints).
+//!
+//! - `#[rustc_clean(cfg="rev2", loaded_from_disk='typeck")]` asserts that
+//!   the query result for `DepNode::typeck(X)` was actually
+//!   loaded from disk (not just marked green). This can be useful
+//!   to ensure that a test is actually exercising the deserialization
+//!   logic for a particular query result. This can be combined with
+//!   `except`
+//!
+//! Errors are reported if we are in the suitable configuration but
+//! the required condition is not met.
+
+use crate::errors;
+use rustc_ast::{self as ast, Attribute, NestedMetaItem};
+use rustc_data_structures::fx::FxHashSet;
+use rustc_data_structures::unord::UnordSet;
+use rustc_hir::def_id::LocalDefId;
+use rustc_hir::intravisit;
+use rustc_hir::Node as HirNode;
+use rustc_hir::{ImplItemKind, ItemKind as HirItem, TraitItemKind};
+use rustc_middle::dep_graph::{label_strs, DepNode, DepNodeExt};
+use rustc_middle::hir::nested_filter;
+use rustc_middle::ty::TyCtxt;
+use rustc_span::symbol::{sym, Symbol};
+use rustc_span::Span;
+use thin_vec::ThinVec;
+use tracing::debug;
+
+const LOADED_FROM_DISK: Symbol = sym::loaded_from_disk;
+const EXCEPT: Symbol = sym::except;
+const CFG: Symbol = sym::cfg;
+
+// Base and Extra labels to build up the labels
+
+/// For typedef, constants, and statics
+const BASE_CONST: &[&str] = &[label_strs::type_of];
+
+/// DepNodes for functions + methods
+const BASE_FN: &[&str] = &[
+    // Callers will depend on the signature of these items, so we better test
+    label_strs::fn_sig,
+    label_strs::generics_of,
+    label_strs::predicates_of,
+    label_strs::type_of,
+    // And a big part of compilation (that we eventually want to cache) is type inference
+    // information:
+    label_strs::typeck,
+];
+
+/// DepNodes for Hir, which is pretty much everything
+const BASE_HIR: &[&str] = &[
+    // opt_hir_owner_nodes should be computed for all nodes
+    label_strs::opt_hir_owner_nodes,
+];
+
+/// `impl` implementation of struct/trait
+const BASE_IMPL: &[&str] =
+    &[label_strs::associated_item_def_ids, label_strs::generics_of, label_strs::impl_trait_header];
+
+/// DepNodes for exported mir bodies, which is relevant in "executable"
+/// code, i.e., functions+methods
+const BASE_MIR: &[&str] = &[label_strs::optimized_mir, label_strs::promoted_mir];
+
+/// Struct, Enum and Union DepNodes
+///
+/// Note that changing the type of a field does not change the type of the struct or enum, but
+/// adding/removing fields or changing a fields name or visibility does.
+const BASE_STRUCT: &[&str] =
+    &[label_strs::generics_of, label_strs::predicates_of, label_strs::type_of];
+
+/// Trait definition `DepNode`s.
+/// Extra `DepNode`s for functions and methods.
+const EXTRA_ASSOCIATED: &[&str] = &[label_strs::associated_item];
+
+const EXTRA_TRAIT: &[&str] = &[];
+
+// Fully Built Labels
+
+const LABELS_CONST: &[&[&str]] = &[BASE_HIR, BASE_CONST];
+
+/// Constant/Typedef in an impl
+const LABELS_CONST_IN_IMPL: &[&[&str]] = &[BASE_HIR, BASE_CONST, EXTRA_ASSOCIATED];
+
+/// Trait-Const/Typedef DepNodes
+const LABELS_CONST_IN_TRAIT: &[&[&str]] = &[BASE_HIR, BASE_CONST, EXTRA_ASSOCIATED, EXTRA_TRAIT];
+
+/// Function `DepNode`s.
+const LABELS_FN: &[&[&str]] = &[BASE_HIR, BASE_MIR, BASE_FN];
+
+/// Method `DepNode`s.
+const LABELS_FN_IN_IMPL: &[&[&str]] = &[BASE_HIR, BASE_MIR, BASE_FN, EXTRA_ASSOCIATED];
+
+/// Trait method `DepNode`s.
+const LABELS_FN_IN_TRAIT: &[&[&str]] =
+    &[BASE_HIR, BASE_MIR, BASE_FN, EXTRA_ASSOCIATED, EXTRA_TRAIT];
+
+/// For generic cases like inline-assembly, modules, etc.
+const LABELS_HIR_ONLY: &[&[&str]] = &[BASE_HIR];
+
+/// Impl `DepNode`s.
+const LABELS_TRAIT: &[&[&str]] = &[
+    BASE_HIR,
+    &[label_strs::associated_item_def_ids, label_strs::predicates_of, label_strs::generics_of],
+];
+
+/// Impl `DepNode`s.
+const LABELS_IMPL: &[&[&str]] = &[BASE_HIR, BASE_IMPL];
+
+/// Abstract data type (struct, enum, union) `DepNode`s.
+const LABELS_ADT: &[&[&str]] = &[BASE_HIR, BASE_STRUCT];
+
+// FIXME: Struct/Enum/Unions Fields (there is currently no way to attach these)
+//
+// Fields are kind of separate from their containers, as they can change independently from
+// them. We should at least check
+//
+//     type_of for these.
+
+type Labels = UnordSet<String>;
+
+/// Represents the requested configuration by rustc_clean/dirty
+struct Assertion {
+    clean: Labels,
+    dirty: Labels,
+    loaded_from_disk: Labels,
+}
+
+pub fn check_dirty_clean_annotations(tcx: TyCtxt<'_>) {
+    if !tcx.sess.opts.unstable_opts.query_dep_graph {
+        return;
+    }
+
+    // can't add `#[rustc_clean]` etc without opting into this feature
+    if !tcx.features().rustc_attrs {
+        return;
+    }
+
+    tcx.dep_graph.with_ignore(|| {
+        let mut dirty_clean_visitor = DirtyCleanVisitor { tcx, checked_attrs: Default::default() };
+
+        let crate_items = tcx.hir_crate_items(());
+
+        for id in crate_items.free_items() {
+            dirty_clean_visitor.check_item(id.owner_id.def_id);
+        }
+
+        for id in crate_items.trait_items() {
+            dirty_clean_visitor.check_item(id.owner_id.def_id);
+        }
+
+        for id in crate_items.impl_items() {
+            dirty_clean_visitor.check_item(id.owner_id.def_id);
+        }
+
+        for id in crate_items.foreign_items() {
+            dirty_clean_visitor.check_item(id.owner_id.def_id);
+        }
+
+        let mut all_attrs = FindAllAttrs { tcx, found_attrs: vec![] };
+        tcx.hir().walk_attributes(&mut all_attrs);
+
+        // Note that we cannot use the existing "unused attribute"-infrastructure
+        // here, since that is running before codegen. This is also the reason why
+        // all codegen-specific attributes are `AssumedUsed` in rustc_ast::feature_gate.
+        all_attrs.report_unchecked_attrs(dirty_clean_visitor.checked_attrs);
+    })
+}
+
+pub struct DirtyCleanVisitor<'tcx> {
+    tcx: TyCtxt<'tcx>,
+    checked_attrs: FxHashSet<ast::AttrId>,
+}
+
+impl<'tcx> DirtyCleanVisitor<'tcx> {
+    /// Possibly "deserialize" the attribute into a clean/dirty assertion
+    fn assertion_maybe(&mut self, item_id: LocalDefId, attr: &Attribute) -> Option<Assertion> {
+        assert!(attr.has_name(sym::rustc_clean));
+        if !check_config(self.tcx, attr) {
+            // skip: not the correct `cfg=`
+            return None;
+        }
+        let assertion = self.assertion_auto(item_id, attr);
+        Some(assertion)
+    }
+
+    /// Gets the "auto" assertion on pre-validated attr, along with the `except` labels.
+    fn assertion_auto(&mut self, item_id: LocalDefId, attr: &Attribute) -> Assertion {
+        let (name, mut auto) = self.auto_labels(item_id, attr);
+        let except = self.except(attr);
+        let loaded_from_disk = self.loaded_from_disk(attr);
+        for e in except.items().into_sorted_stable_ord() {
+            if !auto.remove(e) {
+                self.tcx.dcx().emit_fatal(errors::AssertionAuto { span: attr.span, name, e });
+            }
+        }
+        Assertion { clean: auto, dirty: except, loaded_from_disk }
+    }
+
+    /// `loaded_from_disk=` attribute value
+    fn loaded_from_disk(&self, attr: &Attribute) -> Labels {
+        for item in attr.meta_item_list().unwrap_or_else(ThinVec::new) {
+            if item.has_name(LOADED_FROM_DISK) {
+                let value = expect_associated_value(self.tcx, &item);
+                return self.resolve_labels(&item, value);
+            }
+        }
+        // If `loaded_from_disk=` is not specified, don't assert anything
+        Labels::default()
+    }
+
+    /// `except=` attribute value
+    fn except(&self, attr: &Attribute) -> Labels {
+        for item in attr.meta_item_list().unwrap_or_else(ThinVec::new) {
+            if item.has_name(EXCEPT) {
+                let value = expect_associated_value(self.tcx, &item);
+                return self.resolve_labels(&item, value);
+            }
+        }
+        // if no `label` or `except` is given, only the node's group are asserted
+        Labels::default()
+    }
+
+    /// Return all DepNode labels that should be asserted for this item.
+    /// index=0 is the "name" used for error messages
+    fn auto_labels(&mut self, item_id: LocalDefId, attr: &Attribute) -> (&'static str, Labels) {
+        let node = self.tcx.hir_node_by_def_id(item_id);
+        let (name, labels) = match node {
+            HirNode::Item(item) => {
+                match item.kind {
+                    // note: these are in the same order as hir::Item_;
+                    // FIXME(michaelwoerister): do commented out ones
+
+                    // // An `extern crate` item, with optional original crate name,
+                    // HirItem::ExternCrate(..),  // intentionally no assertions
+
+                    // // `use foo::bar::*;` or `use foo::bar::baz as quux;`
+                    // HirItem::Use(..),  // intentionally no assertions
+
+                    // A `static` item
+                    HirItem::Static(..) => ("ItemStatic", LABELS_CONST),
+
+                    // A `const` item
+                    HirItem::Const(..) => ("ItemConst", LABELS_CONST),
+
+                    // A function declaration
+                    HirItem::Fn(..) => ("ItemFn", LABELS_FN),
+
+                    // // A module
+                    HirItem::Mod(..) => ("ItemMod", LABELS_HIR_ONLY),
+
+                    // // An external module
+                    HirItem::ForeignMod { .. } => ("ItemForeignMod", LABELS_HIR_ONLY),
+
+                    // Module-level inline assembly (from global_asm!)
+                    HirItem::GlobalAsm(..) => ("ItemGlobalAsm", LABELS_HIR_ONLY),
+
+                    // A type alias, e.g., `type Foo = Bar<u8>`
+                    HirItem::TyAlias(..) => ("ItemTy", LABELS_HIR_ONLY),
+
+                    // An enum definition, e.g., `enum Foo<A, B> {C<A>, D<B>}`
+                    HirItem::Enum(..) => ("ItemEnum", LABELS_ADT),
+
+                    // A struct definition, e.g., `struct Foo<A> {x: A}`
+                    HirItem::Struct(..) => ("ItemStruct", LABELS_ADT),
+
+                    // A union definition, e.g., `union Foo<A, B> {x: A, y: B}`
+                    HirItem::Union(..) => ("ItemUnion", LABELS_ADT),
+
+                    // Represents a Trait Declaration
+                    HirItem::Trait(..) => ("ItemTrait", LABELS_TRAIT),
+
+                    // An implementation, eg `impl<A> Trait for Foo { .. }`
+                    HirItem::Impl { .. } => ("ItemKind::Impl", LABELS_IMPL),
+
+                    _ => self.tcx.dcx().emit_fatal(errors::UndefinedCleanDirtyItem {
+                        span: attr.span,
+                        kind: format!("{:?}", item.kind),
+                    }),
+                }
+            }
+            HirNode::TraitItem(item) => match item.kind {
+                TraitItemKind::Fn(..) => ("Node::TraitItem", LABELS_FN_IN_TRAIT),
+                TraitItemKind::Const(..) => ("NodeTraitConst", LABELS_CONST_IN_TRAIT),
+                TraitItemKind::Type(..) => ("NodeTraitType", LABELS_CONST_IN_TRAIT),
+            },
+            HirNode::ImplItem(item) => match item.kind {
+                ImplItemKind::Fn(..) => ("Node::ImplItem", LABELS_FN_IN_IMPL),
+                ImplItemKind::Const(..) => ("NodeImplConst", LABELS_CONST_IN_IMPL),
+                ImplItemKind::Type(..) => ("NodeImplType", LABELS_CONST_IN_IMPL),
+            },
+            _ => self.tcx.dcx().emit_fatal(errors::UndefinedCleanDirty {
+                span: attr.span,
+                kind: format!("{node:?}"),
+            }),
+        };
+        let labels =
+            Labels::from_iter(labels.iter().flat_map(|s| s.iter().map(|l| (*l).to_string())));
+        (name, labels)
+    }
+
+    fn resolve_labels(&self, item: &NestedMetaItem, value: Symbol) -> Labels {
+        let mut out = Labels::default();
+        for label in value.as_str().split(',') {
+            let label = label.trim();
+            if DepNode::has_label_string(label) {
+                if out.contains(label) {
+                    self.tcx
+                        .dcx()
+                        .emit_fatal(errors::RepeatedDepNodeLabel { span: item.span(), label });
+                }
+                out.insert(label.to_string());
+            } else {
+                self.tcx
+                    .dcx()
+                    .emit_fatal(errors::UnrecognizedDepNodeLabel { span: item.span(), label });
+            }
+        }
+        out
+    }
+
+    fn dep_node_str(&self, dep_node: &DepNode) -> String {
+        if let Some(def_id) = dep_node.extract_def_id(self.tcx) {
+            format!("{:?}({})", dep_node.kind, self.tcx.def_path_str(def_id))
+        } else {
+            format!("{:?}({:?})", dep_node.kind, dep_node.hash)
+        }
+    }
+
+    fn assert_dirty(&self, item_span: Span, dep_node: DepNode) {
+        debug!("assert_dirty({:?})", dep_node);
+
+        if self.tcx.dep_graph.is_green(&dep_node) {
+            let dep_node_str = self.dep_node_str(&dep_node);
+            self.tcx
+                .dcx()
+                .emit_err(errors::NotDirty { span: item_span, dep_node_str: &dep_node_str });
+        }
+    }
+
+    fn assert_clean(&self, item_span: Span, dep_node: DepNode) {
+        debug!("assert_clean({:?})", dep_node);
+
+        if self.tcx.dep_graph.is_red(&dep_node) {
+            let dep_node_str = self.dep_node_str(&dep_node);
+            self.tcx
+                .dcx()
+                .emit_err(errors::NotClean { span: item_span, dep_node_str: &dep_node_str });
+        }
+    }
+
+    fn assert_loaded_from_disk(&self, item_span: Span, dep_node: DepNode) {
+        debug!("assert_loaded_from_disk({:?})", dep_node);
+
+        if !self.tcx.dep_graph.debug_was_loaded_from_disk(dep_node) {
+            let dep_node_str = self.dep_node_str(&dep_node);
+            self.tcx
+                .dcx()
+                .emit_err(errors::NotLoaded { span: item_span, dep_node_str: &dep_node_str });
+        }
+    }
+
+    fn check_item(&mut self, item_id: LocalDefId) {
+        let item_span = self.tcx.def_span(item_id.to_def_id());
+        let def_path_hash = self.tcx.def_path_hash(item_id.to_def_id());
+        for attr in self.tcx.get_attrs(item_id, sym::rustc_clean) {
+            let Some(assertion) = self.assertion_maybe(item_id, attr) else {
+                continue;
+            };
+            self.checked_attrs.insert(attr.id);
+            for label in assertion.clean.items().into_sorted_stable_ord() {
+                let dep_node = DepNode::from_label_string(self.tcx, label, def_path_hash).unwrap();
+                self.assert_clean(item_span, dep_node);
+            }
+            for label in assertion.dirty.items().into_sorted_stable_ord() {
+                let dep_node = DepNode::from_label_string(self.tcx, label, def_path_hash).unwrap();
+                self.assert_dirty(item_span, dep_node);
+            }
+            for label in assertion.loaded_from_disk.items().into_sorted_stable_ord() {
+                let dep_node = DepNode::from_label_string(self.tcx, label, def_path_hash).unwrap();
+                self.assert_loaded_from_disk(item_span, dep_node);
+            }
+        }
+    }
+}
+
+/// Given a `#[rustc_clean]` attribute, scan for a `cfg="foo"` attribute and check whether we have
+/// a cfg flag called `foo`.
+fn check_config(tcx: TyCtxt<'_>, attr: &Attribute) -> bool {
+    debug!("check_config(attr={:?})", attr);
+    let config = &tcx.sess.psess.config;
+    debug!("check_config: config={:?}", config);
+    let mut cfg = None;
+    for item in attr.meta_item_list().unwrap_or_else(ThinVec::new) {
+        if item.has_name(CFG) {
+            let value = expect_associated_value(tcx, &item);
+            debug!("check_config: searching for cfg {:?}", value);
+            cfg = Some(config.contains(&(value, None)));
+        } else if !(item.has_name(EXCEPT) || item.has_name(LOADED_FROM_DISK)) {
+            tcx.dcx().emit_err(errors::UnknownItem { span: attr.span, name: item.name_or_empty() });
+        }
+    }
+
+    match cfg {
+        None => tcx.dcx().emit_fatal(errors::NoCfg { span: attr.span }),
+        Some(c) => c,
+    }
+}
+
+fn expect_associated_value(tcx: TyCtxt<'_>, item: &NestedMetaItem) -> Symbol {
+    if let Some(value) = item.value_str() {
+        value
+    } else {
+        if let Some(ident) = item.ident() {
+            tcx.dcx().emit_fatal(errors::AssociatedValueExpectedFor { span: item.span(), ident });
+        } else {
+            tcx.dcx().emit_fatal(errors::AssociatedValueExpected { span: item.span() });
+        }
+    }
+}
+
+/// A visitor that collects all `#[rustc_clean]` attributes from
+/// the HIR. It is used to verify that we really ran checks for all annotated
+/// nodes.
+pub struct FindAllAttrs<'tcx> {
+    tcx: TyCtxt<'tcx>,
+    found_attrs: Vec<&'tcx Attribute>,
+}
+
+impl<'tcx> FindAllAttrs<'tcx> {
+    fn is_active_attr(&mut self, attr: &Attribute) -> bool {
+        if attr.has_name(sym::rustc_clean) && check_config(self.tcx, attr) {
+            return true;
+        }
+
+        false
+    }
+
+    fn report_unchecked_attrs(&self, mut checked_attrs: FxHashSet<ast::AttrId>) {
+        for attr in &self.found_attrs {
+            if !checked_attrs.contains(&attr.id) {
+                self.tcx.dcx().emit_err(errors::UncheckedClean { span: attr.span });
+                checked_attrs.insert(attr.id);
+            }
+        }
+    }
+}
+
+impl<'tcx> intravisit::Visitor<'tcx> for FindAllAttrs<'tcx> {
+    type NestedFilter = nested_filter::All;
+
+    fn nested_visit_map(&mut self) -> Self::Map {
+        self.tcx.hir()
+    }
+
+    fn visit_attribute(&mut self, attr: &'tcx Attribute) {
+        if self.is_active_attr(attr) {
+            self.found_attrs.push(attr);
+        }
+    }
+}
diff --git a/compiler/rustc_incremental/src/persist/file_format.rs b/compiler/rustc_incremental/src/persist/file_format.rs
new file mode 100644
index 00000000000..303785bdb22
--- /dev/null
+++ b/compiler/rustc_incremental/src/persist/file_format.rs
@@ -0,0 +1,178 @@
+//! This module defines a generic file format that allows to check if a given
+//! file generated by incremental compilation was generated by a compatible
+//! compiler version. This file format is used for the on-disk version of the
+//! dependency graph and the exported metadata hashes.
+//!
+//! In practice "compatible compiler version" means "exactly the same compiler
+//! version", since the header encodes the git commit hash of the compiler.
+//! Since we can always just ignore the incremental compilation cache and
+//! compiler versions don't change frequently for the typical user, being
+//! conservative here practically has no downside.
+
+use crate::errors;
+use rustc_data_structures::memmap::Mmap;
+use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
+use rustc_serialize::Encoder;
+use rustc_session::Session;
+use std::borrow::Cow;
+use std::env;
+use std::fs;
+use std::io::{self, Read};
+use std::path::{Path, PathBuf};
+use tracing::debug;
+
+/// The first few bytes of files generated by incremental compilation.
+const FILE_MAGIC: &[u8] = b"RSIC";
+
+/// Change this if the header format changes.
+const HEADER_FORMAT_VERSION: u16 = 0;
+
+pub(crate) fn write_file_header(stream: &mut FileEncoder, sess: &Session) {
+    stream.emit_raw_bytes(FILE_MAGIC);
+    stream
+        .emit_raw_bytes(&[(HEADER_FORMAT_VERSION >> 0) as u8, (HEADER_FORMAT_VERSION >> 8) as u8]);
+
+    let rustc_version = rustc_version(sess.is_nightly_build(), sess.cfg_version);
+    assert_eq!(rustc_version.len(), (rustc_version.len() as u8) as usize);
+    stream.emit_raw_bytes(&[rustc_version.len() as u8]);
+    stream.emit_raw_bytes(rustc_version.as_bytes());
+}
+
+pub(crate) fn save_in<F>(sess: &Session, path_buf: PathBuf, name: &str, encode: F)
+where
+    F: FnOnce(FileEncoder) -> FileEncodeResult,
+{
+    debug!("save: storing data in {}", path_buf.display());
+
+    // Delete the old file, if any.
+    // Note: It's important that we actually delete the old file and not just
+    // truncate and overwrite it, since it might be a shared hard-link, the
+    // underlying data of which we don't want to modify.
+    //
+    // We have to ensure we have dropped the memory maps to this file
+    // before performing this removal.
+    match fs::remove_file(&path_buf) {
+        Ok(()) => {
+            debug!("save: remove old file");
+        }
+        Err(err) if err.kind() == io::ErrorKind::NotFound => (),
+        Err(err) => sess.dcx().emit_fatal(errors::DeleteOld { name, path: path_buf, err }),
+    }
+
+    let mut encoder = match FileEncoder::new(&path_buf) {
+        Ok(encoder) => encoder,
+        Err(err) => sess.dcx().emit_fatal(errors::CreateNew { name, path: path_buf, err }),
+    };
+
+    write_file_header(&mut encoder, sess);
+
+    match encode(encoder) {
+        Ok(position) => {
+            sess.prof.artifact_size(
+                &name.replace(' ', "_"),
+                path_buf.file_name().unwrap().to_string_lossy(),
+                position as u64,
+            );
+            debug!("save: data written to disk successfully");
+        }
+        Err((path, err)) => sess.dcx().emit_fatal(errors::WriteNew { name, path, err }),
+    }
+}
+
+/// Reads the contents of a file with a file header as defined in this module.
+///
+/// - Returns `Ok(Some(data, pos))` if the file existed and was generated by a
+///   compatible compiler version. `data` is the entire contents of the file
+///   and `pos` points to the first byte after the header.
+/// - Returns `Ok(None)` if the file did not exist or was generated by an
+///   incompatible version of the compiler.
+/// - Returns `Err(..)` if some kind of IO error occurred while reading the
+///   file.
+pub fn read_file(
+    path: &Path,
+    report_incremental_info: bool,
+    is_nightly_build: bool,
+    cfg_version: &'static str,
+) -> io::Result<Option<(Mmap, usize)>> {
+    let file = match fs::File::open(path) {
+        Ok(file) => file,
+        Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
+        Err(err) => return Err(err),
+    };
+    // SAFETY: This process must not modify nor remove the backing file while the memory map lives.
+    // For the dep-graph and the work product index, it is as soon as the decoding is done.
+    // For the query result cache, the memory map is dropped in save_dep_graph before calling
+    // save_in and trying to remove the backing file.
+    //
+    // There is no way to prevent another process from modifying this file.
+    let mmap = unsafe { Mmap::map(file) }?;
+
+    let mut file = io::Cursor::new(&*mmap);
+
+    // Check FILE_MAGIC
+    {
+        debug_assert!(FILE_MAGIC.len() == 4);
+        let mut file_magic = [0u8; 4];
+        file.read_exact(&mut file_magic)?;
+        if file_magic != FILE_MAGIC {
+            report_format_mismatch(report_incremental_info, path, "Wrong FILE_MAGIC");
+            return Ok(None);
+        }
+    }
+
+    // Check HEADER_FORMAT_VERSION
+    {
+        debug_assert!(::std::mem::size_of_val(&HEADER_FORMAT_VERSION) == 2);
+        let mut header_format_version = [0u8; 2];
+        file.read_exact(&mut header_format_version)?;
+        let header_format_version =
+            (header_format_version[0] as u16) | ((header_format_version[1] as u16) << 8);
+
+        if header_format_version != HEADER_FORMAT_VERSION {
+            report_format_mismatch(report_incremental_info, path, "Wrong HEADER_FORMAT_VERSION");
+            return Ok(None);
+        }
+    }
+
+    // Check RUSTC_VERSION
+    {
+        let mut rustc_version_str_len = [0u8; 1];
+        file.read_exact(&mut rustc_version_str_len)?;
+        let rustc_version_str_len = rustc_version_str_len[0] as usize;
+        let mut buffer = vec![0; rustc_version_str_len];
+        file.read_exact(&mut buffer)?;
+
+        if buffer != rustc_version(is_nightly_build, cfg_version).as_bytes() {
+            report_format_mismatch(report_incremental_info, path, "Different compiler version");
+            return Ok(None);
+        }
+    }
+
+    let post_header_start_pos = file.position() as usize;
+    Ok(Some((mmap, post_header_start_pos)))
+}
+
+fn report_format_mismatch(report_incremental_info: bool, file: &Path, message: &str) {
+    debug!("read_file: {}", message);
+
+    if report_incremental_info {
+        eprintln!(
+            "[incremental] ignoring cache artifact `{}`: {}",
+            file.file_name().unwrap().to_string_lossy(),
+            message
+        );
+    }
+}
+
+/// A version string that hopefully is always different for compiler versions
+/// with different encodings of incremental compilation artifacts. Contains
+/// the Git commit hash.
+fn rustc_version(nightly_build: bool, cfg_version: &'static str) -> Cow<'static, str> {
+    if nightly_build {
+        if let Ok(val) = env::var("RUSTC_FORCE_RUSTC_VERSION") {
+            return val.into();
+        }
+    }
+
+    cfg_version.into()
+}
diff --git a/compiler/rustc_incremental/src/persist/fs.rs b/compiler/rustc_incremental/src/persist/fs.rs
new file mode 100644
index 00000000000..9afea3d66b0
--- /dev/null
+++ b/compiler/rustc_incremental/src/persist/fs.rs
@@ -0,0 +1,927 @@
+//! This module manages how the incremental compilation cache is represented in
+//! the file system.
+//!
+//! Incremental compilation caches are managed according to a copy-on-write
+//! strategy: Once a complete, consistent cache version is finalized, it is
+//! never modified. Instead, when a subsequent compilation session is started,
+//! the compiler will allocate a new version of the cache that starts out as
+//! a copy of the previous version. Then only this new copy is modified and it
+//! will not be visible to other processes until it is finalized. This ensures
+//! that multiple compiler processes can be executed concurrently for the same
+//! crate without interfering with each other or blocking each other.
+//!
+//! More concretely this is implemented via the following protocol:
+//!
+//! 1. For a newly started compilation session, the compiler allocates a
+//!    new `session` directory within the incremental compilation directory.
+//!    This session directory will have a unique name that ends with the suffix
+//!    "-working" and that contains a creation timestamp.
+//! 2. Next, the compiler looks for the newest finalized session directory,
+//!    that is, a session directory from a previous compilation session that
+//!    has been marked as valid and consistent. A session directory is
+//!    considered finalized if the "-working" suffix in the directory name has
+//!    been replaced by the SVH of the crate.
+//! 3. Once the compiler has found a valid, finalized session directory, it will
+//!    hard-link/copy its contents into the new "-working" directory. If all
+//!    goes well, it will have its own, private copy of the source directory and
+//!    subsequently not have to worry about synchronizing with other compiler
+//!    processes.
+//! 4. Now the compiler can do its normal compilation process, which involves
+//!    reading and updating its private session directory.
+//! 5. When compilation finishes without errors, the private session directory
+//!    will be in a state where it can be used as input for other compilation
+//!    sessions. That is, it will contain a dependency graph and cache artifacts
+//!    that are consistent with the state of the source code it was compiled
+//!    from, with no need to change them ever again. At this point, the compiler
+//!    finalizes and "publishes" its private session directory by renaming it
+//!    from "s-{timestamp}-{random}-working" to "s-{timestamp}-{SVH}".
+//! 6. At this point the "old" session directory that we copied our data from
+//!    at the beginning of the session has become obsolete because we have just
+//!    published a more current version. Thus the compiler will delete it.
+//!
+//! ## Garbage Collection
+//!
+//! Naively following the above protocol might lead to old session directories
+//! piling up if a compiler instance crashes for some reason before its able to
+//! remove its private session directory. In order to avoid wasting disk space,
+//! the compiler also does some garbage collection each time it is started in
+//! incremental compilation mode. Specifically, it will scan the incremental
+//! compilation directory for private session directories that are not in use
+//! any more and will delete those. It will also delete any finalized session
+//! directories for a given crate except for the most recent one.
+//!
+//! ## Synchronization
+//!
+//! There is some synchronization needed in order for the compiler to be able to
+//! determine whether a given private session directory is not in use any more.
+//! This is done by creating a lock file for each session directory and
+//! locking it while the directory is still being used. Since file locks have
+//! operating system support, we can rely on the lock being released if the
+//! compiler process dies for some unexpected reason. Thus, when garbage
+//! collecting private session directories, the collecting process can determine
+//! whether the directory is still in use by trying to acquire a lock on the
+//! file. If locking the file fails, the original process must still be alive.
+//! If locking the file succeeds, we know that the owning process is not alive
+//! any more and we can safely delete the directory.
+//! There is still a small time window between the original process creating the
+//! lock file and actually locking it. In order to minimize the chance that
+//! another process tries to acquire the lock in just that instance, only
+//! session directories that are older than a few seconds are considered for
+//! garbage collection.
+//!
+//! Another case that has to be considered is what happens if one process
+//! deletes a finalized session directory that another process is currently
+//! trying to copy from. This case is also handled via the lock file. Before
+//! a process starts copying a finalized session directory, it will acquire a
+//! shared lock on the directory's lock file. Any garbage collecting process,
+//! on the other hand, will acquire an exclusive lock on the lock file.
+//! Thus, if a directory is being collected, any reader process will fail
+//! acquiring the shared lock and will leave the directory alone. Conversely,
+//! if a collecting process can't acquire the exclusive lock because the
+//! directory is currently being read from, it will leave collecting that
+//! directory to another process at a later point in time.
+//! The exact same scheme is also used when reading the metadata hashes file
+//! from an extern crate. When a crate is compiled, the hash values of its
+//! metadata are stored in a file in its session directory. When the
+//! compilation session of another crate imports the first crate's metadata,
+//! it also has to read in the accompanying metadata hashes. It thus will access
+//! the finalized session directory of all crates it links to and while doing
+//! so, it will also place a read lock on that the respective session directory
+//! so that it won't be deleted while the metadata hashes are loaded.
+//!
+//! ## Preconditions
+//!
+//! This system relies on two features being available in the file system in
+//! order to work really well: file locking and hard linking.
+//! If hard linking is not available (like on FAT) the data in the cache
+//! actually has to be copied at the beginning of each session.
+//! If file locking does not work reliably (like on NFS), some of the
+//! synchronization will go haywire.
+//! In both cases we recommend to locate the incremental compilation directory
+//! on a file system that supports these things.
+//! It might be a good idea though to try and detect whether we are on an
+//! unsupported file system and emit a warning in that case. This is not yet
+//! implemented.
+
+use crate::errors;
+use rustc_data_structures::base_n;
+use rustc_data_structures::base_n::BaseNString;
+use rustc_data_structures::base_n::ToBaseN;
+use rustc_data_structures::base_n::CASE_INSENSITIVE;
+use rustc_data_structures::flock;
+use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
+use rustc_data_structures::svh::Svh;
+use rustc_data_structures::unord::{UnordMap, UnordSet};
+use rustc_errors::ErrorGuaranteed;
+use rustc_fs_util::{link_or_copy, try_canonicalize, LinkOrCopy};
+use rustc_middle::bug;
+use rustc_session::config::CrateType;
+use rustc_session::output::{collect_crate_types, find_crate_name};
+use rustc_session::{Session, StableCrateId};
+
+use std::fs as std_fs;
+use std::io::{self, ErrorKind};
+use std::path::{Path, PathBuf};
+use std::time::{Duration, SystemTime, UNIX_EPOCH};
+
+use rand::{thread_rng, RngCore};
+use tracing::debug;
+
+#[cfg(test)]
+mod tests;
+
+const LOCK_FILE_EXT: &str = ".lock";
+const DEP_GRAPH_FILENAME: &str = "dep-graph.bin";
+const STAGING_DEP_GRAPH_FILENAME: &str = "dep-graph.part.bin";
+const WORK_PRODUCTS_FILENAME: &str = "work-products.bin";
+const QUERY_CACHE_FILENAME: &str = "query-cache.bin";
+
+// We encode integers using the following base, so they are shorter than decimal
+// or hexadecimal numbers (we want short file and directory names). Since these
+// numbers will be used in file names, we choose an encoding that is not
+// case-sensitive (as opposed to base64, for example).
+const INT_ENCODE_BASE: usize = base_n::CASE_INSENSITIVE;
+
+/// Returns the path to a session's dependency graph.
+pub(crate) fn dep_graph_path(sess: &Session) -> PathBuf {
+    in_incr_comp_dir_sess(sess, DEP_GRAPH_FILENAME)
+}
+
+/// Returns the path to a session's staging dependency graph.
+///
+/// On the difference between dep-graph and staging dep-graph,
+/// see `build_dep_graph`.
+pub(crate) fn staging_dep_graph_path(sess: &Session) -> PathBuf {
+    in_incr_comp_dir_sess(sess, STAGING_DEP_GRAPH_FILENAME)
+}
+
+pub(crate) fn work_products_path(sess: &Session) -> PathBuf {
+    in_incr_comp_dir_sess(sess, WORK_PRODUCTS_FILENAME)
+}
+
+/// Returns the path to a session's query cache.
+pub fn query_cache_path(sess: &Session) -> PathBuf {
+    in_incr_comp_dir_sess(sess, QUERY_CACHE_FILENAME)
+}
+
+/// Locks a given session directory.
+fn lock_file_path(session_dir: &Path) -> PathBuf {
+    let crate_dir = session_dir.parent().unwrap();
+
+    let directory_name = session_dir
+        .file_name()
+        .unwrap()
+        .to_str()
+        .expect("malformed session dir name: contains non-Unicode characters");
+
+    let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
+    if dash_indices.len() != 3 {
+        bug!(
+            "Encountered incremental compilation session directory with \
+              malformed name: {}",
+            session_dir.display()
+        )
+    }
+
+    crate_dir.join(&directory_name[0..dash_indices[2]]).with_extension(&LOCK_FILE_EXT[1..])
+}
+
+/// Returns the path for a given filename within the incremental compilation directory
+/// in the current session.
+pub fn in_incr_comp_dir_sess(sess: &Session, file_name: &str) -> PathBuf {
+    in_incr_comp_dir(&sess.incr_comp_session_dir(), file_name)
+}
+
+/// Returns the path for a given filename within the incremental compilation directory,
+/// not necessarily from the current session.
+///
+/// To ensure the file is part of the current session, use [`in_incr_comp_dir_sess`].
+pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBuf {
+    incr_comp_session_dir.join(file_name)
+}
+
+/// Allocates the private session directory.
+///
+/// If the result of this function is `Ok`, we have a valid incremental
+/// compilation session directory. A valid session
+/// directory is one that contains a locked lock file. It may or may not contain
+/// a dep-graph and work products from a previous session.
+///
+/// This always attempts to load a dep-graph from the directory.
+/// If loading fails for some reason, we fallback to a disabled `DepGraph`.
+/// See [`rustc_interface::queries::dep_graph`].
+///
+/// If this function returns an error, it may leave behind an invalid session directory.
+/// The garbage collection will take care of it.
+///
+/// [`rustc_interface::queries::dep_graph`]: ../../rustc_interface/struct.Queries.html#structfield.dep_graph
+pub(crate) fn prepare_session_directory(sess: &Session) -> Result<(), ErrorGuaranteed> {
+    if sess.opts.incremental.is_none() {
+        return Ok(());
+    }
+
+    let _timer = sess.timer("incr_comp_prepare_session_directory");
+
+    debug!("prepare_session_directory");
+
+    // {incr-comp-dir}/{crate-name-and-disambiguator}
+    let crate_dir = crate_path(sess);
+    debug!("crate-dir: {}", crate_dir.display());
+    create_dir(sess, &crate_dir, "crate")?;
+
+    // Hack: canonicalize the path *after creating the directory*
+    // because, on windows, long paths can cause problems;
+    // canonicalization inserts this weird prefix that makes windows
+    // tolerate long paths.
+    let crate_dir = match try_canonicalize(&crate_dir) {
+        Ok(v) => v,
+        Err(err) => {
+            return Err(sess.dcx().emit_err(errors::CanonicalizePath { path: crate_dir, err }));
+        }
+    };
+
+    let mut source_directories_already_tried = FxHashSet::default();
+
+    loop {
+        // Generate a session directory of the form:
+        //
+        // {incr-comp-dir}/{crate-name-and-disambiguator}/s-{timestamp}-{random}-working
+        let session_dir = generate_session_dir_path(&crate_dir);
+        debug!("session-dir: {}", session_dir.display());
+
+        // Lock the new session directory. If this fails, return an
+        // error without retrying
+        let (directory_lock, lock_file_path) = lock_directory(sess, &session_dir)?;
+
+        // Now that we have the lock, we can actually create the session
+        // directory
+        create_dir(sess, &session_dir, "session")?;
+
+        // Find a suitable source directory to copy from. Ignore those that we
+        // have already tried before.
+        let source_directory = find_source_directory(&crate_dir, &source_directories_already_tried);
+
+        let Some(source_directory) = source_directory else {
+            // There's nowhere to copy from, we're done
+            debug!(
+                "no source directory found. Continuing with empty session \
+                    directory."
+            );
+
+            sess.init_incr_comp_session(session_dir, directory_lock);
+            return Ok(());
+        };
+
+        debug!("attempting to copy data from source: {}", source_directory.display());
+
+        // Try copying over all files from the source directory
+        if let Ok(allows_links) = copy_files(sess, &session_dir, &source_directory) {
+            debug!("successfully copied data from: {}", source_directory.display());
+
+            if !allows_links {
+                sess.dcx().emit_warn(errors::HardLinkFailed { path: &session_dir });
+            }
+
+            sess.init_incr_comp_session(session_dir, directory_lock);
+            return Ok(());
+        } else {
+            debug!("copying failed - trying next directory");
+
+            // Something went wrong while trying to copy/link files from the
+            // source directory. Try again with a different one.
+            source_directories_already_tried.insert(source_directory);
+
+            // Try to remove the session directory we just allocated. We don't
+            // know if there's any garbage in it from the failed copy action.
+            if let Err(err) = safe_remove_dir_all(&session_dir) {
+                sess.dcx().emit_warn(errors::DeletePartial { path: &session_dir, err });
+            }
+
+            delete_session_dir_lock_file(sess, &lock_file_path);
+            drop(directory_lock);
+        }
+    }
+}
+
+/// This function finalizes and thus 'publishes' the session directory by
+/// renaming it to `s-{timestamp}-{svh}` and releasing the file lock.
+/// If there have been compilation errors, however, this function will just
+/// delete the presumably invalid session directory.
+pub fn finalize_session_directory(sess: &Session, svh: Option<Svh>) {
+    if sess.opts.incremental.is_none() {
+        return;
+    }
+    // The svh is always produced when incr. comp. is enabled.
+    let svh = svh.unwrap();
+
+    let _timer = sess.timer("incr_comp_finalize_session_directory");
+
+    let incr_comp_session_dir: PathBuf = sess.incr_comp_session_dir().clone();
+
+    if sess.dcx().has_errors_or_delayed_bugs().is_some() {
+        // If there have been any errors during compilation, we don't want to
+        // publish this session directory. Rather, we'll just delete it.
+
+        debug!(
+            "finalize_session_directory() - invalidating session directory: {}",
+            incr_comp_session_dir.display()
+        );
+
+        if let Err(err) = safe_remove_dir_all(&*incr_comp_session_dir) {
+            sess.dcx().emit_warn(errors::DeleteFull { path: &incr_comp_session_dir, err });
+        }
+
+        let lock_file_path = lock_file_path(&*incr_comp_session_dir);
+        delete_session_dir_lock_file(sess, &lock_file_path);
+        sess.mark_incr_comp_session_as_invalid();
+    }
+
+    debug!("finalize_session_directory() - session directory: {}", incr_comp_session_dir.display());
+
+    let mut sub_dir_name = incr_comp_session_dir
+        .file_name()
+        .unwrap()
+        .to_str()
+        .expect("malformed session dir name: contains non-Unicode characters")
+        .to_string();
+
+    // Keep the 's-{timestamp}-{random-number}' prefix, but replace "working" with the SVH of the crate
+    sub_dir_name.truncate(sub_dir_name.len() - "working".len());
+    // Double-check that we kept this: "s-{timestamp}-{random-number}-"
+    assert!(sub_dir_name.ends_with('-'), "{:?}", sub_dir_name);
+    assert!(sub_dir_name.as_bytes().iter().filter(|b| **b == b'-').count() == 3);
+
+    // Append the SVH
+    sub_dir_name.push_str(&svh.as_u128().to_base_fixed_len(CASE_INSENSITIVE));
+
+    // Create the full path
+    let new_path = incr_comp_session_dir.parent().unwrap().join(&*sub_dir_name);
+    debug!("finalize_session_directory() - new path: {}", new_path.display());
+
+    match rename_path_with_retry(&*incr_comp_session_dir, &new_path, 3) {
+        Ok(_) => {
+            debug!("finalize_session_directory() - directory renamed successfully");
+
+            // This unlocks the directory
+            sess.finalize_incr_comp_session(new_path);
+        }
+        Err(e) => {
+            // Warn about the error. However, no need to abort compilation now.
+            sess.dcx().emit_warn(errors::Finalize { path: &incr_comp_session_dir, err: e });
+
+            debug!("finalize_session_directory() - error, marking as invalid");
+            // Drop the file lock, so we can garage collect
+            sess.mark_incr_comp_session_as_invalid();
+        }
+    }
+
+    let _ = garbage_collect_session_directories(sess);
+}
+
+pub(crate) fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> {
+    let sess_dir_iterator = sess.incr_comp_session_dir().read_dir()?;
+    for entry in sess_dir_iterator {
+        let entry = entry?;
+        safe_remove_file(&entry.path())?
+    }
+    Ok(())
+}
+
+fn copy_files(sess: &Session, target_dir: &Path, source_dir: &Path) -> Result<bool, ()> {
+    // We acquire a shared lock on the lock file of the directory, so that
+    // nobody deletes it out from under us while we are reading from it.
+    let lock_file_path = lock_file_path(source_dir);
+
+    // not exclusive
+    let Ok(_lock) = flock::Lock::new(
+        &lock_file_path,
+        false, // don't wait,
+        false, // don't create
+        false,
+    ) else {
+        // Could not acquire the lock, don't try to copy from here
+        return Err(());
+    };
+
+    let Ok(source_dir_iterator) = source_dir.read_dir() else {
+        return Err(());
+    };
+
+    let mut files_linked = 0;
+    let mut files_copied = 0;
+
+    for entry in source_dir_iterator {
+        match entry {
+            Ok(entry) => {
+                let file_name = entry.file_name();
+
+                let target_file_path = target_dir.join(file_name);
+                let source_path = entry.path();
+
+                debug!("copying into session dir: {}", source_path.display());
+                match link_or_copy(source_path, target_file_path) {
+                    Ok(LinkOrCopy::Link) => files_linked += 1,
+                    Ok(LinkOrCopy::Copy) => files_copied += 1,
+                    Err(_) => return Err(()),
+                }
+            }
+            Err(_) => return Err(()),
+        }
+    }
+
+    if sess.opts.unstable_opts.incremental_info {
+        eprintln!(
+            "[incremental] session directory: \
+                  {files_linked} files hard-linked"
+        );
+        eprintln!(
+            "[incremental] session directory: \
+                 {files_copied} files copied"
+        );
+    }
+
+    Ok(files_linked > 0 || files_copied == 0)
+}
+
+/// Generates unique directory path of the form:
+/// {crate_dir}/s-{timestamp}-{random-number}-working
+fn generate_session_dir_path(crate_dir: &Path) -> PathBuf {
+    let timestamp = timestamp_to_string(SystemTime::now());
+    debug!("generate_session_dir_path: timestamp = {}", timestamp);
+    let random_number = thread_rng().next_u32();
+    debug!("generate_session_dir_path: random_number = {}", random_number);
+
+    // Chop the first 3 characters off the timestamp. Those 3 bytes will be zero for a while.
+    let (zeroes, timestamp) = timestamp.split_at(3);
+    assert_eq!(zeroes, "000");
+    let directory_name =
+        format!("s-{}-{}-working", timestamp, random_number.to_base_fixed_len(CASE_INSENSITIVE));
+    debug!("generate_session_dir_path: directory_name = {}", directory_name);
+    let directory_path = crate_dir.join(directory_name);
+    debug!("generate_session_dir_path: directory_path = {}", directory_path.display());
+    directory_path
+}
+
+fn create_dir(sess: &Session, path: &Path, dir_tag: &str) -> Result<(), ErrorGuaranteed> {
+    match std_fs::create_dir_all(path) {
+        Ok(()) => {
+            debug!("{} directory created successfully", dir_tag);
+            Ok(())
+        }
+        Err(err) => Err(sess.dcx().emit_err(errors::CreateIncrCompDir { tag: dir_tag, path, err })),
+    }
+}
+
+/// Allocate the lock-file and lock it.
+fn lock_directory(
+    sess: &Session,
+    session_dir: &Path,
+) -> Result<(flock::Lock, PathBuf), ErrorGuaranteed> {
+    let lock_file_path = lock_file_path(session_dir);
+    debug!("lock_directory() - lock_file: {}", lock_file_path.display());
+
+    match flock::Lock::new(
+        &lock_file_path,
+        false, // don't wait
+        true,  // create the lock file
+        true,
+    ) {
+        // the lock should be exclusive
+        Ok(lock) => Ok((lock, lock_file_path)),
+        Err(lock_err) => {
+            let is_unsupported_lock = flock::Lock::error_unsupported(&lock_err).then_some(());
+            Err(sess.dcx().emit_err(errors::CreateLock {
+                lock_err,
+                session_dir,
+                is_unsupported_lock,
+                is_cargo: rustc_session::utils::was_invoked_from_cargo().then_some(()),
+            }))
+        }
+    }
+}
+
+fn delete_session_dir_lock_file(sess: &Session, lock_file_path: &Path) {
+    if let Err(err) = safe_remove_file(lock_file_path) {
+        sess.dcx().emit_warn(errors::DeleteLock { path: lock_file_path, err });
+    }
+}
+
+/// Finds the most recent published session directory that is not in the
+/// ignore-list.
+fn find_source_directory(
+    crate_dir: &Path,
+    source_directories_already_tried: &FxHashSet<PathBuf>,
+) -> Option<PathBuf> {
+    let iter = crate_dir
+        .read_dir()
+        .unwrap() // FIXME
+        .filter_map(|e| e.ok().map(|e| e.path()));
+
+    find_source_directory_in_iter(iter, source_directories_already_tried)
+}
+
+fn find_source_directory_in_iter<I>(
+    iter: I,
+    source_directories_already_tried: &FxHashSet<PathBuf>,
+) -> Option<PathBuf>
+where
+    I: Iterator<Item = PathBuf>,
+{
+    let mut best_candidate = (UNIX_EPOCH, None);
+
+    for session_dir in iter {
+        debug!("find_source_directory_in_iter - inspecting `{}`", session_dir.display());
+
+        let Some(directory_name) = session_dir.file_name().unwrap().to_str() else {
+            debug!("find_source_directory_in_iter - ignoring");
+            continue;
+        };
+
+        if source_directories_already_tried.contains(&session_dir)
+            || !is_session_directory(&directory_name)
+            || !is_finalized(&directory_name)
+        {
+            debug!("find_source_directory_in_iter - ignoring");
+            continue;
+        }
+
+        let timestamp = match extract_timestamp_from_session_dir(&directory_name) {
+            Ok(timestamp) => timestamp,
+            Err(e) => {
+                debug!("unexpected incr-comp session dir: {}: {}", session_dir.display(), e);
+                continue;
+            }
+        };
+
+        if timestamp > best_candidate.0 {
+            best_candidate = (timestamp, Some(session_dir.clone()));
+        }
+    }
+
+    best_candidate.1
+}
+
+fn is_finalized(directory_name: &str) -> bool {
+    !directory_name.ends_with("-working")
+}
+
+fn is_session_directory(directory_name: &str) -> bool {
+    directory_name.starts_with("s-") && !directory_name.ends_with(LOCK_FILE_EXT)
+}
+
+fn is_session_directory_lock_file(file_name: &str) -> bool {
+    file_name.starts_with("s-") && file_name.ends_with(LOCK_FILE_EXT)
+}
+
+fn extract_timestamp_from_session_dir(directory_name: &str) -> Result<SystemTime, &'static str> {
+    if !is_session_directory(directory_name) {
+        return Err("not a directory");
+    }
+
+    let dash_indices: Vec<_> = directory_name.match_indices('-').map(|(idx, _)| idx).collect();
+    if dash_indices.len() != 3 {
+        return Err("not three dashes in name");
+    }
+
+    string_to_timestamp(&directory_name[dash_indices[0] + 1..dash_indices[1]])
+}
+
+fn timestamp_to_string(timestamp: SystemTime) -> BaseNString {
+    let duration = timestamp.duration_since(UNIX_EPOCH).unwrap();
+    let micros = duration.as_secs() * 1_000_000 + (duration.subsec_nanos() as u64) / 1000;
+    micros.to_base_fixed_len(CASE_INSENSITIVE)
+}
+
+fn string_to_timestamp(s: &str) -> Result<SystemTime, &'static str> {
+    let micros_since_unix_epoch = u64::from_str_radix(s, INT_ENCODE_BASE as u32);
+
+    if micros_since_unix_epoch.is_err() {
+        return Err("timestamp not an int");
+    }
+
+    let micros_since_unix_epoch = micros_since_unix_epoch.unwrap();
+
+    let duration = Duration::new(
+        micros_since_unix_epoch / 1_000_000,
+        1000 * (micros_since_unix_epoch % 1_000_000) as u32,
+    );
+    Ok(UNIX_EPOCH + duration)
+}
+
+fn crate_path(sess: &Session) -> PathBuf {
+    let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();
+
+    let crate_name = find_crate_name(sess, &[]);
+    let crate_types = collect_crate_types(sess, &[]);
+    let stable_crate_id = StableCrateId::new(
+        crate_name,
+        crate_types.contains(&CrateType::Executable),
+        sess.opts.cg.metadata.clone(),
+        sess.cfg_version,
+    );
+
+    let crate_name =
+        format!("{crate_name}-{}", stable_crate_id.as_u64().to_base_fixed_len(CASE_INSENSITIVE));
+    incr_dir.join(crate_name)
+}
+
+fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool {
+    timestamp < SystemTime::now() - Duration::from_secs(10)
+}
+
+/// Runs garbage collection for the current session.
+pub(crate) fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
+    debug!("garbage_collect_session_directories() - begin");
+
+    let session_directory = sess.incr_comp_session_dir();
+    debug!(
+        "garbage_collect_session_directories() - session directory: {}",
+        session_directory.display()
+    );
+
+    let crate_directory = session_directory.parent().unwrap();
+    debug!(
+        "garbage_collect_session_directories() - crate directory: {}",
+        crate_directory.display()
+    );
+
+    // First do a pass over the crate directory, collecting lock files and
+    // session directories
+    let mut session_directories = FxIndexSet::default();
+    let mut lock_files = UnordSet::default();
+
+    for dir_entry in crate_directory.read_dir()? {
+        let Ok(dir_entry) = dir_entry else {
+            // Ignore any errors
+            continue;
+        };
+
+        let entry_name = dir_entry.file_name();
+        let Some(entry_name) = entry_name.to_str() else {
+            continue;
+        };
+
+        if is_session_directory_lock_file(&entry_name) {
+            lock_files.insert(entry_name.to_string());
+        } else if is_session_directory(&entry_name) {
+            session_directories.insert(entry_name.to_string());
+        } else {
+            // This is something we don't know, leave it alone
+        }
+    }
+    session_directories.sort();
+
+    // Now map from lock files to session directories
+    let lock_file_to_session_dir: UnordMap<String, Option<String>> = lock_files
+        .into_items()
+        .map(|lock_file_name| {
+            assert!(lock_file_name.ends_with(LOCK_FILE_EXT));
+            let dir_prefix_end = lock_file_name.len() - LOCK_FILE_EXT.len();
+            let session_dir = {
+                let dir_prefix = &lock_file_name[0..dir_prefix_end];
+                session_directories.iter().find(|dir_name| dir_name.starts_with(dir_prefix))
+            };
+            (lock_file_name, session_dir.map(String::clone))
+        })
+        .into();
+
+    // Delete all lock files, that don't have an associated directory. They must
+    // be some kind of leftover
+    for (lock_file_name, directory_name) in
+        lock_file_to_session_dir.items().into_sorted_stable_ord()
+    {
+        if directory_name.is_none() {
+            let Ok(timestamp) = extract_timestamp_from_session_dir(lock_file_name) else {
+                debug!(
+                    "found lock-file with malformed timestamp: {}",
+                    crate_directory.join(&lock_file_name).display()
+                );
+                // Ignore it
+                continue;
+            };
+
+            let lock_file_path = crate_directory.join(&*lock_file_name);
+
+            if is_old_enough_to_be_collected(timestamp) {
+                debug!(
+                    "garbage_collect_session_directories() - deleting \
+                    garbage lock file: {}",
+                    lock_file_path.display()
+                );
+                delete_session_dir_lock_file(sess, &lock_file_path);
+            } else {
+                debug!(
+                    "garbage_collect_session_directories() - lock file with \
+                    no session dir not old enough to be collected: {}",
+                    lock_file_path.display()
+                );
+            }
+        }
+    }
+
+    // Filter out `None` directories
+    let lock_file_to_session_dir: UnordMap<String, String> = lock_file_to_session_dir
+        .into_items()
+        .filter_map(|(lock_file_name, directory_name)| directory_name.map(|n| (lock_file_name, n)))
+        .into();
+
+    // Delete all session directories that don't have a lock file.
+    for directory_name in session_directories {
+        if !lock_file_to_session_dir.items().any(|(_, dir)| *dir == directory_name) {
+            let path = crate_directory.join(directory_name);
+            if let Err(err) = safe_remove_dir_all(&path) {
+                sess.dcx().emit_warn(errors::InvalidGcFailed { path: &path, err });
+            }
+        }
+    }
+
+    // Now garbage collect the valid session directories.
+    let deletion_candidates =
+        lock_file_to_session_dir.items().filter_map(|(lock_file_name, directory_name)| {
+            debug!("garbage_collect_session_directories() - inspecting: {}", directory_name);
+
+            let Ok(timestamp) = extract_timestamp_from_session_dir(directory_name) else {
+                debug!(
+                    "found session-dir with malformed timestamp: {}",
+                    crate_directory.join(directory_name).display()
+                );
+                // Ignore it
+                return None;
+            };
+
+            if is_finalized(directory_name) {
+                let lock_file_path = crate_directory.join(lock_file_name);
+                match flock::Lock::new(
+                    &lock_file_path,
+                    false, // don't wait
+                    false, // don't create the lock-file
+                    true,
+                ) {
+                    // get an exclusive lock
+                    Ok(lock) => {
+                        debug!(
+                            "garbage_collect_session_directories() - \
+                            successfully acquired lock"
+                        );
+                        debug!(
+                            "garbage_collect_session_directories() - adding \
+                            deletion candidate: {}",
+                            directory_name
+                        );
+
+                        // Note that we are holding on to the lock
+                        return Some((
+                            (timestamp, crate_directory.join(directory_name)),
+                            Some(lock),
+                        ));
+                    }
+                    Err(_) => {
+                        debug!(
+                            "garbage_collect_session_directories() - \
+                            not collecting, still in use"
+                        );
+                    }
+                }
+            } else if is_old_enough_to_be_collected(timestamp) {
+                // When cleaning out "-working" session directories, i.e.
+                // session directories that might still be in use by another
+                // compiler instance, we only look a directories that are
+                // at least ten seconds old. This is supposed to reduce the
+                // chance of deleting a directory in the time window where
+                // the process has allocated the directory but has not yet
+                // acquired the file-lock on it.
+
+                // Try to acquire the directory lock. If we can't, it
+                // means that the owning process is still alive and we
+                // leave this directory alone.
+                let lock_file_path = crate_directory.join(lock_file_name);
+                match flock::Lock::new(
+                    &lock_file_path,
+                    false, // don't wait
+                    false, // don't create the lock-file
+                    true,
+                ) {
+                    // get an exclusive lock
+                    Ok(lock) => {
+                        debug!(
+                            "garbage_collect_session_directories() - \
+                            successfully acquired lock"
+                        );
+
+                        delete_old(sess, &crate_directory.join(directory_name));
+
+                        // Let's make it explicit that the file lock is released at this point,
+                        // or rather, that we held on to it until here
+                        drop(lock);
+                    }
+                    Err(_) => {
+                        debug!(
+                            "garbage_collect_session_directories() - \
+                            not collecting, still in use"
+                        );
+                    }
+                }
+            } else {
+                debug!(
+                    "garbage_collect_session_directories() - not finalized, not \
+                    old enough"
+                );
+            }
+            None
+        });
+    let deletion_candidates = deletion_candidates.into();
+
+    // Delete all but the most recent of the candidates
+    all_except_most_recent(deletion_candidates).into_items().all(|(path, lock)| {
+        debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
+
+        if let Err(err) = safe_remove_dir_all(&path) {
+            sess.dcx().emit_warn(errors::FinalizedGcFailed { path: &path, err });
+        } else {
+            delete_session_dir_lock_file(sess, &lock_file_path(&path));
+        }
+
+        // Let's make it explicit that the file lock is released at this point,
+        // or rather, that we held on to it until here
+        drop(lock);
+        true
+    });
+
+    Ok(())
+}
+
+fn delete_old(sess: &Session, path: &Path) {
+    debug!("garbage_collect_session_directories() - deleting `{}`", path.display());
+
+    if let Err(err) = safe_remove_dir_all(path) {
+        sess.dcx().emit_warn(errors::SessionGcFailed { path: path, err });
+    } else {
+        delete_session_dir_lock_file(sess, &lock_file_path(path));
+    }
+}
+
+fn all_except_most_recent(
+    deletion_candidates: UnordMap<(SystemTime, PathBuf), Option<flock::Lock>>,
+) -> UnordMap<PathBuf, Option<flock::Lock>> {
+    let most_recent = deletion_candidates.items().map(|(&(timestamp, _), _)| timestamp).max();
+
+    if let Some(most_recent) = most_recent {
+        deletion_candidates
+            .into_items()
+            .filter(|&((timestamp, _), _)| timestamp != most_recent)
+            .map(|((_, path), lock)| (path, lock))
+            .collect()
+    } else {
+        UnordMap::default()
+    }
+}
+
+/// Since paths of artifacts within session directories can get quite long, we
+/// need to support deleting files with very long paths. The regular
+/// WinApi functions only support paths up to 260 characters, however. In order
+/// to circumvent this limitation, we canonicalize the path of the directory
+/// before passing it to std::fs::remove_dir_all(). This will convert the path
+/// into the '\\?\' format, which supports much longer paths.
+fn safe_remove_dir_all(p: &Path) -> io::Result<()> {
+    let canonicalized = match try_canonicalize(p) {
+        Ok(canonicalized) => canonicalized,
+        Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
+        Err(err) => return Err(err),
+    };
+
+    std_fs::remove_dir_all(canonicalized)
+}
+
+fn safe_remove_file(p: &Path) -> io::Result<()> {
+    let canonicalized = match try_canonicalize(p) {
+        Ok(canonicalized) => canonicalized,
+        Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
+        Err(err) => return Err(err),
+    };
+
+    match std_fs::remove_file(canonicalized) {
+        Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
+        result => result,
+    }
+}
+
+// On Windows the compiler would sometimes fail to rename the session directory because
+// the OS thought something was still being accessed in it. So we retry a few times to give
+// the OS time to catch up.
+// See https://github.com/rust-lang/rust/issues/86929.
+fn rename_path_with_retry(from: &Path, to: &Path, mut retries_left: usize) -> std::io::Result<()> {
+    loop {
+        match std_fs::rename(from, to) {
+            Ok(()) => return Ok(()),
+            Err(e) => {
+                if retries_left > 0 && e.kind() == ErrorKind::PermissionDenied {
+                    // Try again after a short waiting period.
+                    std::thread::sleep(Duration::from_millis(50));
+                    retries_left -= 1;
+                } else {
+                    return Err(e);
+                }
+            }
+        }
+    }
+}
diff --git a/compiler/rustc_incremental/src/persist/fs/tests.rs b/compiler/rustc_incremental/src/persist/fs/tests.rs
new file mode 100644
index 00000000000..644b8187621
--- /dev/null
+++ b/compiler/rustc_incremental/src/persist/fs/tests.rs
@@ -0,0 +1,77 @@
+use super::*;
+
+#[test]
+fn test_all_except_most_recent() {
+    let input: UnordMap<_, Option<flock::Lock>> = UnordMap::from_iter([
+        ((UNIX_EPOCH + Duration::new(4, 0), PathBuf::from("4")), None),
+        ((UNIX_EPOCH + Duration::new(1, 0), PathBuf::from("1")), None),
+        ((UNIX_EPOCH + Duration::new(5, 0), PathBuf::from("5")), None),
+        ((UNIX_EPOCH + Duration::new(3, 0), PathBuf::from("3")), None),
+        ((UNIX_EPOCH + Duration::new(2, 0), PathBuf::from("2")), None),
+    ]);
+    assert_eq!(
+        all_except_most_recent(input).into_items().map(|(path, _)| path).into_sorted_stable_ord(),
+        vec![PathBuf::from("1"), PathBuf::from("2"), PathBuf::from("3"), PathBuf::from("4")]
+    );
+
+    assert!(all_except_most_recent(UnordMap::default()).is_empty());
+}
+
+#[test]
+fn test_timestamp_serialization() {
+    for i in 0..1_000u64 {
+        let time = UNIX_EPOCH + Duration::new(i * 1_434_578, (i as u32) * 239_000);
+        let s = timestamp_to_string(time);
+        assert_eq!(Ok(time), string_to_timestamp(&s));
+    }
+}
+
+#[test]
+fn test_find_source_directory_in_iter() {
+    let already_visited = FxHashSet::default();
+
+    // Find newest
+    assert_eq!(
+        find_source_directory_in_iter(
+            [
+                PathBuf::from("crate-dir/s-3234-0000-svh"),
+                PathBuf::from("crate-dir/s-2234-0000-svh"),
+                PathBuf::from("crate-dir/s-1234-0000-svh")
+            ]
+            .into_iter(),
+            &already_visited
+        ),
+        Some(PathBuf::from("crate-dir/s-3234-0000-svh"))
+    );
+
+    // Filter out "-working"
+    assert_eq!(
+        find_source_directory_in_iter(
+            [
+                PathBuf::from("crate-dir/s-3234-0000-working"),
+                PathBuf::from("crate-dir/s-2234-0000-svh"),
+                PathBuf::from("crate-dir/s-1234-0000-svh")
+            ]
+            .into_iter(),
+            &already_visited
+        ),
+        Some(PathBuf::from("crate-dir/s-2234-0000-svh"))
+    );
+
+    // Handle empty
+    assert_eq!(find_source_directory_in_iter([].into_iter(), &already_visited), None);
+
+    // Handle only working
+    assert_eq!(
+        find_source_directory_in_iter(
+            [
+                PathBuf::from("crate-dir/s-3234-0000-working"),
+                PathBuf::from("crate-dir/s-2234-0000-working"),
+                PathBuf::from("crate-dir/s-1234-0000-working")
+            ]
+            .into_iter(),
+            &already_visited
+        ),
+        None
+    );
+}
diff --git a/compiler/rustc_incremental/src/persist/load.rs b/compiler/rustc_incremental/src/persist/load.rs
new file mode 100644
index 00000000000..af667a57ce1
--- /dev/null
+++ b/compiler/rustc_incremental/src/persist/load.rs
@@ -0,0 +1,231 @@
+//! Code to load the dep-graph from files.
+
+use crate::errors;
+use rustc_data_structures::memmap::Mmap;
+use rustc_data_structures::unord::UnordMap;
+use rustc_middle::dep_graph::{DepGraph, DepsType, SerializedDepGraph, WorkProductMap};
+use rustc_middle::query::on_disk_cache::OnDiskCache;
+use rustc_serialize::opaque::MemDecoder;
+use rustc_serialize::Decodable;
+use rustc_session::config::IncrementalStateAssertion;
+use rustc_session::Session;
+use rustc_span::ErrorGuaranteed;
+use std::path::{Path, PathBuf};
+use std::sync::Arc;
+use tracing::{debug, warn};
+
+use super::data::*;
+use super::file_format;
+use super::fs::*;
+use super::save::build_dep_graph;
+use super::work_product;
+
+#[derive(Debug)]
+/// Represents the result of an attempt to load incremental compilation data.
+pub enum LoadResult<T> {
+    /// Loading was successful.
+    Ok {
+        #[allow(missing_docs)]
+        data: T,
+    },
+    /// The file either didn't exist or was produced by an incompatible compiler version.
+    DataOutOfDate,
+    /// Loading the dep graph failed.
+    LoadDepGraph(PathBuf, std::io::Error),
+}
+
+impl<T: Default> LoadResult<T> {
+    /// Accesses the data returned in [`LoadResult::Ok`].
+    pub fn open(self, sess: &Session) -> T {
+        // Check for errors when using `-Zassert-incremental-state`
+        match (sess.opts.assert_incr_state, &self) {
+            (Some(IncrementalStateAssertion::NotLoaded), LoadResult::Ok { .. }) => {
+                sess.dcx().emit_fatal(errors::AssertNotLoaded);
+            }
+            (
+                Some(IncrementalStateAssertion::Loaded),
+                LoadResult::LoadDepGraph(..) | LoadResult::DataOutOfDate,
+            ) => {
+                sess.dcx().emit_fatal(errors::AssertLoaded);
+            }
+            _ => {}
+        };
+
+        match self {
+            LoadResult::LoadDepGraph(path, err) => {
+                sess.dcx().emit_warn(errors::LoadDepGraph { path, err });
+                Default::default()
+            }
+            LoadResult::DataOutOfDate => {
+                if let Err(err) = delete_all_session_dir_contents(sess) {
+                    sess.dcx()
+                        .emit_err(errors::DeleteIncompatible { path: dep_graph_path(sess), err });
+                }
+                Default::default()
+            }
+            LoadResult::Ok { data } => data,
+        }
+    }
+}
+
+fn load_data(path: &Path, sess: &Session) -> LoadResult<(Mmap, usize)> {
+    match file_format::read_file(
+        path,
+        sess.opts.unstable_opts.incremental_info,
+        sess.is_nightly_build(),
+        sess.cfg_version,
+    ) {
+        Ok(Some(data_and_pos)) => LoadResult::Ok { data: data_and_pos },
+        Ok(None) => {
+            // The file either didn't exist or was produced by an incompatible
+            // compiler version. Neither is an error.
+            LoadResult::DataOutOfDate
+        }
+        Err(err) => LoadResult::LoadDepGraph(path.to_path_buf(), err),
+    }
+}
+
+fn delete_dirty_work_product(sess: &Session, swp: SerializedWorkProduct) {
+    debug!("delete_dirty_work_product({:?})", swp);
+    work_product::delete_workproduct_files(sess, &swp.work_product);
+}
+
+fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkProductMap)> {
+    let prof = sess.prof.clone();
+
+    if sess.opts.incremental.is_none() {
+        // No incremental compilation.
+        return LoadResult::Ok { data: Default::default() };
+    }
+
+    let _timer = sess.prof.generic_activity("incr_comp_prepare_load_dep_graph");
+
+    // Calling `sess.incr_comp_session_dir()` will panic if `sess.opts.incremental.is_none()`.
+    // Fortunately, we just checked that this isn't the case.
+    let path = dep_graph_path(sess);
+    let expected_hash = sess.opts.dep_tracking_hash(false);
+
+    let mut prev_work_products = UnordMap::default();
+
+    // If we are only building with -Zquery-dep-graph but without an actual
+    // incr. comp. session directory, we skip this. Otherwise we'd fail
+    // when trying to load work products.
+    if sess.incr_comp_session_dir_opt().is_some() {
+        let work_products_path = work_products_path(sess);
+        let load_result = load_data(&work_products_path, sess);
+
+        if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result {
+            // Decode the list of work_products
+            let Ok(mut work_product_decoder) = MemDecoder::new(&work_products_data[..], start_pos)
+            else {
+                sess.dcx().emit_warn(errors::CorruptFile { path: &work_products_path });
+                return LoadResult::DataOutOfDate;
+            };
+            let work_products: Vec<SerializedWorkProduct> =
+                Decodable::decode(&mut work_product_decoder);
+
+            for swp in work_products {
+                let all_files_exist = swp.work_product.saved_files.items().all(|(_, path)| {
+                    let exists = in_incr_comp_dir_sess(sess, path).exists();
+                    if !exists && sess.opts.unstable_opts.incremental_info {
+                        eprintln!("incremental: could not find file for work product: {path}",);
+                    }
+                    exists
+                });
+
+                if all_files_exist {
+                    debug!("reconcile_work_products: all files for {:?} exist", swp);
+                    prev_work_products.insert(swp.id, swp.work_product);
+                } else {
+                    debug!("reconcile_work_products: some file for {:?} does not exist", swp);
+                    delete_dirty_work_product(sess, swp);
+                }
+            }
+        }
+    }
+
+    let _prof_timer = prof.generic_activity("incr_comp_load_dep_graph");
+
+    match load_data(&path, sess) {
+        LoadResult::DataOutOfDate => LoadResult::DataOutOfDate,
+        LoadResult::LoadDepGraph(path, err) => LoadResult::LoadDepGraph(path, err),
+        LoadResult::Ok { data: (bytes, start_pos) } => {
+            let Ok(mut decoder) = MemDecoder::new(&bytes, start_pos) else {
+                sess.dcx().emit_warn(errors::CorruptFile { path: &path });
+                return LoadResult::DataOutOfDate;
+            };
+            let prev_commandline_args_hash = u64::decode(&mut decoder);
+
+            if prev_commandline_args_hash != expected_hash {
+                if sess.opts.unstable_opts.incremental_info {
+                    eprintln!(
+                        "[incremental] completely ignoring cache because of \
+                                    differing commandline arguments"
+                    );
+                }
+                // We can't reuse the cache, purge it.
+                debug!("load_dep_graph_new: differing commandline arg hashes");
+
+                // No need to do any further work
+                return LoadResult::DataOutOfDate;
+            }
+
+            let dep_graph = SerializedDepGraph::decode::<DepsType>(&mut decoder);
+
+            LoadResult::Ok { data: (dep_graph, prev_work_products) }
+        }
+    }
+}
+
+/// Attempts to load the query result cache from disk
+///
+/// If we are not in incremental compilation mode, returns `None`.
+/// Otherwise, tries to load the query result cache from disk,
+/// creating an empty cache if it could not be loaded.
+pub fn load_query_result_cache(sess: &Session) -> Option<OnDiskCache<'_>> {
+    if sess.opts.incremental.is_none() {
+        return None;
+    }
+
+    let _prof_timer = sess.prof.generic_activity("incr_comp_load_query_result_cache");
+
+    let path = query_cache_path(sess);
+    match load_data(&path, sess) {
+        LoadResult::Ok { data: (bytes, start_pos) } => {
+            let cache = OnDiskCache::new(sess, bytes, start_pos).unwrap_or_else(|()| {
+                sess.dcx().emit_warn(errors::CorruptFile { path: &path });
+                OnDiskCache::new_empty(sess.source_map())
+            });
+            Some(cache)
+        }
+        _ => Some(OnDiskCache::new_empty(sess.source_map())),
+    }
+}
+
+/// Setups the dependency graph by loading an existing graph from disk and set up streaming of a
+/// new graph to an incremental session directory.
+pub fn setup_dep_graph(sess: &Session) -> Result<DepGraph, ErrorGuaranteed> {
+    // `load_dep_graph` can only be called after `prepare_session_directory`.
+    prepare_session_directory(sess)?;
+
+    let res = sess.opts.build_dep_graph().then(|| load_dep_graph(sess));
+
+    if sess.opts.incremental.is_some() {
+        sess.time("incr_comp_garbage_collect_session_directories", || {
+            if let Err(e) = garbage_collect_session_directories(sess) {
+                warn!(
+                    "Error while trying to garbage collect incremental \
+                     compilation cache directory: {}",
+                    e
+                );
+            }
+        });
+    }
+
+    Ok(res
+        .and_then(|result| {
+            let (prev_graph, prev_work_products) = result.open(sess);
+            build_dep_graph(sess, prev_graph, prev_work_products)
+        })
+        .unwrap_or_else(DepGraph::new_disabled))
+}
diff --git a/compiler/rustc_incremental/src/persist/mod.rs b/compiler/rustc_incremental/src/persist/mod.rs
new file mode 100644
index 00000000000..94c05f4a2c8
--- /dev/null
+++ b/compiler/rustc_incremental/src/persist/mod.rs
@@ -0,0 +1,21 @@
+//! When in incremental mode, this pass dumps out the dependency graph
+//! into the given directory. At the same time, it also hashes the
+//! various HIR nodes.
+
+mod data;
+mod dirty_clean;
+mod file_format;
+mod fs;
+mod load;
+mod save;
+mod work_product;
+
+pub use fs::finalize_session_directory;
+pub use fs::in_incr_comp_dir;
+pub use fs::in_incr_comp_dir_sess;
+pub use load::load_query_result_cache;
+pub use load::setup_dep_graph;
+pub use load::LoadResult;
+pub use save::save_dep_graph;
+pub use save::save_work_product_index;
+pub use work_product::copy_cgu_workproduct_to_incr_comp_cache_dir;
diff --git a/compiler/rustc_incremental/src/persist/save.rs b/compiler/rustc_incremental/src/persist/save.rs
new file mode 100644
index 00000000000..3bf582bd26c
--- /dev/null
+++ b/compiler/rustc_incremental/src/persist/save.rs
@@ -0,0 +1,184 @@
+use crate::assert_dep_graph::assert_dep_graph;
+use crate::errors;
+use rustc_data_structures::fx::FxIndexMap;
+use rustc_data_structures::sync::join;
+use rustc_middle::dep_graph::{
+    DepGraph, SerializedDepGraph, WorkProduct, WorkProductId, WorkProductMap,
+};
+use rustc_middle::ty::TyCtxt;
+use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
+use rustc_serialize::Encodable as RustcEncodable;
+use rustc_session::Session;
+use std::fs;
+use std::sync::Arc;
+use tracing::debug;
+
+use super::data::*;
+use super::dirty_clean;
+use super::file_format;
+use super::fs::*;
+use super::work_product;
+
+/// Saves and writes the [`DepGraph`] to the file system.
+///
+/// This function saves both the dep-graph and the query result cache,
+/// and drops the result cache.
+///
+/// This function should only run after all queries have completed.
+/// Trying to execute a query afterwards would attempt to read the result cache we just dropped.
+pub fn save_dep_graph(tcx: TyCtxt<'_>) {
+    debug!("save_dep_graph()");
+    tcx.dep_graph.with_ignore(|| {
+        let sess = tcx.sess;
+        if sess.opts.incremental.is_none() {
+            return;
+        }
+        // This is going to be deleted in finalize_session_directory, so let's not create it.
+        if sess.dcx().has_errors_or_delayed_bugs().is_some() {
+            return;
+        }
+
+        let query_cache_path = query_cache_path(sess);
+        let dep_graph_path = dep_graph_path(sess);
+        let staging_dep_graph_path = staging_dep_graph_path(sess);
+
+        sess.time("assert_dep_graph", || assert_dep_graph(tcx));
+        sess.time("check_dirty_clean", || dirty_clean::check_dirty_clean_annotations(tcx));
+
+        if sess.opts.unstable_opts.incremental_info {
+            tcx.dep_graph.print_incremental_info()
+        }
+
+        join(
+            move || {
+                sess.time("incr_comp_persist_dep_graph", || {
+                    if let Err(err) = fs::rename(&staging_dep_graph_path, &dep_graph_path) {
+                        sess.dcx().emit_err(errors::MoveDepGraph {
+                            from: &staging_dep_graph_path,
+                            to: &dep_graph_path,
+                            err,
+                        });
+                    }
+                });
+            },
+            move || {
+                // We execute this after `incr_comp_persist_dep_graph` for the serial compiler
+                // to catch any potential query execution writing to the dep graph.
+                sess.time("incr_comp_persist_result_cache", || {
+                    // Drop the memory map so that we can remove the file and write to it.
+                    if let Some(odc) = &tcx.query_system.on_disk_cache {
+                        odc.drop_serialized_data(tcx);
+                    }
+
+                    file_format::save_in(sess, query_cache_path, "query cache", |e| {
+                        encode_query_cache(tcx, e)
+                    });
+                });
+            },
+        );
+    })
+}
+
+/// Saves the work product index.
+pub fn save_work_product_index(
+    sess: &Session,
+    dep_graph: &DepGraph,
+    new_work_products: FxIndexMap<WorkProductId, WorkProduct>,
+) {
+    if sess.opts.incremental.is_none() {
+        return;
+    }
+    // This is going to be deleted in finalize_session_directory, so let's not create it
+    if sess.dcx().has_errors().is_some() {
+        return;
+    }
+
+    debug!("save_work_product_index()");
+    dep_graph.assert_ignored();
+    let path = work_products_path(sess);
+    file_format::save_in(sess, path, "work product index", |mut e| {
+        encode_work_product_index(&new_work_products, &mut e);
+        e.finish()
+    });
+
+    // We also need to clean out old work-products, as not all of them are
+    // deleted during invalidation. Some object files don't change their
+    // content, they are just not needed anymore.
+    let previous_work_products = dep_graph.previous_work_products();
+    for (id, wp) in previous_work_products.to_sorted_stable_ord() {
+        if !new_work_products.contains_key(id) {
+            work_product::delete_workproduct_files(sess, wp);
+            debug_assert!(
+                !wp.saved_files.items().all(|(_, path)| in_incr_comp_dir_sess(sess, path).exists())
+            );
+        }
+    }
+
+    // Check that we did not delete one of the current work-products:
+    debug_assert!({
+        new_work_products.iter().all(|(_, wp)| {
+            wp.saved_files.items().all(|(_, path)| in_incr_comp_dir_sess(sess, path).exists())
+        })
+    });
+}
+
+fn encode_work_product_index(
+    work_products: &FxIndexMap<WorkProductId, WorkProduct>,
+    encoder: &mut FileEncoder,
+) {
+    let serialized_products: Vec<_> = work_products
+        .iter()
+        .map(|(id, work_product)| SerializedWorkProduct {
+            id: *id,
+            work_product: work_product.clone(),
+        })
+        .collect();
+
+    serialized_products.encode(encoder)
+}
+
+fn encode_query_cache(tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult {
+    tcx.sess.time("incr_comp_serialize_result_cache", || tcx.serialize_query_result_cache(encoder))
+}
+
+/// Builds the dependency graph.
+///
+/// This function creates the *staging dep-graph*. When the dep-graph is modified by a query
+/// execution, the new dependency information is not kept in memory but directly
+/// output to this file. `save_dep_graph` then finalizes the staging dep-graph
+/// and moves it to the permanent dep-graph path
+pub(crate) fn build_dep_graph(
+    sess: &Session,
+    prev_graph: Arc<SerializedDepGraph>,
+    prev_work_products: WorkProductMap,
+) -> Option<DepGraph> {
+    if sess.opts.incremental.is_none() {
+        // No incremental compilation.
+        return None;
+    }
+
+    // Stream the dep-graph to an alternate file, to avoid overwriting anything in case of errors.
+    let path_buf = staging_dep_graph_path(sess);
+
+    let mut encoder = match FileEncoder::new(&path_buf) {
+        Ok(encoder) => encoder,
+        Err(err) => {
+            sess.dcx().emit_err(errors::CreateDepGraph { path: &path_buf, err });
+            return None;
+        }
+    };
+
+    file_format::write_file_header(&mut encoder, sess);
+
+    // First encode the commandline arguments hash
+    sess.opts.dep_tracking_hash(false).encode(&mut encoder);
+
+    Some(DepGraph::new(
+        &sess.prof,
+        prev_graph,
+        prev_work_products,
+        encoder,
+        sess.opts.unstable_opts.query_dep_graph,
+        sess.opts.unstable_opts.incremental_info,
+    ))
+}
diff --git a/compiler/rustc_incremental/src/persist/work_product.rs b/compiler/rustc_incremental/src/persist/work_product.rs
new file mode 100644
index 00000000000..e230da9dfb1
--- /dev/null
+++ b/compiler/rustc_incremental/src/persist/work_product.rs
@@ -0,0 +1,57 @@
+//! Functions for saving and removing intermediate [work products].
+//!
+//! [work products]: WorkProduct
+
+use crate::errors;
+use crate::persist::fs::*;
+use rustc_data_structures::unord::UnordMap;
+use rustc_fs_util::link_or_copy;
+use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
+use rustc_session::Session;
+use std::fs as std_fs;
+use std::path::Path;
+use tracing::debug;
+
+/// Copies a CGU work product to the incremental compilation directory, so next compilation can
+/// find and reuse it.
+pub fn copy_cgu_workproduct_to_incr_comp_cache_dir(
+    sess: &Session,
+    cgu_name: &str,
+    files: &[(&'static str, &Path)],
+) -> Option<(WorkProductId, WorkProduct)> {
+    debug!(?cgu_name, ?files);
+    sess.opts.incremental.as_ref()?;
+
+    let mut saved_files = UnordMap::default();
+    for (ext, path) in files {
+        let file_name = format!("{cgu_name}.{ext}");
+        let path_in_incr_dir = in_incr_comp_dir_sess(sess, &file_name);
+        match link_or_copy(path, &path_in_incr_dir) {
+            Ok(_) => {
+                let _ = saved_files.insert(ext.to_string(), file_name);
+            }
+            Err(err) => {
+                sess.dcx().emit_warn(errors::CopyWorkProductToCache {
+                    from: path,
+                    to: &path_in_incr_dir,
+                    err,
+                });
+            }
+        }
+    }
+
+    let work_product = WorkProduct { cgu_name: cgu_name.to_string(), saved_files };
+    debug!(?work_product);
+    let work_product_id = WorkProductId::from_cgu_name(cgu_name);
+    Some((work_product_id, work_product))
+}
+
+/// Removes files for a given work product.
+pub(crate) fn delete_workproduct_files(sess: &Session, work_product: &WorkProduct) {
+    for (_, path) in work_product.saved_files.items().into_sorted_stable_ord() {
+        let path = in_incr_comp_dir_sess(sess, path);
+        if let Err(err) = std_fs::remove_file(&path) {
+            sess.dcx().emit_warn(errors::DeleteWorkProduct { path: &path, err });
+        }
+    }
+}