Skip to content

Shallowly bail from coerce_unsized more #142941

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

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Jun 24, 2025

We do a lot of coercion in HIR typeck. Most of the time we're just coercing a type to itself, but we always try coerce_unsized even if it's not necessary.

Let's avoid doing that by adding a fast path to coerce_unsized; see the comment in that function.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 24, 2025
@compiler-errors
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 24, 2025
@bors
Copy link
Collaborator

bors commented Jun 24, 2025

⌛ Trying commit e0b68fe with merge 6a35eeb...

bors added a commit that referenced this pull request Jun 24, 2025
[perf] Shallowly bail from `coerce_unsized` more
@compiler-errors
Copy link
Member Author

TODO: test with a fast path for &str: CoerceUnsized<&str>.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jun 24, 2025

☀️ Try build successful - checks-actions
Build commit: 6a35eeb (6a35eeb37ae3dfbce8f5159463405fb81cddd28a)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (6a35eeb): comparison URL.

Overall result: ✅ 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.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
-0.7% [-4.5%, -0.1%] 60
Improvements ✅
(secondary)
-6.7% [-21.6%, -0.1%] 58
All ❌✅ (primary) -0.7% [-4.5%, -0.1%] 60

Max RSS (memory usage)

Results (primary -1.2%, secondary -5.0%)

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)
-1.2% [-1.2%, -1.2%] 2
Improvements ✅
(secondary)
-5.8% [-10.6%, -1.6%] 9
All ❌✅ (primary) -1.2% [-1.2%, -1.2%] 2

Cycles

Results (primary -4.5%, secondary -15.4%)

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)
-4.5% [-5.5%, -3.2%] 6
Improvements ✅
(secondary)
-15.4% [-25.4%, -6.1%] 27
All ❌✅ (primary) -4.5% [-5.5%, -3.2%] 6

Binary size

Results (secondary -0.0%)

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)
-0.0% [-0.0%, -0.0%] 1
All ❌✅ (primary) - - 0

Bootstrap: 688.989s -> 689.016s (0.00%)
Artifact size: 371.96 MiB -> 372.08 MiB (0.03%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 24, 2025
@compiler-errors
Copy link
Member Author

holy fuck lol

@compiler-errors
Copy link
Member Author

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jun 24, 2025

⌛ Trying commit 3d115ac with merge 1eb1a24

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

rust-bors bot added a commit that referenced this pull request Jun 24, 2025
[perf] Shallowly bail from `coerce_unsized` more
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 24, 2025
@compiler-errors compiler-errors marked this pull request as ready for review June 24, 2025 17:01
@compiler-errors
Copy link
Member Author

Anyways this is ready to review.

r? @lcnr

@rustbot
Copy link
Collaborator

rustbot commented Jun 24, 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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 24, 2025
@rustbot rustbot assigned lcnr and unassigned petrochenkov Jun 24, 2025
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-1 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
test [crashes] tests/crashes/122823.rs ... ok
test [crashes] tests/crashes/125841.rs ... ok
test [crashes] tests/crashes/125801.rs ... ok
test [crashes] tests/crashes/126268.rs ... ok
2025-06-24T17:37:08.303374Z ERROR compiletest::runtest: fatal error, panic: "crashtest no longer crashes/triggers ICE, hooray! Please give it a meaningful name, add a doc-comment to the start of the test explaining why it exists and move it to tests/ui or wherever you see fit. Adding 'Fixes #<issueNr>' to your PR description ensures that the corresponding ticket is auto-closed upon merge. If you want to see verbose output, set `COMPILETEST_VERBOSE_CRASHES=1`."
test [crashes] tests/crashes/126269.rs ... FAILED
test [crashes] tests/crashes/126443.rs ... ok
test [crashes] tests/crashes/126667.rs ... ok
test [crashes] tests/crashes/126680.rs ... ok
test [crashes] tests/crashes/126696.rs ... ok
---
test [crashes] tests/crashes/130970.rs ... ok
test [crashes] tests/crashes/131046.rs ... ok
test [crashes] tests/crashes/131292.rs ... ignored, only executed when the architecture is x86_64
test [crashes] tests/crashes/131295.rs ... ok
2025-06-24T17:37:09.173889Z ERROR compiletest::runtest: fatal error, panic: "crashtest no longer crashes/triggers ICE, hooray! Please give it a meaningful name, add a doc-comment to the start of the test explaining why it exists and move it to tests/ui or wherever you see fit. Adding 'Fixes #<issueNr>' to your PR description ensures that the corresponding ticket is auto-closed upon merge. If you want to see verbose output, set `COMPILETEST_VERBOSE_CRASHES=1`."
test [crashes] tests/crashes/131048.rs ... FAILED
test [crashes] tests/crashes/131052.rs ... ok
test [crashes] tests/crashes/131347.rs ... ok
test [crashes] tests/crashes/131373.rs ... ok
test [crashes] tests/crashes/131406.rs ... ok
---
test [crashes] tests/crashes/133808.rs ... ok
test [crashes] tests/crashes/133966.rs ... ok
test [crashes] tests/crashes/134061.rs ... ok
test [crashes] tests/crashes/133965.rs ... ok
2025-06-24T17:37:10.226182Z ERROR compiletest::runtest: fatal error, panic: "crashtest no longer crashes/triggers ICE, hooray! Please give it a meaningful name, add a doc-comment to the start of the test explaining why it exists and move it to tests/ui or wherever you see fit. Adding 'Fixes #<issueNr>' to your PR description ensures that the corresponding ticket is auto-closed upon merge. If you want to see verbose output, set `COMPILETEST_VERBOSE_CRASHES=1`."
test [crashes] tests/crashes/134217.rs ... FAILED
test [crashes] tests/crashes/131342.rs ... ok
test [crashes] tests/crashes/134174.rs ... ok
test [crashes] tests/crashes/134587.rs ... ok
test [crashes] tests/crashes/134615.rs ... ok
---
failures:

---- [crashes] tests/crashes/126269.rs stdout ----

error: crashtest no longer crashes/triggers ICE, hooray! Please give it a meaningful name, add a doc-comment to the start of the test explaining why it exists and move it to tests/ui or wherever you see fit. Adding 'Fixes #<issueNr>' to your PR description ensures that the corresponding ticket is auto-closed upon merge. If you want to see verbose output, set `COMPILETEST_VERBOSE_CRASHES=1`.

thread '[crashes] tests/crashes/126269.rs' panicked at src/tools/compiletest/src/runtest/crashes.rs:17:18:
fatal error
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- [crashes] tests/crashes/131048.rs stdout ----

error: crashtest no longer crashes/triggers ICE, hooray! Please give it a meaningful name, add a doc-comment to the start of the test explaining why it exists and move it to tests/ui or wherever you see fit. Adding 'Fixes #<issueNr>' to your PR description ensures that the corresponding ticket is auto-closed upon merge. If you want to see verbose output, set `COMPILETEST_VERBOSE_CRASHES=1`.

thread '[crashes] tests/crashes/131048.rs' panicked at src/tools/compiletest/src/runtest/crashes.rs:17:18:
fatal error

---- [crashes] tests/crashes/134217.rs stdout ----

error: crashtest no longer crashes/triggers ICE, hooray! Please give it a meaningful name, add a doc-comment to the start of the test explaining why it exists and move it to tests/ui or wherever you see fit. Adding 'Fixes #<issueNr>' to your PR description ensures that the corresponding ticket is auto-closed upon merge. If you want to see verbose output, set `COMPILETEST_VERBOSE_CRASHES=1`.

thread '[crashes] tests/crashes/134217.rs' panicked at src/tools/compiletest/src/runtest/crashes.rs:17:18:
fatal error


@rust-bors
Copy link

rust-bors bot commented Jun 24, 2025

☀️ Try build successful (CI)
Build commit: 1eb1a24 (1eb1a24f58bb3d49e9be66d408afa9adf2b30a78, parent: 36b21637e93b038453924d3c66821089e71d8baa)

@rust-timer

This comment has been minimized.

@compiler-errors
Copy link
Member Author

Crashes should be fixed by #142976; they're only coincidentally fixed here by the fast path bc we don't end up trying coerce_unsized.

@compiler-errors compiler-errors changed the title [perf] Shallowly bail from coerce_unsized more Shallowly bail from coerce_unsized more Jun 24, 2025
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1eb1a24): comparison URL.

