Skip to content

Simplify align_of_val::<[T]>(…)align_of::<T>() #144566

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

Merged
merged 3 commits into from
Jul 29, 2025

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Jul 28, 2025

I spotted this while working on the inliner (#144561). In particular, if Layout::for_value inlines, then it can be pretty easy to end up with an align_of_val::<[T]> today (demo: https://rust.godbolt.org/z/Tesnscj4a) where we can save at least a block, if not more, by using the version that's an rvalue and not a call.

@scottmcm scottmcm added the A-mir-opt Area: MIR optimizations label Jul 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 28, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
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 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 Jul 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 28, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Comment on lines +8 to +12
pub fn of_val_slice<T>(slice: &[T]) -> usize {
// CHECK-LABEL: fn of_val_slice(_1: &[T])
// CHECK: _0 = AlignOf(T);
unsafe { core::intrinsics::align_of_val(slice) }
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you're explicitly mentioning this running after inlining, maybe change it to

Suggested change
pub fn of_val_slice<T>(slice: &[T]) -> usize {
// CHECK-LABEL: fn of_val_slice(_1: &[T])
// CHECK: _0 = AlignOf(T);
unsafe { core::intrinsics::align_of_val(slice) }
}
pub fn of_val_slice<T>(slice: &[T]) -> usize {
// CHECK-LABEL: fn of_val_slice(_1: &[T])
// CHECK: _0 = AlignOf(T);
something_inlined(slice)
}
#[inline(always)]
fn something_inlined<T: ?Sized>(val: &T) -> usize {
unsafe { core::intrinsics::align_of_val(slice) }
}

or use the mem wrapper

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, that wouldn't work with test-mir-pass -- this folder is more unit testy, usually.

I guess I can just bring over the test from https://github.com/rust-lang/rust/pull/144572/files#diff-9e6b893e458a80c489851369ec09247ad188228d94c4c92d3ba28fea1702fdc8 though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just merging is fine with me, I was mostly wondering why it wasn't this way. r=me at your discretion what you think is best

@oli-obk
Copy link
Contributor

oli-obk commented Jul 29, 2025

unlikely to have a perf effect outside of microbenchmarks, but it may also unlock other opts, so let's perf it to decide whether to rollup or not

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jul 29, 2025

⌛ Trying commit 8ef9233 with merge 045ae4f

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

rust-bors bot added a commit that referenced this pull request Jul 29, 2025
Simplify `align_of_val::<[T]>(…)` → `align_of::<T>()`
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 29, 2025
}
scope 23 (inlined Layout::align) {
scope 24 (inlined std::ptr::Alignment::as_usize) {
let mut _10: u32;
Copy link
Member Author

Choose a reason for hiding this comment

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

annot: this u32 is why the test needs to be split by bit width.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

r=me with perf finished

@rust-bors
Copy link

rust-bors bot commented Jul 29, 2025

☀️ Try build successful (CI)
Build commit: 045ae4f (045ae4f32f81a7bb6b048d92aad0cbad72f2761e, parent: cb6785f73df1aa3f558796a22a4ab9652cf38e26)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (045ae4f): 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)
1.0% [1.0%, 1.0%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.0% [1.0%, 1.0%] 1

Max RSS (memory usage)

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

Cycles

Results (secondary 1.9%)

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)
4.0% [2.4%, 5.6%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.3% [-2.3%, -2.3%] 1
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.4%)

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

mean range count
Regressions ❌
(primary)
0.4% [0.4%, 0.4%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.4% [0.4%, 0.4%] 1

Bootstrap: 467.912s -> 468.532s (0.13%)
Artifact size: 376.82 MiB -> 376.83 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 Jul 29, 2025
@oli-obk
Copy link
Contributor

oli-obk commented Jul 29, 2025

Probably llvm opt noise

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jul 29, 2025

📌 Commit 8ef9233 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 29, 2025
bors added a commit that referenced this pull request Jul 29, 2025
Rollup of 6 pull requests

Successful merges:

 - #144560 (coverage: Treat `#[automatically_derived]` as `#[coverage(off)]`)
 - #144566 (Simplify `align_of_val::<[T]>(…)` → `align_of::<T>()`)
 - #144587 (expand: Micro-optimize prelude injection)
 - #144589 (Account for `.yield` in illegal postfix operator message)
 - #144615 (Make resolve_fn_signature responsible for its own rib.)
 - #144634 (Fix typo in `DropGuard` doc)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 826c462 into rust-lang:master Jul 29, 2025
11 checks passed
@rustbot rustbot added this to the 1.90.0 milestone Jul 29, 2025
rust-timer added a commit that referenced this pull request Jul 29, 2025
Rollup merge of #144566 - scottmcm:align-of-slice, r=oli-obk

Simplify `align_of_val::<[T]>(…)` → `align_of::<T>()`

I spotted this while working on the inliner (#144561).  In particular, if [`Layout::for_value`](https://doc.rust-lang.org/std/alloc/struct.Layout.html#method.for_value) inlines, then it can be pretty easy to end up with an `align_of_val::<[T]>` today (demo: <https://rust.godbolt.org/z/Tesnscj4a>) where we can save at least a block, if not more, by using the version that's an rvalue and not a call.
@scottmcm scottmcm deleted the align-of-slice branch July 29, 2025 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt Area: MIR optimizations perf-regression Performance regression. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants