From 686237c49aed588bf266e8dece3130b779605109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Fri, 11 Dec 2020 00:00:00 +0000 Subject: [PATCH] Lower `discriminant_value` intrinsic This allows const propagation to evaluate comparisons involving field-less enums using derived implementations of `PartialEq` (after inlining `eq`). --- compiler/rustc_codegen_ssa/src/mir/rvalue.rs | 1 + compiler/rustc_middle/src/ty/sty.rs | 39 +++++- .../src/transform/lower_intrinsics.rs | 15 +++ ...trinsics.discriminant.LowerIntrinsics.diff | 124 ++++++++++++++++++ src/test/mir-opt/lower_intrinsics.rs | 18 ++- 5 files changed, 191 insertions(+), 6 deletions(-) create mode 100644 src/test/mir-opt/lower_intrinsics.discriminant.LowerIntrinsics.diff diff --git a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs index 2ad470c2693de..e3a6cabd60057 100644 --- a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs +++ b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs @@ -489,6 +489,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { mir::Rvalue::Discriminant(ref place) => { let discr_ty = rvalue.ty(self.mir, bx.tcx()); + let discr_ty = self.monomorphize(discr_ty); let discr = self .codegen_place(&mut bx, place.as_ref()) .codegen_get_discr(&mut bx, discr_ty); diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 78994c6e1c77a..8d452399ba527 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -2213,13 +2213,44 @@ impl<'tcx> TyS<'tcx> { } /// Returns the type of the discriminant of this type. - pub fn discriminant_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { + pub fn discriminant_ty(&'tcx self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { match self.kind() { ty::Adt(adt, _) if adt.is_enum() => adt.repr.discr_type().to_ty(tcx), ty::Generator(_, substs, _) => substs.as_generator().discr_ty(tcx), - _ => { - // This can only be `0`, for now, so `u8` will suffice. - tcx.types.u8 + + ty::Param(_) | ty::Projection(_) | ty::Opaque(..) | ty::Infer(ty::TyVar(_)) => { + let assoc_items = + tcx.associated_items(tcx.lang_items().discriminant_kind_trait().unwrap()); + let discriminant_def_id = assoc_items.in_definition_order().next().unwrap().def_id; + tcx.mk_projection(discriminant_def_id, tcx.mk_substs([self.into()].iter())) + } + + ty::Bool + | ty::Char + | ty::Int(_) + | ty::Uint(_) + | ty::Float(_) + | ty::Adt(..) + | ty::Foreign(_) + | ty::Str + | ty::Array(..) + | ty::Slice(_) + | ty::RawPtr(_) + | ty::Ref(..) + | ty::FnDef(..) + | ty::FnPtr(..) + | ty::Dynamic(..) + | ty::Closure(..) + | ty::GeneratorWitness(..) + | ty::Never + | ty::Tuple(_) + | ty::Error(_) + | ty::Infer(IntVar(_) | FloatVar(_)) => tcx.types.u8, + + ty::Bound(..) + | ty::Placeholder(_) + | ty::Infer(FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => { + bug!("`discriminant_ty` applied to unexpected type: {:?}", self) } } } diff --git a/compiler/rustc_mir/src/transform/lower_intrinsics.rs b/compiler/rustc_mir/src/transform/lower_intrinsics.rs index 543acb74acbc9..f5968532eb396 100644 --- a/compiler/rustc_mir/src/transform/lower_intrinsics.rs +++ b/compiler/rustc_mir/src/transform/lower_intrinsics.rs @@ -83,6 +83,21 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics { terminator.kind = TerminatorKind::Goto { target }; } } + sym::discriminant_value => { + if let (Some((destination, target)), Some(arg)) = + (*destination, args[0].place()) + { + let arg = tcx.mk_place_deref(arg); + block.statements.push(Statement { + source_info: terminator.source_info, + kind: StatementKind::Assign(box ( + destination, + Rvalue::Discriminant(arg), + )), + }); + terminator.kind = TerminatorKind::Goto { target }; + } + } _ => {} } } diff --git a/src/test/mir-opt/lower_intrinsics.discriminant.LowerIntrinsics.diff b/src/test/mir-opt/lower_intrinsics.discriminant.LowerIntrinsics.diff new file mode 100644 index 0000000000000..a21cbfa767edb --- /dev/null +++ b/src/test/mir-opt/lower_intrinsics.discriminant.LowerIntrinsics.diff @@ -0,0 +1,124 @@ +- // MIR for `discriminant` before LowerIntrinsics ++ // MIR for `discriminant` after LowerIntrinsics + + fn discriminant(_1: T) -> () { + debug t => _1; // in scope 0 at $DIR/lower_intrinsics.rs:68:24: 68:25 + let mut _0: (); // return place in scope 0 at $DIR/lower_intrinsics.rs:68:30: 68:30 + let _2: ::Discriminant; // in scope 0 at $DIR/lower_intrinsics.rs:69:5: 69:45 + let mut _3: &T; // in scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44 + let _4: &T; // in scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44 + let _5: u8; // in scope 0 at $DIR/lower_intrinsics.rs:70:5: 70:45 + let mut _6: &i32; // in scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44 + let _7: &i32; // in scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44 + let _8: i32; // in scope 0 at $DIR/lower_intrinsics.rs:70:43: 70:44 + let _9: u8; // in scope 0 at $DIR/lower_intrinsics.rs:71:5: 71:46 + let mut _10: &(); // in scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45 + let _11: &(); // in scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45 + let _12: (); // in scope 0 at $DIR/lower_intrinsics.rs:71:43: 71:45 + let _13: isize; // in scope 0 at $DIR/lower_intrinsics.rs:72:5: 72:48 + let mut _14: &E; // in scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47 + let _15: &E; // in scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47 + let _16: E; // in scope 0 at $DIR/lower_intrinsics.rs:72:43: 72:47 + let mut _17: &E; // in scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47 + let mut _18: &(); // in scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45 + let mut _19: &i32; // in scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44 + + bb0: { + StorageLive(_2); // scope 0 at $DIR/lower_intrinsics.rs:69:5: 69:45 + StorageLive(_3); // scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44 + StorageLive(_4); // scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44 + _4 = &_1; // scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44 + _3 = &(*_4); // scope 0 at $DIR/lower_intrinsics.rs:69:42: 69:44 +- _2 = discriminant_value::(move _3) -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:69:5: 69:45 +- // mir::Constant +- // + span: $DIR/lower_intrinsics.rs:69:5: 69:41 +- // + literal: Const { ty: for<'r> extern "rust-intrinsic" fn(&'r T) -> ::Discriminant {std::intrinsics::discriminant_value::}, val: Value(Scalar()) } ++ _2 = discriminant((*_3)); // scope 0 at $DIR/lower_intrinsics.rs:69:5: 69:45 ++ goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:69:5: 69:45 + } + + bb1: { + StorageDead(_3); // scope 0 at $DIR/lower_intrinsics.rs:69:44: 69:45 + StorageDead(_4); // scope 0 at $DIR/lower_intrinsics.rs:69:45: 69:46 + StorageDead(_2); // scope 0 at $DIR/lower_intrinsics.rs:69:45: 69:46 + StorageLive(_5); // scope 0 at $DIR/lower_intrinsics.rs:70:5: 70:45 + StorageLive(_6); // scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44 + StorageLive(_7); // scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44 + _19 = const discriminant::::promoted[2]; // scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44 + // ty::Const + // + ty: &i32 + // + val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[2])) + // mir::Constant + // + span: $DIR/lower_intrinsics.rs:70:42: 70:44 + // + literal: Const { ty: &i32, val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[2])) } + _7 = &(*_19); // scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44 + _6 = &(*_7); // scope 0 at $DIR/lower_intrinsics.rs:70:42: 70:44 +- _5 = discriminant_value::(move _6) -> bb2; // scope 0 at $DIR/lower_intrinsics.rs:70:5: 70:45 +- // mir::Constant +- // + span: $DIR/lower_intrinsics.rs:70:5: 70:41 +- // + literal: Const { ty: for<'r> extern "rust-intrinsic" fn(&'r i32) -> ::Discriminant {std::intrinsics::discriminant_value::}, val: Value(Scalar()) } ++ _5 = discriminant((*_6)); // scope 0 at $DIR/lower_intrinsics.rs:70:5: 70:45 ++ goto -> bb2; // scope 0 at $DIR/lower_intrinsics.rs:70:5: 70:45 + } + + bb2: { + StorageDead(_6); // scope 0 at $DIR/lower_intrinsics.rs:70:44: 70:45 + StorageDead(_7); // scope 0 at $DIR/lower_intrinsics.rs:70:45: 70:46 + StorageDead(_5); // scope 0 at $DIR/lower_intrinsics.rs:70:45: 70:46 + StorageLive(_9); // scope 0 at $DIR/lower_intrinsics.rs:71:5: 71:46 + StorageLive(_10); // scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45 + StorageLive(_11); // scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45 + _18 = const discriminant::::promoted[1]; // scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45 + // ty::Const + // + ty: &() + // + val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[1])) + // mir::Constant + // + span: $DIR/lower_intrinsics.rs:71:42: 71:45 + // + literal: Const { ty: &(), val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[1])) } + _11 = &(*_18); // scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45 + _10 = &(*_11); // scope 0 at $DIR/lower_intrinsics.rs:71:42: 71:45 +- _9 = discriminant_value::<()>(move _10) -> bb3; // scope 0 at $DIR/lower_intrinsics.rs:71:5: 71:46 +- // mir::Constant +- // + span: $DIR/lower_intrinsics.rs:71:5: 71:41 +- // + literal: Const { ty: for<'r> extern "rust-intrinsic" fn(&'r ()) -> <() as std::marker::DiscriminantKind>::Discriminant {std::intrinsics::discriminant_value::<()>}, val: Value(Scalar()) } ++ _9 = discriminant((*_10)); // scope 0 at $DIR/lower_intrinsics.rs:71:5: 71:46 ++ goto -> bb3; // scope 0 at $DIR/lower_intrinsics.rs:71:5: 71:46 + } + + bb3: { + StorageDead(_10); // scope 0 at $DIR/lower_intrinsics.rs:71:45: 71:46 + StorageDead(_11); // scope 0 at $DIR/lower_intrinsics.rs:71:46: 71:47 + StorageDead(_9); // scope 0 at $DIR/lower_intrinsics.rs:71:46: 71:47 + StorageLive(_13); // scope 0 at $DIR/lower_intrinsics.rs:72:5: 72:48 + StorageLive(_14); // scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47 + StorageLive(_15); // scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47 + _17 = const discriminant::::promoted[0]; // scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47 + // ty::Const + // + ty: &E + // + val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[0])) + // mir::Constant + // + span: $DIR/lower_intrinsics.rs:72:42: 72:47 + // + literal: Const { ty: &E, val: Unevaluated(WithOptConstParam { did: DefId(0:27 ~ lower_intrinsics[8787]::discriminant), const_param_did: None }, [T], Some(promoted[0])) } + _15 = &(*_17); // scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47 + _14 = &(*_15); // scope 0 at $DIR/lower_intrinsics.rs:72:42: 72:47 +- _13 = discriminant_value::(move _14) -> bb4; // scope 0 at $DIR/lower_intrinsics.rs:72:5: 72:48 +- // mir::Constant +- // + span: $DIR/lower_intrinsics.rs:72:5: 72:41 +- // + literal: Const { ty: for<'r> extern "rust-intrinsic" fn(&'r E) -> ::Discriminant {std::intrinsics::discriminant_value::}, val: Value(Scalar()) } ++ _13 = discriminant((*_14)); // scope 0 at $DIR/lower_intrinsics.rs:72:5: 72:48 ++ goto -> bb4; // scope 0 at $DIR/lower_intrinsics.rs:72:5: 72:48 + } + + bb4: { + StorageDead(_14); // scope 0 at $DIR/lower_intrinsics.rs:72:47: 72:48 + StorageDead(_15); // scope 0 at $DIR/lower_intrinsics.rs:72:48: 72:49 + StorageDead(_13); // scope 0 at $DIR/lower_intrinsics.rs:72:48: 72:49 + _0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:68:30: 73:2 + drop(_1) -> bb5; // scope 0 at $DIR/lower_intrinsics.rs:73:1: 73:2 + } + + bb5: { + return; // scope 0 at $DIR/lower_intrinsics.rs:73:2: 73:2 + } + } + diff --git a/src/test/mir-opt/lower_intrinsics.rs b/src/test/mir-opt/lower_intrinsics.rs index de5f692b7da28..8d28354a5f14e 100644 --- a/src/test/mir-opt/lower_intrinsics.rs +++ b/src/test/mir-opt/lower_intrinsics.rs @@ -45,11 +45,11 @@ pub fn f_dispatch(t: T) { } #[inline(never)] -pub fn f_zst(t: T) { +pub fn f_zst(_t: T) { } #[inline(never)] -pub fn f_non_zst(t: T) {} +pub fn f_non_zst(_t: T) {} // EMIT_MIR lower_intrinsics.non_const.LowerIntrinsics.diff pub fn non_const() -> usize { @@ -57,3 +57,17 @@ pub fn non_const() -> usize { let size_of_t = core::intrinsics::size_of::; size_of_t() } + +pub enum E { + A, + B, + C, +} + +// EMIT_MIR lower_intrinsics.discriminant.LowerIntrinsics.diff +pub fn discriminant(t: T) { + core::intrinsics::discriminant_value(&t); + core::intrinsics::discriminant_value(&0); + core::intrinsics::discriminant_value(&()); + core::intrinsics::discriminant_value(&E::B); +}