70 MemRefType srcType = subView.getSourceType();
72 if (!srcType.hasStaticShape())
78 if (srcType.getRank() != subView.getType().getRank())
82 if (!subView.hasUnitStride())
85 return VectorType::isValidElementType(srcType.getElementType());
93 if (!subView.getType().hasStaticShape())
100 return llvm::all_of(subView.getMixedOffsets(), [](
OpFoldResult offset) {
101 return getConstantIntValue(offset).has_value();
110 if (subView.getType().hasStaticShape())
117 return subView.hasZeroOffset();
126 assert(value &&
"expected a static offset");
127 offsets.push_back(*value);
135 auto parentType = cast<MemRefType>(subView.getSource().getType());
136 assert(parentType.hasStaticShape() &&
"expected a statically-shaped parent");
137 return VectorType::get(parentType.getShape(), parentType.getElementType());
143 memref::SubViewOp subView) {
147 return vector::CreateMaskOp::create(
149 VectorType::get(parentVecType.getShape(), builder.
getI1Type()), bounds);
166struct CopyOpMemOpModel
167 :
public PromotableMemOpInterface::ExternalModel<CopyOpMemOpModel,
169 bool loadsFrom(Operation *op,
const MemorySlot &slot)
const {
170 return cast<memref::CopyOp>(op).getSource() == slot.
ptr;
173 bool storesTo(Operation *op,
const MemorySlot &slot)
const {
174 return cast<memref::CopyOp>(op).getTarget() == slot.
ptr;
179 Value getStored(Operation *op,
const MemorySlot &slot, OpBuilder &builder,
180 Value ,
const DataLayout &dataLayout)
const {
181 auto copyOp = cast<memref::CopyOp>(op);
187 bool canUsesBeRemoved(Operation *op,
const MemorySlot &slot,
188 const SmallPtrSetImpl<OpOperand *> &blockingUses,
189 SmallVectorImpl<OpOperand *> &newBlockingUses,
190 const DataLayout &dataLayout)
const {
191 auto copyOp = cast<memref::CopyOp>(op);
192 auto vecType = dyn_cast<VectorType>(slot.
valueType);
193 if (!vecType || vecType.isScalable())
195 bool srcIsSlot = copyOp.getSource() == slot.
ptr;
196 bool dstIsSlot = copyOp.getTarget() == slot.
ptr;
199 if (srcIsSlot == dstIsSlot)
209 removeBlockingUses(Operation *op,
const MemorySlot &slot,
210 const SmallPtrSetImpl<OpOperand *> &blockingUses,
211 OpBuilder &builder, Value reachingDefinition,
212 const DataLayout &dataLayout)
const {
213 auto copyOp = cast<memref::CopyOp>(op);
214 if (copyOp.getSource() == slot.
ptr)
216 reachingDefinition, copyOp.getTarget());
217 return DeletionKind::Delete;
228struct SubViewOpPromotableModel
229 :
public PromotableOpInterface::ExternalModel<SubViewOpPromotableModel,
231 bool canUsesBeRemoved(Operation *op,
232 const SmallPtrSetImpl<OpOperand *> &blockingUses,
233 SmallVectorImpl<OpOperand *> &newBlockingUses,
234 const DataLayout &dataLayout)
const {
237 newBlockingUses.push_back(&use);
242 removeBlockingUses(Operation *op,
243 const SmallPtrSetImpl<OpOperand *> &blockingUses,
244 OpBuilder &builder)
const {
245 return DeletionKind::Delete;
264struct SubViewOpAliasModel
265 :
public PromotableAliaserInterface::ExternalModel<SubViewOpAliasModel,
267 void getPromotableSlotAliases(Operation *op,
268 OpOperand &aliasedSlotPointerOperand,
269 const MemorySlot &parentSlot,
270 SmallVectorImpl<MemorySlot> &newSlots)
const {
271 auto subView = cast<memref::SubViewOp>(op);
274 if (aliasedSlotPointerOperand.
get() != subView.getSource())
279 auto parentVecType = dyn_cast<VectorType>(parentSlot.
valueType);
285 auto resType = cast<MemRefType>(subView.getResult().getType());
286 VectorType aliasVecType =
287 VectorType::get(resType.getShape(), resType.getElementType());
288 newSlots.push_back(MemorySlot{subView.getResult(), aliasVecType});
293 newSlots.push_back(MemorySlot{subView.getResult(), parentVecType});
297 Value projectSlotValueToAliasValue(Operation *op,
300 const MemorySlot &aliasSlot,
302 OpBuilder &builder)
const {
303 auto subView = cast<memref::SubViewOp>(op);
310 auto aliasVecType = cast<VectorType>(aliasSlot.
valueType);
311 SmallVector<int64_t> strides(offsets.size(), 1);
312 return vector::ExtractStridedSliceOp::create(
313 builder, op->
getLoc(), slotValue, offsets,
314 aliasVecType.getShape(), strides)
318 Value projectAliasValueToSlotValue(Operation *op,
322 Value aliasValue, Value reachingDef,
323 OpBuilder &builder)
const {
324 auto subView = cast<memref::SubViewOp>(op);
328 Location loc = op->
getLoc();
330 return arith::SelectOp::create(builder, loc, mask, aliasValue,
335 SmallVector<int64_t> strides(offsets.size(), 1);
336 return vector::InsertStridedSliceOp::create(
337 builder, op->
getLoc(), aliasValue, reachingDef, offsets, strides)
350 memref::SubViewOp::attachInterface<SubViewOpAliasModel>(*ctx);
351 memref::SubViewOp::attachInterface<SubViewOpPromotableModel>(*ctx);
352 memref::CopyOp::attachInterface<CopyOpMemOpModel>(*ctx);
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
IRValueT get() const
Return the current value being used by this operand.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
This class helps build Operations.
This class represents a single result from folding an operation.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Location getLoc()
The source location the operation was defined or derived from.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
use_range getUses() const
Returns a range of all uses, which is useful for iterating over all uses.
void registerMemorySlotInterfacesExternalModels(DialectRegistry ®istry)
Operation * createWriteOrMaskedWrite(OpBuilder &builder, Location loc, Value vecToStore, Value dest, SmallVector< Value > writeIndices={}, bool useInBoundsInsteadOfMasking=false, AffineMap permutationMap=AffineMap())
Create a TransferWriteOp of vecToStore into dest.
Value createReadOrMaskedRead(OpBuilder &builder, Location loc, Value source, const VectorType &vecToReadTy, std::optional< Value > padValue=std::nullopt, bool useInBoundsInsteadOfMasking=false, ArrayRef< Value > indices={}, AffineMap permutationMap=AffineMap())
Creates a TransferReadOp from source.
Include the generated interface declarations.
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
DeletionKind
Returned by operation promotion logic requesting the deletion of an operation.
Value getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc, OpFoldResult ofr)
Converts an OpFoldResult to a Value.
Type valueType
Type of the value contained in the slot.
Value ptr
Pointer to the memory slot, used by operations to refer to it.