Skip to content

Commit 6e34369

Browse files
committed
[mir-opt] simplify Repeats that don't actually repeat the operand
1 parent eb54a50 commit 6e34369

7 files changed

+102
-23
lines changed

compiler/rustc_mir_transform/src/instsimplify.rs

+13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
4949
ctx.simplify_ptr_aggregate(rvalue);
5050
ctx.simplify_cast(rvalue);
5151
ctx.simplify_repeated_aggregate(rvalue);
52+
ctx.simplify_repeat_once(rvalue);
5253
}
5354
_ => {}
5455
}
@@ -207,6 +208,18 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
207208
}
208209
}
209210

211+
/// Simplify `[x; 1]` to just `[x]`.
212+
fn simplify_repeat_once(&self, rvalue: &mut Rvalue<'tcx>) {
213+
if let Rvalue::Repeat(operand, count) = rvalue
214+
&& let Some(1) = count.try_to_target_usize(self.tcx)
215+
{
216+
*rvalue = Rvalue::Aggregate(
217+
Box::new(AggregateKind::Array(operand.ty(self.local_decls, self.tcx))),
218+
[operand.clone()].into(),
219+
);
220+
}
221+
}
222+
210223
fn simplify_primitive_clone(
211224
&self,
212225
terminator: &mut Terminator<'tcx>,

compiler/rustc_mir_transform/src/remove_zsts.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,37 @@ struct Replacer<'a, 'tcx> {
3636
}
3737

3838
/// A cheap, approximate check to avoid unnecessary `layout_of` calls.
39-
fn maybe_zst(ty: Ty<'_>) -> bool {
39+
///
40+
/// `Some(true)` is definitely ZST; `Some(false)` is definitely *not* ZST.
41+
///
42+
/// `None` may or may not be, and must check `layout_of` to be sure.
43+
fn trivially_zst<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<bool> {
4044
match ty.kind() {
45+
// definitely ZST
46+
ty::FnDef(..) | ty::Never => Some(true),
47+
ty::Tuple(fields) if fields.is_empty() => Some(true),
48+
ty::Array(_ty, len) if let Some(0) = len.try_to_target_usize(tcx) => Some(true),
4149
// maybe ZST (could be more precise)
4250
ty::Adt(..)
4351
| ty::Array(..)
4452
| ty::Closure(..)
4553
| ty::CoroutineClosure(..)
4654
| ty::Tuple(..)
47-
| ty::Alias(ty::Opaque, ..) => true,
48-
// definitely ZST
49-
ty::FnDef(..) | ty::Never => true,
55+
| ty::Alias(ty::Opaque, ..) => None,
5056
// unreachable or can't be ZST
51-
_ => false,
57+
_ => Some(false),
5258
}
5359
}
5460

5561
impl<'tcx> Replacer<'_, 'tcx> {
5662
fn known_to_be_zst(&self, ty: Ty<'tcx>) -> bool {
57-
if !maybe_zst(ty) {
58-
return false;
63+
if let Some(is_zst) = trivially_zst(ty, self.tcx) {
64+
is_zst
65+
} else {
66+
self.tcx
67+
.layout_of(self.typing_env.as_query_input(ty))
68+
.is_ok_and(|layout| layout.is_zst())
5969
}
60-
let Ok(layout) = self.tcx.layout_of(self.typing_env.as_query_input(ty)) else {
61-
return false;
62-
};
63-
layout.is_zst()
6470
}
6571

6672
fn make_zst(&self, ty: Ty<'tcx>) -> ConstOperand<'tcx> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
- // MIR for `repeat_once_to_aggregate` before InstSimplify-after-simplifycfg
2+
+ // MIR for `repeat_once_to_aggregate` after InstSimplify-after-simplifycfg
3+
4+
fn repeat_once_to_aggregate(_1: T) -> [T; 1] {
5+
debug x => _1;
6+
let mut _0: [T; 1];
7+
let mut _2: T;
8+
9+
bb0: {
10+
StorageLive(_2);
11+
_2 = copy _1;
12+
- _0 = [move _2; 1];
13+
+ _0 = [move _2];
14+
StorageDead(_2);
15+
return;
16+
}
17+
}
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ test-mir-pass: InstSimplify-after-simplifycfg
2+
//@ compile-flags: -C panic=abort
3+
#![crate_type = "lib"]
4+
5+
// EMIT_MIR simplify_repeat.repeat_once_to_aggregate.InstSimplify-after-simplifycfg.diff
6+
pub fn repeat_once_to_aggregate<T: Copy>(x: T) -> [T; 1] {
7+
// CHECK-LABEL: fn repeat_once_to_aggregate(
8+
// CHECK-NOT: [move {{_[0-9]+}}; 1]
9+
// CHECK: _0 = [move {{_[0-9]+}}];
10+
// CHECK-NOT: [move {{_[0-9]+}}; 1]
11+
12+
[x; 1]
13+
}

tests/mir-opt/remove_zsts.get_union.PreCodegen.after.mir

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
- // MIR for `remove_generic_array` before RemoveZsts
2+
+ // MIR for `remove_generic_array` after RemoveZsts
3+
4+
fn remove_generic_array(_1: T) -> () {
5+
debug x => _1;
6+
let mut _0: ();
7+
let _2: [T; 0];
8+
let mut _3: T;
9+
scope 1 {
10+
- debug a => _2;
11+
+ debug a => const ZeroSized: [T; 0];
12+
}
13+
14+
bb0: {
15+
- StorageLive(_2);
16+
+ nop;
17+
StorageLive(_3);
18+
_3 = copy _1;
19+
- _2 = [];
20+
+ nop;
21+
StorageDead(_3);
22+
- _0 = const ();
23+
- StorageDead(_2);
24+
+ nop;
25+
+ nop;
26+
return;
27+
}
28+
}
29+

tests/mir-opt/remove_zsts.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
// skip-filecheck
1+
//@ test-mir-pass: RemoveZsts
2+
23
union Foo {
34
x: (),
45
y: u64,
56
}
67

78
// EMIT_MIR remove_zsts.get_union.RemoveZsts.diff
8-
// EMIT_MIR remove_zsts.get_union.PreCodegen.after.mir
99
fn get_union() -> Foo {
10+
// CHECK-LABEL: fn get_union
11+
// CHECK: _0 = Foo { x: const () };
1012
Foo { x: () }
1113
}
1214

15+
// EMIT_MIR remove_zsts.remove_generic_array.RemoveZsts.diff
16+
fn remove_generic_array<T: Copy>(x: T) {
17+
// CHECK-LABEL: fn remove_generic_array
18+
// CHECK: debug a => const ZeroSized: [T; 0];
19+
// CHECK-NOT: = [];
20+
let a = [x; 0];
21+
}
22+
1323
fn main() {
1424
get_union();
1525
}

0 commit comments

Comments
 (0)