Skip to content

Commit e6eb401

Browse files
committed
Allow Rvalue::Repeat to return true in rvalue_creates_operand too
The conversation in 143502 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. I'll try out such a world next :)
1 parent 32cd911 commit e6eb401

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
@@ -649,7 +649,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
649649
OperandRef { val: OperandValue::Immediate(static_), layout }
650650
}
651651
mir::Rvalue::Use(ref operand) => self.codegen_operand(bx, operand),
652-
mir::Rvalue::Repeat(..) => bug!("{rvalue:?} in codegen_rvalue_operand"),
652+
mir::Rvalue::Repeat(ref elem, len_const) => {
653+
// All arrays have `BackendRepr::Memory`, so only the ZST cases
654+
// end up here. Anything else forces the destination local to be
655+
// `Memory`, and thus ends up handled in `codegen_rvalue` instead.
656+
let operand = self.codegen_operand(bx, elem);
657+
let array_ty = Ty::new_array_with_const_len(bx.tcx(), operand.layout.ty, len_const);
658+
let array_ty = self.monomorphize(array_ty);
659+
let array_layout = bx.layout_of(array_ty);
660+
assert!(array_layout.is_zst());
661+
OperandRef { val: OperandValue::ZeroSized, layout: array_layout }
662+
},
653663
mir::Rvalue::Aggregate(ref kind, ref fields) => {
654664
let (variant_index, active_field_index) = match **kind {
655665
mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
@@ -1019,12 +1029,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10191029
mir::Rvalue::NullaryOp(..) |
10201030
mir::Rvalue::ThreadLocalRef(_) |
10211031
mir::Rvalue::Use(..) |
1032+
mir::Rvalue::Repeat(..) | // (*)
10221033
mir::Rvalue::Aggregate(..) | // (*)
10231034
mir::Rvalue::WrapUnsafeBinder(..) => // (*)
10241035
true,
1025-
// Arrays are always aggregates, so it's not worth checking anything here.
1026-
// (If it's really `[(); N]` or `[T; 0]` and we use the place path, fine.)
1027-
mir::Rvalue::Repeat(..) => false,
10281036
}
10291037

10301038
// (*) 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)