-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
114 additions
and
6 deletions.
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
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,39 @@ | ||
from xdsl.dialects.arith import XOrIOp | ||
from xdsl.pattern_rewriter import ( | ||
PatternRewriter, | ||
RewritePattern, | ||
op_type_rewrite_pattern, | ||
) | ||
from xdsl.transforms.canonicalization_patterns.utils import const_evaluate_operand | ||
|
||
from inconspiquous.dialects.angle import CondNegateAngleOp, ConstantAngleOp | ||
|
||
|
||
class CondNegateAngleOpFoldPattern(RewritePattern): | ||
""" | ||
Fold an angle.cond_negate when both arguments are constant. | ||
""" | ||
|
||
@op_type_rewrite_pattern | ||
def match_and_rewrite(self, op: CondNegateAngleOp, rewriter: PatternRewriter): | ||
if (cond := const_evaluate_operand(op.cond)) is None: | ||
return | ||
if not cond: | ||
rewriter.replace_matched_op((), (op.angle,)) | ||
|
||
if isinstance(op.angle.owner, ConstantAngleOp): | ||
rewriter.replace_matched_op(ConstantAngleOp(-op.angle.owner.angle)) | ||
|
||
|
||
class CondNegateAngleOpAssocPattern(RewritePattern): | ||
""" | ||
Reassociate two conditional negations to a conditional negation on | ||
the xor of the conditions. | ||
""" | ||
|
||
@op_type_rewrite_pattern | ||
def match_and_rewrite(self, op: CondNegateAngleOp, rewriter: PatternRewriter): | ||
if not isinstance(op.angle.owner, CondNegateAngleOp): | ||
return | ||
xor = XOrIOp(op.cond, op.angle.owner.cond) | ||
rewriter.replace_matched_op((xor, CondNegateAngleOp(xor, op.angle.owner.angle))) |
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,28 @@ | ||
// RUN: quopt %s -p canonicalize | filecheck %s | ||
|
||
%c0 = arith.constant false | ||
|
||
%a = "test.op"() : () -> !angle.type | ||
|
||
%b = angle.cond_negate %c0, %a | ||
|
||
// CHECK: "test.op"(%a) {cond_false} : (!angle.type) -> () | ||
"test.op"(%b) {cond_false} : (!angle.type) -> () | ||
|
||
%c1 = arith.constant true | ||
|
||
%c = angle.constant<0.5pi> | ||
|
||
%d = angle.cond_negate %c1, %c | ||
// CHECK: [[const:%.*]] = angle.constant<1.5pi> | ||
// CHECK: "test.op"([[const]]) : (!angle.type) -> () | ||
"test.op"(%d) : (!angle.type) -> () | ||
|
||
%x, %y = "test.op"() : () -> (i1, i1) | ||
|
||
%e = angle.cond_negate %x, %a | ||
%f = angle.cond_negate %y, %e | ||
// CHECK: [[xor:%.*]] = arith.xori %y, %x | ||
// CHECK: [[assoc:%.*]] = angle.cond_negate [[xor]], %a | ||
// CHECK: "test.op"([[assoc]]) : (!angle.type) -> () | ||
"test.op"(%f) : (!angle.type) -> () |
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