24template <
typename OpTy>
26 return !op.isMasked() && op.getPermutationMap().isMinorIdentity() &&
27 op.isDimInBounds(0) && op.hasPureBufferSemantics() &&
29 .isLastDimUnitStride();
35 if (
auto transferWrite = dyn_cast<vector::TransferWriteOp>(write))
38 return isa<vector::StoreOp>(write);
44 if (
auto transferRead = dyn_cast<vector::TransferReadOp>(read))
47 return isa<vector::LoadOp>(read);
53 vector::CreateMaskOp createMaskOp;
54 SmallVector<int64_t> extractPosition;
61 auto transferRead = dyn_cast<vector::TransferReadOp>(loadOp);
62 if (!transferRead || !transferRead.getMask())
63 return TransferMask{{}, {}};
64 assert(transferRead.getMask().getType().getRank() == 1 &&
69 transferRead.getMask().getDefiningOp<vector::CreateMaskOp>())
70 return TransferMask{maskOp, {}};
77 transferRead.getMask().getDefiningOp<vector::ExtractOp>())
79 extractOp.getSource().getDefiningOp<vector::CreateMaskOp>())
80 if (!extractOp.hasDynamicPosition())
91 FailureOr<TransferMask> transferMask =
getMaskOp(readOp);
92 assert(succeeded(transferMask) &&
"invalid transfer mask");
95 if (!transferMask->createMaskOp)
102 auto clampToDimSize = [&](
Value size) ->
Value {
104 transferMask->createMaskOp.getVectorType().getShape().back();
107 b, loc, std::clamp(*cst,
int64_t(0), dimSize));
110 Value nonNegative = arith::MaxSIOp::create(
b, loc, size, zero);
111 return arith::MinSIOp::create(
b, loc, nonNegative, dim);
115 if (transferMask->extractPosition.empty()) {
116 assert(transferMask->createMaskOp.getNumOperands() == 1 &&
117 "expected single operand");
118 return clampToDimSize(transferMask->createMaskOp.getOperand(0));
125 assert(transferMask->createMaskOp.getVectorType().getRank() -
126 transferMask->extractPosition.size() ==
128 "expected N-D -> (N-1)-D extract");
131 for (
auto [pos, sz] : llvm::zip(transferMask->extractPosition,
132 transferMask->createMaskOp->getOperands())) {
134 arith::CmpIOp::create(
b, loc, arith::CmpIPredicate::slt,
140 cond = arith::AndIOp::create(
b, loc, cmp, cond);
142 return arith::SelectOp::create(
144 clampToDimSize(transferMask->createMaskOp->getOperands().back()),
150 VectorType vecType) {
151 assert(vecType.getRank() == 1 &&
"expected 1-D vector");
152 constexpr int64_t kSupportedCpAsyncAlignmentsInBytes[3] = {4, 8, 16};
155 if (vecType.isScalable())
160 if (memrefType.getElementType() != vecType.getElementType())
167 Type elementType = vecType.getElementType();
169 !llvm::is_contained({8u, 16u, 32u, 64u, 128u},
172 bool supportedCopySize =
false;
173 int64_t numElements = vecType.getNumElements();
174 for (
int64_t alignmentInBytes : kSupportedCpAsyncAlignmentsInBytes) {
175 if (alignmentInBytes * 8 ==
177 supportedCopySize =
true;
181 if (!supportedCopySize)
193 llvm::SmallSetVector<Operation *, 16> copyToSharedMem;
201 if (cast<VectorType>(vectorVal.
getType()).getRank() != 1)
204 if (!nvgpu::NVGPUDialect::hasSharedMemoryAddressSpace(
205 cast<MemRefType>(storeBase.
getType())))
214 if (nvgpu::NVGPUDialect::hasSharedMemoryAddressSpace(
215 cast<MemRefType>(loadBase.
getType())))
219 if (
auto transferRead = dyn_cast<vector::TransferReadOp>(readOp)) {
220 if (
Value mask = transferRead.getMask()) {
231 VectorType vecType = cast<VectorType>(vectorVal.
getType());
239 copyToSharedMem.insert(writeOp);
243 while (!copyToSharedMem.empty()) {
246 Operation *writeOp = *copyToSharedMem.begin();
247 copyToSharedMem.
remove(writeOp);
248 group.push_back(writeOp);
252 while ((nextNode = nextNode->getNextNode())) {
254 auto memInterface = dyn_cast<MemoryEffectOpInterface>(nextNode);
255 if (memInterface && memInterface.hasNoEffect() &&
259 if (isa<vector::TransferReadOp, vector::LoadOp>(nextNode)) {
262 if (!nvgpu::NVGPUDialect::hasSharedMemoryAddressSpace(
263 cast<MemRefType>(memrefOperand.
getType()))) {
267 if (copyToSharedMem.count(nextNode)) {
269 copyToSharedMem.remove(nextNode);
270 group.push_back(nextNode);
282 auto vectorType = cast<VectorType>(vectorVal.
getType());
283 int64_t numElements = vectorType.getNumElements();
287 Value numReadElements =
289 auto dstMemref = cast<MemRefType>(storeBase.
getType());
291 (dstMemref.getElementTypeBitWidth() * numElements) / 8;
293 Value token = nvgpu::DeviceAsyncCopyOp::create(
294 rewriter, writeOp->
getLoc(),
295 nvgpu::DeviceAsyncTokenType::get(op->
getContext()),
301 bypassL1 && sizeInBytes == 16 ? rewriter.
getUnitAttr()
303 tokens.push_back(token);
307 Value groupToken = nvgpu::DeviceAsyncCreateGroupOp::create(
309 nvgpu::DeviceAsyncTokenType::get(op->
getContext()), tokens);
310 nvgpu::DeviceAsyncWaitOp::create(rewriter, op->
getLoc(), groupToken,
static bool isContiguousStore(Operation *write)
Return "true" if the given op is a contiguous and suitable vector.transfer_write or vector....
static bool isContiguousXferOp(OpTy op)
Return "true" if the given vector transfer op is contiguous and suitable for replacement with an asyn...
static FailureOr< TransferMask > getMaskOp(Operation *loadOp)
If the given vector load op has a mask that is defined by vector.create_mask, return that op.
static bool resultsInSupportedAsyncCopy(MemRefType memrefType, VectorType vecType)
Return "true" if the conversion to async copy is supported by "async copy".
static bool isContiguousRead(Operation *read)
Return "true" if the given op is a contiguous and suitable vector.transfer_read or vector....
static Value buildNumReadElements(OpBuilder &b, Location loc, Operation *readOp)
Build an SSA value that represents the number of read elements.
IntegerAttr getIndexAttr(int64_t value)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
This class helps build Operations.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
This trait indicates that the memory effects of an operation includes the effects of operations neste...
Operation is the basic unit of execution within MLIR.
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Location getLoc()
The source location the operation was defined or derived from.
void remove()
Remove the operation from its parent block, but don't delete it.
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
MLIRContext * getContext()
Return the context this operation is associated with.
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
Value getMemrefOperand(Operation *op)
Get the memref that is loaded from/stored into by the given load/store operation.
Value getValueStored(Operation *op)
Get the value that is stored by the given store operation.
Operation::operand_range getIndices(Operation *op)
Get the indices that the given load/store operation is operating on.
void createAsyncGroups(RewriterBase &rewriter, Operation *op, bool bypassL1)
Convert global->shared vector transfers to async device copies.
Include the generated interface declarations.
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.