-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Missing optimization when transmuting enums #109958
Comments
Godbolt: https://rust.godbolt.org/z/ExbzPGf7s cc @scottmcm in case your scalar transmute changes are relevant. Not an LLVM problem because the necessary information is never provided to LLVM. |
rust/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs Lines 267 to 268 in 90a9f69
We emit a couple of assume 's when casting an enum with as but no extra info is emitted for a transmute
|
If we could just put range metadata on arguments, we could emit define noundef i32 @f(i32 noundef range(!0) %x)
...
!0 = !{i32 0, i32 1} and then this and other cases (e.g. I actually have a partial implementation of this here. Though I assume allowing attributes to depend on metadata nodes would be untenable, they're probably separate intentionally? (@nikic thoughts?) (My implementation also doesn't fully work because of this problem with RAUW on forward-declared metadata: erikdesjardins/llvm-project@c1342ac#diff-100c1b4a714c7d260fc41ac27cc8d2d5857d54021f16d73debc71125d110e7daR2809-R2831) The alternative would be something like (Of course the other alternative would be to just add the assumes, but it feels a bit unsatisfying) |
If you do this directly to the Hmm, playground is only on the 2023-04-02 nightly, so I can't look there to see if my change improved things. |
With #109843, this: // CHECK-LABEL: @check_to_enum(
#[no_mangle]
pub unsafe fn check_to_enum(x: i8) -> SmallEnum {
transmute(x)
}
// CHECK-LABEL: @check_from_enum(
#[no_mangle]
pub unsafe fn check_from_enum(x: SmallEnum) -> i8 {
transmute(x)
} just emits ; Function Attrs: uwtable
define noundef i8 @check_to_enum(i8 noundef %x) unnamed_addr #0 {
start:
ret i8 %x
}
; Function Attrs: uwtable
define noundef i8 @check_from_enum(i8 noundef %x) unnamed_addr #0 {
start:
ret i8 %x
} since it's passed as an immediate. Loading an enum puts |
`assume` value ranges in `transmute` Fixes rust-lang#109958
Transmuting a fieldless enum to it's integer type causes the optimizer to lose track of it's possible values. Note that
as
casts are optimized correctly.Example:
cc Lokathor/bytemuck#175
The text was updated successfully, but these errors were encountered: