Skip to content

Commit e9f29a8

Browse files
committed
Auto merge of rust-lang#89030 - nbdd0121:box2, r=jonas-schievink
Introduce `Rvalue::ShallowInitBox` Polished version of rust-lang#88700. Implements MCP rust-lang/compiler-team#460, and should allow rust-lang#43596 to go forward. In short, creating an empty box is split from a nullary-op `NullOp::Box` into two steps, first a call to `exchange_malloc`, then a `Rvalue::ShallowInitBox` which transmutes `*mut u8` to a shallow-initialized `Box<T>`. This allows the `exchange_malloc` call to unwind. Details can be found in the MCP. `NullOp::Box` is not yet removed, purely to make reverting easier in case anything goes wrong as the result of this PR. If revert is needed a reversion of "Use Rvalue::ShallowInitBox for box expression" commit followed by a test bless should be sufficient. Experiments in rust-lang#88700 showed a very slight compile-time perf regression due to (supposedly) slightly more time spent in LLVM. We could omit unwind edge generation (in non-`oom=panic` case) in box expression MIR construction to restore perf; but I don't think it's necessary since runtime perf isn't affected and perf difference is rather small.
2 parents 218a96c + ab64580 commit e9f29a8

File tree

31 files changed

+468
-227
lines changed

31 files changed

+468
-227
lines changed

compiler/rustc_borrowck/src/invalidation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
316316
Rvalue::Use(ref operand)
317317
| Rvalue::Repeat(ref operand, _)
318318
| Rvalue::UnaryOp(_ /*un_op*/, ref operand)
319-
| Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/) => {
319+
| Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/)
320+
| Rvalue::ShallowInitBox(ref operand, _ /*ty*/) => {
320321
self.consume_operand(location, operand)
321322
}
322323

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13611361
Rvalue::Use(ref operand)
13621362
| Rvalue::Repeat(ref operand, _)
13631363
| Rvalue::UnaryOp(_ /*un_op*/, ref operand)
1364-
| Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/) => {
1364+
| Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/)
1365+
| Rvalue::ShallowInitBox(ref operand, _ /*ty*/) => {
13651366
self.consume_operand(location, (operand, span), flow_state)
13661367
}
13671368

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,13 +2024,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20242024
}
20252025
}
20262026

2027-
Rvalue::NullaryOp(_, ty) => {
2028-
// Even with unsized locals cannot box an unsized value.
2029-
if self.unsized_feature_enabled() {
2030-
let span = body.source_info(location).span;
2031-
self.ensure_place_sized(ty, span);
2032-
}
2033-
2027+
Rvalue::NullaryOp(_, ty) | Rvalue::ShallowInitBox(_, ty) => {
20342028
let trait_ref = ty::TraitRef {
20352029
def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
20362030
substs: tcx.mk_substs_trait(ty, &[]),
@@ -2363,6 +2357,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
23632357
| Rvalue::AddressOf(..)
23642358
| Rvalue::Len(..)
23652359
| Rvalue::Cast(..)
2360+
| Rvalue::ShallowInitBox(..)
23662361
| Rvalue::BinaryOp(..)
23672362
| Rvalue::CheckedBinaryOp(..)
23682363
| Rvalue::NullaryOp(..)

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,13 @@ fn codegen_stmt<'tcx>(
701701
let len = codegen_array_len(fx, place);
702702
lval.write_cvalue(fx, CValue::by_val(len, usize_layout));
703703
}
704+
Rvalue::ShallowInitBox(ref operand, content_ty) => {
705+
let content_ty = fx.monomorphize(content_ty);
706+
let box_layout = fx.layout_of(fx.tcx.mk_box(content_ty));
707+
let operand = codegen_operand(fx, operand);
708+
let operand = operand.load_scalar(fx);
709+
lval.write_cvalue(fx, CValue::by_val(operand, box_layout));
710+
}
704711
Rvalue::NullaryOp(NullOp::Box, content_ty) => {
705712
let usize_type = fx.clif_type(fx.tcx.types.usize).unwrap();
706713
let content_ty = fx.monomorphize(content_ty);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,18 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
550550
OperandRef::new_zst(&mut bx, self.cx.layout_of(self.monomorphize(ty)));
551551
(bx, operand)
552552
}
553+
mir::Rvalue::ShallowInitBox(ref operand, content_ty) => {
554+
let operand = self.codegen_operand(&mut bx, operand);
555+
let lloperand = operand.immediate();
556+
557+
let content_ty = self.monomorphize(content_ty);
558+
let box_layout = bx.cx().layout_of(bx.tcx().mk_box(content_ty));
559+
let llty_ptr = bx.cx().backend_type(box_layout);
560+
561+
let val = bx.pointercast(lloperand, llty_ptr);
562+
let operand = OperandRef { val: OperandValue::Immediate(val), layout: box_layout };
563+
(bx, operand)
564+
}
553565
}
554566
}
555567

@@ -763,6 +775,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
763775
mir::Rvalue::AddressOf(..) |
764776
mir::Rvalue::Len(..) |
765777
mir::Rvalue::Cast(..) | // (*)
778+
mir::Rvalue::ShallowInitBox(..) | // (*)
766779
mir::Rvalue::BinaryOp(..) |
767780
mir::Rvalue::CheckedBinaryOp(..) |
768781
mir::Rvalue::UnaryOp(..) |

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
289289
self.write_scalar(Scalar::from_machine_usize(val, self), &dest)?;
290290
}
291291

292+
ShallowInitBox(ref operand, _) => {
293+
let src = self.eval_operand(operand, None)?;
294+
let v = self.read_immediate(&src)?;
295+
self.write_immediate(*v, &dest)?;
296+
}
297+
292298
Cast(cast_kind, ref operand, cast_ty) => {
293299
let src = self.eval_operand(operand, None)?;
294300
let cast_ty = self.subst_from_current_frame_and_normalize_erasing_regions(cast_ty);

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
650650

651651
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) => {}
652652
Rvalue::NullaryOp(NullOp::Box, _) => self.check_op(ops::HeapAllocation),
653+
Rvalue::ShallowInitBox(_, _) => {}
653654

654655
Rvalue::UnaryOp(_, ref operand) => {
655656
let ty = operand.ty(self.body, self.tcx);
@@ -912,6 +913,11 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
912913
return;
913914
}
914915

916+
if Some(callee) == tcx.lang_items().exchange_malloc_fn() {
917+
self.check_op(ops::HeapAllocation);
918+
return;
919+
}
920+
915921
// `async` blocks get lowered to `std::future::from_generator(/* a closure */)`.
916922
let is_async_block = Some(callee) == tcx.lang_items().from_generator_fn();
917923
if is_async_block {

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ where
206206
Rvalue::Use(operand)
207207
| Rvalue::Repeat(operand, _)
208208
| Rvalue::UnaryOp(_, operand)
209-
| Rvalue::Cast(_, operand, _) => in_operand::<Q, _>(cx, in_local, operand),
209+
| Rvalue::Cast(_, operand, _)
210+
| Rvalue::ShallowInitBox(operand, _) => in_operand::<Q, _>(cx, in_local, operand),
210211

211212
Rvalue::BinaryOp(_, box (lhs, rhs)) | Rvalue::CheckedBinaryOp(_, box (lhs, rhs)) => {
212213
in_operand::<Q, _>(cx, in_local, lhs) || in_operand::<Q, _>(cx, in_local, rhs)

compiler/rustc_const_eval/src/transform/promote_consts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ impl<'tcx> Validator<'_, 'tcx> {
523523
NullOp::AlignOf => {}
524524
},
525525

526+
Rvalue::ShallowInitBox(_, _) => return Err(Unpromotable),
527+
526528
Rvalue::UnaryOp(op, operand) => {
527529
match op {
528530
// These operations can never fail.

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,12 @@ pub enum Rvalue<'tcx> {
22002200
/// that `Foo` has a destructor. These rvalues can be optimized
22012201
/// away after type-checking and before lowering.
22022202
Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
2203+
2204+
/// Transmutes a `*mut u8` into shallow-initialized `Box<T>`.
2205+
///
2206+
/// This is different a normal transmute because dataflow analysis will treat the box
2207+
/// as initialized but its content as uninitialized.
2208+
ShallowInitBox(Operand<'tcx>, Ty<'tcx>),
22032209
}
22042210

22052211
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
@@ -2450,6 +2456,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24502456
}),
24512457
}
24522458
}
2459+
2460+
ShallowInitBox(ref place, ref ty) => {
2461+
write!(fmt, "ShallowInitBox({:?}, {:?})", place, ty)
2462+
}
24532463
}
24542464
}
24552465
}

0 commit comments

Comments
 (0)