Instances of the Type class are immutable and uniqued. They wrap a pointer to the storage object owned by MLIRContext. Therefore, instances of Type are passed around by value.
Some types are "primitives" meaning they do not have any parameters, for example the Index type. Parametric types have additional information that differentiates the types of the same kind between them, for example the Integer type has bitwidth, making i8 and i16 belong to the same kind by be different instances of the IntegerType.
Types are constructed and uniqued via the 'detail::TypeUniquer' class.
Derived type classes are expected to implement several required implementation hooks:
- Required:
- static bool kindof(unsigned kind);
- Returns if the provided type kind corresponds to an instance of the current type. Used for isa/dyn_cast casting functionality.
- Optional:
- static LogicalResult verifyConstructionInvariants( Optional<Location> loc, MLIRContext *context, Args... args)
- This method is invoked when calling the 'TypeBase::get/getChecked' methods to ensure that the arguments passed in are valid to construct a type instance with.
- This method is expected to return failure if a type cannot be constructed with 'args', success otherwise.
- 'args' must correspond with the arguments passed into the 'TypeBase::get' call after the type kind.
Type storage objects inherit from TypeStorage and contain the following:
- The type kind (for LLVM-style RTTI).
- The dialect that defined the type.
- Any parameters of the type. For non-parametric types, a convenience DefaultTypeStorage is provided. Parametric storage types must derive TypeStorage and respect the following:
- Define a type alias, KeyTy, to a type that uniquely identifies the instance of the type within its kind.
- The key type must be constructible from the values passed into the detail::TypeUniquer::get call after the type kind.
- If the KeyTy does not have an llvm::DenseMapInfo specialization, the storage class must define a hashing method: 'static unsigned hashKey(const KeyTy &)'
- Provide a method, 'bool operator==(const KeyTy &) const', to compare the storage instance against an instance of the key type.
- Provide a construction method: 'DerivedStorage *construct(TypeStorageAllocator &, const KeyTy &key)' that builds a unique instance of the derived storage. The arguments to this function are an allocator to store any uniqued data within the context and the key type for this storage.