]> git.lizzy.rs Git - rust.git/commitdiff
Make all query forcing go through try_execute_query.
authorCamille GILLOT <gillot.camille@gmail.com>
Mon, 2 Nov 2020 20:27:36 +0000 (21:27 +0100)
committerCamille GILLOT <gillot.camille@gmail.com>
Sun, 22 Aug 2021 18:23:29 +0000 (20:23 +0200)
try_execute_query is now able to centralize the path for query
get/ensure/force.

try_execute_query now takes the dep_node as a parameter, so it can
accommodate `force`. This dep_node is an Option to avoid computing it in
the `get` fast path.

try_execute_query now returns both the result and the dep_node_index to
allow the caller to handle the dep graph.

The caller is responsible for marking the dependency.

compiler/rustc_query_system/src/query/plumbing.rs

index 9f89f6f530910ec5ce26ba1fd79f921b6fe0aeb1..82392dee165444604b6ede96d57417929c6f0ba4 100644 (file)
@@ -427,12 +427,13 @@ fn try_execute_query<CTX, C>(
     span: Span,
     key: C::Key,
     lookup: QueryLookup,
+    dep_node: Option<DepNode<CTX::DepKind>>,
     query: &QueryVtable<CTX, C::Key, C::Value>,
     compute: fn(CTX::DepContext, C::Key) -> C::Value,
-) -> C::Stored
+) -> (C::Stored, Option<DepNodeIndex>)
 where
     C: QueryCache,
-    C::Key: DepNodeParams<CTX::DepContext>,
+    C::Key: Clone + DepNodeParams<CTX::DepContext>,
     CTX: QueryContext,
 {
     let job = match JobOwner::<'_, CTX::DepKind, C>::try_start(
@@ -445,11 +446,10 @@ fn try_execute_query<CTX, C>(
         query,
     ) {
         TryGetJob::NotYetStarted(job) => job,
-        TryGetJob::Cycle(result) => return result,
+        TryGetJob::Cycle(result) => return (result, None),
         #[cfg(parallel_compiler)]
         TryGetJob::JobCompleted((v, index)) => {
-            tcx.dep_context().dep_graph().read_index(index);
-            return v;
+            return (v, Some(index));
         }
     };
 
@@ -461,10 +461,11 @@ fn try_execute_query<CTX, C>(
         let result = tcx.start_query(job.id, None, || compute(*tcx.dep_context(), key));
         let dep_node_index = dep_graph.next_virtual_depnode_index();
         prof_timer.finish_with_query_invocation_id(dep_node_index.into());
-        return job.complete(result, dep_node_index);
+        let result = job.complete(result, dep_node_index);
+        return (result, None);
     }
 
-    if query.anon {
+    let (result, dep_node_index) = if query.anon {
         let prof_timer = tcx.dep_context().profiler().query_provider();
 
         let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
@@ -477,20 +478,21 @@ fn try_execute_query<CTX, C>(
 
         prof_timer.finish_with_query_invocation_id(dep_node_index.into());
 
-        dep_graph.read_index(dep_node_index);
-
         let side_effects = QuerySideEffects { diagnostics };
 
         if unlikely!(!side_effects.is_empty()) {
             tcx.store_side_effects_for_anon_node(dep_node_index, side_effects);
         }
 
-        return job.complete(result, dep_node_index);
-    }
-
-    let dep_node = query.to_dep_node(*tcx.dep_context(), &key);
-
-    if !query.eval_always {
+        let result = job.complete(result, dep_node_index);
+        (result, dep_node_index)
+    } else if query.eval_always {
+        // `to_dep_node` is expensive for some `DepKind`s.
+        let dep_node = dep_node.unwrap_or_else(|| query.to_dep_node(*tcx.dep_context(), &key));
+        force_query_with_job(tcx, key, job, dep_node, query, compute)
+    } else {
+        // `to_dep_node` is expensive for some `DepKind`s.
+        let dep_node = dep_node.unwrap_or_else(|| query.to_dep_node(*tcx.dep_context(), &key));
         // The diagnostics for this query will be
         // promoted to the current session during
         // `try_mark_green()`, so we can ignore them here.
@@ -498,13 +500,13 @@ fn try_execute_query<CTX, C>(
             try_load_from_disk_and_cache_in_memory(tcx, &key, &dep_node, query, compute)
         });
         if let Some((result, dep_node_index)) = loaded {
-            return job.complete(result, dep_node_index);
+            let result = job.complete(result, dep_node_index);
+            (result, dep_node_index)
+        } else {
+            force_query_with_job(tcx, key, job, dep_node, query, compute)
         }
-    }
-
-    let (result, dep_node_index) = force_query_with_job(tcx, key, job, dep_node, query, compute);
-    dep_graph.read_index(dep_node_index);
-    result
+    };
+    (result, Some(dep_node_index))
 }
 
 fn try_load_from_disk_and_cache_in_memory<CTX, K, V>(
@@ -524,7 +526,6 @@ fn try_load_from_disk_and_cache_in_memory<CTX, K, V>(
 
     let (prev_dep_node_index, dep_node_index) =
         tcx.dep_context().dep_graph().try_mark_green(tcx, &dep_node)?;
-    tcx.dep_context().dep_graph().read_index(dep_node_index);
 
     debug_assert!(tcx.dep_context().dep_graph().is_green(dep_node));
 
@@ -700,7 +701,12 @@ fn get_query_impl<CTX, C>(
     C: QueryCache,
     C::Key: DepNodeParams<CTX::DepContext>,
 {
-    try_execute_query(tcx, state, cache, span, key, lookup, query, compute)
+    let (result, dep_node_index) =
+        try_execute_query(tcx, state, cache, span, key, lookup, None, query, compute);
+    if let Some(dep_node_index) = dep_node_index {
+        tcx.dep_context().dep_graph().read_index(dep_node_index)
+    }
+    result
 }
 
 /// Ensure that either this query has all green inputs or been executed.
@@ -779,23 +785,8 @@ fn force_query_impl<CTX, C>(
         Err(lookup) => lookup,
     };
 
-    let job = match JobOwner::<'_, CTX::DepKind, C>::try_start(
-        tcx,
-        state,
-        cache,
-        DUMMY_SP,
-        key.clone(),
-        lookup,
-        query,
-    ) {
-        TryGetJob::NotYetStarted(job) => job,
-        TryGetJob::Cycle(_) => return true,
-        #[cfg(parallel_compiler)]
-        TryGetJob::JobCompleted(_) => return true,
-    };
-
-    force_query_with_job(tcx, key, job, dep_node, query, compute);
-
+    let _ =
+        try_execute_query(tcx, state, cache, DUMMY_SP, key, lookup, Some(dep_node), query, compute);
     true
 }