Skip to content

Commit 1b9e11e

Browse files
authored
Rollup merge of #140142 - nnethercote:some-graphviz-tweaks-2, r=compiler-errors
Some more graphviz tweaks A follow-up to #132346. r? `@davidtwco`
2 parents 6bc57c6 + 521b379 commit 1b9e11e

File tree

9 files changed

+59
-73
lines changed

9 files changed

+59
-73
lines changed

compiler/rustc_borrowck/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,12 @@ struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
702702
// 2. loans made in overlapping scopes do not conflict
703703
// 3. assignments do not affect things loaned out as immutable
704704
// 4. moves do not affect things loaned out in any way
705-
impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a, '_, 'tcx> {
705+
impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a, '_, 'tcx> {
706706
fn visit_after_early_statement_effect(
707707
&mut self,
708708
_results: &mut Results<'tcx, Borrowck<'a, 'tcx>>,
709709
state: &BorrowckDomain,
710-
stmt: &'a Statement<'tcx>,
710+
stmt: &Statement<'tcx>,
711711
location: Location,
712712
) {
713713
debug!("MirBorrowckCtxt::process_statement({:?}, {:?}): {:?}", location, stmt, state);
@@ -783,7 +783,7 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<
783783
&mut self,
784784
_results: &mut Results<'tcx, Borrowck<'a, 'tcx>>,
785785
state: &BorrowckDomain,
786-
term: &'a Terminator<'tcx>,
786+
term: &Terminator<'tcx>,
787787
loc: Location,
788788
) {
789789
debug!("MirBorrowckCtxt::process_terminator({:?}, {:?}): {:?}", loc, term, state);
@@ -896,7 +896,7 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<
896896
&mut self,
897897
_results: &mut Results<'tcx, Borrowck<'a, 'tcx>>,
898898
state: &BorrowckDomain,
899-
term: &'a Terminator<'tcx>,
899+
term: &Terminator<'tcx>,
900900
loc: Location,
901901
) {
902902
let span = term.source_info.span;
@@ -1363,7 +1363,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
13631363
fn consume_rvalue(
13641364
&mut self,
13651365
location: Location,
1366-
(rvalue, span): (&'a Rvalue<'tcx>, Span),
1366+
(rvalue, span): (&Rvalue<'tcx>, Span),
13671367
state: &BorrowckDomain,
13681368
) {
13691369
match rvalue {
@@ -1636,7 +1636,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
16361636
fn consume_operand(
16371637
&mut self,
16381638
location: Location,
1639-
(operand, span): (&'a Operand<'tcx>, Span),
1639+
(operand, span): (&Operand<'tcx>, Span),
16401640
state: &BorrowckDomain,
16411641
) {
16421642
match *operand {

compiler/rustc_mir_dataflow/src/framework/cursor.rs

-15
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,11 @@ where
114114
self.reachable_blocks.insert_all()
115115
}
116116

117-
/// Returns the underlying `Results`.
118-
pub fn results(&self) -> &Results<'tcx, A> {
119-
&self.results
120-
}
121-
122-
/// Returns the underlying `Results`.
123-
pub fn mut_results(&mut self) -> &mut Results<'tcx, A> {
124-
&mut self.results
125-
}
126-
127117
/// Returns the `Analysis` used to generate the underlying `Results`.
128118
pub fn analysis(&self) -> &A {
129119
&self.results.analysis
130120
}
131121

132-
/// Returns the `Analysis` used to generate the underlying `Results`.
133-
pub fn mut_analysis(&mut self) -> &mut A {
134-
&mut self.results.analysis
135-
}
136-
137122
/// Resets the cursor to hold the entry set for the given basic block.
138123
///
139124
/// For forward dataflow analyses, this is the dataflow state prior to the first statement.

compiler/rustc_mir_dataflow/src/framework/direction.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub trait Direction {
4343
block: BasicBlock,
4444
block_data: &'mir mir::BasicBlockData<'tcx>,
4545
results: &mut Results<'tcx, A>,
46-
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
46+
vis: &mut impl ResultsVisitor<'tcx, A>,
4747
) where
4848
A: Analysis<'tcx>;
4949
}
@@ -212,7 +212,7 @@ impl Direction for Backward {
212212
block: BasicBlock,
213213
block_data: &'mir mir::BasicBlockData<'tcx>,
214214
results: &mut Results<'tcx, A>,
215-
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
215+
vis: &mut impl ResultsVisitor<'tcx, A>,
216216
) where
217217
A: Analysis<'tcx>,
218218
{
@@ -394,7 +394,7 @@ impl Direction for Forward {
394394
block: BasicBlock,
395395
block_data: &'mir mir::BasicBlockData<'tcx>,
396396
results: &mut Results<'tcx, A>,
397-
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
397+
vis: &mut impl ResultsVisitor<'tcx, A>,
398398
) where
399399
A: Analysis<'tcx>,
400400
{

compiler/rustc_mir_dataflow/src/framework/graphviz.rs

+32-31
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@ struct Formatter<'mir, 'tcx, A>
201201
where
202202
A: Analysis<'tcx>,
203203
{
204+
body: &'mir Body<'tcx>,
204205
// The `RefCell` is used because `<Formatter as Labeller>::node_label`
205-
// takes `&self`, but it needs to modify the cursor. This is also the
206+
// takes `&self`, but it needs to modify the results. This is also the
206207
// reason for the `Formatter`/`BlockFormatter` split; `BlockFormatter` has
207208
// the operations that involve the mutation, i.e. within the `borrow_mut`.
208-
cursor: RefCell<ResultsCursor<'mir, 'tcx, A>>,
209+
results: RefCell<&'mir mut Results<'tcx, A>>,
209210
style: OutputStyle,
210211
reachable: DenseBitSet<BasicBlock>,
211212
}
@@ -220,11 +221,7 @@ where
220221
style: OutputStyle,
221222
) -> Self {
222223
let reachable = traversal::reachable_as_bitset(body);
223-
Formatter { cursor: results.as_results_cursor(body).into(), style, reachable }
224-
}
225-
226-
fn body(&self) -> &'mir Body<'tcx> {
227-
self.cursor.borrow().body()
224+
Formatter { body, results: results.into(), style, reachable }
228225
}
229226
}
230227

@@ -253,7 +250,7 @@ where
253250
type Edge = CfgEdge;
254251

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

@@ -262,10 +259,16 @@ where
262259
}
263260

264261
fn node_label(&self, block: &Self::Node) -> dot::LabelText<'_> {
265-
let mut cursor = self.cursor.borrow_mut();
266-
let mut fmt =
267-
BlockFormatter { cursor: &mut cursor, style: self.style, bg: Background::Light };
268-
let label = fmt.write_node_label(*block).unwrap();
262+
let mut results = self.results.borrow_mut();
263+
264+
let diffs = StateDiffCollector::run(self.body, *block, *results, self.style);
265+
266+
let mut fmt = BlockFormatter {
267+
cursor: results.as_results_cursor(self.body),
268+
style: self.style,
269+
bg: Background::Light,
270+
};
271+
let label = fmt.write_node_label(*block, diffs).unwrap();
269272

270273
dot::LabelText::html(String::from_utf8(label).unwrap())
271274
}
@@ -275,7 +278,7 @@ where
275278
}
276279

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

290293
fn nodes(&self) -> dot::Nodes<'_, Self::Node> {
291-
self.body()
294+
self.body
292295
.basic_blocks
293296
.indices()
294297
.filter(|&idx| self.reachable.contains(idx))
@@ -297,10 +300,10 @@ where
297300
}
298301

299302
fn edges(&self) -> dot::Edges<'_, Self::Edge> {
300-
let body = self.body();
301-
body.basic_blocks
303+
self.body
304+
.basic_blocks
302305
.indices()
303-
.flat_map(|bb| dataflow_successors(body, bb))
306+
.flat_map(|bb| dataflow_successors(self.body, bb))
304307
.collect::<Vec<_>>()
305308
.into()
306309
}
@@ -310,20 +313,20 @@ where
310313
}
311314

