Skip to content

Conversation

@scottmcm
Copy link
Member

We already skipped intermediate steps like u32 or i32 that support any (initialized) value.

This extends that to also allow skipping intermediate steps whose values are a superset of either the source or destination type. Most importantly, that means that usizeNonZeroUsizeptr::Alignment and ptr::AlignmentNonZeroUsizeusize can skip the middle because NonZeroUsize is a superset of Alignment.

Then Alignment::as_usize is updated to take advantage of that and let us remove some more locals in a few places.

r? cjgillot

@rustbot
Copy link
Collaborator

rustbot commented Jan 24, 2026

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 24, 2026
@scottmcm
Copy link
Member Author

I'm not expecting much, but just in case:
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Jan 25, 2026
GVN: Elide more intermediate transmutes
@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 Jan 25, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 25, 2026

☀️ Try build successful (CI)
Build commit: cd2b201 (cd2b201cee18477fa23de21b4a5bbee37815f3dd, parent: 75963ce795666bc1f961e5d60060809809f6bc68)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (cd2b201): 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)
0.3% [0.3%, 0.3%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.5% [-0.8%, -0.2%] 2
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 1
All ❌✅ (primary) -0.2% [-0.8%, 0.3%] 3

Max RSS (memory usage)

Results (primary -4.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)
-4.0% [-5.7%, -2.2%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -4.0% [-5.7%, -2.2%] 3

Cycles

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

Binary size

Results (primary -0.0%, 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.1% [0.0%, 0.5%] 11
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
-0.1% [-1.1%, -0.0%] 13
Improvements ✅
(secondary)
-0.0% [-0.1%, -0.0%] 6
All ❌✅ (primary) -0.0% [-1.1%, 0.5%] 24

Bootstrap: 471.746s -> 470.956s (-0.17%)
Artifact size: 383.60 MiB -> 383.53 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 Jan 25, 2026
@rust-bors

This comment has been minimized.

// as it's just there to convey the validity invariant.
// (Hopefully it'll eventually be a pattern type instead.)
_inner_repr_trick: AlignmentEnum,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this change be done in its own commit? This would make easier to understand what changes from the mir opt diff.

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely. I split it to three:

  1. Just the compiler change to the mir-opt pass
  2. The change to Alignment::as_usize on its own
  3. The field change in Alignment

That way all the library changes are separated.

Comment on lines 1674 to 1701
} else if let Ok(from_layout) = self.ecx.layout_of(from_ty)
&& !from_layout.uninhabited
&& from_layout.size == middle_layout.size
&& let BackendRepr::Scalar(from_a) = from_layout.backend_repr
&& let a_range = a.valid_range(&self.ecx)
&& let from_range = from_a.valid_range(&self.ecx)
&& a_range.contains_range(from_range, middle_layout.size)
{
false
} else if let Ok(to_layout) = self.ecx.layout_of(to_ty)
&& !to_layout.uninhabited
&& to_layout.size == middle_layout.size
&& let BackendRepr::Scalar(to_a) = to_layout.backend_repr
&& let a_range = a.valid_range(&self.ecx)
&& let to_range = to_a.valid_range(&self.ecx)
&& a_range.contains_range(to_range, middle_layout.size)
{
false
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mind commenting the logic? This looks good to me, but it took me a few frowns to understand, for instance why ranges are compared the way you wrote it

@scottmcm scottmcm force-pushed the elide-more-transmutes branch from 433703d to 9288c20 Compare January 26, 2026 01:26
@rustbot
Copy link
Collaborator

rustbot commented Jan 26, 2026

This PR was rebased onto a different main 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.

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-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. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants