LLVM 23.0.0git
NVPTXInstPrinter.cpp
Go to the documentation of this file.
1//===-- NVPTXInstPrinter.cpp - PTX assembly instruction printing ----------===//
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// Print MCInst instructions to .ptx format.
10//
11//===----------------------------------------------------------------------===//
12
14#include "NVPTX.h"
15#include "NVPTXUtilities.h"
16#include "llvm/ADT/StringRef.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstrInfo.h"
23#include "llvm/MC/MCSymbol.h"
26#include <cctype>
27using namespace llvm;
28
29#define DEBUG_TYPE "asm-printer"
30
31#define GET_SUBTARGETINFO_ENUM
32#include "NVPTXGenSubtargetInfo.inc"
33
34#include "NVPTXGenAsmWriter.inc"
35
36static bool hasParamSubqualifiers(const MCSubtargetInfo &STI) {
37 return STI.hasFeature(NVPTX::PTX83);
38}
39
43
45 // Decode the virtual register
46 // Must be kept in sync with NVPTXAsmPrinter::encodeVirtualRegister
47 unsigned RCId = (Reg.id() >> 28);
48 switch (RCId) {
49 default: report_fatal_error("Bad virtual register encoding");
50 case 0:
51 // This is actually a physical register, so defer to the autogenerated
52 // register printer
53 OS << getRegisterName(Reg);
54 return;
55 case 1:
56 OS << "%p";
57 break;
58 case 2:
59 OS << "%rs";
60 break;
61 case 3:
62 OS << "%r";
63 break;
64 case 4:
65 OS << "%rd";
66 break;
67 case 5:
68 OS << "%f";
69 break;
70 case 6:
71 OS << "%fd";
72 break;
73 case 7:
74 OS << "%rq";
75 break;
76 }
77
78 unsigned VReg = Reg.id() & 0x0FFFFFFF;
79 OS << VReg;
80}
81
83 StringRef Annot, const MCSubtargetInfo &STI,
84 raw_ostream &OS) {
85 printInstruction(MI, Address, STI, OS);
86
87 // Next always print the annotation.
88 printAnnotation(OS, Annot);
89}
90
91void NVPTXInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
92 const MCSubtargetInfo &, raw_ostream &O) {
93 const MCOperand &Op = MI->getOperand(OpNo);
94 if (Op.isReg()) {
95 MCRegister Reg = Op.getReg();
96 printRegName(O, Reg);
97 } else if (Op.isImm()) {
98 markup(O, Markup::Immediate) << formatImm(Op.getImm());
99 } else {
100 assert(Op.isExpr() && "Unknown operand kind in printOperand");
101 MAI.printExpr(O, *Op.getExpr());
102 }
103}
104
106 const MCSubtargetInfo &, raw_ostream &O,
107 StringRef Modifier) {
108 const MCOperand &MO = MI->getOperand(OpNum);
109 int64_t Imm = MO.getImm();
110
111 if (Modifier == "ftz") {
112 // FTZ flag
114 O << ".ftz";
115 return;
116 } else if (Modifier == "sat") {
117 // SAT flag
119 O << ".sat";
120 return;
121 } else if (Modifier == "satfinite") {
122 // SATFINITE flag
124 O << ".satfinite";
125 return;
126 } else if (Modifier == "relu") {
127 // RELU flag
129 O << ".relu";
130 return;
131 } else if (Modifier == "base") {
132 // Default operand
133 switch (Imm & NVPTX::PTXCvtMode::BASE_MASK) {
134 default:
135 return;
137 return;
139 O << ".rni";
140 return;
142 O << ".rzi";
143 return;
145 O << ".rmi";
146 return;
148 O << ".rpi";
149 return;
151 O << ".rn";
152 return;
154 O << ".rz";
155 return;
157 O << ".rm";
158 return;
160 O << ".rp";
161 return;
163 O << ".rna";
164 return;
166 O << ".rs";
167 return;
168 }
169 }
170 llvm_unreachable("Invalid conversion modifier");
171}
172
174 const MCSubtargetInfo &, raw_ostream &O) {
175 const MCOperand &MO = MI->getOperand(OpNum);
176 const int Imm = MO.getImm();
177 if (Imm)
178 O << ".ftz";
179}
180
182 const MCSubtargetInfo &,
183 raw_ostream &O) {
184 if (MI->getOperand(OpNum).getImm())
185 O << "!";
186}
187
189 const MCSubtargetInfo &, raw_ostream &O,
190 StringRef Modifier) {
191 const MCOperand &MO = MI->getOperand(OpNum);
192 int64_t Imm = MO.getImm();
193
194 if (Modifier == "FCmp") {
195 switch (Imm) {
196 default:
197 return;
199 O << "eq";
200 return;
202 O << "ne";
203 return;
205 O << "lt";
206 return;
208 O << "le";
209 return;
211 O << "gt";
212 return;
214 O << "ge";
215 return;
217 O << "equ";
218 return;
220 O << "neu";
221 return;
223 O << "ltu";
224 return;
226 O << "leu";
227 return;
229 O << "gtu";
230 return;
232 O << "geu";
233 return;
235 O << "num";
236 return;
238 O << "nan";
239 return;
240 }
241 }
242 if (Modifier == "ICmp") {
243 switch (Imm) {
244 default:
245 llvm_unreachable("Invalid ICmp mode");
247 O << "eq";
248 return;
250 O << "ne";
251 return;
254 O << "lt";
255 return;
258 O << "le";
259 return;
262 O << "gt";
263 return;
266 O << "ge";
267 return;
268 }
269 }
270 if (Modifier == "IType") {
271 switch (Imm) {
272 default:
273 llvm_unreachable("Invalid IType");
276 O << "b";
277 return;
282 O << "s";
283 return;
288 O << "u";
289 return;
290 }
291 }
292 llvm_unreachable("Empty Modifier");
293}
294
296 const MCSubtargetInfo &STI,
297 raw_ostream &O, StringRef Modifier) {
298 const MCOperand &MO = MI->getOperand(OpNum);
299 int Imm = (int)MO.getImm();
300 if (Modifier == "sem") {
301 auto Ordering = NVPTX::Ordering(Imm);
302 switch (Ordering) {
304 return;
306 O << ".relaxed";
307 return;
309 O << ".acquire";
310 return;
312 O << ".release";
313 return;
315 O << ".acq_rel";
316 return;
319 "NVPTX AtomicCode Printer does not support \"seq_cst\" ordering.");
320 return;
322 O << ".volatile";
323 return;
325 O << ".mmio.relaxed";
326 return;
327 }
328 } else if (Modifier == "scope") {
329 auto S = NVPTX::Scope(Imm);
330 switch (S) {
333 return;
335 O << ".sys";
336 return;
338 O << ".cta";
339 return;
341 O << ".cluster";
342 return;
344 O << ".gpu";
345 return;
346 }
348 "NVPTX AtomicCode Printer does not support \"{}\" scope modifier.",
349 ScopeToString(S)));
350 } else if (Modifier == "addsp") {
351 auto A = NVPTX::AddressSpace(Imm);
352 switch (A) {
354 return;
362 O << "." << addressSpaceToString(A, hasParamSubqualifiers(STI));
363 return;
364 }
366 "NVPTX AtomicCode Printer does not support \"{}\" addsp modifier.",
367 addressSpaceToString(A)));
368 } else if (Modifier == "sign") {
369 switch (Imm) {
371 O << "s";
372 return;
374 O << "u";
375 return;
377 O << "b";
378 return;
380 O << "f";
381 return;
382 default:
383 llvm_unreachable("Unknown register type");
384 }
385 }
386 llvm_unreachable(formatv("Unknown Modifier: {}", Modifier).str().c_str());
387}
388
390 const MCSubtargetInfo &, raw_ostream &O,
391 StringRef Modifier) {
392 const MCOperand &MO = MI->getOperand(OpNum);
393 int Imm = (int)MO.getImm();
394 if (Modifier.empty() || Modifier == "version") {
395 O << Imm; // Just print out PTX version
396 return;
397 } else if (Modifier == "aligned") {
398 // PTX63 requires '.aligned' in the name of the instruction.
399 if (Imm >= 63)
400 O << ".aligned";
401 return;
402 }
403 llvm_unreachable("Unknown Modifier");
404}
405
407 const MCSubtargetInfo &STI,
408 raw_ostream &O, StringRef Modifier) {
409 printOperand(MI, OpNum, STI, O);
410
411 if (Modifier == "add") {
412 O << ", ";
413 printOperand(MI, OpNum + 1, STI, O);
414 } else {
415 if (MI->getOperand(OpNum + 1).isImm() &&
416 MI->getOperand(OpNum + 1).getImm() == 0)
417 return; // don't print ',0' or '+0'
418 O << "+";
419 printOperand(MI, OpNum + 1, STI, O);
420 }
421}
422
424 const MCSubtargetInfo &,
425 raw_ostream &O) {
426 auto &Op = MI->getOperand(OpNum);
427 assert(Op.isImm() && "Invalid operand");
428 uint32_t Imm = (uint32_t)Op.getImm();
429 if (Imm != UINT32_MAX) {
430 O << ".pragma \"used_bytes_mask " << format_hex(Imm, 1) << "\";\n\t";
431 }
432}
433
435 const MCSubtargetInfo &STI,
436 raw_ostream &O) {
437 const MCOperand &Op = MI->getOperand(OpNum);
438 if (Op.isReg() && Op.getReg() == MCRegister::NoRegister)
439 O << "_";
440 else
441 printOperand(MI, OpNum, STI, O);
442}
443
445 const MCSubtargetInfo &, raw_ostream &O) {
446 int64_t Imm = MI->getOperand(OpNum).getImm();
447 O << formatHex(Imm) << "U";
448}
449
451 const MCSubtargetInfo &,
452 raw_ostream &O) {
453 const MCOperand &Op = MI->getOperand(OpNum);
454 assert(Op.isExpr() && "Call prototype is not an MCExpr?");
455 const MCExpr *Expr = Op.getExpr();
456 const MCSymbol &Sym = cast<MCSymbolRefExpr>(Expr)->getSymbol();
457 O << Sym.getName();
458}
459
461 const MCSubtargetInfo &, raw_ostream &O) {
462 const MCOperand &MO = MI->getOperand(OpNum);
463 int64_t Imm = MO.getImm();
464
465 switch (Imm) {
466 default:
467 return;
469 return;
471 O << ".f4e";
472 return;
474 O << ".b4e";
475 return;
477 O << ".rc8";
478 return;
480 O << ".ecl";
481 return;
483 O << ".ecr";
484 return;
486 O << ".rc16";
487 return;
488 }
489}
490
492 const MCSubtargetInfo &,
493 raw_ostream &O) {
494 const MCOperand &MO = MI->getOperand(OpNum);
495 using RedTy = nvvm::TMAReductionOp;
496
497 switch (static_cast<RedTy>(MO.getImm())) {
498 case RedTy::ADD:
499 O << ".add";
500 return;
501 case RedTy::MIN:
502 O << ".min";
503 return;
504 case RedTy::MAX:
505 O << ".max";
506 return;
507 case RedTy::INC:
508 O << ".inc";
509 return;
510 case RedTy::DEC:
511 O << ".dec";
512 return;
513 case RedTy::AND:
514 O << ".and";
515 return;
516 case RedTy::OR:
517 O << ".or";
518 return;
519 case RedTy::XOR:
520 O << ".xor";
521 return;
522 }
524 "Invalid Reduction Op in printCpAsyncBulkTensorReductionMode");
525}
526
528 const MCSubtargetInfo &, raw_ostream &O) {
529 const MCOperand &MO = MI->getOperand(OpNum);
530 using CGTy = nvvm::CTAGroupKind;
531
532 switch (static_cast<CGTy>(MO.getImm())) {
533 case CGTy::CG_NONE:
534 O << "";
535 return;
536 case CGTy::CG_1:
537 O << ".cta_group::1";
538 return;
539 case CGTy::CG_2:
540 O << ".cta_group::2";
541 return;
542 }
543 llvm_unreachable("Invalid cta_group in printCTAGroup");
544}
545
547 const MCSubtargetInfo &, raw_ostream &O,
548 StringRef Modifier) {
549 const MCOperand &MO = MI->getOperand(OpNum);
550 assert(MO.isImm() && "Invalid operand");
551 const auto Imm = MO.getImm();
552
553 if (Modifier == "RetList") {
554 assert((Imm == 1 || Imm == 0) && "Invalid return list");
555 if (Imm)
556 O << " (retval0),";
557 return;
558 }
559
560 if (Modifier == "ParamList") {
561 assert(Imm >= 0 && "Invalid parameter list");
563 [&](const auto &I) { O << "param" << I; });
564 return;
565 }
566 llvm_unreachable("Invalid modifier");
567}
568
569template <unsigned Bits>
571 const MCSubtargetInfo &, raw_ostream &O) {
572 const MCOperand &MO = MI->getOperand(OpNum);
573 assert(MO.isImm() && "Expected immediate operand");
574 assert(isInt<Bits>(MO.getImm()) &&
575 "Immediate value does not fit in specified bits");
576 uint64_t Imm = MO.getImm();
577 Imm &= maskTrailingOnes<uint64_t>(Bits);
578 O << formatHex(Imm) << "U";
579}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
static bool hasParamSubqualifiers(const MCSubtargetInfo &STI)
This file contains the definitions of the enumerations and flags associated with NVVM Intrinsics,...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:64
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
WithMarkup markup(raw_ostream &OS, Markup M)
format_object< int64_t > formatHex(int64_t Value) const
const MCInstrInfo & MII
const MCRegisterInfo & MRI
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii, const MCRegisterInfo &mri)
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
int64_t getImm() const
Definition MCInst.h:84
bool isImm() const
Definition MCInst.h:66
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
static constexpr unsigned NoRegister
Definition MCRegister.h:60
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
void printRegName(raw_ostream &OS, MCRegister Reg) override
Print the assembler register name.
void printMemOperand(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O, StringRef Modifier={})
void printAtomicCode(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O, StringRef Modifier={})
void printInstruction(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &O)
void printMmaCode(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O, StringRef Modifier={})
void printTmaReductionMode(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printCallOperand(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O, StringRef Modifier={})
void printProtoIdent(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printCmpMode(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O, StringRef Modifier={})
static const char * getRegisterName(MCRegister Reg)
void printPrmtMode(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printUsedBytesMaskPragma(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printRegisterOrSinkSymbol(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printHexu32imm(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printCvtMode(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O, StringRef Modifier={})
void printFTZFlag(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, const MCSubtargetInfo &STI, raw_ostream &OS) override
Print the specified MCInst to the specified raw_ostream.
void printCTAGroup(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
NVPTXInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
void printNegatedPredicate(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printHexUImm(const MCInst *MI, int OpNum, const MCSubtargetInfo &STI, raw_ostream &O)
void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:140
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ DeviceParam
Definition NVPTX.h:214
@ SharedCluster
Definition NVPTX.h:207
@ EntryParam
Definition NVPTX.h:208
@ DefaultDevice
Definition NVPTX.h:196
@ RelaxedMMIO
Definition NVPTX.h:186
@ AcquireRelease
Definition NVPTX.h:182
@ NotAtomic
Definition NVPTX.h:175
@ SequentiallyConsistent
Definition NVPTX.h:183
This is an optimization pass for GlobalISel generic memory operations.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
void interleaveComma(const Container &c, StreamT &os, UnaryFunctor each_fn)
Definition STLExtras.h:2312
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition Format.h:191
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:77