312315
fn target(&self, edge: &Self::Edge) -> Self::Node {
313-
self.body()[edge.source].terminator().successors().nth(edge.index).unwrap()
316+
self.body[edge.source].terminator().successors().nth(edge.index).unwrap()
314317
}
315318
}
316319

317-
struct BlockFormatter<'a, 'mir, 'tcx, A>
320+
struct BlockFormatter<'mir, 'tcx, A>
318321
where
319322
A: Analysis<'tcx>,
320323
{
321-
cursor: &'a mut ResultsCursor<'mir, 'tcx, A>,
324+
cursor: ResultsCursor<'mir, 'tcx, A>,
322325
bg: Background,
323326
style: OutputStyle,
324327
}
325328

326-
impl<'tcx, A> BlockFormatter<'_, '_, 'tcx, A>
329+
impl<'tcx, A> BlockFormatter<'_, 'tcx, A>
327330
where
328331
A: Analysis<'tcx>,
329332
A::Domain: DebugWithContext<A>,
@@ -336,7 +339,11 @@ where
336339
bg
337340
}
338341

339-
fn write_node_label(&mut self, block: BasicBlock) -> io::Result<Vec<u8>> {
342+
fn write_node_label(
343+
&mut self,
344+
block: BasicBlock,
345+
diffs: StateDiffCollector<A::Domain>,
346+
) -> io::Result<Vec<u8>> {
340347
use std::io::Write;
341348

342349
// Sample output:
@@ -392,7 +399,7 @@ where
392399
self.write_row_with_full_state(w, "", "(on start)")?;
393400

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

397404
// F: State at end of block
398405

@@ -575,14 +582,8 @@ where
575582
&mut self,
576583
w: &mut impl io::Write,
577584
block: BasicBlock,
585+
diffs: StateDiffCollector<A::Domain>,
578586
) -> io::Result<()> {
579-
let diffs = StateDiffCollector::run(
580-
self.cursor.body(),
581-
block,
582-
self.cursor.mut_results(),
583-
self.style,
584-
);
585-
586587
let mut diffs_before = diffs.before.map(|v| v.into_iter());
587588
let mut diffs_after = diffs.after.into_iter();
588589

@@ -709,7 +710,7 @@ impl<D> StateDiffCollector<D> {
709710
}
710711
}
711712

712-
impl<'tcx, A> ResultsVisitor<'_, 'tcx, A> for StateDiffCollector<A::Domain>
713+
impl<'tcx, A> ResultsVisitor<'tcx, A> for StateDiffCollector<A::Domain>
713714
where
714715
A: Analysis<'tcx>,
715716
A::Domain: DebugWithContext<A>,

compiler/rustc_mir_dataflow/src/framework/results.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ where
4747
&mut self,
4848
body: &'mir Body<'tcx>,
4949
blocks: impl IntoIterator<Item = BasicBlock>,
50-
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
50+
vis: &mut impl ResultsVisitor<'tcx, A>,
5151
) {
5252
visit_results(body, blocks, self, vis)
5353
}
5454

