19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
35 if (typeA == NVVM::WGMMATypes::tf32)
37 if (typeA == NVVM::WGMMATypes::f16 || typeA == NVVM::WGMMATypes::bf16)
39 if (typeA == NVVM::WGMMATypes::s8 || typeA == NVVM::WGMMATypes::u8)
41 if (typeA == NVVM::WGMMATypes::e4m3 || typeA == NVVM::WGMMATypes::e5m2)
43 if (typeA == NVVM::WGMMATypes::b1)
49 NVVM::WGMMATypes typeA,
50 NVVM::WGMMATypes typeB) {
52 case NVVM::WGMMATypes::f16:
53 if ((typeD == NVVM::WGMMATypes::f32 || typeD == NVVM::WGMMATypes::f16) &&
54 typeB == NVVM::WGMMATypes::f16)
57 case NVVM::WGMMATypes::tf32:
58 if (typeD == NVVM::WGMMATypes::f32 && typeB == NVVM::WGMMATypes::tf32)
61 case NVVM::WGMMATypes::u8:
62 case NVVM::WGMMATypes::s8:
63 if (typeD == NVVM::WGMMATypes::s32 &&
64 (typeB == NVVM::WGMMATypes::u8 || typeB == NVVM::WGMMATypes::s8))
67 case NVVM::WGMMATypes::b1:
68 if (typeD == NVVM::WGMMATypes::s32 && typeB == NVVM::WGMMATypes::b1)
71 case NVVM::WGMMATypes::bf16:
72 if (typeD == NVVM::WGMMATypes::f32 && typeB == NVVM::WGMMATypes::bf16)
75 case NVVM::WGMMATypes::e4m3:
76 case NVVM::WGMMATypes::e5m2:
77 if ((typeD == NVVM::WGMMATypes::f32 || typeD == NVVM::WGMMATypes::f16) &&
78 (typeB == NVVM::WGMMATypes::e5m2 || typeB == NVVM::WGMMATypes::e4m3))
83 llvm_unreachable(
"unsupported input types");
91 72, 80, 88, 96, 104, 112, 120, 128,
92 136, 144, 152, 160, 168, 176, 184, 192,
93 200, 208, 216, 224, 232, 240, 248, 256};
95 80, 96, 112, 128, 144, 160,
96 176, 192, 208, 224, 240, 256};
99 case WGMMATypes::tf32:
100 case WGMMATypes::bf16:
101 case WGMMATypes::e4m3:
102 case WGMMATypes::e5m2:
103 if (llvm::is_contained(allowedN, sizeN))
109 if (llvm::is_contained(allowedNshort, sizeN))
112 case WGMMATypes::f32:
113 case WGMMATypes::s32:
114 llvm_unreachable(
"unsupported input types");
120LogicalResult NVVM::WgmmaMmaAsyncOp::verify() {
121 Value outValue = getResults();
122 auto stype = dyn_cast<LLVM::LLVMStructType>(outValue.
getType());
124 return emitOpError() <<
"expected results to be struct";
125 int outputSize = stype.getBody().size();
126 WGMMATypes typeD = getTypeD();
127 WGMMATypes typeA = getTypeA();
128 WGMMATypes typeB = getTypeB();
130 for (
Type t : stype.getBody()) {
131 if (t != stype.getBody().front())
133 <<
"all elements in struct must be same type but there is " << t;
136 if (typeD != WGMMATypes::f32 && typeD != WGMMATypes::f16 &&
137 typeD != WGMMATypes::s32) {
138 return emitOpError() <<
"does not support the given output type " << typeD;
140 if (typeD == WGMMATypes::s32 &&
141 (getScaleA() == WGMMAScaleIn::neg || getScaleB() == WGMMAScaleIn::neg)) {
142 return emitOpError() <<
"has s32 output, scaleA and scaleB cannot be neg";
146 return emitOpError() << typeD <<
" += " << typeA <<
" * " << typeB
147 <<
", it is not supported.";
152 return emitOpError() <<
"shape 'm' must be 64";
157 return emitOpError() <<
"shape 'k' must be " << allowedK.value()
158 <<
" for input type " << typeA;
162 return emitOpError() <<
"has input type " << typeA <<
" n is set to "
163 <<
getShape().getN() <<
", it is not supported.";
170 if ((typeA != WGMMATypes::f16 && typeA != WGMMATypes::bf16) &&
171 (getLayoutA() == mlir::NVVM::MMALayout::col ||
172 getLayoutB() == mlir::NVVM::MMALayout::row)) {
174 <<
"given layouts layout_a = " << getLayoutA()
175 <<
" and layout_b = " << getLayoutB() <<
" for input types " << typeA
177 <<
" requires transpose. However, this is only supported for: "
178 << MMATypes::f16 <<
" and " << MMATypes::bf16;
182 int expectedOutput = 0;
183 if (typeD == WGMMATypes::f32 || typeD == WGMMATypes::s32)
184 expectedOutput =
getShape().getN() / 2;
185 if (typeD == WGMMATypes::f16)
186 expectedOutput =
getShape().getN() / 4;
187 if (outputSize != expectedOutput) {
188 return emitOpError() <<
"results " << expectedOutput
189 <<
", however output struct has " << outputSize
193 if (typeD != WGMMATypes::s32 &&
194 getSatfinite().value_or(NVVM::MMAIntOverflow::wrapped) ==
195 NVVM::MMAIntOverflow::satfinite) {
197 <<
" `satfinite` can be only used with s32 accumulator, however "
198 "the current accumulator is "
205std::string NVVM::WgmmaMmaAsyncOp::getPtx() {
208 bool isF16 = getTypeA() == WGMMATypes::f16 || getTypeA() == WGMMATypes::bf16;
210 StringRef outputTypeName = stringifyWGMMATypes(getTypeD());
212 int expectedOutputRegisters = 0;
213 if (getTypeD() == WGMMATypes::f16)
214 expectedOutputRegisters =
getShape().getN() / 4;
216 expectedOutputRegisters =
getShape().getN() / 2;
219 llvm::raw_string_ostream ss(ptx);
224 << ((expectedOutputRegisters * 2) + 2)
226 "wgmma.mma_async.sync.aligned.m"
227 << m <<
"n" << n <<
"k" << k <<
"." << outputTypeName <<
"." << getTypeA()
228 <<
"." << getTypeB();
229 if (getSatfinite().value_or(NVVM::MMAIntOverflow::wrapped) ==
230 NVVM::MMAIntOverflow::satfinite)
234 for (; regCnt < expectedOutputRegisters; ++regCnt) {
236 if (regCnt != expectedOutputRegisters - 1)
242 regCnt = (regCnt * 2);
243 ss <<
" $" << (regCnt) <<
"," <<
" $" << (regCnt + 1) <<
"," <<
" p";
244 if (getTypeD() != WGMMATypes::s32) {
245 ss <<
", $" << (regCnt + 3) <<
", $" << (regCnt + 4);
249 ss <<
", $" << (regCnt + 5) <<
", $" << (regCnt + 6);
256bool NVVM::WgmmaMmaAsyncOp::getAsmValues(
260 bool isF16 = getTypeA() == WGMMATypes::f16 || getTypeA() == WGMMATypes::bf16;
267 asmValues.push_back({makeConstantI32(rewriter,
static_cast<int>(getScaleD())),
269 if (getTypeD() != WGMMATypes::s32) {
271 {makeConstantI32(rewriter,
272 getScaleA() == NVVM::WGMMAScaleIn::neg ? -1 : 1),
275 {makeConstantI32(rewriter,
276 getScaleB() == NVVM::WGMMAScaleIn::neg ? -1 : 1),
281 {makeConstantI32(rewriter,
static_cast<int>(getLayoutA())),
284 {makeConstantI32(rewriter, 1 -
static_cast<int>(getLayoutB())),
290#define GET_OP_CLASSES
291#include "mlir/Dialect/LLVMIR/NVVMWgmmaOps.cpp.inc"
static LogicalResult isAllowedSizeN(int sizeN, NVVM::WGMMATypes typeA)
static LogicalResult isAllowedWGMMADataType(NVVM::WGMMATypes typeD, NVVM::WGMMATypes typeA, NVVM::WGMMATypes typeB)
static FailureOr< int > getAllowedSizeK(NVVM::WGMMATypes typeA)
static ArrayRef< int64_t > getShape(Type type)
Returns the shape of the given type.
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
@ Write
Write register with '=' modifier.
@ ReadWrite
ReadWrite register with '+' modifier.
@ Read
Read register with no modifier.
uint64_t getN(LevelType lt)
uint64_t getM(LevelType lt)
Include the generated interface declarations.