57 if (blockingUses.size() != 1)
59 Value blockingUse = (*blockingUses.begin())->get();
60 if (blockingUse != slot.
ptr || xferOp.getBase() != slot.
ptr)
64 if (!isa<MemRefType>(xferOp.getBase().getType()))
68 if (xferOp.getVectorType() != slot.
valueType)
74 if (!constIndex || *constIndex != 0)
79 if (!xferOp.getPermutationMap().isIdentity())
85 if (xferOp.hasOutOfBoundsDim())
97struct TransferReadOpMemOpModel
98 :
public PromotableMemOpInterface::ExternalModel<TransferReadOpMemOpModel,
99 vector::TransferReadOp> {
100 bool loadsFrom(Operation *op,
const MemorySlot &slot)
const {
101 return cast<vector::TransferReadOp>(op).getBase() == slot.
ptr;
104 bool storesTo(Operation *op,
const MemorySlot &slot)
const {
return false; }
106 Value getStored(Operation *op,
const MemorySlot &slot, OpBuilder &builder,
107 Value reachingDef,
const DataLayout &dataLayout)
const {
108 llvm_unreachable(
"getStored should not be called on TransferReadOp");
111 bool canUsesBeRemoved(Operation *op,
const MemorySlot &slot,
112 const SmallPtrSetImpl<OpOperand *> &blockingUses,
113 SmallVectorImpl<OpOperand *> &newBlockingUses,
114 const DataLayout &dataLayout)
const {
122 removeBlockingUses(Operation *op,
const MemorySlot &slot,
123 const SmallPtrSetImpl<OpOperand *> &blockingUses,
124 OpBuilder &builder, Value reachingDefinition,
125 const DataLayout &dataLayout)
const {
126 auto readOp = cast<vector::TransferReadOp>(op);
127 Location loc = op->
getLoc();
128 Value
result = reachingDefinition;
129 if (Value mask = readOp.getMask()) {
130 Value padSplat = vector::BroadcastOp::create(
131 builder, loc, readOp.getVectorType(), readOp.getPadding());
132 result = arith::SelectOp::create(builder, loc, mask, reachingDefinition,
136 return DeletionKind::Delete;
141struct TransferWriteOpMemOpModel
142 :
public PromotableMemOpInterface::ExternalModel<TransferWriteOpMemOpModel,
143 vector::TransferWriteOp> {
144 bool loadsFrom(Operation *op,
const MemorySlot &slot)
const {
return false; }
146 bool storesTo(Operation *op,
const MemorySlot &slot)
const {
147 return cast<vector::TransferWriteOp>(op).getBase() == slot.
ptr;
152 Value getStored(Operation *op,
const MemorySlot &slot, OpBuilder &builder,
153 Value reachingDef,
const DataLayout &dataLayout)
const {
154 auto writeOp = cast<vector::TransferWriteOp>(op);
155 Value stored = writeOp.getValueToStore();
158 if (Value mask = writeOp.getMask())
159 stored = arith::SelectOp::create(builder, op->
getLoc(), mask, stored,
164 bool canUsesBeRemoved(Operation *op,
const MemorySlot &slot,
165 const SmallPtrSetImpl<OpOperand *> &blockingUses,
166 SmallVectorImpl<OpOperand *> &newBlockingUses,
167 const DataLayout &dataLayout)
const {
175 removeBlockingUses(Operation *op,
const MemorySlot &slot,
176 const SmallPtrSetImpl<OpOperand *> &blockingUses,
177 OpBuilder &builder, Value reachingDefinition,
178 const DataLayout &dataLayout)
const {
179 return DeletionKind::Delete;
184PromotableMemOpInterface getWrappedMemOp(
Operation *op) {
185 Operation *maskableOp = cast<vector::MaskOp>(op).getMaskableOp();
188 if (!isa<vector::TransferReadOp, vector::TransferWriteOp>(maskableOp))
190 return cast<PromotableMemOpInterface>(maskableOp);
196struct MaskOpMemOpModel
197 :
public PromotableMemOpInterface::ExternalModel<MaskOpMemOpModel,
199 bool ownsWrappedBlockingUses(Operation *op)
const {
200 return static_cast<bool>(getWrappedMemOp(op));
203 bool loadsFrom(Operation *op,
const MemorySlot &slot)
const {
204 PromotableMemOpInterface memOp = getWrappedMemOp(op);
205 return memOp && memOp.loadsFrom(slot);
208 bool storesTo(Operation *op,
const MemorySlot &slot)
const {
209 PromotableMemOpInterface memOp = getWrappedMemOp(op);
210 return memOp && memOp.storesTo(slot);
213 Value getStored(Operation *op,
const MemorySlot &slot, OpBuilder &builder,
214 Value reachingDef,
const DataLayout &dataLayout)
const {
216 getWrappedMemOp(op).getStored(slot, builder, reachingDef, dataLayout);
217 return arith::SelectOp::create(builder, op->
getLoc(),
218 cast<vector::MaskOp>(op).getMask(), stored,
222 bool canUsesBeRemoved(Operation *op,
const MemorySlot &slot,
223 const SmallPtrSetImpl<OpOperand *> &blockingUses,
224 SmallVectorImpl<OpOperand *> &newBlockingUses,
225 const DataLayout &dataLayout)
const {
226 PromotableMemOpInterface memOp = getWrappedMemOp(op);
228 if (!memOp || cast<VectorTransferOpInterface>(*memOp).getMask())
230 return memOp.canUsesBeRemoved(slot, blockingUses, newBlockingUses,
237 removeBlockingUses(Operation *op,
const MemorySlot &slot,
238 const SmallPtrSetImpl<OpOperand *> &blockingUses,
239 OpBuilder &builder, Value reachingDefinition,
240 const DataLayout &dataLayout)
const {
241 auto maskOp = cast<vector::MaskOp>(op);
242 PromotableMemOpInterface memOp = getWrappedMemOp(op);
243 memOp.removeBlockingUses(slot, blockingUses, builder, reachingDefinition,
245 auto readOp = dyn_cast<vector::TransferReadOp>(*memOp);
247 return DeletionKind::Delete;
249 Location loc = op->
getLoc();
250 Value enabled = maskOp.getMaskBlock()->getTerminator()->getOperand(0);
253 ? maskOp.getPassthru()
254 : vector::BroadcastOp::create(builder, loc, readOp.getVectorType(),
255 readOp.getPadding());
256 Value
result = arith::SelectOp::create(builder, loc, maskOp.getMask(),
258 maskOp->getResult(0).replaceAllUsesWith(
result);
259 return DeletionKind::Delete;
271 TransferReadOp::attachInterface<TransferReadOpMemOpModel>(*ctx);
272 TransferWriteOp::attachInterface<TransferWriteOpMemOpModel>(*ctx);
273 MaskOp::attachInterface<MaskOpMemOpModel>(*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.
MLIRContext is the top-level object for a collection of MLIR operations.
Operation is the basic unit of execution within MLIR.
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...
void replaceAllUsesWith(Value newValue)
Replace all uses of 'this' value with the new value, updating anything in the IR that uses 'this' to ...
void registerMemorySlotInterfacesExternalModels(DialectRegistry ®istry)
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.
Represents a slot in memory.
Type valueType
Type of the value contained in the slot.
Value ptr
Pointer to the memory slot, used by operations to refer to it.