Skip to content

Some more graphviz tweaks #140142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,12 @@ struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
// 2. loans made in overlapping scopes do not conflict
// 3. assignments do not affect things loaned out as immutable
// 4. moves do not affect things loaned out in any way
impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a, '_, 'tcx> {
impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a, '_, 'tcx> {
fn visit_after_early_statement_effect(
&mut self,
_results: &mut Results<'tcx, Borrowck<'a, 'tcx>>,
state: &BorrowckDomain,
stmt: &'a Statement<'tcx>,
stmt: &Statement<'tcx>,
location: Location,
) {
debug!("MirBorrowckCtxt::process_statement({:?}, {:?}): {:?}", location, stmt, state);
Expand Down Expand Up @@ -783,7 +783,7 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<
&mut self,
_results: &mut Results<'tcx, Borrowck<'a, 'tcx>>,
state: &BorrowckDomain,
term: &'a Terminator<'tcx>,
term: &Terminator<'tcx>,
loc: Location,
) {
debug!("MirBorrowckCtxt::process_terminator({:?}, {:?}): {:?}", loc, term, state);
Expand Down Expand Up @@ -896,7 +896,7 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<
&mut self,
_results: &mut Results<'tcx, Borrowck<'a, 'tcx>>,
state: &BorrowckDomain,
term: &'a Terminator<'tcx>,
term: &Terminator<'tcx>,
loc: Location,
) {
let span = term.source_info.span;
Expand Down Expand Up @@ -1363,7 +1363,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
fn consume_rvalue(
&mut self,
location: Location,
(rvalue, span): (&'a Rvalue<'tcx>, Span),
(rvalue, span): (&Rvalue<'tcx>, Span),
state: &BorrowckDomain,
) {
match rvalue {
Expand Down Expand Up @@ -1636,7 +1636,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
fn consume_operand(
&mut self,
location: Location,
(operand, span): (&'a Operand<'tcx>, Span),
(operand, span): (&Operand<'tcx>, Span),
state: &BorrowckDomain,
) {
match *operand {
Expand Down
15 changes: 0 additions & 15 deletions compiler/rustc_mir_dataflow/src/framework/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,11 @@ where
self.reachable_blocks.insert_all()
}

/// Returns the underlying `Results`.
pub fn results(&self) -> &Results<'tcx, A> {
&self.results
}

/// Returns the underlying `Results`.
pub fn mut_results(&mut self) -> &mut Results<'tcx, A> {
&mut self.results
}

/// Returns the `Analysis` used to generate the underlying `Results`.
pub fn analysis(&self) -> &A {
&self.results.analysis
}

/// Returns the `Analysis` used to generate the underlying `Results`.
pub fn mut_analysis(&mut self) -> &mut A {
&mut self.results.analysis
}

/// Resets the cursor to hold the entry set for the given basic block.
///
/// For forward dataflow analyses, this is the dataflow state prior to the first statement.
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir_dataflow/src/framework/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub trait Direction {
block: BasicBlock,
block_data: &'mir mir::BasicBlockData<'tcx>,
results: &mut Results<'tcx, A>,
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
vis: &mut impl ResultsVisitor<'tcx, A>,
) where
A: Analysis<'tcx>;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ impl Direction for Backward {
block: BasicBlock,
block_data: &'mir mir::BasicBlockData<'tcx>,
results: &mut Results<'tcx, A>,
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
vis: &mut impl ResultsVisitor<'tcx, A>,
) where
A: Analysis<'tcx>,
{
Expand Down Expand Up @@ -394,7 +394,7 @@ impl Direction for Forward {
block: BasicBlock,
block_data: &'mir mir::BasicBlockData<'tcx>,
results: &mut Results<'tcx, A>,
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
vis: &mut impl ResultsVisitor<'tcx, A>,
) where
A: Analysis<'tcx>,
{
Expand Down
63 changes: 32 additions & 31 deletions compiler/rustc_mir_dataflow/src/framework/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,12 @@ struct Formatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
body: &'mir Body<'tcx>,
// The `RefCell` is used because `<Formatter as Labeller>::node_label`
// takes `&self`, but it needs to modify the cursor. This is also the
// takes `&self`, but it needs to modify the results. This is also the
// reason for the `Formatter`/`BlockFormatter` split; `BlockFormatter` has
// the operations that involve the mutation, i.e. within the `borrow_mut`.
cursor: RefCell<ResultsCursor<'mir, 'tcx, A>>,
results: RefCell<&'mir mut Results<'tcx, A>>,
style: OutputStyle,
reachable: DenseBitSet<BasicBlock>,
}
Expand All @@ -220,11 +221,7 @@ where
style: OutputStyle,
) -> Self {
let reachable = traversal::reachable_as_bitset(body);
Formatter { cursor: results.as_results_cursor(body).into(), style, reachable }
}

fn body(&self) -> &'mir Body<'tcx> {
self.cursor.borrow().body()
Formatter { body, results: results.into(), style, reachable }
}
}

Expand Down Expand Up @@ -253,7 +250,7 @@ where
type Edge = CfgEdge;

fn graph_id(&self) -> dot::Id<'_> {
let name = graphviz_safe_def_name(self.body().source.def_id());
let name = graphviz_safe_def_name(self.body.source.def_id());
dot::Id::new(format!("graph_for_def_id_{name}")).unwrap()
}

Expand All @@ -262,10 +259,16 @@ where
}

fn node_label(&self, block: &Self::Node) -> dot::LabelText<'_> {
let mut cursor = self.cursor.borrow_mut();
let mut fmt =
BlockFormatter { cursor: &mut cursor, style: self.style, bg: Background::Light };
let label = fmt.write_node_label(*block).unwrap();
let mut results = self.results.borrow_mut();

let diffs = StateDiffCollector::run(self.body, *block, *results, self.style);

let mut fmt = BlockFormatter {
cursor: results.as_results_cursor(self.body),
style: self.style,
bg: Background::Light,
};
let label = fmt.write_node_label(*block, diffs).unwrap();

dot::LabelText::html(String::from_utf8(label).unwrap())
}
Expand All @@ -275,7 +278,7 @@ where
}

