-
Notifications
You must be signed in to change notification settings - Fork 72
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: convert type_offsets in ptr
to arith.constant
#3394
Open
mamanain
wants to merge
2
commits into
xdslproject:main
Choose a base branch
from
mamanain:manainen/ptr-dialect-part5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
79 changes: 79 additions & 0 deletions
79
tests/filecheck/mlir-conversion/with-mlir/ptr_loop_folding.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,79 @@ | ||
// RUN: xdsl-opt -p convert-memref-to-ptr,convert-ptr-type-offsets,mlir-opt[scf-for-loop-canonicalization,scf-for-loop-range-folding,scf-for-loop-canonicalization],scf-for-loop-flatten,mlir-opt[scf-for-loop-canonicalization,scf-for-loop-range-folding,scf-for-loop-canonicalization] --split-input-file %s | filecheck %s | ||
|
||
func.func @fill(%m: memref<10xi32>) { | ||
%c0 = arith.constant 0 : index | ||
%end = arith.constant 10 : index | ||
%c1 = arith.constant 1 : index | ||
%val = arith.constant 100 : i32 | ||
scf.for %i = %c0 to %end step %c1 { | ||
memref.store %val, %m[%i] : memref<10xi32> | ||
} | ||
return | ||
} | ||
|
||
// CHECK: func.func @fill(%arg4 : memref<10xi32>) { | ||
// CHECK-NEXT: %0 = arith.constant 0 : index | ||
// CHECK-NEXT: %1 = arith.constant 100 : i32 | ||
// CHECK-NEXT: %2 = arith.constant 40 : index | ||
// CHECK-NEXT: %3 = arith.constant 4 : index | ||
// CHECK-NEXT: scf.for %arg5 = %0 to %2 step %3 { | ||
// CHECK-NEXT: %4 = ptr_xdsl.to_ptr %arg4 : memref<10xi32> -> !ptr_xdsl.ptr | ||
// CHECK-NEXT: %5 = ptr_xdsl.ptradd %4, %arg5 : (!ptr_xdsl.ptr, index) -> !ptr_xdsl.ptr | ||
// CHECK-NEXT: ptr_xdsl.store %1, %5 : i32, !ptr_xdsl.ptr | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: func.return | ||
// CHECK-NEXT: } | ||
|
||
func.func @fill2d(%m: memref<10x10xi32>) { | ||
%c0 = arith.constant 0 : index | ||
%end = arith.constant 10 : index | ||
%c1 = arith.constant 1 : index | ||
%val = arith.constant 100 : i32 | ||
scf.for %i = %c0 to %end step %c1 { | ||
scf.for %j = %c0 to %end step %c1 { | ||
memref.store %val, %m[%i, %j] : memref<10x10xi32> | ||
} | ||
} | ||
return | ||
} | ||
|
||
// CHECK-NEXT: func.func @fill2d(%arg2 : memref<10x10xi32>) { | ||
// CHECK-NEXT: %0 = arith.constant 0 : index | ||
// CHECK-NEXT: %1 = arith.constant 100 : i32 | ||
// CHECK-NEXT: %2 = arith.constant 400 : index | ||
// CHECK-NEXT: %3 = arith.constant 4 : index | ||
// CHECK-NEXT: scf.for %arg3 = %0 to %2 step %3 { | ||
// CHECK-NEXT: %4 = ptr_xdsl.to_ptr %arg2 : memref<10x10xi32> -> !ptr_xdsl.ptr | ||
// CHECK-NEXT: %5 = ptr_xdsl.ptradd %4, %arg3 : (!ptr_xdsl.ptr, index) -> !ptr_xdsl.ptr | ||
// CHECK-NEXT: ptr_xdsl.store %1, %5 : i32, !ptr_xdsl.ptr | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: func.return | ||
// CHECK-NEXT: } | ||
|
||
func.func @fill3d(%m: memref<10x10x10xi32>) { | ||
%c0 = arith.constant 0 : index | ||
%end = arith.constant 10 : index | ||
%c1 = arith.constant 1 : index | ||
%val = arith.constant 100 : i32 | ||
scf.for %i = %c0 to %end step %c1 { | ||
scf.for %j = %c0 to %end step %c1 { | ||
scf.for %k = %c0 to %end step %c1 { | ||
memref.store %val, %m[%i, %j, %k] : memref<10x10x10xi32> | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
// CHECK-NEXT: func.func @fill3d(%arg0 : memref<10x10x10xi32>) { | ||
// CHECK-NEXT: %0 = arith.constant 0 : index | ||
// CHECK-NEXT: %1 = arith.constant 100 : i32 | ||
// CHECK-NEXT: %2 = arith.constant 4000 : index | ||
// CHECK-NEXT: %3 = arith.constant 4 : index | ||
// CHECK-NEXT: scf.for %arg1 = %0 to %2 step %3 { | ||
// CHECK-NEXT: %4 = ptr_xdsl.to_ptr %arg0 : memref<10x10x10xi32> -> !ptr_xdsl.ptr | ||
// CHECK-NEXT: %5 = ptr_xdsl.ptradd %4, %arg1 : (!ptr_xdsl.ptr, index) -> !ptr_xdsl.ptr | ||
// CHECK-NEXT: ptr_xdsl.store %1, %5 : i32, !ptr_xdsl.ptr | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: func.return | ||
// 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,12 @@ | ||
// RUN: xdsl-opt -p convert-ptr-type-offsets --split-input-file --verify-diagnostics %s | filecheck %s | ||
|
||
%a1 = ptr_xdsl.type_offset i32 : index | ||
// CHECK: %a1 = arith.constant 4 : index | ||
|
||
%a2 = ptr_xdsl.type_offset f128 : index | ||
// CHECK-NEXT: %a2 = arith.constant 16 : index | ||
|
||
// ----- | ||
|
||
%a3 = ptr_xdsl.type_offset tensor<4xi32> : index | ||
// CHECK: Type offset is currently only supported for fixed size types |
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,38 @@ | ||
from dataclasses import dataclass | ||
from typing import cast | ||
|
||
from xdsl.context import MLContext | ||
from xdsl.dialects import arith, ptr | ||
from xdsl.dialects.builtin import FixedBitwidthType, IndexType, ModuleOp | ||
from xdsl.passes import ModulePass | ||
from xdsl.pattern_rewriter import ( | ||
GreedyRewritePatternApplier, | ||
PatternRewriter, | ||
PatternRewriteWalker, | ||
RewritePattern, | ||
op_type_rewrite_pattern, | ||
) | ||
from xdsl.utils.exceptions import DiagnosticException | ||
|
||
|
||
@dataclass | ||
class ConvertTypeOffsetOp(RewritePattern): | ||
@op_type_rewrite_pattern | ||
def match_and_rewrite(self, op: ptr.TypeOffsetOp, rewriter: PatternRewriter, /): | ||
if not issubclass(type(op.elem_type), FixedBitwidthType): | ||
raise DiagnosticException( | ||
"Type offset is currently only supported for fixed size types" | ||
) | ||
elem_type = cast(FixedBitwidthType, op.elem_type) | ||
rewriter.replace_matched_op( | ||
arith.Constant.from_int_and_width(elem_type.size, IndexType()) | ||
) | ||
|
||
|
||
class ConvertPtrTypeOffsetsPass(ModulePass): | ||
name = "convert-ptr-type-offsets" | ||
|
||
def apply(self, ctx: MLContext, op: ModuleOp) -> None: | ||
PatternRewriteWalker( | ||
GreedyRewritePatternApplier([ConvertTypeOffsetOp()]), | ||
).rewrite_module(op) |
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.
How much are we missing in xDSL from the mlir-opt pipeline here? If it's not a lot, I'd much rather have this logic in xDSL.
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 believe both
scf-for-loop-canonicalization
andscf-for-loop-range-folding
are missing. Do you think it's worth porting them?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.
Yep definitely, I'd love to keep the Snitch compilation flow entirely working without the need of MLIR, for environments like WASM, and for when we start the work of schedule exploration, and whatever hackery we need to do to make it fast, it's much easier to play with all this in one environment.
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.
scf-for-loop-range-folding is already in, and was less buggy than the MLIR one until recently, where I upstreamed a bug fix to MLIR after noticing the difference with xDSL :)
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.
How about we bench it first? I'm just curious to see if it makes any improvements. Plus, if there is no speed increase, maybe the port won't be worth it.
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.
Oh, right, there is. But I think it's only for the riscv loops. Do we want a general scf version?
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 can guarantee speed improvements, and register pressure improvements in the final assembly, if that's what you mean
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.
yep, I think it would be worth having an scf version for this
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.
ok, I'll port them to xdsl
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.
Do we now have everything we need?