-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
perf(codegen): Eliminate size_of_val == 0 for DSTs with Non-zero-sized Prefix via NUW and Assume
#152843
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
TKanX
wants to merge
5
commits into
rust-lang:main
Choose a base branch
from
TKanX:bugfix/152788-codegen-dst-size-nuw-assume
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+52
−6
Open
perf(codegen): Eliminate size_of_val == 0 for DSTs with Non-zero-sized Prefix via NUW and Assume
#152843
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12a18ee
perf(codegen): Use `nuw nsw` and `assume` to eliminate `size_of_val =…
TKanX 8339cfe
test(codegen): Add regression test for `size_of_val == 0` elimination…
TKanX 689cd64
test(codegen): Restore original CHECK patterns in dst-vtable-align-no…
TKanX 3e6f372
test(codegen): Revert unnecessary ALIGN_RANGE rename in dst-vtable-al…
TKanX 45b1d74
test(codegen): Fix dst-vtable-align-nonzero patterns for aarch64
TKanX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| //@ compile-flags: -Copt-level=3 -Z merge-functions=disabled | ||
| //@ needs-deterministic-layouts | ||
|
|
||
| #![crate_type = "lib"] | ||
|
|
||
| // Regression test for #152788: `size_of_val(p) == 0` should optimize to `false` | ||
| // for types whose statically-known prefix makes them clearly not ZSTs. | ||
| // | ||
| // This works because: | ||
| // 1. The `offset + unsized_size` addition has NUW+NSW, so LLVM knows | ||
| // `unrounded_size >= offset` | ||
| // 2. An `llvm.assume` tells LLVM `aligned_size >= unrounded_size` | ||
| // 3. Together: `aligned_size >= unrounded_size >= offset > 0` | ||
|
|
||
| pub struct Foo<T: ?Sized>(pub [u32; 3], pub T); | ||
|
|
||
| // CHECK-LABEL: @size_of_val_dyn_not_zero | ||
| #[no_mangle] | ||
| pub fn size_of_val_dyn_not_zero(p: &Foo<dyn std::fmt::Debug>) -> bool { | ||
| // CHECK: ret i1 false | ||
| std::mem::size_of_val(p) == 0 | ||
| } | ||
|
|
||
| // CHECK-LABEL: @size_of_val_slice_u8_not_zero | ||
| #[no_mangle] | ||
| pub fn size_of_val_slice_u8_not_zero(p: &Foo<[u8]>) -> bool { | ||
| // CHECK: ret i1 false | ||
| std::mem::size_of_val(p) == 0 | ||
| } | ||
|
|
||
| // CHECK-LABEL: @size_of_val_slice_i32_not_zero | ||
| #[no_mangle] | ||
| pub fn size_of_val_slice_i32_not_zero(p: &Foo<[i32]>) -> bool { | ||
| // CHECK: ret i1 false | ||
| std::mem::size_of_val(p) == 0 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate on which things you tried and why this is the best one? Was it not enough to say that the alignment is a power-of-two? Or...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ask because most of the text in the OP is just useless LLM slop, and the updates to the tests make me suspicious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@scottmcm
Tried nuw-only (
unchecked_uadd) first. That gives LLVMunrounded >= offset > 0but it stops at the rounding — LLVM can't prove(x + a-1) & -a >= x. Also checked whether feedingctpop(align) == 1would help, but there's no fold for "round-up is monotonic when alignment is pow2" in InstCombine/ValueTracking. So the assume tells LLVM the conclusion directly.nsw (making it
unchecked_suadd) is because unrounded ≤ rounded ≤isize::MAX. Same reasoning as your #152867.Sorry about the OP — English isn't my native language, I overwrite when trying to be precise. Will clean it up.
For the tests:
CHECK-NOT: icmpbroke becauseassumeitself emits anicmp. The!rangechecks on the first two functions were dropped because the assume keeps the size computation alive, so there's now a sizeloadbefore the alignment load — FileCheck hits the wrong one. Range metadata is still verified inalign_load_from_align_of_valbelow.RANGE_META→ALIGN_RANGEsince it only covers alignment loads now. Range value{1, 0}→{1, 0x20000001}isAlign::max_for_target(same change as #152929).Happy to close this if you'd rather land it as part of #152867.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Landing this separately is great -- I opened the issue because this particular bit about what LLVM can prove is different enough from the point of
layout_of_valthat it's better to have the changes separated. (That's why I pulled out #152929 too 🙂 )There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, yeah, I experimented a bit https://llvm.godbolt.org/z/haGYz7aax and even getting lots of annotations on everything and assume it's still not able to understand what's happening properly.
(Also it's so annoying to see
add nsw i64 %4, -1since that used to besub nuw nsw i64 %4, 1but LLVM just insists on throwing that information away.)