-
Notifications
You must be signed in to change notification settings - Fork 7
Add lowerings from smt tensor to low-level smt #82
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9f1ce7d
Add lowereings from smt tensor to low-level smt
Hatsunespica 28851a9
Fix format
Hatsunespica 9d5f9ac
Fix format
Hatsunespica d608953
Remove an accident change
Hatsunespica 4714f37
Fix pyright
Hatsunespica 817e0f2
Update existing code and add tests
Hatsunespica 2aeeb68
Fix format
Hatsunespica 92b7075
Add file
Hatsunespica 4acc20f
Fix format
Hatsunespica 7711518
Fix format in tests
Hatsunespica 47093f8
Fix new line
Hatsunespica 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // RUN: xdsl-smt "%s" -p=lower-smt-tensor -t=smt | filecheck "%s" | ||
|
|
||
| // Lower tensor from "smt.tensor" operations. | ||
|
|
||
| builtin.module { | ||
| %0 = "smt.declare_const"() : () -> !smt.tensor.tensor<[3, 3], !smt.fp<8, 24>, none> | ||
| // CHECK: (declare-const $tmp (Array (_ BitVec 64) (Array (_ BitVec 64) (_ FloatingPoint 8 24)) | ||
|
|
||
| %idx1 = "smt.declare_const"() : () -> !smt.bv<64> | ||
| %idx2 = "smt.declare_const"() : () -> !smt.bv<64> | ||
| // CHECK-NEXT: (declare-const $idx1 (_ BitVec 64)) | ||
| // CHECK-NEXT: (declare-const $idx2 (_ BitVec 64)) | ||
|
|
||
| %extract = "smt.tensor.extract"(%0, %idx1, %idx2): | ||
| (!smt.tensor.tensor<[3, 3], !smt.fp<8, 24>, none>, !smt.bv<64>, !smt.bv<64>) -> !smt.fp<8, 24> | ||
| %zero = "smt.fp.pzero"() : () -> !smt.fp<8,24> | ||
| %eq_zero = "smt.eq"(%extract, %zero) : (!smt.fp<8,24>, !smt.fp<8,24>) -> !smt.bool | ||
| "smt.assert"(%eq_zero) : (!smt.bool) -> () | ||
| // CHECK-NEXT: (assert (= (select (select $tmp $idx1) $idx2) (_ +zero 8 24))) | ||
| } |
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,18 @@ | ||
| // RUN: xdsl-smt "%s" -p=rewrite-smt-tensor | filecheck "%s" | ||
|
|
||
| // Rewrite tensor from "smt.extract" operations. | ||
|
|
||
| builtin.module { | ||
| %0 = "smt.declare_const"() : () -> !smt.tensor.tensor<[3, 3], !smt.fp<8, 24>, none> | ||
| %idx1 = "smt.declare_const"() : () -> !smt.bv<64> | ||
| %idx2 = "smt.declare_const"() : () -> !smt.bv<64> | ||
| %transpose = "smt.tensor.transpose"(%0){permutation = [1, 0]} : | ||
| (!smt.tensor.tensor<[3, 3], !smt.fp<8, 24>, none>) -> !smt.tensor.tensor<[3, 3], !smt.fp<8, 24>, none> | ||
| %extract = "smt.tensor.extract"(%transpose, %idx1, %idx2): | ||
| (!smt.tensor.tensor<[3, 3], !smt.fp<8, 24>, none>, !smt.bv<64>, !smt.bv<64>) -> !smt.fp<8, 24> | ||
| } | ||
|
|
||
| // CHECK: %0 = "smt.declare_const"() : () -> !smt.tensor.tensor<[3 : i64, 3 : i64], !smt.fp<8, 24>, none> | ||
| // CHECK-NEXT: %idx1 = "smt.declare_const"() : () -> !smt.bv<64> | ||
| // CHECK-NEXT: %idx2 = "smt.declare_const"() : () -> !smt.bv<64> | ||
| // CHECK-NEXT: %extract = "smt.tensor.extract"(%0, %idx2, %idx1) : (!smt.tensor.tensor<[3 : i64, 3 : i64], !smt.fp<8, 24>, none>, !smt.bv<64>, !smt.bv<64>) -> !smt.fp<8, 24> |
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
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,64 @@ | ||
| from xdsl_smt.dialects import smt_array_dialect as smt_array | ||
|
|
||
| from xdsl_smt.dialects.smt_dialect import ( | ||
| DeclareConstOp, | ||
| ) | ||
| from xdsl_smt.dialects.smt_tensor_dialect import ( | ||
| IndexType, | ||
| SMTTensorType, | ||
| TensorExtractOp, | ||
| ) | ||
| from xdsl.dialects.builtin import ModuleOp | ||
| from xdsl.ir import Attribute | ||
| from xdsl.utils.hints import isa | ||
| from xdsl.context import Context | ||
| from xdsl.pattern_rewriter import ( | ||
| GreedyRewritePatternApplier, | ||
| PatternRewriteWalker, | ||
| PatternRewriter, | ||
| RewritePattern, | ||
| op_type_rewrite_pattern, | ||
| ) | ||
| from xdsl.passes import ModulePass | ||
|
|
||
|
|
||
| def lower_tensor_type(typ: Attribute) -> Attribute: | ||
| if isa(typ, SMTTensorType): | ||
| result = typ.element_type | ||
| index_type = IndexType | ||
| for _ in typ.shape: | ||
| result = smt_array.ArrayType(index_type, result) | ||
| return result | ||
| return typ | ||
|
|
||
|
|
||
| class DeclareConstOpPattern(RewritePattern): | ||
| @op_type_rewrite_pattern | ||
| def match_and_rewrite(self, op: DeclareConstOp, rewriter: PatternRewriter): | ||
| if isa(op.res.type, SMTTensorType): | ||
| new_constant_op = DeclareConstOp(lower_tensor_type(op.res.type)) | ||
| rewriter.replace_matched_op(new_constant_op) | ||
|
|
||
|
|
||
| class TensorExtractOpPattern(RewritePattern): | ||
| @op_type_rewrite_pattern | ||
| def match_and_rewrite(self, op: TensorExtractOp, rewriter: PatternRewriter): | ||
| source = op.tensor | ||
| assert isinstance(source.type, smt_array.ArrayType) | ||
| select_ops: list[smt_array.SelectOp] = [] | ||
| for idx in op.indices: | ||
| select_ops.append(smt_array.SelectOp(source, idx)) | ||
| source = select_ops[-1].res | ||
| rewriter.replace_matched_op(select_ops) | ||
|
|
||
|
|
||
| class LowerSMTTensor(ModulePass): | ||
| name = "lower-smt-tensor" | ||
|
|
||
| def apply(self, ctx: Context, op: ModuleOp): | ||
| walker = PatternRewriteWalker( | ||
| GreedyRewritePatternApplier( | ||
| [DeclareConstOpPattern(), TensorExtractOpPattern()] | ||
| ) | ||
| ) | ||
| walker.rewrite_module(op) | ||
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,50 @@ | ||
| from xdsl.transforms.common_subexpression_elimination import ( | ||
| CommonSubexpressionElimination, | ||
| ) | ||
|
|
||
| from xdsl.ir import SSAValue | ||
| from xdsl_smt.dialects.smt_tensor_dialect import ( | ||
| TensorTransposeOp, | ||
| TensorExtractOp, | ||
| ) | ||
| from xdsl.dialects.builtin import ModuleOp | ||
| from xdsl.context import Context | ||
| from xdsl.pattern_rewriter import ( | ||
| GreedyRewritePatternApplier, | ||
| PatternRewriteWalker, | ||
| PatternRewriter, | ||
| RewritePattern, | ||
| op_type_rewrite_pattern, | ||
| ) | ||
| from xdsl.passes import ModulePass | ||
|
|
||
|
|
||
| class RewriteTransposeOpPattern(RewritePattern): | ||
| @op_type_rewrite_pattern | ||
| def match_and_rewrite(self, op: TensorTransposeOp, rewriter: PatternRewriter): | ||
| for use in op.result.uses: | ||
| extract_op = use.operation | ||
| if isinstance(extract_op, TensorExtractOp): | ||
| permutations = op.get_permutation() | ||
| new_indices: list[SSAValue] = [] | ||
| for i in permutations: | ||
| new_indices.append(extract_op.indices[i]) | ||
| new_extract_op = TensorExtractOp(op.operand, new_indices) | ||
| rewriter.replace_op(extract_op, new_extract_op) | ||
| if op.result.uses.get_length() == 0: | ||
| rewriter.erase_matched_op() | ||
|
|
||
|
|
||
| class RewriteSMTTensor(ModulePass): | ||
| """ | ||
| Rewrite patterns like `extract(op(arg))` to `extract(arg')` | ||
| """ | ||
|
|
||
| name = "rewrite-smt-tensor" | ||
|
|
||
| def apply(self, ctx: Context, op: ModuleOp): | ||
| walker = PatternRewriteWalker( | ||
| GreedyRewritePatternApplier([RewriteTransposeOpPattern()]) | ||
| ) | ||
| walker.rewrite_module(op) | ||
| CommonSubexpressionElimination().apply(ctx, op) |
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.
Is there a reason this file doesn't use the
SMTLowererand use theOperationSemanticsclass?