-
Notifications
You must be signed in to change notification settings - Fork 77
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
dialects: (arith) adding an arith canonicalisation pattern #2094
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
aac36c2
donezo
dshaaban01 ff84ab8
hiii
dshaaban01 4dc68d5
hii
dshaaban01 aef4b86
Merge branch 'dalia/interactive_individual_rewrite_1' into dalia/inte…
dshaaban01 951fc8b
lil fix
dshaaban01 60c7ea2
Merge branch 'dalia/interactive_individual_rewrite_1' into dalia/inte…
dshaaban01 3eee9ac
sasha + matty b comments
dshaaban01 07d7570
wip
dshaaban01 5dcd006
Merge branch 'dalia/interactive_individual_rewrite_1' into dalia/inte…
dshaaban01 045f7f7
hi
dshaaban01 88aed83
Merge branch 'dalia/interactive_individual_rewrite_1' into dalia/inte…
dshaaban01 bbb867c
hi
dshaaban01 051fcd0
Merge branch 'dalia/interactive_individual_rewrite_1' into dalia/inte…
dshaaban01 b07dd6a
filecheck
dshaaban01 f4ee3bd
add file check
dshaaban01 f4faae6
Merge branch 'dalia/interactive_individual_rewrite_1' into dalia/inte…
dshaaban01 b0893b9
mathieu comment
dshaaban01 b9b208c
sashaaaaa
dshaaban01 21337b0
Merge branch 'dalia/interactive_individual_rewrite_1' into dalia/inte…
dshaaban01 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 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,20 @@ | ||
// RUN: xdsl-opt %s -p canonicalize | filecheck %s | ||
|
||
func.func @hello(%n : i32) -> i32 { | ||
%two = arith.constant 0 : i32 | ||
%three = arith.constant 0 : i32 | ||
%res = arith.addi %two, %n : i32 | ||
%res2 = arith.addi %three, %res : i32 | ||
func.return %res : i32 | ||
} | ||
|
||
|
||
//CHECK: builtin.module { | ||
// CHECK-NEXT: func.func @hello(%n : i32) -> i32 { | ||
// CHECK-NEXT: %two = arith.constant 0 : i32 | ||
// CHECK-NEXT: %three = arith.constant 0 : i32 | ||
// CHECK-NEXT: func.return %n : i32 | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: } | ||
|
||
|
This file contains 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,16 @@ | ||
// RUN:xdsl-opt %s -p 'apply-individual-rewrite{matched_operation_index=4 operation_name="riscv.add" pattern_name="AddImmediates"}'| filecheck %s | ||
|
||
%a = riscv.li 1 : () -> !riscv.reg<> | ||
%b = riscv.li 2 : () -> !riscv.reg<> | ||
%c = riscv.li 3 : () -> !riscv.reg<> | ||
%d = riscv.add %a, %b : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> | ||
%e = riscv.add %b, %c : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> | ||
|
||
|
||
//CHECK: builtin.module { | ||
// CHECK-NEXT: %a = riscv.li 1 : () -> !riscv.reg<> | ||
// CHECK-NEXT: %b = riscv.li 2 : () -> !riscv.reg<> | ||
// CHECK-NEXT: %c = riscv.li 3 : () -> !riscv.reg<> | ||
// CHECK-NEXT: %d = riscv.li 3 : () -> !riscv.reg<> | ||
// CHECK-NEXT: %e = riscv.add %b, %c : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> | ||
// CHECK-NEXT: } |
This file contains 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 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 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 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,18 @@ | ||
from xdsl.dialects import arith | ||
from xdsl.dialects.builtin import IntegerAttr | ||
from xdsl.pattern_rewriter import ( | ||
PatternRewriter, | ||
RewritePattern, | ||
op_type_rewrite_pattern, | ||
) | ||
|
||
|
||
class AddImmediateZero(RewritePattern): | ||
@op_type_rewrite_pattern | ||
def match_and_rewrite(self, op: arith.Addi, rewriter: PatternRewriter) -> None: | ||
if ( | ||
isinstance(op.lhs.owner, arith.Constant) | ||
and isinstance(value := op.lhs.owner.value, IntegerAttr) | ||
and value.value.data == 0 | ||
): | ||
rewriter.replace_matched_op([], [op.rhs]) |
This file contains 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,68 @@ | ||
from dataclasses import dataclass | ||
|
||
from xdsl.dialects.builtin import ModuleOp | ||
from xdsl.ir import MLContext | ||
from xdsl.passes import ModulePass | ||
from xdsl.pattern_rewriter import PatternRewriter, RewritePattern | ||
from xdsl.tools.command_line_tool import get_all_dialects | ||
from xdsl.traits import HasCanonicalisationPatternsTrait | ||
|
||
REWRITE_BY_NAMES: dict[str, dict[str, RewritePattern]] = { | ||
op.name: { | ||
pattern.__class__.__name__: pattern | ||
for pattern in trait.get_canonicalization_patterns() | ||
} | ||
for dialect in get_all_dialects().values() | ||
for op in dialect().operations | ||
if (trait := op.get_trait(HasCanonicalisationPatternsTrait)) is not None | ||
} | ||
""" | ||
Returns a dictionary representing all possible rewrites. Keys are operation names, and | ||
values are dictionaries. In the inner dictionary, the keys are names of patterns | ||
associated with each operation, and the values are the corresponding RewritePattern | ||
instances. | ||
""" | ||
|
||
|
||
@dataclass | ||
class IndividualRewrite(ModulePass): | ||
""" | ||
Module pass representing the application of an individual rewrite pattern to a module. | ||
|
||
Matches the operation at the provided index within the module and applies the rewrite | ||
pattern specified by the operation and pattern names. | ||
""" | ||
|
||
name = "apply-individual-rewrite" | ||
|
||
matched_operation_index: int | None = None | ||
operation_name: str | None = None | ||
pattern_name: str | None = None | ||
|
||
def apply(self, ctx: MLContext, op: ModuleOp) -> None: | ||
assert self.matched_operation_index is not None | ||
assert self.operation_name is not None | ||
assert self.pattern_name is not None | ||
|
||
matched_operation_list = list(op.walk()) | ||
if self.matched_operation_index >= len(matched_operation_list): | ||
raise ValueError("Matched operation index out of range.") | ||
|
||
matched_operation = list(op.walk())[self.matched_operation_index] | ||
rewriter = PatternRewriter(matched_operation) | ||
|
||
rewrite_dictionary = REWRITE_BY_NAMES.get(self.operation_name) | ||
if rewrite_dictionary is None: | ||
raise ValueError( | ||
f"Operation name {self.operation_name} not found in the rewrite dictionary." | ||
) | ||
|
||
pattern = rewrite_dictionary.get(self.pattern_name) | ||
if pattern is None: | ||
raise ValueError( | ||
f"Pattern name {self.pattern_name} not found for the provided operation name." | ||
) | ||
|
||
pattern.match_and_rewrite(matched_operation, rewriter) | ||
if not rewriter.has_done_action: | ||
raise ValueError("Invalid rewrite at current location.") | ||
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.
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.
To me, this feels like a case that should have a warning instead of an exception. It feels like this might make it hard to interact with this pass through scripts and stuff.
Though, I guess people consider exceptional control flow for basic stuff acceptable in Python, so this is just a side comment.