-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[codegen] assume the tag, not the relative discriminant #144764
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: master
Are you sure you want to change the base?
[codegen] assume the tag, not the relative discriminant #144764
Conversation
Some changes occurred in compiler/rustc_codegen_ssa |
// CHECK: tail call void @llvm.assume(i1 %[[A_NOT_HOLE]]) | ||
// CHECK: %[[A_DISCR:.+]] = select i1 %[[A_IS_NICHE]], i64 %[[A_REL_DISCR]], i64 1 | ||
// LLVM20: %[[A_DISCR:.+]] = select i1 %[[A_IS_NICHE]], i64 %[[A_REL_DISCR]], i64 1 | ||
// LLVM21: %[[A_MODIFIED_TAG:.+]] = select i1 %[[A_IS_NICHE]], i64 %[[A_TRUNC]], i64 6 |
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.
Wouldn't this select be at the bottom for LLVM 21?
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.
Oops, I completely missed that! Thanks.
This looks like a reasonable thing to do to me, but I'm probably the wrong person to check whether the logic is correct. The surrounding niche/discriminant logic looks kinda tricky. |
6eb273e
to
b90e0fb
Compare
r? codegen |
if niche_variants.contains(&untagged_variant) | ||
&& bx.cx().sess().opts.optimize != OptLevel::No | ||
{ | ||
let ne = bx.icmp(IntPredicate::IntNE, tagged_discr, untagged_variant_const); |
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.
In case this helps review, here's how you can see this just from the diff, rather than from https://doc.rust-lang.org/nightly/nightly-rustc/rustc_abi/enum.TagEncoding.html#variant.Niche:
Previously we were doing tagged_discr != untagged_variant
here.
But
relative_discr = tag - niche_start
delta = niche_variants.start()
tagged_discr = relative_discr + delta
so
tagged_discr != untagged_variant
=> relative_discr + delta != untagged_variant
=> (tag - niche_start) + niche_variants.start() != untagged_variant
=> tag != niche_start + untagged_variant - niche_variants.start()
which is the calculation on line 522.
@bors r+ |
Address the issue mentioned in llvm/llvm-project#134024 (comment) by changing discriminant calculation to
assume
on the originally-loadedtag
, rather than oncast(tag)-OFFSET
.The previous way does make the purpose of the assume clearer, IMHO, since you see
assume(x != 4); if p { x } else { 4 }
, but doing it this way instead means that theadd
s optimize away in LLVM21, which is more important. And this new way is still easily thought of as being like metadata on the load saying specifically which value is impossible.Demo of the LLVM20 vs LLVM21 difference: https://llvm.godbolt.org/z/n54x5Mq1T
r? @nikic