22#include "llvm/ADT/STLExtras.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/IntrinsicsNVPTX.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/NVPTXAddrSpace.h"
39LogicalResult BulkStoreOp::verify() {
40 if (getInitVal() != 0)
41 return emitOpError(
"only 0 is supported for initVal, got ") << getInitVal();
45LogicalResult AsyncStoreGlobalOp::verify() {
46 NVVM::MemScopeKind scope = getScope();
47 bool isMmio = getMmio();
48 bool isMultimem = getMultimem();
50 if (scope != MemScopeKind::SYS && scope != MemScopeKind::GPU)
51 return emitOpError(
"scope must be either SYS or GPU");
53 if (isMmio && scope != MemScopeKind::SYS)
54 return emitOpError(
"mmio is only supported for SYS scope");
56 if (isMmio && isMultimem)
57 return emitOpError(
"multimem is not supported with mmio");
62LogicalResult NVVM::PrefetchOp::verify() {
63 using MemSpace = NVVM::NVVMMemorySpace;
64 using CacheLevel = NVVM::PrefetchCacheLevel;
66 unsigned addressSpace =
67 llvm::cast<LLVM::LLVMPointerType>(getAddr().
getType()).getAddressSpace();
68 std::optional<NVVM::CacheEvictionPriority> evictPriority = getEvictPriority();
69 std::optional<NVVM::PrefetchCacheLevel> cacheLevel = getCacheLevel();
71 if (getTensormap() && cacheLevel)
72 return emitOpError(
"cannot specify both tensormap and cache level");
75 if (addressSpace != MemSpace::Generic &&
76 addressSpace != MemSpace::Constant) {
78 "prefetch tensormap requires a generic or constant pointer");
83 "prefetch tensormap does not support eviction priority");
86 if (getInParamSpace() && addressSpace != MemSpace::Generic) {
88 "in_param_space can only be specified for a generic pointer");
91 }
else if (cacheLevel) {
92 if (addressSpace != MemSpace::Generic && addressSpace != MemSpace::Global &&
93 addressSpace != MemSpace::Local) {
94 return emitOpError(
"prefetch to cache level requires a generic, global, "
99 if (*cacheLevel != CacheLevel::L1) {
101 "unsupported cache level, the only supported uniform "
102 "cache level is L1");
105 if (addressSpace != MemSpace::Generic) {
107 "prefetch to uniform cache requires a generic pointer");
112 if (*cacheLevel != CacheLevel::L2)
114 "cache eviction priority supported only for cache level L2");
116 if (addressSpace != MemSpace::Global)
117 return emitOpError(
"cache eviction priority requires a global pointer");
119 if (*evictPriority != NVVM::CacheEvictionPriority::EvictNormal &&
120 *evictPriority != NVVM::CacheEvictionPriority::EvictLast)
122 "unsupported cache eviction priority, only evict_last and "
123 "evict_normal are supported");
127 return emitOpError(
"predicate supported only on prefetch tensormap");
131 "requires specification of either cache level or tensormap");
137LogicalResult ShflOp::verify() {
138 auto returnStructType = llvm::dyn_cast<LLVM::LLVMStructType>(
getType());
140 auto verifyTypeError = [&](Twine desc,
Type expectedType,
141 Type actualType) -> LogicalResult {
142 return emitOpError(
"expected " + desc +
" to be of type ")
143 << expectedType <<
" but got " << actualType <<
" instead";
146 if (returnStructType) {
147 if (!getReturnValueAndIsValid())
148 return emitOpError(
"\"return_value_and_is_valid\" attribute must be "
149 "specified when the return type is a struct type");
151 if (returnStructType.getBody().size() != 2)
152 return emitOpError(
"expected return type to be a two-element struct");
155 auto resultType = returnStruct[0];
156 if (resultType != getVal().
getType())
157 return verifyTypeError(
"first element in the returned struct",
158 getVal().
getType(), resultType);
160 auto predicateType = returnStruct[1];
161 if (!predicateType.isInteger(1))
162 return verifyTypeError(
"second element in the returned struct",
166 if (getReturnValueAndIsValid())
167 return emitOpError(
"expected return type to be a two-element struct");
170 return verifyTypeError(
"return type", getVal().
getType(),
getType());
176ShflOp::inferReturnTypes(
MLIRContext *context, std::optional<Location> location,
177 ShflOp::Adaptor adaptor,
179 Type valType = adaptor.getVal().getType();
180 if (adaptor.getReturnValueAndIsValid())
181 inferredReturnTypes.push_back(LLVM::LLVMStructType::getLiteral(
182 context, {valType, IntegerType::get(context, 1)}));
184 inferredReturnTypes.push_back(valType);
188LogicalResult PermuteOp::verify() {
189 using Mode = NVVM::PermuteMode;
190 bool hasHi =
static_cast<bool>(getHi());
197 return emitError(
"mode '") << getMode() <<
"' requires 'hi' operand.";
205 << getMode() <<
"' does not accept 'hi' operand.";
219 auto thisOp = cast<NVVM::AsyncStoreGlobalOp>(op);
220 mlir::NVVM::MemScopeKind scope = thisOp.getScope();
221 bool isMmio = thisOp.getMmio();
223 llvm::Value *addr = mt.
lookupValue(thisOp.getAddr());
224 llvm::Value *value = mt.
lookupValue(thisOp.getValue());
225 llvm::Value *isMultimem = builder.getInt1(thisOp.getMultimem());
227 if (scope == MemScopeKind::SYS) {
228 return isMmio ?
IDArgPair(llvm::Intrinsic::nvvm_st_async_mmio_sys,
231 {addr, value, isMultimem});
232 }
else if (scope == MemScopeKind::GPU) {
233 return IDArgPair(llvm::Intrinsic::nvvm_st_async_gpu,
234 {addr, value, isMultimem});
236 llvm_unreachable(
"unsupported scope for AsyncStoreGlobalOp");
240 llvm::IRBuilderBase &builder) {
241 return builder.CreateAddrSpaceCast(
242 addr, builder.getPtrTy(llvm::NVPTXAS::ADDRESS_SPACE_ENTRY_PARAM));
247 llvm::IRBuilderBase &builder) {
248 auto thisOp = cast<NVVM::PrefetchOp>(op);
249 using MemSpace = NVVM::NVVMMemorySpace;
250 using CacheLevel = NVVM::PrefetchCacheLevel;
252 std::optional<NVVM::PrefetchCacheLevel> cacheLevel = thisOp.getCacheLevel();
253 std::optional<NVVM::CacheEvictionPriority> evictPriority =
254 thisOp.getEvictPriority();
255 unsigned addressSpace =
256 llvm::cast<LLVM::LLVMPointerType>(thisOp.getAddr().getType())
260 llvm::Value *addr = mt.
lookupValue(thisOp.getAddr());
264 if (thisOp.getTensormap())
265 return {llvm::Intrinsic::nvvm_prefetch_tensormap, args};
267 assert(cacheLevel &&
"expected cache level for non-tensormap prefetch");
269 if (thisOp.getUniform() && *cacheLevel == CacheLevel::L1)
270 return {llvm::Intrinsic::nvvm_prefetchu_L1, args};
272 if (evictPriority && *cacheLevel == CacheLevel::L2) {
273 switch (*evictPriority) {
274 case NVVM::CacheEvictionPriority::EvictLast:
275 return {llvm::Intrinsic::nvvm_prefetch_global_L2_evict_last, args};
276 case NVVM::CacheEvictionPriority::EvictNormal:
277 return {llvm::Intrinsic::nvvm_prefetch_global_L2_evict_normal, args};
279 llvm_unreachable(
"Invalid cache eviction priority");
283 switch (
static_cast<MemSpace
>(addressSpace)) {
284 case MemSpace::Generic:
285 return *cacheLevel == CacheLevel::L1
287 :
NVVM::
IDArgPair({llvm::Intrinsic::nvvm_prefetch_L2, args});
288 case MemSpace::Global:
289 return *cacheLevel == CacheLevel::L1
291 {llvm::Intrinsic::nvvm_prefetch_global_L1, args})
293 {llvm::Intrinsic::nvvm_prefetch_global_L2, args});
294 case MemSpace::Local:
295 return *cacheLevel == CacheLevel::L1
297 {llvm::Intrinsic::nvvm_prefetch_local_L1, args})
299 {llvm::Intrinsic::nvvm_prefetch_local_L2, args});
301 llvm_unreachable(
"Invalid pointer address space");
307 llvm::IRBuilderBase &builder) {
308 auto thisOp = cast<NVVM::PermuteOp>(op);
309 NVVM::PermuteMode mode = thisOp.getMode();
311 static constexpr llvm::Intrinsic::ID IDs[] = {
312 llvm::Intrinsic::nvvm_prmt, llvm::Intrinsic::nvvm_prmt_f4e,
313 llvm::Intrinsic::nvvm_prmt_b4e, llvm::Intrinsic::nvvm_prmt_rc8,
314 llvm::Intrinsic::nvvm_prmt_ecl, llvm::Intrinsic::nvvm_prmt_ecr,
315 llvm::Intrinsic::nvvm_prmt_rc16};
317 unsigned modeIndex =
static_cast<unsigned>(mode);
318 llvm::SmallVector<llvm::Value *> args;
325 args.push_back(mt.
lookupValue(thisOp.getSelector()));
327 return {IDs[modeIndex], args};
337 if (
auto vecTy = dyn_cast<VectorType>(type))
338 return vecTy.getNumElements();
344 return llvm::isPowerOf2_32(repFactor) && repFactor <= 64;
347LogicalResult SparseCompressOp::verify() {
348 auto dataTy = cast<VectorType>(getData().
getType());
349 auto compressedTy = cast<VectorType>(getCompressedData().
getType());
350 if (dataTy.getElementType() != compressedTy.getElementType())
352 "expects data and compressed data to have the same element type");
355 unsigned elemSize = dataTy.getElementTypeBitWidth();
356 unsigned dataBits = dataTy.getNumElements() * elemSize;
357 unsigned repFactor = dataBits / 64;
359 return emitOpError(
"expects data to occupy 2 * repFactor 32-bit "
360 "registers, where repFactor is a power of two up to 64");
363 if (2 * compressedTy.getNumElements() != dataTy.getNumElements())
365 "expects compressed data to have half as many elements as data");
367 unsigned numMetadataRegs =
368 llvm::divideCeil(repFactor * getIdxSize(), elemSize);
370 return emitOpError(
"expects metadata to occupy ")
371 << numMetadataRegs <<
" 32-bit register(s)";
376LogicalResult SparseDecompressOp::verify() {
377 auto dataTy = cast<VectorType>(getData().
getType());
378 auto compressedTy = cast<VectorType>(getCompressedData().
getType());
379 if (dataTy.getElementType() != compressedTy.getElementType())
381 "expects data and compressed data to have the same element type");
385 unsigned idxSize = getIdxSize();
386 unsigned numTgt = getNumTgt();
387 unsigned numData = dataTy.getNumElements();
388 unsigned numCompressed = compressedTy.getNumElements();
389 unsigned repFactor = numData / numTgt;
391 return emitOpError(
"expects data to have repFactor * num_tgt elements, "
392 "where repFactor is a power of two up to 64");
394 unsigned numSrc = numCompressed / repFactor;
395 if (numCompressed % repFactor != 0 ||
396 !llvm::is_contained({1u, 2u, 4u}, numSrc) || numSrc >= numTgt)
397 return emitOpError(
"unsupported sparsity factor: compressed data has ")
398 << numCompressed <<
" elements for " << repFactor <<
" group(s) of "
399 << numTgt <<
" elements";
401 unsigned elemSize = dataTy.getElementTypeBitWidth();
402 unsigned dataBits = numData * elemSize;
403 if (dataBits < 32 || dataBits > 4096)
404 return emitOpError(
"expects data to be between 32 and 4096 bits");
406 if (numSrc * elemSize > 32)
407 return emitOpError(
"expects at most 32 bits of compressed data per group");
409 if (idxSize == 2 && numTgt > 4)
410 return emitOpError(
"expects num_tgt to be at most 4 if idx_size is 2");
412 unsigned numMetadataRegs = llvm::divideCeil(numSrc * idxSize * repFactor, 32);
414 return emitOpError(
"expects metadata to occupy ")
415 << numMetadataRegs <<
" 32-bit register(s)";
420#define GET_OP_CLASSES
421#include "mlir/Dialect/LLVMIR/NVVMDataMovementOps.cpp.inc"
static bool isValidSPRepFactor(unsigned repFactor)
static unsigned getNumSPRegisters(Type type)
Implementation class for module translation.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
MLIRContext is the top-level object for a collection of MLIR operations.
Operation is the basic unit of execution within MLIR.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
The OpAsmOpInterface, see OpAsmInterface.td for more details.
llvm::Value * getParamCastedAddr(llvm::Value *addr, llvm::IRBuilderBase &builder)
Returns addr cast to the kernel parameter address space.
std::pair< llvm::Intrinsic::ID, llvm::SmallVector< llvm::Value * > > IDArgPair
A pair type of LLVM's Intrinsic ID and args (which are llvm values).
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.