Skip to content

Commit 3dceb6d

Browse files
committed
Allow IndexType inside tensors.
It's common in many dialects to use tensors to themselves hold tensor shapes (for example, the shape is itself the result of some non-trivial calculation). Currently, such dialects have to use `tensor<?xi64>` or worse (like allowing either i32 or i64 tensors to represent shapes). `tensor<?xindex>` is the natural type to represent this, but is currently disallowed. This patch allows it. Differential Revision: https://reviews.llvm.org/D76726
1 parent 47e7bdb commit 3dceb6d

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

mlir/docs/Rationale.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,28 +202,33 @@ and described in
202202
interest
203203
[starts here](https://www.google.com/url?q=https://youtu.be/Ntj8ab-5cvE?t%3D596&sa=D&ust=1529450150971000&usg=AFQjCNFQHEWL7m8q3eO-1DiKw9zqC2v24Q).
204204

205-
### Index type disallowed in vector/tensor/memref types
206-
207-
Index types are not allowed as elements of `vector`, `tensor` or `memref` type.
208-
Index types are intended to be used for platform-specific "size" values and may
209-
appear in subscripts, sizes of aggregate types and affine expressions. They are
210-
also tightly coupled with `affine.apply` and affine.load/store operations;
211-
having `index` type is a necessary precondition of a value to be acceptable by
212-
these operations. While it may be useful to have `memref<?xindex>` to express
213-
indirect accesses, e.g. sparse matrix manipulations or lookup tables, it creates
214-
problems MLIR is not ready to address yet. MLIR needs to internally store
215-
constants of aggregate types and emit code operating on values of those types,
216-
which are subject to target-specific size and alignment constraints. Since MLIR
217-
does not have a target description mechanism at the moment, it cannot reliably
218-
emit such code. Moreover, some platforms may not support vectors of type
219-
equivalent to `index`.
205+
### Index type disallowed in vector/memref types
206+
207+
Index types are not allowed as elements of `vector` and `memref` types. Index
208+
types are intended to be used for platform-specific "size" values and may appear
209+
in subscripts, sizes of aggregate types and affine expressions. They are also
210+
tightly coupled with `affine.apply` and affine.load/store operations; having
211+
`index` type is a necessary precondition of a value to be acceptable by these
212+
operations. While it may be useful to have `memref<?xindex>` to express indirect
213+
accesses, e.g. sparse matrix manipulations or lookup tables, it creates problems
214+
MLIR is not ready to address yet. MLIR needs to internally store constants of
215+
aggregate types and emit code operating on values of those types, which are
216+
subject to target-specific size and alignment constraints. Since MLIR does not
217+
have a target description mechanism at the moment, it cannot reliably emit such
218+
code. Moreover, some platforms may not support vectors of type equivalent to
219+
`index`.
220220

221221
Indirect access use cases can be alternatively supported by providing and
222222
`index_cast` instruction that allows for conversion between `index` and
223223
fixed-width integer types, at the SSA value level. It has an additional benefit
224224
of supporting smaller integer types, e.g. `i8` or `i16`, for small indices
225225
instead of (presumably larger) `index` type.
226226

227+
Index types are allowed as element types of `tensor` types. The `tensor` type
228+
specifically abstracts the target-specific aspects that intersect with the
229+
code-generation-related/lowering-related concerns explained above. In fact, the
230+
`tensor` type even allows dialect-specific types as element types.
231+
227232
### Bit width of a non-primitive types and `index` is undefined
228233

229234
The bit width of a compound type is not defined by MLIR, it may be defined by a

mlir/include/mlir/IR/StandardTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class TensorType : public ShapedType {
330330
// element type within that dialect.
331331
return type.isa<ComplexType>() || type.isa<FloatType>() ||
332332
type.isa<IntegerType>() || type.isa<OpaqueType>() ||
333-
type.isa<VectorType>() ||
333+
type.isa<VectorType>() || type.isa<IndexType>() ||
334334
(type.getKind() > Type::Kind::LAST_STANDARD_TYPE);
335335
}
336336

mlir/test/IR/invalid.mlir

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ func @indexvector(vector<4 x index>) -> () // expected-error {{vector elements m
2323

2424
func @indexmemref(memref<? x index>) -> () // expected-error {{invalid memref element type}}
2525

26-
// -----
27-
28-
func @indextensor(tensor<4 x index>) -> () // expected-error {{invalid tensor element type}}
29-
3026
// -----
3127
// Test no map in memref type.
3228
func @memrefs(memref<2x4xi8, >) // expected-error {{expected list element}}

0 commit comments

Comments
 (0)