Skip to content

Commit 96b505e

Browse files
committed
Let LLVM de-duplicates arrays has same value
1 parent eac1ae7 commit 96b505e

File tree

3 files changed

+5
-17
lines changed

3 files changed

+5
-17
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,17 +1539,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
15391539
.or_else(|| self.simplify_rvalue(rvalue, location));
15401540
let Some(value) = value else { return };
15411541

1542-
// De-duplicate locals has the same arrays assigned to prevent code bloat.
1543-
let disallowed_duplicated_array = if rvalue.ty(self.local_decls, self.tcx).is_array()
1544-
&& let Some(locals) = self.rev_locals.get(value).as_deref()
1545-
&& let [first, ..] = locals[..]
1546-
{
1547-
first != lhs.local
1548-
} else {
1549-
false
1550-
};
1551-
1552-
if !disallowed_duplicated_array && let Some(const_) = self.try_as_constant(value) {
1542+
if let Some(const_) = self.try_as_constant(value) {
15531543
*rvalue = Rvalue::Use(Operand::Constant(Box::new(const_)));
15541544
} else if let Some(local) = self.try_as_local(value, location)
15551545
&& *rvalue != Rvalue::Use(Operand::Move(local.into()))

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@
3737
}
3838

3939
bb0: {
40-
- StorageLive(_1);
40+
StorageLive(_1);
4141
- _1 = [const 255_i32, const 105_i32, const 15_i32, const 39_i32, const 62_i32];
42-
+ nop;
4342
+ _1 = const [255_i32, 105_i32, 15_i32, 39_i32, 62_i32];
4443
StorageLive(_2);
4544
- _2 = [const 255_i32, const 105_i32, const 15_i32, const 39_i32, const 62_i32];
46-
+ _2 = _1;
45+
+ _2 = const [255_i32, 105_i32, 15_i32, 39_i32, 62_i32];
4746
StorageLive(_3);
4847
StorageLive(_4);
4948
- _4 = [const 178_i32, const 9_i32, const 4_i32, const 56_i32, const 221_i32];
@@ -94,8 +93,7 @@
9493
StorageDead(_6);
9594
StorageDead(_3);
9695
StorageDead(_2);
97-
- StorageDead(_1);
98-
+ nop;
96+
StorageDead(_1);
9997
return;
10098
}
10199
+ }

tests/mir-opt/const_array_locals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct F32x8([f32; 8]);
1414
pub fn main() {
1515
// CHECK: [[_arr]] = const [255_i32, 105_i32, 15_i32, 39_i32, 62_i32];
1616
let _arr = [255, 105, 15, 39, 62];
17-
// CHECK: [[_duplicated_arr]] = [[_arr]];
17+
// CHECK: [[_duplicated_arr]] = const [255_i32, 105_i32, 15_i32, 39_i32, 62_i32];
1818
let _duplicated_arr = [255, 105, 15, 39, 62];
1919
// CHECK: [[subarray1:_[0-9]+]] = const [178_i32, 9_i32, 4_i32, 56_i32, 221_i32];
2020
// CHECK: [[subarray2:_[0-9]+]] = const [193_i32, 164_i32, 194_i32, 197_i32, 6_i32];

0 commit comments

Comments
 (0)