5555
pub fn visit_reachable_with<'mir>(
5656
&mut self,
5757
body: &'mir Body<'tcx>,
58-
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
58+
vis: &mut impl ResultsVisitor<'tcx, A>,
5959
) {
6060
let blocks = traversal::reachable(body);
6161
visit_results(body, blocks.map(|(bb, _)| bb), self, vis)

compiler/rustc_mir_dataflow/src/framework/visitor.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn visit_results<'mir, 'tcx, A>(
88
body: &'mir mir::Body<'tcx>,
99
blocks: impl IntoIterator<Item = BasicBlock>,
1010
results: &mut Results<'tcx, A>,
11-
vis: &mut impl ResultsVisitor<'mir, 'tcx, A>,
11+
vis: &mut impl ResultsVisitor<'tcx, A>,
1212
) where
1313
A: Analysis<'tcx>,
1414
{
@@ -29,7 +29,7 @@ pub fn visit_results<'mir, 'tcx, A>(
2929
/// A visitor over the results of an `Analysis`. Use this when you want to inspect domain values in
3030
/// many or all locations; use `ResultsCursor` if you want to inspect domain values only in certain
3131
/// locations.
32-
pub trait ResultsVisitor<'mir, 'tcx, A>
32+
pub trait ResultsVisitor<'tcx, A>
3333
where
3434
A: Analysis<'tcx>,
3535
{
@@ -40,7 +40,7 @@ where
4040
&mut self,
4141
_results: &mut Results<'tcx, A>,
4242
_state: &A::Domain,
43-
_statement: &'mir mir::Statement<'tcx>,
43+
_statement: &mir::Statement<'tcx>,
4444
_location: Location,
4545
) {
4646
}
@@ -50,7 +50,7 @@ where
5050
&mut self,
5151
_results: &mut Results<'tcx, A>,
5252
_state: &A::Domain,
53-
_statement: &'mir mir::Statement<'tcx>,
53+
_statement: &mir::Statement<'tcx>,
5454
_location: Location,
5555
) {
5656
}
@@ -60,7 +60,7 @@ where
6060
&mut self,
6161
_results: &mut Results<'tcx, A>,
6262
_state: &A::Domain,
63-
_terminator: &'mir mir::Terminator<'tcx>,
63+
_terminator: &mir::Terminator<'tcx>,
6464
_location: Location,
6565
) {
6666
}
@@ -72,7 +72,7 @@ where
7272
&mut self,
7373
_results: &mut Results<'tcx, A>,
7474
_state: &A::Domain,
75-
_terminator: &'mir mir::Terminator<'tcx>,
75+
_terminator: &mir::Terminator<'tcx>,
7676
_location: Location,
7777
) {
7878
}

