12 #include "llvm/ADT/Sequence.h"
21 #include "mlir/IR/BuiltinAttributeInterfaces.cpp.inc"
28 return elementsAttr.getShapedType().getElementType();
32 return elementsAttr.getShapedType().getNumElements();
37 int64_t rank = type.getRank();
38 if (rank == 0 && index.size() == 1 && index[0] == 0)
40 if (rank !=
static_cast<int64_t
>(index.size()))
45 return llvm::all_of(llvm::seq<int>(0, rank), [&](
int i) {
46 int64_t dim =
static_cast<int64_t
>(index[i]);
47 return 0 <= dim && dim < shape[i];
50 bool ElementsAttr::isValidIndex(ElementsAttr elementsAttr,
52 return isValidIndex(elementsAttr.getShapedType(), index);
56 ShapedType shapeType = llvm::cast<ShapedType>(type);
57 assert(isValidIndex(shapeType, index) &&
58 "expected valid multi-dimensional index");
62 auto rank = shapeType.getRank();
64 uint64_t valueIndex = 0;
65 uint64_t dimMultiplier = 1;
66 for (
int i = rank - 1; i >= 0; --i) {
67 valueIndex += index[i] * dimMultiplier;
68 dimMultiplier *= shape[i];
80 if (m.getNumDims() != shape.size())
81 return emitError() <<
"memref layout mismatch between rank and affine map: "
82 << shape.size() <<
" != " << m.getNumDims();
static int64_t getNumElements(Type t)
Compute the total number of elements in the given type, also taking into account nested types.
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
This class represents a diagnostic that is inflight and set to be reported.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
LogicalResult verifyAffineMapAsLayout(AffineMap m, ArrayRef< int64_t > shape, function_ref< InFlightDiagnostic()> emitError)
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.