Skip to content

Reduce some function calls in the query system #108134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ impl QueryContext for QueryCtxt<'_> {
};

// Use the `ImplicitCtxt` while we execute the query.
tls::enter_context(&new_icx, || {
rustc_data_structures::stack::ensure_sufficient_stack(compute)
})
tls::enter_context(&new_icx, compute)
})
}

Expand Down
31 changes: 12 additions & 19 deletions compiler/rustc_query_system/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::profiling::TimingGuard;
#[cfg(parallel_compiler)]
use rustc_data_structures::sharded::Sharded;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::sync::Lock;
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, FatalError};
use rustc_session::Session;
Expand Down Expand Up @@ -188,12 +189,12 @@ where
#[cfg(not(parallel_compiler))]
let mut state_lock = state.active.lock();
let lock = &mut *state_lock;
let current_job_id = qcx.current_query_job();

match lock.entry(key) {
Entry::Vacant(entry) => {
let id = qcx.next_job_id();
let job = qcx.current_query_job();
let job = QueryJob::new(id, span, job);
let job = QueryJob::new(id, span, current_job_id);

let key = *entry.key();
entry.insert(QueryResult::Started(job));
Expand All @@ -212,7 +213,7 @@ where
// so we just return the error.
return TryGetJob::Cycle(id.find_cycle_in_stack(
qcx.try_collect_active_jobs().unwrap(),
&qcx.current_query_job(),
&current_job_id,
span,
));
}
Expand All @@ -230,7 +231,7 @@ where

// With parallel queries we might just have to wait on some other
// thread.
let result = latch.wait_on(qcx.current_query_job(), span);
let result = latch.wait_on(current_job_id, span);

match result {
Ok(()) => TryGetJob::JobCompleted(query_blocked_prof_timer),
Expand Down Expand Up @@ -348,8 +349,6 @@ where

fn try_execute_query<Q, Qcx>(
qcx: Qcx,
state: &QueryState<Q::Key, Qcx::DepKind>,
cache: &Q::Cache,
span: Span,
key: Q::Key,
dep_node: Option<DepNode<Qcx::DepKind>>,
Expand All @@ -358,9 +357,11 @@ where
Q: QueryConfig<Qcx>,
Qcx: QueryContext,
{
let state = Q::query_state(qcx);
match JobOwner::<'_, Q::Key, Qcx::DepKind>::try_start(&qcx, state, span, key) {
TryGetJob::NotYetStarted(job) => {
let (result, dep_node_index) = execute_job::<Q, Qcx>(qcx, key, dep_node, job.id);
let cache = Q::query_cache(qcx);
if Q::FEEDABLE {
// We should not compute queries that also got a value via feeding.
// This can't happen, as query feeding adds the very dependencies to the fed query
Expand All @@ -381,7 +382,7 @@ where
}
#[cfg(parallel_compiler)]
TryGetJob::JobCompleted(query_blocked_prof_timer) => {
let Some((v, index)) = cache.lookup(&key) else {
let Some((v, index)) = Q::query_cache(qcx).lookup(&key) else {
panic!("value must be in cache after waiting")
};

Expand Down Expand Up @@ -739,14 +740,8 @@ where
None
};

let (result, dep_node_index) = try_execute_query::<Q, Qcx>(
qcx,
Q::query_state(qcx),
Q::query_cache(qcx),
span,
key,
dep_node,
);
let (result, dep_node_index) =
ensure_sufficient_stack(|| try_execute_query::<Q, Qcx>(qcx, span, key, dep_node));
if let Some(dep_node_index) = dep_node_index {
qcx.dep_context().dep_graph().read_index(dep_node_index)
}
Expand All @@ -762,14 +757,12 @@ where
{
// We may be concurrently trying both execute and force a query.
// Ensure that only one of them runs the query.
let cache = Q::query_cache(qcx);
if let Some((_, index)) = cache.lookup(&key) {
if let Some((_, index)) = Q::query_cache(qcx).lookup(&key) {
qcx.dep_context().profiler().query_cache_hit(index.into());
return;
}

let state = Q::query_state(qcx);
debug_assert!(!Q::ANON);

try_execute_query::<Q, _>(qcx, state, cache, DUMMY_SP, key, Some(dep_node));
ensure_sufficient_stack(|| try_execute_query::<Q, _>(qcx, DUMMY_SP, key, Some(dep_node)));
}