-
-
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
base: main
Are you sure you want to change the base?
perf(codegen): Eliminate size_of_val == 0 for DSTs with Non-zero-sized Prefix via NUW and Assume
#152843
Changes from 2 commits
12a18ee
8339cfe
689cd64
3e6f372
45b1d74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,9 +30,8 @@ pub struct Struct<W: ?Sized> { | |
| pub fn eliminates_runtime_check_when_align_1( | ||
| x: &Struct<WrapperWithAlign1<dyn Trait>>, | ||
| ) -> &WrapperWithAlign1<dyn Trait> { | ||
| // CHECK: load [[USIZE:i[0-9]+]], {{.+}} !range [[RANGE_META:![0-9]+]] | ||
| // CHECK: load [[USIZE:i[0-9]+]] | ||
| // CHECK-NOT: llvm.umax | ||
| // CHECK-NOT: icmp | ||
| // CHECK-NOT: select | ||
| // CHECK: ret | ||
|
Comment on lines
-33
to
36
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the problem here is that if this was testing for "not icmp", just removing that check means this test is (potentially) no longer testing what it was trying to test before. If there's an icmp now, probably what you want instead is something like so that the test is that the only icmp is the expected one that's used for the assume. Similarly, why remove the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checked the emitted IR — the assume (and the entire size computation) gets DCE'd in these two functions at -O3, since they only need alignment for the field projection. So there's no extra icmp at all, and the alignment load is still the first one with |
||
| &x.dst | ||
|
|
@@ -43,7 +42,7 @@ pub fn eliminates_runtime_check_when_align_1( | |
| pub fn does_not_eliminate_runtime_check_when_align_2( | ||
| x: &Struct<WrapperWithAlign2<dyn Trait>>, | ||
| ) -> &WrapperWithAlign2<dyn Trait> { | ||
| // CHECK: [[X0:%[0-9]+]] = load [[USIZE]], {{.+}} !range [[RANGE_META]] | ||
| // CHECK: load [[USIZE]] | ||
| // CHECK: {{icmp|llvm.umax}} | ||
| // CHECK: ret | ||
| &x.dst | ||
|
|
@@ -52,16 +51,16 @@ pub fn does_not_eliminate_runtime_check_when_align_2( | |
| // CHECK-LABEL: @align_load_from_align_of_val | ||
| #[no_mangle] | ||
| pub fn align_load_from_align_of_val(x: &dyn Trait) -> usize { | ||
| // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] | ||
| // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[ALIGN_RANGE:![0-9]+]] | ||
| core::mem::align_of_val(x) | ||
| } | ||
|
|
||
| // CHECK-LABEL: @align_load_from_vtable_align_intrinsic | ||
| #[no_mangle] | ||
| pub unsafe fn align_load_from_vtable_align_intrinsic(x: &dyn Trait) -> usize { | ||
| let (data, vtable): (*const (), *const ()) = core::mem::transmute(x); | ||
| // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] | ||
| // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[ALIGN_RANGE]] | ||
| core::intrinsics::vtable_align(vtable) | ||
| } | ||
|
|
||
| // CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] [[#0x20000001]] | ||
| // CHECK: [[ALIGN_RANGE]] = !{[[USIZE]] 1, [[USIZE]] [[#0x20000001]] | ||
TKanX marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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.)