Skip to content

Commit 38859dd

Browse files
authored
Rollup merge of rust-lang#143720 - scottmcm:rvalue-always-operand, r=lcnr
Allow `Rvalue::Repeat` to return true in `rvalue_creates_operand` too The conversation in rust-lang#143502 (comment) made be realize how easy this is to handle, since the only possibilty is ZSTs -- everything else ends up with the destination being `LocalKind::Memory` and thus doesn't call `codegen_rvalue_operand` at all. This gets us perilously close to a world where `rvalue_creates_operand` only ever returns true. (See rust-lang#143860 for more.)
2 parents 26ea00b + 45e04b8 commit 38859dd

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
630630
OperandRef { val: OperandValue::Immediate(static_), layout }
631631
}
632632
mir::Rvalue::Use(ref operand) => self.codegen_operand(bx, operand),
633-
mir::Rvalue::Repeat(..) => bug!("{rvalue:?} in codegen_rvalue_operand"),
633+
mir::Rvalue::Repeat(ref elem, len_const) => {
634+
// All arrays have `BackendRepr::Memory`, so only the ZST cases
635+
// end up here. Anything else forces the destination local to be
636+
// `Memory`, and thus ends up handled in `codegen_rvalue` instead.
637+
let operand = self.codegen_operand(bx, elem);
638+
let array_ty = Ty::new_array_with_const_len(bx.tcx(), operand.layout.ty, len_const);
639+
let array_ty = self.monomorphize(array_ty);
640+
let array_layout = bx.layout_of(array_ty);
641+
assert!(array_layout.is_zst());
642+
OperandRef { val: OperandValue::ZeroSized, layout: array_layout }
643+
}
634644
mir::Rvalue::Aggregate(ref kind, ref fields) => {
635645
let (variant_index, active_field_index) = match **kind {
636646
mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
@@ -1000,12 +1010,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10001010
mir::Rvalue::NullaryOp(..) |
10011011
mir::Rvalue::ThreadLocalRef(_) |
10021012
mir::Rvalue::Use(..) |
1013+
mir::Rvalue::Repeat(..) | // (*)
10031014
mir::Rvalue::Aggregate(..) | // (*)
10041015
mir::Rvalue::WrapUnsafeBinder(..) => // (*)
10051016
true,
1006-
// Arrays are always aggregates, so it's not worth checking anything here.
1007-
// (If it's really `[(); N]` or `[T; 0]` and we use the place path, fine.)
1008-
mir::Rvalue::Repeat(..) => false,
10091017
}
10101018

10111019
// (*) this is only true if the type is suitable

tests/codegen/repeat-operand.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ compile-flags: -Copt-level=1 -Cno-prepopulate-passes
2+
3+
// This test is here to hit the `Rvalue::Repeat` case in `codegen_rvalue_operand`.
4+
// It only applies when the resulting array is a ZST, so the test is written in
5+
// such a way as to keep MIR optimizations from seeing that fact and removing
6+
// the local and statement altogether. (At the time of writing, no other codegen
7+
// test hit that code path, nor did a stage 2 build of the compiler.)
8+
9+
#![crate_type = "lib"]
10+
11+
#[repr(transparent)]
12+
pub struct Wrapper<T, const N: usize>([T; N]);
13+
14+
// CHECK-LABEL: define {{.+}}do_repeat{{.+}}()
15+
// CHECK-NEXT: start:
16+
// CHECK-NOT: alloca
17+
// CHECK-NEXT: ret void
18+
// CHECK-LABEL: define {{.+}}do_repeat{{.+}}(i32 noundef %x)
19+
// CHECK-NEXT: start:
20+
// CHECK-NOT: alloca
21+
// CHECK-NEXT: ret void
22+
#[inline(never)]
23+
pub fn do_repeat<T: Copy, const N: usize>(x: T) -> Wrapper<T, N> {
24+
Wrapper([x; N])
25+
}
26+
27+
// CHECK-LABEL: @trigger_repeat_zero_len
28+
#[no_mangle]
29+
pub fn trigger_repeat_zero_len() -> Wrapper<u32, 0> {
30+
// CHECK: call void {{.+}}do_repeat{{.+}}(i32 noundef 4)
31+
do_repeat(4)
32+
}
33+
34+
// CHECK-LABEL: @trigger_repeat_zst
35+
#[no_mangle]
36+
pub fn trigger_repeat_zst() -> Wrapper<(), 8> {
37+
// CHECK: call void {{.+}}do_repeat{{.+}}()
38+
do_repeat(())
39+
}

0 commit comments

Comments
 (0)