MLIR  20.0.0git
NVVMDialect.cpp
Go to the documentation of this file.
1 //===- NVVMDialect.cpp - NVVM IR Ops and Dialect registration -------------===//
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 types and operation details for the NVVM IR dialect in
10 // MLIR, and the LLVM IR dialect. It also registers the dialect.
11 //
12 // The NVVM dialect only contains GPU specific additions on top of the general
13 // LLVM dialect.
14 //
15 //===----------------------------------------------------------------------===//
16 
18 
22 #include "mlir/IR/Builders.h"
24 #include "mlir/IR/BuiltinTypes.h"
25 #include "mlir/IR/Diagnostics.h"
27 #include "mlir/IR/MLIRContext.h"
28 #include "mlir/IR/Operation.h"
30 #include "mlir/IR/Types.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/TypeSwitch.h"
33 #include "llvm/AsmParser/Parser.h"
34 #include "llvm/IR/Attributes.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/SourceMgr.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include <cassert>
41 #include <optional>
42 #include <string>
43 
44 using namespace mlir;
45 using namespace NVVM;
46 
47 #include "mlir/Dialect/LLVMIR/NVVMOpsDialect.cpp.inc"
48 #include "mlir/Dialect/LLVMIR/NVVMOpsEnums.cpp.inc"
49 
50 //===----------------------------------------------------------------------===//
51 // Printing/parsing for NVVM ops
52 //===----------------------------------------------------------------------===//
53 
55  p << " " << op->getOperands();
56  if (op->getNumResults() > 0)
57  p << " : " << op->getResultTypes();
58 }
59 
60 // <operation> ::= `llvm.nvvm.vote.ballot.sync %mask, %pred` : result_type
61 ParseResult VoteBallotOp::parse(OpAsmParser &parser, OperationState &result) {
62  MLIRContext *context = parser.getContext();
63  auto int32Ty = IntegerType::get(context, 32);
64  auto int1Ty = IntegerType::get(context, 1);
65 
67  Type type;
68  return failure(parser.parseOperandList(ops) ||
69  parser.parseOptionalAttrDict(result.attributes) ||
70  parser.parseColonType(type) ||
71  parser.addTypeToList(type, result.types) ||
72  parser.resolveOperands(ops, {int32Ty, int1Ty},
73  parser.getNameLoc(), result.operands));
74 }
75 
77 
79  if (getCoordinates().empty() || getCoordinates().size() > 5)
80  return emitError("expects coordinates between 1 to 5 dimension");
81 
82  // Check for im2col mode
83  if (!getIm2colOffsets().empty()) {
84  if (getCoordinates().size() < 3)
85  return emitError(
86  "to use im2col mode, the tensor has to be at least 3-dimensional");
87  if (getCoordinates().size() != (getIm2colOffsets().size() + 2))
88  return emitError(
89  "im2col offsets must be 2 less than number of coordinates");
90  }
91  return success();
92 }
93 
95  if (getCoordinates().size() > 5)
96  return emitError("Maximum 5 coordinates and dimension is supported.");
97  return success();
98 }
99 
100 LogicalResult CpAsyncOp::verify() {
101  if (getModifier() != LoadCacheModifierKind::CG &&
102  getModifier() != LoadCacheModifierKind::CA)
103  return emitError("Only CG and CA cache modifiers are supported.");
104  if (getSize() != 4 && getSize() != 8 && getSize() != 16)
105  return emitError("expected byte size to be either 4, 8 or 16.");
106  if (getModifier() == LoadCacheModifierKind::CG && getSize() != 16)
107  return emitError("CG cache modifier is only support for 16 bytes copy.");
108  return success();
109 }
110 
111 // Given the element type of an operand and whether or not it is an accumulator,
112 // this function returns the PTX type (`NVVM::MMATypes`) that corresponds to the
113 // operand's element type.
114 std::optional<mlir::NVVM::MMATypes>
115 MmaOp::inferOperandMMAType(Type operandElType, bool isAccumulator) {
116  auto half2Type =
118  if (operandElType.isF64())
119  return NVVM::MMATypes::f64;
120  if (operandElType.isF16() || operandElType == half2Type)
121  return NVVM::MMATypes::f16;
122  if (operandElType.isF32() && isAccumulator)
123  return NVVM::MMATypes::f32;
124  if (operandElType.isF32() && !isAccumulator)
125  return NVVM::MMATypes::tf32;
126  if (llvm::isa<IntegerType>(operandElType)) {
127  if (isAccumulator)
128  return NVVM::MMATypes::s32;
129  return std::nullopt;
130  }
131 
132  if (auto structType = llvm::dyn_cast<LLVM::LLVMStructType>(operandElType)) {
133  if (structType.getBody().empty())
134  return std::nullopt;
135  return inferOperandMMAType(structType.getBody()[0], isAccumulator);
136  }
137 
138  return std::nullopt;
139 }
140 
141 static bool isInt4PtxType(MMATypes type) {
142  return (type == MMATypes::u4 || type == MMATypes::s4);
143 }
144 
145 static bool isInt8PtxType(MMATypes type) {
146  return (type == MMATypes::u8 || type == MMATypes::s8);
147 }
148 
149 static bool isIntegerPtxType(MMATypes type) {
150  return isInt4PtxType(type) || isInt8PtxType(type) || type == MMATypes::b1 ||
151  type == MMATypes::s32;
152 }
153 
154 MMATypes MmaOp::accumPtxType() {
155  std::optional<mlir::NVVM::MMATypes> val = inferOperandMMAType(
156  getODSOperands(2).getTypes().front(), /*isAccum=*/true);
157  assert(val.has_value() && "accumulator PTX type should always be inferrable");
158  return val.value();
159 }
160 
161 MMATypes MmaOp::resultPtxType() {
162  std::optional<mlir::NVVM::MMATypes> val =
163  inferOperandMMAType(getResult().getType(), /*isAccum=*/true);
164  assert(val.has_value() && "result PTX type should always be inferrable");
165  return val.value();
166 }
167 
168 void MmaOp::print(OpAsmPrinter &p) {
169  SmallVector<Type, 4> regTypes;
170  struct OperandFragment {
171  StringRef operandName;
172  StringRef ptxTypeAttr;
174  explicit OperandFragment(StringRef name, StringRef ptxTypeName)
175  : operandName(name), ptxTypeAttr(ptxTypeName) {}
176  };
177 
178  std::array<OperandFragment, 3> frags{
179  OperandFragment("A", getMultiplicandAPtxTypeAttrName()),
180  OperandFragment("B", getMultiplicandBPtxTypeAttrName()),
181  OperandFragment("C", "")};
182  SmallVector<StringRef, 4> ignoreAttrNames{
183  mlir::NVVM::MmaOp::getOperandSegmentSizeAttr()};
184 
185  for (unsigned fragIdx = 0; fragIdx < frags.size(); fragIdx++) {
186  auto &frag = frags[fragIdx];
187  auto varOperandSpec = getODSOperandIndexAndLength(fragIdx);
188  for (auto operandIdx = varOperandSpec.first;
189  operandIdx < varOperandSpec.first + varOperandSpec.second;
190  operandIdx++) {
191  frag.regs.push_back(this->getOperand(operandIdx));
192  if (operandIdx == 0) {
193  regTypes.push_back(this->getOperand(operandIdx).getType());
194  }
195  }
196  std::optional<MMATypes> inferredType =
197  inferOperandMMAType(regTypes.back(), /*isAccum=*/fragIdx >= 2);
198  if (inferredType)
199  ignoreAttrNames.push_back(frag.ptxTypeAttr);
200  }
201 
202  auto printMmaOperand = [&](const OperandFragment &frag) -> void {
203  p << " " << frag.operandName;
204  p << "[";
205  p.printOperands(frag.regs);
206  p << "] ";
207  };
208 
209  for (const auto &frag : frags) {
210  printMmaOperand(frag);
211  }
212 
213  p.printOptionalAttrDict(this->getOperation()->getAttrs(), ignoreAttrNames);
214 
215  // Print the types of the operands and result.
216  p << " : " << "(";
217  llvm::interleaveComma(SmallVector<Type, 3>{frags[0].regs[0].getType(),
218  frags[1].regs[0].getType(),
219  frags[2].regs[0].getType()},
220  p);
221  p << ")";
222  p.printArrowTypeList(TypeRange{this->getRes().getType()});
223 }
224 
225 void MmaOp::build(OpBuilder &builder, OperationState &result, Type resultType,
226  ValueRange operandA, ValueRange operandB, ValueRange operandC,
227  ArrayRef<int64_t> shape, std::optional<MMAB1Op> b1Op,
228  std::optional<MMAIntOverflow> intOverflow,
229  std::optional<std::array<MMATypes, 2>> multiplicandPtxTypes,
230  std::optional<std::array<MMALayout, 2>> multiplicandLayouts) {
231 
232  assert(shape.size() == 3 && "expected shape to have size 3 (m, n, k)");
233  MLIRContext *ctx = builder.getContext();
234  result.addAttribute(
235  "shape", builder.getAttr<MMAShapeAttr>(shape[0], shape[1], shape[2]));
236 
237  result.addOperands(operandA);
238  result.addOperands(operandB);
239  result.addOperands(operandC);
240 
241  if (multiplicandPtxTypes) {
242  result.addAttribute("multiplicandAPtxType",
243  MMATypesAttr::get(ctx, (*multiplicandPtxTypes)[0]));
244  result.addAttribute("multiplicandBPtxType",
245  MMATypesAttr::get(ctx, (*multiplicandPtxTypes)[1]));
246  } else {
247  if (auto res = inferOperandMMAType(operandA[0].getType(), false))
248  result.addAttribute("multiplicandAPtxType", MMATypesAttr::get(ctx, *res));
249  if (auto res = inferOperandMMAType(operandB[0].getType(), false))
250  result.addAttribute("multiplicandBPtxType", MMATypesAttr::get(ctx, *res));
251  }
252 
253  if (multiplicandLayouts) {
254  result.addAttribute("layoutA",
255  MMALayoutAttr::get(ctx, (*multiplicandLayouts)[0]));
256  result.addAttribute("layoutB",
257  MMALayoutAttr::get(ctx, (*multiplicandLayouts)[1]));
258  } else {
259  result.addAttribute("layoutA", MMALayoutAttr::get(ctx, MMALayout::row));
260  result.addAttribute("layoutB", MMALayoutAttr::get(ctx, MMALayout::col));
261  }
262 
263  if (intOverflow.has_value())
264  result.addAttribute("intOverflowBehavior",
265  MMAIntOverflowAttr::get(ctx, *intOverflow));
266  if (b1Op.has_value())
267  result.addAttribute("b1Op", MMAB1OpAttr::get(ctx, *b1Op));
268 
269  result.addTypes(resultType);
270  result.addAttribute(
271  MmaOp::getOperandSegmentSizeAttr(),
272  builder.getDenseI32ArrayAttr({static_cast<int32_t>(operandA.size()),
273  static_cast<int32_t>(operandB.size()),
274  static_cast<int32_t>(operandC.size())}));
275 }
276 
277 // <operation> :=
278 // A `[` $operandA `]` B `[` $operandB `]` C `[` $operandC `]`
279 // attr-dict : (type($operandA[0]), type($operandB[0]), type($operandC[0]))
280 // `->` type($res)
281 ParseResult MmaOp::parse(OpAsmParser &parser, OperationState &result) {
282  struct OperandFragment {
283  std::optional<MMATypes> elemtype;
285  SmallVector<Type> regTypes;
286  };
287 
288  Builder &builder = parser.getBuilder();
289  std::array<OperandFragment, 4> frags;
290 
291  NamedAttrList namedAttributes;
292 
293  // A helper to parse the operand segments.
294  auto parseMmaOperand = [&](StringRef operandName,
295  OperandFragment &frag) -> LogicalResult {
296  if (parser.parseKeyword(operandName).failed())
297  return failure();
298  if (parser
299  .parseOperandList(frag.regs, OpAsmParser::Delimiter::OptionalSquare)
300  .failed())
301  return failure();
302  return success();
303  };
304 
305  // Parse the operand segments.
306  if (parseMmaOperand("A", frags[0]).failed())
307  return failure();
308  if (parseMmaOperand("B", frags[1]).failed())
309  return failure();
310  if (parseMmaOperand("C", frags[2]).failed())
311  return failure();
312 
313  if (parser.parseOptionalAttrDict(namedAttributes).failed())
314  return failure();
315 
316  // Parse the type specification and resolve operands.
317  SmallVector<Type, 3> operandTypes;
318  if (failed(parser.parseColon()))
319  return failure();
320  if (failed(parser.parseLParen()))
321  return failure();
322  if (failed(parser.parseTypeList(operandTypes)))
323  return failure();
324  if (failed(parser.parseRParen()))
325  if (operandTypes.size() != 3)
326  return parser.emitError(
327  parser.getNameLoc(),
328  "expected one type for each operand segment but got " +
329  Twine(operandTypes.size()) + " types");
330  for (const auto &iter : llvm::enumerate(operandTypes)) {
331  auto &frag = frags[iter.index()];
332  frag.regTypes.resize(frag.regs.size(), iter.value());
333  if (failed(parser.resolveOperands(frag.regs, frag.regTypes,
334  parser.getNameLoc(), result.operands)))
335  return failure();
336  frag.elemtype =
337  inferOperandMMAType(frag.regTypes[0], /*isAccum=*/iter.index() < 2);
338  }
339 
340  Type resultType;
341  if (parser.parseArrow() || parser.parseType(resultType))
342  return failure();
343  frags[3].elemtype = inferOperandMMAType(resultType, /*isAccum=*/true);
344 
345  std::array<StringRef, 2> names{"multiplicandAPtxType",
346  "multiplicandBPtxType"};
347  for (unsigned idx = 0; idx < names.size(); idx++) {
348  const auto &frag = frags[idx];
349  std::optional<NamedAttribute> attr = namedAttributes.getNamed(names[idx]);
350  if (!frag.elemtype.has_value() && !attr.has_value()) {
351  return parser.emitError(
352  parser.getNameLoc(),
353  "attribute " + names[idx] +
354  " is not provided explicitly and cannot be inferred");
355  }
356  if (!attr.has_value())
357  result.addAttribute(
358  names[idx], MMATypesAttr::get(parser.getContext(), *frag.elemtype));
359  }
360 
361  result.addTypes(resultType);
362  if (!namedAttributes.empty())
363  result.addAttributes(namedAttributes);
364  result.addAttribute(MmaOp::getOperandSegmentSizeAttr(),
365  builder.getDenseI32ArrayAttr({
366  static_cast<int32_t>(frags[0].regs.size()),
367  static_cast<int32_t>(frags[1].regs.size()),
368  static_cast<int32_t>(frags[2].regs.size()),
369  }));
370  return success();
371 }
372 
373 LogicalResult MmaOp::verify() {
374  MLIRContext *context = getContext();
375  auto f16Ty = Float16Type::get(context);
376  auto i32Ty = IntegerType::get(context, 32);
377  auto f16x2Ty = LLVM::getFixedVectorType(f16Ty, 2);
378  auto f32Ty = Float32Type::get(context);
379  auto f16x2x4StructTy = LLVM::LLVMStructType::getLiteral(
380  context, {f16x2Ty, f16x2Ty, f16x2Ty, f16x2Ty});
381 
382  auto s32x4StructTy =
383  LLVM::LLVMStructType::getLiteral(context, {i32Ty, i32Ty, i32Ty, i32Ty});
384  auto f32x8StructTy =
386  auto f16x2x2StructTy =
387  LLVM::LLVMStructType::getLiteral(context, {f16x2Ty, f16x2Ty});
388  auto f32x4StructTy =
389  LLVM::LLVMStructType::getLiteral(context, {f32Ty, f32Ty, f32Ty, f32Ty});
390  auto s32x2StructTy =
391  LLVM::LLVMStructType::getLiteral(context, {i32Ty, i32Ty});
392 
393  std::array<int64_t, 3> mmaShape{getShapeAttr().getM(), getShapeAttr().getN(),
394  getShapeAttr().getK()};
395 
396  // These variables define the set of allowed data types for matrices A, B, C,
397  // and result.
398  using AllowedShapes = SmallVector<std::array<int64_t, 3>, 2>;
399  using AllowedTypes = SmallVector<SmallVector<Type, 4>, 2>;
400  AllowedShapes allowedShapes;
401  AllowedTypes expectedA;
402  AllowedTypes expectedB;
403  AllowedTypes expectedC;
404  SmallVector<Type> expectedResult;
405 
406  // When M = 16, we just need to calculate the number of 8xk tiles, where
407  // k is a factor that depends on the data type.
408  if (mmaShape[0] == 16) {
409  int64_t kFactor;
410  Type multiplicandFragType;
411  switch (*getMultiplicandAPtxType()) {
412  case MMATypes::tf32:
413  kFactor = 4;
414  multiplicandFragType = i32Ty;
415  expectedResult.push_back(LLVM::LLVMStructType::getLiteral(
416  context, {f32Ty, f32Ty, f32Ty, f32Ty}));
417  break;
418  case MMATypes::f16:
419  case MMATypes::bf16:
420  kFactor = 8;
421  multiplicandFragType = f16x2Ty;
422  expectedResult.push_back(f16x2x2StructTy);
423  expectedResult.push_back(f32x4StructTy);
424  break;
425  case MMATypes::s4:
426  case MMATypes::u4:
427  kFactor = 32;
428  break;
429  case MMATypes::b1:
430  kFactor = 128;
431  break;
432  case MMATypes::s8:
433  case MMATypes::u8:
434  kFactor = 16;
435  break;
436  default:
437  return emitError("invalid shape or multiplicand type: " +
438  stringifyEnum(getMultiplicandAPtxType().value()));
439  }
440 
441  if (isIntegerPtxType(getMultiplicandAPtxType().value())) {
442  expectedResult.push_back(s32x4StructTy);
443  expectedC.emplace_back(4, i32Ty);
444  multiplicandFragType = i32Ty;
445  } else {
446  expectedC.emplace_back(2, f16x2Ty);
447  expectedC.emplace_back(4, f32Ty);
448  }
449 
450  int64_t unitA = (mmaShape[0] / 8) * (mmaShape[2] / kFactor);
451  int64_t unitB = (mmaShape[1] / 8) * (mmaShape[2] / kFactor);
452  expectedA.emplace_back(unitA, multiplicandFragType);
453  expectedB.emplace_back(unitB, multiplicandFragType);
454  allowedShapes.push_back({16, 8, kFactor});
455  allowedShapes.push_back({16, 8, kFactor * 2});
456  }
457 
458  // In the M=8 case, there is only 1 possible case per data type.
459  if (mmaShape[0] == 8) {
460  if (*getMultiplicandAPtxType() == MMATypes::f16) {
461  expectedA.emplace_back(2, f16x2Ty);
462  expectedB.emplace_back(2, f16x2Ty);
463  expectedResult.push_back(f16x2x4StructTy);
464  expectedResult.push_back(f32x8StructTy);
465  expectedC.emplace_back(4, f16x2Ty);
466  expectedC.emplace_back(8, f32Ty);
467  allowedShapes.push_back({8, 8, 4});
468  }
469  if (*getMultiplicandAPtxType() == MMATypes::f64) {
470  Type f64Ty = Float64Type::get(context);
471  expectedA.emplace_back(1, f64Ty);
472  expectedB.emplace_back(1, f64Ty);
473  expectedC.emplace_back(2, f64Ty);
474  // expectedC.emplace_back(1, LLVM::getFixedVectorType(f64Ty, 2));
475  expectedResult.emplace_back(LLVM::LLVMStructType::getLiteral(
476  context, SmallVector<Type>(2, f64Ty)));
477  allowedShapes.push_back({8, 8, 4});
478  }
479  if (isIntegerPtxType(getMultiplicandAPtxType().value())) {
480  expectedA.push_back({i32Ty});
481  expectedB.push_back({i32Ty});
482  expectedC.push_back({i32Ty, i32Ty});
483  expectedResult.push_back(s32x2StructTy);
484  if (isInt4PtxType(getMultiplicandAPtxType().value()))
485  allowedShapes.push_back({8, 8, 32});
486  if (isInt8PtxType(getMultiplicandAPtxType().value()))
487  allowedShapes.push_back({8, 8, 16});
488  if (getMultiplicandAPtxType().value() == MMATypes::b1)
489  allowedShapes.push_back({8, 8, 128});
490  }
491  }
492 
493  std::string errorMessage;
494  llvm::raw_string_ostream errorStream(errorMessage);
495 
496  // Check that we matched an existing shape/dtype combination.
497  if (expectedA.empty() || expectedB.empty() || expectedC.empty() ||
498  !llvm::is_contained(allowedShapes, mmaShape)) {
499  errorStream << "unimplemented variant for MMA shape <";
500  llvm::interleaveComma(mmaShape, errorStream);
501  errorStream << ">";
502  return emitOpError(errorMessage);
503  }
504 
505  // Verify the operand types for segments of A, B, and C operands.
506  std::array<StringRef, 3> operandNames{"A", "B", "C"};
507  for (const auto &iter : llvm::enumerate(
508  SmallVector<AllowedTypes, 3>{expectedA, expectedB, expectedC})) {
509  auto spec = this->getODSOperandIndexAndLength(iter.index());
510  SmallVector<Type, 4> operandTySeg(operand_type_begin() + spec.first,
511  operand_type_begin() + spec.first +
512  spec.second);
513  bool match = llvm::is_contained(iter.value(), operandTySeg);
514 
515  if (!match) {
516  errorStream << "Could not match types for the "
517  << operandNames[iter.index()]
518  << " operands; expected one of ";
519  for (const auto &x : iter.value()) {
520  errorStream << x.size() << "x" << x[0] << " ";
521  }
522  errorStream << "but got ";
523  llvm::interleaveComma(operandTySeg, errorStream);
524  return emitOpError(errorStream.str());
525  }
526  }
527 
528  // Check the result type
529  if (!llvm::any_of(expectedResult, [&](Type expectedResultType) {
530  return expectedResultType == getResult().getType();
531  })) {
532  errorStream
533  << "Could not match allowed types for the result; expected one of ";
534  llvm::interleaveComma(expectedResult, errorStream);
535  errorStream << " but got " << getResult().getType();
536  return emitOpError(errorStream.str());
537  }
538 
539  // Ensure that binary MMA variants have a b1 MMA operation defined.
540  if (getMultiplicandAPtxType() == MMATypes::b1 && !getB1Op()) {
541  return emitOpError("op requires " + getB1OpAttrName().strref() +
542  " attribute");
543  }
544 
545  // Ensure int4/int8 MMA variants specify the accum overflow behavior
546  // attribute.
547  if (isInt4PtxType(*getMultiplicandAPtxType()) ||
548  isInt8PtxType(*getMultiplicandAPtxType())) {
549  if (!getIntOverflowBehavior())
550  return emitOpError("op requires " +
551  getIntOverflowBehaviorAttrName().strref() +
552  " attribute");
553  }
554 
555  return success();
556 }
557 
558 LogicalResult ShflOp::verify() {
559  if (!(*this)->getAttrOfType<UnitAttr>("return_value_and_is_valid"))
560  return success();
561  auto type = llvm::dyn_cast<LLVM::LLVMStructType>(getType());
562  auto elementType = (type && type.getBody().size() == 2)
563  ? llvm::dyn_cast<IntegerType>(type.getBody()[1])
564  : nullptr;
565  if (!elementType || elementType.getWidth() != 1)
566  return emitError("expected return type to be a two-element struct with "
567  "i1 as the second element");
568  return success();
569 }
570 
571 std::pair<mlir::Type, unsigned> NVVM::inferMMAType(NVVM::MMATypes type,
572  NVVM::MMAFrag frag, int nRow,
573  int nCol,
574  MLIRContext *context) {
575  unsigned numberElements = 0;
576  Type elementType;
577  OpBuilder builder(context);
578  Type f16x2 = VectorType::get(2, builder.getF16Type());
579  if (type == NVVM::MMATypes::f16) {
580  elementType = f16x2;
581  if (frag == NVVM::MMAFrag::a || frag == NVVM::MMAFrag::b)
582  numberElements = 8;
583  else
584  numberElements = 4;
585  } else if (type == NVVM::MMATypes::f32) {
586  elementType = builder.getF32Type();
587  numberElements = 8;
588  } else if (type == NVVM::MMATypes::tf32) {
589  elementType = builder.getI32Type();
590  numberElements = 4;
591  } else if (type == NVVM::MMATypes::s8 || type == NVVM::MMATypes::u8) {
592  elementType = builder.getI32Type();
593  int parallelSize = 0;
594  if (frag == NVVM::MMAFrag::a)
595  parallelSize = nRow;
596  if (frag == NVVM::MMAFrag::b)
597  parallelSize = nCol;
598 
599  // m == 16 && n == 16 && k == 16
600  if (parallelSize == 16)
601  numberElements = 2;
602  // m == 8 && n == 32 && k == 16 or m == 32 && n == 8 && k == 16
603  else if (parallelSize == 8)
604  numberElements = 1;
605  else if (parallelSize == 32)
606  numberElements = 4;
607  } else if (type == NVVM::MMATypes::s32) {
608  elementType = builder.getI32Type();
609  numberElements = 8;
610  }
611  assert(numberElements != 0 && elementType != nullptr);
612  return std::make_pair(elementType, numberElements);
613 }
614 
615 static std::pair<mlir::Type, unsigned>
616 inferMMATypeFromMNK(NVVM::MMATypes type, NVVM::MMAFrag frag, int m, int n,
617  int k, MLIRContext *context) {
618  int nRow, nCol;
619  if (frag == NVVM::MMAFrag::a) {
620  nRow = m;
621  nCol = k;
622  } else if (frag == NVVM::MMAFrag::b) {
623  nRow = k;
624  nCol = n;
625  } else {
626  nRow = m;
627  nCol = n;
628  }
629  assert(nRow && nCol);
630  return inferMMAType(type, frag, nRow, nCol, context);
631 }
632 
633 LogicalResult NVVM::WMMALoadOp::verify() {
634  unsigned addressSpace =
635  llvm::cast<LLVM::LLVMPointerType>(getPtr().getType()).getAddressSpace();
636  if (addressSpace != 0 && addressSpace != NVVM::kGlobalMemorySpace &&
637  addressSpace != NVVM::kSharedMemorySpace)
638  return emitOpError("expected source pointer in memory "
639  "space 0, 1, 3");
640 
641  if (NVVM::WMMALoadOp::getIntrinsicID(getM(), getN(), getK(), getLayout(),
642  getEltype(), getFrag()) == 0)
643  return emitOpError() << "invalid attribute combination";
644  std::pair<Type, unsigned> typeInfo = inferMMATypeFromMNK(
645  getEltype(), getFrag(), getM(), getN(), getK(), getContext());
647  getContext(), SmallVector<Type, 8>(typeInfo.second, typeInfo.first));
648  if (getType() != dstType)
649  return emitOpError("expected destination type is a structure of ")
650  << typeInfo.second << " elements of type " << typeInfo.first;
651  return success();
652 }
653 
654 LogicalResult NVVM::WMMAStoreOp::verify() {
655  unsigned addressSpace =
656  llvm::cast<LLVM::LLVMPointerType>(getPtr().getType()).getAddressSpace();
657  if (addressSpace != 0 && addressSpace != NVVM::kGlobalMemorySpace &&
658  addressSpace != NVVM::kSharedMemorySpace)
659  return emitOpError("expected operands to be a source pointer in memory "
660  "space 0, 1, 3");
661 
662  if (NVVM::WMMAStoreOp::getIntrinsicID(getM(), getN(), getK(), getLayout(),
663  getEltype()) == 0)
664  return emitOpError() << "invalid attribute combination";
665  std::pair<Type, unsigned> typeInfo = inferMMATypeFromMNK(
666  getEltype(), NVVM::MMAFrag::c, getM(), getN(), getK(), getContext());
667  if (getArgs().size() != typeInfo.second)
668  return emitOpError() << "expected " << typeInfo.second << " data operands";
669  if (llvm::any_of(getArgs(), [&typeInfo](Value operands) {
670  return operands.getType() != typeInfo.first;
671  }))
672  return emitOpError() << "expected data operands of type " << typeInfo.first;
673  return success();
674 }
675 
676 LogicalResult NVVM::WMMAMmaOp::verify() {
677  if (NVVM::WMMAMmaOp::getIntrinsicID(getM(), getN(), getK(), getLayoutA(),
678  getLayoutB(), getEltypeA(),
679  getEltypeB()) == 0)
680  return emitOpError() << "invalid attribute combination";
681  std::pair<Type, unsigned> typeInfoA = inferMMATypeFromMNK(
682  getEltypeA(), NVVM::MMAFrag::a, getM(), getN(), getK(), getContext());
683  std::pair<Type, unsigned> typeInfoB = inferMMATypeFromMNK(
684  getEltypeA(), NVVM::MMAFrag::b, getM(), getN(), getK(), getContext());
685  std::pair<Type, unsigned> typeInfoC = inferMMATypeFromMNK(
686  getEltypeB(), NVVM::MMAFrag::c, getM(), getN(), getK(), getContext());
687  SmallVector<Type, 32> arguments;
688  arguments.append(typeInfoA.second, typeInfoA.first);
689  arguments.append(typeInfoB.second, typeInfoB.first);
690  arguments.append(typeInfoC.second, typeInfoC.first);
691  unsigned numArgs = arguments.size();
692  if (getArgs().size() != numArgs)
693  return emitOpError() << "expected " << numArgs << " arguments";
694  for (unsigned i = 0; i < numArgs; i++) {
695  if (getArgs()[i].getType() != arguments[i])
696  return emitOpError() << "expected argument " << i << " to be of type "
697  << arguments[i];
698  }
700  getContext(), SmallVector<Type, 8>(typeInfoC.second, typeInfoC.first));
701  if (getType() != dstType)
702  return emitOpError("expected destination type is a structure of ")
703  << typeInfoC.second << " elements of type " << typeInfoC.first;
704  return success();
705 }
706 
707 LogicalResult NVVM::LdMatrixOp::verify() {
708  unsigned addressSpace =
709  llvm::cast<LLVM::LLVMPointerType>(getPtr().getType()).getAddressSpace();
710  if (addressSpace != NVVM::kSharedMemorySpace)
711  return emitOpError("expected source pointer in memory space 3");
712 
713  if (getNum() != 1 && getNum() != 2 && getNum() != 4)
714  return emitOpError("expected num attribute to be 1, 2 or 4");
715 
716  Type i32 = IntegerType::get(getContext(), 32);
717  if (getNum() == 1 && getType() != i32)
718  return emitOpError("expected destination type is i32");
719  if (getNum() == 2 || getNum() == 4) {
721  getContext(), SmallVector<Type>(getNum(), i32));
722  if (getType() != dstType)
723  return emitOpError("expected destination type is a structure of ")
724  << getNum() << " elements of type i32";
725  }
726  return success();
727 }
728 
729 LogicalResult NVVM::StMatrixOp::verify() {
730  unsigned addressSpace =
731  llvm::cast<LLVM::LLVMPointerType>(getPtr().getType()).getAddressSpace();
732  if (addressSpace != NVVM::kSharedMemorySpace)
733  return emitOpError("expected source pointer in memory space 3");
734 
735  int numMatrix = getSources().size();
736  if (numMatrix != 1 && numMatrix != 2 && numMatrix != 4)
737  return emitOpError("expected num attribute to be 1, 2 or 4");
738 
739  return success();
740 }
741 
742 FailureOr<int> getAllowedSizeK(NVVM::WGMMATypes typeA) {
743  if (typeA == NVVM::WGMMATypes::tf32)
744  return 8;
745  if (typeA == NVVM::WGMMATypes::f16 || typeA == NVVM::WGMMATypes::bf16)
746  return 16;
747  if (typeA == NVVM::WGMMATypes::s8 || typeA == NVVM::WGMMATypes::u8)
748  return 32;
749  if (typeA == NVVM::WGMMATypes::e4m3 || typeA == NVVM::WGMMATypes::e5m2)
750  return 32;
751  if (typeA == NVVM::WGMMATypes::b1)
752  return 256;
753  return failure();
754 }
755 
756 LogicalResult isAllowedWGMMADataType(NVVM::WGMMATypes typeD,
757  NVVM::WGMMATypes typeA,
758  NVVM::WGMMATypes typeB) {
759  switch (typeA) {
760  case NVVM::WGMMATypes::f16:
761  if ((typeD == NVVM::WGMMATypes::f32 || typeD == NVVM::WGMMATypes::f16) &&
762  typeB == NVVM::WGMMATypes::f16)
763  return success();
764  break;
765  case NVVM::WGMMATypes::tf32:
766  if (typeD == NVVM::WGMMATypes::f32 && typeB == NVVM::WGMMATypes::tf32)
767  return success();
768  break;
769  case NVVM::WGMMATypes::u8:
770  case NVVM::WGMMATypes::s8:
771  if (typeD == NVVM::WGMMATypes::s32 &&
772  (typeB == NVVM::WGMMATypes::u8 || typeB == NVVM::WGMMATypes::s8))
773  return success();
774  break;
775  case NVVM::WGMMATypes::b1:
776  if (typeD == NVVM::WGMMATypes::s32 && typeB == NVVM::WGMMATypes::b1)
777  return success();
778  break;
779  case NVVM::WGMMATypes::bf16:
780  if ((typeD == NVVM::WGMMATypes::f32 || typeD == NVVM::WGMMATypes::f16) &&
781  typeB == NVVM::WGMMATypes::bf16)
782  return success();
783  break;
784  case NVVM::WGMMATypes::e4m3:
785  case NVVM::WGMMATypes::e5m2:
786  if ((typeD == NVVM::WGMMATypes::f32 || typeD == NVVM::WGMMATypes::f16) &&
787  (typeB == NVVM::WGMMATypes::e5m2 || typeB == NVVM::WGMMATypes::e4m3))
788  return success();
789  break;
790  case WGMMATypes::f32:
791  case WGMMATypes::s32:
792  llvm_unreachable("unsupported input types");
793  break;
794  }
795  return failure();
796 }
797 
798 LogicalResult isAllowedSizeN(int sizeN, NVVM::WGMMATypes typeA) {
799  SmallVector<int> allowedN = {8, 16, 24, 32, 40, 48, 56, 64,
800  72, 80, 88, 96, 104, 112, 120, 128,
801  136, 144, 152, 160, 168, 176, 184, 192,
802  200, 208, 216, 224, 232, 240, 248, 256};
803  SmallVector<int> allowedNshort = {8, 16, 24, 32, 48, 64,
804  80, 96, 112, 128, 144, 160,
805  176, 192, 208, 224, 240, 256};
806  switch (typeA) {
807  case WGMMATypes::f16:
808  case WGMMATypes::tf32:
809  case WGMMATypes::bf16:
810  case WGMMATypes::e4m3:
811  case WGMMATypes::e5m2:
812  if (llvm::is_contained(allowedN, sizeN))
813  return success();
814  break;
815  case WGMMATypes::u8:
816  case WGMMATypes::s8:
817  case WGMMATypes::b1:
818  if (llvm::is_contained(allowedNshort, sizeN))
819  return success();
820  break;
821  case WGMMATypes::f32:
822  case WGMMATypes::s32:
823  llvm_unreachable("unsupported input types");
824  break;
825  }
826  return failure();
827 }
828 
829 LogicalResult NVVM::WgmmaMmaAsyncOp::verify() {
830  Value outValue = getResults();
831  auto stype = dyn_cast<LLVM::LLVMStructType>(outValue.getType());
832  if (!stype)
833  return emitOpError() << "expected results to be struct";
834  int outputSize = stype.getBody().size();
835  WGMMATypes typeD = getTypeD();
836  WGMMATypes typeA = getTypeA();
837  WGMMATypes typeB = getTypeB();
838 
839  for (Type t : stype.getBody()) {
840  if (t != stype.getBody().front())
841  return emitOpError()
842  << "all elements in struct must be same type but there is " << t;
843  }
844 
845  if (typeD != WGMMATypes::f32 && typeD != WGMMATypes::f16 &&
846  typeD != WGMMATypes::s32) {
847  return emitOpError() << "does not support the given output type "
848  << NVVM::stringifyWGMMATypes(typeD);
849  }
850  if (typeD == WGMMATypes::s32 &&
851  (getScaleA() == WGMMAScaleIn::neg || getScaleB() == WGMMAScaleIn::neg)) {
852  return emitOpError() << "has s32 output, scaleA and scaleB cannot be neg";
853  }
854 
855  if (failed(isAllowedWGMMADataType(typeD, typeA, typeB))) {
856  return emitOpError() << NVVM::stringifyWGMMATypes(typeD)
857  << " += " << NVVM::stringifyWGMMATypes(typeA) << " * "
858  << NVVM::stringifyWGMMATypes(typeB)
859  << ", it is not supported.";
860  }
861 
862  // Check M
863  if (getShape().getM() != 64)
864  return emitOpError() << "shape 'm' must be 64";
865 
866  // Check K
867  FailureOr<int> allowedK = getAllowedSizeK(typeA);
868  if (failed(allowedK) || allowedK.value() != getShape().getK())
869  return emitOpError() << "shape 'k' must be " << allowedK.value()
870  << " for input type "
871  << NVVM::stringifyWGMMATypes(typeA);
872 
873  // Check N
874  if (failed(isAllowedSizeN(getShape().getN(), typeA))) {
875  return emitOpError() << "has input type "
876  << NVVM::stringifyWGMMATypes(typeA) << " n is set to "
877  << getShape().getN() << ", it is not supported.";
878  }
879 
880  // Check transpose (only available for f16/bf16)
881  // Matrices A should be stored in row-major and B in column-major.
882  // Only f16/bf16 matrices can be stored in either column-major or row-major
883  // by setting the tranpose value(imm-trans-a,imm-trans-b) in PTX code.
884  if ((typeA != WGMMATypes::f16 && typeA != WGMMATypes::bf16) &&
885  (getLayoutA() == mlir::NVVM::MMALayout::col ||
886  getLayoutB() == mlir::NVVM::MMALayout::row)) {
887  return emitOpError()
888  << "given layouts layout_a = " << stringifyMMALayout(getLayoutA())
889  << " and layout_b = " << stringifyMMALayout(getLayoutB())
890  << " for input types " << stringifyWGMMATypes(typeA) << " and "
891  << stringifyWGMMATypes(typeB)
892  << " requires transpose. However, this is only supported for: "
893  << stringifyMMATypes(MMATypes::f16) << " and "
894  << stringifyMMATypes(MMATypes::bf16);
895  }
896 
897  // Check result registers
898  int expectedOutput = 0;
899  if (typeD == WGMMATypes::f32 || typeD == WGMMATypes::s32)
900  expectedOutput = getShape().getN() / 2;
901  if (typeD == WGMMATypes::f16)
902  expectedOutput = getShape().getN() / 4;
903  if (outputSize != expectedOutput) {
904  return emitOpError() << "results " << expectedOutput
905  << ", however output struct has " << outputSize
906  << " elements";
907  }
908  // Check satfinite (only available for s32 accumulator)
909  if (typeD != WGMMATypes::s32 &&
910  getSatfinite().value_or(NVVM::MMAIntOverflow::wrapped) ==
911  NVVM::MMAIntOverflow::satfinite) {
912  return emitOpError()
913  << " `satfinite` can be only used with s32 accumulator, however "
914  "the current accumulator is "
915  << NVVM::stringifyWGMMATypes(typeD);
916  }
917 
918  return success();
919 }
920 
921 std::string NVVM::WgmmaMmaAsyncOp::getPtx() {
922 
923  int m = getShape().getM(), n = getShape().getN(), k = getShape().getK();
924  bool isF16 = getTypeA() == WGMMATypes::f16 || getTypeA() == WGMMATypes::bf16;
925 
926  StringRef outputTypeName = stringifyWGMMATypes(getTypeD());
927 
928  int expectedOutputRegisters = 0;
929  if (getTypeD() == WGMMATypes::f16)
930  expectedOutputRegisters = getShape().getN() / 4;
931  else
932  expectedOutputRegisters = getShape().getN() / 2;
933 
934  std::string ptx;
935  llvm::raw_string_ostream ss(ptx);
936 
937  ss << "{\n"
938  ".reg .pred p;\n"
939  "setp.ne.b32 p, $"
940  << ((expectedOutputRegisters * 2) + 2)
941  << ", 0;\n"
942  "wgmma.mma_async.sync.aligned.m"
943  << m << "n" << n << "k" << k << "." << outputTypeName << "."
944  << stringifyWGMMATypes(getTypeA()) << "."
945  << stringifyWGMMATypes(getTypeB());
946  if (getSatfinite().value_or(NVVM::MMAIntOverflow::wrapped) ==
947  NVVM::MMAIntOverflow::satfinite)
948  ss << ".satfinite";
949  ss << " {";
950  int regCnt = 0;
951  for (; regCnt < expectedOutputRegisters; ++regCnt) {
952  ss << "$" << regCnt;
953  if (regCnt != expectedOutputRegisters - 1)
954  ss << ", ";
955  }
956 
957  ss << "},";
958  // Need to map read/write registers correctly.
959  regCnt = (regCnt * 2);
960  ss << " $" << (regCnt) << "," << " $" << (regCnt + 1) << "," << " p";
961  if (getTypeD() != WGMMATypes::s32) {
962  ss << ", $" << (regCnt + 3) << ", $" << (regCnt + 4);
963  }
964  // Don't add transpose parameters unless needed.
965  if (isF16) {
966  ss << ", $" << (regCnt + 5) << ", $" << (regCnt + 6);
967  }
968  ss << ";\n"
969  << "}\n";
970  ss.flush();
971  return ptx;
972 }
973 
974 void NVVM::WgmmaMmaAsyncOp::getAsmValues(
975  RewriterBase &rewriter,
976  llvm::SmallVectorImpl<std::pair<mlir::Value, mlir::NVVM::PTXRegisterMod>>
977  &asmValues) {
978  bool isF16 = getTypeA() == WGMMATypes::f16 || getTypeA() == WGMMATypes::bf16;
979  if (getResults())
980  asmValues.push_back({getResults(), mlir::NVVM::PTXRegisterMod::Write});
981  if (getInouts())
982  asmValues.push_back({getInouts(), mlir::NVVM::PTXRegisterMod::ReadWrite});
983  asmValues.push_back({getDescriptorA(), mlir::NVVM::PTXRegisterMod::Read});
984  asmValues.push_back({getDescriptorB(), mlir::NVVM::PTXRegisterMod::Read});
985  asmValues.push_back({makeConstantI32(rewriter, static_cast<int>(getScaleD())),
987  if (getTypeD() != WGMMATypes::s32) {
988  asmValues.push_back(
989  {makeConstantI32(rewriter,
990  getScaleA() == NVVM::WGMMAScaleIn::neg ? -1 : 1),
992  asmValues.push_back(
993  {makeConstantI32(rewriter,
994  getScaleB() == NVVM::WGMMAScaleIn::neg ? -1 : 1),
996  }
997  if (isF16) {
998  asmValues.push_back(
999  {makeConstantI32(rewriter, static_cast<int>(getLayoutA())),
1001  asmValues.push_back(
1002  {makeConstantI32(rewriter, 1 - static_cast<int>(getLayoutB())),
1004  }
1005 }
1006 LogicalResult NVVM::FenceProxyOp::verify() {
1007  if (getKind() == NVVM::ProxyKind::async_shared && !getSpace().has_value()) {
1008  return emitOpError() << "async_shared fence requires space attribute";
1009  }
1010  if (getKind() != NVVM::ProxyKind::async_shared && getSpace().has_value()) {
1011  return emitOpError() << "only async_shared fence can have space attribute";
1012  }
1013  return success();
1014 }
1015 
1016 LogicalResult NVVM::SetMaxRegisterOp::verify() {
1017  if (getRegCount() % 8)
1018  return emitOpError("new register size must be multiple of 8");
1019  if (getRegCount() < 24 || getRegCount() > 256)
1020  return emitOpError("new register size must be in between 24 to 256");
1021  return success();
1022 }
1023 
1024 LogicalResult NVVM::BarrierOp::verify() {
1025  if (getNumberOfThreads() && !getBarrierId())
1026  return emitOpError(
1027  "barrier id is missing, it should be set between 0 to 15");
1028  return success();
1029 }
1030 
1031 //===----------------------------------------------------------------------===//
1032 // NVVMDialect initialization, type parsing, and registration.
1033 //===----------------------------------------------------------------------===//
1034 
1035 // TODO: This should be the llvm.nvvm dialect once this is supported.
1036 void NVVMDialect::initialize() {
1037  addOperations<
1038 #define GET_OP_LIST
1039 #include "mlir/Dialect/LLVMIR/NVVMOps.cpp.inc"
1040  >();
1041  addAttributes<
1042 #define GET_ATTRDEF_LIST
1043 #include "mlir/Dialect/LLVMIR/NVVMOpsAttributes.cpp.inc"
1044  >();
1045 
1046  // Support unknown operations because not all NVVM operations are
1047  // registered.
1048  allowUnknownOperations();
1049  declarePromisedInterface<ConvertToLLVMPatternInterface, NVVMDialect>();
1050  declarePromisedInterface<gpu::TargetAttrInterface, NVVMTargetAttr>();
1051 }
1052 
1053 LogicalResult NVVMDialect::verifyOperationAttribute(Operation *op,
1054  NamedAttribute attr) {
1055  StringAttr attrName = attr.getName();
1056  // Kernel function attribute should be attached to functions.
1057  if (attrName == NVVMDialect::getKernelFuncAttrName()) {
1058  if (!isa<LLVM::LLVMFuncOp>(op)) {
1059  return op->emitError() << "'" << NVVMDialect::getKernelFuncAttrName()
1060  << "' attribute attached to unexpected op";
1061  }
1062  }
1063  // If maxntid and reqntid exist, it must be an array with max 3 dim
1064  if (attrName == NVVMDialect::getMaxntidAttrName() ||
1065  attrName == NVVMDialect::getReqntidAttrName()) {
1066  auto values = llvm::dyn_cast<DenseI32ArrayAttr>(attr.getValue());
1067  if (!values || values.empty() || values.size() > 3)
1068  return op->emitError()
1069  << "'" << attrName
1070  << "' attribute must be integer array with maximum 3 index";
1071  }
1072  // If minctasm and maxnreg exist, it must be an integer attribute
1073  if (attrName == NVVMDialect::getMinctasmAttrName() ||
1074  attrName == NVVMDialect::getMaxnregAttrName()) {
1075  if (!llvm::dyn_cast<IntegerAttr>(attr.getValue()))
1076  return op->emitError()
1077  << "'" << attrName << "' attribute must be integer constant";
1078  }
1079 
1080  return success();
1081 }
1082 
1083 LogicalResult NVVMDialect::verifyRegionArgAttribute(Operation *op,
1084  unsigned regionIndex,
1085  unsigned argIndex,
1086  NamedAttribute argAttr) {
1087  auto funcOp = dyn_cast<FunctionOpInterface>(op);
1088  if (!funcOp)
1089  return success();
1090 
1091  bool isKernel = op->hasAttr(NVVMDialect::getKernelFuncAttrName());
1092  StringAttr attrName = argAttr.getName();
1093  if (attrName == NVVM::NVVMDialect::getGridConstantAttrName()) {
1094  if (!isKernel) {
1095  return op->emitError()
1096  << "'" << attrName
1097  << "' attribute must be present only on kernel arguments";
1098  }
1099  if (!isa<UnitAttr>(argAttr.getValue()))
1100  return op->emitError() << "'" << attrName << "' must be a unit attribute";
1101  if (!funcOp.getArgAttr(argIndex, LLVM::LLVMDialect::getByValAttrName())) {
1102  return op->emitError()
1103  << "'" << attrName
1104  << "' attribute requires the argument to also have attribute '"
1105  << LLVM::LLVMDialect::getByValAttrName() << "'";
1106  }
1107  }
1108 
1109  return success();
1110 }
1111 
1112 //===----------------------------------------------------------------------===//
1113 // NVVM target attribute.
1114 //===----------------------------------------------------------------------===//
1115 LogicalResult
1117  int optLevel, StringRef triple, StringRef chip,
1118  StringRef features, DictionaryAttr flags,
1119  ArrayAttr files) {
1120  if (optLevel < 0 || optLevel > 3) {
1121  emitError() << "The optimization level must be a number between 0 and 3.";
1122  return failure();
1123  }
1124  if (triple.empty()) {
1125  emitError() << "The target triple cannot be empty.";
1126  return failure();
1127  }
1128  if (chip.empty()) {
1129  emitError() << "The target chip cannot be empty.";
1130  return failure();
1131  }
1132  if (files && !llvm::all_of(files, [](::mlir::Attribute attr) {
1133  return attr && mlir::isa<StringAttr>(attr);
1134  })) {
1135  emitError() << "All the elements in the `link` array must be strings.";
1136  return failure();
1137  }
1138  return success();
1139 }
1140 
1141 #define GET_OP_CLASSES
1142 #include "mlir/Dialect/LLVMIR/NVVMOps.cpp.inc"
1143 
1144 #define GET_ATTRDEF_CLASSES
1145 #include "mlir/Dialect/LLVMIR/NVVMOpsAttributes.cpp.inc"
static MLIRContext * getContext(OpFoldResult val)
static std::pair< mlir::Type, unsigned > inferMMATypeFromMNK(NVVM::MMATypes type, NVVM::MMAFrag frag, int m, int n, int k, MLIRContext *context)
LogicalResult isAllowedSizeN(int sizeN, NVVM::WGMMATypes typeA)
static void printNVVMIntrinsicOp(OpAsmPrinter &p, Operation *op)
Definition: NVVMDialect.cpp:54
FailureOr< int > getAllowedSizeK(NVVM::WGMMATypes typeA)
LogicalResult isAllowedWGMMADataType(NVVM::WGMMATypes typeD, NVVM::WGMMATypes typeA, NVVM::WGMMATypes typeB)
static bool isInt8PtxType(MMATypes type)
static bool isInt4PtxType(MMATypes type)
static bool isIntegerPtxType(MMATypes type)
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
static ArrayRef< int64_t > getShape(Type type)
Returns the shape of the given type.
Definition: Traits.cpp:118
@ OptionalSquare
Square brackets supporting zero or more ops, or nothing.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseOptionalAttrDict(NamedAttrList &result)=0
Parse a named dictionary into 'result' if it is present.
MLIRContext * getContext() const
Definition: AsmPrinter.cpp:73
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
ParseResult addTypeToList(Type type, SmallVectorImpl< Type > &result)
Add the specified type to the end of the specified type list and return success.
virtual ParseResult parseColonType(Type &result)=0
Parse a colon followed by a type.
virtual ParseResult parseColon()=0
Parse a : token.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseArrow()=0
Parse a '->' token.
virtual ParseResult parseLParen()=0
Parse a ( token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
ParseResult parseTypeList(SmallVectorImpl< Type > &result)
Parse a type list.
Definition: AsmPrinter.cpp:77
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
void printArrowTypeList(TypeRange &&types)
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:50
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:183
FloatType getF32Type()
Definition: Builders.cpp:67
IntegerType getI32Type()
Definition: Builders.cpp:87
FloatType getF16Type()
Definition: Builders.cpp:63
MLIRContext * getContext() const
Definition: Builders.h:55
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
Definition: Builders.h:101
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:313
static LLVMStructType getLiteral(MLIRContext *context, ArrayRef< Type > types, bool isPacked=false)
Gets or creates a literal struct with the given body in the provided context.
Definition: LLVMTypes.cpp:453
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
std::optional< NamedAttribute > getNamed(StringRef name) const
Return the specified named attribute if present, std::nullopt otherwise.
NamedAttribute represents a combination of a name and an Attribute value.
Definition: Attributes.h:207
StringAttr getName() const
Return the name of the attribute.
Definition: Attributes.cpp:49
Attribute getValue() const
Return the value of the attribute.
Definition: Attributes.h:221
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
ParseResult resolveOperands(Operands &&operands, Type type, SmallVectorImpl< Value > &result)
Resolve a list of operands to SSA values, emitting an error on failure, or appending the results to t...
virtual ParseResult parseOperandList(SmallVectorImpl< UnresolvedOperand > &result, Delimiter delimiter=Delimiter::None, bool allowResultNumber=true, int requiredOperandCount=-1)=0
Parse zero or more SSA comma-separated operand references with a specified surrounding delimiter,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
void printOperands(const ContainerType &container)
Print a comma separated list of operands.
virtual void printOptionalAttrDict(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary with their values.
This class helps build Operations.
Definition: Builders.h:210
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
bool hasAttr(StringAttr name)
Return true if the operation has an attribute with the provided name, false otherwise.
Definition: Operation.h:555
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Definition: Operation.cpp:268
result_type_range getResultTypes()
Definition: Operation.h:423
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition: Operation.h:373
unsigned getNumResults()
Return the number of results held by this operation.
Definition: Operation.h:399
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:400
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
bool isF64() const
Definition: Types.cpp:53
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
Definition: Types.cpp:35
bool isF32() const
Definition: Types.cpp:52
bool isF16() const
Definition: Types.cpp:50
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
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:129
SmallVector< int64_t, 4 > getCoordinates(ArrayRef< int64_t > basis, unsigned linearIndex)
Type getFixedVectorType(Type elementType, unsigned numElements)
Creates an LLVM dialect-compatible type with the given element type and length.
Definition: LLVMTypes.cpp:953
@ Write
Read register with '+' modifier.
@ ReadWrite
Read register with '=' modifier.
@ Read
Read register with no modifier.
@ kGlobalMemorySpace
Global memory space identifier.
Definition: NVVMDialect.h:36
@ kSharedMemorySpace
Shared memory space identifier.
Definition: NVVMDialect.h:38
std::pair< mlir::Type, unsigned > inferMMAType(mlir::NVVM::MMATypes type, mlir::NVVM::MMAFrag frag, int nRow, int nCol, mlir::MLIRContext *context)
Return the element type and number of elements associated with a wmma matrix of given chracteristics.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Definition: Matchers.h:285
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Definition: Query.cpp:20
uint64_t getN(LevelType lt)
Definition: Enums.h:442
uint64_t getM(LevelType lt)
Definition: Enums.h:443
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition: Utils.cpp:305
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
Definition: Verifier.cpp:421
This represents an operation in an abstracted form, suitable for use with the builder APIs.
SmallVector< Value, 4 > operands
void addOperands(ValueRange newOperands)
void addAttributes(ArrayRef< NamedAttribute > newAttributes)
Add an array of named attributes.
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
void addTypes(ArrayRef< Type > newTypes)
NamedAttrList attributes
SmallVector< Type, 4 > types
Types of the results of this operation.