Skip to content

Clean up query-forcing functions#154236

Open
Zalathar wants to merge 4 commits intorust-lang:mainfrom
Zalathar:force-query
Open

Clean up query-forcing functions#154236
Zalathar wants to merge 4 commits intorust-lang:mainfrom
Zalathar:force-query

Conversation

@Zalathar
Copy link
Member

@Zalathar Zalathar commented Mar 23, 2026

This PR takes the force_query function, inlines it into its only caller force_from_dep_node_inner, and renames the resulting function to force_query_dep_node. Combining the functions became possible after the removal of rustc_query_system, because they are now in the same crate.

There are two other notable cleanups along the way:

  • Removing an unhelpful assertion and its verbose comment
    • The removed comment was originally added in 0454a41, but is too verbose to be worth preserving inline.
  • Removing a redundant cache lookup while forcing, as it is useless in the serial compiler and unnecessary (and probably unhelpful) in the parallel compiler

r? nnethercote

@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 23, 2026
@Zalathar
Copy link
Member Author

I'm curious to see whether removing the cache lookup has any measurable perf effect.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 23, 2026
Clean up query-forcing functions
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 23, 2026
Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok. A couple of comments. I'm a bit unsure about the second commit so I'll wait to see if @Zoxc or @zetanumbers has anything to say about it.

View changes since this review

// lead to having to re-execute compile_codegen_unit, possibly unnecessarily.
//
// For more background, see the comment in `plumbing.rs` at
// <https://github.com/rust-lang/rust/commit/0454a41>.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels weird to refer to a deleted comment. Seems like either this reference should be removed, or part/all of the deleted comment should be replicated here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linked comment is basically a longer and more detailed version of the comment that's just above these changed lines.

The main reason I wanted to add a link here is that someone looking at these codegen_unit calls could reasonably want more context on why they were added, and the actual history is rather tricky to track down due to a number of intermediate moves. But at the same time I think it would be too disruptive to add much more inline context here.

Perhaps I could try to find a more agreeable way to do this.

),
promote_from_disk_fn: (can_recover && is_cache_on_disk)
.then_some(promote_from_disk_inner::<Q>),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the second Q::query_vtable call be hoisted into a closure here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it could be.

Note that this is effectively me changing my mind on 4284edc. At that time I thought GetQueryVTable might be useful for other kinds of static lookup in the future, but after various other simplifications and cleanups we seem to be doing fine with just using QueryVTable almost everywhere.

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 23, 2026

☀️ Try build successful (CI)
Build commit: 00d810c (00d810cf78c57daea23ccd90a17ab8627f76570b, parent: 562dee4820c458d823175268e41601d4c060588a)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (00d810c): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.3%] 3
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary 2.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.3% [2.3%, 2.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results (secondary -2.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-2.7%, -2.3%] 2
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 485.484s -> 483.408s (-0.43%)
Artifact size: 394.88 MiB -> 394.97 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 23, 2026
@Zalathar
Copy link
Member Author

Perf results look like pure noise. No need to avoid rollup.
@bors rollup=maybe

This assertion and its comment are much visually heavier than the part of the
function that actually performs important work.

This assertion is also useless, because the `codegen_unit` query does not have
a `force_from_dep_node_fn` in its DepKindVTable, so the assertion would never
be reached even in the situation it is trying to guard against.

This assertion is also redundant, because we have plenty of incremental tests
that fail if the corresponding calls to `tcx.ensure_ok().codegen_unit(..)` are
removed.
In the serial compiler, we should never be trying to force a query node that
has a cached value, because we would have already marked the node red or green
when putting its value in cache.

In the parallel compiler, we immediately re-check the cache while holding the
state shard lock anyway, and trying to avoid that lock doesn't seem worthwhile.
The different parts of this function used to be split across different crates,
but nowadays they are both in `rustc_query_impl`, so they can be combined.

This commit also hoists the vtable lookup to a closure in
`make_dep_kind_vtable_for_query`, to reduce the amount of code that deals with
`GetQueryVTable`, and to make the inner function more consistent with other
functions in `execution`.
This effectively reverses <rust-lang@4284edc>.

At that time, I thought GetQueryVTable might be useful for other kinds of
static lookup in the future, but after various other simplifications and
cleanups that now seems less likely, and this style is more consistent with
other vtable-related functions.
@zetanumbers
Copy link
Contributor

This is fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants