Skip to content

Commit 855e0fe

Browse files
committed
Auto merge of #142911 - mejrs:unsized, r=compiler-errors
Remove support for dynamic allocas Followup to #141811
2 parents cdac44e + 25eb382 commit 855e0fe

File tree

16 files changed

+163
-94
lines changed

16 files changed

+163
-94
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
926926
.get_address(self.location)
927927
}
928928

929-
fn dynamic_alloca(&mut self, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
930-
unimplemented!();
931-
}
932-
933929
fn load(&mut self, pointee_ty: Type<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
934930
let block = self.llbb();
935931
let function = block.get_function();

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -538,16 +538,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
538538
}
539539
}
540540

541-
fn dynamic_alloca(&mut self, size: &'ll Value, align: Align) -> &'ll Value {
542-
unsafe {
543-
let alloca =
544-
llvm::LLVMBuildArrayAlloca(self.llbuilder, self.cx().type_i8(), size, UNNAMED);
545-
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
546-
// Cast to default addrspace if necessary
547-
llvm::LLVMBuildPointerCast(self.llbuilder, alloca, self.cx().type_ptr(), UNNAMED)
548-
}
549-
}
550-
551541
fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value {
552542
unsafe {
553543
let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,12 +1492,6 @@ unsafe extern "C" {
14921492
Ty: &'a Type,
14931493
Name: *const c_char,
14941494
) -> &'a Value;
1495-
pub(crate) fn LLVMBuildArrayAlloca<'a>(
1496-
B: &Builder<'a>,
1497-
Ty: &'a Type,
1498-
Val: &'a Value,
1499-
Name: *const c_char,
1500-
) -> &'a Value;
15011495
pub(crate) fn LLVMBuildLoad2<'a>(
15021496
B: &Builder<'a>,
15031497
Ty: &'a Type,

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,13 @@ enum LocalRef<'tcx, V> {
140140
Place(PlaceRef<'tcx, V>),
141141
/// `UnsizedPlace(p)`: `p` itself is a thin pointer (indirect place).
142142
/// `*p` is the wide pointer that references the actual unsized place.
143-
/// Every time it is initialized, we have to reallocate the place
144-
/// and update the wide pointer. That's the reason why it is indirect.
143+
///
144+
/// MIR only supports unsized args, not dynamically-sized locals, so
145+
/// new unsized temps don't exist and we must reuse the referred-to place.
146+
///
147+
/// FIXME: Since the removal of unsized locals in <https://github.com/rust-lang/rust/pull/142911>,
148+
/// can we maybe use `Place` here? Or refactor it in another way? There are quite a few
149+
/// `UnsizedPlace => bug` branches now.
145150
UnsizedPlace(PlaceRef<'tcx, V>),
146151
/// The backend [`OperandValue`] has already been generated.
147152
Operand(OperandRef<'tcx, V>),
@@ -498,7 +503,7 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
498503
LocalRef::Place(PlaceRef::new_sized(llarg, arg.layout))
499504
}
500505
}
501-
// Unsized indirect qrguments
506+
// Unsized indirect arguments
502507
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
503508
// As the storage for the indirect argument lives during
504509
// the whole function call, we just copy the wide pointer.

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use tracing::{debug, instrument};
1616
use super::place::{PlaceRef, PlaceValue};
1717
use super::rvalue::transmute_scalar;
1818
use super::{FunctionCx, LocalRef};
19+
use crate::MemFlags;
1920
use crate::common::IntPredicate;
2021
use crate::traits::*;
21-
use crate::{MemFlags, size_of_val};
2222

2323
/// The representation of a Rust value. The enum variant is in fact
2424
/// uniquely determined by the value's type, but is kept as a
@@ -861,44 +861,6 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
861861
}
862862
}
863863
}
864-
865-
pub fn store_unsized<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
866-
self,
867-
bx: &mut Bx,
868-
indirect_dest: PlaceRef<'tcx, V>,
869-
) {
870-
debug!("OperandRef::store_unsized: operand={:?}, indirect_dest={:?}", self, indirect_dest);
871-
// `indirect_dest` must have `*mut T` type. We extract `T` out of it.
872-
let unsized_ty = indirect_dest
873-
.layout
874-
.ty
875-
.builtin_deref(true)
876-
.unwrap_or_else(|| bug!("indirect_dest has non-pointer type: {:?}", indirect_dest));
877-
878-
let OperandValue::Ref(PlaceValue { llval: llptr, llextra: Some(llextra), .. }) = self
879-
else {
880-
bug!("store_unsized called with a sized value (or with an extern type)")
881-
};
882-
883-
// Allocate an appropriate region on the stack, and copy the value into it. Since alloca
884-
// doesn't support dynamic alignment, we allocate an extra align - 1 bytes, and align the
885-
// pointer manually.
886-
let (size, align) = size_of_val::size_and_align_of_dst(bx, unsized_ty, Some(llextra));
887-
let one = bx.const_usize(1);
888-
let align_minus_1 = bx.sub(align, one);
889-
let size_extra = bx.add(size, align_minus_1);
890-
let min_align = Align::ONE;
891-
let alloca = bx.dynamic_alloca(size_extra, min_align);
892-
let address = bx.ptrtoint(alloca, bx.type_isize());
893-
let neg_address = bx.neg(address);
894-
let offset = bx.and(neg_address, align_minus_1);
895-
let dst = bx.inbounds_ptradd(alloca, offset);
896-
bx.memcpy(dst, min_align, llptr, min_align, size, MemFlags::empty());
897-
898-
// Store the allocated region and the extra to the indirect place.
899-
let indirect_operand = OperandValue::Pair(dst, llextra);
900-
indirect_operand.store(bx, indirect_dest);
901-
}
902864
}
903865

