Skip to content
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

dot-out: Improvements #1987

Merged
merged 1 commit into from
Aug 3, 2022
Merged

dot-out: Improvements #1987

merged 1 commit into from
Aug 3, 2022

Conversation

JCapucho
Copy link
Collaborator

Had to debug some issues in the spirv frontend and decided to use the dot backend to help me debug it, this quickly snowballed into me hacking in the dot backend.

Improves the dot backend output by:

  • Linking new nodes to the end of other blocks, instead of the beginning
  • Generating merge nodes for conditional statements
Comparison
fn test(a: i32) {
    if (a == a) {
        var b = a;
        var c = b;
    }

    return;
}
Old New
test test
  • Generating connections from break/continue nodes to their target
Comparison
fn test(a: i32) {
    switch(a) {
        default {
            break;
        }
    }
}
Old New
old1 new1
  • Introducing a "cfg only" mode that only generates statements,
    useful for spirv
Showcase
fn test(a: i32) {
    switch(a) {
        default {
            break;
        }
    }
}

cfg-only

Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an improvement (especially the comments), so I think we should merge it, but it doesn't do too well with code like this:

fn f(a: bool, b: bool) -> i32 {
   loop {
      if a {
         break;
      } else {
         break;
      }
      return 5;
   }
   return 0;
}

I think StatementGraph::add needs to return (NodeId, Option<NodeId>), since some blocks never have control flow off the end. For example, a block that ends in a return, or a break within a loop, shouldn't get an outgoing edge from the end.

src/back/dot/mod.rs Outdated Show resolved Hide resolved
cli/src/main.rs Outdated Show resolved Hide resolved
Improves the dot backend output by:
- Linking new nodes to the end of other blocks, instead of the beginning
- Generating merge nodes for conditional statements
- Generating connections from break/continue nodes to their target
- Introducing a "cfg only" mode that only generates statements
@JCapucho
Copy link
Collaborator Author

JCapucho commented Jul 1, 2022

I think StatementGraph::add needs to return (NodeId, Option<NodeId>), since some blocks never have control flow off the end. For example, a block that ends in a return, or a break within a loop, shouldn't get an outgoing edge from the end.

That would be a nice improvement but it isn't so easy because in case a block doesn't have an outgoing edge the graph might be split in two, like this one

test

@jimblandy
Copy link
Member

Doesn't that mean that the other fragment is unreachable? That seems like the right outcome, no?

@JCapucho
Copy link
Collaborator Author

JCapucho commented Jul 8, 2022

Doesn't that mean that the other fragment is unreachable? That seems like the right outcome, no?

I don't think it's a good idea to leave it disconnected from the rest of the control flow graph, Imagine if you had 2 or more random fragments floating around

@jimblandy jimblandy merged commit c6f34fa into gfx-rs:master Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants