@@ -1331,7 +1331,79 @@ def MemRef_ReinterpretCastOp
1331
1331
let description = [{
1332
1332
Modify offset, sizes and strides of an unranked/ranked memref.
1333
1333
1334
- Example:
1334
+ Example 1:
1335
+
1336
+ Consecutive `reinterpret_cast` operations on memref's with static
1337
+ dimensions.
1338
+
1339
+ We distinguish between *underlying memory* — the sequence of elements as
1340
+ they appear in the contiguous memory of the memref — and the
1341
+ *strided memref*, which refers to the underlying memory interpreted
1342
+ according to specified offsets, sizes, and strides.
1343
+
1344
+ ```mlir
1345
+ %result1 = memref.reinterpret_cast %arg0 to
1346
+ offset: [9],
1347
+ sizes: [4, 4],
1348
+ strides: [16, 2]
1349
+ : memref<8x8xf32, strided<[8, 1], offset: 0>> to
1350
+ memref<4x4xf32, strided<[16, 2], offset: 9>>
1351
+
1352
+ %result2 = memref.reinterpret_cast %result1 to
1353
+ offset: [0],
1354
+ sizes: [2, 2],
1355
+ strides: [4, 2]
1356
+ : memref<4x4xf32, strided<[16, 2], offset: 9>> to
1357
+ memref<2x2xf32, strided<[4, 2], offset: 0>>
1358
+ ```
1359
+
1360
+ The underlying memory of `%arg0` consists of a linear sequence of integers
1361
+ from 1 to 64. Its memref has the following 8x8 elements:
1362
+
1363
+ ```mlir
1364
+ [[1, 2, 3, 4, 5, 6, 7, 8],
1365
+ [9, 10, 11, 12, 13, 14, 15, 16],
1366
+ [17, 18, 19, 20, 21, 22, 23, 24],
1367
+ [25, 26, 27, 28, 29, 30, 31, 32],
1368
+ [33, 34, 35, 36, 37, 38, 39, 40],
1369
+ [41, 42, 43, 44, 45, 46, 47, 48],
1370
+ [49, 50, 51, 52, 53, 54, 55, 56],
1371
+ [57, 58, 59, 60, 61, 62, 63, 64]]
1372
+ ```
1373
+
1374
+ Following the first `reinterpret_cast`, the strided memref elements
1375
+ of `%result1` are:
1376
+
1377
+ ```mlir
1378
+ [[10, 12, 14, 16],
1379
+ [26, 28, 30, 32],
1380
+ [42, 44, 46, 48],
1381
+ [58, 60, 62, 64]]
1382
+ ```
1383
+
1384
+ Note: The offset and strides are relative to the underlying memory of
1385
+ `%arg0`.
1386
+
1387
+ The second `reinterpret_cast` results in the following strided memref
1388
+ for `%result2`:
1389
+
1390
+ ```mlir
1391
+ [[1, 3],
1392
+ [5, 7]]
1393
+ ```
1394
+
1395
+ Notice that it does not matter if you use %result1 or %arg0 as a source
1396
+ for the second `reinterpret_cast` operation. Only the underlying memory
1397
+ pointers will be reused.
1398
+
1399
+ The offset and stride are relative to the base underlying memory of the
1400
+ memref, starting at 1, not at 10 as seen in the output of `%result1`.
1401
+ This behavior contrasts with the `subview` operator, where values are
1402
+ relative to the strided memref (refer to `subview` examples).
1403
+ Consequently, the second `reinterpret_cast` behaves as if `%arg0` were
1404
+ passed directly as its argument.
1405
+
1406
+ Example 2:
1335
1407
```mlir
1336
1408
memref.reinterpret_cast %ranked to
1337
1409
offset: [0],
@@ -1898,6 +1970,64 @@ def SubViewOp : MemRef_OpWithOffsetSizesAndStrides<"subview", [
1898
1970
1899
1971
Example 1:
1900
1972
1973
+ Consecutive `subview` operations on memref's with static dimensions.
1974
+
1975
+ We distinguish between *underlying memory* — the sequence of elements as
1976
+ they appear in the contiguous memory of the memref — and the
1977
+ *strided memref*, which refers to the underlying memory interpreted
1978
+ according to specified offsets, sizes, and strides.
1979
+
1980
+ ```mlir
1981
+ %result1 = memref.subview %arg0[1, 1][4, 4][2, 2]
1982
+ : memref<8x8xf32, strided<[8, 1], offset: 0>> to
1983
+ memref<4x4xf32, strided<[16, 2], offset: 9>>
1984
+
1985
+ %result2 = memref.subview %result1[1, 1][2, 2][2, 2]
1986
+ : memref<4x4xf32, strided<[16, 2], offset: 9>> to
1987
+ memref<2x2xf32, strided<[32, 4], offset: 27>>
1988
+ ```
1989
+
1990
+ The underlying memory of `%arg0` consists of a linear sequence of integers
1991
+ from 1 to 64. Its memref has the following 8x8 elements:
1992
+
1993
+ ```mlir
1994
+ [[1, 2, 3, 4, 5, 6, 7, 8],
1995
+ [9, 10, 11, 12, 13, 14, 15, 16],
1996
+ [17, 18, 19, 20, 21, 22, 23, 24],
1997
+ [25, 26, 27, 28, 29, 30, 31, 32],
1998
+ [33, 34, 35, 36, 37, 38, 39, 40],
1999
+ [41, 42, 43, 44, 45, 46, 47, 48],
2000
+ [49, 50, 51, 52, 53, 54, 55, 56],
2001
+ [57, 58, 59, 60, 61, 62, 63, 64]]
2002
+ ```
2003
+
2004
+ Following the first `subview`, the strided memref elements of `%result1`
2005
+ are:
2006
+
2007
+ ```mlir
2008
+ [[10, 12, 14, 16],
2009
+ [26, 28, 30, 32],
2010
+ [42, 44, 46, 48],
2011
+ [58, 60, 62, 64]]
2012
+ ```
2013
+
2014
+ Note: The offset and strides are relative to the strided memref of `%arg0`
2015
+ (compare to the corresponding `reinterpret_cast` example).
2016
+
2017
+ The second `subview` results in the following strided memref for
2018
+ `%result2`:
2019
+
2020
+ ```mlir
2021
+ [[28, 32],
2022
+ [60, 64]]
2023
+ ```
2024
+
2025
+ Unlike the `reinterpret_cast`, the values are relative to the strided
2026
+ memref of the input (`%result1` in this case) and not its
2027
+ underlying memory.
2028
+
2029
+ Example 2:
2030
+
1901
2031
```mlir
1902
2032
// Subview of static memref with strided layout at static offsets, sizes
1903
2033
// and strides.
@@ -1906,7 +2036,7 @@ def SubViewOp : MemRef_OpWithOffsetSizesAndStrides<"subview", [
1906
2036
memref<8x2xf32, strided<[21, 18], offset: 137>>
1907
2037
```
1908
2038
1909
- Example 2 :
2039
+ Example 3 :
1910
2040
1911
2041
```mlir
1912
2042
// Subview of static memref with identity layout at dynamic offsets, sizes
@@ -1915,7 +2045,7 @@ def SubViewOp : MemRef_OpWithOffsetSizesAndStrides<"subview", [
1915
2045
: memref<64x4xf32> to memref<?x?xf32, strided<[?, ?], offset: ?>>
1916
2046
```
1917
2047
1918
- Example 3 :
2048
+ Example 4 :
1919
2049
1920
2050
```mlir
1921
2051
// Subview of dynamic memref with strided layout at dynamic offsets and
@@ -1925,7 +2055,7 @@ def SubViewOp : MemRef_OpWithOffsetSizesAndStrides<"subview", [
1925
2055
memref<4x4xf32, strided<[?, ?], offset: ?>>
1926
2056
```
1927
2057
1928
- Example 4 :
2058
+ Example 5 :
1929
2059
1930
2060
```mlir
1931
2061
// Rank-reducing subviews.
@@ -1935,14 +2065,13 @@ def SubViewOp : MemRef_OpWithOffsetSizesAndStrides<"subview", [
1935
2065
: memref<8x16x4xf32> to memref<6x3xf32, strided<[4, 1], offset: 210>>
1936
2066
```
1937
2067
1938
- Example 5 :
2068
+ Example 6 :
1939
2069
1940
2070
```mlir
1941
2071
// Identity subview. The subview is the full source memref.
1942
2072
%1 = memref.subview %0[0, 0, 0] [8, 16, 4] [1, 1, 1]
1943
2073
: memref<8x16x4xf32> to memref<8x16x4xf32>
1944
2074
```
1945
-
1946
2075
}];
1947
2076
1948
2077
let arguments = (ins AnyMemRef:$source,
0 commit comments