-
Notifications
You must be signed in to change notification settings - Fork 1
Rewrite rule from scf to cf #404
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
Open
weinbe58
wants to merge
7
commits into
main
Choose a base branch
from
181-rewrite-rule-from-scf-to-cf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
df901e1
finish implementation with test
weinbe58 dade63f
refactor function
weinbe58 5b9f460
adding test for for-loop
weinbe58 05f1fe6
bump version
Roger-luo 53b88f4
feat: add result value to function (#403)
Roger-luo ad2238e
removing script
weinbe58 ff45746
fixing return value
weinbe58 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 hidden or 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 hidden or 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 hidden or 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,189 @@ | ||
from ... import ir | ||
from .stmts import For, Yield, IfElse | ||
from ...rewrite.abc import RewriteRule, RewriteResult | ||
|
||
|
||
class ScfToCfRule(RewriteRule): | ||
|
||
def rewrite_ifelse( | ||
self, node: ir.Region, block_idx: int, curr_block: ir.Block, stmt: IfElse | ||
): | ||
from kirin.dialects import cf | ||
|
||
# create a new block for entering the if statement | ||
entry_block = ir.Block() | ||
for arg in curr_block.args: | ||
arg.replace_by(entry_block.args.append_from(arg.type, arg.name)) | ||
|
||
# delete the args of the old block and replace with the result of the # if statement | ||
for arg in curr_block.args: | ||
curr_block.args.delete(arg) | ||
|
||
for arg in stmt.results: | ||
arg.replace_by(curr_block.args.append_from(arg.type, arg.name)) | ||
|
||
(then_block := stmt.then_body.blocks[0]).detach() | ||
(else_block := stmt.else_body.blocks[0]).detach() | ||
|
||
entry_block.stmts.append( | ||
cf.ConditionalBranch( | ||
cond=stmt.cond, | ||
then_arguments=tuple(stmt.args), | ||
then_successor=then_block, | ||
else_arguments=tuple(stmt.args), | ||
else_successor=else_block, | ||
) | ||
) | ||
|
||
# insert the then/else blocks and add branch to the current block | ||
# if the last statement of the then block is a yield | ||
if isinstance(last_stmt := else_block.last_stmt, Yield): | ||
last_stmt.replace_by( | ||
cf.Branch( | ||
arguments=tuple(last_stmt.args), | ||
successor=curr_block, | ||
) | ||
) | ||
|
||
if isinstance(last_stmt := then_block.last_stmt, Yield): | ||
last_stmt.replace_by( | ||
cf.Branch( | ||
arguments=tuple(last_stmt.args), | ||
successor=curr_block, | ||
) | ||
) | ||
|
||
node.blocks.insert(block_idx, curr_block) | ||
node.blocks.insert(block_idx, else_block) | ||
node.blocks.insert(block_idx, then_block) | ||
|
||
curr_stmt = stmt | ||
next_stmt = stmt.prev_stmt | ||
curr_stmt.delete() | ||
|
||
return next_stmt, entry_block | ||
|
||
def rewrite_for( | ||
self, node: ir.Region, block_idx: int, curr_block: ir.Block, stmt: For | ||
): | ||
from kirin.dialects import cf, py, func | ||
|
||
(body_block := stmt.body.blocks[0]).detach() | ||
|
||
entry_block = ir.Block() | ||
for arg in curr_block.args: | ||
arg.replace_by(entry_block.args.append_from(arg.type, arg.name)) | ||
|
||
# Get iterator from iterable object | ||
entry_block.stmts.append(iterable_stmt := py.iterable.Iter(stmt.iterable)) | ||
entry_block.stmts.append(const_none := func.ConstantNone()) | ||
last_stmt = entry_block.last_stmt | ||
entry_block.stmts.append( | ||
next_stmt := py.iterable.Next(iterable_stmt.expect_one_result()) | ||
) | ||
entry_block.stmts.append( | ||
loop_cmp := py.cmp.Is(next_stmt.expect_one_result(), const_none.result) | ||
) | ||
entry_block.stmts.append( | ||
cf.ConditionalBranch( | ||
cond=loop_cmp.result, | ||
then_arguments=tuple(stmt.initializers), | ||
then_successor=curr_block, | ||
else_arguments=(next_stmt.expect_one_result(),) + tuple(stmt.args), | ||
else_successor=body_block, | ||
) | ||
) | ||
|
||
for arg in curr_block.args: | ||
curr_block.args.delete(arg) | ||
|
||
for arg in stmt.results: | ||
arg.replace_by(curr_block.args.append_from(arg.type, arg.name)) | ||
|
||
if isinstance(last_stmt := body_block.last_stmt, Yield): | ||
( | ||
next_stmt := py.iterable.Next(iterable_stmt.expect_one_result()) | ||
).insert_before(last_stmt) | ||
( | ||
loop_cmp := py.cmp.Is(next_stmt.expect_one_result(), const_none.result) | ||
).insert_before(last_stmt) | ||
last_stmt.replace_by( | ||
cf.ConditionalBranch( | ||
cond=loop_cmp.result, | ||
then_arguments=(next_stmt.expect_one_result(),) | ||
+ tuple(last_stmt.args), | ||
then_successor=curr_block, | ||
else_arguments=tuple(last_stmt.args), | ||
else_successor=body_block, | ||
) | ||
) | ||
|
||
# insert the body block and add branch to the current block | ||
node.blocks.insert(block_idx, curr_block) | ||
node.blocks.insert(block_idx, body_block) | ||
|
||
curr_stmt = stmt | ||
next_stmt = stmt.prev_stmt | ||
curr_stmt.delete() | ||
|
||
return next_stmt, entry_block | ||
|
||
def rewrite_ssacfg(self, node: ir.Region): | ||
|
||
has_done_something = False | ||
|
||
for block_idx in range(len(node.blocks)): | ||
|
||
block = node.blocks.pop(block_idx) | ||
|
||
stmt = block.last_stmt | ||
if stmt is None: | ||
continue | ||
|
||
curr_block = ir.Block() | ||
|
||
for arg in block.args: | ||
arg.replace_by(curr_block.args.append_from(arg.type, arg.name)) | ||
|
||
while stmt is not None: | ||
if isinstance(stmt, For): | ||
has_done_something = True | ||
stmt, curr_block = self.rewrite_for( | ||
node, block_idx, curr_block, stmt | ||
) | ||
|
||
elif isinstance(stmt, IfElse): | ||
has_done_something = True | ||
stmt, curr_block = self.rewrite_ifelse( | ||
node, block_idx, curr_block, stmt | ||
) | ||
else: | ||
curr_stmt = stmt | ||
stmt = stmt.prev_stmt | ||
curr_stmt.detach() | ||
|
||
if curr_block.first_stmt is None: | ||
curr_block.stmts.append(curr_stmt) | ||
else: | ||
curr_stmt.insert_before(curr_block.first_stmt) | ||
|
||
# if the last block is empty, remove it | ||
if curr_block.parent is None and curr_block.first_stmt is not None: | ||
node.blocks.insert(block_idx, curr_block) | ||
|
||
return RewriteResult(has_done_something=has_done_something) | ||
|
||
def rewrite_Statement(self, node: ir.Statement) -> RewriteResult: | ||
if ( | ||
isinstance(node, (For, IfElse)) | ||
or not node.has_trait(ir.HasCFG) | ||
and not node.has_trait(ir.SSACFG) | ||
): | ||
# do not do rewrite in scf regions | ||
return RewriteResult() | ||
|
||
result = RewriteResult() | ||
for region in node.regions: | ||
result = result.join(self.rewrite_ssacfg(region)) | ||
|
||
return result |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.