Skip to content

Commit 87104ba

Browse files
committed
Do not intern too large aggregates.
1 parent ed93d73 commit 87104ba

12 files changed

+53
-56
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+24-18
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,30 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
284284
.collect::<Option<Vec<_>>>()?;
285285
let variant = if ty.is_enum() { Some(variant) } else { None };
286286
let ty = self.ecx.layout_of(ty).ok()?;
287-
let alloc_id = self
288-
.ecx
289-
.intern_with_temp_alloc(ty, |ecx, dest| {
290-
let variant_dest = if let Some(variant) = variant {
291-
ecx.project_downcast(dest, variant)?
292-
} else {
293-
dest.clone()
294-
};
295-
for (field_index, op) in fields.into_iter().enumerate() {
296-
let field_dest = ecx.project_field(&variant_dest, field_index)?;
297-
ecx.copy_op(op, &field_dest, /*allow_transmute*/ false)?;
298-
}
299-
ecx.write_discriminant(variant.unwrap_or(FIRST_VARIANT), dest)
300-
})
301-
.ok()?;
302-
let mplace =
303-
self.ecx.raw_const_to_mplace(ConstAlloc { alloc_id, ty: ty.ty }).ok()?;
304-
mplace.into()
287+
if ty.is_zst() {
288+
ImmTy::uninit(ty).into()
289+
} else if matches!(ty.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) {
290+
let alloc_id = self
291+
.ecx
292+
.intern_with_temp_alloc(ty, |ecx, dest| {
293+
let variant_dest = if let Some(variant) = variant {
294+
ecx.project_downcast(dest, variant)?
295+
} else {
296+
dest.clone()
297+
};
298+
for (field_index, op) in fields.into_iter().enumerate() {
299+
let field_dest = ecx.project_field(&variant_dest, field_index)?;
300+
ecx.copy_op(op, &field_dest, /*allow_transmute*/ false)?;
301+
}
302+
ecx.write_discriminant(variant.unwrap_or(FIRST_VARIANT), dest)
303+
})
304+
.ok()?;
305+
let mplace =
306+
self.ecx.raw_const_to_mplace(ConstAlloc { alloc_id, ty: ty.ty }).ok()?;
307+
mplace.into()
308+
} else {
309+
return None;
310+
}
305311
}
306312

307313
Projection(base, elem) => {

tests/mir-opt/const_prop/aggregate.main.GVN.panic-abort.diff

+4-7
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@
1818
StorageLive(_2);
1919
StorageLive(_3);
2020
_3 = (const 0_i32, const 1_u8, const 2_i32);
21-
- _2 = (_3.1: u8);
22-
- _1 = Add(move _2, const 0_u8);
23-
+ _2 = const 1_u8;
24-
+ _1 = const 1_u8;
21+
_2 = (_3.1: u8);
22+
_1 = Add(move _2, const 0_u8);
2523
StorageDead(_2);
2624
StorageDead(_3);
2725
StorageLive(_4);
2826
StorageLive(_5);
29-
- _5 = _1;
27+
_5 = _1;
3028
- _4 = foo(move _5) -> [return: bb1, unwind unreachable];
31-
+ _5 = const 1_u8;
32-
+ _4 = foo(const 1_u8) -> [return: bb1, unwind unreachable];
29+
+ _4 = foo(_1) -> [return: bb1, unwind unreachable];
3330
}
3431

3532
bb1: {

tests/mir-opt/const_prop/aggregate.main.GVN.panic-unwind.diff

+4-7
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@
1818
StorageLive(_2);
1919
StorageLive(_3);
2020
_3 = (const 0_i32, const 1_u8, const 2_i32);
21-
- _2 = (_3.1: u8);
22-
- _1 = Add(move _2, const 0_u8);
23-
+ _2 = const 1_u8;
24-
+ _1 = const 1_u8;
21+
_2 = (_3.1: u8);
22+
_1 = Add(move _2, const 0_u8);
2523
StorageDead(_2);
2624
StorageDead(_3);
2725
StorageLive(_4);
2826
StorageLive(_5);
29-
- _5 = _1;
27+
_5 = _1;
3028
- _4 = foo(move _5) -> [return: bb1, unwind continue];
31-
+ _5 = const 1_u8;
32-
+ _4 = foo(const 1_u8) -> [return: bb1, unwind continue];
29+
+ _4 = foo(_1) -> [return: bb1, unwind continue];
3330
}
3431

