Skip to content

Commit

Permalink
try to make things nice without regressing perf
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 19, 2024
1 parent 1ed8ff2 commit bc52909
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 13 additions & 1 deletion compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ impl<'tcx> Const<'tcx> {
Const::Ty(c) => match c.kind() {
ty::ConstKind::Value(valtree) if c.ty().is_primitive() => {
// A valtree of a type where leaves directly represent the scalar const value.
// Just checking whether it is a leaf is insufficient as e.g. references are leafs
// but the leaf value is the value they point to, not the reference itself!
Some(valtree.unwrap_leaf().into())
}
_ => None,
Expand All @@ -255,7 +257,17 @@ impl<'tcx> Const<'tcx> {

#[inline]
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
self.try_to_scalar()?.try_to_int().ok()
// This is equivalent to `self.try_to_scalar()?.try_to_int().ok()`, but measurably faster.
match self {
Const::Val(ConstValue::Scalar(Scalar::Int(x)), _) => Some(x),
Const::Ty(c) => match c.kind() {
ty::ConstKind::Value(valtree) if c.ty().is_primitive() => {
Some(valtree.unwrap_leaf())
}
_ => None,
},
_ => None,
}
}

#[inline]
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_hir::{BindingAnnotation, ByRef, HirId, MatchSource, RangeEnd};
use rustc_index::newtype_index;
use rustc_index::IndexVec;
use rustc_middle::middle::region;
use rustc_middle::mir::interpret::{AllocId, Scalar};
use rustc_middle::mir::interpret::AllocId;
use rustc_middle::mir::{self, BinOp, BorrowKind, FakeReadCause, UnOp};
use rustc_middle::ty::adjustment::PointerCoercion;
use rustc_middle::ty::layout::IntegerExt;
Expand Down Expand Up @@ -1013,12 +1013,12 @@ impl<'tcx> PatRangeBoundary<'tcx> {
// many ranges such as '\u{037A}'..='\u{037F}', and chars can be compared
// in this way.
(Finite(a), Finite(b)) if matches!(ty.kind(), ty::Uint(_) | ty::Char) => {
let to_scalar_int = |x| match x {
/*let to_scalar_int = |x| match x {
mir::Const::Val(mir::ConstValue::Scalar(Scalar::Int(x)), _) => Some(x),
mir::Const::Ty(x) => Some(x.to_valtree().unwrap_leaf()),
_ => None,
};
if let (Some(a), Some(b)) = (to_scalar_int(a), to_scalar_int(b)) {
};*/
if let (Some(a), Some(b)) = (a.try_to_scalar_int(), b.try_to_scalar_int()) {
let sz = ty.primitive_size(tcx);
let a = a.assert_uint(sz);
let b = b.assert_uint(sz);
Expand Down

0 comments on commit bc52909

Please sign in to comment.