Skip to content

Add --emit-ir-graphviz=output.dot to dump a graphviz dot file #484

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

Closed
fitzgen opened this issue Feb 6, 2017 · 10 comments
Closed

Add --emit-ir-graphviz=output.dot to dump a graphviz dot file #484

fitzgen opened this issue Feb 6, 2017 · 10 comments

Comments

@fitzgen
Copy link
Member

fitzgen commented Feb 6, 2017

Shouldn't be too hard, and I often wish I had this when trying to debug anything involving resolved type references and the like.

There is a crate: https://github.com/GrahamDennis/dot-rust

Here is an intro to dot: http://graphviz.org/pdf/dotguide.pdf

If we didn't want to bring in an extra dependency, it shouldn't be hard to just write text to a file manually.

We would dump each item's attributes as an HTML table in the label for the item, and use TypeCollector to draw edges between items.

Here is a sketch on what an implementation might look like:

let mut dot_file = try!(open_the_specified_file_for_writing());
try!(writeln!(&mut dot_file, "digraph {"));

for (id, item) in ctx.items() {
    try!(writeln!(&mut dot_file, "{} {};", id.0, item.dot_attributes()));

    let mut edges = ItemSet::new();
    item.collect_types(ctx, &mut edges);

    for sub_id in edges {
        try!(writeln!(&mut dot_file, "{} -> {};", id, sub_id));
    }
}

try!(writeln!(&mut dot_file, "}"));

Output should look something like this (we can add more table rows incrementally):

digraph {
    1 [fontname="courier", label=<
       <table border="0">
       <tr><td>ItemId(1)</td></tr>
       <tr><td>name</td><td>Foo</td></tr>
       <tr><td>kind</td><td>Type</td></tr>
       </table>
       >];
    1 -> 2;
    1 -> 3;

    2 [fontname="courier", label=<
       <table border="0">
       <tr><td>ItemId(2)</td></tr>
       <tr><td>name</td><td>Bar</td></tr>
       <tr><td>kind</td><td>Module</td></tr>
       </table>
       >];
    2 -> 3;

    3 [fontname="courier", label=<
       <table border="0">
       <tr><td>ItemId(3)</td></tr>
       <tr><td>name</td><td>Quux</td></tr>
       <tr><td>kind</td><td>Type</td></tr>
       </table>
       >];
}

Which produces an image like this:

graph

@fitzgen
Copy link
Member Author

fitzgen commented Feb 6, 2017

@impowski, maybe you're interested in hacking on this?

@fitzgen
Copy link
Member Author

fitzgen commented Feb 6, 2017

For posterity, I used this command to go from a dot file to a png:

dot -Tpng graph.dot -o graph.png

@emilio
Copy link
Contributor

emilio commented Feb 6, 2017

This would be quite awesome actually, thanks for thinking about this @fitzgen!

@emilio
Copy link
Contributor

emilio commented Feb 6, 2017

Also this would be an easy way to diagnose which of the perf optimizations I mentioned in #59 would be more beneficial.

@fitzgen
Copy link
Member Author

fitzgen commented Feb 6, 2017

Looking at the dot crate's API, I suspect it might be easier to just write the output file directly without using the dot crate.

@impowski
Copy link
Contributor

impowski commented Feb 6, 2017

Wow. It's might be interesting. I guess I'll take it, because I've never done something like this.

@emilio
Copy link
Contributor

emilio commented Feb 6, 2017

If we didn't want to bring in an extra dependency, it shouldn't be hard to just write text to a file manually.

We could also feature-gate it, I guess.

@emilio
Copy link
Contributor

emilio commented Feb 6, 2017

Wow. It's might be interesting. I guess I'll take it, because I've never done something like this.

That's awesome! I'll mark it as assigned then :)

@fitzgen
Copy link
Member Author

fitzgen commented Feb 6, 2017

@impowski Great! Let me know if you need any more pointers or if you run into unexpected roadblocks :)

I very much look forward to having such a feature when debugging!! :-D

@emilio:

@fitzgen:

If we didn't want to bring in an extra dependency, it shouldn't be hard to just write text to a file manually.

We could also feature-gate it, I guess.

I think the dot crate's API just isn't a good fit for what we will be doing, so I don't think it is even worth that.

bors-servo pushed a commit that referenced this issue Feb 15, 2017
Graphviz implementation

This will solve #484 . Right now it's really basic and I will change some of things in future commits like docs and other things.

r? @fitzgen
@fitzgen
Copy link
Member Author

fitzgen commented Mar 6, 2017

This was fixed in #508

@fitzgen fitzgen closed this as completed Mar 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants