Skip to content

Commit

Permalink
Lower discriminant_value intrinsic
Browse files Browse the repository at this point in the history
This allows const propagation to evaluate comparisons involving
field-less enums using derived implementations of `PartialEq` (after
inlining `eq`).
  • Loading branch information
tmiasko committed Dec 11, 2020
1 parent d32c320 commit 686237c
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 6 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/src/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 35 additions & 4 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_mir/src/transform/lower_intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
_ => {}
}
}
Expand Down
124 changes: 124 additions & 0 deletions src/test/mir-opt/lower_intrinsics.discriminant.LowerIntrinsics.diff
Original file line number Diff line number Diff line change
@@ -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: <T as std::marker::DiscriminantKind>::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::<T>(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) -> <T as std::marker::DiscriminantKind>::Discriminant {std::intrinsics::discriminant_value::<T>}, val: Value(Scalar(<ZST>)) }
+ _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::<T>::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::<i32>(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) -> <i32 as std::marker::DiscriminantKind>::Discriminant {std::intrinsics::discriminant_value::<i32>}, val: Value(Scalar(<ZST>)) }
+ _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::<T>::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(<ZST>)) }
+ _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::<T>::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::<E>(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) -> <E as std::marker::DiscriminantKind>::Discriminant {std::intrinsics::discriminant_value::<E>}, val: Value(Scalar(<ZST>)) }
+ _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
}
}

18 changes: 16 additions & 2 deletions src/test/mir-opt/lower_intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,29 @@ pub fn f_dispatch<T>(t: T) {
}

#[inline(never)]
pub fn f_zst<T>(t: T) {
pub fn f_zst<T>(_t: T) {
}

#[inline(never)]
pub fn f_non_zst<T>(t: T) {}
pub fn f_non_zst<T>(_t: T) {}

// EMIT_MIR lower_intrinsics.non_const.LowerIntrinsics.diff
pub fn non_const<T>() -> usize {
// Check that lowering works with non-const operand as a func.
let size_of_t = core::intrinsics::size_of::<T>;
size_of_t()
}

pub enum E {
A,
B,
C,
}

// EMIT_MIR lower_intrinsics.discriminant.LowerIntrinsics.diff
pub fn discriminant<T>(t: T) {
core::intrinsics::discriminant_value(&t);
core::intrinsics::discriminant_value(&0);
core::intrinsics::discriminant_value(&());
core::intrinsics::discriminant_value(&E::B);
}

0 comments on commit 686237c

Please sign in to comment.