3532
bb1: {

tests/mir-opt/const_prop/aggregate.main.PreCodegen.after.panic-abort.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ fn main() -> () {
1616
StorageLive(_2);
1717
StorageLive(_3);
1818
_3 = (const 0_i32, const 1_u8, const 2_i32);
19-
_2 = const 1_u8;
20-
_1 = const 1_u8;
19+
_2 = (_3.1: u8);
20+
_1 = Add(move _2, const 0_u8);
2121
StorageDead(_2);
2222
StorageDead(_3);
2323
StorageLive(_4);
2424
StorageLive(_5);
25-
_5 = const 1_u8;
26-
_4 = foo(const 1_u8) -> [return: bb1, unwind unreachable];
25+
_5 = _1;
26+
_4 = foo(_1) -> [return: bb1, unwind unreachable];
2727
}
2828

2929
bb1: {

tests/mir-opt/const_prop/aggregate.main.PreCodegen.after.panic-unwind.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ fn main() -> () {
1616
StorageLive(_2);
1717
StorageLive(_3);
1818
_3 = (const 0_i32, const 1_u8, const 2_i32);
19-
_2 = const 1_u8;
20-
_1 = const 1_u8;
19+
_2 = (_3.1: u8);
20+
_1 = Add(move _2, const 0_u8);
2121
StorageDead(_2);
2222
StorageDead(_3);
2323
StorageLive(_4);
2424
StorageLive(_5);
25-
_5 = const 1_u8;
26-
_4 = foo(const 1_u8) -> [return: bb1, unwind continue];
25+
_5 = _1;
26+
_4 = foo(_1) -> [return: bb1, unwind continue];
2727
}
2828

2929
bb1: {

tests/mir-opt/const_prop/array_index.main.GVN.32bit.panic-abort.diff

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
1919
StorageLive(_3);
2020
_3 = const 2_usize;
21-
- _4 = Len(_2);
21+
_4 = Len(_2);
2222
- _5 = Lt(_3, _4);
2323
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
24-
+ _4 = const 4_usize;
25-
+ _5 = const true;
26-
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind unreachable];
24+
+ _5 = Lt(const 2_usize, _4);
25+
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind unreachable];
2726
}
2827

2928
bb1: {

tests/mir-opt/const_prop/array_index.main.GVN.32bit.panic-unwind.diff

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
1919
StorageLive(_3);
2020
_3 = const 2_usize;
21-
- _4 = Len(_2);
21+
_4 = Len(_2);
2222
- _5 = Lt(_3, _4);
2323
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
24-
+ _4 = const 4_usize;
25-
+ _5 = const true;
26-
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
24+
+ _5 = Lt(const 2_usize, _4);
25+
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind continue];
2726
}
2827

2928
bb1: {

tests/mir-opt/const_prop/array_index.main.GVN.64bit.panic-unwind.diff

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
1919
StorageLive(_3);
2020
_3 = const 2_usize;
21-
- _4 = Len(_2);
21+
_4 = Len(_2);
2222
- _5 = Lt(_3, _4);
2323
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
24-
+ _4 = const 4_usize;
25-
+ _5 = const true;
26-
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
24+
+ _5 = Lt(const 2_usize, _4);
25+
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind continue];
2726
}
2827

2928
bb1: {

tests/mir-opt/pre-codegen/optimizes_into_variable.main.GVN.32bit.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
}
5757
+ }
5858
+
59-
+ alloc5 (size: 8, align: 4) {
59+
+ alloc4 (size: 8, align: 4) {
6060
+ 04 00 00 00 00 __ __ __ │ .....░░░
6161
}
6262

tests/mir-opt/pre-codegen/optimizes_into_variable.main.GVN.32bit.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
}
5757
+ }
5858
+
59-
+ alloc5 (size: 8, align: 4) {
59+
+ alloc4 (size: 8, align: 4) {
6060
+ 04 00 00 00 00 __ __ __ │ .....░░░
6161
}
6262

tests/mir-opt/pre-codegen/optimizes_into_variable.main.GVN.64bit.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
}
5757
+ }
5858
+
59-
+ alloc5 (size: 8, align: 4) {
59+
+ alloc4 (size: 8, align: 4) {
6060
+ 04 00 00 00 00 __ __ __ │ .....░░░
6161
}
6262

tests/mir-opt/pre-codegen/simple_option_map.ezmap.PreCodegen.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ fn ezmap(_1: Option<i32>) -> Option<i32> {
4545
}
4646
}
4747

48-
alloc14 (size: 8, align: 4) {
48+
alloc13 (size: 8, align: 4) {
4949
00 00 00 00 __ __ __ __ │ ....░░░░
5050
}

0 commit comments

Comments
 (0)