-
Notifications
You must be signed in to change notification settings - Fork 7
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
implementation of an AST -> SCFG transformer #114
Conversation
AST: Python Abstract Synatx Tree (S)CFG: (Structured) Control Flow Graph Items to note for reviewers: * Most of changes are in the `ast_transforms.py` module which contains the meat of the implementation. * Temporary data structures are created for a pure AST based CFG (`ASTCFG`) with blocks that are writable. This made it much easier to implement the builder pattern. They can easily be converted to a SCFG using the `ASTCFG.to_SCFG()` function. * The `AST2SCFGTranformer` is stateful and is populated with housekeeping data structures post transform. * The tests are all in `test_ast_transforms.py`. They are of the usual format: Python function as input and serialized graph to compare to. * The tests also check the values of the unreachable and empty nodes that have been pruned from the CFG. * The for-loop decomposition is a bit involved, hence there is an extensive docstring to describe what it does. * Some minor changes had to be made to the `scfg.py` module to support displaying "broken" SCFGs during development. This makes it easier to visually debug the graph upon making programming mistakes. * The modules `basic_block.py` and `rendering.py` were modified to support construction and visualization of the `PythonASTBlock`, which is a new block type for the SCFG that contains a block derived from Python AST. * The central entrypoint is: `AST2CFG(function) -> SCFG `. This will turn a Python function into an SCFG. * A stub has been added for the invserse, SCFG -> AST transformer. Example: ```python from numba_rvsdg import AST2SCFG def fun(): ... scfg = AST2SCFG(fun) scfg.render() scfg.restructure() scfg.render() ```
@brandonwillard have a look at the above ☝️ may be of interest to you. |
This is fantastic! |
As title
As title
As title
This reduces the graph even more, since blocks with a single break or continue become empty blocks and are pruned
Coverage report for caa5ea2
|
As title
The jump_targets for the else-branch of the for-loop must be set after recursion, not before. Otherwise nested flow-control constructs in the else-branch will fail to codegen correctly, leading to a broken CFG with blocks that point to non-existing blocks. The test exposes this and demonstrates the fix is valid.
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.
Just some minor adjustments. The biggest thing is for loop orelse
need to be sealed.
This test exposes a bug in a missing seal for using continue in the nested for-else clause. Specifically, one needs to have an else-clause with a series of linear statements and a continue to hit this. If you try to break, the break index will be the same as the exit index for the seal_inside_of_loop as the default_index is also the exit_index. So you can only hit this with exit_index. In that case the block with the continue does not point to the loop header but to the incorrect exit instead. The test has been constructed such that a potential reconstruction would also fail execution with an incorrect return value.
As title
As title
As title
As title
@sklam I have addressed all the review comments and added most of the features we spoke about OOB. The only thing I don't know how to decide on is how to further structure the API. In any case, the transformations are implemented and work so maybe we need to defer decisions about the API to a later stage. |
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.
Some nitpicks regarding accept AST as input. Everything else looks good.
As per an OOB discussion with @sklam I have addressed all the requested improvements and am merging this now! Thank you for the review. |
AST: Python Abstract Synatx Tree
(S)CFG: (Structured) Control Flow Graph
Items to note for reviewers:
ast_transforms.py
module which contains the meat of the implementation.ASTCFG
) with blocks that are writable. This made it much easier to implement the builder pattern. They can easily be converted to a SCFG using theASTCFG.to_SCFG()
function.AST2SCFGTranformer
is stateful and is populated with housekeeping data structures post transform.test_ast_transforms.py
. They are of the usual format: Python function as input and serialized graph to compare to.scfg.py
module to support displaying "broken" SCFGs during development. This makes it easier to visually debug the graph upon making programming mistakes.basic_block.py
andrendering.py
were modified to support construction and visualization of thePythonASTBlock
, which is a new block type for the SCFG that contains a block derived from Python AST.AST2CFG(function) -> SCFG
. This will turn a Python function into an SCFG.Example: