Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e18ec78

Browse files
committed
Auto merge of rust-lang#138526 - scottmcm:no-const-none, r=<try>
Stop making `const None` in GVN Having `ALLOC`s for these seems unhelpful, since it's extra stuff to keep track of, makes it harder for other passes to know what's in it (reversing a tag constant to a `VariantIdx` isn't trivial), and even in codegen it's arguably harder to get a `None` from a valtree than from seeing "oh, aggregate nothing with this `VariantIdx`". Making constants for the `discriminant(_n)` is useful for putting in `switchInt`s, and still happens with this PR. It's just for the full enum that we don't make consts (except for might-as-well-be-an-integer enums like `Ordering`, or ones that are actually ZSTs like `const Option::<Infallible>::None`). This also adds a couple tests I've been using as I look at how we're doing enums in MIR. They're not really *improved* by this change materially, but they help motivate why I'm trying to tweak how the enums come out.
2 parents d9e5539 + 41f6339 commit e18ec78

File tree

37 files changed

+682
-87
lines changed

37 files changed

+682
-87
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,21 @@ impl<'tcx> VnState<'_, 'tcx> {
17001700
return Some(ConstOperand { span: DUMMY_SP, user_ty: None, const_: value });
17011701
}
17021702

1703+
// When an enum can have a payload, it's not worth turning into a constant.
1704+
// Making a bunch of EvalCx allocations for `Option::None`s isn't productive
1705+
// and just makes it harder for subsequent passes to see the Discriminants.
1706+
// It's still easy for codegen to build the aggregate from the fields, since
1707+
// for an enum that's just the fields and maybe the constant tag.
1708+
// (We don't ever make consts for things bigger than a ScalarPair anyway.)
1709+
if let Value::Aggregate(agg_ty, _, _) = *self.get(index)
1710+
&& let AggregateTy::Def(did, _) = agg_ty
1711+
&& let DefKind::Enum = self.tcx.def_kind(did)
1712+
&& let adt = self.tcx.adt_def(did)
1713+
&& !adt.is_payloadfree()
1714+
{
1715+
return None;
1716+
}
1717+
17031718
let op = self.evaluated[index].as_ref()?;
17041719
if op.layout.is_unsized() {
17051720
// Do not attempt to propagate unsized locals.

tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
let _10: (bool, bool, u32);
3333
scope 6 {
3434
debug f => _10;
35-
let _11: std::option::Option<u16>;
35+
let _11: MyEnum;
3636
scope 7 {
3737
- debug o => _11;
38-
+ debug o => const Option::<u16>::Some(99_u16);
38+
+ debug o => const MyEnum::Bar;
3939
let _12: Point;
4040
scope 8 {
4141
- debug p => _12;
@@ -89,7 +89,7 @@
8989
StorageLive(_10);
9090
_10 = (const true, const false, const 123_u32);
9191
StorageLive(_11);
92-
- _11 = const Option::<u16>::Some(99_u16);
92+
- _11 = const MyEnum::Bar;
9393
+ nop;
9494
StorageLive(_12);
9595
- _12 = const Point {{ x: 32_u32, y: 32_u32 }};
@@ -121,5 +121,3 @@
121121

122122
ALLOC0 (size: 8, align: 4) { .. }
123123

124-
ALLOC1 (size: 4, align: 2) { .. }
125-

tests/mir-opt/const_debuginfo.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ struct Point {
88
y: u32,
99
}
1010

11+
#[repr(u16)]
12+
enum MyEnum {
13+
Foo = 88,
14+
Bar = 99,
15+
Baz = 111,
16+
}
17+
1118
// EMIT_MIR const_debuginfo.main.SingleUseConsts.diff
1219
fn main() {
1320
// CHECK-LABEL: fn main(
@@ -17,7 +24,7 @@ fn main() {
1724
// CHECK: debug sum => const 6_u8;
1825
// CHECK: debug s => const "hello, world!";
1926
// CHECK: debug f => {{_.*}};
20-
// CHECK: debug o => const Option::<u16>::Some(99_u16);
27+
// CHECK: debug o => const MyEnum::Bar;
2128
// CHECK: debug p => const Point
2229
// CHECK: debug a => const 64_u32;
2330
let x = 1u8;
@@ -29,7 +36,7 @@ fn main() {
2936

3037
let f = (true, false, 123u32);
3138

32-
let o = Some(99u16);
39+
let o = MyEnum::Bar;
3340

3441
let p = Point { x: 32, y: 32 };
3542
let a = p.x + p.y;

tests/mir-opt/const_prop/discriminant.main.GVN.32bit.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
StorageLive(_1);
1818
StorageLive(_2);
1919
StorageLive(_3);
20-
- _3 = Option::<bool>::Some(const true);
20+
_3 = Option::<bool>::Some(const true);
2121
- _4 = discriminant(_3);
2222
- switchInt(move _4) -> [1: bb1, otherwise: bb3];
23-
+ _3 = const Option::<bool>::Some(true);
2423
+ _4 = const 1_isize;
2524
+ switchInt(const 1_isize) -> [1: bb1, otherwise: bb3];
2625
}

tests/mir-opt/const_prop/discriminant.main.GVN.64bit.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
StorageLive(_1);
1818
StorageLive(_2);
1919
StorageLive(_3);
20-
- _3 = Option::<bool>::Some(const true);
20+
_3 = Option::<bool>::Some(const true);
2121
- _4 = discriminant(_3);
2222
- switchInt(move _4) -> [1: bb1, otherwise: bb3];
23-
+ _3 = const Option::<bool>::Some(true);
2423
+ _4 = const 1_isize;
2524
+ switchInt(const 1_isize) -> [1: bb1, otherwise: bb3];
2625
}

tests/mir-opt/const_prop/while_let_loops.change_loop_body.GVN.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
StorageLive(_1);
2222
_1 = const 0_i32;
2323
StorageLive(_3);
24-
- _3 = Option::<u32>::None;
24+
_3 = Option::<u32>::None;
2525
- _4 = discriminant(_3);
2626
- switchInt(move _4) -> [1: bb1, otherwise: bb3];
27-
+ _3 = const Option::<u32>::None;
2827
+ _4 = const 0_isize;
2928
+ switchInt(const 0_isize) -> [1: bb1, otherwise: bb3];
3029
}

tests/mir-opt/funky_arms.float_to_exponential_common.GVN.32bit.panic-abort.diff

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
}
161161

162162
bb11: {
163-
_6 = const Option::<usize>::None;
163+
_6 = Option::<usize>::None;
164164
goto -> bb9;
165165
}
166166

@@ -174,7 +174,3 @@
174174
}
175175
}
176176

177-
ALLOC0 (size: 8, align: 4) {
178-
00 00 00 00 __ __ __ __ │ ....░░░░
179-
}
180-

tests/mir-opt/funky_arms.float_to_exponential_common.GVN.32bit.panic-unwind.diff

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
}
161161

