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 == "relu") {
122 // RELU flag
124 O << ".relu";
125 return;
126 } else if (Modifier == "base") {
127 // Default operand
128 switch (Imm & NVPTX::PTXCvtMode::BASE_MASK) {
129 default:
130 return;
132 return;
134 O << ".rni";
135 return;
137 O << ".rzi";
138 return;
140 O << ".rmi";
141 return;
143 O << ".rpi";
144 return;
146 O << ".rn";
147 return;
149 O << ".rz";
150 return;
152 O << ".rm";
153 return;
155 O << ".rp";
156 return;
158 O << ".rna";
159 return;
161 O << ".rs";
162 return;
163 }
164 }
165 llvm_unreachable("Invalid conversion modifier");
166}
167
169 const MCSubtargetInfo &, raw_ostream &O) {
170 const MCOperand &MO = MI->getOperand(OpNum);
171 const int Imm = MO.getImm();
172 if (Imm)
173 O << ".ftz";
174}
175
177 const MCSubtargetInfo &, raw_ostream &O,
178 StringRef Modifier) {
179 const MCOperand &MO = MI->getOperand(OpNum);
180 int64_t Imm = MO.getImm();
181
182 if (Modifier == "FCmp") {
183 switch (Imm) {
184 default:
185 return;
187 O << "eq";
188 return;
190 O << "ne";
191 return;
193 O << "lt";
194 return;
196 O << "le";
197 return;
199 O << "gt";
200 return;
202 O << "ge";
203 return;
205 O << "equ";
206 return;
208 O << "neu";
209 return;
211 O << "ltu";
212 return;
214 O << "leu";
215 return;
217 O << "gtu";
218 return;
220 O << "geu";
221 return;
223 O << "num";
224 return;
226 O << "nan";
227 return;
228 }
229 }
230 if (Modifier == "ICmp") {
231 switch (Imm) {
232 default:
233 llvm_unreachable("Invalid ICmp mode");
235 O << "eq";
236 return;
238 O << "ne";
239 return;
242 O << "lt";
243 return;
246 O << "le";
247 return;
250 O << "gt";
251 return;
254 O << "ge";
255 return;
256 }
257 }
258 if (Modifier == "IType") {
259 switch (Imm) {
260 default:
261 llvm_unreachable("Invalid IType");
264 O << "b";
265 return;
270 O << "s";
271 return;
276 O << "u";
277 return;
278 }
279 }
280 llvm_unreachable("Empty Modifier");
281}
282
284 const MCSubtargetInfo &STI,
285 raw_ostream &O, StringRef Modifier) {
286 const MCOperand &MO = MI->getOperand(OpNum);
287 int Imm = (int)MO.getImm();
288 if (Modifier == "sem") {
289 auto Ordering = NVPTX::Ordering(Imm);
290 switch (Ordering) {
292 return;
294 O << ".relaxed";
295 return;
297 O << ".acquire";
298 return;
300 O << ".release";
301 return;
303 O << ".acq_rel";
304 return;
307 "NVPTX AtomicCode Printer does not support \"seq_cst\" ordering.");
308 return;
310 O << ".volatile";
311 return;
313 O << ".mmio.relaxed";
314 return;
315 }
316 } else if (Modifier == "scope") {
317 auto S = NVPTX::Scope(Imm);
318 switch (S) {
321 return;
323 O << ".sys";
324 return;
326 O << ".cta";
327 return;
329 O << ".cluster";
330 return;
332 O << ".gpu";
333 return;
334 }
336 "NVPTX AtomicCode Printer does not support \"{}\" scope modifier.",
337 ScopeToString(S)));
338 } else if (Modifier == "addsp") {
339 auto A = NVPTX::AddressSpace(Imm);
340 switch (A) {
342 return;
350 O << "." << addressSpaceToString(A, hasParamSubqualifiers(STI));
351 return;
352 }
354 "NVPTX AtomicCode Printer does not support \"{}\" addsp modifier.",
355 addressSpaceToString(A)));
356 } else if (Modifier == "sign") {
357 switch (Imm) {
359 O << "s";
360 return;
362 O << "u";
363 return;
365 O << "b";
366 return;
368 O << "f";
369 return;
370 default:
371 llvm_unreachable("Unknown register type");
372 }
373 }
374 llvm_unreachable(formatv("Unknown Modifier: {}", Modifier).str().c_str());
375}
376
378 const MCSubtargetInfo &, raw_ostream &O,
379 StringRef Modifier) {
380 const MCOperand &MO = MI->getOperand(OpNum);
381 int Imm = (int)MO.getImm();
382 if (Modifier.empty() || Modifier == "version") {
383 O << Imm; // Just print out PTX version
384 return;
385 } else if (Modifier == "aligned") {
386 // PTX63 requires '.aligned' in the name of the instruction.
387 if (Imm >= 63)
388 O << ".aligned";
389 return;
390 }
391 llvm_unreachable("Unknown Modifier");
392}
393
395 const MCSubtargetInfo &STI,
396 raw_ostream &O, StringRef Modifier) {
397 printOperand(MI, OpNum, STI, O);
398
399 if (Modifier == "add") {
400 O << ", ";
401 printOperand(MI, OpNum + 1, STI, O);
402 } else {
403 if (MI->getOperand(OpNum + 1).isImm() &&
404 MI->getOperand(OpNum + 1).getImm() == 0)
405 return; // don't print ',0' or '+0'
406 O << "+";
407 printOperand(MI, OpNum + 1, STI, O);
408 }
409}
410
412 const MCSubtargetInfo &,
413 raw_ostream &O) {
414 auto &Op = MI->getOperand(OpNum);
415 assert(Op.isImm() && "Invalid operand");
416 uint32_t Imm = (uint32_t)Op.getImm();
417 if (Imm != UINT32_MAX) {
418 O << ".pragma \"used_bytes_mask " << format_hex(Imm, 1) << "\";\n\t";
419 }
420}
421
423 const MCSubtargetInfo &STI,
424 raw_ostream &O) {
425 const MCOperand &Op = MI->getOperand(OpNum);
426 if (Op.isReg() && Op.getReg() == MCRegister::NoRegister)
427 O << "_";
428 else
429 printOperand(MI, OpNum, STI, O);
430}
431
433 const MCSubtargetInfo &, raw_ostream &O) {
434 int64_t Imm = MI->getOperand(OpNum).getImm();
435 O << formatHex(Imm) << "U";
436}
437
439 const MCSubtargetInfo &,
440 raw_ostream &O) {
441 const MCOperand &Op = MI->getOperand(OpNum);
442 assert(Op.isExpr() && "Call prototype is not an MCExpr?");
443 const MCExpr *Expr = Op.getExpr();
444 const MCSymbol &Sym = cast<MCSymbolRefExpr>(Expr)->getSymbol();
445 O << Sym.getName();
446}
447
449 const MCSubtargetInfo &, raw_ostream &O) {
450 const MCOperand &MO = MI->getOperand(OpNum);
451 int64_t Imm = MO.getImm();
452
453 switch (Imm) {
454 default:
455 return;
457 return;
459 O << ".f4e";
460 return;
462 O << ".b4e";
463 return;
465 O << ".rc8";
466 return;
468 O << ".ecl";
469 return;
471 O << ".ecr";
472 return;
474 O << ".rc16";
475 return;
476 }
477}
478
480 const MCSubtargetInfo &,
481 raw_ostream &O) {
482 const MCOperand &MO = MI->getOperand(OpNum);
483 using RedTy = nvvm::TMAReductionOp;
484
485 switch (static_cast<RedTy>(MO.getImm())) {
486 case RedTy::ADD:
487 O << ".add";
488 return;
489 case RedTy::MIN:
490 O << ".min";
491 return;
492 case RedTy::MAX:
493 O << ".max";
494 return;
495 case RedTy::INC:
496 O << ".inc";
497 return;
498 case RedTy::DEC:
499 O << ".dec";
500 return;
501 case RedTy::AND:
502 O << ".and";
503 return;
504 case RedTy::OR:
505 O << ".or";
506 return;
507 case RedTy::XOR:
508 O << ".xor";
509 return;
510 }
512 "Invalid Reduction Op in printCpAsyncBulkTensorReductionMode");
513}
514
516 const MCSubtargetInfo &, raw_ostream &O) {
517 const MCOperand &MO = MI->getOperand(OpNum);
518 using CGTy = nvvm::CTAGroupKind;
519
520 switch (static_cast<CGTy>(MO.getImm())) {
521 case CGTy::CG_NONE:
522 O << "";
523 return;
524 case CGTy::CG_1:
525 O << ".cta_group::1";
526 return;
527 case CGTy::CG_2:
528 O << ".cta_group::2";
529 return;
530 }
531 llvm_unreachable("Invalid cta_group in printCTAGroup");
532}
533
535 const MCSubtargetInfo &, raw_ostream &O,
536 StringRef Modifier) {
537 const MCOperand &MO = MI->getOperand(OpNum);
538 assert(MO.isImm() && "Invalid operand");
539 const auto Imm = MO.getImm();
540
541 if (Modifier == "RetList") {
542 assert((Imm == 1 || Imm == 0) && "Invalid return list");
543 if (Imm)
544 O << " (retval0),";
545 return;
546 }
547
548 if (Modifier == "ParamList") {
549 assert(Imm >= 0 && "Invalid parameter list");
551 [&](const auto &I) { O << "param" << I; });
552 return;
553 }
554 llvm_unreachable("Invalid modifier");
555}
556
557template <unsigned Bits>
559 const MCSubtargetInfo &, raw_ostream &O) {
560 const MCOperand &MO = MI->getOperand(OpNum);
561 assert(MO.isImm() && "Expected immediate operand");
562 assert(isInt<Bits>(MO.getImm()) &&
563 "Immediate value does not fit in specified bits");
564 uint64_t Imm = MO.getImm();
565 Imm &= maskTrailingOnes<uint64_t>(Bits);
566 O << formatHex(Imm) << "U";
567}
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 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:2313
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