Skip to content

Commit 2dac9bb

Browse files
author
Ivan Garcia
committed
Add examples for reinterpret_cast and subview operators to show their behavior in relation to their input memref underlying memory and view.
1 parent 85614e1 commit 2dac9bb

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ def MemRef_ReinterpretCastOp
13311331
let description = [{
13321332
Modify offset, sizes and strides of an unranked/ranked memref.
13331333

1334-
Example:
1334+
Example 1:
13351335
```mlir
13361336
memref.reinterpret_cast %ranked to
13371337
offset: [0],
@@ -1363,6 +1363,58 @@ def MemRef_ReinterpretCastOp
13631363
%dst.sizes = %sizes
13641364
%dst.strides = %strides
13651365
```
1366+
1367+
Example 2:
1368+
1369+
Consecutive `reinterpret_cast` operations on memref's with static dimensions.
1370+
1371+
We distinguish between *underlying memory* — the sequence of elements as
1372+
they appear in the contiguous memory of the memref — and the *view*, which refers to
1373+
the underlying memory interpreted according to specified offsets, sizes, and strides.
1374+
1375+
```mlir
1376+
%result1 = memref.reinterpret_cast %arg0 to offset: [9], sizes: [4, 4], strides: [16, 2] : memref<8x8xf32, strided<[8, 1], offset: 0>> to memref<4x4xf32, strided<[16, 2], offset: 9>>
1377+
1378+
%result2 = memref.reinterpret_cast %result1 to offset: [0], sizes: [2, 2], strides: [4, 2] : memref<4x4xf32, strided<[16, 2], offset: 9>> to memref<2x2xf32, strided<[4, 2], offset: 0>>
1379+
```
1380+
1381+
The input memref `%arg0` has the following view. The underlying memory consists
1382+
of a linear sequence of integers from 1 to 64:
1383+
1384+
```mlir
1385+
[[1, 2, 3, 4, 5, 6, 7, 8],
1386+
[9, 10, 11, 12, 13, 14, 15, 16],
1387+
[17, 18, 19, 20, 21, 22, 23, 24],
1388+
[25, 26, 27, 28, 29, 30, 31, 32],
1389+
[33, 34, 35, 36, 37, 38, 39, 40],
1390+
[41, 42, 43, 44, 45, 46, 47, 48],
1391+
[49, 50, 51, 52, 53, 54, 55, 56],
1392+
[57, 58, 59, 60, 61, 62, 63, 64]]
1393+
```
1394+
1395+
Following the first `reinterpret_cast`, the view of `%result1` is:
1396+
1397+
```mlir
1398+
[[10, 12, 14, 16],
1399+
[26, 28, 30, 32],
1400+
[42, 44, 46, 48],
1401+
[58, 60, 62, 64]]
1402+
```
1403+
1404+
Note: The offset and strides are relative to the underlying memory of `%arg0`.
1405+
1406+
The second `reinterpret_cast` results in the following view for `%result2`:
1407+
1408+
```mlir
1409+
[[1, 3],
1410+
[5, 7]]
1411+
```
1412+
1413+
It is important to observe that the offset and stride are relative to the base underlying
1414+
memory of the memref, starting at 1, not at 10 as seen in the output of `%result1`.
1415+
This behavior contrasts with the `subview` operator, where values are relative to the view of
1416+
the memref (refer to `subview` examples). Consequently, the second `reinterpret_cast` behaves
1417+
as if `%arg0` were passed directly as its argument.
13661418
}];
13671419

13681420
let arguments = (ins Arg<AnyRankedOrUnrankedMemRef, "", []>:$source,
@@ -1942,7 +1994,55 @@ def SubViewOp : MemRef_OpWithOffsetSizesAndStrides<"subview", [
19421994
%1 = memref.subview %0[0, 0, 0] [8, 16, 4] [1, 1, 1]
19431995
: memref<8x16x4xf32> to memref<8x16x4xf32>
19441996
```
1997+
Example 6:
1998+
1999+
Consecutive `subview` operations on memref's with static dimensions.
2000+
2001+
We distinguish between *underlying memory* — the sequence of elements as
2002+
they appear in the contiguous memory of the memref — and the *view*, which refers to
2003+
the underlying memory interpreted according to specified offsets, sizes, and strides.
2004+
2005+
```mlir
2006+
%result1 = memref.subview %arg0[1, 1][4, 4][2, 2] : memref<8x8xf32, strided<[8, 1], offset: 0>> to memref<4x4xf32, strided<[16, 2], offset: 9>>
2007+
2008+
%result2 = memref.subview %result1[1, 1][2, 2][2, 2] : memref<4x4xf32, strided<[16, 2], offset: 9>> to memref<2x2xf32, strided<[32, 4], offset: 27>>
2009+
```
2010+
2011+
The input memref `%arg0` has the following view. The underlying memory for this input
2012+
memref is a linear sequence of integers from 1 to 64:
2013+
2014+
```mlir
2015+
[[1, 2, 3, 4, 5, 6, 7, 8],
2016+
[9, 10, 11, 12, 13, 14, 15, 16],
2017+
[17, 18, 19, 20, 21, 22, 23, 24],
2018+
[25, 26, 27, 28, 29, 30, 31, 32],
2019+
[33, 34, 35, 36, 37, 38, 39, 40],
2020+
[41, 42, 43, 44, 45, 46, 47, 48],
2021+
[49, 50, 51, 52, 53, 54, 55, 56],
2022+
[57, 58, 59, 60, 61, 62, 63, 64]]
2023+
```
2024+
2025+
Following the first `subview`, the view of `%result1` is:
2026+
2027+
```mlir
2028+
[[10, 12, 14, 16],
2029+
[26, 28, 30, 32],
2030+
[42, 44, 46, 48],
2031+
[58, 60, 62, 64]]
2032+
```
2033+
2034+
Note: The offset and strides are relative to the memref view of `%arg0` (compare to the
2035+
corresponding `reinterpret_cast` example).
2036+
2037+
The second `subview` results in the following view for `%result2`:
2038+
2039+
```mlir
2040+
[[28, 32],
2041+
[60, 64]]
2042+
```
19452043

2044+
Unlike the `reinterpret_cast`, the values are relative to the view of the input memref
2045+
(`%result1` in this case) and not its underlying memory.
19462046
}];
19472047

19482048
let arguments = (ins AnyMemRef:$source,

0 commit comments

Comments
 (0)