Skip to content

Commit

Permalink
Do not intern too large aggregates.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Sep 23, 2023
1 parent ed93d73 commit 87104ba
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 56 deletions.
42 changes: 24 additions & 18 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,24 +284,30 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
.collect::<Option<Vec<_>>>()?;
let variant = if ty.is_enum() { Some(variant) } else { None };
let ty = self.ecx.layout_of(ty).ok()?;
let alloc_id = self
.ecx
.intern_with_temp_alloc(ty, |ecx, dest| {
let variant_dest = if let Some(variant) = variant {
ecx.project_downcast(dest, variant)?
} else {
dest.clone()
};
for (field_index, op) in fields.into_iter().enumerate() {
let field_dest = ecx.project_field(&variant_dest, field_index)?;
ecx.copy_op(op, &field_dest, /*allow_transmute*/ false)?;
}
ecx.write_discriminant(variant.unwrap_or(FIRST_VARIANT), dest)
})
.ok()?;
let mplace =
self.ecx.raw_const_to_mplace(ConstAlloc { alloc_id, ty: ty.ty }).ok()?;
mplace.into()
if ty.is_zst() {
ImmTy::uninit(ty).into()
} else if matches!(ty.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) {
let alloc_id = self
.ecx
.intern_with_temp_alloc(ty, |ecx, dest| {
let variant_dest = if let Some(variant) = variant {
ecx.project_downcast(dest, variant)?
} else {
dest.clone()
};
for (field_index, op) in fields.into_iter().enumerate() {
let field_dest = ecx.project_field(&variant_dest, field_index)?;
ecx.copy_op(op, &field_dest, /*allow_transmute*/ false)?;
}
ecx.write_discriminant(variant.unwrap_or(FIRST_VARIANT), dest)
})
.ok()?;
let mplace =
self.ecx.raw_const_to_mplace(ConstAlloc { alloc_id, ty: ty.ty }).ok()?;
mplace.into()
} else {
return None;
}
}

Projection(base, elem) => {
Expand Down
11 changes: 4 additions & 7 deletions tests/mir-opt/const_prop/aggregate.main.GVN.panic-abort.diff
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@
StorageLive(_2);
StorageLive(_3);
_3 = (const 0_i32, const 1_u8, const 2_i32);
- _2 = (_3.1: u8);
- _1 = Add(move _2, const 0_u8);
+ _2 = const 1_u8;
+ _1 = const 1_u8;
_2 = (_3.1: u8);
_1 = Add(move _2, const 0_u8);
StorageDead(_2);
StorageDead(_3);
StorageLive(_4);
StorageLive(_5);
- _5 = _1;
_5 = _1;
- _4 = foo(move _5) -> [return: bb1, unwind unreachable];
+ _5 = const 1_u8;
+ _4 = foo(const 1_u8) -> [return: bb1, unwind unreachable];
+ _4 = foo(_1) -> [return: bb1, unwind unreachable];
}

bb1: {
Expand Down
11 changes: 4 additions & 7 deletions tests/mir-opt/const_prop/aggregate.main.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@
StorageLive(_2);
StorageLive(_3);
_3 = (const 0_i32, const 1_u8, const 2_i32);
- _2 = (_3.1: u8);
- _1 = Add(move _2, const 0_u8);
+ _2 = const 1_u8;
+ _1 = const 1_u8;
_2 = (_3.1: u8);
_1 = Add(move _2, const 0_u8);
StorageDead(_2);
StorageDead(_3);
StorageLive(_4);
StorageLive(_5);
- _5 = _1;
_5 = _1;
- _4 = foo(move _5) -> [return: bb1, unwind continue];
+ _5 = const 1_u8;
+ _4 = foo(const 1_u8) -> [return: bb1, unwind continue];
+ _4 = foo(_1) -> [return: bb1, unwind continue];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ fn main() -> () {
StorageLive(_2);
StorageLive(_3);
_3 = (const 0_i32, const 1_u8, const 2_i32);
_2 = const 1_u8;
_1 = const 1_u8;
_2 = (_3.1: u8);
_1 = Add(move _2, const 0_u8);
StorageDead(_2);
StorageDead(_3);
StorageLive(_4);
StorageLive(_5);
_5 = const 1_u8;
_4 = foo(const 1_u8) -> [return: bb1, unwind unreachable];
_5 = _1;
_4 = foo(_1) -> [return: bb1, unwind unreachable];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ fn main() -> () {
StorageLive(_2);
StorageLive(_3);
_3 = (const 0_i32, const 1_u8, const 2_i32);
_2 = const 1_u8;
_1 = const 1_u8;
_2 = (_3.1: u8);
_1 = Add(move _2, const 0_u8);
StorageDead(_2);
StorageDead(_3);
StorageLive(_4);
StorageLive(_5);
_5 = const 1_u8;
_4 = foo(const 1_u8) -> [return: bb1, unwind continue];
_5 = _1;
_4 = foo(_1) -> [return: bb1, unwind continue];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
_4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind unreachable];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind unreachable];
+ _5 = Lt(const 2_usize, _4);
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind unreachable];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
_4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
+ _5 = Lt(const 2_usize, _4);
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind continue];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
_2 = [const 0_u32, const 1_u32, const 2_u32, const 3_u32];
StorageLive(_3);
_3 = const 2_usize;
- _4 = Len(_2);
_4 = Len(_2);
- _5 = Lt(_3, _4);
- assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind continue];
+ _4 = const 4_usize;
+ _5 = const true;
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 4_usize, const 2_usize) -> [success: bb1, unwind continue];
+ _5 = Lt(const 2_usize, _4);
+ assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, const 2_usize) -> [success: bb1, unwind continue];
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
}
+ }
+
+ alloc5 (size: 8, align: 4) {
+ alloc4 (size: 8, align: 4) {
+ 04 00 00 00 00 __ __ __ │ .....░░░
}

Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
}
+ }
+
+ alloc5 (size: 8, align: 4) {
+ alloc4 (size: 8, align: 4) {
+ 04 00 00 00 00 __ __ __ │ .....░░░
}

Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
}
+ }
+
+ alloc5 (size: 8, align: 4) {
+ alloc4 (size: 8, align: 4) {
+ 04 00 00 00 00 __ __ __ │ .....░░░
}

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ fn ezmap(_1: Option<i32>) -> Option<i32> {
}
}

alloc14 (size: 8, align: 4) {
alloc13 (size: 8, align: 4) {
00 00 00 00 __ __ __ __ │ ....░░░░
}

0 comments on commit 87104ba

Please sign in to comment.