-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add OperandValue::Uninit to improve lowering of MaybeUninit::uninit #142837
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?
Changes from all commits
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 |
---|---|---|
|
@@ -67,9 +67,14 @@ pub enum OperandValue<V> { | |
/// `is_zst` on its `Layout` returns `true`. Note however that | ||
/// these values can still require alignment. | ||
ZeroSized, | ||
Uninit, | ||
} | ||
|
||
impl<V: CodegenObject> OperandValue<V> { | ||
pub(crate) fn is_uninit(&self) -> bool { | ||
matches!(self, OperandValue::Uninit) | ||
} | ||
|
||
/// Treat this value as a pointer and return the data pointer and | ||
/// optional metadata as backend values. | ||
/// | ||
|
@@ -100,6 +105,7 @@ impl<V: CodegenObject> OperandValue<V> { | |
ty: TyAndLayout<'tcx>, | ||
) -> bool { | ||
match self { | ||
OperandValue::Uninit => true, | ||
OperandValue::ZeroSized => ty.is_zst(), | ||
OperandValue::Immediate(_) => cx.is_backend_immediate(ty), | ||
OperandValue::Pair(_, _) => cx.is_backend_scalar_pair(ty), | ||
|
@@ -144,6 +150,10 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> { | |
) -> Self { | ||
let layout = bx.layout_of(ty); | ||
|
||
if val.all_bytes_uninit(bx.tcx()) { | ||
return OperandRef { val: OperandValue::Uninit, layout }; | ||
} | ||
|
||
let val = match val { | ||
ConstValue::Scalar(x) => { | ||
let BackendRepr::Scalar(scalar) = layout.backend_repr else { | ||
|
@@ -442,6 +452,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> { | |
|
||
// Read the tag/niche-encoded discriminant from memory. | ||
let tag_op = match self.val { | ||
OperandValue::Uninit => bug!("shouldn't load from uninit"), | ||
OperandValue::ZeroSized => bug!(), | ||
OperandValue::Immediate(_) | OperandValue::Pair(_, _) => { | ||
self.extract_field(fx, bx, tag_field.as_usize()) | ||
|
@@ -591,6 +602,28 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> { | |
} | ||
|
||
impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, Result<V, abi::Scalar>> { | ||
fn update_uninit<Bx: BuilderMethods<'a, 'tcx, Value = V>>( | ||
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. What do these 2 methods do? Could you maybe write a brief doc comment for each? |
||
bx: &mut Bx, | ||
tgt: &mut Result<V, abi::Scalar>, | ||
) { | ||
let to_scalar = tgt.unwrap_err(); | ||
let bty = bx.cx().type_from_scalar(to_scalar); | ||
*tgt = Ok(bx.const_undef(bty)); | ||
} | ||
|
||
fn update<Bx: BuilderMethods<'a, 'tcx, Value = V>>( | ||
bx: &mut Bx, | ||
tgt: &mut Result<V, abi::Scalar>, | ||
src: V, | ||
from_scalar: rustc_abi::Scalar, | ||
) { | ||
let from_bty = bx.cx().type_from_scalar(from_scalar); | ||
let to_scalar = tgt.unwrap_err(); | ||
let to_bty = bx.cx().type_from_scalar(to_scalar); | ||
let imm = transmute_immediate(bx, src, from_scalar, from_bty, to_scalar, to_bty); | ||
*tgt = Ok(imm); | ||
} | ||
|
||
pub(crate) fn insert_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>( | ||
&mut self, | ||
bx: &mut Bx, | ||
|
@@ -614,37 +647,48 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, Result<V, abi::Scalar>> { | |
(field_layout.is_zst(), field_offset == Size::ZERO) | ||
}; | ||
|
||
let mut update = |tgt: &mut Result<V, abi::Scalar>, src, from_scalar| { | ||
let from_bty = bx.cx().type_from_scalar(from_scalar); | ||
let to_scalar = tgt.unwrap_err(); | ||
let to_bty = bx.cx().type_from_scalar(to_scalar); | ||
let imm = transmute_immediate(bx, src, from_scalar, from_bty, to_scalar, to_bty); | ||
*tgt = Ok(imm); | ||
}; | ||
|
||
match (operand.val, operand.layout.backend_repr) { | ||
(OperandValue::ZeroSized, _) if expect_zst => {} | ||
(OperandValue::Immediate(v), BackendRepr::Scalar(from_scalar)) => match &mut self.val { | ||
OperandValue::Immediate(val @ Err(_)) if is_zero_offset => { | ||
update(val, v, from_scalar); | ||
Self::update(bx, val, v, from_scalar); | ||
} | ||
OperandValue::Pair(fst @ Err(_), _) if is_zero_offset => { | ||
update(fst, v, from_scalar); | ||
Self::update(bx, fst, v, from_scalar); | ||
} | ||
OperandValue::Pair(_, snd @ Err(_)) if !is_zero_offset => { | ||
update(snd, v, from_scalar); | ||
Self::update(bx, snd, v, from_scalar); | ||
} | ||
_ => bug!("Tried to insert {operand:?} into {v:?}.{f:?} of {self:?}"), | ||
}, | ||
(OperandValue::Uninit, BackendRepr::Scalar(_)) => match &mut self.val { | ||
OperandValue::Immediate(val @ Err(_)) if is_zero_offset => { | ||
Self::update_uninit(bx, val); | ||
} | ||
OperandValue::Pair(fst @ Err(_), _) if is_zero_offset => { | ||
Self::update_uninit(bx, fst); | ||
} | ||
OperandValue::Pair(_, snd @ Err(_)) if !is_zero_offset => { | ||
Self::update_uninit(bx, snd); | ||
} | ||
_ => bug!("Tried to insert {operand:?} into {v:?}.{f:?} of {self:?}"), | ||
}, | ||
(OperandValue::Pair(a, b), BackendRepr::ScalarPair(from_sa, from_sb)) => { | ||
match &mut self.val { | ||
OperandValue::Pair(fst @ Err(_), snd @ Err(_)) => { | ||
update(fst, a, from_sa); | ||
update(snd, b, from_sb); | ||
Self::update(bx, fst, a, from_sa); | ||
Self::update(bx, snd, b, from_sb); | ||
} | ||
_ => bug!("Tried to insert {operand:?} into {v:?}.{f:?} of {self:?}"), | ||
} | ||
} | ||
(OperandValue::Uninit, BackendRepr::ScalarPair(..)) => match &mut self.val { | ||
OperandValue::Pair(fst @ Err(_), snd @ Err(_)) => { | ||
Self::update_uninit(bx, fst); | ||
Self::update_uninit(bx, snd); | ||
} | ||
_ => bug!("Tried to insert {operand:?} into {v:?}.{f:?} of {self:?}"), | ||
}, | ||
_ => bug!("Unsupported operand {operand:?} inserting into {v:?}.{f:?} of {self:?}"), | ||
} | ||
} | ||
|
@@ -663,6 +707,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, Result<V, abi::Scalar>> { | |
}; | ||
|
||
let val = match val { | ||
OperandValue::Uninit => OperandValue::Uninit, | ||
OperandValue::ZeroSized => OperandValue::ZeroSized, | ||
OperandValue::Immediate(v) => OperandValue::Immediate(unwrap(v)), | ||
OperandValue::Pair(a, b) => OperandValue::Pair(unwrap(a), unwrap(b)), | ||
|
@@ -739,6 +784,13 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue<V> { | |
) { | ||
debug!("OperandRef::store: operand={:?}, dest={:?}", self, dest); | ||
match self { | ||
OperandValue::Uninit => { | ||
// Ideally we'd hint to the backend that the destination is deinitialized by the | ||
// store. But in practice the destination is almost always uninit already because | ||
// OperandValue::Uninit is pretty much only produced by MaybeUninit::uninit. | ||
// Attempting to generate a hint by calling memset with undef mostly seems to | ||
// confuse LLVM. | ||
} | ||
OperandValue::ZeroSized => { | ||
// Avoid generating stores of zero-sized values, because the only way to have a | ||
// zero-sized value is through `undef`/`poison`, and the store itself is useless. | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -328,7 +328,10 @@ impl<T> MaybeUninit<T> { | |||||||||||||
#[inline(always)] | ||||||||||||||
#[rustc_diagnostic_item = "maybe_uninit_uninit"] | ||||||||||||||
pub const fn uninit() -> MaybeUninit<T> { | ||||||||||||||
MaybeUninit { uninit: () } | ||||||||||||||
// It is very helpful for codegen to know when are writing uninit bytes. MIR optimizations | ||||||||||||||
// currently do not const-propagate unions, but if we create the const manually that can be | ||||||||||||||
// trivially propagated. See #142837. | ||||||||||||||
Comment on lines
+331
to
+333
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.
Suggested change
|
||||||||||||||
const { MaybeUninit { uninit: () } } | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Creates a new `MaybeUninit<T>` in an uninitialized state, with the memory being | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//@ compile-flags: -Copt-level=3 -Cdebuginfo=0 | ||
|
||
// This is a regression test for https://github.com/rust-lang/rust/issues/139355 as well as | ||
// regressions I introduced while implementing a solution. | ||
|
||
#![crate_type = "lib"] | ||
|
||
use std::mem::MaybeUninit; | ||
|
||
// CHECK-LABEL: @create_small_uninit_array | ||
#[no_mangle] | ||
fn create_small_uninit_array() -> [MaybeUninit<u8>; 4] { | ||
// CHECK-NEXT: start: | ||
// CHECK-NEXT: ret i32 undef | ||
[MaybeUninit::<u8>::uninit(); 4] | ||
} | ||
|
||
// CHECK-LABEL: @create_nested_uninit_array | ||
#[no_mangle] | ||
fn create_nested_uninit_array() -> [[MaybeUninit<u8>; 4]; 100] { | ||
// CHECK-NEXT: start: | ||
// CHECK-NEXT: ret void | ||
[[MaybeUninit::<u8>::uninit(); 4]; 100] | ||
} | ||
|
||
// CHECK-LABEL: @create_ptr | ||
#[no_mangle] | ||
fn create_ptr() -> MaybeUninit<&'static str> { | ||
// CHECK-NEXT: start: | ||
// CHECK-NEXT: ret { ptr, i64 } undef | ||
MaybeUninit::uninit() | ||
} | ||
Comment on lines
+26
to
+32
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. Note that for scalar pair cases like this I'm already fixing it in https://github.com/rust-lang/rust/pull/138759/files#diff-68480918205d32f0b23d06ba76c5fbd702b7dc842f0f5cf262db6e2e6ae3c630R51-R59 (That also handles partial uninit cases like I wonder if it's only 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. This already compiles to the |
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.
Could you add some docs here?
Uh oh!
There was an error while loading. Please reload this page.
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.
Agreed -- this is changing the "well if it's
is_backend_immediate
then it'sOperandValue::Immediate
" rule, for example, so it needs to be thought about carefully since it affects all the consumers ofOperandValue
, potentially. (And thus probably needs those comments on the other variants updated too.)For example, it's not clear to me why
Immediate(cx.const_undef(…))
wouldn't be fine as representing undef for things withis_backend_immediate
.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'm not sure what you mean by "fine". It's valid to do so even with this PR, it just doesn't fix the missed optimization. It does improve the IR that we emit, if also combined with the change to
MaybeUninit::uninit
. But all that it accomplishes is tripping over a different problem in LLVM. It also breaks the manybeuninit-nrvo codegen tests.This is the diff I applied to try out your idea: