MLIR 24.0.0git
NVVMWgmmaOps.cpp
Go to the documentation of this file.
1//===- NVVMWgmmaOps.cpp - NVVM dialect wgmma 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 `Wgmma` of the NVVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
16#include "mlir/IR/Diagnostics.h"
18#include "mlir/IR/Types.h"
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"
23
24#include <string>
25#include <utility>
26
27using namespace mlir;
28using namespace mlir::NVVM;
29
30//===----------------------------------------------------------------------===//
31// Verifier methods
32//===----------------------------------------------------------------------===//
33
34static FailureOr<int> getAllowedSizeK(NVVM::WGMMATypes typeA) {
35 if (typeA == NVVM::WGMMATypes::tf32)
36 return 8;
37 if (typeA == NVVM::WGMMATypes::f16 || typeA == NVVM::WGMMATypes::bf16)
38 return 16;
39 if (typeA == NVVM::WGMMATypes::s8 || typeA == NVVM::WGMMATypes::u8)
40 return 32;
41 if (typeA == NVVM::WGMMATypes::e4m3 || typeA == NVVM::WGMMATypes::e5m2)
42 return 32;
43 if (typeA == NVVM::WGMMATypes::b1)
44 return 256;
45 return failure();
46}
47
48static LogicalResult isAllowedWGMMADataType(NVVM::WGMMATypes typeD,
49 NVVM::WGMMATypes typeA,
50 NVVM::WGMMATypes typeB) {
51 switch (typeA) {
52 case NVVM::WGMMATypes::f16:
53 if ((typeD == NVVM::WGMMATypes::f32 || typeD == NVVM::WGMMATypes::f16) &&
54 typeB == NVVM::WGMMATypes::f16)
55 return success();
56 break;
57 case NVVM::WGMMATypes::tf32:
58 if (typeD == NVVM::WGMMATypes::f32 && typeB == NVVM::WGMMATypes::tf32)
59 return success();
60 break;
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))
65 return success();
66 break;
67 case NVVM::WGMMATypes::b1:
68 if (typeD == NVVM::WGMMATypes::s32 && typeB == NVVM::WGMMATypes::b1)
69 return success();
70 break;
71 case NVVM::WGMMATypes::bf16:
72 if (typeD == NVVM::WGMMATypes::f32 && typeB == NVVM::WGMMATypes::bf16)
73 return success();
74 break;
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))
79 return success();
80 break;
81 case WGMMATypes::f32:
82 case WGMMATypes::s32:
83 llvm_unreachable("unsupported input types");
84 break;
85 }
86 return failure();
87}
88
89static LogicalResult isAllowedSizeN(int sizeN, NVVM::WGMMATypes typeA) {
90 SmallVector<int> allowedN = {8, 16, 24, 32, 40, 48, 56, 64,
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};
94 SmallVector<int> allowedNshort = {8, 16, 24, 32, 48, 64,
95 80, 96, 112, 128, 144, 160,
96 176, 192, 208, 224, 240, 256};
97 switch (typeA) {
98 case WGMMATypes::f16:
99 case WGMMATypes::tf32:
100 case WGMMATypes::bf16:
101 case WGMMATypes::e4m3:
102 case WGMMATypes::e5m2:
103 if (llvm::is_contained(allowedN, sizeN))
104 return success();
105 break;
106 case WGMMATypes::u8:
107 case WGMMATypes::s8:
108 case WGMMATypes::b1:
109 if (llvm::is_contained(allowedNshort, sizeN))
110 return success();
111 break;
112 case WGMMATypes::f32:
113 case WGMMATypes::s32:
114 llvm_unreachable("unsupported input types");
115 break;
116 }
117 return failure();
118}
119
120LogicalResult NVVM::WgmmaMmaAsyncOp::verify() {
121 Value outValue = getResults();
122 auto stype = dyn_cast<LLVM::LLVMStructType>(outValue.getType());
123 if (!stype)
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();
129
130 for (Type t : stype.getBody()) {
131 if (t != stype.getBody().front())
132 return emitOpError()
133 << "all elements in struct must be same type but there is " << t;
134 }
135
136 if (typeD != WGMMATypes::f32 && typeD != WGMMATypes::f16 &&
137 typeD != WGMMATypes::s32) {
138 return emitOpError() << "does not support the given output type " << typeD;
139 }
140 if (typeD == WGMMATypes::s32 &&
141 (getScaleA() == WGMMAScaleIn::neg || getScaleB() == WGMMAScaleIn::neg)) {
142 return emitOpError() << "has s32 output, scaleA and scaleB cannot be neg";
143 }
144
145 if (failed(isAllowedWGMMADataType(typeD, typeA, typeB))) {
146 return emitOpError() << typeD << " += " << typeA << " * " << typeB
147 << ", it is not supported.";
148 }
149
150 // Check M
151 if (getShape().getM() != 64)
152 return emitOpError() << "shape 'm' must be 64";
153
154 // Check K
155 FailureOr<int> allowedK = getAllowedSizeK(typeA);
156 if (failed(allowedK) || allowedK.value() != getShape().getK())
157 return emitOpError() << "shape 'k' must be " << allowedK.value()
158 << " for input type " << typeA;
159
160 // Check N
161 if (failed(isAllowedSizeN(getShape().getN(), typeA))) {
162 return emitOpError() << "has input type " << typeA << " n is set to "
163 << getShape().getN() << ", it is not supported.";
164 }
165
166 // Check transpose (only available for f16/bf16)
167 // Matrices A should be stored in row-major and B in column-major.
168 // Only f16/bf16 matrices can be stored in either column-major or row-major
169 // by setting the transpose value(imm-trans-a,imm-trans-b) in PTX code.
170 if ((typeA != WGMMATypes::f16 && typeA != WGMMATypes::bf16) &&
171 (getLayoutA() == mlir::NVVM::MMALayout::col ||
172 getLayoutB() == mlir::NVVM::MMALayout::row)) {
173 return emitOpError()
174 << "given layouts layout_a = " << getLayoutA()
175 << " and layout_b = " << getLayoutB() << " for input types " << typeA
176 << " and " << typeB
177 << " requires transpose. However, this is only supported for: "
178 << MMATypes::f16 << " and " << MMATypes::bf16;
179 }
180
181 // Check result registers
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
190 << " elements";
191 }
192 // Check satfinite (only available for s32 accumulator)
193 if (typeD != WGMMATypes::s32 &&
194 getSatfinite().value_or(NVVM::MMAIntOverflow::wrapped) ==
195 NVVM::MMAIntOverflow::satfinite) {
196 return emitOpError()
197 << " `satfinite` can be only used with s32 accumulator, however "
198 "the current accumulator is "
199 << typeD;
200 }
201
202 return success();
203}
204
205std::string NVVM::WgmmaMmaAsyncOp::getPtx() {
206
207 int m = getShape().getM(), n = getShape().getN(), k = getShape().getK();
208 bool isF16 = getTypeA() == WGMMATypes::f16 || getTypeA() == WGMMATypes::bf16;
209
210 StringRef outputTypeName = stringifyWGMMATypes(getTypeD());
211
212 int expectedOutputRegisters = 0;
213 if (getTypeD() == WGMMATypes::f16)
214 expectedOutputRegisters = getShape().getN() / 4;
215 else
216 expectedOutputRegisters = getShape().getN() / 2;
217
218 std::string ptx;
219 llvm::raw_string_ostream ss(ptx);
220
221 ss << "{\n"
222 ".reg .pred p;\n"
223 "setp.ne.b32 p, $"
224 << ((expectedOutputRegisters * 2) + 2)
225 << ", 0;\n"
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)
231 ss << ".satfinite";
232 ss << " {";
233 int regCnt = 0;
234 for (; regCnt < expectedOutputRegisters; ++regCnt) {
235 ss << "$" << regCnt;
236 if (regCnt != expectedOutputRegisters - 1)
237 ss << ", ";
238 }
239
240 ss << "},";
241 // Need to map read/write registers correctly.
242 regCnt = (regCnt * 2);
243 ss << " $" << (regCnt) << "," << " $" << (regCnt + 1) << "," << " p";
244 if (getTypeD() != WGMMATypes::s32) {
245 ss << ", $" << (regCnt + 3) << ", $" << (regCnt + 4);
246 }
247 // Don't add transpose parameters unless needed.
248 if (isF16) {
249 ss << ", $" << (regCnt + 5) << ", $" << (regCnt + 6);
250 }
251 ss << ";\n"
252 << "}\n";
253 return ptx;
254}
255
256bool NVVM::WgmmaMmaAsyncOp::getAsmValues(
257 RewriterBase &rewriter,
258 llvm::SmallVectorImpl<std::pair<mlir::Value, mlir::NVVM::PTXRegisterMod>>
259 &asmValues) {
260 bool isF16 = getTypeA() == WGMMATypes::f16 || getTypeA() == WGMMATypes::bf16;
261 if (getResults())
262 asmValues.push_back({getResults(), mlir::NVVM::PTXRegisterMod::Write});
263 if (getInouts())
264 asmValues.push_back({getInouts(), mlir::NVVM::PTXRegisterMod::ReadWrite});
265 asmValues.push_back({getDescriptorA(), mlir::NVVM::PTXRegisterMod::Read});
266 asmValues.push_back({getDescriptorB(), mlir::NVVM::PTXRegisterMod::Read});
267 asmValues.push_back({makeConstantI32(rewriter, static_cast<int>(getScaleD())),
269 if (getTypeD() != WGMMATypes::s32) {
270 asmValues.push_back(
271 {makeConstantI32(rewriter,
272 getScaleA() == NVVM::WGMMAScaleIn::neg ? -1 : 1),
274 asmValues.push_back(
275 {makeConstantI32(rewriter,
276 getScaleB() == NVVM::WGMMAScaleIn::neg ? -1 : 1),
278 }
279 if (isF16) {
280 asmValues.push_back(
281 {makeConstantI32(rewriter, static_cast<int>(getLayoutA())),
283 asmValues.push_back(
284 {makeConstantI32(rewriter, 1 - static_cast<int>(getLayoutB())),
286 }
287 return true; // Has manual mapping
288}
289
290#define GET_OP_CLASSES
291#include "mlir/Dialect/LLVMIR/NVVMWgmmaOps.cpp.inc"
return success()
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.
Definition Traits.cpp:117
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...
Definition Types.h:74
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
@ Write
Write register with '=' modifier.
@ ReadWrite
ReadWrite register with '+' modifier.
@ Read
Read register with no modifier.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
uint64_t getN(LevelType lt)
Definition Enums.h:442
uint64_t getM(LevelType lt)
Definition Enums.h:443
Include the generated interface declarations.