Skip to content

Commit 1ee7582

Browse files
committed
add NLL region graph to the polonius MIR dump
1 parent c2270be commit 1ee7582

File tree

1 file changed

+83
-1
lines changed
  • compiler/rustc_borrowck/src/polonius

1 file changed

+83
-1
lines changed

compiler/rustc_borrowck/src/polonius/dump.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
use std::io;
22

3+
use rustc_data_structures::fx::FxHashSet;
34
use rustc_middle::mir::pretty::{
45
PassWhere, PrettyPrintMirOptions, create_dump_file, dump_enabled, dump_mir_to_writer,
56
};
67
use rustc_middle::mir::{Body, ClosureRegionRequirements};
7-
use rustc_middle::ty::TyCtxt;
8+
use rustc_middle::ty::{RegionVid, TyCtxt};
89
use rustc_session::config::MirIncludeSpans;
910

1011
use crate::borrow_set::BorrowSet;
12+
use crate::constraints::OutlivesConstraint;
1113
use crate::polonius::{LocalizedOutlivesConstraint, LocalizedOutlivesConstraintSet};
14+
use crate::type_check::Locations;
1215
use crate::{BorrowckInferCtxt, RegionInferenceContext};
1316

1417
/// `-Zdump-mir=polonius` dumps MIR annotated with NLL and polonius specific information.
@@ -50,6 +53,7 @@ pub(crate) fn dump_polonius_mir<'tcx>(
5053
/// - the NLL MIR
5154
/// - the list of polonius localized constraints
5255
/// - a mermaid graph of the CFG
56+
/// - a mermaid graph of the NLL regions and the constraints between them
5357
fn emit_polonius_dump<'tcx>(
5458
tcx: TyCtxt<'tcx>,
5559
body: &Body<'tcx>,
@@ -89,6 +93,14 @@ fn emit_polonius_dump<'tcx>(
8993
writeln!(out, "</pre></code>")?;
9094
writeln!(out, "</div>")?;
9195

96+
// Section 3: mermaid visualization of the NLL region graph.
97+
writeln!(out, "<div>")?;
98+
writeln!(out, "NLL regions")?;
99+
writeln!(out, "<pre class='mermaid'>")?;
100+
emit_mermaid_nll_regions(regioncx, out)?;
101+
writeln!(out, "</pre>")?;
102+
writeln!(out, "</div>")?;
103+
92104
// Finalize the dump with the HTML epilogue.
93105
writeln!(
94106
out,
@@ -261,3 +273,73 @@ fn emit_mermaid_cfg(body: &Body<'_>, out: &mut dyn io::Write) -> io::Result<()>
261273

262274
Ok(())
263275
}
276+
277+
/// Emits a region's label: index, universe, external name.
278+
fn render_region(
279+
region: RegionVid,
280+
regioncx: &RegionInferenceContext<'_>,
281+
out: &mut dyn io::Write,
282+
) -> io::Result<()> {
283+
let def = regioncx.region_definition(region);
284+
let universe = def.universe;
285+
286+
write!(out, "'{}", region.as_usize())?;
287+
if !universe.is_root() {
288+
write!(out, "/{universe:?}")?;
289+
}
290+
if let Some(name) = def.external_name.and_then(|e| e.get_name()) {
291+
write!(out, " ({name})")?;
292+
}
293+
Ok(())
294+
}
295+
296+
/// Emits a mermaid flowchart of the NLL regions and the outlives constraints between them, similar
297+
/// to the graphviz version.
298+
fn emit_mermaid_nll_regions<'tcx>(
299+
regioncx: &RegionInferenceContext<'tcx>,
300+
out: &mut dyn io::Write,
301+
) -> io::Result<()> {
302+
// The mermaid chart type: a top-down flowchart.
303+
writeln!(out, "flowchart TD")?;
304+
305+
// Emit the region nodes.
306+
for region in regioncx.var_infos.indices() {
307+
write!(out, "{}[\"", region.as_usize())?;
308+
render_region(region, regioncx, out)?;
309+
writeln!(out, "\"]")?;
310+
}
311+
312+
// Get a set of edges to check for the reverse edge being present.
313+
let edges: FxHashSet<_> = regioncx.outlives_constraints().map(|c| (c.sup, c.sub)).collect();
314+
315+
// Order (and deduplicate) edges for traversal, to display them in a generally increasing order.
316+
let constraint_key = |c: &OutlivesConstraint<'_>| {
317+
let min = c.sup.min(c.sub);
318+
let max = c.sup.max(c.sub);
319+
(min, max)
320+
};
321+
let mut ordered_edges: Vec<_> = regioncx.outlives_constraints().collect();
322+
ordered_edges.sort_by_key(|c| constraint_key(c));
323+
ordered_edges.dedup_by_key(|c| constraint_key(c));
324+
325+
for outlives in ordered_edges {
326+
// Source node.
327+
write!(out, "{} ", outlives.sup.as_usize())?;
328+
329+
// The kind of arrow: bidirectional if the opposite edge exists in the set.
330+
if edges.contains(&(outlives.sub, outlives.sup)) {
331+
write!(out, "&lt;")?;
332+
}
333+
write!(out, "-- ")?;
334+
335+
// Edge label from its `Locations`.
336+
match outlives.locations {
337+
Locations::All(_) => write!(out, "All")?,
338+
Locations::Single(location) => write!(out, "{:?}", location)?,
339+
}
340+
341+
// Target node.
342+
writeln!(out, " --> {}", outlives.sub.as_usize())?;
343+
}
344+
Ok(())
345+
}

0 commit comments

Comments
 (0)