Skip to content

uniquify root goals during HIR typeck #144405

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lcnr
Copy link
Contributor

@lcnr lcnr commented Jul 24, 2025

We need to rely on region identity to deal with hangs such as rust-lang/trait-system-refactor-initiative#210 and to keep the current behavior of fn try_merge_responses.

This is a problem as borrowck starts by replacing each occurrence of a region with a unique inference variable. This frequently splits a single region during HIR typeck into multiple distinct regions. As we assume goals to always succeed during borrowck, relying on two occurances of a region being identical during HIR typeck causes ICE. See the now fixed examples in rust-lang/trait-system-refactor-initiative#27 and #139409.

We've previously tried to avoid this issue by always uniquifying regions when canonicalizing goals. This prevents caching subtrees during canonicalization which resulted in hangs for very large types. People rely on such types in practice, which caused us to revert our attempt to reinstate #[type_length_limit] in #127670. The complete list of changes here:

After more consideration, all occurrences of such large types need to happen outside of typeck/borrowck. We know this as we already walk over all types in the MIR body when replacing their regions with nll vars.

This PR therefore enables us to rely on region identity inside of the trait solver by exclusively uniquifying root goals during HIR typeck. These are the only goals we assume to hold during borrowck. This is insufficient as type inference variables may "hide" regions we later uniquify. Because of this, we now stash proven goals which depend on inference variables in HIR typeck and reprove them after writeback. This closes rust-lang/trait-system-refactor-initiative#127.

This was originally part of #144258 but I've moved it into a separate PR. While I believe we need to rely on region identity to fix the performance issues in some way, I don't know whether #144258 is the best approach to actually do so. Regardless of how we deal with the hangs however, this change is necessary and desirable regardless.

r? @compiler-errors or @BoxyUwU

@rustbot
Copy link
Collaborator

rustbot commented Jul 24, 2025

compiler-errors is not on the review rotation at the moment.
They may take a while to respond.

@rustbot rustbot added 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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jul 24, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 24, 2025

This PR changes a file inside tests/crashes. If a crash was fixed, please move into the corresponding ui subdir and add 'Fixes #' to the PR description to autoclose the issue upon merge.

Comment on lines +24 to +27
/// When canonicalizing the `param_env`, we keep `'static` as merging
/// trait candidates relies on it when deciding whether a where-bound
/// is trivial.
ParamEnv,
Copy link
Member

Choose a reason for hiding this comment

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

new solver special cases 'static? I thought we weren't supposed to do this 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to do so for the param_env to detect "global where-bounds". See #133643 / the FCP in #132325

Copy link
Member

Choose a reason for hiding this comment

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

ah yh that makes sense

Comment on lines 500 to 508
if let CanonicalizeMode::Input(CanonicalizeInputKind::Predicate {
is_hir_typeck_root_goal: true,
}) = self.canonicalize_mode
{
self.cached_fold_ty(t)
} else if let Some(&ty) = self.cache.get(&(self.binder_index, t)) {
Copy link
Member

Choose a reason for hiding this comment

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

I find this somewhat confusing, it reads like "if its a root goal use the cache" which seems wrong, and then we follow it up with "otherwise use the cache" which I thought we just did :>

but cached_fold_ty is about "caching" in the sense that we want to map multiple occurrences of ?x to the same canonical var? whereas self.cache is a proper ty -> canonical ty cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cached_fold_ty is the part of fn fold_ty which was cached :3

renamed it to inner_fold_ty and added a comment

///
/// See the tests in trait-system-refactor-initiative#27 for concrete examples.
///
/// FIXME(-Znext-solver): This is insufficient in theory as a goal `T: Trait<?x, ?x>`
Copy link
Member

Choose a reason for hiding this comment

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

why not do this at the same time :3 uniquifying regions seems somewhat meh if we aren't handling T: Trait<?x, ?x> correctly, and if it turns out that we cant "just" handle it correctly then we'd probably want to revert this (again again xd)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because it's annoying :3

@BoxyUwU
Copy link
Member

BoxyUwU commented Jul 24, 2025

r? BoxyUwU

@rustbot rustbot assigned BoxyUwU and unassigned compiler-errors Jul 24, 2025
@BoxyUwU BoxyUwU 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 Jul 24, 2025
@rust-cloud-vms rust-cloud-vms bot force-pushed the hir-typeck-uniquify branch from 53a905a to 0cb0427 Compare July 25, 2025 12:40
@rustbot
Copy link
Collaborator

rustbot commented Jul 25, 2025

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

@rust-cloud-vms rust-cloud-vms bot force-pushed the hir-typeck-uniquify branch 2 times, most recently from 5df011f to 5082eab Compare July 25, 2025 12:44
@lcnr
Copy link
Contributor Author

lcnr commented Jul 25, 2025

expect this to result in a minor perf hit

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Jul 25, 2025
uniquify root goals during HIR typeck
@rust-bors
Copy link

rust-bors bot commented Jul 25, 2025

⌛ Trying commit 5082eab with merge b91de35

To cancel the try build, run the command @bors try cancel.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 25, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jul 25, 2025

☀️ Try build successful (CI)
Build commit: b91de35 (b91de35e69e61422874a93fd8135dd731eb4baa3, parent: b56aaec52bc0fa35591a872fb4aac81f606e265c)

@rust-timer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms bot force-pushed the hir-typeck-uniquify branch from 5082eab to c55b6af Compare July 25, 2025 15:28
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (b91de35): 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)
- - 0
Regressions ❌
(secondary)
1.9% [0.1%, 7.6%] 22
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary 2.2%)

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.2% [1.3%, 3.7%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results (primary 2.2%, secondary 3.8%)

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

mean range count
Regressions ❌
(primary)
2.2% [2.2%, 2.2%] 1
Regressions ❌
(secondary)
3.8% [2.1%, 5.3%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.2% [2.2%, 2.2%] 1

Binary size

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

Bootstrap: 469.891s -> 470.268s (0.08%)
Artifact size: 374.63 MiB -> 374.53 MiB (-0.03%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jul 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dealing with region uniquification
6 participants