LLVM 22.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#include "NVPTXGenAsmWriter.inc"
32
36
38 // Decode the virtual register
39 // Must be kept in sync with NVPTXAsmPrinter::encodeVirtualRegister
40 unsigned RCId = (Reg.id() >> 28);
41 switch (RCId) {
42 default: report_fatal_error("Bad virtual register encoding");
43 case 0:
44 // This is actually a physical register, so defer to the autogenerated
45 // register printer
46 OS << getRegisterName(Reg);
47 return;
48 case 1:
49 OS << "%p";
50 break;
51 case 2:
52 OS << "%rs";
53 break;
54 case 3:
55 OS << "%r";
56 break;
57 case 4:
58 OS << "%rd";
59 break;
60 case 5:
61 OS << "%f";
62 break;
63 case 6:
64 OS << "%fd";
65 break;
66 case 7:
67 OS << "%rq";
68 break;
69 }
70
71 unsigned VReg = Reg.id() & 0x0FFFFFFF;
72 OS << VReg;
73}
74
76 StringRef Annot, const MCSubtargetInfo &STI,
77 raw_ostream &OS) {
79
80 // Next always print the annotation.
81 printAnnotation(OS, Annot);
82}
83
84void NVPTXInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
85 raw_ostream &O) {
86 const MCOperand &Op = MI->getOperand(OpNo);
87 if (Op.isReg()) {
88 MCRegister Reg = Op.getReg();
89 printRegName(O, Reg);
90 } else if (Op.isImm()) {
91 markup(O, Markup::Immediate) << formatImm(Op.getImm());
92 } else {
93 assert(Op.isExpr() && "Unknown operand kind in printOperand");
94 MAI.printExpr(O, *Op.getExpr());
95 }
96}
97
99 StringRef Modifier) {
100 const MCOperand &MO = MI->getOperand(OpNum);
101 int64_t Imm = MO.getImm();
102
103 if (Modifier == "ftz") {
104 // FTZ flag
106 O << ".ftz";
107 return;
108 } else if (Modifier == "sat") {
109 // SAT flag
111 O << ".sat";
112 return;
113 } else if (Modifier == "relu") {
114 // RELU flag
116 O << ".relu";
117 return;
118 } else if (Modifier == "base") {
119 // Default operand
120 switch (Imm & NVPTX::PTXCvtMode::BASE_MASK) {
121 default:
122 return;
124 return;
126 O << ".rni";
127 return;
129 O << ".rzi";
130 return;
132 O << ".rmi";
133 return;
135 O << ".rpi";
136 return;
138 O << ".rn";
139 return;
141 O << ".rz";
142 return;
144 O << ".rm";
145 return;
147 O << ".rp";
148 return;
150 O << ".rna";
151 return;
153 O << ".rs";
154 return;
155 }
156 }
157 llvm_unreachable("Invalid conversion modifier");
158}
159
161 raw_ostream &O) {
162 const MCOperand &MO = MI->getOperand(OpNum);
163 const int Imm = MO.getImm();
164 if (Imm)
165 O << ".ftz";
166}
167
169 StringRef Modifier) {
170 const MCOperand &MO = MI->getOperand(OpNum);
171 int64_t Imm = MO.getImm();
172
173 if (Modifier == "FCmp") {
174 switch (Imm) {
175 default:
176 return;
178 O << "eq";
179 return;
181 O << "ne";
182 return;
184 O << "lt";
185 return;
187 O << "le";
188 return;
190 O << "gt";
191 return;
193 O << "ge";
194 return;
196 O << "equ";
197 return;
199 O << "neu";
200 return;
202 O << "ltu";
203 return;
205 O << "leu";
206 return;
208 O << "gtu";
209 return;
211 O << "geu";
212 return;
214 O << "num";
215 return;
217 O << "nan";
218 return;
219 }
220 }
221 if (Modifier == "ICmp") {
222 switch (Imm) {
223 default:
224 llvm_unreachable("Invalid ICmp mode");
226 O << "eq";
227 return;
229 O << "ne";
230 return;
233 O << "lt";
234 return;
237 O << "le";
238 return;
241 O << "gt";
242 return;
245 O << "ge";
246 return;
247 }
248 }
249 if (Modifier == "IType") {
250 switch (Imm) {
251 default:
252 llvm_unreachable("Invalid IType");
255 O << "b";
256 return;
261 O << "s";
262 return;
267 O << "u";
268 return;
269 }
270 }
271 llvm_unreachable("Empty Modifier");
272}
273
275 raw_ostream &O, StringRef Modifier) {
276 const MCOperand &MO = MI->getOperand(OpNum);
277 int Imm = (int)MO.getImm();
278 if (Modifier == "sem") {
279 auto Ordering = NVPTX::Ordering(Imm);
280 switch (Ordering) {
282 return;
284 O << ".relaxed";
285 return;
287 O << ".acquire";
288 return;
290 O << ".release";
291 return;
293 O << ".acq_rel";
294 return;
297 "NVPTX AtomicCode Printer does not support \"seq_cst\" ordering.");
298 return;
300 O << ".volatile";
301 return;
303 O << ".mmio.relaxed";
304 return;
305 }
306 } else if (Modifier == "scope") {
307 auto S = NVPTX::Scope(Imm);
308 switch (S) {
311 return;
313 O << ".sys";
314 return;
316 O << ".cta";
317 return;
319 O << ".cluster";
320 return;
322 O << ".gpu";
323 return;
324 }
326 "NVPTX AtomicCode Printer does not support \"{}\" scope modifier.",
327 ScopeToString(S)));
328 } else if (Modifier == "addsp") {
329 auto A = NVPTX::AddressSpace(Imm);
330 switch (A) {
332 return;
339 O << "." << A;
340 return;
341 }
343 "NVPTX AtomicCode Printer does not support \"{}\" addsp modifier.",
344 AddressSpaceToString(A)));
345 } else if (Modifier == "sign") {
346 switch (Imm) {
348 O << "s";
349 return;
351 O << "u";
352 return;
354 O << "b";
355 return;
357 O << "f";
358 return;
359 default:
360 llvm_unreachable("Unknown register type");
361 }
362 }
363 llvm_unreachable(formatv("Unknown Modifier: {}", Modifier).str().c_str());
364}
365
367 StringRef Modifier) {
368 const MCOperand &MO = MI->getOperand(OpNum);
369 int Imm = (int)MO.getImm();
370 if (Modifier.empty() || Modifier == "version") {
371 O << Imm; // Just print out PTX version
372 return;
373 } else if (Modifier == "aligned") {
374 // PTX63 requires '.aligned' in the name of the instruction.
375 if (Imm >= 63)
376 O << ".aligned";
377 return;
378 }
379 llvm_unreachable("Unknown Modifier");
380}
381
383 raw_ostream &O, StringRef Modifier) {
384 printOperand(MI, OpNum, O);
385
386 if (Modifier == "add") {
387 O << ", ";
388 printOperand(MI, OpNum + 1, O);
389 } else {
390 if (MI->getOperand(OpNum + 1).isImm() &&
391 MI->getOperand(OpNum + 1).getImm() == 0)
392 return; // don't print ',0' or '+0'
393 O << "+";
394 printOperand(MI, OpNum + 1, O);
395 }
396}
397
399 raw_ostream &O) {
400 int64_t Imm = MI->getOperand(OpNum).getImm();
401 O << formatHex(Imm) << "U";
402}
403
405 raw_ostream &O) {
406 const MCOperand &Op = MI->getOperand(OpNum);
407 assert(Op.isExpr() && "Call prototype is not an MCExpr?");
408 const MCExpr *Expr = Op.getExpr();
409 const MCSymbol &Sym = cast<MCSymbolRefExpr>(Expr)->getSymbol();
410 O << Sym.getName();
411}
412
414 raw_ostream &O) {
415 const MCOperand &MO = MI->getOperand(OpNum);
416 int64_t Imm = MO.getImm();
417
418 switch (Imm) {
419 default:
420 return;
422 return;
424 O << ".f4e";
425 return;
427 O << ".b4e";
428 return;
430 O << ".rc8";
431 return;
433 O << ".ecl";
434 return;
436 O << ".ecr";
437 return;
439 O << ".rc16";
440 return;
441 }
442}
443
445 raw_ostream &O) {
446 const MCOperand &MO = MI->getOperand(OpNum);
447 using RedTy = nvvm::TMAReductionOp;
448
449 switch (static_cast<RedTy>(MO.getImm())) {
450 case RedTy::ADD:
451 O << ".add";
452 return;
453 case RedTy::MIN:
454 O << ".min";
455 return;
456 case RedTy::MAX:
457 O << ".max";
458 return;
459 case RedTy::INC:
460 O << ".inc";
461 return;
462 case RedTy::DEC:
463 O << ".dec";
464 return;
465 case RedTy::AND:
466 O << ".and";
467 return;
468 case RedTy::OR:
469 O << ".or";
470 return;
471 case RedTy::XOR:
472 O << ".xor";
473 return;
474 }
476 "Invalid Reduction Op in printCpAsyncBulkTensorReductionMode");
477}
478
480 raw_ostream &O) {
481 const MCOperand &MO = MI->getOperand(OpNum);
482 using CGTy = nvvm::CTAGroupKind;
483
484 switch (static_cast<CGTy>(MO.getImm())) {
485 case CGTy::CG_NONE:
486 O << "";
487 return;
488 case CGTy::CG_1:
489 O << ".cta_group::1";
490 return;
491 case CGTy::CG_2:
492 O << ".cta_group::2";
493 return;
494 }
495 llvm_unreachable("Invalid cta_group in printCTAGroup");
496}
497
499 raw_ostream &O, StringRef Modifier) {
500 const MCOperand &MO = MI->getOperand(OpNum);
501 assert(MO.isImm() && "Invalid operand");
502 const auto Imm = MO.getImm();
503
504 if (Modifier == "RetList") {
505 assert((Imm == 1 || Imm == 0) && "Invalid return list");
506 if (Imm)
507 O << " (retval0),";
508 return;
509 }
510
511 if (Modifier == "ParamList") {
512 assert(Imm >= 0 && "Invalid parameter list");
514 [&](const auto &I) { O << "param" << I; });
515 return;
516 }
517 llvm_unreachable("Invalid modifier");
518}
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:58
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:33
Generic base class for all target subtargets.
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 printFTZFlag(const MCInst *MI, int OpNum, raw_ostream &O)
void printCmpMode(const MCInst *MI, int OpNum, raw_ostream &O, StringRef Modifier={})
void printRegName(raw_ostream &OS, MCRegister Reg) override
Print the assembler register name.
void printHexu32imm(const MCInst *MI, int OpNum, raw_ostream &O)
void printCTAGroup(const MCInst *MI, int OpNum, raw_ostream &O)
void printCvtMode(const MCInst *MI, int OpNum, raw_ostream &O, StringRef Modifier={})
void printMmaCode(const MCInst *MI, int OpNum, raw_ostream &O, StringRef Modifier={})
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O)
void printProtoIdent(const MCInst *MI, int OpNum, raw_ostream &O)
void printAtomicCode(const MCInst *MI, int OpNum, raw_ostream &O, StringRef Modifier={})
static const char * getRegisterName(MCRegister Reg)
void printCallOperand(const MCInst *MI, int OpNum, raw_ostream &O, StringRef Modifier={})
void printTmaReductionMode(const MCInst *MI, int OpNum, 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.
NVPTXInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
void printPrmtMode(const MCInst *MI, int OpNum, raw_ostream &O)
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printMemOperand(const MCInst *MI, int OpNum, raw_ostream &O, StringRef Modifier={})
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:143
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.
@ SharedCluster
Definition NVPTX.h:187
@ DefaultDevice
Definition NVPTX.h:176
@ RelaxedMMIO
Definition NVPTX.h:166
@ AcquireRelease
Definition NVPTX.h:162
@ NotAtomic
Definition NVPTX.h:155
@ SequentiallyConsistent
Definition NVPTX.h:163
This is an optimization pass for GlobalISel generic memory operations.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
void interleaveComma(const Container &c, StreamT &os, UnaryFunctor each_fn)
Definition STLExtras.h:2231
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:167
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305