-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
make MIR graphviz generation use gsgdt #78399
Merged
+103
−136
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ea14607
make MIR graphviz generation use gsgdt
vn-ki 5b049e1
write to a String instead to reduce churn
vn-ki a4e94ec
update gsgdt
vn-ki 86a7831
formatting
vn-ki 51ecb96
add different color for cleanup nodes in dark mode
vn-ki 6fe31e7
fix clippy test
vn-ki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use gsgdt::{Edge, Graph, Node, NodeStyle}; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_index::vec::Idx; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::ty::TyCtxt; | ||
|
||
/// Convert an MIR function into a gsgdt Graph | ||
pub fn mir_fn_to_generic_graph<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'_>) -> Graph { | ||
let def_id = body.source.def_id(); | ||
let def_name = graphviz_safe_def_name(def_id); | ||
let graph_name = format!("Mir_{}", def_name); | ||
let dark_mode = tcx.sess.opts.debugging_opts.graphviz_dark_mode; | ||
|
||
// Nodes | ||
let nodes: Vec<Node> = body | ||
.basic_blocks() | ||
.iter_enumerated() | ||
.map(|(block, _)| bb_to_graph_node(block, body, dark_mode)) | ||
.collect(); | ||
|
||
// Edges | ||
let mut edges = Vec::new(); | ||
for (source, _) in body.basic_blocks().iter_enumerated() { | ||
let def_id = body.source.def_id(); | ||
let terminator = body[source].terminator(); | ||
let labels = terminator.kind.fmt_successor_labels(); | ||
|
||
for (&target, label) in terminator.successors().zip(labels) { | ||
let src = node(def_id, source); | ||
let trg = node(def_id, target); | ||
edges.push(Edge::new(src, trg, label.to_string())); | ||
} | ||
} | ||
|
||
Graph::new(graph_name, nodes, edges) | ||
} | ||
|
||
fn bb_to_graph_node(block: BasicBlock, body: &Body<'_>, dark_mode: bool) -> Node { | ||
let def_id = body.source.def_id(); | ||
let data = &body[block]; | ||
let label = node(def_id, block); | ||
|
||
let (title, bgcolor) = if data.is_cleanup { | ||
(format!("{} (cleanup)", block.index()), "lightblue") | ||
} else { | ||
let color = if dark_mode { "dimgray" } else { "gray" }; | ||
(format!("{}", block.index()), color) | ||
}; | ||
|
||
let style = NodeStyle { title_bg: Some(bgcolor.to_owned()), ..Default::default() }; | ||
let mut stmts: Vec<String> = data.statements.iter().map(|x| format!("{:?}", x)).collect(); | ||
|
||
// add the terminator to the stmts, gsgdt can print it out seperately | ||
let mut terminator_head = String::new(); | ||
data.terminator().kind.fmt_head(&mut terminator_head).unwrap(); | ||
stmts.push(terminator_head); | ||
|
||
Node::new(stmts, label, title, style) | ||
} | ||
|
||
// Must match `[0-9A-Za-z_]*`. This does not appear in the rendered graph, so | ||
// it does not have to be user friendly. | ||
pub fn graphviz_safe_def_name(def_id: DefId) -> String { | ||
format!("{}_{}", def_id.krate.index(), def_id.index.index(),) | ||
} | ||
|
||
fn node(def_id: DefId, block: BasicBlock) -> String { | ||
format!("bb{}__{}", block.index(), graphviz_safe_def_name(def_id)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed you missed a bug fix during the rebase conflict resolution:
This should be:
See line 116 in the deleted sections of graphviz.rs