fn edge_label(&self, e: &Self::Edge) -> dot::LabelText<'_> {
let label = &self.body()[e.source].terminator().kind.fmt_successor_labels()[e.index];
let label = &self.body[e.source].terminator().kind.fmt_successor_labels()[e.index];
dot::LabelText::label(label.clone())
}
}
Expand All @@ -288,7 +291,7 @@ where
type Edge = CfgEdge;

fn nodes(&self) -> dot::Nodes<'_, Self::Node> {
self.body()
self.body
.basic_blocks
.indices()
.filter(|&idx| self.reachable.contains(idx))
Expand All @@ -297,10 +300,10 @@ where
}

fn edges(&self) -> dot::Edges<'_, Self::Edge> {
let body = self.body();
body.basic_blocks
self.body
.basic_blocks
.indices()
.flat_map(|bb| dataflow_successors(body, bb))
.flat_map(|bb| dataflow_successors(self.body, bb))
.collect::<Vec<_>>()
.into()
}
Expand All @@ -310,20 +313,20 @@ where
}

fn target(&self, edge: &Self::Edge) -> Self::Node {
self.body()[edge.source].terminator().successors().nth(edge.index).unwrap()
self.body[edge.source].terminator().successors().nth(edge.index).unwrap()
}
}

struct BlockFormatter<'a, 'mir, 'tcx, A>
struct BlockFormatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
cursor: &'a mut ResultsCursor<'mir, 'tcx, A>,
cursor: ResultsCursor<'mir, 'tcx, A>,
bg: Background,
style: OutputStyle,
}

impl<'tcx, A> BlockFormatter<'_, '_, 'tcx, A>
impl<'tcx, A> BlockFormatter<'_, 'tcx, A>
where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,
Expand All @@ -336,7 +339,11 @@ where
bg
}

fn write_node_label(&mut self, block: BasicBlock) -> io::Result<Vec<u8>> {
fn write_node_label(
&mut self,
block: BasicBlock,
diffs: StateDiffCollector<A::Domain>,
) -> io::Result<Vec<u8>> {
use std::io::Write;

// Sample output:
Expand Down Expand Up @@ -392,7 +399,7 @@ where
self.write_row_with_full_state(w, "", "(on start)")?;

// D + E: Statement and terminator transfer functions
self.write_statements_and_terminator(w, block)?;
self.write_statements_and_terminator(w, block, diffs)?;

// F: State at end of block

Expand Down Expand Up @@ -575,14 +582,8 @@ where
&mut self,
w: &mut impl io::Write,
block: BasicBlock,
diffs: StateDiffCollector<A::Domain>,
) -> io::Result<()> {
let diffs = StateDiffCollector::run(
self.cursor.body(),
block,
self.cursor.mut_results(),
self.style,
);

let mut diffs_before = diffs.before.map(|v| v.into_iter());
let mut diffs_after = diffs.after.into_iter();

Expand Down Expand Up @@ -709,7 +710,7 @@ impl<D> StateDiffCollector<D> {
}
}

impl<'tcx, A> ResultsVisitor<'_, 'tcx, A> for StateDiffCollector<A::Domain>
impl<'tcx, A> ResultsVisitor<'tcx, A> for StateDiffCollector<A::Domain>
where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_dataflow/src/framework/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ where
&mut self,
body: &'mir Body<'tcx>,
blocks: impl IntoIterator<Item = BasicBlock>,
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
vis: &mut impl ResultsVisitor<'tcx, A>,
) {
visit_results(body, blocks, self, vis)
}

