21#include "llvm/IR/IRBuilder.h"
22#include "llvm/IR/IntrinsicsNVPTX.h"
23#include "llvm/IR/NVVMIntrinsicUtils.h"
24#include "llvm/Support/Casting.h"
25#include "llvm/Support/FormatVariadic.h"
39static llvm::nvvm::CTAGroupKind
42 case NVVM::CTAGroupKind::CTA_1:
43 return llvm::nvvm::CTAGroupKind::CG_1;
44 case NVVM::CTAGroupKind::CTA_2:
45 return llvm::nvvm::CTAGroupKind::CG_2;
47 llvm_unreachable(
"unsupported cta_group value");
54LogicalResult NVVM::Tcgen05CpOp::verify() {
55 auto mc = getMulticast();
57 using SH = Tcgen05CpShape;
58 using MC = Tcgen05CpMulticast;
60 case SH::SHAPE_128x256b:
61 case SH::SHAPE_128x128b:
62 case SH::SHAPE_4x256b:
64 return emitError(
"Invalid multicast type for tcgen05.cp Op");
66 case SH::SHAPE_64x128b:
67 if (mc != MC::WARPX2_01_23 && mc != MC::WARPX2_02_13)
68 return emitError(
"Shape 64x128b requires multicast warpx2_01_23 or "
69 "warpx2_02_13 for tcgen05.cp Op");
71 case SH::SHAPE_32x128b:
74 "Shape 32x128b requires multicast warpx4 for tcgen05.cp Op");
88 field = builder.CreateZExtOrBitCast(field, builder.getInt32Ty());
90 unsigned mask = (sizeInBits < 32 ? ((1u << sizeInBits) - 1) : 0xffffffffu);
91 if (mask != 0xffffffffu)
92 field = builder.CreateAnd(field, builder.getInt32(mask));
94 field = builder.CreateZExtOrBitCast(field, builder.getInt64Ty());
95 field = builder.CreateShl(field, start);
97 return builder.CreateOr(
result, field);
100void Tcgen05MmaSmemDescOp::createSmemDescriptor(
Operation &op,
102 llvm::IRBuilderBase &builder) {
103 auto thisOp = cast<NVVM::Tcgen05MmaSmemDescOp>(op);
104 llvm::Value *smemDesc = builder.getInt64(0);
109 builder, smemDesc, mt.
lookupValue(thisOp.getLeadingDimOffset()), 14, 16);
111 builder, smemDesc, mt.
lookupValue(thisOp.getStrideDimOffset()), 14, 32);
117 builder, smemDesc, mt.
lookupValue(thisOp.getLeadingDimMode()), 1, 52);
121 mt.
mapValue(thisOp.getRes()) = smemDesc;
130 auto curOp = cast<NVVM::Tcgen05AllocOp>(op);
131 bool is2CTAMode = curOp.getGroup() == CTAGroupKind::CTA_2;
133 llvm::Intrinsic::ID
id = is2CTAMode ? llvm::Intrinsic::nvvm_tcgen05_alloc_cg2
134 : llvm::Intrinsic::nvvm_tcgen05_alloc_cg1;
138 builder.getInt1(curOp.getIsExclusive())}};
143 auto curOp = cast<NVVM::Tcgen05DeallocOp>(op);
144 llvm::Intrinsic::ID
id = (curOp.getGroup() == CTAGroupKind::CTA_1)
145 ? llvm::Intrinsic::nvvm_tcgen05_dealloc_cg1
146 : llvm::Intrinsic::nvvm_tcgen05_dealloc_cg2;
150 builder.getInt1(curOp.getIsExclusive())}};
154Tcgen05CommitOp::getIntrinsicIDAndArgs(
Operation &op,
157 auto curOp = cast<NVVM::Tcgen05CommitOp>(op);
158 bool hasMulticast =
static_cast<bool>(curOp.getMulticastMask());
159 bool is2CTAMode = curOp.getGroup() == CTAGroupKind::CTA_2;
160 bool hasSmemARead = curOp.getSmemARead();
161 unsigned index = (
static_cast<unsigned>(hasSmemARead) << 1) |
162 static_cast<unsigned>(is2CTAMode);
164 using namespace llvm::Intrinsic;
165 static constexpr ID IDs[] = {
166 nvvm_tcgen05_commit_cg1,
167 nvvm_tcgen05_commit_cg2,
168 nvvm_tcgen05_commit_smem_a_read_cg1,
169 nvvm_tcgen05_commit_smem_a_read_cg2,
172 static constexpr ID multicastIDs[] = {
173 nvvm_tcgen05_commit_mc_cg1,
174 nvvm_tcgen05_commit_mc_cg2,
175 nvvm_tcgen05_commit_smem_a_read_mc_cg1,
176 nvvm_tcgen05_commit_smem_a_read_mc_cg2,
179 ID
id = hasMulticast ? multicastIDs[
index] : IDs[
index];
183 args.push_back(mt.
lookupValue(curOp.getMulticastMask()));
188#define TCGEN05_CP_IMPL(shape_mc, src_fmt, cg) \
189 llvm::Intrinsic::nvvm_tcgen05_cp##shape_mc##src_fmt##cg
191#define TCGEN05_CP_2CTA(shape_mc, src_fmt, is_2cta) \
192 is_2cta ? TCGEN05_CP_IMPL(shape_mc, src_fmt, _cg2) \
193 : TCGEN05_CP_IMPL(shape_mc, src_fmt, _cg1)
195#define GET_TCGEN05_CP_ID(shape_mc, src_fmt, is_2cta) \
197 if ((src_fmt) == Tcgen05CpSrcFormat::B6x16_P32) \
198 return TCGEN05_CP_2CTA(shape_mc, _b6x16_p32, is_2cta); \
199 if ((src_fmt) == Tcgen05CpSrcFormat::B4x16_P64) \
200 return TCGEN05_CP_2CTA(shape_mc, _b4x16_p64, is_2cta); \
201 return TCGEN05_CP_2CTA(shape_mc, , is_2cta); \
204llvm::Intrinsic::ID Tcgen05CpOp::getIntrinsicID(
Operation &op) {
205 auto curOp = cast<NVVM::Tcgen05CpOp>(op);
206 bool is2CTA = curOp.getGroup() == CTAGroupKind::CTA_2;
207 auto srcFmt = curOp.getSrcFormat();
208 auto mc = curOp.getMulticast();
210 switch (curOp.getShape()) {
211 case Tcgen05CpShape::SHAPE_128x256b:
213 case Tcgen05CpShape::SHAPE_128x128b:
215 case Tcgen05CpShape::SHAPE_4x256b:
217 case Tcgen05CpShape::SHAPE_32x128b:
219 case Tcgen05CpShape::SHAPE_64x128b:
220 return (mc == Tcgen05CpMulticast::WARPX2_01_23)
224 llvm_unreachable(
"Invalid shape in tcgen05 cp Op");
231 if (
shape == NVVM::Tcgen05LdStShape::SHAPE_16X128B)
233 if (
shape == NVVM::Tcgen05LdStShape::SHAPE_16X256B)
238LogicalResult Tcgen05LdOp::verify() {
240 if (
getShape() == NVVM::Tcgen05LdStShape::SHAPE_16X32BX2 && !getOffset())
243 if (
getShape() != NVVM::Tcgen05LdStShape::SHAPE_16X32BX2 && getOffset())
244 result =
emitError(
"offset argument is only supported for shape 16x32bx2");
246 auto resTy = getRes().getType();
247 unsigned resLen = isa<VectorType>(resTy)
248 ? llvm::cast<VectorType>(resTy).getNumElements()
251 result =
emitError(llvm::formatv(
"invalid result type length {0} for shape "
252 "{1} in tcgen05.ld Op",
253 resLen, stringifyEnum(
getShape())));
258LogicalResult Tcgen05StOp::verify() {
260 if (
getShape() == NVVM::Tcgen05LdStShape::SHAPE_16X32BX2 && !getOffset())
263 auto valTy = getVal().getType();
264 unsigned valLen = isa<VectorType>(valTy)
265 ? llvm::cast<VectorType>(valTy).getNumElements()
269 "{1} in tcgen05.st Op",
270 valLen, stringifyEnum(
getShape())));
279static llvm::nvvm::Tcgen05MMAKind
282 case NVVM::Tcgen05MMAKind::F16:
283 return llvm::nvvm::Tcgen05MMAKind::F16;
284 case NVVM::Tcgen05MMAKind::TF32:
285 return llvm::nvvm::Tcgen05MMAKind::TF32;
286 case NVVM::Tcgen05MMAKind::F8F6F4:
287 return llvm::nvvm::Tcgen05MMAKind::F8F6F4;
288 case NVVM::Tcgen05MMAKind::I8:
289 return llvm::nvvm::Tcgen05MMAKind::I8;
290 case NVVM::Tcgen05MMAKind::TI16:
291 return llvm::nvvm::Tcgen05MMAKind::TI16;
292 case NVVM::Tcgen05MMAKind::MXF8F6F4:
293 case NVVM::Tcgen05MMAKind::MXF4:
294 case NVVM::Tcgen05MMAKind::MXF4NVF4:
297 llvm_unreachable(
"Unsupported tcgen05.mma kind");
299 llvm_unreachable(
"Invalid tcgen05.mma kind");
304 llvm::IRBuilderBase &builder) {
306 auto thisOp = cast<NVVM::Tcgen05MMAOp>(op);
309 args.push_back(mt.
lookupValue(thisOp.getMatrixD()));
312 const bool isATensor = isa<llvm::PointerType>(
A->getType());
315 args.push_back(mt.
lookupValue(thisOp.getMatrixB()));
317 args.push_back(mt.
lookupValue(thisOp.getEnableInputD()));
319 using EnableAShiftArray = std::array<llvm::Intrinsic::ID, 2>;
320 using CtaGroupArray = std::array<EnableAShiftArray, 2>;
321 using IsATensorArray = std::array<CtaGroupArray, 2>;
322 using HasScaleInputDArray = std::array<IsATensorArray, 2>;
323 using HasDisableOutputLaneArray = std::array<HasScaleInputDArray, 2>;
326 static constexpr HasDisableOutputLaneArray tcgen05MMAIDs = {
332 {llvm::Intrinsic::nvvm_tcgen05_mma_shared,
notIntrinsic},
334 {llvm::Intrinsic::nvvm_tcgen05_mma_shared,
notIntrinsic}}},
338 llvm::Intrinsic::nvvm_tcgen05_mma_tensor,
339 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_ashift,
343 llvm::Intrinsic::nvvm_tcgen05_mma_tensor,
344 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_ashift,
350 {llvm::Intrinsic::nvvm_tcgen05_mma_shared_scale_d,
notIntrinsic},
352 {llvm::Intrinsic::nvvm_tcgen05_mma_shared_scale_d,
notIntrinsic}}},
356 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_scale_d,
357 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_scale_d_ashift,
361 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_scale_d,
362 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_scale_d_ashift,
368 {llvm::Intrinsic::nvvm_tcgen05_mma_shared_disable_output_lane_cg1,
371 {llvm::Intrinsic::nvvm_tcgen05_mma_shared_disable_output_lane_cg2,
376 nvvm_tcgen05_mma_tensor_disable_output_lane_cg1,
378 nvvm_tcgen05_mma_tensor_disable_output_lane_cg1_ashift,
383 nvvm_tcgen05_mma_tensor_disable_output_lane_cg2,
385 nvvm_tcgen05_mma_tensor_disable_output_lane_cg2_ashift,
391 nvvm_tcgen05_mma_shared_scale_d_disable_output_lane_cg1,
395 nvvm_tcgen05_mma_shared_scale_d_disable_output_lane_cg2,
400 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg1,
402 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg1_ashift},
406 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg2,
408 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg2_ashift,
411 llvm::Value *ScaleInputD = mt.
lookupValue(thisOp.getScaleInputD());
412 bool hasScaleInputD = ScaleInputD !=
nullptr;
414 llvm::Value *DisableOutputLane =
416 bool hasDisableOutputLane = DisableOutputLane !=
nullptr;
418 const unsigned ctaGroup =
421 llvm::Intrinsic::ID ID =
422 tcgen05MMAIDs[hasDisableOutputLane][hasScaleInputD][isATensor]
423 [ctaGroup - 1][thisOp.getAShift()];
425 assert(ID !=
notIntrinsic &&
"Invalid intrinsic for Tcgen05MMAOp.");
428 args.push_back(ScaleInputD);
430 if (hasDisableOutputLane)
431 args.push_back(DisableOutputLane);
433 args.push_back(builder.getInt32(
436 if (!hasDisableOutputLane)
437 args.push_back(builder.getInt32(ctaGroup));
440 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOp())));
443 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOpB())));
450 NVVM::CTAGroupKind ctaGroup,
bool hasAShift,
451 NVVM::Tcgen05MMACollectorOp collectorOp,
Location loc) {
453 if (disableOutputLane) {
454 mlir::VectorType disableOutputLaneType =
455 cast<mlir::VectorType>(disableOutputLane.
getType());
456 if ((ctaGroup == NVVM::CTAGroupKind::CTA_1 &&
457 disableOutputLaneType.getNumElements() != 4) ||
458 (ctaGroup == NVVM::CTAGroupKind::CTA_2 &&
459 disableOutputLaneType.getNumElements() != 8))
460 return emitError(loc) <<
"Disable Output Lane of length "
461 << disableOutputLaneType.getNumElements()
462 <<
" is incompatible with CtaGroupAttr";
465 if (hasAShift && !isATensor)
467 loc,
"A-shift can be applied only when matrix A is in tensor memory");
469 if (hasAShift ==
true && (collectorOp == Tcgen05MMACollectorOp::FILL ||
470 collectorOp == Tcgen05MMACollectorOp::USE))
472 loc,
"Cannot use collector buffer operation fill or use with ashift");
477LogicalResult Tcgen05MMAOp::verify() {
479 getDisableOutputLane(), getCtaGroup(), getAShift(),
480 getCollectorOp(), getLoc());
490 auto thisOp = cast<NVVM::Tcgen05MMASparseOp>(op);
493 args.push_back(mt.
lookupValue(thisOp.getMatrixD()));
496 bool isATensor = isa<llvm::PointerType>(
A->getType());
499 args.push_back(mt.
lookupValue(thisOp.getMatrixB()));
501 args.push_back(mt.
lookupValue(thisOp.getEnableInputD()));
502 args.push_back(mt.
lookupValue(thisOp.getSparseMetadata()));
504 using EnableAShiftArray = std::array<llvm::Intrinsic::ID, 2>;
505 using CtaGroupArray = std::array<EnableAShiftArray, 2>;
506 using IsATensorArray = std::array<CtaGroupArray, 2>;
507 using HasScaleInputDArray = std::array<IsATensorArray, 2>;
508 using HasDisableOutputLaneArray = std::array<HasScaleInputDArray, 2>;
511 static constexpr HasDisableOutputLaneArray tcgen05MMASparseIDs = {
517 {llvm::Intrinsic::nvvm_tcgen05_mma_sp_shared,
notIntrinsic},
519 {llvm::Intrinsic::nvvm_tcgen05_mma_sp_shared,
notIntrinsic}}},
523 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor,
524 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_ashift,
528 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor,
529 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_ashift,
535 {llvm::Intrinsic::nvvm_tcgen05_mma_sp_shared_scale_d,
538 {llvm::Intrinsic::nvvm_tcgen05_mma_sp_shared_scale_d,
543 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d,
544 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d_ashift,
548 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d,
549 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d_ashift,
556 nvvm_tcgen05_mma_sp_shared_disable_output_lane_cg1,
560 nvvm_tcgen05_mma_sp_shared_disable_output_lane_cg2,
565 nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg1,
567 nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg1_ashift,
572 nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg2,
574 nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg2_ashift,
580 nvvm_tcgen05_mma_sp_shared_scale_d_disable_output_lane_cg1,
584 nvvm_tcgen05_mma_sp_shared_scale_d_disable_output_lane_cg2,
589 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg1,
591 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg1_ashift},
595 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg2,
597 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg2_ashift,
600 llvm::Value *ScaleInputD = mt.
lookupValue(thisOp.getScaleInputD());
601 bool hasScaleInputD = ScaleInputD !=
nullptr;
603 llvm::Value *DisableOutputLane =
605 bool hasDisableOutputLane = DisableOutputLane !=
nullptr;
610 llvm::Intrinsic::ID ID =
611 tcgen05MMASparseIDs[hasDisableOutputLane][hasScaleInputD][isATensor]
612 [ctaGroup - 1][thisOp.getAShift()];
614 assert(ID !=
notIntrinsic &&
"Invalid intrinsic for Tcgen05MMASparseOp.");
617 args.push_back(ScaleInputD);
619 if (hasDisableOutputLane)
620 args.push_back(DisableOutputLane);
622 args.push_back(builder.getInt32(
625 if (!hasDisableOutputLane)
626 args.push_back(builder.getInt32(ctaGroup));
629 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOp())));
632 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOpB())));
637LogicalResult Tcgen05MMASparseOp::verify() {
639 getDisableOutputLane(), getCtaGroup(), getAShift(),
640 getCollectorOp(), getLoc());
650 auto thisOp = cast<NVVM::Tcgen05MMABlockScaleOp>(op);
653 args.push_back(mt.
lookupValue(thisOp.getMatrixD()));
656 bool isATensor = isa<llvm::PointerType>(
A->getType());
659 args.push_back(mt.
lookupValue(thisOp.getMatrixB()));
661 args.push_back(mt.
lookupValue(thisOp.getEnableInputD()));
662 args.push_back(mt.
lookupValue(thisOp.getScaleA()));
663 args.push_back(mt.
lookupValue(thisOp.getScaleB()));
664 args.push_back(builder.getInt32(
667 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOp())));
669 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOpB())));
671 auto kind = thisOp.getKind();
672 auto blockScale = thisOp.getBlockScale();
673 llvm::Intrinsic::ID ID = [&]() {
674 if (kind == NVVM::Tcgen05MMAKind::MXF8F6F4) {
675 if (blockScale == NVVM::Tcgen05MMABlockScale::DEFAULT) {
676 return isATensor ? llvm::Intrinsic::
677 nvvm_tcgen05_mma_tensor_mxf8f6f4_block_scale
679 nvvm_tcgen05_mma_shared_mxf8f6f4_block_scale;
680 }
else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
683 nvvm_tcgen05_mma_tensor_mxf8f6f4_block_scale_block32
685 nvvm_tcgen05_mma_shared_mxf8f6f4_block_scale_block32;
687 }
else if (kind == NVVM::Tcgen05MMAKind::MXF4) {
688 if (blockScale == NVVM::Tcgen05MMABlockScale::DEFAULT) {
690 ? llvm::Intrinsic::nvvm_tcgen05_mma_tensor_mxf4_block_scale
691 : llvm::Intrinsic::nvvm_tcgen05_mma_shared_mxf4_block_scale;
692 }
else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
693 return isATensor ? llvm::Intrinsic::
694 nvvm_tcgen05_mma_tensor_mxf4_block_scale_block32
696 nvvm_tcgen05_mma_shared_mxf4_block_scale_block32;
698 }
else if (kind == NVVM::Tcgen05MMAKind::MXF4NVF4) {
699 if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
702 nvvm_tcgen05_mma_tensor_mxf4nvf4_block_scale_block32
704 nvvm_tcgen05_mma_shared_mxf4nvf4_block_scale_block32;
706 }
else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK16) {
709 nvvm_tcgen05_mma_tensor_mxf4nvf4_block_scale_block16
711 nvvm_tcgen05_mma_shared_mxf4nvf4_block_scale_block16;
714 llvm_unreachable(
"Invalid tcgen05.mma.block_scale attributes");
721 NVVM::Tcgen05MMACollectorOp collectorOp, NVVM::Tcgen05MMAKind kind,
722 NVVM::Tcgen05MMABlockScale blockScale,
Location loc) {
723 if (blockScale == NVVM::Tcgen05MMABlockScale::DEFAULT &&
724 kind == NVVM::Tcgen05MMAKind::MXF4NVF4)
725 return emitError(loc,
"mxf4nvf4 requires block scale attribute");
727 if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK16 &&
728 kind != NVVM::Tcgen05MMAKind::MXF4NVF4)
730 llvm::formatv(
"{} kind does not support block16 attribute",
731 stringifyEnum(kind)));
736LogicalResult Tcgen05MMABlockScaleOp::verify() {
738 getBlockScale(), getLoc());
748 auto thisOp = cast<NVVM::Tcgen05MMASparseBlockScaleOp>(op);
751 args.push_back(mt.
lookupValue(thisOp.getMatrixD()));
754 bool isATensor = isa<llvm::PointerType>(
A->getType());
757 args.push_back(mt.
lookupValue(thisOp.getMatrixB()));
759 args.push_back(mt.
lookupValue(thisOp.getEnableInputD()));
760 args.push_back(mt.
lookupValue(thisOp.getSparseMetadata()));
761 args.push_back(mt.
lookupValue(thisOp.getScaleA()));
762 args.push_back(mt.
lookupValue(thisOp.getScaleB()));
763 args.push_back(builder.getInt32(
766 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOp())));
768 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOpB())));
770 auto kind = thisOp.getKind();
771 auto blockScale = thisOp.getBlockScale();
772 llvm::Intrinsic::ID ID = [&]() {
773 if (kind == NVVM::Tcgen05MMAKind::MXF8F6F4) {
774 if (blockScale == NVVM::Tcgen05MMABlockScale::DEFAULT) {
775 return isATensor ? llvm::Intrinsic::
776 nvvm_tcgen05_mma_sp_tensor_mxf8f6f4_block_scale
778 nvvm_tcgen05_mma_sp_shared_mxf8f6f4_block_scale;
779 }
else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
782 nvvm_tcgen05_mma_sp_tensor_mxf8f6f4_block_scale_block32
784 nvvm_tcgen05_mma_sp_shared_mxf8f6f4_block_scale_block32;
786 }
else if (kind == NVVM::Tcgen05MMAKind::MXF4) {
787 if (blockScale == NVVM::Tcgen05MMABlockScale::DEFAULT) {
788 return isATensor ? llvm::Intrinsic::
789 nvvm_tcgen05_mma_sp_tensor_mxf4_block_scale
791 nvvm_tcgen05_mma_sp_shared_mxf4_block_scale;
792 }
else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
795 nvvm_tcgen05_mma_sp_tensor_mxf4_block_scale_block32
797 nvvm_tcgen05_mma_sp_shared_mxf4_block_scale_block32;
799 }
else if (kind == NVVM::Tcgen05MMAKind::MXF4NVF4) {
800 if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
803 nvvm_tcgen05_mma_sp_tensor_mxf4nvf4_block_scale_block32
805 nvvm_tcgen05_mma_sp_shared_mxf4nvf4_block_scale_block32;
807 }
else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK16) {
810 nvvm_tcgen05_mma_sp_tensor_mxf4nvf4_block_scale_block16
812 nvvm_tcgen05_mma_sp_shared_mxf4nvf4_block_scale_block16;
815 llvm_unreachable(
"Invalid tcgen05.mma.sp.block_scale attributes");
821LogicalResult Tcgen05MMASparseBlockScaleOp::verify() {
823 getBlockScale(), getLoc());
833 auto thisOp = cast<NVVM::Tcgen05MMAWsOp>(op);
836 args.push_back(mt.
lookupValue(thisOp.getMatrixD()));
839 bool isATensor = isa<llvm::PointerType>(
A->getType());
842 args.push_back(mt.
lookupValue(thisOp.getMatrixB()));
844 args.push_back(mt.
lookupValue(thisOp.getEnableInputD()));
850 ID = isATensor ? llvm::Intrinsic::nvvm_tcgen05_mma_ws_tensor_zero_col_mask
851 : llvm::Intrinsic::nvvm_tcgen05_mma_ws_shared_zero_col_mask;
853 ID = isATensor ? llvm::Intrinsic::nvvm_tcgen05_mma_ws_tensor
854 : llvm::Intrinsic::nvvm_tcgen05_mma_ws_shared;
856 args.push_back(builder.getInt32(
859 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorBBuffer())));
861 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOp())));
873 auto thisOp = cast<NVVM::Tcgen05MMAWsSparseOp>(op);
876 args.push_back(mt.
lookupValue(thisOp.getMatrixD()));
879 bool isATensor = isa<llvm::PointerType>(
A->getType());
882 args.push_back(mt.
lookupValue(thisOp.getMatrixB()));
884 args.push_back(mt.
lookupValue(thisOp.getEnableInputD()));
885 args.push_back(mt.
lookupValue(thisOp.getSparseMetadata()));
892 ? llvm::Intrinsic::nvvm_tcgen05_mma_ws_sp_tensor_zero_col_mask
893 : llvm::Intrinsic::nvvm_tcgen05_mma_ws_sp_shared_zero_col_mask;
895 ID = isATensor ? llvm::Intrinsic::nvvm_tcgen05_mma_ws_sp_tensor
896 : llvm::Intrinsic::nvvm_tcgen05_mma_ws_sp_shared;
898 args.push_back(builder.getInt32(
901 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorBBuffer())));
903 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOp())));
914 auto thisOp = cast<Tcgen05MMADecompressBOp>(op);
917 args.push_back(mt.
lookupValue(thisOp.getMatrixD()));
920 const bool isATensor = isa<llvm::PointerType>(
A->getType());
923 args.push_back(mt.
lookupValue(thisOp.getMatrixB()));
925 args.push_back(mt.
lookupValue(thisOp.getEnableInputD()));
926 args.push_back(mt.
lookupValue(thisOp.getDecompressBMetadata()));
928 llvm::Value *DisableOutputLane =
930 bool hasDisableOutputLane = DisableOutputLane !=
nullptr;
932 NVVM::CTAGroupKind ctaGroup = thisOp.getCtaGroup();
934 using namespace llvm::Intrinsic;
935 ID intrinsicID = not_intrinsic;
937 if (hasDisableOutputLane) {
938 if (ctaGroup == NVVM::CTAGroupKind::CTA_1) {
941 ? nvvm_tcgen05_mma_tensor_f8f6f4_disable_output_lane_cg1_decompress_b
942 : nvvm_tcgen05_mma_shared_f8f6f4_disable_output_lane_cg1_decompress_b;
943 }
else if (ctaGroup == NVVM::CTAGroupKind::CTA_2) {
946 ? nvvm_tcgen05_mma_tensor_f8f6f4_disable_output_lane_cg2_decompress_b
947 : nvvm_tcgen05_mma_shared_f8f6f4_disable_output_lane_cg2_decompress_b;
949 llvm_unreachable(
"Unknown ctaGroup for tcgen05.mma.decompress_b");
952 intrinsicID = isATensor ? nvvm_tcgen05_mma_tensor_f8f6f4_decompress_b
953 : nvvm_tcgen05_mma_shared_f8f6f4_decompress_b;
956 assert(intrinsicID != not_intrinsic &&
957 "Invalid intrinsic for Tcgen05MMADecompressBOp.");
959 if (hasDisableOutputLane)
960 args.push_back(DisableOutputLane);
966 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOpA())));
968 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOpB())));
970 return {intrinsicID, args};
973LogicalResult Tcgen05MMADecompressBOp::verify() {
974 mlir::Value disableOutputLane = getDisableOutputLane();
976 if (disableOutputLane) {
977 NVVM::CTAGroupKind ctaGroup = getCtaGroup();
979 mlir::VectorType disableOutputLaneType =
980 cast<mlir::VectorType>(disableOutputLane.
getType());
981 if ((ctaGroup == NVVM::CTAGroupKind::CTA_1 &&
982 disableOutputLaneType.getNumElements() != 4) ||
983 (ctaGroup == NVVM::CTAGroupKind::CTA_2 &&
984 disableOutputLaneType.getNumElements() != 8))
985 return emitOpError() <<
"Disable Output Lane of length "
986 << disableOutputLaneType.getNumElements()
987 <<
" is incompatible with CtaGroupAttr";
999 auto thisOp = cast<Tcgen05MMABlockScaleDecompressBOp>(op);
1002 args.push_back(mt.
lookupValue(thisOp.getMatrixD()));
1005 const bool isATensor = isa<llvm::PointerType>(
A->getType());
1008 args.push_back(mt.
lookupValue(thisOp.getMatrixB()));
1009 args.push_back(mt.
lookupValue(thisOp.getIdesc()));
1010 args.push_back(mt.
lookupValue(thisOp.getEnableInputD()));
1011 args.push_back(mt.
lookupValue(thisOp.getScaleA()));
1012 args.push_back(mt.
lookupValue(thisOp.getScaleB()));
1013 args.push_back(mt.
lookupValue(thisOp.getDecompressBMetadata()));
1014 args.push_back(builder.getInt32(
1017 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOpA())));
1019 builder.getInt32(
static_cast<unsigned>(thisOp.getCollectorOpB())));
1021 llvm::Intrinsic::ID intrinsicID =
1024 nvvm_tcgen05_mma_tensor_mxf8f6f4_block_scale_block32_decompress_b
1026 nvvm_tcgen05_mma_shared_mxf8f6f4_block_scale_block32_decompress_b;
1028 return {intrinsicID, args};
1035#define TCGEN05LDRED(SHAPE, NUM, TYPE) \
1036 llvm::Intrinsic::nvvm_tcgen05_ld_red_##SHAPE##_##NUM##_##TYPE
1040 auto thisOp = cast<NVVM::Tcgen05LdRedOp>(op);
1043 mlir::VectorType VecResTy =
1044 cast<mlir::VectorType>(thisOp.getData().getType());
1045 unsigned Num = VecResTy.getNumElements();
1046 bool IsFloat = thisOp.getRedVal().getType().isF32();
1048 llvm::Intrinsic::ID Shape32x32b[][2] = {
1059 llvm::Intrinsic::ID Shape16x32bx2[][2] = {
1070 NVVM::Tcgen05LdStShape
shape = thisOp.getShape();
1071 unsigned ID = [&]() {
1074 unsigned idx = std::log2(Num);
1076 case NVVM::Tcgen05LdStShape::SHAPE_32X32B:
1077 return Shape32x32b[idx][IsFloat];
1078 case NVVM::Tcgen05LdStShape::SHAPE_16X32BX2:
1079 return Shape16x32bx2[idx][IsFloat];
1081 llvm_unreachable(
"unhandled tcgen05.ld lowering");
1087 if (
shape == NVVM::Tcgen05LdStShape::SHAPE_16X32BX2)
1088 args.push_back(mt.
lookupValue(thisOp.getOffset()));
1091 builder.getInt32(thisOp.getOp() == NVVM::ReductionKind::MIN ? 0 : 1));
1094 args.push_back(builder.getInt1(
static_cast<unsigned>(thisOp.getAbs())));
1095 args.push_back(builder.getInt1(
static_cast<unsigned>(thisOp.getNan())));
1100LogicalResult Tcgen05LdRedOp::verify() {
1101 VectorType data = cast<VectorType>(getData().
getType());
1102 Type redVal = getRedVal().getType();
1104 if (data.getElementType() != redVal)
1106 "type of reduction value and element type of vector data should match");
1108 if (getOp() != NVVM::ReductionKind::MIN &&
1109 getOp() != NVVM::ReductionKind::MAX)
1110 return emitError(
"only min and max reduction kinds are supported");
1112 if (redVal.
isInteger() && (getAbs() || getNan())) {
1113 return emitError(
"abs or nan is only applicable for f32 type");
1125LogicalResult Tcgen05LdRedSPCompressOp::verify() {
1126 unsigned num = 2 * cast<VectorType>(getData().
getType()).getNumElements();
1127 unsigned expectedRegs = llvm::divideCeil(num, 32);
1128 auto metadataTy = dyn_cast<VectorType>(getMetadata().
getType());
1129 unsigned numRegs = metadataTy ? metadataTy.getNumElements() : 1;
1130 if (numRegs != expectedRegs)
1131 return emitOpError(
"expects metadata to occupy ")
1132 << expectedRegs <<
" 32-bit register(s) for repeat factor x" << num;
1135 if (getNan() && !getRedVal())
1136 return emitOpError(
"nan requires the reduction result redVal");
1142 auto thisOp = cast<NVVM::Tcgen05LdRedSPCompressOp>(op);
1143 llvm::Intrinsic::ID
id = llvm::Intrinsic::nvvm_tcgen05_ld_spcompress_32x32b;
1148 builder.getInt32(thisOp.getOp() == NVVM::ReductionKind::MAX ? 1 : 0),
1149 builder.getInt1(thisOp.getAbs())};
1150 if (thisOp.getRedVal()) {
1151 id = llvm::Intrinsic::nvvm_tcgen05_ld_red_spcompress_32x32b;
1152 args.push_back(builder.getInt1(thisOp.getNan()));
1158#define GET_OP_CLASSES
1159#include "mlir/Dialect/LLVMIR/NVVMTcgen05Ops.cpp.inc"
#define GET_TCGEN05_CP_ID(shape_mc, src_fmt, is_2cta)
static LogicalResult verifyTcgen05MMAOp(bool isATensor, mlir::Value disableOutputLane, NVVM::CTAGroupKind ctaGroup, bool hasAShift, NVVM::Tcgen05MMACollectorOp collectorOp, Location loc)
static llvm::nvvm::CTAGroupKind getNVVMCtaGroupKind(NVVM::CTAGroupKind ctaGroup)
static llvm::nvvm::Tcgen05MMAKind getNVVMTcgen05MMAKind(NVVM::Tcgen05MMAKind kind)
static LogicalResult verifyTcgen05MMABlockScaleOp(NVVM::Tcgen05MMACollectorOp collectorOp, NVVM::Tcgen05MMAKind kind, NVVM::Tcgen05MMABlockScale blockScale, Location loc)
static llvm::Value * packValInto64Bits(llvm::IRBuilderBase &builder, llvm::Value *result, llvm::Value *field, unsigned sizeInBits, unsigned start)
Packs the given field into the result.
#define TCGEN05LDRED(SHAPE, NUM, TYPE)
static unsigned isValidVectorLength(NVVM::Tcgen05LdStShape shape, unsigned vecLen)
static ArrayRef< int64_t > getShape(Type type)
Returns the shape of the given type.
Implementation class for module translation.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
void mapValue(Value mlir, llvm::Value *llvm)
Stores the mapping between an MLIR value and its LLVM IR counterpart.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
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...
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.
constexpr unsigned notIntrinsic
The ID of no intrinsic, used in the intrinsic tables of the ops.
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.