19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/iterator_range.h"
21#include "llvm/IR/IRBuilder.h"
22#include "llvm/IR/IntrinsicsNVPTX.h"
23#include "llvm/Support/FormatVariadic.h"
29#define REDUX_F32_ID_IMPL(op, abs, hasNaN) \
30 hasNaN ? llvm::Intrinsic::nvvm_redux_sync_f##op##abs##_NaN \
31 : llvm::Intrinsic::nvvm_redux_sync_f##op##abs
33#define GET_REDUX_F32_ID(op, hasAbs, hasNaN) \
34 hasAbs ? REDUX_F32_ID_IMPL(op, _abs, hasNaN) : REDUX_F32_ID_IMPL(op, , hasNaN)
38 bool hasAbs,
bool hasNaN) {
40 case NVVM::ReduxKind::ADD:
41 return llvm::Intrinsic::nvvm_redux_sync_add;
42 case NVVM::ReduxKind::UMAX:
43 return llvm::Intrinsic::nvvm_redux_sync_umax;
44 case NVVM::ReduxKind::UMIN:
45 return llvm::Intrinsic::nvvm_redux_sync_umin;
46 case NVVM::ReduxKind::AND:
47 return llvm::Intrinsic::nvvm_redux_sync_and;
48 case NVVM::ReduxKind::OR:
49 return llvm::Intrinsic::nvvm_redux_sync_or;
50 case NVVM::ReduxKind::XOR:
51 return llvm::Intrinsic::nvvm_redux_sync_xor;
52 case NVVM::ReduxKind::MAX:
53 return llvm::Intrinsic::nvvm_redux_sync_max;
54 case NVVM::ReduxKind::MIN:
55 return llvm::Intrinsic::nvvm_redux_sync_min;
56 case NVVM::ReduxKind::FMIN:
58 case NVVM::ReduxKind::FMAX:
61 llvm_unreachable(
"unknown redux kind");
69 resultType = cast<llvm::StructType>(resultType)->getElementType(0);
71 case NVVM::ShflKind::bfly:
72 return resultType->isFloatTy()
73 ? llvm::Intrinsic::nvvm_shfl_sync_bfly_f32p
74 : llvm::Intrinsic::nvvm_shfl_sync_bfly_i32p;
75 case NVVM::ShflKind::up:
76 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_up_f32p
77 : llvm::Intrinsic::nvvm_shfl_sync_up_i32p;
78 case NVVM::ShflKind::down:
79 return resultType->isFloatTy()
80 ? llvm::Intrinsic::nvvm_shfl_sync_down_f32p
81 : llvm::Intrinsic::nvvm_shfl_sync_down_i32p;
82 case NVVM::ShflKind::idx:
83 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_idx_f32p
84 : llvm::Intrinsic::nvvm_shfl_sync_idx_i32p;
88 case NVVM::ShflKind::bfly:
89 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_bfly_f32
90 : llvm::Intrinsic::nvvm_shfl_sync_bfly_i32;
91 case NVVM::ShflKind::up:
92 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_up_f32
93 : llvm::Intrinsic::nvvm_shfl_sync_up_i32;
94 case NVVM::ShflKind::down:
95 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_down_f32
96 : llvm::Intrinsic::nvvm_shfl_sync_down_i32;
97 case NVVM::ShflKind::idx:
98 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_idx_f32
99 : llvm::Intrinsic::nvvm_shfl_sync_idx_i32;
102 llvm_unreachable(
"unknown shuffle kind");
106 NVVM::MatchSyncKind kind) {
108 case NVVM::MatchSyncKind::any:
109 return valType.
isInteger(32) ? llvm::Intrinsic::nvvm_match_any_sync_i32
110 : llvm::Intrinsic::nvvm_match_any_sync_i64;
111 case NVVM::MatchSyncKind::all:
115 return valType.
isInteger(32) ? llvm::Intrinsic::nvvm_match_all_sync_i32p
116 : llvm::Intrinsic::nvvm_match_all_sync_i64p;
118 llvm_unreachable(
"unsupported match sync kind");
123 case NVVM::VoteSyncKind::any:
124 return llvm::Intrinsic::nvvm_vote_any_sync;
125 case NVVM::VoteSyncKind::all:
126 return llvm::Intrinsic::nvvm_vote_all_sync;
127 case NVVM::VoteSyncKind::ballot:
128 return llvm::Intrinsic::nvvm_vote_ballot_sync;
129 case NVVM::VoteSyncKind::uni:
130 return llvm::Intrinsic::nvvm_vote_uni_sync;
132 llvm_unreachable(
"unsupported vote kind");
135static llvm::Intrinsic::ID
137 NVVM::LdStMatrixShapeAttr
shape,
138 NVVM::LdStMatrixEltType eltType) {
142 return (layout == NVVM::MMALayout::row)
143 ? llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x1_b16
145 nvvm_ldmatrix_sync_aligned_m8n8_x1_trans_b16;
147 return (layout == NVVM::MMALayout::row)
148 ? llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x2_b16
150 nvvm_ldmatrix_sync_aligned_m8n8_x2_trans_b16;
152 return (layout == NVVM::MMALayout::row)
153 ? llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x4_b16
155 nvvm_ldmatrix_sync_aligned_m8n8_x4_trans_b16;
157 }
else if (
shape.getM() == 8 &&
shape.getN() == 16) {
158 if (eltType == NVVM::LdStMatrixEltType::B8X16_B6X16_P32) {
161 return llvm::Intrinsic::
162 nvvm_ldmatrix_sync_aligned_m8n16_x1_b8x16_b6x16_p32;
164 return llvm::Intrinsic::
165 nvvm_ldmatrix_sync_aligned_m8n16_x2_b8x16_b6x16_p32;
167 return llvm::Intrinsic::
168 nvvm_ldmatrix_sync_aligned_m8n16_x4_b8x16_b6x16_p32;
170 }
else if (eltType == NVVM::LdStMatrixEltType::B8X16_B4X16_P64) {
173 return llvm::Intrinsic::
174 nvvm_ldmatrix_sync_aligned_m8n16_x1_b8x16_b4x16_p64;
176 return llvm::Intrinsic::
177 nvvm_ldmatrix_sync_aligned_m8n16_x2_b8x16_b4x16_p64;
179 return llvm::Intrinsic::
180 nvvm_ldmatrix_sync_aligned_m8n16_x4_b8x16_b4x16_p64;
183 }
else if (
shape.getM() == 16 &&
shape.getN() == 16) {
184 if (eltType == NVVM::LdStMatrixEltType::B8) {
187 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m16n16_x1_trans_b8;
189 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m16n16_x2_trans_b8;
191 }
else if (eltType == NVVM::LdStMatrixEltType::B8X16_B6X16_P32) {
194 return llvm::Intrinsic::
195 nvvm_ldmatrix_sync_aligned_m16n16_x1_trans_b8x16_b6x16_p32;
197 return llvm::Intrinsic::
198 nvvm_ldmatrix_sync_aligned_m16n16_x2_trans_b8x16_b6x16_p32;
200 }
else if (eltType == NVVM::LdStMatrixEltType::B8X16_B4X16_P64) {
203 return llvm::Intrinsic::
204 nvvm_ldmatrix_sync_aligned_m16n16_x1_trans_b8x16_b4x16_p64;
206 return llvm::Intrinsic::
207 nvvm_ldmatrix_sync_aligned_m16n16_x2_trans_b8x16_b4x16_p64;
211 llvm_unreachable(
"unknown ldmatrix kind");
215static llvm::Intrinsic::ID
217 NVVM::LdStMatrixShapeAttr
shape,
218 NVVM::LdStMatrixEltType eltType) {
222 return (layout == NVVM::MMALayout::row)
223 ? llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x1_b16
225 nvvm_stmatrix_sync_aligned_m8n8_x1_trans_b16;
227 return (layout == NVVM::MMALayout::row)
228 ? llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x2_b16
230 nvvm_stmatrix_sync_aligned_m8n8_x2_trans_b16;
232 return (layout == NVVM::MMALayout::row)
233 ? llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x4_b16
235 nvvm_stmatrix_sync_aligned_m8n8_x4_trans_b16;
237 }
else if (
shape.getM() == 16 &&
shape.getN() == 8) {
240 return llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m16n8_x1_trans_b8;
242 return llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m16n8_x2_trans_b8;
244 return llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m16n8_x4_trans_b8;
247 llvm_unreachable(
"unknown stmatrix kind");
251static llvm::Intrinsic::ID
254 static_cast<unsigned>(NVVM::NVVMMemorySpace::Shared);
256 : llvm::Intrinsic::nvvm_st_bulk;
260 NVVM::ProxyKind toProxy,
261 NVVM::MemScopeKind scope,
263 if (fromProxy == NVVM::ProxyKind::GENERIC &&
264 toProxy == NVVM::ProxyKind::TENSORMAP) {
266 case NVVM::MemScopeKind::CTA: {
268 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_release_cta;
269 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_acquire_cta;
271 case NVVM::MemScopeKind::CLUSTER: {
273 return llvm::Intrinsic::
274 nvvm_fence_proxy_tensormap_generic_release_cluster;
275 return llvm::Intrinsic::
276 nvvm_fence_proxy_tensormap_generic_acquire_cluster;
278 case NVVM::MemScopeKind::GPU: {
280 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_release_gpu;
281 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_acquire_gpu;
283 case NVVM::MemScopeKind::SYS: {
285 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_release_sys;
286 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_acquire_sys;
289 llvm_unreachable(
"Unknown scope for uni-directional fence.proxy operation");
291 llvm_unreachable(
"Unsupported proxy kinds");
296 case NVVM::MemScopeKind::CTA:
297 return llvm::Intrinsic::nvvm_membar_cta;
298 case NVVM::MemScopeKind::CLUSTER:
299 return llvm::Intrinsic::nvvm_fence_sc_cluster;
300 case NVVM::MemScopeKind::GPU:
301 return llvm::Intrinsic::nvvm_membar_gl;
302 case NVVM::MemScopeKind::SYS:
303 return llvm::Intrinsic::nvvm_membar_sys;
305 llvm_unreachable(
"Unknown scope for memory barrier");
308#define TCGEN05LD(SHAPE, NUM) llvm::Intrinsic::nvvm_tcgen05_ld_##SHAPE##_##NUM
310static llvm::Intrinsic::ID
312 llvm::Intrinsic::ID Shape16x64b[] = {
318 llvm::Intrinsic::ID Shape16x128b[] = {
324 llvm::Intrinsic::ID Shape16x256b[] = {
329 llvm::Intrinsic::ID Shape16x32bx2[] = {
336 llvm::Intrinsic::ID Shape32x32b[] = {
344 unsigned Idx = std::log2(num);
347 case NVVM::Tcgen05LdStShape::SHAPE_16X64B:
348 return Shape16x64b[Idx];
349 case NVVM::Tcgen05LdStShape::SHAPE_16X128B:
350 return Shape16x128b[Idx - 1];
351 case NVVM::Tcgen05LdStShape::SHAPE_16X256B:
352 return Shape16x256b[Idx - 2];
353 case NVVM::Tcgen05LdStShape::SHAPE_32X32B:
354 return Shape32x32b[Idx];
355 case NVVM::Tcgen05LdStShape::SHAPE_16X32BX2:
356 return Shape16x32bx2[Idx];
358 llvm_unreachable(
"unhandled tcgen05.ld lowering");
361#define TCGEN05ST(SHAPE, NUM) llvm::Intrinsic::nvvm_tcgen05_st_##SHAPE##_##NUM
363static llvm::Intrinsic::ID
365 llvm::Intrinsic::ID Shape16x64b[] = {
371 llvm::Intrinsic::ID Shape16x128b[] = {
377 llvm::Intrinsic::ID Shape16x256b[] = {
382 llvm::Intrinsic::ID Shape16x32bx2[] = {
389 llvm::Intrinsic::ID Shape32x32b[] = {
397 unsigned Idx = std::log2(num);
400 case NVVM::Tcgen05LdStShape::SHAPE_16X64B:
401 return Shape16x64b[Idx];
402 case NVVM::Tcgen05LdStShape::SHAPE_16X128B:
403 return Shape16x128b[Idx - 1];
404 case NVVM::Tcgen05LdStShape::SHAPE_16X256B:
405 return Shape16x256b[Idx - 2];
406 case NVVM::Tcgen05LdStShape::SHAPE_32X32B:
407 return Shape32x32b[Idx];
408 case NVVM::Tcgen05LdStShape::SHAPE_16X32BX2:
409 return Shape16x32bx2[Idx];
411 llvm_unreachable(
"unhandled tcgen05.st lowering");
417class NVVMDialectLLVMIRTranslationInterface
425 convertOperation(Operation *op, llvm::IRBuilderBase &builder,
426 LLVM::ModuleTranslation &moduleTranslation)
const final {
427 Operation &opInst = *op;
428#include "mlir/Dialect/LLVMIR/NVVMConversions.inc"
435 amendOperation(Operation *op, ArrayRef<llvm::Instruction *> instructions,
436 NamedAttribute attribute,
437 LLVM::ModuleTranslation &moduleTranslation)
const final {
438 auto func = dyn_cast<LLVM::LLVMFuncOp>(op);
441 llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
443 if (attribute.getName() == NVVM::NVVMDialect::getMaxntidAttrName()) {
444 if (!isa<DenseI32ArrayAttr>(attribute.getValue()))
446 auto values = cast<DenseI32ArrayAttr>(attribute.getValue());
447 const std::string attr = llvm::formatv(
448 "{0:$[,]}", llvm::make_range(values.asArrayRef().begin(),
449 values.asArrayRef().end()));
450 llvmFunc->addFnAttr(
"nvvm.maxntid", attr);
451 }
else if (attribute.getName() == NVVM::NVVMDialect::getReqntidAttrName()) {
452 if (!isa<DenseI32ArrayAttr>(attribute.getValue()))
454 auto values = cast<DenseI32ArrayAttr>(attribute.getValue());
455 const std::string attr = llvm::formatv(
456 "{0:$[,]}", llvm::make_range(values.asArrayRef().begin(),
457 values.asArrayRef().end()));
458 llvmFunc->addFnAttr(
"nvvm.reqntid", attr);
459 }
else if (attribute.getName() ==
460 NVVM::NVVMDialect::getClusterDimAttrName()) {
461 if (!isa<DenseI32ArrayAttr>(attribute.getValue()))
463 auto values = cast<DenseI32ArrayAttr>(attribute.getValue());
464 const std::string attr = llvm::formatv(
465 "{0:$[,]}", llvm::make_range(values.asArrayRef().begin(),
466 values.asArrayRef().end()));
467 llvmFunc->addFnAttr(
"nvvm.cluster_dim", attr);
468 }
else if (attribute.getName() ==
469 NVVM::NVVMDialect::getClusterMaxBlocksAttrName()) {
470 auto value = dyn_cast<IntegerAttr>(attribute.getValue());
471 llvmFunc->addFnAttr(
"nvvm.maxclusterrank", llvm::utostr(value.getInt()));
472 }
else if (attribute.getName() ==
473 NVVM::NVVMDialect::getMinctasmAttrName()) {
474 auto value = dyn_cast<IntegerAttr>(attribute.getValue());
475 llvmFunc->addFnAttr(
"nvvm.minctasm", llvm::utostr(value.getInt()));
476 }
else if (attribute.getName() == NVVM::NVVMDialect::getMaxnregAttrName()) {
477 auto value = dyn_cast<IntegerAttr>(attribute.getValue());
478 llvmFunc->addFnAttr(
"nvvm.maxnreg", llvm::utostr(value.getInt()));
479 }
else if (attribute.getName() ==
480 NVVM::NVVMDialect::getKernelFuncAttrName()) {
481 llvmFunc->setCallingConv(llvm::CallingConv::PTX_Kernel);
482 }
else if (attribute.getName() ==
483 NVVM::NVVMDialect::getBlocksAreClustersAttrName()) {
484 llvmFunc->addFnAttr(
"nvvm.blocksareclusters");
492 LLVM::ModuleTranslation &moduleTranslation)
const final {
494 llvm::LLVMContext &llvmContext = moduleTranslation.getLLVMContext();
495 llvm::Function *llvmFunc =
496 moduleTranslation.lookupFunction(funcOp.getName());
498 if (attribute.getName() == NVVM::NVVMDialect::getGridConstantAttrName()) {
499 llvmFunc->addParamAttr(
500 argIdx, llvm::Attribute::get(llvmContext,
"nvvm.grid_constant"));
508 registry.
insert<NVVM::NVVMDialect>();
510 dialect->addInterfaces<NVVMDialectLLVMIRTranslationInterface>();
static LogicalResult convertParameterAttr(llvm::AttrBuilder &attrBuilder, llvm::Attribute::AttrKind llvmKind, NamedAttribute namedAttr, ModuleTranslation &moduleTranslation, Location loc)
static llvm::Intrinsic::ID getLdMatrixIntrinsicId(NVVM::MMALayout layout, int32_t num, NVVM::LdStMatrixShapeAttr shape, NVVM::LdStMatrixEltType eltType)
#define GET_REDUX_F32_ID(op, hasAbs, hasNaN)
static llvm::Intrinsic::ID getStMatrixIntrinsicId(NVVM::MMALayout layout, int32_t num, NVVM::LdStMatrixShapeAttr shape, NVVM::LdStMatrixEltType eltType)
Return the intrinsic ID associated with stmatrix for the given paramters.
static llvm::Intrinsic::ID getTcgen05StIntrinsicID(mlir::NVVM::Tcgen05LdStShape shape, uint32_t num)
static llvm::Intrinsic::ID getTcgen05LdIntrinsicID(mlir::NVVM::Tcgen05LdStShape shape, uint32_t num)
static unsigned getMembarIntrinsicID(NVVM::MemScopeKind scope)
static unsigned getUnidirectionalFenceProxyID(NVVM::ProxyKind fromProxy, NVVM::ProxyKind toProxy, NVVM::MemScopeKind scope, bool isRelease)
#define TCGEN05ST(SHAPE, NUM)
static llvm::Intrinsic::ID getReduxIntrinsicId(llvm::Type *resultType, NVVM::ReduxKind kind, bool hasAbs, bool hasNaN)
#define TCGEN05LD(SHAPE, NUM)
static llvm::Intrinsic::ID getShflIntrinsicId(llvm::Type *resultType, NVVM::ShflKind kind, bool withPredicate)
static llvm::Intrinsic::ID getVoteSyncIntrinsicId(NVVM::VoteSyncKind kind)
static llvm::Intrinsic::ID getMatchSyncIntrinsicId(Type valType, NVVM::MatchSyncKind kind)
static llvm::Intrinsic::ID getStBulkIntrinsicId(LLVM::LLVMPointerType addrType)
Return the intrinsic ID associated with st.bulk for the given address type.
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
static bool isSharedMemory(MemRefType type)
Return true if this is a shared memory memref type.
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.
Base class for dialect interfaces providing translation to LLVM IR.
LLVMTranslationDialectInterface(Dialect *dialect)
MLIRContext is the top-level object for a collection of MLIR operations.
void appendDialectRegistry(const DialectRegistry ®istry)
Append the contents of the given dialect registry to the registry associated with this context.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isInteger() const
Return true if this is an integer type (with the specified width).
llvm::CallInst * createIntrinsicCall(llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, ArrayRef< llvm::Value * > args={}, ArrayRef< llvm::Type * > tys={})
Creates a call to an LLVM IR intrinsic function with the given arguments.
Include the generated interface declarations.
void registerNVVMDialectTranslation(DialectRegistry ®istry)
Register the NVVM dialect and the translation from it to the LLVM IR in the given registry;.