pub fn visit_reachable_with<'mir>(
&mut self,
body: &'mir Body<'tcx>,
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
vis: &mut impl ResultsVisitor<'tcx, A>,
) {
let blocks = traversal::reachable(body);
visit_results(body, blocks.map(|(bb, _)| bb), self, vis)
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_mir_dataflow/src/framework/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn visit_results<'mir, 'tcx, A>(
body: &'mir mir::Body<'tcx>,
blocks: impl IntoIterator<Item = BasicBlock>,
results: &mut Results<'tcx, A>,
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
vis: &mut impl ResultsVisitor<'tcx, A>,
) where
A: Analysis<'tcx>,
{
Expand All @@ -29,7 +29,7 @@ pub fn visit_results<'mir, 'tcx, A>(
/// A visitor over the results of an `Analysis`. Use this when you want to inspect domain values in
/// many or all locations; use `ResultsCursor` if you want to inspect domain values only in certain
/// locations.
pub trait ResultsVisitor<'mir, 'tcx, A>
pub trait ResultsVisitor<'tcx, A>
where
A: Analysis<'tcx>,
{
Expand All @@ -40,7 +40,7 @@ where
&mut self,
_results: &mut Results<'tcx, A>,
_state: &A::Domain,
_statement: &'mir mir::Statement<'tcx>,
_statement: &mir::Statement<'tcx>,
_location: Location,
) {
}
Expand All @@ -50,7 +50,7 @@ where
&mut self,
_results: &mut Results<'tcx, A>,
_state: &A::Domain,
_statement: &'mir mir::Statement<'tcx>,
_statement: &mir::Statement<'tcx>,
_location: Location,
) {
}
Expand All @@ -60,7 +60,7 @@ where
&mut self,
_results: &mut Results<'tcx, A>,
_state: &A::Domain,
_terminator: &'mir mir::Terminator<'tcx>,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
) {
}
Expand All @@ -72,7 +72,7 @@ where
&mut self,
_results: &mut Results<'tcx, A>,
_state: &A::Domain,
_terminator: &'mir mir::Terminator<'tcx>,
_terminator: &mir::Terminator<'tcx>,
_location: Location,
) {
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir_dataflow/src/points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ struct Visitor<'a, N: Idx> {
values: SparseIntervalMatrix<N, PointIndex>,
}

impl<'mir, 'tcx, A, N> ResultsVisitor<'mir, 'tcx, A> for Visitor<'_, N>
impl<'tcx, A, N> ResultsVisitor<'tcx, A> for Visitor<'_, N>
where
A: Analysis<'tcx, Domain = DenseBitSet<N>>,
N: Idx,
{
fn visit_after_primary_statement_effect(
fn visit_after_primary_statement_effect<'mir>(
&mut self,
_results: &mut Results<'tcx, A>,
state: &A::Domain,
Expand All @@ -139,7 +139,7 @@ where
});
}

fn visit_after_primary_terminator_effect(
fn visit_after_primary_terminator_effect<'mir>(
&mut self,
_results: &mut Results<'tcx, A>,
state: &A::Domain,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir_transform/src/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,14 +875,14 @@ struct StorageConflictVisitor<'a, 'tcx> {
eligible_storage_live: DenseBitSet<Local>,
}

impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, MaybeRequiresStorage<'a, 'tcx>>
impl<'a, 'tcx> ResultsVisitor<'tcx, MaybeRequiresStorage<'a, 'tcx>>
for StorageConflictVisitor<'a, 'tcx>
{
fn visit_after_early_statement_effect(
&mut self,
_results: &mut Results<'tcx, MaybeRequiresStorage<'a, 'tcx>>,
state: &DenseBitSet<Local>,
_statement: &'a Statement<'tcx>,
_statement: &Statement<'tcx>,
loc: Location,
) {
self.apply_state(state, loc);
Expand All @@ -892,7 +892,7 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, MaybeRequiresStorage<'a, 'tcx>>
&mut self,
_results: &mut Results<'tcx, MaybeRequiresStorage<'a, 'tcx>>,
state: &DenseBitSet<Local>,
_terminator: &'a Terminator<'tcx>,
_terminator: &Terminator<'tcx>,
loc: Location,
) {
self.apply_state(state, loc);
Expand Down
Loading
Loading