Skip to content

Conversation

@zetanumbers
Copy link
Contributor

@zetanumbers zetanumbers commented Dec 19, 2025

Fixes #150018
Fixes #142778
Fixes #141540

This PR allows parallel front-end's green-red node coloring processes in the query dep graph to race. Recall that whenever one thread tries to mark query node green and then fails to reconstruct a subquery's key to force it, parent query is tried to be forced or executed too. The subquery could be marked green by the thread during execution the parent query because key is now present. Other thread during this process is able to mark green parent query itself. And so after some time the first thread finishes the execution and finds in its place a green node. This is bad for no_hash queries since those are conservatively presumed to be red but actually queries must be deterministic anyway and so no_hash nodes could be colored green if its deps are green too after its execution finishes. As such we can consider green color to be "stronger" that the red color and thus former would replace the latter.

TODO: update rustc-dev-guide query docs and rustc comments on no_hash modifier

I have left some refactor-ish changes I did during my dirty debugging. Let me know if you would want those to be redone or removed.

Steps to reproduce

Quote from #150018, more details there:

Do a clean build x build library --stage 1 at 52fda2e, then git checkout to ba45f0b and without cleaning try rebuilding it the same way again. And don't forget to set RUSTFLAGS_BOOTSTRAP to -Zthreads=n where n > 1.

@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 Dec 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 19, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@petrochenkov
Copy link
Contributor

cc @Zoxc @cjgillot @nnethercote

@zetanumbers
Copy link
Contributor Author

zetanumbers commented Dec 19, 2025

My first idea was to add a special "locked" color, but I wasn't sure what was even happening in there because coloring races currently are considered to be normal anyway.

@matthiaskrgr
Copy link
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Dec 19, 2025
@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 19, 2025
@zetanumbers
Copy link
Contributor Author

zetanumbers commented Dec 19, 2025

I ran rustc incremental builds as described in reproduction steps section for several hours with -Zthreads, debug=true and -Zincremental_verify_ich and it's looking good.

@rust-bors
Copy link

rust-bors bot commented Dec 19, 2025

☀️ Try build successful (CI)
Build commit: bb5a19e (bb5a19e1ef3ed803b9a2acd98a3b223334df5063, parent: 0f0d850e9146f53b16fb1c6a56ed2f821962ff94)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (bb5a19e): comparison URL.

Overall result: ❌ regressions - please read the text below

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.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@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)
2.3% [0.3%, 6.6%] 117
Regressions ❌
(secondary)
1.7% [0.2%, 4.9%] 75
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.3% [0.3%, 6.6%] 117

Max RSS (memory usage)

Results (primary -3.5%, secondary -1.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)
-3.5% [-3.5%, -3.5%] 1
Improvements ✅
(secondary)
-1.5% [-2.2%, -0.8%] 2
All ❌✅ (primary) -3.5% [-3.5%, -3.5%] 1

Cycles

Results (primary 3.0%, secondary 3.4%)

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

mean range count
Regressions ❌
(primary)
3.0% [2.9%, 3.1%] 2
Regressions ❌
(secondary)
3.4% [2.3%, 4.9%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.0% [2.9%, 3.1%] 2

Binary size

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

Bootstrap: 479.055s -> 481.828s (0.58%)
Artifact size: 390.50 MiB -> 390.50 MiB (0.00%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Dec 19, 2025
pub(super) enum DepNodeColor {
Green(DepNodeIndex),
Red,
Red(() /* DepNodeIndex */),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Red(() /* DepNodeIndex */),
Red,

Probably left from some debugging.

}

impl DepNodeColor {
pub(super) fn as_green(self) -> Option<DepNodeIndex> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub(super) fn as_green(self) -> Option<DepNodeIndex> {
pub(super) fn expect_green(self) -> DepNodeIndex {

The result is always unwrapped.


return dep_node_index;
}
debug_assert_eq!(DepNodeColor::Unknown, color);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
debug_assert_eq!(DepNodeColor::Unknown, color);
debug_assert_eq!(color, DepNodeColor::Unknown);

(Sorry.)

// session, its query was re-executed, but it doesn't compute a result hash
// (i.e. it represents a `no_hash` query), so we have no way of determining
// whether or not the result was the same as before.
if !feed && !cx.is_eval_always(key.kind) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Need a comment explaining what's going on here.

values: IndexVec<SerializedDepNodeIndex, AtomicU32>,
}

// All values below `COMPRESSED_RED` are green.
Copy link
Contributor

Choose a reason for hiding this comment

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

Outdated comment.


Err(DepNodeIndex::from_u32(green))
}
Err(green) => Err(DepNodeIndex::from_u32(green)),
Copy link
Contributor

Choose a reason for hiding this comment

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

Need comments explaining when all of the cases above may happen, and what we are asserting.

Err(red) if red & COMPRESSED_RED_BIT != 0 => {
panic!("trying to encode a dep node twice for index {prev_index:?}")
}
Err(green) => Err(DepNodeIndex::from_u32(green)),
Copy link
Contributor

Choose a reason for hiding this comment

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

Need comments explaining when all of the cases above may happen, and what we are asserting.

// previous session and has been marked green
for prev_index in data.colors.values.indices() {
if data.colors.current(prev_index) == Some(dep_node_index) {
if let DepNodeColor::Green(c) = data.colors.current(prev_index)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if let DepNodeColor::Green(c) = data.colors.current(prev_index)
if data.colors.current(prev_index) == DepNodeColor::Green(dep_node_index) {

Or remove several derived impls on DepNodeColor as unnecessary.


if is_green {
// Use `try_mark_green` to avoid racing when `send_promoted` is called concurrently
// on the same index.
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to keep a comment similar to this one for both green and red cases.

assert_eq!(index & COMPRESSED_RED_BIT, 0);
let green = index;

match self.values[prev_index].compare_exchange(COMPRESSED_UNKNOWN, green, Release, Acquire)
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps just use SeqCst everywhere until the parallel frontend is stabilized?

Or add comments explaining the orderings, although I'm not sure who exactly they will be supposed to convince.

@petrochenkov
Copy link
Contributor

As usual, it would be nice to split the work into a few parts (especially given the perf regressions):

  • Preparatory changes and refactorings (passing the frame and feed around, the named iterator change, the COMPRESSED_RED_BIT change, and few others)
  • The assert hardening changes
  • The substantial changes modifying the logic in try_mark and alloc_and_color_node

But it may be better to wait for a high level design review from Zoxc or cjgillot before doing that.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 19, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

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) perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

5 participants