Skip to content

Commit

Permalink
Auto merge of #116042 - Nadrieril:linear-pass-take-2, r=<try>
Browse files Browse the repository at this point in the history
[Experiment] Rewrite exhaustiveness in one pass

Arm reachability checking does a quadratic amount of work: for each arm we check if it is reachable given the arms above it. This feels wasteful since we often end up re-exploring the same cases when we check for exhaustiveness.

This PR is an attempt to check reachability at the same time as exhaustiveness. This opens the door to a bunch of code simplifications I'm very excited about. The main question is whether I can get actual performance gains out of this.

I had started the experiment in #111720 but I can't reopen it.

r? `@ghost`
  • Loading branch information
bors committed Sep 29, 2023
2 parents 7b4d9e1 + 1efc54c commit dde0381
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ fn expand_or_pat<'p, 'tcx>(pat: &'p Pat<'tcx>) -> Vec<&'p Pat<'tcx>> {
#[derive(Clone, PartialEq, Eq)]
pub(crate) struct IntRange {
range: RangeInclusive<u128>,
/// Keeps the bias used for encoding the range. It depends on the type of the range and
/// possibly the pointer size of the current architecture. The algorithm ensures we never
/// compare `IntRange`s with different types/architectures.
bias: u128,
}

impl IntRange {
Expand Down Expand Up @@ -150,7 +146,7 @@ impl IntRange {
}?;

let val = val ^ bias;
Some(IntRange { range: val..=val, bias })
Some(IntRange { range: val..=val })
}

#[inline]
Expand All @@ -171,7 +167,7 @@ impl IntRange {
// This should have been caught earlier by E0030.
bug!("malformed range pattern: {}..={}", lo, (hi - offset));
}
IntRange { range: lo..=(hi - offset), bias }
IntRange { range: lo..=(hi - offset) }
})
}

Expand All @@ -194,7 +190,7 @@ impl IntRange {
let (lo, hi) = self.boundaries();
let (other_lo, other_hi) = other.boundaries();
if lo <= other_hi && other_lo <= hi {
Some(IntRange { range: max(lo, other_lo)..=min(hi, other_hi), bias: self.bias })
Some(IntRange { range: max(lo, other_lo)..=min(hi, other_hi) })
} else {
None
}
Expand All @@ -221,7 +217,7 @@ impl IntRange {
fn to_pat<'tcx>(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Pat<'tcx> {
let (lo, hi) = self.boundaries();

let bias = self.bias;
let bias = IntRange::signed_bias(tcx, ty);
let (lo, hi) = (lo ^ bias, hi ^ bias);

let env = ty::ParamEnv::empty().and(ty);
Expand Down Expand Up @@ -304,8 +300,6 @@ impl IntRange {
impl fmt::Debug for IntRange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (lo, hi) = self.boundaries();
let bias = self.bias;
let (lo, hi) = (lo ^ bias, hi ^ bias);
write!(f, "{lo}")?;
write!(f, "{}", RangeEnd::Included)?;
write!(f, "{hi}")
Expand Down Expand Up @@ -402,7 +396,7 @@ impl SplitIntRange {
(JustBefore(n), AfterMax) => n..=u128::MAX,
_ => unreachable!(), // Ruled out by the sorting and filtering we did
};
IntRange { range, bias: self.range.bias }
IntRange { range }
})
}
}
Expand Down

0 comments on commit dde0381

Please sign in to comment.