Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions compiler/rustc_data_structures/src/graph/dominators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,24 @@ impl<Node: Idx> Dominators<Node> {
}
}
}

/// Returns true if `a` **strictly** dominates `b`
///
/// # Panics
///
/// Panics if `b` is unreachable
#[inline]
pub fn strictly_dominates(&self, a: Node, b: Node) -> bool {
match &self.kind {
Kind::Path => a.index() < b.index(),
Kind::General(g) => {
let a = g.time[a];
let b = g.time[b];
assert!(b.start != 0, "node {b:?} is not reachable");
a.start < b.start && b.finish < a.finish
}
}
}
}

/// Describes the number of vertices discovered at the time when processing of a particular vertex
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,11 @@ impl Location {
dominators.dominates(self.block, other.block)
}
}

#[inline]
pub fn strictly_dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
self.block != other.block && dominators.strictly_dominates(self.block, other.block)
}
}

/// `DefLocation` represents the location of a definition - either an argument or an assignment
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_mir_transform/src/ssa_range_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ impl<'tcx> MutVisitor<'tcx> for RangeSet<'tcx, '_, '_> {
&& self.is_ssa(place) =>
{
let successor = Location { block: *target, statement_index: 0 };
if location.dominates(successor, &self.dominators) {
assert_ne!(location.block, successor.block);
if location.strictly_dominates(successor, &self.dominators) {
let val = *expected as u128;
let range = WrappingRange { start: val, end: val };
self.insert_range(place, successor, range);
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/mir/ssa-range-prop-bb-self-domination.rs
Comment thread
Human9000-bit marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// Regression test for <https://github.com/rust-lang/rust/issues/155836>.
///
/// SsaRangeProp pass used to fail the assert when encountering self-dominating block
/// e.g. small loops like in `a`

//@ compile-flags: -Copt-level=2
//@ build-pass

use std::hint::black_box;
fn a(d: u8) {
loop {
1 % d;
}
}
pub fn e(d: u8) {
if d == 0 || black_box(false) {
a(d);
}
}
fn main() {
e(1);
}
Loading