Skip to content

Conversation

adwinwhite
Copy link
Contributor

@adwinwhite adwinwhite commented Sep 1, 2025

Fixes #92004
Fixes #92470
Fixes #95134
Fixes #105275
Fixes #105937
Fixes #117696-2
Fixes #118590
Fixes #122823
Fixes #131342
Fixes #139659

Analysis:

The causes of these issues are similar. They contain generic recursive functions that can be instantiated with different args infinitely at monomorphization stage.
Ideally this should be caught by the check_recursion_limit function. The reality is that normalization can reach recursion limit earlier than monomorphization's check because they calculate depths in different ways.
Since normalization is called everywhere, ICEs appear in different locations.

Fix:

If we abort on overflow with TypingMode::PostAnalysis in the trait solver, it would also catch these errors.
The main challenge is providing good diagnostics for them. So it's quite natural to put the check right before these normalization happening.
I first tried to check the whole MIR body's normalization and references_error. (As elaborate_drop handles normalization failure by returning ty::Error.)
It turns out that checking all Locals seems sufficient.
These types are gonna be normalized anyway. So with cache, these checks shouldn't be expensive.

This fixes these ICEs for both the next and old solver, though I'm not sure the change I made to the old solver is proper. Its overflow handling looks convoluted thus I didn't try to fix it more "upstream".

@rustbot

This comment was marked as outdated.

@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. labels Sep 1, 2025
@jieyouxu

This comment was marked as outdated.

@rustbot rustbot assigned saethlin and unassigned jieyouxu Sep 2, 2025
@jieyouxu
Copy link
Member

jieyouxu commented Sep 2, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Sep 2, 2025
…ono1, r=<try>

Fix normalization overflow ICEs in monomorphization
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 2, 2025
@rust-bors
Copy link

rust-bors bot commented Sep 2, 2025

☀️ Try build successful (CI)
Build commit: fb35892 (fb35892cd06f475414cd254159fed42f93083db6, parent: 05abce5d058db0de3abd10f32f1a442d0f699b30)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (fb35892): comparison URL.

Overall result: ❌✅ regressions and improvements - 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)
5.9% [0.3%, 45.5%] 63
Regressions ❌
(secondary)
8.7% [0.1%, 53.7%] 32
Improvements ✅
(primary)
-0.2% [-0.3%, -0.1%] 21
Improvements ✅
(secondary)
-1.0% [-2.9%, -0.1%] 28
All ❌✅ (primary) 4.4% [-0.3%, 45.5%] 84

Max RSS (memory usage)

Results (primary 9.4%, secondary 6.5%)

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

mean range count
Regressions ❌
(primary)
9.4% [1.2%, 31.2%] 57
Regressions ❌
(secondary)
6.5% [1.4%, 19.0%] 31
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 9.4% [1.2%, 31.2%] 57

Cycles

Results (primary 11.3%, secondary 16.9%)

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

mean range count
Regressions ❌
(primary)
11.3% [1.7%, 51.8%] 44
Regressions ❌
(secondary)
19.5% [2.5%, 64.2%] 15
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.8% [-3.1%, -2.5%] 2
All ❌✅ (primary) 11.3% [1.7%, 51.8%] 44

Binary size

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

Bootstrap: 467.236s -> 465.852s (-0.30%)
Artifact size: 388.42 MiB -> 388.49 MiB (0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Sep 2, 2025
@@ -1,3 +1,4 @@
//@ build-fail
//@ known-bug: #105937
Copy link
Member

@bjorn3 bjorn3 Sep 4, 2025

Choose a reason for hiding this comment

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

Suggested change
//@ known-bug: #105937

right? Same elsewhere.

@adwinwhite
Copy link
Contributor Author

adwinwhite commented Sep 4, 2025

I have to make the check a query in order to cache its result for incremental build.
collect_and_partition_mono_items query is eval always. So optimzed_mir query in instance_mir and normalization query always got called. Even with query cache, loading MIR from disk is not that cheap.
That's why the regressions happened.

@bors
Copy link
Collaborator

bors commented Sep 9, 2025

☔ The latest upstream changes (presumably #145717) made this pull request unmergeable. Please resolve the merge conflicts.

// may be expensive.
fn has_normalization_error_in_mono<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool {
let body = tcx.instance_mir(instance.def);
body.local_decls.iter().any(|local| {
Copy link
Member

Choose a reason for hiding this comment

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

Why are you instantiating types seen in local_decls instead of the entire body? I'm worried that an error will sneak by this check because it's hidden somewhere else in the MIR body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did check the whole body at first. Then I realized that instantiation is not cached and worried that the computational cost is too high if the body is huge, so I tried to scale down the check.

Ofc this is just speculation, I'm not sure about the real cost.

Copy link
Member

Choose a reason for hiding this comment

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

This is done in a query, which should provide the requisite level of caching. Switch this to check the whole body and I can submit another perf run before merging.

@saethlin
Copy link
Member

Just one question ^ then I think this is good

@saethlin saethlin 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 Sep 14, 2025
Comment on lines +481 to +492
if tcx.has_normalization_error_in_mono(instance) {
let def_id = instance.def_id();
let def_span = tcx.def_span(def_id);
let def_path_str = tcx.def_path_str(def_id);
tcx.dcx().emit_fatal(RecursionLimit {
span: starting_item.span,
instance,
def_span,
def_path_str,
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we move error reporting to the query itself, and make the query return unit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we do so, the query would be dependent on starting_item.span which may pollute the cache unnecessarily?

@adwinwhite adwinwhite force-pushed the handle_normalization_overflow_in_mono1 branch from 2e360ea to a454317 Compare September 15, 2025 04:15
@rustbot
Copy link
Collaborator

rustbot commented Sep 15, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@adwinwhite adwinwhite force-pushed the handle_normalization_overflow_in_mono1 branch from a454317 to ade5b67 Compare September 15, 2025 05:20
@rust-log-analyzer

This comment has been minimized.

@adwinwhite
Copy link
Contributor Author

Ready for another perf run. :⁠-⁠)

@saethlin
Copy link
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Sep 16, 2025
…ono1, r=<try>

Fix normalization overflow ICEs in monomorphization
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 16, 2025
@rust-bors
Copy link

rust-bors bot commented Sep 16, 2025

☀️ Try build successful (CI)
Build commit: 3ce4b4f (3ce4b4f6df15e7de71a8b7125c2480f73f4c9a83, parent: 9d82de19dfae60e55c291f5f28e28cfc2c1b9630)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (3ce4b4f): 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.7% [0.2%, 2.0%] 44
Regressions ❌
(secondary)
1.6% [0.3%, 18.6%] 22
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.5% [-0.5%, -0.5%] 1
All ❌✅ (primary) 0.7% [0.2%, 2.0%] 44

Max RSS (memory usage)

Results (primary 0.8%)

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

mean range count
Regressions ❌
(primary)
1.4% [0.7%, 2.1%] 5
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.0% [-2.0%, -2.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.8% [-2.0%, 2.1%] 6

Cycles

Results (primary 2.6%, secondary 4.3%)

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

mean range count
Regressions ❌
(primary)
2.6% [2.0%, 3.2%] 3
Regressions ❌
(secondary)
9.2% [2.6%, 16.6%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.0% [-3.9%, -2.2%] 2
All ❌✅ (primary) 2.6% [2.0%, 3.2%] 3

Binary size

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

Bootstrap: 475.305s -> 474.801s (-0.11%)
Artifact size: 390.12 MiB -> 390.20 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment