MLIR 24.0.0git
NVVMTcgen05Ops.cpp
Go to the documentation of this file.
1//===- NVVMTcgen05Ops.cpp - NVVM dialect tcgen05 ops ----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ops of op partition `Tcgen05` of the NVVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
15#include "IR/NVVMOps.h"
17#include "mlir/IR/Diagnostics.h"
18#include "mlir/IR/Operation.h"
19#include "mlir/IR/Types.h"
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"
26
27#include <array>
28#include <cassert>
29#include <cmath>
30
31using namespace mlir;
32using namespace mlir::NVVM;
33
34//===----------------------------------------------------------------------===//
35// Helper/Utility methods
36//===----------------------------------------------------------------------===//
37
38// Helper method to convert CtaGroupKind in NVVM Dialect to CtaGroupKind in LLVM
39static llvm::nvvm::CTAGroupKind
40getNVVMCtaGroupKind(NVVM::CTAGroupKind ctaGroup) {
41 switch (ctaGroup) {
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;
46 }
47 llvm_unreachable("unsupported cta_group value");
48}
49
50//===----------------------------------------------------------------------===//
51// Verifier methods
52//===----------------------------------------------------------------------===//
53
54LogicalResult NVVM::Tcgen05CpOp::verify() {
55 auto mc = getMulticast();
56
57 using SH = Tcgen05CpShape;
58 using MC = Tcgen05CpMulticast;
59 switch (getShape()) {
60 case SH::SHAPE_128x256b:
61 case SH::SHAPE_128x128b:
62 case SH::SHAPE_4x256b:
63 if (mc != MC::NONE)
64 return emitError("Invalid multicast type for tcgen05.cp Op");
65 break;
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");
70 break;
71 case SH::SHAPE_32x128b:
72 if (mc != MC::WARPX4)
73 return emitError(
74 "Shape 32x128b requires multicast warpx4 for tcgen05.cp Op");
75 break;
76 }
77 return success();
78}
79
80/// Packs the given `field` into the `result`.
81/// The `result` is 64-bits and each `field` can be 32-bits or narrower.
82static llvm::Value *
83packValInto64Bits(llvm::IRBuilderBase &builder,
84 llvm::Value *result, // the `result` (unset bits are zero)
85 llvm::Value *field, // `field` to pack into `result`
86 unsigned sizeInBits, // Size of `field` in bits
87 unsigned start) { // Starting bit within `result`
88 field = builder.CreateZExtOrBitCast(field, builder.getInt32Ty());
89
90 unsigned mask = (sizeInBits < 32 ? ((1u << sizeInBits) - 1) : 0xffffffffu);
91 if (mask != 0xffffffffu)
92 field = builder.CreateAnd(field, builder.getInt32(mask));
93
94 field = builder.CreateZExtOrBitCast(field, builder.getInt64Ty());
95 field = builder.CreateShl(field, start);
96
97 return builder.CreateOr(result, field);
98}
99
100void Tcgen05MmaSmemDescOp::createSmemDescriptor(Operation &op,
102 llvm::IRBuilderBase &builder) {
103 auto thisOp = cast<NVVM::Tcgen05MmaSmemDescOp>(op);
104 llvm::Value *smemDesc = builder.getInt64(0);
105
106 smemDesc = packValInto64Bits(builder, smemDesc,
107 mt.lookupValue(thisOp.getStartAddr()), 14, 0);
108 smemDesc = packValInto64Bits(
109 builder, smemDesc, mt.lookupValue(thisOp.getLeadingDimOffset()), 14, 16);
110 smemDesc = packValInto64Bits(
111 builder, smemDesc, mt.lookupValue(thisOp.getStrideDimOffset()), 14, 32);
112
113 smemDesc = packValInto64Bits(builder, smemDesc, builder.getInt32(1), 3, 46);
114 smemDesc = packValInto64Bits(builder, smemDesc,
115 mt.lookupValue(thisOp.getBaseOffset()), 3, 49);
116 smemDesc = packValInto64Bits(
117 builder, smemDesc, mt.lookupValue(thisOp.getLeadingDimMode()), 1, 52);
118 smemDesc = packValInto64Bits(builder, smemDesc,
119 mt.lookupValue(thisOp.getSwizzleMode()), 3, 61);
120
121 mt.mapValue(thisOp.getRes()) = smemDesc;
122}
123
124//===----------------------------------------------------------------------===//
125// getIntrinsicID/getIntrinsicIDAndArgs methods
126//===----------------------------------------------------------------------===//
127
128mlir::NVVM::IDArgPair Tcgen05AllocOp::getIntrinsicIDAndArgs(
129 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
130 auto curOp = cast<NVVM::Tcgen05AllocOp>(op);
131 bool is2CTAMode = curOp.getGroup() == CTAGroupKind::CTA_2;
132
133 llvm::Intrinsic::ID id = is2CTAMode ? llvm::Intrinsic::nvvm_tcgen05_alloc_cg2
134 : llvm::Intrinsic::nvvm_tcgen05_alloc_cg1;
135
136 return {id,
137 {mt.lookupValue(curOp.getAddr()), mt.lookupValue(curOp.getNCols()),
138 builder.getInt1(curOp.getIsExclusive())}};
139}
140
141mlir::NVVM::IDArgPair Tcgen05DeallocOp::getIntrinsicIDAndArgs(
142 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
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;
147
148 return {id,
149 {mt.lookupValue(curOp.getTaddr()), mt.lookupValue(curOp.getNCols()),
150 builder.getInt1(curOp.getIsExclusive())}};
151}
152
153llvm::Intrinsic::ID
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);
163
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,
170 };
171
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,
177 };
178
179 ID id = hasMulticast ? multicastIDs[index] : IDs[index];
180 // Fill the Intrinsic Args
181 args.push_back(mt.lookupValue(curOp.getAddr()));
182 if (hasMulticast)
183 args.push_back(mt.lookupValue(curOp.getMulticastMask()));
184
185 return id;
186}
187
188#define TCGEN05_CP_IMPL(shape_mc, src_fmt, cg) \
189 llvm::Intrinsic::nvvm_tcgen05_cp##shape_mc##src_fmt##cg
190
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)
194
195#define GET_TCGEN05_CP_ID(shape_mc, src_fmt, is_2cta) \
196 [&]() -> auto { \
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); \
202 }()
203
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();
209
210 switch (curOp.getShape()) {
211 case Tcgen05CpShape::SHAPE_128x256b:
212 return GET_TCGEN05_CP_ID(_128x256b, srcFmt, is2CTA);
213 case Tcgen05CpShape::SHAPE_128x128b:
214 return GET_TCGEN05_CP_ID(_128x128b, srcFmt, is2CTA);
215 case Tcgen05CpShape::SHAPE_4x256b:
216 return GET_TCGEN05_CP_ID(_4x256b, srcFmt, is2CTA);
217 case Tcgen05CpShape::SHAPE_32x128b:
218 return GET_TCGEN05_CP_ID(_32x128b_warpx4, srcFmt, is2CTA);
219 case Tcgen05CpShape::SHAPE_64x128b:
220 return (mc == Tcgen05CpMulticast::WARPX2_01_23)
221 ? GET_TCGEN05_CP_ID(_64x128b_warpx2_01_23, srcFmt, is2CTA)
222 : GET_TCGEN05_CP_ID(_64x128b_warpx2_02_13, srcFmt, is2CTA);
223 }
224 llvm_unreachable("Invalid shape in tcgen05 cp Op");
225}
226
227// Returns the valid vector length for a given shape and vector length, the
228// function models the table mentioned in the tcgen05.{ld, st} Op description
229static unsigned isValidVectorLength(NVVM::Tcgen05LdStShape shape,
230 unsigned vecLen) {
231 if (shape == NVVM::Tcgen05LdStShape::SHAPE_16X128B)
232 return vecLen >= 2;
233 if (shape == NVVM::Tcgen05LdStShape::SHAPE_16X256B)
234 return vecLen >= 4;
235 return true;
236}
237
238LogicalResult Tcgen05LdOp::verify() {
239 LogicalResult result = success();
240 if (getShape() == NVVM::Tcgen05LdStShape::SHAPE_16X32BX2 && !getOffset())
241 result = emitError("shape 16x32bx2 requires offset argument");
242
243 if (getShape() != NVVM::Tcgen05LdStShape::SHAPE_16X32BX2 && getOffset())
244 result = emitError("offset argument is only supported for shape 16x32bx2");
245
246 auto resTy = getRes().getType();
247 unsigned resLen = isa<VectorType>(resTy)
248 ? llvm::cast<VectorType>(resTy).getNumElements()
249 : 1;
250 if (!isValidVectorLength(getShape(), resLen))
251 result = emitError(llvm::formatv("invalid result type length {0} for shape "
252 "{1} in tcgen05.ld Op",
253 resLen, stringifyEnum(getShape())));
254
255 return result;
256}
257
258LogicalResult Tcgen05StOp::verify() {
259 LogicalResult result = success();
260 if (getShape() == NVVM::Tcgen05LdStShape::SHAPE_16X32BX2 && !getOffset())
261 result = emitError("shape 16x32bx2 requires offset argument");
262
263 auto valTy = getVal().getType();
264 unsigned valLen = isa<VectorType>(valTy)
265 ? llvm::cast<VectorType>(valTy).getNumElements()
266 : 1;
267 if (!isValidVectorLength(getShape(), valLen))
268 result = emitError(llvm::formatv("invalid input length {0} for shape "
269 "{1} in tcgen05.st Op",
270 valLen, stringifyEnum(getShape())));
271
272 return result;
273}
274
275//===----------------------------------------------------------------------===//
276// NVVM tcgen05.mma functions
277//===----------------------------------------------------------------------===//
278
279static llvm::nvvm::Tcgen05MMAKind
280getNVVMTcgen05MMAKind(NVVM::Tcgen05MMAKind kind) {
281 switch (kind) {
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:
295 // Block-scale kinds are handled by the tcgen05.mma.block_scale
296 // lowering paths and are not valid for plain tcgen05.mma.
297 llvm_unreachable("Unsupported tcgen05.mma kind");
298 }
299 llvm_unreachable("Invalid tcgen05.mma kind");
300}
301
303Tcgen05MMAOp::getIntrinsicIDAndArgs(Operation &op, LLVM::ModuleTranslation &mt,
304 llvm::IRBuilderBase &builder) {
305
306 auto thisOp = cast<NVVM::Tcgen05MMAOp>(op);
308
309 args.push_back(mt.lookupValue(thisOp.getMatrixD()));
310
311 llvm::Value *A = mt.lookupValue(thisOp.getMatrixA());
312 const bool isATensor = isa<llvm::PointerType>(A->getType());
313 args.push_back(A);
314
315 args.push_back(mt.lookupValue(thisOp.getMatrixB()));
316 args.push_back(mt.lookupValue(thisOp.getIdesc()));
317 args.push_back(mt.lookupValue(thisOp.getEnableInputD()));
318
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>;
324
325 // [hasDisableOutputLane][hasScaleInputD][isATensor][CtaGroup][EnableAShift]
326 static constexpr HasDisableOutputLaneArray tcgen05MMAIDs = {
327 { // without diable output lane
328 {{// without scale input D
329 {{
330 // shared
331 {{// cg1
332 {llvm::Intrinsic::nvvm_tcgen05_mma_shared, notIntrinsic},
333 // cg2
334 {llvm::Intrinsic::nvvm_tcgen05_mma_shared, notIntrinsic}}},
335 {{// tensor
336 {
337 // cg1
338 llvm::Intrinsic::nvvm_tcgen05_mma_tensor,
339 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_ashift,
340 },
341 {
342 // cg2
343 llvm::Intrinsic::nvvm_tcgen05_mma_tensor,
344 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_ashift,
345 }}},
346 }},
347 // with scale input D
348 {{ // shared
349 {{// cg1
350 {llvm::Intrinsic::nvvm_tcgen05_mma_shared_scale_d, notIntrinsic},
351 // cg2
352 {llvm::Intrinsic::nvvm_tcgen05_mma_shared_scale_d, notIntrinsic}}},
353 {{// tensor
354 {
355 // cg1
356 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_scale_d,
357 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_scale_d_ashift,
358 },
359 {
360 // cg2
361 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_scale_d,
362 llvm::Intrinsic::nvvm_tcgen05_mma_tensor_scale_d_ashift,
363 }}}}}}},
364 // with disable output lane
365 {{ // without scale input D
366 {{ // shared
367 {{// cg1
368 {llvm::Intrinsic::nvvm_tcgen05_mma_shared_disable_output_lane_cg1,
370 // cg2
371 {llvm::Intrinsic::nvvm_tcgen05_mma_shared_disable_output_lane_cg2,
372 notIntrinsic}}},
373 {{// cg1
374 {
375 llvm::Intrinsic::
376 nvvm_tcgen05_mma_tensor_disable_output_lane_cg1,
377 llvm::Intrinsic::
378 nvvm_tcgen05_mma_tensor_disable_output_lane_cg1_ashift,
379 },
380 // cg2
381 {
382 llvm::Intrinsic::
383 nvvm_tcgen05_mma_tensor_disable_output_lane_cg2,
384 llvm::Intrinsic::
385 nvvm_tcgen05_mma_tensor_disable_output_lane_cg2_ashift,
386 }}}}},
387 // with scale input D
388 {{ // shared
389 {{// cg1
390 {llvm::Intrinsic::
391 nvvm_tcgen05_mma_shared_scale_d_disable_output_lane_cg1,
393 // cg2
394 {llvm::Intrinsic::
395 nvvm_tcgen05_mma_shared_scale_d_disable_output_lane_cg2,
396 notIntrinsic}}},
397 // tensor
398 {{// cg1
399 {llvm::Intrinsic::
400 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg1,
401 llvm::Intrinsic::
402 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg1_ashift},
403 // cg2
404 {
405 llvm::Intrinsic::
406 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg2,
407 llvm::Intrinsic::
408 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg2_ashift,
409 }}}}}}}}};
410
411 llvm::Value *ScaleInputD = mt.lookupValue(thisOp.getScaleInputD());
412 bool hasScaleInputD = ScaleInputD != nullptr;
413
414 llvm::Value *DisableOutputLane =
415 mt.lookupValue(thisOp.getDisableOutputLane());
416 bool hasDisableOutputLane = DisableOutputLane != nullptr;
417
418 const unsigned ctaGroup =
419 static_cast<unsigned>(getNVVMCtaGroupKind(thisOp.getCtaGroup()));
420
421 llvm::Intrinsic::ID ID =
422 tcgen05MMAIDs[hasDisableOutputLane][hasScaleInputD][isATensor]
423 [ctaGroup - 1][thisOp.getAShift()];
424
425 assert(ID != notIntrinsic && "Invalid intrinsic for Tcgen05MMAOp.");
426
427 if (hasScaleInputD)
428 args.push_back(ScaleInputD);
429
430 if (hasDisableOutputLane)
431 args.push_back(DisableOutputLane);
432
433 args.push_back(builder.getInt32(
434 static_cast<unsigned>(getNVVMTcgen05MMAKind(thisOp.getKind()))));
435
436 if (!hasDisableOutputLane)
437 args.push_back(builder.getInt32(ctaGroup));
438
439 args.push_back(
440 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOp())));
441
442 args.push_back(
443 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOpB())));
444
445 return {ID, args};
446}
447
448static LogicalResult
449verifyTcgen05MMAOp(bool isATensor, mlir::Value disableOutputLane,
450 NVVM::CTAGroupKind ctaGroup, bool hasAShift,
451 NVVM::Tcgen05MMACollectorOp collectorOp, Location loc) {
452
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";
463 }
464
465 if (hasAShift && !isATensor)
466 return emitError(
467 loc, "A-shift can be applied only when matrix A is in tensor memory");
468
469 if (hasAShift == true && (collectorOp == Tcgen05MMACollectorOp::FILL ||
470 collectorOp == Tcgen05MMACollectorOp::USE))
471 return emitError(
472 loc, "Cannot use collector buffer operation fill or use with ashift");
473
474 return success();
475}
476
477LogicalResult Tcgen05MMAOp::verify() {
478 return verifyTcgen05MMAOp(isa<LLVM::LLVMPointerType>(getMatrixA().getType()),
479 getDisableOutputLane(), getCtaGroup(), getAShift(),
480 getCollectorOp(), getLoc());
481}
482
483//===----------------------------------------------------------------------===//
484// NVVM tcgen05.mma.sp functions
485//===----------------------------------------------------------------------===//
486
487mlir::NVVM::IDArgPair Tcgen05MMASparseOp::getIntrinsicIDAndArgs(
488 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
489
490 auto thisOp = cast<NVVM::Tcgen05MMASparseOp>(op);
492
493 args.push_back(mt.lookupValue(thisOp.getMatrixD()));
494
495 llvm::Value *A = mt.lookupValue(thisOp.getMatrixA());
496 bool isATensor = isa<llvm::PointerType>(A->getType());
497 args.push_back(A);
498
499 args.push_back(mt.lookupValue(thisOp.getMatrixB()));
500 args.push_back(mt.lookupValue(thisOp.getIdesc()));
501 args.push_back(mt.lookupValue(thisOp.getEnableInputD()));
502 args.push_back(mt.lookupValue(thisOp.getSparseMetadata()));
503
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>;
509
510 // [hasDisableOutputLane][hasScaleInputD][isATensor][CtaGroup][EnableAShift]
511 static constexpr HasDisableOutputLaneArray tcgen05MMASparseIDs = {
512 { // without diable output lane
513 {{// without scale input D
514 {{
515 // shared
516 {{// cg1
517 {llvm::Intrinsic::nvvm_tcgen05_mma_sp_shared, notIntrinsic},
518 // cg2
519 {llvm::Intrinsic::nvvm_tcgen05_mma_sp_shared, notIntrinsic}}},
520 {{// tensor
521 {
522 // cg1
523 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor,
524 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_ashift,
525 },
526 {
527 // cg2
528 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor,
529 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_ashift,
530 }}},
531 }},
532 // with scale input D
533 {{ // shared
534 {{// cg1
535 {llvm::Intrinsic::nvvm_tcgen05_mma_sp_shared_scale_d,
537 // cg2
538 {llvm::Intrinsic::nvvm_tcgen05_mma_sp_shared_scale_d,
539 notIntrinsic}}},
540 {{// tensor
541 {
542 // cg1
543 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d,
544 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d_ashift,
545 },
546 {
547 // cg2
548 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d,
549 llvm::Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d_ashift,
550 }}}}}}},
551 // with disable output lane
552 {{ // without scale input D
553 {{ // shared
554 {{// cg1
555 {llvm::Intrinsic::
556 nvvm_tcgen05_mma_sp_shared_disable_output_lane_cg1,
558 // cg2
559 {llvm::Intrinsic::
560 nvvm_tcgen05_mma_sp_shared_disable_output_lane_cg2,
561 notIntrinsic}}},
562 {{// cg1
563 {
564 llvm::Intrinsic::
565 nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg1,
566 llvm::Intrinsic::
567 nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg1_ashift,
568 },
569 // cg2
570 {
571 llvm::Intrinsic::
572 nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg2,
573 llvm::Intrinsic::
574 nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg2_ashift,
575 }}}}},
576 // with scale input D
577 {{ // shared
578 {{// cg1
579 {llvm::Intrinsic::
580 nvvm_tcgen05_mma_sp_shared_scale_d_disable_output_lane_cg1,
582 // cg2
583 {llvm::Intrinsic::
584 nvvm_tcgen05_mma_sp_shared_scale_d_disable_output_lane_cg2,
585 notIntrinsic}}},
586 // tensor
587 {{// cg1
588 {llvm::Intrinsic::
589 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg1,
590 llvm::Intrinsic::
591 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg1_ashift},
592 // cg2
593 {
594 llvm::Intrinsic::
595 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg2,
596 llvm::Intrinsic::
597 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg2_ashift,
598 }}}}}}}}};
599
600 llvm::Value *ScaleInputD = mt.lookupValue(thisOp.getScaleInputD());
601 bool hasScaleInputD = ScaleInputD != nullptr;
602
603 llvm::Value *DisableOutputLane =
604 mt.lookupValue(thisOp.getDisableOutputLane());
605 bool hasDisableOutputLane = DisableOutputLane != nullptr;
606
607 unsigned ctaGroup =
608 static_cast<unsigned>(getNVVMCtaGroupKind(thisOp.getCtaGroup()));
609
610 llvm::Intrinsic::ID ID =
611 tcgen05MMASparseIDs[hasDisableOutputLane][hasScaleInputD][isATensor]
612 [ctaGroup - 1][thisOp.getAShift()];
613
614 assert(ID != notIntrinsic && "Invalid intrinsic for Tcgen05MMASparseOp.");
615
616 if (hasScaleInputD)
617 args.push_back(ScaleInputD);
618
619 if (hasDisableOutputLane)
620 args.push_back(DisableOutputLane);
621
622 args.push_back(builder.getInt32(
623 static_cast<unsigned>(getNVVMTcgen05MMAKind(thisOp.getKind()))));
624
625 if (!hasDisableOutputLane)
626 args.push_back(builder.getInt32(ctaGroup));
627
628 args.push_back(
629 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOp())));
630
631 args.push_back(
632 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOpB())));
633
634 return {ID, args};
635}
636
637LogicalResult Tcgen05MMASparseOp::verify() {
638 return verifyTcgen05MMAOp(isa<LLVM::LLVMPointerType>(getMatrixA().getType()),
639 getDisableOutputLane(), getCtaGroup(), getAShift(),
640 getCollectorOp(), getLoc());
641}
642
643//===----------------------------------------------------------------------===//
644// NVVM tcgen05.mma.block_scale functions
645//===----------------------------------------------------------------------===//
646
647mlir::NVVM::IDArgPair Tcgen05MMABlockScaleOp::getIntrinsicIDAndArgs(
648 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
649
650 auto thisOp = cast<NVVM::Tcgen05MMABlockScaleOp>(op);
652
653 args.push_back(mt.lookupValue(thisOp.getMatrixD()));
654
655 llvm::Value *A = mt.lookupValue(thisOp.getMatrixA());
656 bool isATensor = isa<llvm::PointerType>(A->getType());
657 args.push_back(A);
658
659 args.push_back(mt.lookupValue(thisOp.getMatrixB()));
660 args.push_back(mt.lookupValue(thisOp.getIdesc()));
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(
665 static_cast<unsigned>(getNVVMCtaGroupKind(thisOp.getCtaGroup()))));
666 args.push_back(
667 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOp())));
668 args.push_back(
669 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOpB())));
670
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
678 : llvm::Intrinsic::
679 nvvm_tcgen05_mma_shared_mxf8f6f4_block_scale;
680 } else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
681 return isATensor
682 ? llvm::Intrinsic::
683 nvvm_tcgen05_mma_tensor_mxf8f6f4_block_scale_block32
684 : llvm::Intrinsic::
685 nvvm_tcgen05_mma_shared_mxf8f6f4_block_scale_block32;
686 }
687 } else if (kind == NVVM::Tcgen05MMAKind::MXF4) {
688 if (blockScale == NVVM::Tcgen05MMABlockScale::DEFAULT) {
689 return isATensor
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
695 : llvm::Intrinsic::
696 nvvm_tcgen05_mma_shared_mxf4_block_scale_block32;
697 }
698 } else if (kind == NVVM::Tcgen05MMAKind::MXF4NVF4) {
699 if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
700 return isATensor
701 ? llvm::Intrinsic::
702 nvvm_tcgen05_mma_tensor_mxf4nvf4_block_scale_block32
703 : llvm::Intrinsic::
704 nvvm_tcgen05_mma_shared_mxf4nvf4_block_scale_block32;
705
706 } else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK16) {
707 return isATensor
708 ? llvm::Intrinsic::
709 nvvm_tcgen05_mma_tensor_mxf4nvf4_block_scale_block16
710 : llvm::Intrinsic::
711 nvvm_tcgen05_mma_shared_mxf4nvf4_block_scale_block16;
712 }
713 }
714 llvm_unreachable("Invalid tcgen05.mma.block_scale attributes");
715 }();
716
717 return {ID, args};
718}
719
720static LogicalResult verifyTcgen05MMABlockScaleOp(
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");
726
727 if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK16 &&
728 kind != NVVM::Tcgen05MMAKind::MXF4NVF4)
729 return emitError(loc,
730 llvm::formatv("{} kind does not support block16 attribute",
731 stringifyEnum(kind)));
732
733 return success();
734}
735
736LogicalResult Tcgen05MMABlockScaleOp::verify() {
737 return verifyTcgen05MMABlockScaleOp(getCollectorOp(), getKind(),
738 getBlockScale(), getLoc());
739}
740
741//===----------------------------------------------------------------------===//
742// NVVM tcgen05.mma.sp.block_scale functions
743//===----------------------------------------------------------------------===//
744
745mlir::NVVM::IDArgPair Tcgen05MMASparseBlockScaleOp::getIntrinsicIDAndArgs(
746 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
747
748 auto thisOp = cast<NVVM::Tcgen05MMASparseBlockScaleOp>(op);
750
751 args.push_back(mt.lookupValue(thisOp.getMatrixD()));
752
753 llvm::Value *A = mt.lookupValue(thisOp.getMatrixA());
754 bool isATensor = isa<llvm::PointerType>(A->getType());
755 args.push_back(A);
756
757 args.push_back(mt.lookupValue(thisOp.getMatrixB()));
758 args.push_back(mt.lookupValue(thisOp.getIdesc()));
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(
764 static_cast<unsigned>(getNVVMCtaGroupKind(thisOp.getCtaGroup()))));
765 args.push_back(
766 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOp())));
767 args.push_back(
768 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOpB())));
769
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
777 : llvm::Intrinsic::
778 nvvm_tcgen05_mma_sp_shared_mxf8f6f4_block_scale;
779 } else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
780 return isATensor
781 ? llvm::Intrinsic::
782 nvvm_tcgen05_mma_sp_tensor_mxf8f6f4_block_scale_block32
783 : llvm::Intrinsic::
784 nvvm_tcgen05_mma_sp_shared_mxf8f6f4_block_scale_block32;
785 }
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
790 : llvm::Intrinsic::
791 nvvm_tcgen05_mma_sp_shared_mxf4_block_scale;
792 } else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
793 return isATensor
794 ? llvm::Intrinsic::
795 nvvm_tcgen05_mma_sp_tensor_mxf4_block_scale_block32
796 : llvm::Intrinsic::
797 nvvm_tcgen05_mma_sp_shared_mxf4_block_scale_block32;
798 }
799 } else if (kind == NVVM::Tcgen05MMAKind::MXF4NVF4) {
800 if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK32) {
801 return isATensor
802 ? llvm::Intrinsic::
803 nvvm_tcgen05_mma_sp_tensor_mxf4nvf4_block_scale_block32
804 : llvm::Intrinsic::
805 nvvm_tcgen05_mma_sp_shared_mxf4nvf4_block_scale_block32;
806
807 } else if (blockScale == NVVM::Tcgen05MMABlockScale::BLOCK16) {
808 return isATensor
809 ? llvm::Intrinsic::
810 nvvm_tcgen05_mma_sp_tensor_mxf4nvf4_block_scale_block16
811 : llvm::Intrinsic::
812 nvvm_tcgen05_mma_sp_shared_mxf4nvf4_block_scale_block16;
813 }
814 }
815 llvm_unreachable("Invalid tcgen05.mma.sp.block_scale attributes");
816 }();
817
818 return {ID, args};
819}
820
821LogicalResult Tcgen05MMASparseBlockScaleOp::verify() {
822 return verifyTcgen05MMABlockScaleOp(getCollectorOp(), getKind(),
823 getBlockScale(), getLoc());
824}
825
826//===----------------------------------------------------------------------===//
827// NVVM tcgen05.mma.ws functions
828//===----------------------------------------------------------------------===//
829
830mlir::NVVM::IDArgPair Tcgen05MMAWsOp::getIntrinsicIDAndArgs(
831 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
832
833 auto thisOp = cast<NVVM::Tcgen05MMAWsOp>(op);
835
836 args.push_back(mt.lookupValue(thisOp.getMatrixD()));
837
838 llvm::Value *A = mt.lookupValue(thisOp.getMatrixA());
839 bool isATensor = isa<llvm::PointerType>(A->getType());
840 args.push_back(A);
841
842 args.push_back(mt.lookupValue(thisOp.getMatrixB()));
843 args.push_back(mt.lookupValue(thisOp.getIdesc()));
844 args.push_back(mt.lookupValue(thisOp.getEnableInputD()));
845
846 mlir::Value ZeroColMask = thisOp.getZeroColMask();
847 llvm::Intrinsic::ID ID = notIntrinsic;
848 if (ZeroColMask) {
849 args.push_back(mt.lookupValue(ZeroColMask));
850 ID = isATensor ? llvm::Intrinsic::nvvm_tcgen05_mma_ws_tensor_zero_col_mask
851 : llvm::Intrinsic::nvvm_tcgen05_mma_ws_shared_zero_col_mask;
852 } else
853 ID = isATensor ? llvm::Intrinsic::nvvm_tcgen05_mma_ws_tensor
854 : llvm::Intrinsic::nvvm_tcgen05_mma_ws_shared;
855
856 args.push_back(builder.getInt32(
857 static_cast<unsigned>(getNVVMTcgen05MMAKind(thisOp.getKind()))));
858 args.push_back(
859 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorBBuffer())));
860 args.push_back(
861 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOp())));
862
863 return {ID, args};
864}
865
866//===----------------------------------------------------------------------===//
867// NVVM tcgen05.mma.ws.sp functions
868//===----------------------------------------------------------------------===//
869
870mlir::NVVM::IDArgPair Tcgen05MMAWsSparseOp::getIntrinsicIDAndArgs(
871 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
872
873 auto thisOp = cast<NVVM::Tcgen05MMAWsSparseOp>(op);
875
876 args.push_back(mt.lookupValue(thisOp.getMatrixD()));
877
878 llvm::Value *A = mt.lookupValue(thisOp.getMatrixA());
879 bool isATensor = isa<llvm::PointerType>(A->getType());
880 args.push_back(A);
881
882 args.push_back(mt.lookupValue(thisOp.getMatrixB()));
883 args.push_back(mt.lookupValue(thisOp.getIdesc()));
884 args.push_back(mt.lookupValue(thisOp.getEnableInputD()));
885 args.push_back(mt.lookupValue(thisOp.getSparseMetadata()));
886
887 mlir::Value ZeroColMask = thisOp.getZeroColMask();
888 llvm::Intrinsic::ID ID = notIntrinsic;
889 if (ZeroColMask) {
890 args.push_back(mt.lookupValue(ZeroColMask));
891 ID = isATensor
892 ? llvm::Intrinsic::nvvm_tcgen05_mma_ws_sp_tensor_zero_col_mask
893 : llvm::Intrinsic::nvvm_tcgen05_mma_ws_sp_shared_zero_col_mask;
894 } else
895 ID = isATensor ? llvm::Intrinsic::nvvm_tcgen05_mma_ws_sp_tensor
896 : llvm::Intrinsic::nvvm_tcgen05_mma_ws_sp_shared;
897
898 args.push_back(builder.getInt32(
899 static_cast<unsigned>(getNVVMTcgen05MMAKind(thisOp.getKind()))));
900 args.push_back(
901 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorBBuffer())));
902 args.push_back(
903 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOp())));
904
905 return {ID, args};
906}
907
908//===----------------------------------------------------------------------===//
909// NVVM tcgen05.mma.decompress_b functions
910//===----------------------------------------------------------------------===//
911
912mlir::NVVM::IDArgPair Tcgen05MMADecompressBOp::getIntrinsicIDAndArgs(
913 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
914 auto thisOp = cast<Tcgen05MMADecompressBOp>(op);
916
917 args.push_back(mt.lookupValue(thisOp.getMatrixD()));
918
919 llvm::Value *A = mt.lookupValue(thisOp.getMatrixA());
920 const bool isATensor = isa<llvm::PointerType>(A->getType());
921 args.push_back(A);
922
923 args.push_back(mt.lookupValue(thisOp.getMatrixB()));
924 args.push_back(mt.lookupValue(thisOp.getIdesc()));
925 args.push_back(mt.lookupValue(thisOp.getEnableInputD()));
926 args.push_back(mt.lookupValue(thisOp.getDecompressBMetadata()));
927
928 llvm::Value *DisableOutputLane =
929 mt.lookupValue(thisOp.getDisableOutputLane());
930 bool hasDisableOutputLane = DisableOutputLane != nullptr;
931
932 NVVM::CTAGroupKind ctaGroup = thisOp.getCtaGroup();
933
934 using namespace llvm::Intrinsic;
935 ID intrinsicID = not_intrinsic;
936
937 if (hasDisableOutputLane) {
938 if (ctaGroup == NVVM::CTAGroupKind::CTA_1) {
939 intrinsicID =
940 isATensor
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) {
944 intrinsicID =
945 isATensor
946 ? nvvm_tcgen05_mma_tensor_f8f6f4_disable_output_lane_cg2_decompress_b
947 : nvvm_tcgen05_mma_shared_f8f6f4_disable_output_lane_cg2_decompress_b;
948 } else {
949 llvm_unreachable("Unknown ctaGroup for tcgen05.mma.decompress_b");
950 }
951 } else {
952 intrinsicID = isATensor ? nvvm_tcgen05_mma_tensor_f8f6f4_decompress_b
953 : nvvm_tcgen05_mma_shared_f8f6f4_decompress_b;
954 }
955
956 assert(intrinsicID != not_intrinsic &&
957 "Invalid intrinsic for Tcgen05MMADecompressBOp.");
958
959 if (hasDisableOutputLane)
960 args.push_back(DisableOutputLane);
961 else
962 args.push_back(
963 builder.getInt32(static_cast<unsigned>(getNVVMCtaGroupKind(ctaGroup))));
964
965 args.push_back(
966 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOpA())));
967 args.push_back(
968 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOpB())));
969
970 return {intrinsicID, args};
971}
972
973LogicalResult Tcgen05MMADecompressBOp::verify() {
974 mlir::Value disableOutputLane = getDisableOutputLane();
975
976 if (disableOutputLane) {
977 NVVM::CTAGroupKind ctaGroup = getCtaGroup();
978
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";
988 }
989
990 return success();
991}
992
993//===----------------------------------------------------------------------===//
994// NVVM tcgen05.mma.block_scale.decompress_b functions
995//===----------------------------------------------------------------------===//
996
997mlir::NVVM::IDArgPair Tcgen05MMABlockScaleDecompressBOp::getIntrinsicIDAndArgs(
998 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
999 auto thisOp = cast<Tcgen05MMABlockScaleDecompressBOp>(op);
1001
1002 args.push_back(mt.lookupValue(thisOp.getMatrixD()));
1003
1004 llvm::Value *A = mt.lookupValue(thisOp.getMatrixA());
1005 const bool isATensor = isa<llvm::PointerType>(A->getType());
1006 args.push_back(A);
1007
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(
1015 static_cast<unsigned>(getNVVMCtaGroupKind(thisOp.getCtaGroup()))));
1016 args.push_back(
1017 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOpA())));
1018 args.push_back(
1019 builder.getInt32(static_cast<unsigned>(thisOp.getCollectorOpB())));
1020
1021 llvm::Intrinsic::ID intrinsicID =
1022 isATensor
1023 ? llvm::Intrinsic::
1024 nvvm_tcgen05_mma_tensor_mxf8f6f4_block_scale_block32_decompress_b
1025 : llvm::Intrinsic::
1026 nvvm_tcgen05_mma_shared_mxf8f6f4_block_scale_block32_decompress_b;
1027
1028 return {intrinsicID, args};
1029}
1030
1031//===----------------------------------------------------------------------===//
1032// NVVM tcgen05.ld.red functions
1033//===----------------------------------------------------------------------===//
1034
1035#define TCGEN05LDRED(SHAPE, NUM, TYPE) \
1036 llvm::Intrinsic::nvvm_tcgen05_ld_red_##SHAPE##_##NUM##_##TYPE
1037
1038mlir::NVVM::IDArgPair NVVM::Tcgen05LdRedOp::getIntrinsicIDAndArgs(
1039 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1040 auto thisOp = cast<NVVM::Tcgen05LdRedOp>(op);
1042
1043 mlir::VectorType VecResTy =
1044 cast<mlir::VectorType>(thisOp.getData().getType());
1045 unsigned Num = VecResTy.getNumElements();
1046 bool IsFloat = thisOp.getRedVal().getType().isF32();
1047
1048 llvm::Intrinsic::ID Shape32x32b[][2] = {
1050 {TCGEN05LDRED(32x32b, x2, i32), TCGEN05LDRED(32x32b, x2, f32)},
1051 {TCGEN05LDRED(32x32b, x4, i32), TCGEN05LDRED(32x32b, x4, f32)},
1052 {TCGEN05LDRED(32x32b, x8, i32), TCGEN05LDRED(32x32b, x8, f32)},
1053 {TCGEN05LDRED(32x32b, x16, i32), TCGEN05LDRED(32x32b, x16, f32)},
1054 {TCGEN05LDRED(32x32b, x32, i32), TCGEN05LDRED(32x32b, x32, f32)},
1055 {TCGEN05LDRED(32x32b, x64, i32), TCGEN05LDRED(32x32b, x64, f32)},
1056 {TCGEN05LDRED(32x32b, x128, i32), TCGEN05LDRED(32x32b, x128, f32)},
1057 };
1058
1059 llvm::Intrinsic::ID Shape16x32bx2[][2] = {
1061 {TCGEN05LDRED(16x32bx2, x2, i32), TCGEN05LDRED(16x32bx2, x2, f32)},
1062 {TCGEN05LDRED(16x32bx2, x4, i32), TCGEN05LDRED(16x32bx2, x4, f32)},
1063 {TCGEN05LDRED(16x32bx2, x8, i32), TCGEN05LDRED(16x32bx2, x8, f32)},
1064 {TCGEN05LDRED(16x32bx2, x16, i32), TCGEN05LDRED(16x32bx2, x16, f32)},
1065 {TCGEN05LDRED(16x32bx2, x32, i32), TCGEN05LDRED(16x32bx2, x32, f32)},
1066 {TCGEN05LDRED(16x32bx2, x64, i32), TCGEN05LDRED(16x32bx2, x64, f32)},
1067 {TCGEN05LDRED(16x32bx2, x128, i32), TCGEN05LDRED(16x32bx2, x128, f32)},
1068 };
1069
1070 NVVM::Tcgen05LdStShape shape = thisOp.getShape();
1071 unsigned ID = [&]() {
1072 // `num` contains the length of vector and log2 of `num` returns the index
1073 // into the shape array
1074 unsigned idx = std::log2(Num);
1075 switch (shape) {
1076 case NVVM::Tcgen05LdStShape::SHAPE_32X32B:
1077 return Shape32x32b[idx][IsFloat];
1078 case NVVM::Tcgen05LdStShape::SHAPE_16X32BX2:
1079 return Shape16x32bx2[idx][IsFloat];
1080 default:
1081 llvm_unreachable("unhandled tcgen05.ld lowering");
1082 }
1083 }();
1084
1085 args.push_back(mt.lookupValue(thisOp.getAddr()));
1086
1087 if (shape == NVVM::Tcgen05LdStShape::SHAPE_16X32BX2)
1088 args.push_back(mt.lookupValue(thisOp.getOffset()));
1089
1090 args.push_back(
1091 builder.getInt32(thisOp.getOp() == NVVM::ReductionKind::MIN ? 0 : 1));
1092
1093 if (IsFloat) {
1094 args.push_back(builder.getInt1(static_cast<unsigned>(thisOp.getAbs())));
1095 args.push_back(builder.getInt1(static_cast<unsigned>(thisOp.getNan())));
1096 }
1097 return {ID, args};
1098}
1099
1100LogicalResult Tcgen05LdRedOp::verify() {
1101 VectorType data = cast<VectorType>(getData().getType());
1102 Type redVal = getRedVal().getType();
1103
1104 if (data.getElementType() != redVal)
1105 return emitError(
1106 "type of reduction value and element type of vector data should match");
1107
1108 if (getOp() != NVVM::ReductionKind::MIN &&
1109 getOp() != NVVM::ReductionKind::MAX)
1110 return emitError("only min and max reduction kinds are supported");
1111
1112 if (redVal.isInteger() && (getAbs() || getNan())) {
1113 return emitError("abs or nan is only applicable for f32 type");
1114 }
1115 return success();
1116}
1117
1118//===----------------------------------------------------------------------===//
1119// NVVM tcgen05.ld{.red}.spcompress functions
1120//===----------------------------------------------------------------------===//
1121
1122// tcgen05.ld{.red}.spcompress loads 32x32b.num elements and keeps two of every
1123// four of them in data. Each kept element has a 2-bit index in metadata, which
1124// therefore occupies ceil(num / 32) 32-bit registers.
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;
1133
1134 // nan only applies to the reduction.
1135 if (getNan() && !getRedVal())
1136 return emitOpError("nan requires the reduction result redVal");
1137 return success();
1138}
1139
1140mlir::NVVM::IDArgPair NVVM::Tcgen05LdRedSPCompressOp::getIntrinsicIDAndArgs(
1141 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1142 auto thisOp = cast<NVVM::Tcgen05LdRedSPCompressOp>(op);
1143 llvm::Intrinsic::ID id = llvm::Intrinsic::nvvm_tcgen05_ld_spcompress_32x32b;
1144
1146 mt.lookupValue(thisOp.getAddr()),
1147 // row_op: 0 for min and 1 for max.
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()));
1153 }
1154
1155 return {id, args};
1156}
1157
1158#define GET_OP_CLASSES
1159#include "mlir/Dialect/LLVMIR/NVVMTcgen05Ops.cpp.inc"
return success()
#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.
Definition Traits.cpp:117
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...
Definition Location.h:76
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition Types.cpp:58
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
constexpr unsigned notIntrinsic
The ID of no intrinsic, used in the intrinsic tables of the ops.
Definition NVVMOps.h:31
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.
Definition Utils.cpp:311
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.