MLIR 24.0.0git
NVVMConvertOps.cpp
Go to the documentation of this file.
1//===- NVVMConvertOps.cpp - NVVM dialect conversion 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 `Convert` of the NVVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
15#include "IR/NVVMOps.h"
17#include "mlir/IR/Diagnostics.h"
18#include "mlir/IR/MLIRContext.h"
19#include "mlir/IR/Operation.h"
20#include "mlir/IR/Types.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/TypeSwitch.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/IntrinsicsNVPTX.h"
26#include "llvm/Support/Casting.h"
27
28#include <utility>
29
30using namespace mlir;
31using namespace mlir::NVVM;
32
33//===----------------------------------------------------------------------===//
34// Verifier methods
35//===----------------------------------------------------------------------===//
36
37LogicalResult ConvertFloatToTF32Op::verify() {
38 using RndMode = NVVM::FPRoundingMode;
39 switch (getRnd()) {
40 case RndMode::RNA:
41 if (getRelu())
42 return emitError("Relu not supported with rna rounding mode.");
43 break;
44 case RndMode::RN:
45 case RndMode::RZ:
46 break;
47 default:
48 return emitError(
49 "Only {rn,rz,rna} rounding modes supported for ConvertFloatToTF32Op.");
50 }
51 return success();
52}
53
54LogicalResult ConvertF32x2ToF6x2Op::verify() {
56
57 if (!llvm::isa<mlir::Float6E2M3FNType, mlir::Float6E3M2FNType>(getDstTy())) {
58 return emitOpError("Only ")
59 << mlir::Float6E2M3FNType::get(ctx) << " and "
60 << mlir::Float6E3M2FNType::get(ctx)
61 << " types are supported for conversions from f32x2 to f6x2.";
62 }
63 return success();
64}
65
66LogicalResult ConvertF32x2ToF8x2Op::verify() {
67 using RndMode = NVVM::FPRoundingMode;
68 using SatMode = NVVM::SaturationMode;
69
70 bool isRoundingModeRN = getRnd() == RndMode::RN;
71 bool isRoundingModeRZ = getRnd() == RndMode::RZ;
72 bool isRoundingModeRP = getRnd() == RndMode::RP;
73 bool isSatFinite = getSat() == SatMode::SATFINITE;
74
75 bool hasRelu = getRelu();
76 bool hasPzo = getPzo();
77
79 bool hasScale = static_cast<bool>(getScaleFactor());
80
82 .Case<mlir::Float8E4M3FNType, mlir::Float8E5M2Type>(
83 [&](mlir::Type) -> LogicalResult {
84 if (!(isRoundingModeRN || isRoundingModeRZ)) {
85 return emitOpError("Only RN and RZ rounding modes are supported "
86 "for conversions from f32x2 to ")
87 << mlir::Float8E4M3FNType::get(ctx) << " and "
88 << mlir::Float8E5M2Type::get(ctx) << " types";
89 }
90 if (!isSatFinite) {
91 return emitOpError("Only SATFINITE saturation mode is supported "
92 "for conversions "
93 "from f32x2 to ")
94 << mlir::Float8E4M3FNType::get(ctx) << " and "
95 << mlir::Float8E5M2Type::get(ctx) << " types";
96 }
97 return success();
98 })
99 .Case<mlir::Float8E8M0FNUType>([&](mlir::Type) -> LogicalResult {
100 if (!(isRoundingModeRZ || isRoundingModeRP)) {
101 return emitOpError("Only RZ and RP rounding modes are supported for "
102 "conversions from f32x2 to ")
103 << mlir::Float8E8M0FNUType::get(ctx) << " type";
104 }
105 if (hasRelu || hasPzo) {
106 return emitOpError() << (hasRelu ? "relu" : "pzo")
107 << " not supported for conversions to "
108 << mlir::Float8E8M0FNUType::get(ctx) << " type";
109 }
110 if (hasScale)
111 return emitOpError("scale not supported for conversions to ")
112 << mlir::Float8E8M0FNUType::get(ctx) << " type";
113 return success();
114 })
115 .Case<mlir::Float8E5M3FNUType>([&](mlir::Type) -> LogicalResult {
116 if (!(isRoundingModeRN || isRoundingModeRZ || isRoundingModeRP))
117 return emitOpError("Only RN, RZ, and RP rounding modes are supported "
118 "for conversions from f32x2 to ")
119 << mlir::Float8E5M3FNUType::get(ctx) << " type";
120 if (hasRelu || hasPzo)
121 return emitOpError() << (hasRelu ? "relu" : "pzo")
122 << " not supported for conversions to "
123 << mlir::Float8E5M3FNUType::get(ctx) << " type";
124 if (hasScale && isRoundingModeRP)
125 return emitOpError("scale not supported for conversions to ")
126 << mlir::Float8E5M3FNUType::get(ctx)
127 << " type with RP rounding mode";
128 return success();
129 })
130 .Default([&](mlir::Type) {
131 return emitOpError("Only ")
132 << mlir::Float8E4M3FNType::get(ctx) << ", "
133 << mlir::Float8E5M2Type::get(ctx) << ", "
134 << mlir::Float8E5M3FNUType::get(ctx) << ", and "
135 << mlir::Float8E8M0FNUType::get(ctx)
136 << " types are "
137 "supported for conversions from f32x2 to f8x2";
138 });
139}
140
141LogicalResult ConvertF16x2ToF8x2Op::verify() {
143 bool hasScale = static_cast<bool>(getScaleFactor());
144
145 if (llvm::isa<mlir::Float8E4M3FNType, mlir::Float8E5M2Type>(getDstTy())) {
146 if (getRnd() != FPRoundingMode::RN && getRnd() != FPRoundingMode::RZ)
147 return emitOpError("Only RN and RZ rounding modes are supported for "
148 "conversions from f16x2 to ")
149 << mlir::Float8E4M3FNType::get(ctx) << " and "
150 << mlir::Float8E5M2Type::get(ctx) << " types";
151 if (getSat() != SaturationMode::NONE)
152 return emitOpError("Only NONE saturation mode is supported for "
153 "conversions from f16x2 to ")
154 << mlir::Float8E4M3FNType::get(ctx) << " and "
155 << mlir::Float8E5M2Type::get(ctx) << " types";
156 return success();
157 }
158 if (llvm::isa<mlir::Float8E5M3FNUType>(getDstTy())) {
159 if (getRnd() != FPRoundingMode::RN && getRnd() != FPRoundingMode::RZ &&
160 getRnd() != FPRoundingMode::RP)
161 return emitOpError("Only RN, RZ, and RP rounding modes are supported "
162 "for conversions from f16x2 to ")
163 << mlir::Float8E5M3FNUType::get(ctx) << " type";
164 if (getRelu() || getPzo())
165 return emitOpError() << (getRelu() ? "relu" : "pzo")
166 << " not supported for conversions to "
167 << mlir::Float8E5M3FNUType::get(ctx) << " type";
168 if (hasScale && getRnd() == FPRoundingMode::RP)
169 return emitOpError("scale not supported for conversions to ")
170 << mlir::Float8E5M3FNUType::get(ctx)
171 << " type with RP rounding mode";
172 return success();
173 }
174 {
175 return emitOpError("Only ")
176 << mlir::Float8E5M3FNUType::get(ctx) << ", "
177 << mlir::Float8E4M3FNType::get(ctx) << ", and "
178 << mlir::Float8E5M2Type::get(ctx)
179 << " types are supported for conversions from f16x2 to f8x2.";
180 }
181}
182
183LogicalResult ConvertBF16x2ToF8x2Op::verify() {
184 using RndMode = NVVM::FPRoundingMode;
185 using SatMode = NVVM::SaturationMode;
186
187 bool isRoundingModeRN = getRnd() == RndMode::RN;
188 bool isRoundingModeRZ = getRnd() == RndMode::RZ;
189 bool isRoundingModeRP = getRnd() == RndMode::RP;
190 bool isSatFinite = getSat() == SatMode::SATFINITE;
191 bool hasRelu = getRelu();
192 bool hasPzo = getPzo();
193
195 bool hasScale = static_cast<bool>(getScaleFactor());
196
198 .Case<mlir::Float8E4M3FNType, mlir::Float8E5M2Type>(
199 [&](mlir::Type) -> LogicalResult {
200 if (!(isRoundingModeRN || isRoundingModeRZ))
201 return emitOpError("Only RN and RZ rounding modes are supported "
202 "for conversions from bf16x2 to ")
203 << mlir::Float8E4M3FNType::get(ctx) << " and "
204 << mlir::Float8E5M2Type::get(ctx) << " types";
205 if (!isSatFinite)
206 return emitOpError("Only SATFINITE saturation mode is supported "
207 "for conversions from bf16x2 to ")
208 << mlir::Float8E4M3FNType::get(ctx) << " and "
209 << mlir::Float8E5M2Type::get(ctx) << " types";
210 return success();
211 })
212 .Case<mlir::Float8E8M0FNUType>([&](mlir::Type) -> LogicalResult {
213 if (!(isRoundingModeRZ || isRoundingModeRP))
214 return emitOpError("Only RZ and RP rounding modes are supported for "
215 "conversions from bf16x2 to ")
216 << mlir::Float8E8M0FNUType::get(ctx) << " type";
217 if (hasRelu || hasPzo)
218 return emitOpError() << (hasRelu ? "relu" : "pzo")
219 << " not supported for conversions to "
220 << mlir::Float8E8M0FNUType::get(ctx) << " type";
221 if (hasScale)
222 return emitOpError("scale not supported for conversions to ")
223 << mlir::Float8E8M0FNUType::get(ctx) << " type";
224 return success();
225 })
226 .Case<mlir::Float8E5M3FNUType>([&](mlir::Type) -> LogicalResult {
227 if (!(isRoundingModeRN || isRoundingModeRZ || isRoundingModeRP))
228 return emitOpError("Only RN, RZ, and RP rounding modes are supported "
229 "for conversions from bf16x2 to ")
230 << mlir::Float8E5M3FNUType::get(ctx) << " type";
231 if (hasRelu || hasPzo)
232 return emitOpError() << (hasRelu ? "relu" : "pzo")
233 << " not supported for conversions to "
234 << mlir::Float8E5M3FNUType::get(ctx) << " type";
235 if (hasScale && isRoundingModeRP)
236 return emitOpError("scale not supported for conversions to ")
237 << mlir::Float8E5M3FNUType::get(ctx)
238 << " type with RP rounding mode";
239 return success();
240 })
241 .Default([&](mlir::Type) -> LogicalResult {
242 llvm_unreachable("Invalid conversion in ConvertBF16x2ToF8x2Op");
243 return failure();
244 });
245}
246
247LogicalResult ConvertF32x2ToF4x2Op::verify() {
249
250 if (!llvm::isa<mlir::Float4E2M1FNType>(getDstTy()))
251 return emitOpError("Only ")
252 << mlir::Float4E2M1FNType::get(ctx)
253 << " type is supported for conversions from f32x2 to f4x2.";
254
255 return success();
256}
257
258LogicalResult ConvertF8x2ToF16x2Op::verify() {
260 if (llvm::isa<Float8E5M3FNUType>(getSrcType()) && getRelu())
261 return emitOpError("relu not supported for conversions from ")
262 << Float8E5M3FNUType::get(ctx) << " type.";
263 return success();
264}
265
266LogicalResult ConvertF8x2ToBF16x2Op::verify() {
268 if (llvm::isa<Float8E8M0FNUType>(getSrcType())) {
269 if (getSat() != SaturationMode::NONE)
270 return emitOpError(
271 "Only NONE saturation mode is supported for conversions from ")
272 << Float8E8M0FNUType::get(ctx) << " type";
273 if (getScaleFactor())
274 return emitOpError("scaleFactor not supported for conversions from ")
275 << Float8E8M0FNUType::get(ctx) << " type";
276 if (getRelu())
277 return emitOpError("relu not supported for conversions from ")
278 << Float8E8M0FNUType::get(ctx) << " type";
279 }
280 if (llvm::isa<Float8E5M3FNUType>(getSrcType()) && getRelu())
281 return emitOpError("relu not supported for conversions from ")
282 << Float8E5M3FNUType::get(ctx) << " type";
283
284 return success();
285}
286
287//===----------------------------------------------------------------------===//
288// Stochastic Rounding Conversion Ops
289//===----------------------------------------------------------------------===//
290
291static LogicalResult verifyConvertF32x2ToFP16x2Op(Twine dstType,
292 FPRoundingMode rnd,
293 bool hasRandomBits,
294 bool hasPzo, Operation *op) {
295 static constexpr FPRoundingMode validRndModes[] = {
296 FPRoundingMode::RN, FPRoundingMode::RZ, FPRoundingMode::RS};
297
298 if (!llvm::is_contained(validRndModes, rnd)) {
299 return op->emitOpError(
300 "Only RN, RZ, and RS rounding modes are supported for "
301 "conversions from f32x2 to ")
302 << dstType << ".";
303 }
304
305 if (rnd == FPRoundingMode::RS) {
306 if (!hasRandomBits)
307 return op->emitOpError("random_bits is required for RS rounding mode.");
308 if (hasPzo)
309 return op->emitOpError("pzo not supported for RS rounding mode.");
310 } else {
311 if (hasRandomBits) {
312 return op->emitOpError(
313 "random_bits not supported for RN and RZ rounding modes.");
314 }
315 }
316
317 return success();
318}
319
320LogicalResult ConvertF32x2ToF16x2Op::verify() {
322 "f16x2", getRnd(), getRandomBits() ? true : false, getPzo(), *this);
323}
324
325LogicalResult ConvertF32x2ToBF16x2Op::verify() {
327 "bf16x2", getRnd(), getRandomBits() ? true : false, getPzo(), *this);
328}
329
330LogicalResult ConvertF32x4ToF8x4Op::verify() {
332
333 if (!llvm::isa<mlir::Float8E4M3FNType, mlir::Float8E5M2Type>(getDstTy()))
334 return emitOpError("Only ")
335 << mlir::Float8E4M3FNType::get(ctx) << " and "
336 << mlir::Float8E5M2Type::get(ctx)
337 << " types are supported for conversions from f32x4 to f8x4.";
338
339 return success();
340}
341
342LogicalResult ConvertF32x4ToF6x4Op::verify() {
344
345 if (!llvm::isa<mlir::Float6E2M3FNType, mlir::Float6E3M2FNType>(getDstTy()))
346 return emitOpError("Only ")
347 << mlir::Float6E2M3FNType::get(ctx) << " and "
348 << mlir::Float6E3M2FNType::get(ctx)
349 << " types are supported for conversions from f32x4 to f6x4.";
350
351 return success();
352}
353
354LogicalResult ConvertF32x4ToF4x4Op::verify() {
356
357 if (!llvm::isa<mlir::Float4E2M1FNType>(getDstTy()))
358 return emitOpError("Only ") << mlir::Float4E2M1FNType::get(ctx)
359 << " type is supported for conversions from "
360 "f32x4 to f4x4.";
361
362 return success();
363}
364
365//===----------------------------------------------------------------------===//
366// getIntrinsicID/getIntrinsicIDAndArgs methods
367//===----------------------------------------------------------------------===//
368
369#define _none
370
371#define CVT_F2TF32_ID_IMPL(rnd, relu, sf) \
372 hasRelu ? llvm::Intrinsic::nvvm_f2tf32_##rnd##relu##sf \
373 : llvm::Intrinsic::nvvm_f2tf32_##rnd##sf
374
375#define GET_CVT_F2TF32_ID(rnd, relu, sf) \
376 hasSatFinite ? CVT_F2TF32_ID_IMPL(rnd, relu, sf) \
377 : CVT_F2TF32_ID_IMPL(rnd, relu, )
378
379llvm::Intrinsic::ID
380ConvertFloatToTF32Op::getIntrinsicID(NVVM::FPRoundingMode rnd,
381 NVVM::SaturationMode sat, bool hasRelu) {
382 using RndMode = NVVM::FPRoundingMode;
383 bool hasSatFinite = (sat == NVVM::SaturationMode::SATFINITE);
384 switch (rnd) {
385 case RndMode::RN:
386 return GET_CVT_F2TF32_ID(rn, _relu, _satfinite);
387 case RndMode::RZ:
388 return GET_CVT_F2TF32_ID(rz, _relu, _satfinite);
389 case RndMode::RNA:
390 return GET_CVT_F2TF32_ID(rna, _none, _satfinite);
391 default:
392 llvm_unreachable("Invalid RoundingMode for CvtFloatToTF32Op");
393 }
394}
395
396static void appendN1ScaleArg(Value scaleFactor, LLVM::ModuleTranslation &mt,
397 llvm::IRBuilderBase &builder,
399 if (scaleFactor)
400 args.push_back(
401 builder.CreateZExt(mt.lookupValue(scaleFactor),
402 llvm::Type::getInt16Ty(builder.getContext())));
403}
404
406ConvertF32x2ToF4x2Op::getIntrinsicIDAndArgs(NVVM::ConvertF32x2ToF4x2Op op,
408 llvm::IRBuilderBase &builder) {
410 args.push_back(mt.lookupValue(op.getA()));
411 args.push_back(mt.lookupValue(op.getB()));
412 appendN1ScaleArg(op.getScaleFactor(), mt, builder, args);
413 args.push_back(builder.getInt1(op.getPzo()));
414
415 unsigned idx =
416 (static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RZ) << 1) |
417 static_cast<unsigned>(op.getRelu());
418 static constexpr llvm::Intrinsic::ID ids[] = {
419 llvm::Intrinsic::nvvm_ff_to_e2m1x2_rn_satfinite,
420 llvm::Intrinsic::nvvm_ff_to_e2m1x2_rn_relu_satfinite,
421 llvm::Intrinsic::nvvm_ff_to_e2m1x2_rz_satfinite,
422 llvm::Intrinsic::nvvm_ff_to_e2m1x2_rz_relu_satfinite,
423 };
424 static constexpr llvm::Intrinsic::ID scaledIds[] = {
425 llvm::Intrinsic::nvvm_ff_to_e2m1x2_rn_satfinite_scale_n1_ue8m0,
426 llvm::Intrinsic::nvvm_ff_to_e2m1x2_rn_relu_satfinite_scale_n1_ue8m0,
427 llvm::Intrinsic::nvvm_ff_to_e2m1x2_rz_satfinite_scale_n1_ue8m0,
428 llvm::Intrinsic::nvvm_ff_to_e2m1x2_rz_relu_satfinite_scale_n1_ue8m0,
429 };
430
431 return {(op.getScaleFactor() ? scaledIds : ids)[idx], std::move(args)};
432}
433
435ConvertF32x2ToF6x2Op::getIntrinsicIDAndArgs(NVVM::ConvertF32x2ToF6x2Op &op,
437 llvm::IRBuilderBase &builder) {
438 unsigned idx =
439 (static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RZ) << 1) |
440 static_cast<unsigned>(op.getRelu());
441 bool hasScale = static_cast<bool>(op.getScaleFactor());
442 static constexpr llvm::Intrinsic::ID e2m3x2IDs[] = {
443 llvm::Intrinsic::nvvm_ff_to_e2m3x2_rn_satfinite,
444 llvm::Intrinsic::nvvm_ff_to_e2m3x2_rn_relu_satfinite,
445 llvm::Intrinsic::nvvm_ff_to_e2m3x2_rz_satfinite,
446 llvm::Intrinsic::nvvm_ff_to_e2m3x2_rz_relu_satfinite,
447 };
448 static constexpr llvm::Intrinsic::ID e3m2x2IDs[] = {
449 llvm::Intrinsic::nvvm_ff_to_e3m2x2_rn_satfinite,
450 llvm::Intrinsic::nvvm_ff_to_e3m2x2_rn_relu_satfinite,
451 llvm::Intrinsic::nvvm_ff_to_e3m2x2_rz_satfinite,
452 llvm::Intrinsic::nvvm_ff_to_e3m2x2_rz_relu_satfinite,
453 };
454 static constexpr llvm::Intrinsic::ID e2m3x2ScaledIDs[] = {
455 llvm::Intrinsic::nvvm_ff_to_e2m3x2_rn_satfinite_scale_n1_ue8m0,
456 llvm::Intrinsic::nvvm_ff_to_e2m3x2_rn_relu_satfinite_scale_n1_ue8m0,
457 llvm::Intrinsic::nvvm_ff_to_e2m3x2_rz_satfinite_scale_n1_ue8m0,
458 llvm::Intrinsic::nvvm_ff_to_e2m3x2_rz_relu_satfinite_scale_n1_ue8m0,
459 };
460 static constexpr llvm::Intrinsic::ID e3m2x2ScaledIDs[] = {
461 llvm::Intrinsic::nvvm_ff_to_e3m2x2_rn_satfinite_scale_n1_ue8m0,
462 llvm::Intrinsic::nvvm_ff_to_e3m2x2_rn_relu_satfinite_scale_n1_ue8m0,
463 llvm::Intrinsic::nvvm_ff_to_e3m2x2_rz_satfinite_scale_n1_ue8m0,
464 llvm::Intrinsic::nvvm_ff_to_e3m2x2_rz_relu_satfinite_scale_n1_ue8m0,
465 };
466
467 llvm::Intrinsic::ID intId =
469 .Case([&](mlir::Float6E2M3FNType) {
470 return (hasScale ? e2m3x2ScaledIDs : e2m3x2IDs)[idx];
471 })
472 .Case([&](mlir::Float6E3M2FNType) {
473 return (hasScale ? e3m2x2ScaledIDs : e3m2x2IDs)[idx];
474 })
475 .Default([](mlir::Type) {
476 llvm_unreachable("Invalid conversion in ConvertF32x2ToF6x2Op");
477 return llvm::Intrinsic::not_intrinsic;
478 });
479
480 llvm::SmallVector<llvm::Value *> args = {mt.lookupValue(op.getA()),
481 mt.lookupValue(op.getB())};
482 appendN1ScaleArg(op.getScaleFactor(), mt, builder, args);
483 args.push_back(builder.getInt1(op.getPzo()));
484 return {intId, std::move(args)};
485}
486
488ConvertF16x2ToF4x2Op::getIntrinsicIDAndArgs(NVVM::ConvertF16x2ToF4x2Op &op,
490 llvm::IRBuilderBase &builder) {
491 mlir::Type dstTy = op.getDstTy();
492
493 unsigned idx =
494 (static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RZ) << 1) |
495 static_cast<unsigned>(op.getRelu());
496 static constexpr llvm::Intrinsic::ID ids[] = {
497 llvm::Intrinsic::nvvm_f16x2_to_e2m1x2_rn_satfinite,
498 llvm::Intrinsic::nvvm_f16x2_to_e2m1x2_rn_relu_satfinite,
499 llvm::Intrinsic::nvvm_f16x2_to_e2m1x2_rz_satfinite,
500 llvm::Intrinsic::nvvm_f16x2_to_e2m1x2_rz_relu_satfinite,
501 };
502 static constexpr llvm::Intrinsic::ID scaledIds[] = {
503 llvm::Intrinsic::nvvm_f16x2_to_e2m1x2_rn_satfinite_scale_n1_ue8m0,
504 llvm::Intrinsic::nvvm_f16x2_to_e2m1x2_rn_relu_satfinite_scale_n1_ue8m0,
505 llvm::Intrinsic::nvvm_f16x2_to_e2m1x2_rz_satfinite_scale_n1_ue8m0,
506 llvm::Intrinsic::nvvm_f16x2_to_e2m1x2_rz_relu_satfinite_scale_n1_ue8m0,
507 };
508
509 if (!llvm::isa<mlir::Float4E2M1FNType>(dstTy))
510 llvm_unreachable("Invalid conversion in ConvertF16x2ToF4x2Op");
511
513 args.push_back(mt.lookupValue(op.getSrc()));
514 appendN1ScaleArg(op.getScaleFactor(), mt, builder, args);
515 args.push_back(builder.getInt1(op.getPzo()));
516
517 return {(op.getScaleFactor() ? scaledIds : ids)[idx], std::move(args)};
518}
519
521ConvertBF16x2ToF4x2Op::getIntrinsicIDAndArgs(NVVM::ConvertBF16x2ToF4x2Op &op,
523 llvm::IRBuilderBase &builder) {
524 mlir::Type dstTy = op.getDstTy();
525
526 unsigned idx =
527 (static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RZ) << 1) |
528 static_cast<unsigned>(op.getRelu());
529 static constexpr llvm::Intrinsic::ID ids[] = {
530 llvm::Intrinsic::nvvm_bf16x2_to_e2m1x2_rn_satfinite,
531 llvm::Intrinsic::nvvm_bf16x2_to_e2m1x2_rn_relu_satfinite,
532 llvm::Intrinsic::nvvm_bf16x2_to_e2m1x2_rz_satfinite,
533 llvm::Intrinsic::nvvm_bf16x2_to_e2m1x2_rz_relu_satfinite,
534 };
535 static constexpr llvm::Intrinsic::ID scaledIds[] = {
536 llvm::Intrinsic::nvvm_bf16x2_to_e2m1x2_rn_satfinite_scale_n1_ue8m0,
537 llvm::Intrinsic::nvvm_bf16x2_to_e2m1x2_rn_relu_satfinite_scale_n1_ue8m0,
538 llvm::Intrinsic::nvvm_bf16x2_to_e2m1x2_rz_satfinite_scale_n1_ue8m0,
539 llvm::Intrinsic::nvvm_bf16x2_to_e2m1x2_rz_relu_satfinite_scale_n1_ue8m0,
540 };
541
542 if (!llvm::isa<mlir::Float4E2M1FNType>(dstTy))
543 llvm_unreachable("Invalid conversion in ConvertBF16x2ToF4x2Op");
544
546 args.push_back(mt.lookupValue(op.getSrc()));
547 appendN1ScaleArg(op.getScaleFactor(), mt, builder, args);
548 args.push_back(builder.getInt1(op.getPzo()));
549
550 return {(op.getScaleFactor() ? scaledIds : ids)[idx], std::move(args)};
551}
552
554ConvertF16x2ToF6x2Op::getIntrinsicIDAndArgs(NVVM::ConvertF16x2ToF6x2Op &op,
556 llvm::IRBuilderBase &builder) {
557 unsigned idx =
558 (static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RZ) << 1) |
559 static_cast<unsigned>(op.getRelu());
560 bool hasScale = static_cast<bool>(op.getScaleFactor());
561 static constexpr llvm::Intrinsic::ID e2m3x2IDs[] = {
562 llvm::Intrinsic::nvvm_f16x2_to_e2m3x2_rn_satfinite,
563 llvm::Intrinsic::nvvm_f16x2_to_e2m3x2_rn_relu_satfinite,
564 llvm::Intrinsic::nvvm_f16x2_to_e2m3x2_rz_satfinite,
565 llvm::Intrinsic::nvvm_f16x2_to_e2m3x2_rz_relu_satfinite,
566 };
567 static constexpr llvm::Intrinsic::ID e3m2x2IDs[] = {
568 llvm::Intrinsic::nvvm_f16x2_to_e3m2x2_rn_satfinite,
569 llvm::Intrinsic::nvvm_f16x2_to_e3m2x2_rn_relu_satfinite,
570 llvm::Intrinsic::nvvm_f16x2_to_e3m2x2_rz_satfinite,
571 llvm::Intrinsic::nvvm_f16x2_to_e3m2x2_rz_relu_satfinite,
572 };
573 static constexpr llvm::Intrinsic::ID e2m3x2ScaledIDs[] = {
574 llvm::Intrinsic::nvvm_f16x2_to_e2m3x2_rn_satfinite_scale_n1_ue8m0,
575 llvm::Intrinsic::nvvm_f16x2_to_e2m3x2_rn_relu_satfinite_scale_n1_ue8m0,
576 llvm::Intrinsic::nvvm_f16x2_to_e2m3x2_rz_satfinite_scale_n1_ue8m0,
577 llvm::Intrinsic::nvvm_f16x2_to_e2m3x2_rz_relu_satfinite_scale_n1_ue8m0,
578 };
579 static constexpr llvm::Intrinsic::ID e3m2x2ScaledIDs[] = {
580 llvm::Intrinsic::nvvm_f16x2_to_e3m2x2_rn_satfinite_scale_n1_ue8m0,
581 llvm::Intrinsic::nvvm_f16x2_to_e3m2x2_rn_relu_satfinite_scale_n1_ue8m0,
582 llvm::Intrinsic::nvvm_f16x2_to_e3m2x2_rz_satfinite_scale_n1_ue8m0,
583 llvm::Intrinsic::nvvm_f16x2_to_e3m2x2_rz_relu_satfinite_scale_n1_ue8m0,
584 };
585
586 llvm::Intrinsic::ID intId =
588 .Case<mlir::Float6E2M3FNType>([&](mlir::Float6E2M3FNType) {
589 return (hasScale ? e2m3x2ScaledIDs : e2m3x2IDs)[idx];
590 })
591 .Case<mlir::Float6E3M2FNType>([&](mlir::Float6E3M2FNType) {
592 return (hasScale ? e3m2x2ScaledIDs : e3m2x2IDs)[idx];
593 })
594 .Default([](mlir::Type) {
595 llvm_unreachable("Invalid conversion in ConvertF16x2ToF6x2Op");
596 return llvm::Intrinsic::not_intrinsic;
597 });
598 llvm::SmallVector<llvm::Value *> args = {mt.lookupValue(op.getSrc())};
599 appendN1ScaleArg(op.getScaleFactor(), mt, builder, args);
600 args.push_back(builder.getInt1(op.getPzo()));
601 return {intId, std::move(args)};
602}
603
605ConvertBF16x2ToF6x2Op::getIntrinsicIDAndArgs(NVVM::ConvertBF16x2ToF6x2Op &op,
607 llvm::IRBuilderBase &builder) {
608 unsigned idx =
609 (static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RZ) << 1) |
610 static_cast<unsigned>(op.getRelu());
611 bool hasScale = static_cast<bool>(op.getScaleFactor());
612 static constexpr llvm::Intrinsic::ID e2m3x2IDs[] = {
613 llvm::Intrinsic::nvvm_bf16x2_to_e2m3x2_rn_satfinite,
614 llvm::Intrinsic::nvvm_bf16x2_to_e2m3x2_rn_relu_satfinite,
615 llvm::Intrinsic::nvvm_bf16x2_to_e2m3x2_rz_satfinite,
616 llvm::Intrinsic::nvvm_bf16x2_to_e2m3x2_rz_relu_satfinite,
617 };
618 static constexpr llvm::Intrinsic::ID e3m2x2IDs[] = {
619 llvm::Intrinsic::nvvm_bf16x2_to_e3m2x2_rn_satfinite,
620 llvm::Intrinsic::nvvm_bf16x2_to_e3m2x2_rn_relu_satfinite,
621 llvm::Intrinsic::nvvm_bf16x2_to_e3m2x2_rz_satfinite,
622 llvm::Intrinsic::nvvm_bf16x2_to_e3m2x2_rz_relu_satfinite,
623 };
624 static constexpr llvm::Intrinsic::ID e2m3x2ScaledIDs[] = {
625 llvm::Intrinsic::nvvm_bf16x2_to_e2m3x2_rn_satfinite_scale_n1_ue8m0,
626 llvm::Intrinsic::nvvm_bf16x2_to_e2m3x2_rn_relu_satfinite_scale_n1_ue8m0,
627 llvm::Intrinsic::nvvm_bf16x2_to_e2m3x2_rz_satfinite_scale_n1_ue8m0,
628 llvm::Intrinsic::nvvm_bf16x2_to_e2m3x2_rz_relu_satfinite_scale_n1_ue8m0,
629 };
630 static constexpr llvm::Intrinsic::ID e3m2x2ScaledIDs[] = {
631 llvm::Intrinsic::nvvm_bf16x2_to_e3m2x2_rn_satfinite_scale_n1_ue8m0,
632 llvm::Intrinsic::nvvm_bf16x2_to_e3m2x2_rn_relu_satfinite_scale_n1_ue8m0,
633 llvm::Intrinsic::nvvm_bf16x2_to_e3m2x2_rz_satfinite_scale_n1_ue8m0,
634 llvm::Intrinsic::nvvm_bf16x2_to_e3m2x2_rz_relu_satfinite_scale_n1_ue8m0,
635 };
636
637 llvm::Intrinsic::ID intId =
639 .Case<mlir::Float6E2M3FNType>([&](mlir::Float6E2M3FNType) {
640 return (hasScale ? e2m3x2ScaledIDs : e2m3x2IDs)[idx];
641 })
642 .Case<mlir::Float6E3M2FNType>([&](mlir::Float6E3M2FNType) {
643 return (hasScale ? e3m2x2ScaledIDs : e3m2x2IDs)[idx];
644 })
645 .Default([](mlir::Type) {
646 llvm_unreachable("Invalid conversion in ConvertBF16x2ToF6x2Op");
647 return llvm::Intrinsic::not_intrinsic;
648 });
649 llvm::SmallVector<llvm::Value *> args = {mt.lookupValue(op.getSrc())};
650 appendN1ScaleArg(op.getScaleFactor(), mt, builder, args);
651 args.push_back(builder.getInt1(op.getPzo()));
652 return {intId, std::move(args)};
653}
654
655static llvm::Intrinsic::ID
656getUE5M3IntrinsicID(NVVM::FPRoundingMode rnd, bool hasSatFinite, bool hasScale,
659 unsigned rndIdx;
660 switch (rnd) {
661 case NVVM::FPRoundingMode::RN:
662 rndIdx = 0;
663 break;
664 case NVVM::FPRoundingMode::RZ:
665 rndIdx = 1;
666 break;
667 case NVVM::FPRoundingMode::RP:
668 rndIdx = 2;
669 break;
670 default:
671 llvm_unreachable("Invalid rounding mode for UE5M3 conversion");
672 }
673 // satfinite selects the next rounding-mode group: stride 3 unscaled
674 // (rn/rz/rp), stride 2 scaled (rn/rz; there is no scaled rp).
675 if (hasScale)
676 return scaledIds[hasSatFinite * 2 + rndIdx];
677 return ids[hasSatFinite * 3 + rndIdx];
678}
679
681ConvertF32x2ToF8x2Op::getIntrinsicIDAndArgs(NVVM::ConvertF32x2ToF8x2Op &op,
683 llvm::IRBuilderBase &builder) {
684 bool hasSatFinite = op.getSat() == NVVM::SaturationMode::SATFINITE;
685 bool hasScale = static_cast<bool>(op.getScaleFactor());
686 unsigned idx =
687 (static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RZ) << 1) |
688 static_cast<unsigned>(op.getRelu());
689 unsigned ue8m0Idx =
690 (static_cast<unsigned>(hasSatFinite) << 1) |
691 static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RP);
692
693 static constexpr llvm::Intrinsic::ID e4m3x2IDs[] = {
694 llvm::Intrinsic::nvvm_ff_to_e4m3x2_rn,
695 llvm::Intrinsic::nvvm_ff_to_e4m3x2_rn_relu,
696 llvm::Intrinsic::nvvm_ff_to_e4m3x2_rz,
697 llvm::Intrinsic::nvvm_ff_to_e4m3x2_rz_relu,
698 };
699 static constexpr llvm::Intrinsic::ID e5m2x2IDs[] = {
700 llvm::Intrinsic::nvvm_ff_to_e5m2x2_rn,
701 llvm::Intrinsic::nvvm_ff_to_e5m2x2_rn_relu,
702 llvm::Intrinsic::nvvm_ff_to_e5m2x2_rz,
703 llvm::Intrinsic::nvvm_ff_to_e5m2x2_rz_relu,
704 };
705 static constexpr llvm::Intrinsic::ID ue8m0x2IDs[] = {
706 llvm::Intrinsic::nvvm_ff_to_ue8m0x2_rz,
707 llvm::Intrinsic::nvvm_ff_to_ue8m0x2_rp,
708 llvm::Intrinsic::nvvm_ff_to_ue8m0x2_rz_satfinite,
709 llvm::Intrinsic::nvvm_ff_to_ue8m0x2_rp_satfinite,
710 };
711 static constexpr llvm::Intrinsic::ID e4m3x2ScaledIDs[] = {
712 llvm::Intrinsic::nvvm_ff_to_e4m3x2_rn_scale_n1_ue8m0,
713 llvm::Intrinsic::nvvm_ff_to_e4m3x2_rn_relu_scale_n1_ue8m0,
714 llvm::Intrinsic::nvvm_ff_to_e4m3x2_rz_scale_n1_ue8m0,
715 llvm::Intrinsic::nvvm_ff_to_e4m3x2_rz_relu_scale_n1_ue8m0,
716 };
717 static constexpr llvm::Intrinsic::ID e5m2x2ScaledIDs[] = {
718 llvm::Intrinsic::nvvm_ff_to_e5m2x2_rn_scale_n1_ue8m0,
719 llvm::Intrinsic::nvvm_ff_to_e5m2x2_rn_relu_scale_n1_ue8m0,
720 llvm::Intrinsic::nvvm_ff_to_e5m2x2_rz_scale_n1_ue8m0,
721 llvm::Intrinsic::nvvm_ff_to_e5m2x2_rz_relu_scale_n1_ue8m0,
722 };
723 static constexpr llvm::Intrinsic::ID ue5m3x2IDs[] = {
724 llvm::Intrinsic::nvvm_ff_to_ue5m3x2_rn,
725 llvm::Intrinsic::nvvm_ff_to_ue5m3x2_rz,
726 llvm::Intrinsic::nvvm_ff_to_ue5m3x2_rp,
727 llvm::Intrinsic::nvvm_ff_to_ue5m3x2_rn_satfinite,
728 llvm::Intrinsic::nvvm_ff_to_ue5m3x2_rz_satfinite,
729 llvm::Intrinsic::nvvm_ff_to_ue5m3x2_rp_satfinite,
730 };
731 static constexpr llvm::Intrinsic::ID ue5m3x2ScaledIDs[] = {
732 llvm::Intrinsic::nvvm_ff_to_ue5m3x2_rn_scale_n1_ue8m0,
733 llvm::Intrinsic::nvvm_ff_to_ue5m3x2_rz_scale_n1_ue8m0,
734 llvm::Intrinsic::nvvm_ff_to_ue5m3x2_rn_satfinite_scale_n1_ue8m0,
735 llvm::Intrinsic::nvvm_ff_to_ue5m3x2_rz_satfinite_scale_n1_ue8m0,
736 };
737
738 llvm::Intrinsic::ID intId =
740 .Case([&](mlir::Float8E4M3FNType) {
741 return hasScale ? e4m3x2ScaledIDs[idx] : e4m3x2IDs[idx];
742 })
743 .Case([&](mlir::Float8E5M2Type) {
744 return hasScale ? e5m2x2ScaledIDs[idx] : e5m2x2IDs[idx];
745 })
746 .Case([&](mlir::Float8E8M0FNUType) {
747 if (op.getRnd() != NVVM::FPRoundingMode::RZ &&
748 op.getRnd() != NVVM::FPRoundingMode::RP)
749 llvm_unreachable("Invalid conversion in ConvertF32x2ToF8x2Op");
750 return ue8m0x2IDs[ue8m0Idx];
751 })
752 .Case([&](mlir::Float8E5M3FNUType) {
753 return getUE5M3IntrinsicID(op.getRnd(), hasSatFinite, hasScale,
754 ue5m3x2IDs, ue5m3x2ScaledIDs);
755 })
756 .Default([](mlir::Type) {
757 llvm_unreachable("Invalid conversion in ConvertF32x2ToF8x2Op");
758 return llvm::Intrinsic::not_intrinsic;
759 });
760
761 llvm::SmallVector<llvm::Value *> args = {mt.lookupValue(op.getA()),
762 mt.lookupValue(op.getB())};
763 appendN1ScaleArg(op.getScaleFactor(), mt, builder, args);
764 if (!isa<Float8E8M0FNUType, Float8E5M3FNUType>(op.getDstTy()))
765 args.push_back(builder.getInt1(op.getPzo()));
766 return {intId, std::move(args)};
767}
768
770ConvertF16x2ToF8x2Op::getIntrinsicIDAndArgs(NVVM::ConvertF16x2ToF8x2Op &op,
772 llvm::IRBuilderBase &builder) {
773 bool hasScale = static_cast<bool>(op.getScaleFactor());
774 unsigned idx =
775 (static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RZ) << 1) |
776 static_cast<unsigned>(op.getRelu());
777 static constexpr llvm::Intrinsic::ID e4m3x2IDs[] = {
778 llvm::Intrinsic::nvvm_f16x2_to_e4m3x2_rn,
779 llvm::Intrinsic::nvvm_f16x2_to_e4m3x2_rn_relu,
780 llvm::Intrinsic::nvvm_f16x2_to_e4m3x2_rz,
781 llvm::Intrinsic::nvvm_f16x2_to_e4m3x2_rz_relu,
782 };
783 static constexpr llvm::Intrinsic::ID e5m2x2IDs[] = {
784 llvm::Intrinsic::nvvm_f16x2_to_e5m2x2_rn,
785 llvm::Intrinsic::nvvm_f16x2_to_e5m2x2_rn_relu,
786 llvm::Intrinsic::nvvm_f16x2_to_e5m2x2_rz,
787 llvm::Intrinsic::nvvm_f16x2_to_e5m2x2_rz_relu,
788 };
789 static constexpr llvm::Intrinsic::ID e4m3x2ScaledIDs[] = {
790 llvm::Intrinsic::nvvm_f16x2_to_e4m3x2_rn_scale_n1_ue8m0,
791 llvm::Intrinsic::nvvm_f16x2_to_e4m3x2_rn_relu_scale_n1_ue8m0,
792 llvm::Intrinsic::nvvm_f16x2_to_e4m3x2_rz_scale_n1_ue8m0,
793 llvm::Intrinsic::nvvm_f16x2_to_e4m3x2_rz_relu_scale_n1_ue8m0,
794 };
795 static constexpr llvm::Intrinsic::ID e5m2x2ScaledIDs[] = {
796 llvm::Intrinsic::nvvm_f16x2_to_e5m2x2_rn_scale_n1_ue8m0,
797 llvm::Intrinsic::nvvm_f16x2_to_e5m2x2_rn_relu_scale_n1_ue8m0,
798 llvm::Intrinsic::nvvm_f16x2_to_e5m2x2_rz_scale_n1_ue8m0,
799 llvm::Intrinsic::nvvm_f16x2_to_e5m2x2_rz_relu_scale_n1_ue8m0,
800 };
801 static constexpr llvm::Intrinsic::ID ue5m3x2IDs[] = {
802 llvm::Intrinsic::nvvm_f16x2_to_ue5m3x2_rn,
803 llvm::Intrinsic::nvvm_f16x2_to_ue5m3x2_rz,
804 llvm::Intrinsic::nvvm_f16x2_to_ue5m3x2_rp,
805 llvm::Intrinsic::nvvm_f16x2_to_ue5m3x2_rn_satfinite,
806 llvm::Intrinsic::nvvm_f16x2_to_ue5m3x2_rz_satfinite,
807 llvm::Intrinsic::nvvm_f16x2_to_ue5m3x2_rp_satfinite,
808 };
809 static constexpr llvm::Intrinsic::ID ue5m3x2ScaledIDs[] = {
810 llvm::Intrinsic::nvvm_f16x2_to_ue5m3x2_rn_scale_n1_ue8m0,
811 llvm::Intrinsic::nvvm_f16x2_to_ue5m3x2_rz_scale_n1_ue8m0,
812 llvm::Intrinsic::nvvm_f16x2_to_ue5m3x2_rn_satfinite_scale_n1_ue8m0,
813 llvm::Intrinsic::nvvm_f16x2_to_ue5m3x2_rz_satfinite_scale_n1_ue8m0,
814 };
815
816 llvm::Intrinsic::ID intId =
818 .Case([&](mlir::Float8E4M3FNType) {
819 return hasScale ? e4m3x2ScaledIDs[idx] : e4m3x2IDs[idx];
820 })
821 .Case([&](mlir::Float8E5M2Type) {
822 return hasScale ? e5m2x2ScaledIDs[idx] : e5m2x2IDs[idx];
823 })
824 .Case([&](mlir::Float8E5M3FNUType) {
825 return getUE5M3IntrinsicID(
826 op.getRnd(), op.getSat() == NVVM::SaturationMode::SATFINITE,
827 hasScale, ue5m3x2IDs, ue5m3x2ScaledIDs);
828 })
829 .Default([](mlir::Type) {
830 llvm_unreachable("Invalid conversion in ConvertF16x2ToF8x2Op");
831 return llvm::Intrinsic::not_intrinsic;
832 });
833
834 llvm::SmallVector<llvm::Value *> args = {mt.lookupValue(op.getA())};
835 appendN1ScaleArg(op.getScaleFactor(), mt, builder, args);
836 if (!isa<Float8E5M3FNUType>(op.getDstTy()))
837 args.push_back(builder.getInt1(op.getPzo()));
838 return {intId, std::move(args)};
839}
840
842ConvertBF16x2ToF8x2Op::getIntrinsicIDAndArgs(NVVM::ConvertBF16x2ToF8x2Op &op,
844 llvm::IRBuilderBase &builder) {
845 bool hasSatFinite = op.getSat() == NVVM::SaturationMode::SATFINITE;
846 bool hasScale = static_cast<bool>(op.getScaleFactor());
847 unsigned idx =
848 (static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RZ) << 1) |
849 static_cast<unsigned>(op.getRelu());
850 unsigned ue8m0Idx =
851 (static_cast<unsigned>(hasSatFinite) << 1) |
852 static_cast<unsigned>(op.getRnd() == NVVM::FPRoundingMode::RP);
853
854 static constexpr llvm::Intrinsic::ID e4m3x2IDs[] = {
855 llvm::Intrinsic::nvvm_bf16x2_to_e4m3x2_rn_satfinite,
856 llvm::Intrinsic::nvvm_bf16x2_to_e4m3x2_rn_relu_satfinite,
857 llvm::Intrinsic::nvvm_bf16x2_to_e4m3x2_rz_satfinite,
858 llvm::Intrinsic::nvvm_bf16x2_to_e4m3x2_rz_relu_satfinite,
859 };
860 static constexpr llvm::Intrinsic::ID e5m2x2IDs[] = {
861 llvm::Intrinsic::nvvm_bf16x2_to_e5m2x2_rn_satfinite,
862 llvm::Intrinsic::nvvm_bf16x2_to_e5m2x2_rn_relu_satfinite,
863 llvm::Intrinsic::nvvm_bf16x2_to_e5m2x2_rz_satfinite,
864 llvm::Intrinsic::nvvm_bf16x2_to_e5m2x2_rz_relu_satfinite,
865 };
866 static constexpr llvm::Intrinsic::ID ue8m0x2IDs[] = {
867 llvm::Intrinsic::nvvm_bf16x2_to_ue8m0x2_rz,
868 llvm::Intrinsic::nvvm_bf16x2_to_ue8m0x2_rp,
869 llvm::Intrinsic::nvvm_bf16x2_to_ue8m0x2_rz_satfinite,
870 llvm::Intrinsic::nvvm_bf16x2_to_ue8m0x2_rp_satfinite,
871 };
872 static constexpr llvm::Intrinsic::ID e4m3x2ScaledIDs[] = {
873 llvm::Intrinsic::nvvm_bf16x2_to_e4m3x2_rn_satfinite_scale_n1_ue8m0,
874 llvm::Intrinsic::nvvm_bf16x2_to_e4m3x2_rn_relu_satfinite_scale_n1_ue8m0,
875 llvm::Intrinsic::nvvm_bf16x2_to_e4m3x2_rz_satfinite_scale_n1_ue8m0,
876 llvm::Intrinsic::nvvm_bf16x2_to_e4m3x2_rz_relu_satfinite_scale_n1_ue8m0,
877 };
878 static constexpr llvm::Intrinsic::ID e5m2x2ScaledIDs[] = {
879 llvm::Intrinsic::nvvm_bf16x2_to_e5m2x2_rn_satfinite_scale_n1_ue8m0,
880 llvm::Intrinsic::nvvm_bf16x2_to_e5m2x2_rn_relu_satfinite_scale_n1_ue8m0,
881 llvm::Intrinsic::nvvm_bf16x2_to_e5m2x2_rz_satfinite_scale_n1_ue8m0,
882 llvm::Intrinsic::nvvm_bf16x2_to_e5m2x2_rz_relu_satfinite_scale_n1_ue8m0,
883 };
884 static constexpr llvm::Intrinsic::ID ue5m3x2IDs[] = {
885 llvm::Intrinsic::nvvm_bf16x2_to_ue5m3x2_rn,
886 llvm::Intrinsic::nvvm_bf16x2_to_ue5m3x2_rz,
887 llvm::Intrinsic::nvvm_bf16x2_to_ue5m3x2_rp,
888 llvm::Intrinsic::nvvm_bf16x2_to_ue5m3x2_rn_satfinite,
889 llvm::Intrinsic::nvvm_bf16x2_to_ue5m3x2_rz_satfinite,
890 llvm::Intrinsic::nvvm_bf16x2_to_ue5m3x2_rp_satfinite,
891 };
892 static constexpr llvm::Intrinsic::ID ue5m3x2ScaledIDs[] = {
893 llvm::Intrinsic::nvvm_bf16x2_to_ue5m3x2_rn_scale_n1_ue8m0,
894 llvm::Intrinsic::nvvm_bf16x2_to_ue5m3x2_rz_scale_n1_ue8m0,
895 llvm::Intrinsic::nvvm_bf16x2_to_ue5m3x2_rn_satfinite_scale_n1_ue8m0,
896 llvm::Intrinsic::nvvm_bf16x2_to_ue5m3x2_rz_satfinite_scale_n1_ue8m0,
897 };
898
899 llvm::Intrinsic::ID intId =
901 .Case<mlir::Float8E4M3FNType>([&](mlir::Float8E4M3FNType) {
902 return hasScale ? e4m3x2ScaledIDs[idx] : e4m3x2IDs[idx];
903 })
904 .Case<mlir::Float8E5M2Type>([&](mlir::Float8E5M2Type) {
905 return hasScale ? e5m2x2ScaledIDs[idx] : e5m2x2IDs[idx];
906 })
907 .Case<mlir::Float8E8M0FNUType>(
908 [&](mlir::Float8E8M0FNUType) { return ue8m0x2IDs[ue8m0Idx]; })
909 .Case<mlir::Float8E5M3FNUType>([&](mlir::Float8E5M3FNUType) {
910 return getUE5M3IntrinsicID(op.getRnd(), hasSatFinite, hasScale,
911 ue5m3x2IDs, ue5m3x2ScaledIDs);
912 })
913 .Default([](mlir::Type) {
914 llvm_unreachable("Invalid conversion in ConvertBF16x2ToF8x2Op");
915 return llvm::Intrinsic::not_intrinsic;
916 });
917
918 llvm::SmallVector<llvm::Value *> args = {mt.lookupValue(op.getSrc())};
919 appendN1ScaleArg(op.getScaleFactor(), mt, builder, args);
920 if (!isa<Float8E8M0FNUType, Float8E5M3FNUType>(op.getDstTy()))
921 args.push_back(builder.getInt1(op.getPzo()));
922 return {intId, std::move(args)};
923}
924
925NVVM::IDArgPair ConvertF8x2ToF16x2Op::getIntrinsicIDAndArgs(
926 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
927 auto curOp = cast<NVVM::ConvertF8x2ToF16x2Op>(op);
928
929 bool hasRelu = curOp.getRelu();
930
931 llvm::Intrinsic::ID intId =
933 .Case([&](Float8E4M3FNType type) {
934 return hasRelu ? llvm::Intrinsic::nvvm_e4m3x2_to_f16x2_rn_relu
935 : llvm::Intrinsic::nvvm_e4m3x2_to_f16x2_rn;
936 })
937 .Case([&](Float8E5M2Type type) {
938 return hasRelu ? llvm::Intrinsic::nvvm_e5m2x2_to_f16x2_rn_relu
939 : llvm::Intrinsic::nvvm_e5m2x2_to_f16x2_rn;
940 })
941 .Case([&](Float8E5M3FNUType type) {
942 return llvm::Intrinsic::nvvm_ue5m3x2_to_f16x2_rn;
943 })
944 .Default([](mlir::Type type) {
945 llvm_unreachable("Invalid type for ConvertF8x2ToF16x2Op");
946 return llvm::Intrinsic::not_intrinsic;
947 });
948
949 llvm::Value *packedI16 =
950 builder.CreateBitCast(mt.lookupValue(curOp.getSrc()),
951 llvm::Type::getInt16Ty(builder.getContext()));
952
953 return {intId, {packedI16}};
954}
955
956NVVM::IDArgPair ConvertF8x2ToBF16x2Op::getIntrinsicIDAndArgs(
957 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
958 auto curOp = cast<NVVM::ConvertF8x2ToBF16x2Op>(op);
959 bool hasScale = static_cast<bool>(curOp.getScaleFactor());
960 bool hasSatfinite = curOp.getSat() == NVVM::SaturationMode::SATFINITE;
961 bool hasRelu = curOp.getRelu();
962
963 static constexpr llvm::Intrinsic::ID E4M3Ids[] = {
964 llvm::Intrinsic::nvvm_e4m3x2_to_bf16x2_rn_scale_n2_ue8m0,
965 llvm::Intrinsic::nvvm_e4m3x2_to_bf16x2_rn_relu_scale_n2_ue8m0,
966 llvm::Intrinsic::nvvm_e4m3x2_to_bf16x2_rn_satfinite_scale_n2_ue8m0,
967 llvm::Intrinsic::nvvm_e4m3x2_to_bf16x2_rn_relu_satfinite_scale_n2_ue8m0,
968 };
969
970 static constexpr llvm::Intrinsic::ID E5M2Ids[] = {
971 llvm::Intrinsic::nvvm_e5m2x2_to_bf16x2_rn_scale_n2_ue8m0,
972 llvm::Intrinsic::nvvm_e5m2x2_to_bf16x2_rn_relu_scale_n2_ue8m0,
973 llvm::Intrinsic::nvvm_e5m2x2_to_bf16x2_rn_satfinite_scale_n2_ue8m0,
974 llvm::Intrinsic::nvvm_e5m2x2_to_bf16x2_rn_relu_satfinite_scale_n2_ue8m0,
975 };
976 static constexpr llvm::Intrinsic::ID UE5M3Ids[] = {
977 llvm::Intrinsic::nvvm_ue5m3x2_to_bf16x2_rn,
978 llvm::Intrinsic::nvvm_ue5m3x2_to_bf16x2_rn,
979 llvm::Intrinsic::nvvm_ue5m3x2_to_bf16x2_rn_satfinite,
980 llvm::Intrinsic::nvvm_ue5m3x2_to_bf16x2_rn_satfinite,
981 };
982 static constexpr llvm::Intrinsic::ID UE5M3ScaledIds[] = {
983 llvm::Intrinsic::nvvm_ue5m3x2_to_bf16x2_rn_scale_n2_ue8m0,
984 llvm::Intrinsic::nvvm_ue5m3x2_to_bf16x2_rn_scale_n2_ue8m0,
985 llvm::Intrinsic::nvvm_ue5m3x2_to_bf16x2_rn_satfinite_scale_n2_ue8m0,
986 llvm::Intrinsic::nvvm_ue5m3x2_to_bf16x2_rn_satfinite_scale_n2_ue8m0,
987 };
988
989 llvm::Intrinsic::ID intId =
991 .Case([&](Float8E8M0FNUType type) {
992 return llvm::Intrinsic::nvvm_ue8m0x2_to_bf16x2;
993 })
994 .Case([&](Float8E4M3FNType type) {
995 return E4M3Ids[hasSatfinite << 1 | hasRelu];
996 })
997 .Case([&](Float8E5M2Type type) {
998 return E5M2Ids[hasSatfinite << 1 | hasRelu];
999 })
1000 .Case([&](Float8E5M3FNUType type) {
1001 return (hasScale ? UE5M3ScaledIds : UE5M3Ids)[hasSatfinite << 1];
1002 })
1003 .Default([](mlir::Type type) {
1004 llvm_unreachable("Invalid type for ConvertF8x2ToBF16x2Op");
1005 return llvm::Intrinsic::not_intrinsic;
1006 });
1007 llvm::Value *packedI16 =
1008 builder.CreateBitCast(mt.lookupValue(curOp.getSrc()),
1009 llvm::Type::getInt16Ty(builder.getContext()));
1010
1012 args.push_back(packedI16);
1013 if (isa<Float8E5M3FNUType>(curOp.getSrcType())) {
1014 if (hasScale)
1015 args.push_back(mt.lookupValue(curOp.getScaleFactor()));
1016 } else if (!isa<Float8E8M0FNUType>(curOp.getSrcType())) {
1017 args.push_back(
1018 hasScale ? mt.lookupValue(curOp.getScaleFactor())
1019 : builder.getInt16(0x7f7f)); // default scale factor (value of
1020 // 1 for both elements).
1021 }
1022
1023 return {intId, std::move(args)};
1024}
1025
1026NVVM::IDArgPair ConvertF6x2ToF16x2Op::getIntrinsicIDAndArgs(
1027 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1028 auto curOp = cast<NVVM::ConvertF6x2ToF16x2Op>(op);
1029
1030 bool hasRelu = curOp.getRelu();
1031
1032 llvm::Intrinsic::ID intId =
1034 .Case([&](Float6E2M3FNType type) {
1035 return hasRelu ? llvm::Intrinsic::nvvm_e2m3x2_to_f16x2_rn_relu
1036 : llvm::Intrinsic::nvvm_e2m3x2_to_f16x2_rn;
1037 })
1038 .Case([&](Float6E3M2FNType type) {
1039 return hasRelu ? llvm::Intrinsic::nvvm_e3m2x2_to_f16x2_rn_relu
1040 : llvm::Intrinsic::nvvm_e3m2x2_to_f16x2_rn;
1041 })
1042 .Default([](mlir::Type type) {
1043 llvm_unreachable("Invalid type for ConvertF6x2ToF16x2Op");
1044 return llvm::Intrinsic::not_intrinsic;
1045 });
1046
1047 llvm::Value *packedI16 =
1048 builder.CreateBitCast(mt.lookupValue(curOp.getSrc()),
1049 llvm::Type::getInt16Ty(builder.getContext()));
1050
1051 return {intId, {packedI16}};
1052}
1053
1054NVVM::IDArgPair ConvertF6x2ToBF16x2Op::getIntrinsicIDAndArgs(
1055 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1056 auto curOp = cast<NVVM::ConvertF6x2ToBF16x2Op>(op);
1057 bool hasScale = static_cast<bool>(curOp.getScaleFactor());
1058 bool hasSatfinite = curOp.getSat() == NVVM::SaturationMode::SATFINITE;
1059 bool hasRelu = curOp.getRelu();
1060
1061 static constexpr llvm::Intrinsic::ID E2M3Ids[] = {
1062 llvm::Intrinsic::nvvm_e2m3x2_to_bf16x2_rn_scale_n2_ue8m0,
1063 llvm::Intrinsic::nvvm_e2m3x2_to_bf16x2_rn_relu_scale_n2_ue8m0,
1064 llvm::Intrinsic::nvvm_e2m3x2_to_bf16x2_rn_satfinite_scale_n2_ue8m0,
1065 llvm::Intrinsic::nvvm_e2m3x2_to_bf16x2_rn_relu_satfinite_scale_n2_ue8m0,
1066 };
1067
1068 static constexpr llvm::Intrinsic::ID E3M2Ids[] = {
1069 llvm::Intrinsic::nvvm_e3m2x2_to_bf16x2_rn_scale_n2_ue8m0,
1070 llvm::Intrinsic::nvvm_e3m2x2_to_bf16x2_rn_relu_scale_n2_ue8m0,
1071 llvm::Intrinsic::nvvm_e3m2x2_to_bf16x2_rn_satfinite_scale_n2_ue8m0,
1072 llvm::Intrinsic::nvvm_e3m2x2_to_bf16x2_rn_relu_satfinite_scale_n2_ue8m0,
1073 };
1074
1075 unsigned idx = (hasSatfinite << 1) | hasRelu;
1076 llvm::Intrinsic::ID intId =
1078 .Case([&](Float6E2M3FNType type) { return E2M3Ids[idx]; })
1079 .Case([&](Float6E3M2FNType type) { return E3M2Ids[idx]; })
1080 .Default([](mlir::Type type) {
1081 llvm_unreachable("Invalid type for ConvertF6x2ToBF16x2Op");
1082 return llvm::Intrinsic::not_intrinsic;
1083 });
1084
1085 llvm::Value *packedI16 =
1086 builder.CreateBitCast(mt.lookupValue(curOp.getSrc()),
1087 llvm::Type::getInt16Ty(builder.getContext()));
1088
1090 args.push_back(packedI16);
1091 args.push_back(
1092 hasScale
1093 ? mt.lookupValue(curOp.getScaleFactor())
1094 : builder.getInt16(
1095 0x7f7f)); // default scale factor (value of 1 for both elements)
1096
1097 return {intId, std::move(args)};
1098}
1099
1100NVVM::IDArgPair ConvertF4x2ToF16x2Op::getIntrinsicIDAndArgs(
1101 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1102 auto curOp = cast<NVVM::ConvertF4x2ToF16x2Op>(op);
1103
1104 bool hasRelu = curOp.getRelu();
1105
1106 llvm::Intrinsic::ID intId =
1108 .Case([&](Float4E2M1FNType type) {
1109 return hasRelu ? llvm::Intrinsic::nvvm_e2m1x2_to_f16x2_rn_relu
1110 : llvm::Intrinsic::nvvm_e2m1x2_to_f16x2_rn;
1111 })
1112 .Default([](mlir::Type type) {
1113 llvm_unreachable("Invalid type for ConvertF4x2ToF16x2Op");
1114 return llvm::Intrinsic::not_intrinsic;
1115 });
1116
1117 llvm::Value *extendedI16 =
1118 builder.CreateZExt(mt.lookupValue(curOp.getSrc()),
1119 llvm::Type::getInt16Ty(builder.getContext()));
1120
1121 return {intId, {extendedI16}};
1122}
1123
1124NVVM::IDArgPair ConvertF4x2ToBF16x2Op::getIntrinsicIDAndArgs(
1125 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1126 auto curOp = cast<NVVM::ConvertF4x2ToBF16x2Op>(op);
1127 bool hasScale = static_cast<bool>(curOp.getScaleFactor());
1128 bool hasSatfinite = curOp.getSat() == NVVM::SaturationMode::SATFINITE;
1129 bool hasRelu = curOp.getRelu();
1130
1131 static constexpr llvm::Intrinsic::ID E2M1Ids[] = {
1132 llvm::Intrinsic::nvvm_e2m1x2_to_bf16x2_rn_scale_n2_ue8m0,
1133 llvm::Intrinsic::nvvm_e2m1x2_to_bf16x2_rn_relu_scale_n2_ue8m0,
1134 llvm::Intrinsic::nvvm_e2m1x2_to_bf16x2_rn_satfinite_scale_n2_ue8m0,
1135 llvm::Intrinsic::nvvm_e2m1x2_to_bf16x2_rn_relu_satfinite_scale_n2_ue8m0,
1136 };
1137
1138 unsigned idx = (hasSatfinite << 1) | hasRelu;
1139 llvm::Intrinsic::ID intId =
1141 .Case([&](Float4E2M1FNType type) { return E2M1Ids[idx]; })
1142 .Default([](mlir::Type type) {
1143 llvm_unreachable("Invalid type for ConvertF4x2ToBF16x2Op");
1144 return llvm::Intrinsic::not_intrinsic;
1145 });
1146
1147 llvm::Value *extendedI16 =
1148 builder.CreateZExt(mt.lookupValue(curOp.getSrc()),
1149 llvm::Type::getInt16Ty(builder.getContext()));
1150
1152 args.push_back(extendedI16);
1153 args.push_back(
1154 hasScale
1155 ? mt.lookupValue(curOp.getScaleFactor())
1156 : builder.getInt16(
1157 0x7f7f)); // default scale factor (value of 1 for both elements)
1158
1159 return {intId, std::move(args)};
1160}
1161
1162NVVM::IDArgPair ConvertF32x2ToS2F6x2Op::getIntrinsicIDAndArgs(
1163 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1164 auto thisOp = cast<NVVM::ConvertF32x2ToS2F6x2Op>(op);
1165 bool hasRelu = thisOp.getRelu();
1166 bool hasScale = static_cast<bool>(thisOp.getScaleFactor());
1167
1168 llvm::Intrinsic::ID id =
1169 hasRelu
1170 ? llvm::Intrinsic::nvvm_ff_to_s2f6x2_rn_relu_satfinite_scale_n2_ue8m0
1171 : llvm::Intrinsic::nvvm_ff_to_s2f6x2_rn_satfinite_scale_n2_ue8m0;
1172
1173 // Fill the Intrinsic Args
1175 args.push_back(mt.lookupValue(thisOp.getA()));
1176 args.push_back(mt.lookupValue(thisOp.getB()));
1177 args.push_back(hasScale ? mt.lookupValue(thisOp.getScaleFactor())
1178 : builder.getInt16(0x7f7f));
1179 return {id, std::move(args)};
1180}
1181
1182NVVM::IDArgPair ConvertBF16x2ToS2F6x2Op::getIntrinsicIDAndArgs(
1183 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1184 auto thisOp = cast<NVVM::ConvertBF16x2ToS2F6x2Op>(op);
1185 bool hasRelu = thisOp.getRelu();
1186 bool hasScale = static_cast<bool>(thisOp.getScaleFactor());
1187
1188 llvm::Intrinsic::ID id =
1189 hasRelu
1190 ? llvm::Intrinsic::
1191 nvvm_bf16x2_to_s2f6x2_rn_relu_satfinite_scale_n2_ue8m0
1192 : llvm::Intrinsic::nvvm_bf16x2_to_s2f6x2_rn_satfinite_scale_n2_ue8m0;
1193
1194 // Fill the Intrinsic Args
1196 args.push_back(mt.lookupValue(thisOp.getSrc()));
1197 args.push_back(hasScale ? mt.lookupValue(thisOp.getScaleFactor())
1198 : builder.getInt16(0x7f7f));
1199 return {id, std::move(args)};
1200}
1201
1202NVVM::IDArgPair ConvertS2F6x2ToBF16x2Op::getIntrinsicIDAndArgs(
1203 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1204 auto thisOp = cast<NVVM::ConvertS2F6x2ToBF16x2Op>(op);
1205 bool hasRelu = thisOp.getRelu();
1206 bool hasScale = static_cast<bool>(thisOp.getScaleFactor());
1207 bool hasSat = thisOp.getSat() == NVVM::SaturationMode::SATFINITE;
1208
1209 static constexpr llvm::Intrinsic::ID ids[] = {
1210 llvm::Intrinsic::nvvm_s2f6x2_to_bf16x2_rn_scale_n2_ue8m0,
1211 llvm::Intrinsic::nvvm_s2f6x2_to_bf16x2_rn_relu_scale_n2_ue8m0,
1212 llvm::Intrinsic::nvvm_s2f6x2_to_bf16x2_rn_satfinite_scale_n2_ue8m0,
1213 llvm::Intrinsic::nvvm_s2f6x2_to_bf16x2_rn_relu_satfinite_scale_n2_ue8m0,
1214 };
1215
1216 unsigned idx = (hasSat << 1) | hasRelu;
1217
1218 // Fill the Intrinsic Args
1220 llvm::Value *packedI16 =
1221 builder.CreateBitCast(mt.lookupValue(thisOp.getSrc()),
1222 llvm::Type::getInt16Ty(builder.getContext()));
1223 args.push_back(packedI16);
1224 args.push_back(hasScale ? mt.lookupValue(thisOp.getScaleFactor())
1225 : builder.getInt16(0x7f7f));
1226
1227 return {ids[idx], std::move(args)};
1228}
1229
1230NVVM::IDArgPair ConvertF32x2ToF16x2Op::getIntrinsicIDAndArgs(
1231 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1232 auto thisOp = cast<NVVM::ConvertF32x2ToF16x2Op>(op);
1233 static constexpr llvm::Intrinsic::ID rndRNIds[] = {
1234 llvm::Intrinsic::nvvm_ff2f16x2_rn,
1235 llvm::Intrinsic::nvvm_ff2f16x2_rn_relu,
1236 llvm::Intrinsic::nvvm_ff2f16x2_rn_satfinite,
1237 llvm::Intrinsic::nvvm_ff2f16x2_rn_relu_satfinite,
1238 };
1239 static constexpr llvm::Intrinsic::ID rndRZIds[] = {
1240 llvm::Intrinsic::nvvm_ff2f16x2_rz,
1241 llvm::Intrinsic::nvvm_ff2f16x2_rz_relu,
1242 llvm::Intrinsic::nvvm_ff2f16x2_rz_satfinite,
1243 llvm::Intrinsic::nvvm_ff2f16x2_rz_relu_satfinite,
1244 };
1245 static constexpr llvm::Intrinsic::ID rndRSIds[] = {
1246 llvm::Intrinsic::nvvm_ff2f16x2_rs,
1247 llvm::Intrinsic::nvvm_ff2f16x2_rs_relu,
1248 llvm::Intrinsic::nvvm_ff2f16x2_rs_satfinite,
1249 llvm::Intrinsic::nvvm_ff2f16x2_rs_relu_satfinite,
1250 };
1251
1252 unsigned hasRelu = thisOp.getRelu() ? 1 : 0;
1253 unsigned hasSatFinite =
1254 (thisOp.getSat() == NVVM::SaturationMode::SATFINITE) ? 1 : 0;
1255 // idx: bit-0 - relu
1256 // bit-1 - satfinite
1257 unsigned idx = (hasSatFinite << 1) | hasRelu;
1258
1260 args.push_back(mt.lookupValue(thisOp.getSrcHi()));
1261 args.push_back(mt.lookupValue(thisOp.getSrcLo()));
1262 if (thisOp.getRandomBits())
1263 args.push_back(mt.lookupValue(thisOp.getRandomBits()));
1264
1265 if (thisOp.getRnd() != FPRoundingMode::RS)
1266 args.push_back(builder.getInt1(thisOp.getPzo()));
1267
1268 switch (thisOp.getRnd()) {
1269 case FPRoundingMode::RN:
1270 return {rndRNIds[idx], std::move(args)};
1271 case FPRoundingMode::RZ:
1272 return {rndRZIds[idx], std::move(args)};
1273 case FPRoundingMode::RS:
1274 return {rndRSIds[idx], std::move(args)};
1275 default:
1276 llvm_unreachable("Invalid rounding mode for ConvertF32x2ToF16x2Op");
1277 }
1278}
1279
1280NVVM::IDArgPair ConvertF32x2ToBF16x2Op::getIntrinsicIDAndArgs(
1281 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
1282 auto thisOp = cast<NVVM::ConvertF32x2ToBF16x2Op>(op);
1283 static constexpr llvm::Intrinsic::ID rndRNIds[] = {
1284 llvm::Intrinsic::nvvm_ff2bf16x2_rn,
1285 llvm::Intrinsic::nvvm_ff2bf16x2_rn_relu,
1286 llvm::Intrinsic::nvvm_ff2bf16x2_rn_satfinite,
1287 llvm::Intrinsic::nvvm_ff2bf16x2_rn_relu_satfinite,
1288 };
1289 static constexpr llvm::Intrinsic::ID rndRZIds[] = {
1290 llvm::Intrinsic::nvvm_ff2bf16x2_rz,
1291 llvm::Intrinsic::nvvm_ff2bf16x2_rz_relu,
1292 llvm::Intrinsic::nvvm_ff2bf16x2_rz_satfinite,
1293 llvm::Intrinsic::nvvm_ff2bf16x2_rz_relu_satfinite,
1294 };
1295 static constexpr llvm::Intrinsic::ID rndRSIds[] = {
1296 llvm::Intrinsic::nvvm_ff2bf16x2_rs,
1297 llvm::Intrinsic::nvvm_ff2bf16x2_rs_relu,
1298 llvm::Intrinsic::nvvm_ff2bf16x2_rs_satfinite,
1299 llvm::Intrinsic::nvvm_ff2bf16x2_rs_relu_satfinite,
1300 };
1301
1302 unsigned hasRelu = thisOp.getRelu() ? 1 : 0;
1303 unsigned hasSatFinite =
1304 (thisOp.getSat() == NVVM::SaturationMode::SATFINITE) ? 1 : 0;
1305 // idx: bit-0 - relu
1306 // bit-1 - satfinite
1307 unsigned idx = (hasSatFinite << 1) | hasRelu;
1308
1310 args.push_back(mt.lookupValue(thisOp.getSrcHi()));
1311 args.push_back(mt.lookupValue(thisOp.getSrcLo()));
1312 if (thisOp.getRandomBits())
1313 args.push_back(mt.lookupValue(thisOp.getRandomBits()));
1314
1315 if (thisOp.getRnd() != FPRoundingMode::RS)
1316 args.push_back(builder.getInt1(thisOp.getPzo()));
1317
1318 switch (thisOp.getRnd()) {
1319 case FPRoundingMode::RN:
1320 return {rndRNIds[idx], std::move(args)};
1321 case FPRoundingMode::RZ:
1322 return {rndRZIds[idx], std::move(args)};
1323 case FPRoundingMode::RS:
1324 return {rndRSIds[idx], std::move(args)};
1325 default:
1326 llvm_unreachable("Invalid rounding mode for ConvertF32x2ToBF16x2Op");
1327 }
1328}
1329
1330llvm::Intrinsic::ID ConvertF32x4ToF8x4Op::getIntrinsicID() {
1331 mlir::Type dstTy = getDstTy();
1332 bool hasRelu = getRelu();
1333
1335 .Case([&](mlir::Float8E4M3FNType) {
1336 return hasRelu ? llvm::Intrinsic::nvvm_f32x4_to_e4m3x4_rs_relu_satfinite
1337 : llvm::Intrinsic::nvvm_f32x4_to_e4m3x4_rs_satfinite;
1338 })
1339 .Case([&](mlir::Float8E5M2Type) {
1340 return hasRelu ? llvm::Intrinsic::nvvm_f32x4_to_e5m2x4_rs_relu_satfinite
1341 : llvm::Intrinsic::nvvm_f32x4_to_e5m2x4_rs_satfinite;
1342 })
1343 .Default([](mlir::Type) {
1344 llvm_unreachable("Invalid F8 type in ConvertF32x4ToF8x4Op");
1345 return llvm::Intrinsic::not_intrinsic;
1346 });
1347}
1348
1349llvm::Intrinsic::ID ConvertF32x4ToF6x4Op::getIntrinsicID() {
1350 mlir::Type dstTy = getDstTy();
1351 bool hasRelu = getRelu();
1352
1354 .Case([&](mlir::Float6E2M3FNType) {
1355 return hasRelu ? llvm::Intrinsic::nvvm_f32x4_to_e2m3x4_rs_relu_satfinite
1356 : llvm::Intrinsic::nvvm_f32x4_to_e2m3x4_rs_satfinite;
1357 })
1358 .Case([&](mlir::Float6E3M2FNType) {
1359 return hasRelu ? llvm::Intrinsic::nvvm_f32x4_to_e3m2x4_rs_relu_satfinite
1360 : llvm::Intrinsic::nvvm_f32x4_to_e3m2x4_rs_satfinite;
1361 })
1362 .Default([](mlir::Type) {
1363 llvm_unreachable("Invalid F6 type in ConvertF32x4ToF6x4Op");
1364 return llvm::Intrinsic::not_intrinsic;
1365 });
1366}
1367
1368llvm::Intrinsic::ID ConvertF32x4ToF4x4Op::getIntrinsicID() {
1369 mlir::Type dstTy = getDstTy();
1370 bool hasRelu = getRelu();
1371
1373 .Case([&](mlir::Float4E2M1FNType) {
1374 return hasRelu ? llvm::Intrinsic::nvvm_f32x4_to_e2m1x4_rs_relu_satfinite
1375 : llvm::Intrinsic::nvvm_f32x4_to_e2m1x4_rs_satfinite;
1376 })
1377 .Default([](mlir::Type) {
1378 llvm_unreachable("Invalid F4 type in ConvertF32x4ToF4x4Op");
1379 return llvm::Intrinsic::not_intrinsic;
1380 });
1381}
1382
1383#define GET_OP_CLASSES
1384#include "mlir/Dialect/LLVMIR/NVVMConvertOps.cpp.inc"
return success()
b getContext())
#define _none
static LogicalResult verifyConvertF32x2ToFP16x2Op(Twine dstType, FPRoundingMode rnd, bool hasRandomBits, bool hasPzo, Operation *op)
#define GET_CVT_F2TF32_ID(rnd, relu, sf)
static llvm::Intrinsic::ID getUE5M3IntrinsicID(NVVM::FPRoundingMode rnd, bool hasSatFinite, bool hasScale, ArrayRef< llvm::Intrinsic::ID > ids, ArrayRef< llvm::Intrinsic::ID > scaledIds)
static void appendN1ScaleArg(Value scaleFactor, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder, llvm::SmallVectorImpl< llvm::Value * > &args)
Implementation class for module translation.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
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
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.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.