-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transformations: port MLIR's empty-tensor-to-alloc-tensor pass
- Loading branch information
1 parent
82e7072
commit af5dc1d
Showing
4 changed files
with
62 additions
and
2 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
10 changes: 10 additions & 0 deletions
10
tests/filecheck/transforms/empty-tensor-to-alloc-tensor.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,10 @@ | ||
// RUN: xdsl-opt -p empty-tensor-to-alloc-tensor %s | filecheck %s | ||
|
||
// CHECK: %val = "test.op"() : () -> index | ||
%val = "test.op"() : () -> index | ||
|
||
// CHECK-NEXT: %static = bufferization.alloc_tensor() : tensor<1024xi32> | ||
%static = tensor.empty() : tensor<1024xi32> | ||
|
||
// CHECK-NEXT: %dynamic = bufferization.alloc_tensor(%val) : tensor<1024x?xi32> | ||
%dynamic = tensor.empty(%val) : tensor<1024x?xi32> |
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,36 @@ | ||
from xdsl.context import MLContext | ||
from xdsl.dialects import bufferization, builtin, tensor | ||
from xdsl.passes import ModulePass | ||
from xdsl.pattern_rewriter import ( | ||
PatternRewriter, | ||
PatternRewriteWalker, | ||
RewritePattern, | ||
op_type_rewrite_pattern, | ||
) | ||
|
||
|
||
class EmptyTensorLoweringPattern(RewritePattern): | ||
@op_type_rewrite_pattern | ||
def match_and_rewrite(self, op: tensor.EmptyOp, rewriter: PatternRewriter, /): | ||
rewriter.replace_matched_op( | ||
bufferization.AllocTensorOp( | ||
op.tensor.type, | ||
op.dynamic_sizes, | ||
) | ||
) | ||
|
||
|
||
class EmptyTensorToAllocTensorPass(ModulePass): | ||
""" | ||
tensor.empty ops return a tensor of unspecified contents who's only purpose | ||
is to carry the tensor shape. This pass converts such ops to | ||
bufferization.alloc_tensor ops, which bufferize to buffer allocations. | ||
""" | ||
|
||
name = "empty-tensor-to-alloc-tensor" | ||
|
||
def apply(self, ctx: MLContext, op: builtin.ModuleOp) -> None: | ||
PatternRewriteWalker( | ||
EmptyTensorLoweringPattern(), | ||
apply_recursively=False, | ||
).rewrite_module(op) |