Skip to content
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

Add LowerEqPattern and LowerDistinctPattern for pair types #55

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/filecheck/lower-pairs/distinct_pairs.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: xdsl-smt "%s" -p=lower-pairs | filecheck "%s"

// Lower pairs from a "smt.distinct" operation.

builtin.module {
%0 = "smt.declare_const"() : () -> !smt.utils.pair<!smt.bool, !smt.utils.pair<!smt.bv.bv<32>, !smt.bool>>
%1 = "smt.declare_const"() : () -> !smt.utils.pair<!smt.bool, !smt.utils.pair<!smt.bv.bv<32>, !smt.bool>>
%distinct = "smt.distinct"(%0, %1) : (!smt.utils.pair<!smt.bool, !smt.utils.pair<!smt.bv.bv<32>, !smt.bool>>, !smt.utils.pair<!smt.bool, !smt.utils.pair<!smt.bv.bv<32>, !smt.bool>>) -> !smt.bool
"smt.assert"(%distinct) : (!smt.bool) -> ()
}

// CHECK: %const_first = "smt.declare_const"() : () -> !smt.bool
// CHECK-NEXT: %const_second_first = "smt.declare_const"() : () -> !smt.bv.bv<32>
// CHECK-NEXT: %const_second_second = "smt.declare_const"() : () -> !smt.bool
// CHECK: %const_first_1 = "smt.declare_const"() : () -> !smt.bool
// CHECK-NEXT: %const_second_first_1 = "smt.declare_const"() : () -> !smt.bv.bv<32>
// CHECK-NEXT: %const_second_second_1 = "smt.declare_const"() : () -> !smt.bool
// CHECK-NEXT: %distinct = "smt.distinct"(%const_first, %const_first_1) : (!smt.bool, !smt.bool) -> !smt.bool
// CHECK-NEXT: %distinct_1 = "smt.distinct"(%const_second_first, %const_second_first_1) : (!smt.bv.bv<32>, !smt.bv.bv<32>) -> !smt.bool
// CHECK-NEXT: %distinct_2 = "smt.distinct"(%const_second_second, %const_second_second_1) : (!smt.bool, !smt.bool) -> !smt.bool
// CHECK-NEXT: %distinct_3 = "smt.or"(%distinct_1, %distinct_2) : (!smt.bool, !smt.bool) -> !smt.bool
// CHECK-NEXT: %distinct_4 = "smt.or"(%distinct, %distinct_3) : (!smt.bool, !smt.bool) -> !smt.bool
// CHECK-NEXT: "smt.assert"(%distinct_4) : (!smt.bool) -> ()
23 changes: 23 additions & 0 deletions tests/filecheck/lower-pairs/eq_pairs.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: xdsl-smt "%s" -p=lower-pairs | filecheck "%s"

// Lower pairs from a "smt.eq" operation.

builtin.module {
%0 = "smt.declare_const"() : () -> !smt.utils.pair<!smt.bool, !smt.utils.pair<!smt.bv.bv<32>, !smt.bool>>
%1 = "smt.declare_const"() : () -> !smt.utils.pair<!smt.bool, !smt.utils.pair<!smt.bv.bv<32>, !smt.bool>>
%eq = "smt.eq"(%0, %1) : (!smt.utils.pair<!smt.bool, !smt.utils.pair<!smt.bv.bv<32>, !smt.bool>>, !smt.utils.pair<!smt.bool, !smt.utils.pair<!smt.bv.bv<32>, !smt.bool>>) -> !smt.bool
"smt.assert"(%eq) : (!smt.bool) -> ()
}

// CHECK: %const_first = "smt.declare_const"() : () -> !smt.bool
// CHECK-NEXT: %const_second_first = "smt.declare_const"() : () -> !smt.bv.bv<32>
// CHECK-NEXT: %const_second_second = "smt.declare_const"() : () -> !smt.bool
// CHECK: %const_first_1 = "smt.declare_const"() : () -> !smt.bool
// CHECK-NEXT: %const_second_first_1 = "smt.declare_const"() : () -> !smt.bv.bv<32>
// CHECK-NEXT: %const_second_second_1 = "smt.declare_const"() : () -> !smt.bool
// CHECK-NEXT: %eq = "smt.eq"(%const_first, %const_first_1) : (!smt.bool, !smt.bool) -> !smt.bool
// CHECK-NEXT: %eq_1 = "smt.eq"(%const_second_first, %const_second_first_1) : (!smt.bv.bv<32>, !smt.bv.bv<32>) -> !smt.bool
// CHECK-NEXT: %eq_2 = "smt.eq"(%const_second_second, %const_second_second_1) : (!smt.bool, !smt.bool) -> !smt.bool
// CHECK-NEXT: %eq_3 = "smt.and"(%eq_1, %eq_2) : (!smt.bool, !smt.bool) -> !smt.bool
// CHECK-NEXT: %eq_4 = "smt.and"(%eq, %eq_3) : (!smt.bool, !smt.bool) -> !smt.bool
// CHECK-NEXT: "smt.assert"(%eq_4) : (!smt.bool) -> ()
72 changes: 72 additions & 0 deletions xdsl_smt/passes/lower_pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
DefineFunOp,
ReturnOp,
ForallOp,
EqOp,
DistinctOp,
OrOp,
AndOp,
)
from ..dialects.smt_utils_dialect import (
AnyPairType,
Expand Down Expand Up @@ -199,6 +203,72 @@ def match_and_rewrite(self, op: DeclareConstOp, rewriter: PatternRewriter):
rewriter.replace_op(op, [first, second, pair])


class LowerDistinctPattern(RewritePattern):
@op_type_rewrite_pattern
def match_and_rewrite(self, op: DistinctOp, rewriter: PatternRewriter):
if not isa(op.lhs.type, AnyPairType):
return

"""
(distinct (pair lhsFirst lhsSecond) (pair rhsFirst rhsSecond))
->
(or (distinct lhsFirst rhsFirst) (distinct lhsSecond rhsSecond))
"""
lhsFirst = FirstOp(op.lhs)
lhsSecond = SecondOp(op.lhs)
rhsFirst = FirstOp(op.rhs)
rhsSecond = SecondOp(op.rhs)
firstDistinct = DistinctOp(lhsFirst.res, rhsFirst.res)
secondDistinct = DistinctOp(lhsSecond.res, rhsSecond.res)
orOp = OrOp(firstDistinct.res, secondDistinct.res)

rewriter.replace_op(
op,
[
lhsFirst,
lhsSecond,
rhsFirst,
rhsSecond,
firstDistinct,
secondDistinct,
orOp,
],
)


class LowerEqPattern(RewritePattern):
@op_type_rewrite_pattern
def match_and_rewrite(self, op: EqOp, rewriter: PatternRewriter):
if not isa(op.lhs.type, AnyPairType):
return

"""
(= (pair lhsFirst lhsSecond) (pair rhsFirst rhsSecond))
->
(and (= lhsFirst rhsFirst) (= lhsSecond rhsSecond))
"""
lhsFirst = FirstOp(op.lhs)
lhsSecond = SecondOp(op.lhs)
rhsFirst = FirstOp(op.rhs)
rhsSecond = SecondOp(op.rhs)
firstEq = EqOp(lhsFirst.res, rhsFirst.res)
secondEq = EqOp(lhsSecond.res, rhsSecond.res)
andOp = AndOp(firstEq.res, secondEq.res)

rewriter.replace_op(
op,
[
lhsFirst,
lhsSecond,
rhsFirst,
rhsSecond,
firstEq,
secondEq,
andOp,
],
)


class LowerPairs(ModulePass):
name = "lower-pairs"

Expand All @@ -211,6 +281,8 @@ def apply(self, ctx: MLContext, op: ModuleOp):
RemovePairArgsFunction(),
RemovePairArgsCall(),
LowerDeclareConstPattern(),
LowerEqPattern(),
LowerDistinctPattern(),
]
)
)
Expand Down
Loading