162162
bb11: {
163-
_6 = const Option::<usize>::None;
163+
_6 = Option::<usize>::None;
164164
goto -> bb9;
165165
}
166166

@@ -174,7 +174,3 @@
174174
}
175175
}
176176

177-
ALLOC0 (size: 8, align: 4) {
178-
00 00 00 00 __ __ __ __ │ ....░░░░
179-
}
180-

tests/mir-opt/funky_arms.float_to_exponential_common.GVN.64bit.panic-abort.diff

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
}
161161

162162
bb11: {
163-
_6 = const Option::<usize>::None;
163+
_6 = Option::<usize>::None;
164164
goto -> bb9;
165165
}
166166

@@ -174,7 +174,3 @@
174174
}
175175
}
176176

177-
ALLOC0 (size: 16, align: 8) {
178-
00 00 00 00 00 00 00 00 __ __ __ __ __ __ __ __ │ ........░░░░░░░░
179-
}
180-

tests/mir-opt/funky_arms.float_to_exponential_common.GVN.64bit.panic-unwind.diff

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
}
161161

162162
bb11: {
163-
_6 = const Option::<usize>::None;
163+
_6 = Option::<usize>::None;
164164
goto -> bb9;
165165
}
166166

@@ -174,7 +174,3 @@
174174
}
175175
}
176176

177-
ALLOC0 (size: 16, align: 8) {
178-
00 00 00 00 00 00 00 00 __ __ __ __ __ __ __ __ │ ........░░░░░░░░
179-
}
180-

0 commit comments

Comments
 (0)