Skip to content

Commit 0e46458

Browse files
authored
dialects: (memref) build a dim with an init rather than a static method (#6384)
1 parent db5db33 commit 0e46458

6 files changed

Lines changed: 36 additions & 18 deletions

File tree

tests/dialects/test_memref.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,24 @@ def test_memref_dealloc():
209209
def test_memref_dim():
210210
idx = arith.ConstantOp.from_int_and_width(1, IndexType())
211211
alloc0 = AllocOp.get(i32, 64, [3, 1, 2])
212-
dim_1 = memref.DimOp.from_source_and_index(alloc0, idx)
212+
dim_1 = memref.DimOp(alloc0, idx)
213213

214214
assert dim_1.source is alloc0.memref
215215
assert dim_1.index is idx.result
216216
assert isinstance(dim_1.result.type, IndexType)
217217

218218

219+
def test_memref_dim_from_source_and_index_is_deprecated():
220+
idx = arith.ConstantOp.from_int_and_width(1, IndexType())
221+
alloc0 = AllocOp.get(i32, 64, [3, 1, 2])
222+
223+
with pytest.deprecated_call():
224+
dim_1 = memref.DimOp.from_source_and_index(alloc0, idx) # pyright: ignore[reportDeprecated]
225+
226+
assert dim_1.source is alloc0.memref
227+
assert dim_1.index is idx.result
228+
229+
219230
def test_memref_rank():
220231
alloc0 = AllocOp.get(i32, 64, [3, 1, 2])
221232
dim_1 = memref.RankOp.from_memref(alloc0)
@@ -244,10 +255,10 @@ def matmul(args: tuple[BlockArgument, ...]) -> None:
244255

245256
lit0 = arith.ConstantOp.from_int_and_width(0, builtin.IndexType())
246257
lit1 = arith.ConstantOp.from_int_and_width(1, builtin.IndexType())
247-
dim_a0 = memref.DimOp.from_source_and_index(a, lit0)
248-
dim_a1 = memref.DimOp.from_source_and_index(a, lit1)
249-
dim_b0 = memref.DimOp.from_source_and_index(b, lit0)
250-
dim_b1 = memref.DimOp.from_source_and_index(b, lit1)
258+
dim_a0 = memref.DimOp(a, lit0)
259+
dim_a1 = memref.DimOp(a, lit1)
260+
dim_b0 = memref.DimOp(b, lit0)
261+
dim_b1 = memref.DimOp(b, lit1)
251262
out = memref.AllocaOp.get(
252263
builtin.f64, None, [DYNAMIC_INDEX] * 2, [dim_a0, dim_b1]
253264
)

xdsl/dialects/linalg/transforms/tiling.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,9 @@ def _build_loop_range(
260260
assert isa(source_type, MemRefType | TensorType)
261261
match source_type:
262262
case MemRefType():
263-
dim_op = memref.DimOp.from_source_and_index(source, position_op)
263+
dim_op = memref.DimOp(source, position_op)
264264
case TensorType():
265-
dim_op = tensor.DimOp.build(
266-
operands=[source, position_op], result_types=[IndexType()]
267-
)
265+
dim_op = tensor.DimOp(source, position_op)
268266
case _:
269267
assert_never(source_type)
270268

xdsl/dialects/memref.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from __future__ import annotations
22

33
import abc
4-
from collections.abc import Iterable, Sequence
4+
from collections.abc import Iterable, Mapping, Sequence
55
from typing import ClassVar, cast
66

7-
from typing_extensions import Self
7+
from typing_extensions import Self, deprecated
88

99
from xdsl.dialects.builtin import (
1010
DYNAMIC_INDEX,
@@ -489,11 +489,22 @@ class DimOp(IRDLOperation):
489489

490490
assembly_format = "$source `,` $index attr-dict `:` type($source)"
491491

492+
def __init__(
493+
self,
494+
source: SSAValue | Operation,
495+
index: SSAValue | Operation,
496+
attributes: Mapping[str, Attribute] | None = None,
497+
):
498+
super().__init__(
499+
operands=(source, index), result_types=(IndexType(),), attributes=attributes
500+
)
501+
492502
@staticmethod
503+
@deprecated("Use DimOp(source, index) instead")
493504
def from_source_and_index(
494505
source: SSAValue | Operation, index: SSAValue | Operation
495-
):
496-
return DimOp.build(operands=[source, index], result_types=[IndexType()])
506+
) -> DimOp:
507+
return DimOp(source, index)
497508

498509

499510
@irdl_op_definition

xdsl/transforms/convert_linalg_to_loops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def materialize_loop_bound(
4343
dim_index_op = arith.ConstantOp.from_int_and_width(dim_index, IndexType())
4444
rewriter.insert(dim_index_op, insertion_point)
4545

46-
dim_op = memref.DimOp.from_source_and_index(operand, dim_index_op.result)
46+
dim_op = memref.DimOp(operand, dim_index_op.result)
4747
rewriter.insert(dim_op, insertion_point)
4848
return dim_op.result
4949

xdsl/transforms/convert_memref_to_ptr.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ def get_strides(
105105
arith.ConstantOp.from_int_and_width(i + 1, _index_type)
106106
)
107107
dim_idx.result.name_hint = "dim_idx"
108-
dim_size = builder.insert(
109-
memref.DimOp.from_source_and_index(memref_val, dim_idx.result)
110-
).result
108+
dim_size = builder.insert(memref.DimOp(memref_val, dim_idx.result)).result
111109
prev = strides[i + 1]
112110
match (prev, dim_size):
113111
case (int(p), int(d)):

xdsl/transforms/experimental/Apply1DMPIToStencil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def match_and_rewrite(
4949
0, builtin.IndexType()
5050
)
5151
dim_zero_const = arith.ConstantOp(int_attr, builtin.IndexType())
52-
dim_zero_size_op = memref.DimOp.from_source_and_index(op.field, dim_zero_const)
52+
dim_zero_size_op = memref.DimOp(op.field, dim_zero_const)
5353
dim_zero_i32_op = arith.IndexCastOp(dim_zero_size_op, builtin.i32)
5454
dim_zero_i64_op = arith.IndexCastOp(dim_zero_size_op, builtin.i64)
5555

0 commit comments

Comments
 (0)