Skip to content

Commit 686237c

Browse files
committed
Lower discriminant_value intrinsic
This allows const propagation to evaluate comparisons involving field-less enums using derived implementations of `PartialEq` (after inlining `eq`).
1 parent d32c320 commit 686237c

File tree

5 files changed

+191
-6
lines changed

5 files changed

+191
-6
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+1
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
489489

490490
mir::Rvalue::Discriminant(ref place) => {
491491
let discr_ty = rvalue.ty(self.mir, bx.tcx());
492+
let discr_ty = self.monomorphize(discr_ty);
492493
let discr = self
493494
.codegen_place(&mut bx, place.as_ref())
494495
.codegen_get_discr(&mut bx, discr_ty);

compiler/rustc_middle/src/ty/sty.rs

+35-4
Original file line numberDiff line numberDiff line change
@@ -2213,13 +2213,44 @@ impl<'tcx> TyS<'tcx> {
22132213
}
22142214

22152215
/// Returns the type of the discriminant of this type.
2216-
pub fn discriminant_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
2216+
pub fn discriminant_ty(&'tcx self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
22172217
match self.kind() {
22182218
ty::Adt(adt, _) if adt.is_enum() => adt.repr.discr_type().to_ty(tcx),
22192219
ty::Generator(_, substs, _) => substs.as_generator().discr_ty(tcx),
2220-
_ => {
2221-
// This can only be `0`, for now, so `u8` will suffice.
2222-
tcx.types.u8
2220+
2221+
ty::Param(_) | ty::Projection(_) | ty::Opaque(..) | ty::Infer(ty::TyVar(_)) => {
2222+
let assoc_items =
2223+
tcx.associated_items(tcx.lang_items().discriminant_kind_trait().unwrap());
2224+
let discriminant_def_id = assoc_items.in_definition_order().next().unwrap().def_id;
2225+
tcx.mk_projection(discriminant_def_id, tcx.mk_substs([self.into()].iter()))
2226+
}
2227+
2228+
ty::Bool
2229+
| ty::Char
2230+
| ty::Int(_)
2231+
| ty::Uint(_)
2232+
| ty::Float(_)
2233+
| ty::Adt(..)
2234+
| ty::Foreign(_)
2235+
| ty::Str
2236+
| ty::Array(..)
2237+
| ty::Slice(_)
2238+
| ty::RawPtr(_)
2239+
| ty::Ref(..)
2240+
| ty::FnDef(..)
2241+
| ty::FnPtr(..)
2242+
| ty::Dynamic(..)
2243+
| ty::Closure(..)
2244+
| ty::GeneratorWitness(..)
2245+
| ty::Never
2246+
| ty::Tuple(_)
2247+
| ty::Error(_)
2248+
| ty::Infer(IntVar(_) | FloatVar(_)) => tcx.types.u8,
2249+
2250+
ty::Bound(..)
2251+
| ty::Placeholder(_)
2252+
| ty::Infer(FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
2253+
bug!("`discriminant_ty` applied to unexpected type: {:?}", self)
22232254
}
22242255
}
22252256
}

compiler/rustc_mir/src/transform/lower_intrinsics.rs

+15
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
8383
terminator.kind = TerminatorKind::Goto { target };
8484
}
8585
}
86+
sym::discriminant_value => {
87+
if let (Some((destination, target)), Some(arg)) =
88+
(*destination, args[0].place())
89+
{
90+
let arg = tcx.mk_place_deref(arg);
91+
block.statements.push(Statement {
92+
source_info: terminator.source_info,
93+
kind: StatementKind::Assign(box (
94+
destination,
95+
Rvalue::Discriminant(arg),
96+
)),
97+
});
98+
terminator.kind = TerminatorKind::Goto { target };
99+
}
100+
}
86101
_ => {}
87102
}
88103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
- // MIR for `discriminant` before LowerIntrinsics
2+
+ // MIR for `discriminant` after LowerIntrinsics
3+
4+
fn discriminant(_1: T) -> () {
5+
debug t => _1; // in scope 0 at $DIR/lower_intrinsics.rs:68:24: 68:25
6+
let mut _0: (); // return place in scope 0 at $DIR/lower_intrinsics.rs:68:30: 68:30
7+
let _2: <T as std::marker::DiscriminantKind>::Discriminant; // in scope 0 at $DIR/lower_intrinsics.rs:69:5: 69:45
8+
let mut _3: &T; // in scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44
9+
let _4: &T; // in scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44
10+
let _5: u8; // in scope 0 at $DIR/lower_intrinsics.rs:70:5: 70:45
11+
let mut _6: &i32; // in scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44
12+
let _7: &i32; // in scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44
13+
let _8: i32; // in scope 0 at $DIR/lower_intrinsics.rs:70:43: 70:44
14+
let _9: u8; // in scope 0 at $DIR/lower_intrinsics.rs:71:5: 71:46
15+
let mut _10: &(); // in scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45
16+
let _11: &(); // in scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45
17+
let _12: (); // in scope 0 at $DIR/lower_intrinsics.rs:71:43: 71:45
18+
let _13: isize; // in scope 0 at $DIR/lower_intrinsics.rs:72:5: 72:48
19+
let mut _14: &E; // in scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47
20+
let _15: &E; // in scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47
21+
let _16: E; // in scope 0 at $DIR/lower_intrinsics.rs:72:43: 72:47
22+
let mut _17: &E; // in scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47
23+
let mut _18: &(); // in scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45
24+
let mut _19: &i32; // in scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44
25+
26+
bb0: {
27+
StorageLive(_2); // scope 0 at $DIR/lower_intrinsics.rs:69:5: 69:45
28+
StorageLive(_3); // scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44
29+
StorageLive(_4); // scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44
30+
_4 = &_1; // scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44
31+
_3 = &(*_4); // scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44
32+
- _2 = discriminant_value::<T>(move _3) -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:69:5: 69:45
33+
- // mir::Constant
34+
- // + span: $DIR/lower_intrinsics.rs:69:5: 69:41
35+
- // + literal: Const { ty: for<'r> extern "rust-intrinsic" fn(&'r T) -> <T as std::marker::DiscriminantKind>::Discriminant {std::intrinsics::discriminant_value::<T>}, val: Value(Scalar(<ZST>)) }
36+
+ _2 = discriminant((*_3)); // scope 0 at $DIR/lower_intrinsics.rs:69:5: 69:45
37+
+ goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:69:5: 69:45
38+
}
39+
40+
bb1: {
41+
StorageDead(_3); // scope 0 at $DIR/lower_intrinsics.rs:69:44: 69:45
42+
StorageDead(_4); // scope 0 at $DIR/lower_intrinsics.rs:69:45: 69:46
43+
StorageDead(_2); // scope 0 at $DIR/lower_intrinsics.rs:69:45: 69:46
44+
StorageLive(_5); // scope 0 at $DIR/lower_intrinsics.rs:70:5: 70:45
45+
StorageLive(_6); // scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44
46+
StorageLive(_7); // scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44
47+
_19 = const discriminant::<T>::promoted[2]; // scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44
48+
// ty::Const
49+
// + ty: &i32
50+
// + val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[2]))
51+
// mir::Constant
52+
// + span: $DIR/lower_intrinsics.rs:70:42: 70:44
53+
// + literal: Const { ty: &i32, val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[2])) }
54+
_7 = &(*_19); // scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44
55+
_6 = &(*_7); // scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44
56+
- _5 = discriminant_value::<i32>(move _6) -> bb2; // scope 0 at $DIR/lower_intrinsics.rs:70:5: 70:45
57+
- // mir::Constant
58+
- // + span: $DIR/lower_intrinsics.rs:70:5: 70:41
59+
- // + literal: Const { ty: for<'r> extern "rust-intrinsic" fn(&'r i32) -> <i32 as std::marker::DiscriminantKind>::Discriminant {std::intrinsics::discriminant_value::<i32>}, val: Value(Scalar(<ZST>)) }
60+
+ _5 = discriminant((*_6)); // scope 0 at $DIR/lower_intrinsics.rs:70:5: 70:45
61+
+ goto -> bb2; // scope 0 at $DIR/lower_intrinsics.rs:70:5: 70:45
62+
}
63+
64+
bb2: {
65+
StorageDead(_6); // scope 0 at $DIR/lower_intrinsics.rs:70:44: 70:45
66+
StorageDead(_7); // scope 0 at $DIR/lower_intrinsics.rs:70:45: 70:46
67+
StorageDead(_5); // scope 0 at $DIR/lower_intrinsics.rs:70:45: 70:46
68+
StorageLive(_9); // scope 0 at $DIR/lower_intrinsics.rs:71:5: 71:46
69+
StorageLive(_10); // scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45
70+
StorageLive(_11); // scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45
71+
_18 = const discriminant::<T>::promoted[1]; // scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45
72+
// ty::Const
73+
// + ty: &()
74+
// + val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[1]))
75+
// mir::Constant
76+
// + span: $DIR/lower_intrinsics.rs:71:42: 71:45
77+
// + literal: Const { ty: &(), val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[1])) }
78+
_11 = &(*_18); // scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45
79+
_10 = &(*_11); // scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45
80+
- _9 = discriminant_value::<()>(move _10) -> bb3; // scope 0 at $DIR/lower_intrinsics.rs:71:5: 71:46
81+
- // mir::Constant
82+
- // + span: $DIR/lower_intrinsics.rs:71:5: 71:41
83+
- // + literal: Const { ty: for<'r> extern "rust-intrinsic" fn(&'r ()) -> <() as std::marker::DiscriminantKind>::Discriminant {std::intrinsics::discriminant_value::<()>}, val: Value(Scalar(<ZST>)) }
84+
+ _9 = discriminant((*_10)); // scope 0 at $DIR/lower_intrinsics.rs:71:5: 71:46
85+
+ goto -> bb3; // scope 0 at $DIR/lower_intrinsics.rs:71:5: 71:46
86+
}
87+
88+
bb3: {
89+
StorageDead(_10); // scope 0 at $DIR/lower_intrinsics.rs:71:45: 71:46
90+
StorageDead(_11); // scope 0 at $DIR/lower_intrinsics.rs:71:46: 71:47
91+
StorageDead(_9); // scope 0 at $DIR/lower_intrinsics.rs:71:46: 71:47
92+
StorageLive(_13); // scope 0 at $DIR/lower_intrinsics.rs:72:5: 72:48
93+
StorageLive(_14); // scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47
94+
StorageLive(_15); // scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47
95+
_17 = const discriminant::<T>::promoted[0]; // scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47
96+
// ty::Const
97+
// + ty: &E
98+
// + val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[0]))
99+
// mir::Constant
100+
// + span: $DIR/lower_intrinsics.rs:72:42: 72:47
101+
// + literal: Const { ty: &E, val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[0])) }
102+
_15 = &(*_17); // scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47
103+
_14 = &(*_15); // scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47
104+
- _13 = discriminant_value::<E>(move _14) -> bb4; // scope 0 at $DIR/lower_intrinsics.rs:72:5: 72:48
105+
- // mir::Constant
106+
- // + span: $DIR/lower_intrinsics.rs:72:5: 72:41
107+
- // + literal: Const { ty: for<'r> extern "rust-intrinsic" fn(&'r E) -> <E as std::marker::DiscriminantKind>::Discriminant {std::intrinsics::discriminant_value::<E>}, val: Value(Scalar(<ZST>)) }
108+
+ _13 = discriminant((*_14)); // scope 0 at $DIR/lower_intrinsics.rs:72:5: 72:48
109+
+ goto -> bb4; // scope 0 at $DIR/lower_intrinsics.rs:72:5: 72:48
110+
}
111+
112+
bb4: {
113+
StorageDead(_14); // scope 0 at $DIR/lower_intrinsics.rs:72:47: 72:48
114+
StorageDead(_15); // scope 0 at $DIR/lower_intrinsics.rs:72:48: 72:49
115+
StorageDead(_13); // scope 0 at $DIR/lower_intrinsics.rs:72:48: 72:49
116+
_0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:68:30: 73:2
117+
drop(_1) -> bb5; // scope 0 at $DIR/lower_intrinsics.rs:73:1: 73:2
118+
}
119+
120+
bb5: {
121+
return; // scope 0 at $DIR/lower_intrinsics.rs:73:2: 73:2
122+
}
123+
}
124+

src/test/mir-opt/lower_intrinsics.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,29 @@ pub fn f_dispatch<T>(t: T) {
4545
}
4646

4747
#[inline(never)]
48-
pub fn f_zst<T>(t: T) {
48+
pub fn f_zst<T>(_t: T) {
4949
}
5050

5151
#[inline(never)]
52-
pub fn f_non_zst<T>(t: T) {}
52+
pub fn f_non_zst<T>(_t: T) {}
5353

5454
// EMIT_MIR lower_intrinsics.non_const.LowerIntrinsics.diff
5555
pub fn non_const<T>() -> usize {
5656
// Check that lowering works with non-const operand as a func.
5757
let size_of_t = core::intrinsics::size_of::<T>;
5858
size_of_t()
5959
}
60+
61+
pub enum E {
62+
A,
63+
B,
64+
C,
65+
}
66+
67+
// EMIT_MIR lower_intrinsics.discriminant.LowerIntrinsics.diff
68+
pub fn discriminant<T>(t: T) {
69+
core::intrinsics::discriminant_value(&t);
70+
core::intrinsics::discriminant_value(&0);
71+
core::intrinsics::discriminant_value(&());
72+
core::intrinsics::discriminant_value(&E::B);
73+
}

0 commit comments

Comments
 (0)