16#include <mlir/Dialect/Arith/IR/Arith.h>
17#include <mlir/Dialect/Utils/IndexingUtils.h>
18#include <mlir/IR/Attributes.h>
19#include <mlir/IR/BuiltinOps.h>
20#include <mlir/IR/Diagnostics.h>
21#include <mlir/IR/OwningOpRef.h>
22#include <mlir/IR/SymbolTable.h>
23#include <mlir/IR/ValueRange.h>
24#include <mlir/Support/LogicalResult.h>
26#include <llvm/ADT/ArrayRef.h>
27#include <llvm/ADT/Twine.h>
45 OpBuilder &odsBuilder, OperationState &odsState,
ArrayType result, ValueRange elements
47 odsState.addTypes(result);
48 odsState.addOperands(elements);
52 odsBuilder, odsState,
static_cast<int32_t
>(elements.size())
57 OpBuilder &odsBuilder, OperationState &odsState,
ArrayType result,
58 ArrayRef<ValueRange> mapOperands, DenseI32ArrayAttr numDimsPerMap
60 odsState.addTypes(result);
62 odsBuilder, odsState, mapOperands, numDimsPerMap
75llvm::SmallVector<Type> CreateArrayOp::resultTypeToElementsTypes(Type resultType) {
77 ArrayType a = llvm::cast<ArrayType>(resultType);
78 return llvm::SmallVector<Type>(a.getNumElements(), a.
getElementType());
82 OpAsmParser &parser, llvm::SmallVector<Type, 1> &elementsTypes,
83 ArrayRef<OpAsmParser::UnresolvedOperand> elements, Type resultType
85 assert(elementsTypes.size() == 0);
88 if (elements.size() > 0) {
89 elementsTypes.append(resultTypeToElementsTypes(resultType));
95 OpAsmPrinter &printer,
CreateArrayOp, TypeRange, OperandRange, Type
102 assert(llvm::isa<ArrayType>(retTy));
105 SmallVector<AffineMapAttr> mapAttrs;
107 ArrayType arrTy = llvm::cast<ArrayType>(retTy);
109 if (AffineMapAttr m = dyn_cast<AffineMapAttr>(a)) {
110 mapAttrs.push_back(m);
120 assert(
getElements().empty() &&
"must run after initialization is split from allocation");
122 if (!arrType.hasStaticShape() || arrType.getNumElements() == 1) {
126 return {DestructurableMemorySlot {{
getResult(), arrType}, std::move(*destructured)}};
133 const DestructurableMemorySlot &slot,
const SmallPtrSetImpl<Attribute> &usedIndices,
134 OpBuilder &builder, SmallVectorImpl<DestructurableAllocationOpInterface> &newAllocators
137 assert(slot.elemType == getType());
139 builder.setInsertionPointAfter(*
this);
141 DenseMap<Attribute, MemorySlot> slotMap;
142 for (Attribute index : usedIndices) {
144 ArrayAttr indexAsArray = llvm::dyn_cast<ArrayAttr>(index);
145 assert(indexAsArray &&
"expected ArrayAttr");
147 Type destructAs = getType().getTypeAtIndex(indexAsArray);
148 assert(destructAs == slot.subelementTypes.lookup(indexAsArray));
150 ArrayType destructAsArrayTy = llvm::dyn_cast<ArrayType>(destructAs);
151 assert(destructAsArrayTy &&
"expected ArrayType");
153 auto subCreate = builder.create<
CreateArrayOp>(getLoc(), destructAsArrayTy);
154 newAllocators.push_back(subCreate);
155 slotMap.try_emplace<MemorySlot>(index, {subCreate.getResult(), destructAs});
163 const DestructurableMemorySlot &slot, OpBuilder &
173 if (!arrType.hasStaticShape()) {
178 if (arrType.getNumElements() != 1) {
194 const MemorySlot & , Value defaultValue, OpBuilder &
196 if (defaultValue.use_empty()) {
197 defaultValue.getDefiningOp()->erase();
213 if (arrTy.hasStaticShape()) {
215 return ArrayAttr::get(getContext(), *converted);
223 const DestructurableMemorySlot &slot, SmallPtrSetImpl<Attribute> &usedIndices,
224 SmallVectorImpl<MemorySlot> & ,
const DataLayout &
241 usedIndices.insert(indexAsAttr);
247 const DestructurableMemorySlot &slot, DenseMap<Attribute, MemorySlot> &subslots,
248 OpBuilder &builder,
const DataLayout &
256 assert(indexAsAttr &&
"canRewire() should have returned false");
257 const MemorySlot &memorySlot = subslots.at(indexAsAttr);
260 OpBuilder::InsertionGuard guard(builder);
261 builder.setInsertionPoint(this->getOperation());
268 return DeletionKind::Keep;
281 auto compare = numIndices <=> dims.size();
283 return errFn().append(
284 "has ", (compare < 0 ?
"insufficient" :
"too many"),
" indexed dimensions: expected ",
285 dims.size(),
" but found ", numIndices
300 llvm::SmallVectorImpl<Type> &inferredReturnTypes
302 inferredReturnTypes.resize(1);
303 Type lvalType = adaptor.
getArrRef().getType();
304 assert(llvm::isa<ArrayType>(lvalType));
305 inferredReturnTypes[0] = llvm::cast<ArrayType>(lvalType).getElementType();
320 const MemorySlot &slot,
const SmallPtrSetImpl<OpOperand *> &blockingUses,
321 SmallVectorImpl<OpOperand *> &newBlockingUses,
const DataLayout &
323 if (blockingUses.size() != 1) {
326 Value blockingUse = (*blockingUses.begin())->get();
327 return blockingUse == slot.ptr &&
getArrRef() == slot.ptr &&
333 const MemorySlot & ,
const SmallPtrSetImpl<OpOperand *> &blockingUses,
334 OpBuilder & , Value reachingDefinition,
const DataLayout &
337 getResult().replaceAllUsesWith(reachingDefinition);
338 return DeletionKind::Delete;
359 const MemorySlot &slot,
const SmallPtrSetImpl<OpOperand *> &blockingUses,
360 SmallVectorImpl<OpOperand *> &newBlockingUses,
const DataLayout &
362 if (blockingUses.size() != 1) {
365 Value blockingUse = (*blockingUses.begin())->get();
366 return blockingUse == slot.ptr &&
getArrRef() == slot.ptr &&
getRvalue() != slot.ptr &&
372 const MemorySlot &,
const SmallPtrSetImpl<OpOperand *> &, OpBuilder &, Value,
const DataLayout &
374 return DeletionKind::Delete;
388 llvm::SmallVectorImpl<Type> &inferredReturnTypes
390 size_t numToSkip = adaptor.
getIndices().size();
391 Type arrRefType = adaptor.
getArrRef().getType();
392 assert(llvm::isa<ArrayType>(arrRefType));
393 ArrayType arrRefArrType = llvm::cast<ArrayType>(arrRefType);
397 auto compare = numToSkip <=> arrRefDimSizes.size();
399 return mlir::emitOptionalError(
404 }
else if (compare > 0) {
405 return mlir::emitOptionalError(
407 "' op cannot select more dimensions than exist in the source array"
412 inferredReturnTypes.resize(1);
413 inferredReturnTypes[0] =
436 assert(llvm::isa<ArrayType>(rValueType));
437 ArrayType rValueArrType = llvm::cast<ArrayType>(rValueType);
445 if (numIndices > lhsDims) {
446 return emitOpError(
"cannot select more dimensions than exist in the source array");
450 auto compare = (numIndices + rhsDims) <=> lhsDims;
452 return emitOpError().append(
453 "has ", (compare < 0 ?
"insufficient" :
"too many"),
" indexed dimensions: expected ",
454 (lhsDims - rhsDims),
" but found ", numIndices
::mlir::DeletionKind rewire(const ::mlir::DestructurableMemorySlot &slot, ::llvm::DenseMap<::mlir::Attribute, ::mlir::MemorySlot > &subslots, ::mlir::OpBuilder &builder, const ::mlir::DataLayout &dataLayout)
Required by companion interface DestructurableAccessorOpInterface / SROA pass.
::mlir::Operation::operand_range getIndices()
Gets the operand range containing the index for each dimension.
::mlir::OpOperand & getArrRefMutable()
Gets the mutable operand slot holding the SSA Value for the referenced array.
inline ::mlir::ArrayRef<::mlir::Attribute > getValueOperandDims()
Compute the dimensions of the read/write value.
::mlir::ArrayAttr indexOperandsToAttributeArray()
Returns the multi-dimensional indices of the array access as an Attribute array or a null pointer if ...
::mlir::TypedValue<::llzk::array::ArrayType > getArrRef()
Gets the SSA Value for the referenced array.
bool canRewire(const ::mlir::DestructurableMemorySlot &slot, ::llvm::SmallPtrSetImpl<::mlir::Attribute > &usedIndices, ::mlir::SmallVectorImpl<::mlir::MemorySlot > &mustBeSafelyUsed, const ::mlir::DataLayout &dataLayout)
Required by companion interface DestructurableAccessorOpInterface / SROA pass.
inline ::llzk::array::ArrayType getArrRefType()
Gets the type of the referenced array.
::mlir::MutableOperandRange getIndicesMutable()
Gets the mutable operand range containing the index for each dimension.
static ArrayIndexGen from(ArrayType)
Construct new ArrayIndexGen. Will assert if hasStaticShape() is false.
inline ::llzk::array::ArrayType getArrRefType()
Gets the type of the referenced base array.
::llvm::LogicalResult verifySymbolUses(::mlir::SymbolTableCollection &symbolTable)
::mlir::Type getElementType() const
::std::optional<::llvm::DenseMap<::mlir::Attribute, ::mlir::Type > > getSubelementIndexMap() const
Required by DestructurableTypeInterface / SROA pass.
static ArrayType get(::mlir::Type elementType, ::llvm::ArrayRef<::mlir::Attribute > dimensionSizes)
::llvm::ArrayRef<::mlir::Attribute > getDimensionSizes() const
static void printInferredArrayType(::mlir::OpAsmPrinter &printer, CreateArrayOp, ::mlir::TypeRange, ::mlir::OperandRange, ::mlir::Type)
static void build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::llzk::array::ArrayType result, ::mlir::ValueRange elements={})
::mlir::TypedValue<::llzk::array::ArrayType > getResult()
::mlir::ParseResult parseInferredArrayType(::mlir::OpAsmParser &parser, ::llvm::SmallVector<::mlir::Type, 1 > &elementsTypes, ::mlir::ArrayRef<::mlir::OpAsmParser::UnresolvedOperand > elements, ::mlir::Type resultType)
::llvm::SmallVector<::mlir::DestructurableMemorySlot > getDestructurableSlots()
Required by DestructurableAllocationOpInterface / SROA pass.
::std::optional<::mlir::PromotableAllocationOpInterface > handlePromotionComplete(const ::mlir::MemorySlot &slot, ::mlir::Value defaultValue, ::mlir::OpBuilder &builder)
Required by PromotableAllocationOpInterface / mem2reg pass.
::std::optional<::mlir::DestructurableAllocationOpInterface > handleDestructuringComplete(const ::mlir::DestructurableMemorySlot &slot, ::mlir::OpBuilder &builder)
Required by DestructurableAllocationOpInterface / SROA pass.
::llvm::LogicalResult verify()
::mlir::Value getDefaultValue(const ::mlir::MemorySlot &slot, ::mlir::OpBuilder &builder)
Required by PromotableAllocationOpInterface / mem2reg pass.
::llvm::SmallVector<::mlir::MemorySlot > getPromotableSlots()
Required by PromotableAllocationOpInterface / mem2reg pass.
::llvm::DenseMap<::mlir::Attribute, ::mlir::MemorySlot > destructure(const ::mlir::DestructurableMemorySlot &slot, const ::llvm::SmallPtrSetImpl<::mlir::Attribute > &usedIndices, ::mlir::OpBuilder &builder, ::mlir::SmallVectorImpl<::mlir::DestructurableAllocationOpInterface > &newAllocators)
Required by DestructurableAllocationOpInterface / SROA pass.
::llvm::ArrayRef< int32_t > getNumDimsPerMap()
void getAsmResultNames(::mlir::OpAsmSetValueNameFn setNameFn)
void handleBlockArgument(const ::mlir::MemorySlot &slot, ::mlir::BlockArgument argument, ::mlir::OpBuilder &builder)
Required by PromotableAllocationOpInterface / mem2reg pass.
::mlir::OperandRangeRange getMapOperands()
::mlir::Operation::operand_range getElements()
::llvm::LogicalResult verifySymbolUses(::mlir::SymbolTableCollection &symbolTable)
inline ::llzk::array::ArrayType getArrRefType()
Gets the type of the referenced base array.
::mlir::Operation::operand_range getIndices()
::llvm::LogicalResult verify()
::mlir::TypedValue<::llzk::array::ArrayType > getRvalue()
::llvm::LogicalResult verifySymbolUses(::mlir::SymbolTableCollection &symbolTable)
::mlir::DeletionKind removeBlockingUses(const ::mlir::MemorySlot &slot, const ::llvm::SmallPtrSetImpl< mlir::OpOperand * > &blockingUses, ::mlir::OpBuilder &builder, ::mlir::Value reachingDefinition, const ::mlir::DataLayout &dataLayout)
Required by PromotableMemOpInterface / mem2reg pass.
inline ::llzk::array::ArrayType getArrRefType()
Gets the type of the referenced base array.
::mlir::TypedValue<::mlir::Type > getResult()
::mlir::TypedValue<::llzk::array::ArrayType > getArrRef()
::mlir::Operation::operand_range getIndices()
::llvm::LogicalResult inferReturnTypes(::mlir::MLIRContext *context, ::std::optional<::mlir::Location > location, ::mlir::ValueRange operands, ::mlir::DictionaryAttr attributes, ::mlir::OpaqueProperties properties, ::mlir::RegionRange regions, ::llvm::SmallVectorImpl<::mlir::Type > &inferredReturnTypes)
::llvm::LogicalResult verify()
::llvm::LogicalResult verifySymbolUses(::mlir::SymbolTableCollection &symbolTable)
static constexpr ::llvm::StringLiteral getOperationName()
static bool isCompatibleReturnTypes(::mlir::TypeRange l, ::mlir::TypeRange r)
bool canUsesBeRemoved(const ::mlir::MemorySlot &slot, const ::llvm::SmallPtrSetImpl<::mlir::OpOperand * > &blockingUses, ::llvm::SmallVectorImpl<::mlir::OpOperand * > &newBlockingUses, const ::mlir::DataLayout &datalayout)
Required by PromotableMemOpInterface / mem2reg pass.
::llvm::LogicalResult verify()
::mlir::Operation::operand_range getIndices()
inline ::llzk::array::ArrayType getArrRefType()
Gets the type of the referenced base array.
bool canUsesBeRemoved(const ::mlir::MemorySlot &slot, const ::llvm::SmallPtrSetImpl<::mlir::OpOperand * > &blockingUses, ::llvm::SmallVectorImpl<::mlir::OpOperand * > &newBlockingUses, const ::mlir::DataLayout &datalayout)
Required by PromotableMemOpInterface / mem2reg pass.
::mlir::DeletionKind removeBlockingUses(const ::mlir::MemorySlot &slot, const ::llvm::SmallPtrSetImpl< mlir::OpOperand * > &blockingUses, ::mlir::OpBuilder &builder, ::mlir::Value reachingDefinition, const ::mlir::DataLayout &dataLayout)
Required by PromotableMemOpInterface / mem2reg pass.
::mlir::TypedValue<::llzk::array::ArrayType > getArrRef()
::llvm::LogicalResult verifySymbolUses(::mlir::SymbolTableCollection &symbolTable)
::mlir::TypedValue<::mlir::Type > getRvalue()
OpClass::Properties & buildInstantiationAttrs(mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState, mlir::ArrayRef< mlir::ValueRange > mapOperands, mlir::DenseI32ArrayAttr numDimsPerMap, int32_t firstSegmentSize=0)
Utility for build() functions that initializes the operandSegmentSizes, mapOpGroupSizes,...
LogicalResult verifyAffineMapInstantiations(OperandRangeRange mapOps, ArrayRef< int32_t > numDimsPerMap, ArrayRef< AffineMapAttr > mapAttrs, Operation *origin)
OpClass::Properties & buildInstantiationAttrsEmpty(mlir::OpBuilder &odsBuilder, mlir::OperationState &odsState, int32_t firstSegmentSize=0)
Utility for build() functions that initializes the operandSegmentSizes, mapOpGroupSizes,...
LogicalResult verifySubArrayType(EmitErrorFn emitError, ArrayType arrayType, ArrayType subArrayType)
Determine if the subArrayType is a valid subarray of arrayType.
bool singletonTypeListsUnify(Iter1 lhs, Iter2 rhs, mlir::ArrayRef< llvm::StringRef > rhsReversePrefix={}, UnificationMap *unifications=nullptr)
LogicalResult verifyTypeResolution(SymbolTableCollection &tables, Operation *origin, Type ty)
OwningEmitErrorFn getEmitOpErrFn(mlir::Operation *op)
std::function< InFlightDiagnosticWrapper()> OwningEmitErrorFn
This type is required in cases like the functions below to take ownership of the lambda so it is not ...