-
Notifications
You must be signed in to change notification settings - Fork 5
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
Question: Are you interested to support automatic loop unrolling into batches of iterations? #60
Comments
Yes this seems like a useful feature. Would you be interested in adding it? We would be happy to help guide you on how to do it. If not, we will look into adding it when we get a chance. |
With the caveat that I am quite new to looking at ASTs, I am interested to have a look at it sure. |
No problem :) I think this would be the perfect task for learning more about ASTs as well as this library. It's not too complicated, but requires some knowledge of the nodes and can leverage some of the reusable tools we've built here. First, I'll mention this doc as a useful guide to the python ast: https://greentreesnakes.readthedocs.io/en/latest/nodes.html For ast_tools, we use a specific pass pattern to make it easy to write and compose passes. To write a new pass, you'll subclass the Pass class and define a For this pass, you won't need to use themetadata parameters (at least for now) and so you can simply just return that unchaged/unused. You'll want to take tree and env parameter, modify it to do your loop unrolling, and return the modified tree. Here's the core logic for the current loop unroller: https://github.com/leonardt/ast_tools/blob/master/ast_tools/transformers/loop_unroller.py It defines a visit_For method which will be invoked on For loop nodes inside the AST. It checks if the loop iter can be evaluated to a constant value and that value is an instance of the Here, instead of replacing the loop variable with a constant (ast.Num node), you can instead construct an ast node for the desired expression (e.g. i + 1). I think this could possibly be folded into the same logic as the current unroll pass. Perhaps we can call this macro unroll_by (referring to the constant factor to unroll_by, right now unroll tries to unroll the entire loop). So, the syntax could look something like In this case, we'll probably want to restrict the arguments to be a simple range with a step of 1 for now, and you could rewrite the loop by using the step argument to range, e.g. The let me know if you have any questions or run into trouble, more than happy to help! |
Thanks for that write up it was very helpful. 👍 A prototype is coming along, fairly early days but some experiments with basic ast manipulation look promising so far :) |
Given a trivial code example:
unrolled in batches of say 10 iterations:
The text was updated successfully, but these errors were encountered: