-
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
transformations: add convert-arith-to-riscv-snitch pass #2914
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f9a639c
testing: add ssum testcase to bottom-up tests
superlopuh cb8c277
dialects: (riscv_snitch) add fastmath flags in vector ops
superlopuh 76bb149
transformations: add convert-arith-to-riscv-snitch pass
superlopuh d48500f
Merge branch 'main' into sasha/memref_stream/ssum-lower-vector
superlopuh 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
29 changes: 29 additions & 0 deletions
29
tests/filecheck/backend/riscv/convert_arith_to_riscv_snitch.mlir
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,29 @@ | ||
// RUN: xdsl-opt -p convert-arith-to-riscv-snitch,reconcile-unrealized-casts %s | filecheck %s | ||
|
||
// CHECK: builtin.module | ||
// CHECK-NEXT: %l, %r = "test.op"() : () -> (!riscv.freg, !riscv.freg) | ||
%l, %r = "test.op"() : () -> (!riscv.freg, !riscv.freg) | ||
%l32 = builtin.unrealized_conversion_cast %l : !riscv.freg to vector<2xf32> | ||
%r32 = builtin.unrealized_conversion_cast %r : !riscv.freg to vector<2xf32> | ||
%lhsvf64 = builtin.unrealized_conversion_cast %l : !riscv.freg to vector<1xf64> | ||
%rhsvf64 = builtin.unrealized_conversion_cast %r : !riscv.freg to vector<1xf64> | ||
|
||
// CHECK-NEXT: %addf32 = riscv_snitch.vfadd.s %l, %r : (!riscv.freg, !riscv.freg) -> !riscv.freg | ||
%addf32 = arith.addf %l32, %r32 : vector<2xf32> | ||
|
||
// tests with fastmath flags when set to "fast" | ||
// CHECK-NEXT: %addf32_fm = riscv_snitch.vfadd.s %l, %r fastmath<fast> : (!riscv.freg, !riscv.freg) -> !riscv.freg | ||
%addf32_fm = arith.addf %l32, %r32 fastmath<fast> : vector<2xf32> | ||
|
||
|
||
// CHECK-NEXT: %addf64 = riscv.fadd.d %l, %r : (!riscv.freg, !riscv.freg) -> !riscv.freg | ||
%addf64 = arith.addf %lhsvf64, %rhsvf64 : vector<1xf64> | ||
|
||
// tests with fastmath flags when set to "fast" | ||
// CHECK-NEXT: %addf64_fm = riscv.fadd.d %l, %r fastmath<fast> : (!riscv.freg, !riscv.freg) -> !riscv.freg | ||
%addf64_fm = arith.addf %lhsvf64, %rhsvf64 fastmath<fast> : vector<1xf64> | ||
|
||
// tests with fastmath flags when set to "contract" | ||
// CHECK-NEXT: %addf64_fm_contract = riscv.fadd.d %l, %r fastmath<contract> : (!riscv.freg, !riscv.freg) -> !riscv.freg | ||
%addf64_fm_contract = arith.addf %lhsvf64, %rhsvf64 fastmath<contract> : vector<1xf64> | ||
|
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
50 changes: 50 additions & 0 deletions
50
tests/filecheck/projects/riscv-backend-paper/bottom_up.mlir
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
87 changes: 87 additions & 0 deletions
87
xdsl/backend/riscv/lowering/convert_arith_to_riscv_snitch.py
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,87 @@ | ||
from dataclasses import dataclass | ||
from math import prod | ||
from typing import Any, cast | ||
|
||
from xdsl.context import MLContext | ||
from xdsl.dialects import arith, riscv, riscv_snitch | ||
from xdsl.dialects.builtin import ( | ||
Float32Type, | ||
Float64Type, | ||
ModuleOp, | ||
UnrealizedConversionCastOp, | ||
VectorType, | ||
) | ||
from xdsl.ir import Operation | ||
from xdsl.passes import ModulePass | ||
from xdsl.pattern_rewriter import ( | ||
GreedyRewritePatternApplier, | ||
PatternRewriter, | ||
PatternRewriteWalker, | ||
RewritePattern, | ||
) | ||
|
||
_FLOAT_REGISTER_TYPE = riscv.FloatRegisterType.unallocated() | ||
|
||
|
||
@dataclass | ||
class LowerBinaryFloatVectorOp(RewritePattern): | ||
arith_op_cls: type[arith.FloatingPointLikeBinaryOp] | ||
riscv_d_op_cls: type[riscv.RdRsRsFloatOperationWithFastMath] | ||
riscv_snitch_v_f_op_cls: type[riscv.RdRsRsFloatOperationWithFastMath] | ||
|
||
def match_and_rewrite(self, op: Operation, rewriter: PatternRewriter) -> None: | ||
if not isinstance(op, self.arith_op_cls): | ||
return | ||
|
||
operand_type = op.result.type | ||
if not isinstance(operand_type, VectorType): | ||
return | ||
shape = operand_type.shape | ||
count = prod(dim.data for dim in shape.data) | ||
|
||
operand_type = cast(VectorType[Any], operand_type) | ||
scalar_type = operand_type.element_type | ||
|
||
lhs = UnrealizedConversionCastOp.get((op.lhs,), (_FLOAT_REGISTER_TYPE,)) | ||
rhs = UnrealizedConversionCastOp.get((op.rhs,), (_FLOAT_REGISTER_TYPE,)) | ||
|
||
match scalar_type: | ||
case Float64Type(): | ||
if count != 1: | ||
return | ||
cls = self.riscv_d_op_cls | ||
case Float32Type(): | ||
if count != 2: | ||
return | ||
cls = self.riscv_snitch_v_f_op_cls | ||
case _: | ||
assert False, f"Unexpected float type {op.lhs.type}" | ||
|
||
rv_flags = riscv.FastMathFlagsAttr("none") | ||
if op.fastmath is not None: | ||
rv_flags = riscv.FastMathFlagsAttr(op.fastmath.data) | ||
|
||
new_op = cls(lhs, rhs, rd=_FLOAT_REGISTER_TYPE, fastmath=rv_flags) | ||
cast_op = UnrealizedConversionCastOp.get((new_op.rd,), (op.result.type,)) | ||
|
||
rewriter.replace_matched_op((lhs, rhs, new_op, cast_op)) | ||
|
||
|
||
lower_arith_addf = LowerBinaryFloatVectorOp( | ||
arith.Addf, riscv.FAddDOp, riscv_snitch.VFAddSOp | ||
) | ||
|
||
|
||
class ConvertArithToRiscvSnitchPass(ModulePass): | ||
name = "convert-arith-to-riscv-snitch" | ||
|
||
def apply(self, ctx: MLContext, op: ModuleOp) -> None: | ||
walker = PatternRewriteWalker( | ||
GreedyRewritePatternApplier( | ||
[ | ||
lower_arith_addf, | ||
] | ||
), | ||
apply_recursively=False, | ||
) | ||
walker.rewrite_module(op) |
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
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.
Since 9b43500 landed, does this mean we can use named SSA variables now?
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.
Why do it in this case if the variable names are stable and more readable?
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.
I'll merge for now since you've approved but happy to refactor tests together tomorrow, there's more to come