Skip to content

Comments

Add a layout_of_val intrinsic for when you want both size_of_val+align_of_val#152786

Closed
scottmcm wants to merge 6 commits intorust-lang:mainfrom
scottmcm:layout_of_val
Closed

Add a layout_of_val intrinsic for when you want both size_of_val+align_of_val#152786
scottmcm wants to merge 6 commits intorust-lang:mainfrom
scottmcm:layout_of_val

Conversation

@scottmcm
Copy link
Member

@scottmcm scottmcm commented Feb 18, 2026

EDIT: github stopped picking up pushes here, so moved to #152867


Today we're emitting a bunch more MIR and LLVM-IR than we need for stuff like this.

Fixes #152773

Closes #152641 because this solves the motivating example from that PR of avoiding the alignment transmutes when getting a layout from a DST, just in a better way.

While I was in the area I also put a more specific alignment !range metadata on loading alignment from a vtable and some extra no-wrap flags when calculating the size of an ADT with an unsized tail.

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 18, 2026
@scottmcm
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Feb 18, 2026
Add a `layout_of_val` intrinsic for when you want both `size_of_val`+`align_of_val`
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 18, 2026
@rust-log-analyzer

This comment has been minimized.

It was already computing both anyway with `size_of_val::size_and_align_of_dst`, so we just need to return them both instead of ignoring one.
@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 18, 2026

☀️ Try build successful (CI)
Build commit: f751a56 (f751a568aedd4c46059df62535b974a66346fd29, parent: 8387095803f21a256a9a772ac1f9b41ed4d5aa0a)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f751a56): 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
Improvements ✅
(primary)
-0.4% [-0.5%, -0.3%] 2
Improvements ✅
(secondary)
-1.1% [-3.1%, -0.4%] 14
All ❌✅ (primary) -0.4% [-0.5%, -0.3%] 2

Max RSS (memory usage)

Results (primary 0.2%, secondary -2.7%)

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

mean range count
Regressions ❌
(primary)
2.8% [2.0%, 3.5%] 2
Regressions ❌
(secondary)
1.7% [1.7%, 1.7%] 1
Improvements ✅
(primary)
-2.4% [-2.9%, -2.0%] 2
Improvements ✅
(secondary)
-3.3% [-5.5%, -2.2%] 8
All ❌✅ (primary) 0.2% [-2.9%, 3.5%] 4

Cycles

Results (primary -2.1%, secondary -3.5%)

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.1% [-2.1%, -2.1%] 1
Improvements ✅
(secondary)
-3.5% [-3.5%, -3.5%] 1
All ❌✅ (primary) -2.1% [-2.1%, -2.1%] 1

Binary size

Results (primary -0.4%, secondary -1.6%)

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.4% [-0.8%, -0.0%] 25
Improvements ✅
(secondary)
-1.6% [-3.9%, -0.3%] 13
All ❌✅ (primary) -0.4% [-0.8%, -0.0%] 25

Bootstrap: 480.396s -> 480.025s (-0.08%)
Artifact size: 397.88 MiB -> 397.84 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 18, 2026
@scottmcm
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Feb 18, 2026
Add a `layout_of_val` intrinsic for when you want both `size_of_val`+`align_of_val`
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 18, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 18, 2026

☀️ Try build successful (CI)
Build commit: 20e17c0 (20e17c08eb05c07af1938fee20fae4fb3351712d, parent: c043085801b7a884054add21a94882216df5971c)

@rust-timer

This comment has been minimized.

@rust-timer

This comment was marked as outdated.

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Feb 18, 2026
@scottmcm scottmcm removed the perf-regression Performance regression. label Feb 19, 2026
@scottmcm
Copy link
Member Author

I'd tried something else that just didn't work, so I reverted back to what got the previous fully-green perf run (#152786 (comment)).

@scottmcm scottmcm marked this pull request as ready for review February 19, 2026 06:15
@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2026

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

⚠️ #[miri::intrinsic_fallback_is_spec] must only be used if the function actively checks for all UB cases,
and explores the possible non-determinism of the intrinsic.

cc @rust-lang/miri

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 19, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2026

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: codegen, compiler
  • codegen, compiler expanded to 68 candidates
  • Random selection from 15 candidates

// Alignment is always nonzero.
bx.range_metadata(align, WrappingRange { start: 1, end: !0 });
// Alignment is always a power of two, thus 1..=0x800…000.
let align_bound = size_bound + 1;
Copy link
Member

Choose a reason for hiding this comment

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

In fact the bound is Align::MAX, is it worth emitting that?

Copy link
Member Author

@scottmcm scottmcm Feb 19, 2026

Choose a reason for hiding this comment

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

Oh, really good point 👍 I was stuck still thinking of it as a load of ptr::Alignment, but it's not.

I don't know how much it'll really matter, since isize::MAX+1 is enough to prove that size+align doesn't unsigned-overflow, but might as well include it because it's easy, cheap, clearly not worse, and makes the test verification check slightly nicer.

sym::size_of_val => llsize,
sym::align_of_val => llalign,
sym::layout_of_val => {
// Use the builder so we're insulated from the in-memory field order
Copy link
Member

Choose a reason for hiding this comment

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

We're still relying on the in-source field order here. That deserves a comment on both ends (here and in the library). Or can the field lookup here be done by name?

@scottmcm
Copy link
Member Author

Something really odd is happening here where github is noticing the pushes in my fork but not in this pr.

turning it off and on again...

@scottmcm scottmcm closed this Feb 19, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 19, 2026
@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
finished building tool typos
error: `implemention` should be `implementation`
   ╭▸ library/core/src/alloc/layout.rs:29:16
   │
29 │ // BEWARE! The implemention of the `layout_of_val` intrinsic is coupled to the
   ╰╴               ━━━━━━━━━━━━
rerun tidy with `--extra-checks=spellcheck --bless` to fix typos
tidy [extra_checks]: checks with external tool 'typos' failed
tidy [extra_checks]: FAIL
tidy: The following check failed: extra_checks
Bootstrap failed while executing `test src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck`
Command `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools-bin/rust-tidy --root-path=/checkout --cargo-path=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo --output-dir=/checkout/obj/build --concurrency=4 --npm-path=/node/bin/yarn --extra-checks=py,cpp,js,spellcheck` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:1612:23
Executed at: src/bootstrap/src/core/build_steps/test.rs:1365:29

Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:02:38
  local time: Thu Feb 19 21:02:30 UTC 2026
  network time: Thu, 19 Feb 2026 21:02:30 GMT
##[error]Process completed with exit code 1.
##[group]Run echo "disk usage:"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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.

Dropping boxes with unsized tails generates a bunch of duplicate IR

6 participants