15#include <mlir/IR/BuiltinTypeInterfaces.h>
22 MLIRContext *ctx, ArrayRef<int64_t> shape, SmallVector<Attribute> &dimensionSizes
25 dimensionSizes = llvm::map_to_vector(shape, [&builder](int64_t v) -> Attribute {
26 return builder.getIndexAttr(v);
28 assert(dimensionSizes.size() == shape.size());
33 EmitErrorFn emitError, ArrayRef<Attribute> dimensionSizes, SmallVector<int64_t> &shape
35 assert(shape.empty());
43 for (Attribute a : dimensionSizes) {
44 if (
auto p = llvm::dyn_cast_if_present<IntegerAttr>(a)) {
46 }
else if (llvm::isa_and_present<SymbolRefAttr, AffineMapAttr>(a)) {
48 shape.push_back(ShapedType::kDynamic);
51 llvm::report_fatal_error(
"computeShapeFromDims() is out of sync with ArrayDimensionTypes");
55 assert(shape.size() == dimensionSizes.size());
60 AsmParser &parser, SmallVector<int64_t> &shape, SmallVector<Attribute> dimensionSizes
64 auto emitError = [&parser] {
74 function_ref<InFlightDiagnostic()> emitError, Type elementType,
75 ArrayRef<Attribute> dimensionSizes, ArrayRef<int64_t> shape
93inline ArrayType createArrayOfSizeOne(Type elemType) {
return ArrayType::get(elemType, {1}); }
97bool ArrayType::collectIndices(llvm::function_ref<
void(ArrayAttr)> inserter)
const {
98 if (!hasStaticShape()) {
101 MLIRContext *ctx = getContext();
103 for (int64_t e = getNumElements(), i = 0; i < e; ++i) {
104 auto delinearized = idxGen.delinearize(i, ctx);
105 assert(delinearized.has_value());
106 inserter(ArrayAttr::get(ctx, delinearized.value()));
112 SmallVector<ArrayAttr> ret;
113 bool success = collectIndices([&ret](ArrayAttr v) { ret.push_back(v); });
114 return success ? std::make_optional(ret) : std::nullopt;
119 DenseMap<Attribute, Type> ret;
121 bool success = collectIndices([&](ArrayAttr v) { ret[v] = destructAs; });
122 return success ? std::make_optional(ret) : std::nullopt;
127 if (!hasStaticShape()) {
131 ArrayAttr indexAttr = llvm::dyn_cast<ArrayAttr>(index);
143ParseResult
parseAttrVec(AsmParser &parser, SmallVector<Attribute> &value) {
144 SmallVector<Attribute> attrs;
145 auto parseElement = [&parser, &value]() -> ParseResult {
146 auto qResult = parser.parseOptionalQuestion();
147 if (succeeded(qResult)) {
148 auto &builder = parser.getBuilder();
149 value.push_back(builder.getIntegerAttr(builder.getIndexType(), ShapedType::kDynamic));
153 if (succeeded(attrParseResult)) {
154 auto emitError = [&parser] {
157 FailureOr<Attribute> forced =
forceIntAttrType(*attrParseResult, emitError);
158 if (failed(forced)) {
161 value.push_back(*forced);
163 return ParseResult(attrParseResult);
165 if (failed(parser.parseCommaSeparatedList(AsmParser::Delimiter::None, parseElement))) {
166 return parser.emitError(parser.getCurrentLocation(),
"failed to parse array dimensions");
Wrapper around InFlightDiagnostic that can either be a regular InFlightDiagnostic or a special versio...
static ArrayIndexGen from(ArrayType)
Construct new ArrayIndexGen. Will assert if hasStaticShape() is false.
::llvm::ArrayRef< int64_t > getShape() const
ArrayType cloneWith(std::optional<::llvm::ArrayRef< int64_t > > shape, ::mlir::Type elementType) const
Clone this type with the given shape and element type.
std::optional<::llvm::SmallVector<::mlir::ArrayAttr > > getSubelementIndices() const
Return a list of all valid indices for this ArrayType.
::mlir::Type getElementType() const
::std::optional<::llvm::DenseMap<::mlir::Attribute, ::mlir::Type > > getSubelementIndexMap() const
Required by DestructurableTypeInterface / SROA pass.
::mlir::Type getTypeAtIndex(::mlir::Attribute index) const
Required by DestructurableTypeInterface / SROA pass.
static ArrayType get(::mlir::Type elementType, ::llvm::ArrayRef<::mlir::Attribute > dimensionSizes)
::llvm::LogicalResult verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::Type elementType, ::llvm::ArrayRef<::mlir::Attribute > dimensionSizes, ::llvm::ArrayRef< int64_t > shape)
::llvm::ArrayRef<::mlir::Attribute > getDimensionSizes() const
LogicalResult computeShapeFromDims(EmitErrorFn emitError, ArrayRef< Attribute > dimensionSizes, SmallVector< int64_t > &shape)
ParseResult parseAttrVec(AsmParser &parser, SmallVector< Attribute > &value)
void printDerivedShape(AsmPrinter &, ArrayRef< int64_t >, ArrayRef< Attribute >)
LogicalResult computeDimsFromShape(MLIRContext *ctx, ArrayRef< int64_t > shape, SmallVector< Attribute > &dimensionSizes)
void printAttrVec(AsmPrinter &printer, ArrayRef< Attribute > value)
ParseResult parseDerivedShape(AsmParser &parser, SmallVector< int64_t > &shape, SmallVector< Attribute > dimensionSizes)
FailureOr< Attribute > forceIntAttrType(Attribute attr, EmitErrorFn emitError)
void printAttrs(AsmPrinter &printer, ArrayRef< Attribute > attrs, const StringRef &separator)
LogicalResult verifyArrayType(EmitErrorFn emitError, Type elementType, ArrayRef< Attribute > dimensionSizes)
llvm::function_ref< InFlightDiagnosticWrapper()> EmitErrorFn
Callback to produce an error diagnostic.
OwningEmitErrorFn wrapNonNullableInFlightDiagnostic(llvm::function_ref< mlir::InFlightDiagnostic()> emitError)
int64_t fromAPInt(const llvm::APInt &i)
LogicalResult verifyArrayDimSizes(EmitErrorFn emitError, ArrayRef< Attribute > dimensionSizes)