18#include <mlir/Dialect/Arith/IR/Arith.h>
19#include <mlir/Dialect/Utils/IndexingUtils.h>
20#include <mlir/IR/Attributes.h>
21#include <mlir/IR/BuiltinOps.h>
22#include <mlir/IR/Diagnostics.h>
23#include <mlir/IR/OwningOpRef.h>
24#include <mlir/IR/SymbolTable.h>
25#include <mlir/IR/ValueRange.h>
26#include <mlir/Support/LogicalResult.h>
28#include <llvm/ADT/ArrayRef.h>
29#include <llvm/ADT/Twine.h>
47 OpBuilder &odsBuilder, OperationState &odsState,
ArrayType result, ValueRange elements
49 odsState.addTypes(result);
50 odsState.addOperands(elements);
59 OpBuilder &odsBuilder, OperationState &odsState,
ArrayType result,
60 ArrayRef<ValueRange> mapOperands, DenseI32ArrayAttr numDimsPerMap
62 odsState.addTypes(result);
64 odsBuilder, odsState, mapOperands, numDimsPerMap
77llvm::SmallVector<Type> CreateArrayOp::resultTypeToElementsTypes(Type resultType) {
79 ArrayType a = llvm::cast<ArrayType>(resultType);
80 return llvm::SmallVector<Type>(a.getNumElements(), a.
getElementType());
83ParseResult CreateArrayOp::parseInferredArrayType(
84 OpAsmParser & , llvm::SmallVector<Type, 1> &elementsTypes,
85 ArrayRef<OpAsmParser::UnresolvedOperand> elements, Type resultType
87 assert(elementsTypes.size() == 0);
90 if (elements.size() > 0) {
91 elementsTypes.append(resultTypeToElementsTypes(resultType));
96void CreateArrayOp::printInferredArrayType(
97 OpAsmPrinter &printer,
CreateArrayOp, TypeRange, OperandRange, Type
104 assert(llvm::isa<ArrayType>(retTy));
107 SmallVector<AffineMapAttr> mapAttrs;
109 ArrayType arrTy = llvm::cast<ArrayType>(retTy);
111 if (AffineMapAttr m = dyn_cast<AffineMapAttr>(a)) {
112 mapAttrs.push_back(m);
122 assert(
getElements().empty() &&
"must run after initialization is split from allocation");
124 if (!arrType.hasStaticShape() || arrType.getNumElements() == 1) {
128 return {DestructurableMemorySlot {{
getResult(), arrType}, std::move(*destructured)}};
135 const DestructurableMemorySlot &slot,
const SmallPtrSetImpl<Attribute> &usedIndices,
136 OpBuilder &builder, SmallVectorImpl<DestructurableAllocationOpInterface> &newAllocators
139 assert(slot.elemType == getType());
141 builder.setInsertionPointAfter(*
this);
143 DenseMap<Attribute, MemorySlot> slotMap;
144 for (Attribute index : usedIndices) {
146 ArrayAttr indexAsArray = llvm::dyn_cast<ArrayAttr>(index);
147 assert(indexAsArray &&
"expected ArrayAttr");
149 Type destructAs = getType().getTypeAtIndex(indexAsArray);
150 assert(destructAs == slot.subelementTypes.lookup(indexAsArray));
152 ArrayType destructAsArrayTy = llvm::dyn_cast<ArrayType>(destructAs);
153 assert(destructAsArrayTy &&
"expected ArrayType");
155 auto subCreate = builder.create<
CreateArrayOp>(getLoc(), destructAsArrayTy);
156 newAllocators.push_back(subCreate);
157 slotMap.try_emplace<MemorySlot>(index, {subCreate.getResult(), destructAs});
165 const DestructurableMemorySlot &slot, OpBuilder &
175 if (!arrType.hasStaticShape()) {
180 if (arrType.getNumElements() != 1) {
196 const MemorySlot & , Value defaultValue, OpBuilder &
198 if (defaultValue.use_empty()) {
199 defaultValue.getDefiningOp()->erase();
215 if (arrTy.hasStaticShape()) {
217 return ArrayAttr::get(getContext(), *converted);
225 const DestructurableMemorySlot &slot, SmallPtrSetImpl<Attribute> &usedIndices,
226 SmallVectorImpl<MemorySlot> & ,
const DataLayout &
243 usedIndices.insert(indexAsAttr);
249 const DestructurableMemorySlot &slot, DenseMap<Attribute, MemorySlot> &subslots,
250 OpBuilder &builder,
const DataLayout &
258 assert(indexAsAttr &&
"canRewire() should have returned false");
259 const MemorySlot &memorySlot = subslots.at(indexAsAttr);
262 OpBuilder::InsertionGuard guard(builder);
263 builder.setInsertionPoint(this->getOperation());
270 return DeletionKind::Keep;
283 auto compare = numIndices <=> dims.size();
285 return errFn().append(
286 "has ", (compare < 0 ?
"insufficient" :
"too many"),
" indexed dimensions: expected ",
287 dims.size(),
" but found ", numIndices
302 llvm::SmallVectorImpl<Type> &inferredReturnTypes
304 inferredReturnTypes.resize(1);
305 Type lvalType = adaptor.
getArrRef().getType();
306 assert(llvm::isa<ArrayType>(lvalType));
307 inferredReturnTypes[0] = llvm::cast<ArrayType>(lvalType).getElementType();
322 const MemorySlot &slot,
const SmallPtrSetImpl<OpOperand *> &blockingUses,
323 SmallVectorImpl<OpOperand *> & ,
const DataLayout &
325 if (blockingUses.size() != 1) {
328 Value blockingUse = (*blockingUses.begin())->get();
329 return blockingUse == slot.ptr &&
getArrRef() == slot.ptr &&
335 const MemorySlot & ,
const SmallPtrSetImpl<OpOperand *> & ,
336 OpBuilder & , Value reachingDefinition,
const DataLayout &
339 getResult().replaceAllUsesWith(reachingDefinition);
340 return DeletionKind::Delete;
361 const MemorySlot &slot,
const SmallPtrSetImpl<OpOperand *> &blockingUses,
362 SmallVectorImpl<OpOperand *> & ,
const DataLayout &
364 if (blockingUses.size() != 1) {
367 Value blockingUse = (*blockingUses.begin())->get();
368 return blockingUse == slot.ptr &&
getArrRef() == slot.ptr &&
getRvalue() != slot.ptr &&
374 const MemorySlot &,
const SmallPtrSetImpl<OpOperand *> &, OpBuilder &, Value,
const DataLayout &
376 return DeletionKind::Delete;
390 llvm::SmallVectorImpl<Type> &inferredReturnTypes
392 size_t numToSkip = adaptor.
getIndices().size();
393 Type arrRefType = adaptor.
getArrRef().getType();
394 assert(llvm::isa<ArrayType>(arrRefType));
395 ArrayType arrRefArrType = llvm::cast<ArrayType>(arrRefType);
399 auto compare = numToSkip <=> arrRefDimSizes.size();
401 return mlir::emitOptionalError(
406 }
else if (compare > 0) {
407 return mlir::emitOptionalError(
409 "' op cannot select more dimensions than exist in the source array"
414 inferredReturnTypes.resize(1);
415 inferredReturnTypes[0] =
438 assert(llvm::isa<ArrayType>(rValueType));
439 ArrayType rValueArrType = llvm::cast<ArrayType>(rValueType);
447 if (numIndices > lhsDims) {
448 return emitOpError(
"cannot select more dimensions than exist in the source array");
452 auto compare = (numIndices + rhsDims) <=> lhsDims;
454 return emitOpError().append(
455 "has ", (compare < 0 ?
"insufficient" :
"too many"),
" indexed dimensions: expected ",
456 (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 build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::llzk::array::ArrayType result, ::mlir::ValueRange elements={})
::mlir::TypedValue<::llzk::array::ArrayType > getResult()
::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)
constexpr T checkedCast(U u) noexcept
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 ...