Commit 70a626c
committed
[mlir] Add a contiguous<perm, offset> layout, use as identity layout
This PR introduces a new ContiguousLayoutAttr, which holds a
permutation of the dimensions of the memref and an optional offset,
and replaces the default memref layout (which was previously the N-D
identity map) with contiguous<N>.
In general, the syntax for this attribute is
`contiguous<[I0, I1, .. IN], offset: O>` where `I0` through `IN` are
integers in 0..=N and O is either a static offset or ? for a dynamic
one. If the offset is 0, the offset isn't printed, while if the
permutation is `[0, 1, ... N]`, we print it as N+1. That is, the 2-D
identity/row-major layout is `contiguous<2>` and not
`(d0, d1) -> (d0, d1)` like it used to be.
\# Motivation
In summary, the contiguous<> layout both fills in the "layout
hierarchy" (all contiguous layouts are strided, and all strided
layouts are affine maps, but you can't go back down) with a primitive
that enables useful optimizations and makes it easier to have
relocatable/mergable allocations in MLIR code.
Consider `memref<?0 x ?1 x ?2 x T>` - a memref with three dynamic
dimensions. This memref has a row-major identity layout.
Suppose I want to make this memref "relocatable" - declare that it has
an unonwn offset so that I can, for example, have a pass that merges
allocations into larger contiguous buffers. With the current layouts
in MLIR, I can either use:
- `strided<[?, ?, 1], offset: ?>`, which loses the fact that this is a
row-major memref. We don't know the relationship of those two `?`s to
each other.
- `(d0, d1, d2)[s0] -> (d0, d1, d2 + s0)`, which isn't a "strided"
layout by existing definitions and encounters the fact that meany
memref operations don't handle non-strided or arbitrary affine
layouts.
Being able to use `contiguous<3, offset: ?>` (or, in its long form,
contiguous<[0, 1, 2], offset: ?>`) resolves this isue. That is now a
strided layout that directly encodes the fact that this is a 3-D
row-major memref with some dynamic offset.
As seen in my changes to some passes like `gpu-decompose-memrefs` or
the vector transfer op flattener, knowing that a layout is contiguous
- if not necessarily row-major, allows us to use operations like
`affine.linearize_index` for index computations, which fold well with
operations like `affine.delinearize_index`, allowing for eliminating
unnecessariy "divide an ID into parts and multiply them together
again" computations that often come up in tiling-based code generation
that the affine map simplifier has difficulty with or generates
inefficiently.
This layout also allows describing permuted layouts, like column-major
layouts, without needing code to handle the general complexity of an
affine map layout. For exmample,
memref.expand_shape %arg [[0, 1], [2]]
: memref<?x?xi32, contiguous<[1, 0]>
into memref<?x?x?xi32, contiguous<[1, 2, 0]>
accurately describes the effects of expand_shape'ing a column-major
memref.
\## Why change the default layout?
Since the built-in layout attributes form a hierarchy of
specificy (all contiguous layouts are strided ...), there are multiple
ways to represent the identity row-major layout. The contiguous layout
is the most specific of these, so it makes sense to declare it the
canonical form of the identity layout. That is, `strided<[?, ?, 1]>` is
less specific of a layout for `memref<?x?x?xi32>`. The identity
affine_map also has non-canonical forms and is less spcefici: code
that can handle te identity AffineMapAttr may not know what to do with
other affine maps because of how general they are, but it will be
easier to go from the identity ContiguousLayoutAttr to permuted and/or
offset attributes.
Therefore, making the contiguous layout the default form of
MemRefLayoutAttrInterface makes writing memref-handling code easier
going forward.
\# Concrete impacts of the change
1. `memref<...xT, affine_map<(d0, d1, ..., dN) -> (d0, d1, ... dN)>`
no longer prints as `memref<...xT>`.
2. Similarly, the default memref layout is no longer an AffineMapAttr.
This didn't break any code in-tree, since almost everything had moved
to MemRefLayoutAttrInterface::getAffineMap(), but it's worth calling
out.
3. `memref.subview`, `memref.reinterperet_cast`, and so on do not
alwasy produce a `strided` layout: if code needed to create
`strided<[], offset: O>`, it'll now create `contiguous<0, offset: O>`
and similarly for `strided<[1], offset: O>`, which is a 1-D contiguous
layout. This is facilitated by the new `StridedLayout::getCanonical`
method, which doesn't always return a strided layout
4. Some passes have been updated to use `affine.linearize_index
disjoint` when they were flatting a contiguous (subset of) a memref,
allowing for more efficient code generatino compared to an
`affine.apply` over the strides.
4. `getStridesAndOfffset()` has learned a new trick for affine maps:
any "offset permutation" (that is, a permutation where the last result
can be dX + E for any E) is now considered strided. This means that
you can now `getStridesAndOffset` a
`memref<MxNxf32, affine_map<(i, j) -> (j, i)>`, which would previously
fail.
5. `MemRefType::canonicalizeLayout` has been updated to canonicalize
strided layouts to their `contiguous` equivalent for static-shaped
memrefs.
6. `bufferization.buffer_layout` can be any
`MemRefLayoutAttrInterface`, and any identity maps present in such
attributes are transparently migrated to their contiguous<>
equivalents.
7. Certain reshape folders will now work with any row-major layout,
even if it has an offset.
While this is a breaking change, we expect that it will allow
long-term improvments to how MLIR represents memrefs in common
situations.1 parent 2443fe5 commit 70a626c
File tree
56 files changed
+1560
-483
lines changed- mlir
- include
- mlir-c
- mlir
- Dialect
- MemRef/IR
- Utils
- IR
- lib
- AsmParser
- Bindings/Python
- CAPI/IR
- Dialect
- AMDGPU/IR
- Affine/Utils
- Bufferization
- IR
- Transforms
- GPU/Transforms
- MemRef
- IR
- Transforms
- Tensor/Transforms
- Utils
- Vector/Transforms
- IR
- python/mlir
- _mlir_libs/_mlir
- extras
- test
- CAPI
- Conversion/MemRefToLLVM
- Dialect
- Affine
- Bufferization/Transforms
- GPU
- Linalg
- MemRef
- Tensor
- Transform
- Vector
- IR
- python
- dialects
- ir
- unittests/Dialect/MemRef
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
56 files changed
+1560
-483
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
697 | 697 | | |
698 | 698 | | |
699 | 699 | | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
700 | 707 | | |
701 | 708 | | |
702 | 709 | | |
| |||
711 | 718 | | |
712 | 719 | | |
713 | 720 | | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
714 | 753 | | |
715 | 754 | | |
716 | 755 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
181 | | - | |
182 | | - | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
183 | 185 | | |
184 | 186 | | |
185 | 187 | | |
| |||
197 | 199 | | |
198 | 200 | | |
199 | 201 | | |
200 | | - | |
201 | | - | |
202 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
203 | 205 | | |
204 | 206 | | |
205 | 207 | | |
| |||
265 | 267 | | |
266 | 268 | | |
267 | 269 | | |
268 | | - | |
269 | | - | |
270 | | - | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
271 | 273 | | |
272 | 274 | | |
273 | 275 | | |
| |||
331 | 333 | | |
332 | 334 | | |
333 | 335 | | |
334 | | - | |
335 | | - | |
336 | | - | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
337 | 339 | | |
338 | 340 | | |
339 | 341 | | |
| |||
451 | 453 | | |
452 | 454 | | |
453 | 455 | | |
454 | | - | |
| 456 | + | |
455 | 457 | | |
456 | 458 | | |
457 | 459 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1081 | 1081 | | |
1082 | 1082 | | |
1083 | 1083 | | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
1084 | 1106 | | |
1085 | 1107 | | |
1086 | 1108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
167 | | - | |
| 167 | + | |
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
| |||
494 | 494 | | |
495 | 495 | | |
496 | 496 | | |
497 | | - | |
| 497 | + | |
498 | 498 | | |
499 | 499 | | |
500 | 500 | | |
| |||
1051 | 1051 | | |
1052 | 1052 | | |
1053 | 1053 | | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
1054 | 1062 | | |
1055 | 1063 | | |
1056 | 1064 | | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
1057 | 1144 | | |
1058 | 1145 | | |
1059 | 1146 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
585 | 585 | | |
586 | 586 | | |
587 | 587 | | |
588 | | - | |
589 | | - | |
590 | | - | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
591 | 591 | | |
592 | 592 | | |
593 | 593 | | |
594 | 594 | | |
595 | 595 | | |
596 | 596 | | |
597 | | - | |
598 | | - | |
599 | | - | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
600 | 601 | | |
601 | | - | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
602 | 609 | | |
603 | 610 | | |
604 | 611 | | |
| |||
656 | 663 | | |
657 | 664 | | |
658 | 665 | | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
659 | 697 | | |
660 | 698 | | |
661 | 699 | | |
| |||
815 | 853 | | |
816 | 854 | | |
817 | 855 | | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
818 | 860 | | |
819 | 861 | | |
820 | 862 | | |
| |||
830 | 872 | | |
831 | 873 | | |
832 | 874 | | |
833 | | - | |
834 | | - | |
| 875 | + | |
| 876 | + | |
835 | 877 | | |
836 | 878 | | |
837 | 879 | | |
| |||
0 commit comments