20#include "llvm/ADT/StringExtras.h"
21#include "llvm/ADT/iterator_range.h"
22#include "llvm/IR/IRBuilder.h"
23#include "llvm/IR/IntrinsicsNVPTX.h"
24#include "llvm/Support/FormatVariadic.h"
25#include "llvm/Support/NVVMAttributes.h"
32#define REDUX_F32_ID_IMPL(op, abs, hasNaN) \
33 hasNaN ? llvm::Intrinsic::nvvm_redux_sync_f##op##abs##_NaN \
34 : llvm::Intrinsic::nvvm_redux_sync_f##op##abs
36#define GET_REDUX_F32_ID(op, hasAbs, hasNaN) \
37 hasAbs ? REDUX_F32_ID_IMPL(op, _abs, hasNaN) : REDUX_F32_ID_IMPL(op, , hasNaN)
40 NVVM::ReductionKind kind,
41 bool hasAbs,
bool hasNaN) {
43 case NVVM::ReductionKind::ADD:
44 return llvm::Intrinsic::nvvm_redux_sync_add;
45 case NVVM::ReductionKind::UMAX:
46 return llvm::Intrinsic::nvvm_redux_sync_umax;
47 case NVVM::ReductionKind::UMIN:
48 return llvm::Intrinsic::nvvm_redux_sync_umin;
49 case NVVM::ReductionKind::AND:
50 return llvm::Intrinsic::nvvm_redux_sync_and;
51 case NVVM::ReductionKind::OR:
52 return llvm::Intrinsic::nvvm_redux_sync_or;
53 case NVVM::ReductionKind::XOR:
54 return llvm::Intrinsic::nvvm_redux_sync_xor;
55 case NVVM::ReductionKind::MAX:
56 return llvm::Intrinsic::nvvm_redux_sync_max;
57 case NVVM::ReductionKind::MIN:
58 return llvm::Intrinsic::nvvm_redux_sync_min;
59 case NVVM::ReductionKind::FMIN:
61 case NVVM::ReductionKind::FMAX:
64 llvm_unreachable(
"unknown reduction kind");
72 resultType = cast<llvm::StructType>(resultType)->getElementType(0);
74 case NVVM::ShflKind::bfly:
75 return resultType->isFloatTy()
76 ? llvm::Intrinsic::nvvm_shfl_sync_bfly_f32p
77 : llvm::Intrinsic::nvvm_shfl_sync_bfly_i32p;
78 case NVVM::ShflKind::up:
79 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_up_f32p
80 : llvm::Intrinsic::nvvm_shfl_sync_up_i32p;
81 case NVVM::ShflKind::down:
82 return resultType->isFloatTy()
83 ? llvm::Intrinsic::nvvm_shfl_sync_down_f32p
84 : llvm::Intrinsic::nvvm_shfl_sync_down_i32p;
85 case NVVM::ShflKind::idx:
86 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_idx_f32p
87 : llvm::Intrinsic::nvvm_shfl_sync_idx_i32p;
91 case NVVM::ShflKind::bfly:
92 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_bfly_f32
93 : llvm::Intrinsic::nvvm_shfl_sync_bfly_i32;
94 case NVVM::ShflKind::up:
95 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_up_f32
96 : llvm::Intrinsic::nvvm_shfl_sync_up_i32;
97 case NVVM::ShflKind::down:
98 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_down_f32
99 : llvm::Intrinsic::nvvm_shfl_sync_down_i32;
100 case NVVM::ShflKind::idx:
101 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_idx_f32
102 : llvm::Intrinsic::nvvm_shfl_sync_idx_i32;
105 llvm_unreachable(
"unknown shuffle kind");
109 NVVM::MatchSyncKind kind) {
111 case NVVM::MatchSyncKind::any:
112 return valType.
isInteger(32) ? llvm::Intrinsic::nvvm_match_any_sync_i32
113 : llvm::Intrinsic::nvvm_match_any_sync_i64;
114 case NVVM::MatchSyncKind::all:
118 return valType.
isInteger(32) ? llvm::Intrinsic::nvvm_match_all_sync_i32p
119 : llvm::Intrinsic::nvvm_match_all_sync_i64p;
121 llvm_unreachable(
"unsupported match sync kind");
126 case NVVM::VoteSyncKind::any:
127 return llvm::Intrinsic::nvvm_vote_any_sync;
128 case NVVM::VoteSyncKind::all:
129 return llvm::Intrinsic::nvvm_vote_all_sync;
130 case NVVM::VoteSyncKind::ballot:
131 return llvm::Intrinsic::nvvm_vote_ballot_sync;
132 case NVVM::VoteSyncKind::uni:
133 return llvm::Intrinsic::nvvm_vote_uni_sync;
135 llvm_unreachable(
"unsupported vote kind");
138static llvm::Intrinsic::ID
140 NVVM::LdStMatrixShapeAttr
shape,
141 NVVM::LdStMatrixEltType eltType) {
145 return (layout == NVVM::MMALayout::row)
146 ? llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x1_b16
148 nvvm_ldmatrix_sync_aligned_m8n8_x1_trans_b16;
150 return (layout == NVVM::MMALayout::row)
151 ? llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x2_b16
153 nvvm_ldmatrix_sync_aligned_m8n8_x2_trans_b16;
155 return (layout == NVVM::MMALayout::row)
156 ? llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x4_b16
158 nvvm_ldmatrix_sync_aligned_m8n8_x4_trans_b16;
160 }
else if (
shape.getM() == 8 &&
shape.getN() == 16) {
161 if (eltType == NVVM::LdStMatrixEltType::B8X16_B6X16_P32) {
164 return llvm::Intrinsic::
165 nvvm_ldmatrix_sync_aligned_m8n16_x1_b8x16_b6x16_p32;
167 return llvm::Intrinsic::
168 nvvm_ldmatrix_sync_aligned_m8n16_x2_b8x16_b6x16_p32;
170 return llvm::Intrinsic::
171 nvvm_ldmatrix_sync_aligned_m8n16_x4_b8x16_b6x16_p32;
173 }
else if (eltType == NVVM::LdStMatrixEltType::B8X16_B4X16_P64) {
176 return llvm::Intrinsic::
177 nvvm_ldmatrix_sync_aligned_m8n16_x1_b8x16_b4x16_p64;
179 return llvm::Intrinsic::
180 nvvm_ldmatrix_sync_aligned_m8n16_x2_b8x16_b4x16_p64;
182 return llvm::Intrinsic::
183 nvvm_ldmatrix_sync_aligned_m8n16_x4_b8x16_b4x16_p64;
185 }
else if (eltType == NVVM::LdStMatrixEltType::S8_S4) {
188 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x1_s8_s4;
190 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x2_s8_s4;
192 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x4_s8_s4;
195 }
else if (
shape.getM() == 16 &&
shape.getN() == 16) {
196 if (eltType == NVVM::LdStMatrixEltType::B8) {
199 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m16n16_x1_trans_b8;
201 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m16n16_x2_trans_b8;
203 }
else if (eltType == NVVM::LdStMatrixEltType::B8X16_B6X16_P32) {
206 return llvm::Intrinsic::
207 nvvm_ldmatrix_sync_aligned_m16n16_x1_trans_b8x16_b6x16_p32;
209 return llvm::Intrinsic::
210 nvvm_ldmatrix_sync_aligned_m16n16_x2_trans_b8x16_b6x16_p32;
212 }
else if (eltType == NVVM::LdStMatrixEltType::B8X16_B4X16_P64) {
215 return llvm::Intrinsic::
216 nvvm_ldmatrix_sync_aligned_m16n16_x1_trans_b8x16_b4x16_p64;
218 return llvm::Intrinsic::
219 nvvm_ldmatrix_sync_aligned_m16n16_x2_trans_b8x16_b4x16_p64;
223 llvm_unreachable(
"unknown ldmatrix kind");
227static llvm::Intrinsic::ID
229 NVVM::LdStMatrixShapeAttr
shape,
230 NVVM::LdStMatrixEltType eltType) {
234 return (layout == NVVM::MMALayout::row)
235 ? llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x1_b16
237 nvvm_stmatrix_sync_aligned_m8n8_x1_trans_b16;
239 return (layout == NVVM::MMALayout::row)
240 ? llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x2_b16
242 nvvm_stmatrix_sync_aligned_m8n8_x2_trans_b16;
244 return (layout == NVVM::MMALayout::row)
245 ? llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x4_b16
247 nvvm_stmatrix_sync_aligned_m8n8_x4_trans_b16;
249 }
else if (
shape.getM() == 16 &&
shape.getN() == 8) {
252 return llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m16n8_x1_trans_b8;
254 return llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m16n8_x2_trans_b8;
256 return llvm::Intrinsic::nvvm_stmatrix_sync_aligned_m16n8_x4_trans_b8;
259 llvm_unreachable(
"unknown stmatrix kind");
263 NVVM::ProxyKind toProxy,
264 NVVM::MemScopeKind scope,
266 if (fromProxy == NVVM::ProxyKind::GENERIC &&
267 toProxy == NVVM::ProxyKind::TENSORMAP) {
269 case NVVM::MemScopeKind::CTA: {
271 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_release_cta;
272 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_acquire_cta;
274 case NVVM::MemScopeKind::CLUSTER: {
276 return llvm::Intrinsic::
277 nvvm_fence_proxy_tensormap_generic_release_cluster;
278 return llvm::Intrinsic::
279 nvvm_fence_proxy_tensormap_generic_acquire_cluster;
281 case NVVM::MemScopeKind::GPU: {
283 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_release_gpu;
284 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_acquire_gpu;
286 case NVVM::MemScopeKind::SYS: {
288 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_release_sys;
289 return llvm::Intrinsic::nvvm_fence_proxy_tensormap_generic_acquire_sys;
292 llvm_unreachable(
"Unknown scope for uni-directional fence.proxy operation");
294 llvm_unreachable(
"Unsupported proxy kinds");
299 case NVVM::MemScopeKind::CTA:
300 return llvm::Intrinsic::nvvm_membar_cta;
301 case NVVM::MemScopeKind::CLUSTER:
302 return llvm::Intrinsic::nvvm_fence_sc_cluster;
303 case NVVM::MemScopeKind::GPU:
304 return llvm::Intrinsic::nvvm_membar_gl;
305 case NVVM::MemScopeKind::SYS:
306 return llvm::Intrinsic::nvvm_membar_sys;
308 llvm_unreachable(
"Unknown scope for memory barrier");
311#define TCGEN05LD(SHAPE, NUM) llvm::Intrinsic::nvvm_tcgen05_ld_##SHAPE##_##NUM
313static llvm::Intrinsic::ID
315 llvm::Intrinsic::ID Shape16x64b[] = {
321 llvm::Intrinsic::ID Shape16x128b[] = {
327 llvm::Intrinsic::ID Shape16x256b[] = {
332 llvm::Intrinsic::ID Shape16x32bx2[] = {
339 llvm::Intrinsic::ID Shape32x32b[] = {
347 unsigned Idx = std::log2(num);
350 case NVVM::Tcgen05LdStShape::SHAPE_16X64B:
351 return Shape16x64b[Idx];
352 case NVVM::Tcgen05LdStShape::SHAPE_16X128B:
353 return Shape16x128b[Idx - 1];
354 case NVVM::Tcgen05LdStShape::SHAPE_16X256B:
355 return Shape16x256b[Idx - 2];
356 case NVVM::Tcgen05LdStShape::SHAPE_32X32B:
357 return Shape32x32b[Idx];
358 case NVVM::Tcgen05LdStShape::SHAPE_16X32BX2:
359 return Shape16x32bx2[Idx];
361 llvm_unreachable(
"unhandled tcgen05.ld lowering");
364#define TCGEN05ST(SHAPE, NUM) llvm::Intrinsic::nvvm_tcgen05_st_##SHAPE##_##NUM
366static llvm::Intrinsic::ID
368 llvm::Intrinsic::ID Shape16x64b[] = {
374 llvm::Intrinsic::ID Shape16x128b[] = {
380 llvm::Intrinsic::ID Shape16x256b[] = {
385 llvm::Intrinsic::ID Shape16x32bx2[] = {
392 llvm::Intrinsic::ID Shape32x32b[] = {
400 unsigned Idx = std::log2(num);
403 case NVVM::Tcgen05LdStShape::SHAPE_16X64B:
404 return Shape16x64b[Idx];
405 case NVVM::Tcgen05LdStShape::SHAPE_16X128B:
406 return Shape16x128b[Idx - 1];
407 case NVVM::Tcgen05LdStShape::SHAPE_16X256B:
408 return Shape16x256b[Idx - 2];
409 case NVVM::Tcgen05LdStShape::SHAPE_32X32B:
410 return Shape32x32b[Idx];
411 case NVVM::Tcgen05LdStShape::SHAPE_16X32BX2:
412 return Shape16x32bx2[Idx];
414 llvm_unreachable(
"unhandled tcgen05.st lowering");
418 return order == NVVM::MemOrderKind::ACQUIRE
420 nvvm_fence_acquire_sync_restrict_space_cluster_scope_cluster
422 nvvm_fence_release_sync_restrict_space_cta_scope_cluster;
425static llvm::Intrinsic::ID
428 case NVVM::ProxyKind::alias:
429 return llvm::Intrinsic::nvvm_fence_proxy_alias;
430 case NVVM::ProxyKind::async:
431 return llvm::Intrinsic::nvvm_fence_proxy_async;
432 case NVVM::ProxyKind::async_global:
433 return llvm::Intrinsic::nvvm_fence_proxy_async_global;
434 case NVVM::ProxyKind::async_shared:
435 return *space == NVVM::SharedSpace::shared_cta
436 ? llvm::Intrinsic::nvvm_fence_proxy_async_shared_cta
437 : llvm::Intrinsic::nvvm_fence_proxy_async_shared_cluster;
439 llvm_unreachable(
"unsupported proxy kind");
443static llvm::Intrinsic::ID
445 return order == NVVM::MemOrderKind::ACQUIRE
447 nvvm_fence_proxy_async_generic_acquire_sync_restrict_space_cluster_scope_cluster
449 nvvm_fence_proxy_async_generic_release_sync_restrict_space_cta_scope_cluster;
452static llvm::RoundingMode
455 case NVVM::FPRoundingMode::RN:
456 return llvm::RoundingMode::NearestTiesToEven;
457 case NVVM::FPRoundingMode::RM:
458 return llvm::RoundingMode::TowardNegative;
459 case NVVM::FPRoundingMode::RP:
460 return llvm::RoundingMode::TowardPositive;
461 case NVVM::FPRoundingMode::RZ:
462 return llvm::RoundingMode::TowardZero;
465 assert(rndMode == NVVM::FPRoundingMode::NONE &&
466 "unsupported rounding mode for nvvm fp arithmetic");
467 return llvm::RoundingMode::NearestTiesToEven;
477 llvm::Intrinsic::ID IID, llvm::Type *opTypeLLVM,
479 llvm::Type *retType) {
480 if (opTypeLLVM->isVectorTy() && (opTypeLLVM->getScalarType()->isFloatTy() ||
481 opTypeLLVM->getScalarType()->isDoubleTy())) {
482 llvm::Value *
result = llvm::PoisonValue::get(
483 llvm::FixedVectorType::get(opTypeLLVM->getScalarType(), 2));
484 for (
int64_t i = 0; i < 2; ++i) {
486 for (llvm::Value *op : operands)
487 scalarArgs.push_back(
488 op->getType()->isVectorTy()
489 ? builder.CreateExtractElement(op, builder.getInt32(i))
492 result = builder.CreateInsertElement(
result, res, builder.getInt32(i));
503 llvm::Value *argLHS, llvm::Value *argRHS,
504 Value res, NVVM::FPRoundingMode rndMode,
505 NVVM::SaturationMode satMode,
bool isFTZ,
507 llvm::IRBuilderBase &builder) {
508 llvm::Type *opTypeLLVM = argLHS->getType();
509 bool isSat = satMode != NVVM::SaturationMode::NONE;
511 llvm::Intrinsic::ID
id = ids[isFTZ][isSat];
512 llvm::Value *rnd = builder.getInt32(
517 llvm::Type *scalarTypeLLVM = opTypeLLVM->getScalarType();
518 if (opTypeLLVM->isVectorTy() && (scalarTypeLLVM->isDoubleTy() ||
519 (isSat && scalarTypeLLVM->isFloatTy()))) {
521 {argLHS, argRHS, rnd},
530void NVVM::AddFOp::lowerAddFToLLVMIR(llvm::Value *argLHS, llvm::Value *argRHS,
531 Value res, NVVM::FPRoundingMode rndMode,
532 NVVM::SaturationMode satMode,
bool isFTZ,
534 llvm::IRBuilderBase &builder) {
535 static constexpr llvm::Intrinsic::ID addIDs[2][2] = {
536 {llvm::Intrinsic::nvvm_fadd, llvm::Intrinsic::nvvm_fadd_sat},
537 {llvm::Intrinsic::nvvm_fadd_ftz, llvm::Intrinsic::nvvm_fadd_ftz_sat}};
542void NVVM::MulFOp::lowerMulFToLLVMIR(llvm::Value *argLHS, llvm::Value *argRHS,
543 Value res, NVVM::FPRoundingMode rndMode,
544 NVVM::SaturationMode satMode,
bool isFTZ,
546 llvm::IRBuilderBase &builder) {
547 static constexpr llvm::Intrinsic::ID mulIDs[2][2] = {
548 {llvm::Intrinsic::nvvm_fmul, llvm::Intrinsic::nvvm_fmul_sat},
549 {llvm::Intrinsic::nvvm_fmul_ftz, llvm::Intrinsic::nvvm_fmul_ftz_sat}};
555 llvm::IRBuilderBase &builder) {
556 auto thisOp = cast<NVVM::FmaOp>(op);
557 mlir::NVVM::FPRoundingMode rndMode = thisOp.getRnd();
558 bool isFTZ = thisOp.getFtz();
559 bool isRelu = thisOp.getRelu();
560 bool isSat = thisOp.getSat() == NVVM::SaturationMode::SAT;
561 bool isOOB = thisOp.getOob();
563 mlir::Type opType = thisOp.getRes().getType();
572 llvm::Intrinsic::ID
id = isRelu ? llvm::Intrinsic::nvvm_fma_rn_oob_relu
573 : llvm::Intrinsic::nvvm_fma_rn_oob;
575 {argA, argB, argC}));
579 static constexpr llvm::Intrinsic::ID fmaIDs[2][3] = {
580 {llvm::Intrinsic::nvvm_fma, llvm::Intrinsic::nvvm_fma_sat,
581 llvm::Intrinsic::nvvm_fma_relu},
582 {llvm::Intrinsic::nvvm_fma_ftz, llvm::Intrinsic::nvvm_fma_ftz_sat,
583 llvm::Intrinsic::nvvm_fma_ftz_relu}};
585 llvm::Intrinsic::ID
id = fmaIDs[isFTZ][isSat ? 1 : (isRelu ? 2 : 0)];
586 llvm::Value *rnd = builder.getInt32(
591 llvm::Type *scalarTypeLLVM = opTypeLLVM->getScalarType();
592 if (opTypeLLVM->isVectorTy() && (scalarTypeLLVM->isDoubleTy() ||
593 (isSat && scalarTypeLLVM->isFloatTy()))) {
595 builder,
id, opTypeLLVM,
596 {argA, argB, argC, rnd}, scalarTypeLLVM));
601 {argA, argB, argC, rnd}));
607 auto vecTy = dyn_cast<VectorType>(type);
608 if (!vecTy || vecTy.getNumElements() == 1)
609 return builder.getInt32Ty();
610 return llvm::FixedVectorType::get(builder.getInt32Ty(),
611 vecTy.getNumElements());
614void NVVM::SparseCompressOp::lowerToLLVMIR(
Operation &op,
616 llvm::IRBuilderBase &builder) {
617 auto thisOp = cast<NVVM::SparseCompressOp>(op);
618 Value metadata = thisOp.getMetadata();
619 Value compressedData = thisOp.getCompressedData();
620 llvm::Type *resultTy =
625 builder, llvm::Intrinsic::nvvm_spcompress, resultTy,
627 builder.getInt32(thisOp.getIdxSize()),
629 builder.getInt32(4)});
632 builder.CreateBitCast(builder.CreateExtractValue(
result, 0),
634 mt.
mapValue(compressedData) = builder.CreateExtractValue(
result, 1);
650 builder,
id, llvm::StructType::get(builder.getContext(), resultTys),
654void NVVM::SparseDecompressOp::lowerToLLVMIR(
Operation &op,
656 llvm::IRBuilderBase &builder) {
657 auto thisOp = cast<NVVM::SparseDecompressOp>(op);
658 Value metadata = thisOp.getMetadata();
660 builder, llvm::Intrinsic::nvvm_spdecompress,
662 {builder.CreateBitCast(mt.lookupValue(metadata),
663 getSPMetadataType(metadata.getType(), builder)),
664 mt.lookupValue(thisOp.getCompressedData()),
665 builder.getInt32(thisOp.getIdxSize()),
666 builder.getInt32(thisOp.getNumTgt())});
672class NVVMDialectLLVMIRTranslationInterface
673 :
public LLVMTranslationDialectInterface {
675 using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
680 convertOperation(Operation *op, llvm::IRBuilderBase &builder,
681 LLVM::ModuleTranslation &moduleTranslation)
const final {
685 if (!builder.GetInsertBlock())
687 "cannot be translated to LLVM IR without an active insertion "
688 "point; make sure the op is inside a function");
689 Operation &opInst = *op;
690#include "mlir/Dialect/LLVMIR/NVVMConversions.inc"
698 amendOperation(Operation *op, ArrayRef<llvm::Instruction *> instructions,
699 NamedAttribute attribute,
700 LLVM::ModuleTranslation &moduleTranslation)
const final {
701 if (
auto globalOp = dyn_cast<LLVM::GlobalOp>(op)) {
702 if (attribute.getName() == NVVM::NVVMDialect::getManagedAttrName()) {
703 auto *gv = cast<llvm::GlobalVariable>(
704 moduleTranslation.lookupGlobal(globalOp));
705 llvm::Module *m = gv->getParent();
706 llvm::LLVMContext &ctx = m->getContext();
707 llvm::NamedMDNode *md = m->getOrInsertNamedMetadata(
"nvvm.annotations");
708 md->addOperand(llvm::MDNode::get(
709 ctx, {llvm::ConstantAsMetadata::get(gv),
710 llvm::MDString::get(ctx,
"managed"),
711 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
712 llvm::Type::getInt32Ty(ctx), 1))}));
717 auto func = dyn_cast<LLVM::LLVMFuncOp>(op);
720 llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
722 if (attribute.getName() == NVVM::NVVMDialect::getMaxntidAttrName()) {
723 if (!isa<DenseI32ArrayAttr>(attribute.getValue()))
725 auto values = cast<DenseI32ArrayAttr>(attribute.getValue());
726 const std::string attr = llvm::formatv(
727 "{0:$[,]}", llvm::make_range(values.asArrayRef().begin(),
728 values.asArrayRef().end()));
729 llvmFunc->addFnAttr(llvm::NVVMAttr::MaxNTID, attr);
730 }
else if (attribute.getName() == NVVM::NVVMDialect::getReqntidAttrName()) {
731 if (!isa<DenseI32ArrayAttr>(attribute.getValue()))
733 auto values = cast<DenseI32ArrayAttr>(attribute.getValue());
734 const std::string attr = llvm::formatv(
735 "{0:$[,]}", llvm::make_range(values.asArrayRef().begin(),
736 values.asArrayRef().end()));
737 llvmFunc->addFnAttr(llvm::NVVMAttr::ReqNTID, attr);
738 }
else if (attribute.getName() ==
739 NVVM::NVVMDialect::getClusterDimAttrName()) {
740 if (!isa<DenseI32ArrayAttr>(attribute.getValue()))
742 auto values = cast<DenseI32ArrayAttr>(attribute.getValue());
743 const std::string attr = llvm::formatv(
744 "{0:$[,]}", llvm::make_range(values.asArrayRef().begin(),
745 values.asArrayRef().end()));
746 llvmFunc->addFnAttr(llvm::NVVMAttr::ClusterDim, attr);
747 }
else if (attribute.getName() ==
748 NVVM::NVVMDialect::getClusterMaxBlocksAttrName()) {
749 auto value = dyn_cast<IntegerAttr>(attribute.getValue());
750 llvmFunc->addFnAttr(llvm::NVVMAttr::MaxClusterRank,
751 llvm::utostr(value.getInt()));
752 }
else if (attribute.getName() ==
753 NVVM::NVVMDialect::getMinctasmAttrName()) {
754 auto value = dyn_cast<IntegerAttr>(attribute.getValue());
755 llvmFunc->addFnAttr(llvm::NVVMAttr::MinCTASm,
756 llvm::utostr(value.getInt()));
757 }
else if (attribute.getName() == NVVM::NVVMDialect::getMaxnregAttrName()) {
758 auto value = dyn_cast<IntegerAttr>(attribute.getValue());
759 llvmFunc->addFnAttr(llvm::NVVMAttr::MaxNReg,
760 llvm::utostr(value.getInt()));
761 }
else if (attribute.getName() ==
762 NVVM::NVVMDialect::getKernelFuncAttrName()) {
763 llvmFunc->setCallingConv(llvm::CallingConv::PTX_Kernel);
764 }
else if (attribute.getName() ==
765 NVVM::NVVMDialect::getBlocksAreClustersAttrName()) {
766 llvmFunc->addFnAttr(llvm::NVVMAttr::BlocksAreClusters);
774 LLVM::ModuleTranslation &moduleTranslation)
const final {
776 llvm::LLVMContext &llvmContext = moduleTranslation.getLLVMContext();
777 llvm::Function *llvmFunc =
778 moduleTranslation.lookupFunction(funcOp.getName());
780 if (attribute.getName() == NVVM::NVVMDialect::getGridConstantAttrName()) {
781 llvmFunc->addParamAttr(
783 llvm::Attribute::get(llvmContext, llvm::NVVMAttr::GridConstant));
791 registry.
insert<NVVM::NVVMDialect>();
793 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)
static llvm::Intrinsic::ID getFenceProxyID(NVVM::ProxyKind kind, std::optional< NVVM::SharedSpace > space)
static llvm::Value * createTcgen05LdSPCompressCall(llvm::Intrinsic::ID id, ArrayRef< llvm::Value * > args, Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder)
#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)
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.
static llvm::Intrinsic::ID getFenceProxySyncRestrictID(NVVM::MemOrderKind order)
#define TCGEN05ST(SHAPE, NUM)
static llvm::Intrinsic::ID getReduxIntrinsicId(llvm::Type *resultType, NVVM::ReductionKind kind, bool hasAbs, bool hasNaN)
static llvm::RoundingMode getLLVMRoundingModeForFPArith(NVVM::FPRoundingMode rndMode)
static void lowerFloatBinaryOpToLLVMIR(const llvm::Intrinsic::ID(&ids)[2][2], llvm::Value *argLHS, llvm::Value *argRHS, Value res, NVVM::FPRoundingMode rndMode, NVVM::SaturationMode satMode, bool isFTZ, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder)
static llvm::Value * createScalarizedIntrinsicCall(llvm::IRBuilderBase &builder, llvm::Intrinsic::ID IID, llvm::Type *opTypeLLVM, ArrayRef< llvm::Value * > operands, llvm::Type *retType)
#define TCGEN05LD(SHAPE, NUM)
static llvm::Intrinsic::ID getFenceSyncRestrictID(NVVM::MemOrderKind order)
static llvm::Type * getSPMetadataType(Type type, llvm::IRBuilderBase &builder)
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 Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
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.
Implementation class for module translation.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
llvm::Type * convertType(Type type)
Converts the type from MLIR LLVM dialect to LLVM.
void mapValue(Value mlir, llvm::Value *llvm)
Stores the mapping between an MLIR value and its LLVM IR counterpart.
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.
Operation is the basic unit of execution within MLIR.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
result_type_range getResultTypes()
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
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).
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.
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;.