-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
348 additions
and
17 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
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,31 @@ | ||
from xdsl.dialects.bufferization import AllocTensorOp, ToTensorOp | ||
from xdsl.dialects.builtin import MemRefType, TensorType, UnitAttr, f64 | ||
from xdsl.dialects.test import TestOp | ||
|
||
|
||
def test_to_tensor(): | ||
memref_t = MemRefType(f64, [10, 20, 30]) | ||
tensor_t = TensorType(f64, [10, 20, 30]) | ||
memref_v = TestOp(result_types=[memref_t]).res[0] | ||
|
||
to_tensor = ToTensorOp(memref_v) | ||
assert to_tensor.memref == memref_v | ||
assert to_tensor.restrict is None | ||
assert to_tensor.writable is None | ||
assert to_tensor.tensor.type == tensor_t | ||
|
||
to_tensor = ToTensorOp(memref_v, writable=True, restrict=True) | ||
assert to_tensor.memref == memref_v | ||
assert to_tensor.restrict == UnitAttr() | ||
assert to_tensor.writable == UnitAttr() | ||
assert to_tensor.tensor.type == tensor_t | ||
|
||
|
||
def test_alloc_tensor_static(): | ||
t = TensorType(f64, [10, 20, 30]) | ||
alloc_tensor = AllocTensorOp(t) | ||
|
||
assert alloc_tensor.tensor.type == t | ||
assert alloc_tensor.dynamic_sizes == () | ||
assert alloc_tensor.copy is None | ||
assert alloc_tensor.size_hint is None |
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,67 @@ | ||
from xdsl.dialects.builtin import DenseArrayBase, TensorType, f64, i64 | ||
from xdsl.dialects.tensor import ExtractSliceOp, InsertSliceOp | ||
from xdsl.dialects.test import TestOp | ||
|
||
|
||
def test_extract_slice_static(): | ||
input_t = TensorType(f64, [10, 20, 30]) | ||
input_v = TestOp(result_types=[input_t]).res[0] | ||
|
||
extract_slice = ExtractSliceOp.from_static_parameters(input_v, [1, 2, 3], [4, 5, 6]) | ||
|
||
assert extract_slice.source is input_v | ||
assert extract_slice.static_offsets == DenseArrayBase.from_list(i64, [1, 2, 3]) | ||
assert extract_slice.static_sizes == DenseArrayBase.from_list(i64, [4, 5, 6]) | ||
assert extract_slice.static_strides == DenseArrayBase.from_list(i64, [1, 1, 1]) | ||
assert extract_slice.offsets == () | ||
assert extract_slice.sizes == () | ||
assert extract_slice.strides == () | ||
assert extract_slice.result.type == TensorType(f64, [4, 5, 6]) | ||
|
||
extract_slice = ExtractSliceOp.from_static_parameters( | ||
input_v, [1, 2, 3], [4, 5, 6], [8, 9, 10] | ||
) | ||
|
||
assert extract_slice.source is input_v | ||
assert extract_slice.static_offsets == DenseArrayBase.from_list(i64, [1, 2, 3]) | ||
assert extract_slice.static_sizes == DenseArrayBase.from_list(i64, [4, 5, 6]) | ||
assert extract_slice.static_strides == DenseArrayBase.from_list(i64, [8, 9, 10]) | ||
assert extract_slice.offsets == () | ||
assert extract_slice.sizes == () | ||
assert extract_slice.strides == () | ||
assert extract_slice.result.type == TensorType(f64, [4, 5, 6]) | ||
|
||
|
||
def test_insert_slice_static(): | ||
source_t = TensorType(f64, [10, 20]) | ||
source_v = TestOp(result_types=[source_t]).res[0] | ||
dest_t = TensorType(f64, [10, 20, 30]) | ||
dest_v = TestOp(result_types=[dest_t]).res[0] | ||
|
||
insert_slice = InsertSliceOp.from_static_parameters( | ||
source_v, dest_v, [1, 2], [4, 5] | ||
) | ||
|
||
assert insert_slice.source is source_v | ||
assert insert_slice.dest is dest_v | ||
assert insert_slice.static_offsets == DenseArrayBase.from_list(i64, [1, 2]) | ||
assert insert_slice.static_sizes == DenseArrayBase.from_list(i64, [4, 5]) | ||
assert insert_slice.static_strides == DenseArrayBase.from_list(i64, [1, 1]) | ||
assert insert_slice.offsets == () | ||
assert insert_slice.sizes == () | ||
assert insert_slice.strides == () | ||
assert insert_slice.result.type == dest_t | ||
|
||
insert_slice = InsertSliceOp.from_static_parameters( | ||
source_v, dest_v, [1, 2], [4, 5], [8, 9] | ||
) | ||
|
||
assert insert_slice.source is source_v | ||
assert insert_slice.dest is dest_v | ||
assert insert_slice.static_offsets == DenseArrayBase.from_list(i64, [1, 2]) | ||
assert insert_slice.static_sizes == DenseArrayBase.from_list(i64, [4, 5]) | ||
assert insert_slice.static_strides == DenseArrayBase.from_list(i64, [8, 9]) | ||
assert insert_slice.offsets == () | ||
assert insert_slice.sizes == () | ||
assert insert_slice.strides == () | ||
assert insert_slice.result.type == dest_t |
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
13 changes: 13 additions & 0 deletions
13
tests/filecheck/mlir-conversion/with-mlir/dialects/bufferization/ops.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,13 @@ | ||
// RUN: xdsl-opt %s | mlir-opt --mlir-print-op-generic | xdsl-opt | filecheck %s | ||
|
||
module{ | ||
%t = "bufferization.alloc_tensor"() <{"operandSegmentSizes" = array<i32: 0, 0, 0>}> : () -> tensor<10x20x30xf64> | ||
%m = "test.op"() : () -> memref<30x20x10xf32> | ||
%m_t = "bufferization.to_tensor"(%m) : (memref<30x20x10xf32>) -> tensor<30x20x10xf32> | ||
} | ||
|
||
// CHECK: builtin.module { | ||
// CHECK-NEXT: %0 = "bufferization.alloc_tensor"() <{"operandSegmentSizes" = array<i32: 0, 0, 0>}> : () -> tensor<10x20x30xf64> | ||
// CHECK-NEXT: %1 = "test.op"() : () -> memref<30x20x10xf32> | ||
// CHECK-NEXT: %2 = "bufferization.to_tensor"(%1) : (memref<30x20x10xf32>) -> tensor<30x20x10xf32> | ||
// 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from typing import Any | ||
|
||
from xdsl.dialects.builtin import ( | ||
ContainerType, | ||
IndexType, | ||
MemRefType, | ||
ShapedType, | ||
TensorType, | ||
UnitAttr, | ||
UnrankedMemrefType, | ||
UnrankedTensorType, | ||
) | ||
from xdsl.ir import Attribute, Dialect, Operation, SSAValue | ||
from xdsl.irdl import ( | ||
AnyOf, | ||
AttrSizedOperandSegments, | ||
IRDLOperation, | ||
irdl_op_definition, | ||
operand_def, | ||
opt_operand_def, | ||
opt_prop_def, | ||
result_def, | ||
var_operand_def, | ||
) | ||
|
||
|
||
@irdl_op_definition | ||
class AllocTensorOp(IRDLOperation): | ||
name = "bufferization.alloc_tensor" | ||
|
||
dynamic_sizes = var_operand_def(IndexType()) | ||
copy = opt_operand_def(AnyOf((TensorType, UnrankedTensorType))) | ||
size_hint = opt_operand_def(IndexType()) | ||
|
||
tensor = result_def(AnyOf((TensorType, UnrankedTensorType))) | ||
|
||
irdl_options = [AttrSizedOperandSegments(as_property=True)] | ||
|
||
def __init__( | ||
self, | ||
result_type: Attribute, | ||
dynamic_sizes: list[Operation | SSAValue] | None = None, | ||
copy: SSAValue | Operation | None = None, | ||
size_hint: SSAValue | Operation | None = None, | ||
): | ||
super().__init__( | ||
operands=(dynamic_sizes, copy, size_hint), | ||
result_types=(result_type,), | ||
) | ||
|
||
|
||
@irdl_op_definition | ||
class ToTensorOp(IRDLOperation): | ||
name = "bufferization.to_tensor" | ||
|
||
memref = operand_def(AnyOf((MemRefType, UnrankedMemrefType))) | ||
tensor = result_def(AnyOf((TensorType, UnrankedTensorType))) | ||
writable = opt_prop_def(UnitAttr) | ||
restrict = opt_prop_def(UnitAttr) | ||
|
||
def __init__( | ||
self, | ||
memref: SSAValue | Operation, | ||
restrict: bool = False, | ||
writable: bool = False, | ||
): | ||
memref_v = SSAValue.get(memref) | ||
memref_t = memref_v.type | ||
if not isinstance(memref_t, ContainerType): | ||
raise ValueError(f"Expected ContainerType, got {memref_t}") | ||
if not isinstance(memref_t, ShapedType): | ||
raise ValueError(f"Expected ShapedType, got {memref_t}") | ||
properties = dict[str, Attribute]() | ||
if restrict: | ||
properties["restrict"] = UnitAttr() | ||
if writable: | ||
properties["writable"] = UnitAttr() | ||
super().__init__( | ||
operands=(memref,), | ||
result_types=( | ||
TensorType[Any](memref_t.get_element_type(), memref_t.get_shape()), | ||
), | ||
properties=properties, | ||
) | ||
|
||
|
||
Bufferization = Dialect( | ||
"bufferization", | ||
[ | ||
AllocTensorOp, | ||
ToTensorOp, | ||
], | ||
[], | ||
) |
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.