904866
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -327,27 +327,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
327327
Some(imm)
328328
}
329329

330-
pub(crate) fn codegen_rvalue_unsized(
331-
&mut self,
332-
bx: &mut Bx,
333-
indirect_dest: PlaceRef<'tcx, Bx::Value>,
334-
rvalue: &mir::Rvalue<'tcx>,
335-
) {
336-
debug!(
337-
"codegen_rvalue_unsized(indirect_dest.llval={:?}, rvalue={:?})",
338-
indirect_dest.val.llval, rvalue
339-
);
340-
341-
match *rvalue {
342-
mir::Rvalue::Use(ref operand) => {
343-
let cg_operand = self.codegen_operand(bx, operand);
344-
cg_operand.val.store_unsized(bx, indirect_dest);
345-
}
346-
347-
_ => bug!("unsized assignment other than `Rvalue::Use`"),
348-
}
349-
}
350-
351330
pub(crate) fn codegen_rvalue_operand(
352331
&mut self,
353332
bx: &mut Bx,

compiler/rustc_codegen_ssa/src/mir/statement.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1515
match self.locals[index] {
1616
LocalRef::Place(cg_dest) => self.codegen_rvalue(bx, cg_dest, rvalue),
1717
LocalRef::UnsizedPlace(cg_indirect_dest) => {
18-
self.codegen_rvalue_unsized(bx, cg_indirect_dest, rvalue)
18+
let ty = cg_indirect_dest.layout.ty;
19+
span_bug!(
20+
statement.source_info.span,
21+
"cannot reallocate from `UnsizedPlace({ty})` \
22+
into `{rvalue:?}`; dynamic alloca is not supported",
23+
);
1924
}
2025
LocalRef::PendingOperand => {
2126
let operand = self.codegen_rvalue_operand(bx, rvalue);

compiler/rustc_codegen_ssa/src/traits/builder.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ pub trait BuilderMethods<'a, 'tcx>:
224224
fn to_immediate_scalar(&mut self, val: Self::Value, scalar: Scalar) -> Self::Value;
225225

226226
fn alloca(&mut self, size: Size, align: Align) -> Self::Value;
227-
fn dynamic_alloca(&mut self, size: Self::Value, align: Align) -> Self::Value;
228227

229228
fn load(&mut self, ty: Self::Type, ptr: Self::Value, align: Align) -> Self::Value;
230229
fn volatile_load(&mut self, ty: Self::Type, ptr: Self::Value) -> Self::Value;

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
241241
arg_expr.span,
242242
ObligationCauseCode::WellFormed(None),
243243
);
244+
245+
self.check_place_expr_if_unsized(fn_input_ty, arg_expr);
244246
}
245247

246248
// First, let's unify the formal method signature with the expectation eagerly.
@@ -543,6 +545,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
543545
}
544546
}
545547

548+
/// If `unsized_fn_params` is active, check that unsized values are place expressions. Since
549+
/// the removal of `unsized_locals` in <https://github.com/rust-lang/rust/pull/142911> we can't
550+
/// store them in MIR locals as temporaries.
551+
///
552+
/// If `unsized_fn_params` is inactive, this will be checked in borrowck instead.
553+
fn check_place_expr_if_unsized(&self, ty: Ty<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
554+
if self.tcx.features().unsized_fn_params() && !expr.is_syntactic_place_expr() {
555+
self.require_type_is_sized(
556+
ty,
557+
expr.span,
558+
ObligationCauseCode::UnsizedNonPlaceExpr(expr.span),
559+
);
560+
}
561+
}
562+
546563
fn report_arg_errors(
547564
&self,
548565
compatibility_diagonal: IndexVec<ProvidedIdx, Compatibility<'tcx>>,
@@ -1873,7 +1890,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18731890
});
18741891
}
18751892
hir::StmtKind::Semi(expr) => {
1876-
self.check_expr(expr);
1893+
let ty = self.check_expr(expr);
1894+
self.check_place_expr_if_unsized(ty, expr);
18771895
}
18781896
}
18791897

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ pub enum ObligationCauseCode<'tcx> {
412412

413413
/// Obligations emitted during the normalization of a free type alias.
414414
TypeAlias(ObligationCauseCodeHandle<'tcx>, Span, DefId),
415+
416+
/// Only reachable if the `unsized_fn_params` feature is used. Unsized function arguments must
417+
/// be place expressions because we can't store them in MIR locals as temporaries.
418+
UnsizedNonPlaceExpr(Span),
415419
}
416420

417421
/// Whether a value can be extracted into a const.

0 commit comments

Comments
 (0)