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];
81 return emitError() <<
"memref layout mismatch between rank and affine map: "
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,...
static int64_t getNumElements(ShapedType type)
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
unsigned getNumDims() const
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...
Detect if any of the given parameter types has a sub-element handler.
LogicalResult verifyAffineMapAsLayout(AffineMap m, ArrayRef< int64_t > shape, function_ref< InFlightDiagnostic()> emitError)
This header declares functions that assist transformations in the MemRef dialect.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
This class represents an efficient way to signal success or failure.