compiler/rustc_mir_dataflow/src/points.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ struct Visitor<'a, N: Idx> {
120120
values: SparseIntervalMatrix<N, PointIndex>,
121121
}
122122

123-
impl<'mir, 'tcx, A, N> ResultsVisitor<'mir, 'tcx, A> for Visitor<'_, N>
123+
impl<'tcx, A, N> ResultsVisitor<'tcx, A> for Visitor<'_, N>
124124
where
125125
A: Analysis<'tcx, Domain = DenseBitSet<N>>,
126126
N: Idx,
127127
{
128-
fn visit_after_primary_statement_effect(
128+
fn visit_after_primary_statement_effect<'mir>(
129129
&mut self,
130130
_results: &mut Results<'tcx, A>,
131131
state: &A::Domain,
@@ -139,7 +139,7 @@ where
139139
});
140140
}
141141

142-
fn visit_after_primary_terminator_effect(
142+
fn visit_after_primary_terminator_effect<'mir>(
143143
&mut self,
144144
_results: &mut Results<'tcx, A>,
145145
state: &A::Domain,

compiler/rustc_mir_transform/src/coroutine.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -875,14 +875,14 @@ struct StorageConflictVisitor<'a, 'tcx> {
875875
eligible_storage_live: DenseBitSet<Local>,
876876
}
877877

878-
impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, MaybeRequiresStorage<'a, 'tcx>>
878+
impl<'a, 'tcx> ResultsVisitor<'tcx, MaybeRequiresStorage<'a, 'tcx>>
879879
for StorageConflictVisitor<'a, 'tcx>
880880
{
881881
fn visit_after_early_statement_effect(
882882
&mut self,
883883
_results: &mut Results<'tcx, MaybeRequiresStorage<'a, 'tcx>>,
884884
state: &DenseBitSet<Local>,
885-
_statement: &'a Statement<'tcx>,
885+
_statement: &Statement<'tcx>,
886886
loc: Location,
887887
) {
888888
self.apply_state(state, loc);
@@ -892,7 +892,7 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, MaybeRequiresStorage<'a, 'tcx>>
892892
&mut self,
893893
_results: &mut Results<'tcx, MaybeRequiresStorage<'a, 'tcx>>,
894894
state: &DenseBitSet<Local>,
895-
_terminator: &'a Terminator<'tcx>,
895+
_terminator: &Terminator<'tcx>,
896896
loc: Location,
897897
) {
898898
self.apply_state(state, loc);

0 commit comments

Comments
 (0)