Background
All tensor/memref/vector types currently have a shared type interface ShapedType. This interface provides the following interface methods:
getElementTypegetShape: Asserts that the type is ranked and returns the shape.cloneWith
There are also a few helper functions around those interface methods such as getRank, getDimSize, etc. Those also assert that the type is ranked.
Proposal
Add a new RankedShapedType interface that is a sub-interface of ShapedType, utilizing interface inheritance (âš™ D140198 [mlir] Add support for interface inheritance).
All previously ranked ShapedTypes (ranked tensor types, ranked memref types, vector types) implement RankedShapedType instead of ShapedType. (They are still considered ShapedTypes because of the interface inheritance.)
Interface methods and helper functions/members that only make sense for ranked types (e.g., getShape(), getDimSize(), kDynamicSize) are moved to RankedShapedType.
(Dashed lines indicate interface implementation.)
Prototype: âš™ D148453 [mlir][IR] Add RankedShapedType interface. Most changes are mechanical, but because so many parts of the code base are affected, I decided to send this as an RFC.
Benefits
Errors that were previously runtime assertion failures (e.g., calling getShape() on an unranked tensor through TensorType or ShapedType) are now compile time errors: TensorType, BaseMemRefType and ShapeType no longer have functions such as getShape().
Potential Next Step
RankedShapedType and ShapedType could be implemented via external models in the tensor/memref/vector dialects. Then, interface methods such as createDimOp and createRankOp could be added, cleaning up various parts of the code base that currently have a hard-coded switch-case statement when both memref and tensor types are supported.