Overall result: ✅ 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.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
0.4% [0.3%, 0.4%] 2
Improvements ✅
(primary)
-0.8% [-4.5%, -0.1%] 80
Improvements ✅
(secondary)
-5.5% [-21.6%, -0.0%] 73
All ❌✅ (primary) -0.8% [-4.5%, 0.2%] 81

Max RSS (memory usage)

Results (primary -0.9%, 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)
1.3% [1.3%, 1.3%] 1
Regressions ❌
(secondary)
3.9% [3.9%, 4.0%] 2
Improvements ✅
(primary)
-3.1% [-3.1%, -3.1%] 1
Improvements ✅
(secondary)
-5.2% [-10.6%, -1.5%] 11
All ❌✅ (primary) -0.9% [-3.1%, 1.3%] 2

Cycles

Results (primary -2.0%, secondary -12.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)
- - 0
Improvements ✅
(primary)
-2.0% [-4.1%, -0.8%] 26
Improvements ✅
(secondary)
-12.2% [-24.0%, -0.9%] 31
All ❌✅ (primary) -2.0% [-4.1%, -0.8%] 26

Binary size

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

Bootstrap: 689.383s -> 687.827s (-0.23%)
Artifact size: 371.94 MiB -> 372.03 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 25, 2025
@@ -515,11 +515,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
///
/// [unsized coercion](https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions)
#[instrument(skip(self), level = "debug")]
fn coerce_unsized(&self, mut source: Ty<'tcx>, mut target: Ty<'tcx>) -> CoerceResult<'tcx> {
source = self.shallow_resolve(source);
target = self.shallow_resolve(target);
Copy link
Contributor

Choose a reason for hiding this comment

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

why remove these, are we guaranteed that they are shallow resolved? if so, please convert them into debug asserts

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, we shallowly resolve at the top of the coerce entrypoint

| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
| ty::Str
| ty::Array(_, _)
| ty::Slice(_)
Copy link
Contributor

Choose a reason for hiding this comment

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

array to slice is always Deref an not CoerceUnsized? 🤔

Copy link
Member Author

@compiler-errors compiler-errors Jun 25, 2025

Choose a reason for hiding this comment

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

Slices are the RHS for Unsize, not CoerceUnsized. The latter is for pointers not data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

7 participants