Skip to content

Commit a275a8f

Browse files
committed
gvn: Allow evaluating rvalue operands
This reverts 8093f11. This makes nested arrays and operands evaluable to const.
1 parent 8672700 commit a275a8f

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

compiler/rustc_mir_transform/src/gvn.rs

-5
Original file line numberDiff line numberDiff line change
@@ -778,11 +778,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
778778
}
779779
Operand::Copy(ref mut place) | Operand::Move(ref mut place) => {
780780
let value = self.simplify_place_value(place, location)?;
781-
// NOTE(tesuji): We just want to promote rvalues of array kind that assigned to a local, and
782-
// not evaluate this local as a const operand of a function to avoid bloating MIR.
783-
if let Value::Aggregate(AggregateTy::Array, ..) = self.get(value) {
784-
return None;
785-
}
786781
if let Some(const_) = self.try_as_constant(value) {
787782
*operand = Operand::Constant(Box::new(const_));
788783
}

tests/mir-opt/const_array_locals.main.GVN.diff

+16-8
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@
4949
+ _4 = const [178_i32, 9_i32, 4_i32, 56_i32, 221_i32];
5050
StorageLive(_5);
5151
- _5 = [const 193_i32, const 164_i32, const 194_i32, const 197_i32, const 6_i32];
52+
- _3 = [move _4, move _5];
5253
+ _5 = const [193_i32, 164_i32, 194_i32, 197_i32, 6_i32];
53-
_3 = [move _4, move _5];
54+
+ _3 = const [[178_i32, 9_i32, 4_i32, 56_i32, 221_i32], [193_i32, 164_i32, 194_i32, 197_i32, 6_i32]];
5455
StorageDead(_5);
5556
StorageDead(_4);
5657
StorageLive(_6);
@@ -63,8 +64,9 @@
6364
StorageLive(_9);
6465
StorageLive(_10);
6566
- _10 = [const 31_u32, const 96_u32, const 173_u32, const 50_u32, const 1_u32];
67+
- _9 = consume(move _10) -> [return: bb1, unwind continue];
6668
+ _10 = const [31_u32, 96_u32, 173_u32, 50_u32, 1_u32];
67-
_9 = consume(move _10) -> [return: bb1, unwind continue];
69+
+ _9 = consume(const [31_u32, 96_u32, 173_u32, 50_u32, 1_u32]) -> [return: bb1, unwind continue];
6870
}
6971

7072
bb1: {
@@ -73,8 +75,9 @@
7375
StorageLive(_11);
7476
StorageLive(_12);
7577
- _12 = [const 1f32, const 2f32, const 3f32, const 1f32, const 1f32, const 1f32, const 1f32, const 42f32];
78+
- _11 = F32x8(move _12);
7679
+ _12 = const [1f32, 2f32, 3f32, 1f32, 1f32, 1f32, 1f32, 42f32];
77-
_11 = F32x8(move _12);
80+
+ _11 = F32x8(const [1f32, 2f32, 3f32, 1f32, 1f32, 1f32, 1f32, 42f32]);
7881
StorageDead(_12);
7982
StorageLive(_13);
8083
StorageLive(_14);
@@ -85,8 +88,9 @@
8588
+ _15 = const [0_i32, 1_i32, 0_i32];
8689
StorageLive(_16);
8790
- _16 = [const 0_i32, const 0_i32, const 1_i32];
91+
- _13 = [move _14, move _15, move _16];
8892
+ _16 = const [0_i32, 0_i32, 1_i32];
89-
_13 = [move _14, move _15, move _16];
93+
+ _13 = const [[1_i32, 0_i32, 0_i32], [0_i32, 1_i32, 0_i32], [0_i32, 0_i32, 1_i32]];
9094
StorageDead(_16);
9195
StorageDead(_15);
9296
StorageDead(_14);
@@ -101,21 +105,25 @@
101105
}
102106
}
103107
+
104-
+ ALLOC0 (size: 12, align: 4) { .. }
108+
+ ALLOC0 (size: 36, align: 4) { .. }
105109
+
106110
+ ALLOC1 (size: 12, align: 4) { .. }
107111
+
108112
+ ALLOC2 (size: 12, align: 4) { .. }
109113
+
110-
+ ALLOC3 (size: 32, align: 4) { .. }
114+
+ ALLOC3 (size: 12, align: 4) { .. }
111115
+
112-
+ ALLOC4 (size: 20, align: 4) { .. }
116+
+ ALLOC4 (size: 32, align: 4) { .. }
113117
+
114118
+ ALLOC5 (size: 20, align: 4) { .. }
115119
+
116-
+ ALLOC6 (size: 20, align: 4) { .. }
120+
+ ALLOC6 (size: 40, align: 4) { .. }
117121
+
118122
+ ALLOC7 (size: 20, align: 4) { .. }
119123
+
120124
+ ALLOC8 (size: 20, align: 4) { .. }
125+
+
126+
+ ALLOC9 (size: 20, align: 4) { .. }
127+
+
128+
+ ALLOC10 (size: 20, align: 4) { .. }
121129

tests/mir-opt/const_array_locals.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ pub fn main() {
1919
let _duplicated_arr = [255, 105, 15, 39, 62];
2020
// CHECK: [[subarray1:_[0-9]+]] = const [178_i32, 9_i32, 4_i32, 56_i32, 221_i32];
2121
// CHECK: [[subarray2:_[0-9]+]] = const [193_i32, 164_i32, 194_i32, 197_i32, 6_i32];
22-
// CHECK: [[_foo]] = [move [[subarray1]], move [[subarray2]]];
22+
// CHECK{LITERAL}: const [[178_i32, 9_i32, 4_i32, 56_i32, 221_i32], [193_i32, 164_i32, 194_i32, 197_i32, 6_i32]];
2323
let _foo = [[178, 9, 4, 56, 221], [193, 164, 194, 197, 6]];
2424
// CHECK: [[PROMOTED:_[0-9]+]] = const main::promoted[0];
2525
// CHECK: [[_darr]] = const [254_i32, 42_i32, 15_i32, 39_i32, 62_i32];
2626
let _darr = *&[254, 42, 15, 39, 62];
2727

2828
// CHECK: [[ARG:_[0-9]+]] = const [31_u32, 96_u32, 173_u32, 50_u32, 1_u32];
29-
// CHECK: consume(move [[ARG]])
29+
// CHECK: consume(const [31_u32, 96_u32, 173_u32, 50_u32, 1_u32])
3030
consume([31, 96, 173, 50, 1]);
3131

3232
// CHECK: [[OP:_[0-9]+]] = const [1f32, 2f32, 3f32, 1f32, 1f32, 1f32, 1f32, 42f32];
33-
// CHECK: [[_f]] = F32x8(move [[OP]]);
33+
// CHECK: [[_f]] = F32x8(const [1f32, 2f32, 3f32, 1f32, 1f32, 1f32, 1f32, 42f32]);
3434
let _f = F32x8([1.0, 2.0, 3.0, 1.0, 1.0, 1.0, 1.0, 42.0]);
3535

3636
// ice with small arrays
3737
// CHECK: [[A:_[0-9]+]] = const [1_i32, 0_i32, 0_i32];
3838
// CHECK: [[B:_[0-9]+]] = const [0_i32, 1_i32, 0_i32];
3939
// CHECK: [[C:_[0-9]+]] = const [0_i32, 0_i32, 1_i32];
40-
// CHECK: {{_[0-9]+}} = [move [[A]], move [[B]], move [[C]]];
40+
// CHECK{LITERAL}: const [[1_i32, 0_i32, 0_i32], [0_i32, 1_i32, 0_i32], [0_i32, 0_i32, 1_i32]];
4141
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]; // 2D array
4242
}
4343

0 commit comments

Comments
 (0)