-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Missed optimization when using const-blocks for MaybeUninit arrays #138625
Comments
As with most such examples, you can get the codegen you want with more const: #[unsafe(no_mangle)]
pub fn uninit_arr_via_const() -> SmallVec<String> {
SmallVec { len: 0, arr: const { [const { MaybeUninit::uninit() }; 24] } }
} So it's nice that works, but I'll try to fix this properly. |
I'm not tagging this as LLVM because the bug here is our bug. We are giving the backend a loop for the assignment of |
Lower to a no-op when Rvalue::Repeat repeats uninit Fixes rust-lang#138625 r? oli-obk
…,oli-obk Lower to a memset(undef) when Rvalue::Repeat repeats uninit Fixes rust-lang#138625. It is technically correct to just do nothing. But if we actually do nothing, we may miss that this is de-initializing something, so instead we just lower to a single memset that writes undef. This is still superior to the memcpy loop, in both quality of code we hand to the backend and LLVM's final output.
Lower to a memset(undef) when Rvalue::Repeat repeats uninit Fixes rust-lang/rust#138625. It is technically correct to just do nothing. But if we actually do nothing, we may miss that this is de-initializing something, so instead we just lower to a single memset that writes undef. This is still superior to the memcpy loop, in both quality of code we hand to the backend and LLVM's final output.
@saethlin I'm truly impressed by your swiftness and precision. The fix works like a charm! Just wanted to share my appreciation, keep it up! 😊 |
Thanks. Some issues like this one are refreshing to work on. |
Please see the assembly from these functions (Rust 1.85.0
--release
):The
const {}
variant compiles with code that seems to zero-out thearr
field. This should not be necessary and the other version (correctly) only initializes thelen
field.This is particularly troublesome because the documentation in multiple places expresses that const-blocks are the preferred way to create a
[MaybeUnint; _]
.The text was updated successfully, but these errors were encountered: