LLVM 19.0.0git
SPIRVInstPrinter.cpp
Go to the documentation of this file.
1//===-- SPIRVInstPrinter.cpp - Output SPIR-V MCInsts as ASM -----*- C++ -*-===//
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 class prints a SPIR-V MCInst to a .s file.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SPIRVInstPrinter.h"
14#include "SPIRV.h"
15#include "SPIRVBaseInfo.h"
16#include "SPIRVInstrInfo.h"
17#include "llvm/ADT/APFloat.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCInstrInfo.h"
23#include "llvm/MC/MCSymbol.h"
27
28using namespace llvm;
29using namespace llvm::SPIRV;
30
31#define DEBUG_TYPE "asm-printer"
32
33// Include the auto-generated portion of the assembly writer.
34#include "SPIRVGenAsmWriter.inc"
35
37 unsigned StartIndex,
38 raw_ostream &O,
39 bool SkipFirstSpace,
40 bool SkipImmediates) {
41 const unsigned NumOps = MI->getNumOperands();
42 for (unsigned i = StartIndex; i < NumOps; ++i) {
43 if (!SkipImmediates || !MI->getOperand(i).isImm()) {
44 if (!SkipFirstSpace || i != StartIndex)
45 O << ' ';
46 printOperand(MI, i, O);
47 }
48 }
49}
50
52 unsigned StartIndex,
53 raw_ostream &O) {
54 unsigned IsBitwidth16 = MI->getFlags() & SPIRV::ASM_PRINTER_WIDTH16;
55 const unsigned NumVarOps = MI->getNumOperands() - StartIndex;
56
57 assert((NumVarOps == 1 || NumVarOps == 2) &&
58 "Unsupported number of bits for literal variable");
59
60 O << ' ';
61
62 uint64_t Imm = MI->getOperand(StartIndex).getImm();
63
64 // Handle 64 bit literals.
65 if (NumVarOps == 2) {
66 Imm |= (MI->getOperand(StartIndex + 1).getImm() << 32);
67 }
68
69 // Format and print float values.
70 if (MI->getOpcode() == SPIRV::OpConstantF && IsBitwidth16 == 0) {
71 APFloat FP = NumVarOps == 1 ? APFloat(APInt(32, Imm).bitsToFloat())
72 : APFloat(APInt(64, Imm).bitsToDouble());
73
74 // Print infinity and NaN as hex floats.
75 // TODO: Make sure subnormal numbers are handled correctly as they may also
76 // require hex float notation.
77 if (FP.isInfinity()) {
78 if (FP.isNegative())
79 O << '-';
80 O << "0x1p+128";
81 return;
82 }
83 if (FP.isNaN()) {
84 O << "0x1.8p+128";
85 return;
86 }
87
88 // Format val as a decimal floating point or scientific notation (whichever
89 // is shorter), with enough digits of precision to produce the exact value.
90 O << format("%.*g", std::numeric_limits<double>::max_digits10,
91 FP.convertToDouble());
92
93 return;
94 }
95
96 // Print integer values directly.
97 O << Imm;
98}
99
100void SPIRVInstPrinter::recordOpExtInstImport(const MCInst *MI) {
101 Register Reg = MI->getOperand(0).getReg();
102 auto Name = getSPIRVStringOperand(*MI, 1);
103 auto Set = getExtInstSetFromString(Name);
104 ExtInstSetIDs.insert({Reg, Set});
105}
106
108 StringRef Annot, const MCSubtargetInfo &STI,
109 raw_ostream &OS) {
110 const unsigned OpCode = MI->getOpcode();
112
113 if (OpCode == SPIRV::OpDecorate) {
115 } else if (OpCode == SPIRV::OpExtInstImport) {
116 recordOpExtInstImport(MI);
117 } else if (OpCode == SPIRV::OpExtInst) {
119 } else {
120 // Print any extra operands for variadic instructions.
121 const MCInstrDesc &MCDesc = MII.get(OpCode);
122 if (MCDesc.isVariadic()) {
123 const unsigned NumFixedOps = MCDesc.getNumOperands();
124 const unsigned LastFixedIndex = NumFixedOps - 1;
125 const int FirstVariableIndex = NumFixedOps;
126 if (NumFixedOps > 0 && MCDesc.operands()[LastFixedIndex].OperandType ==
128 // For instructions where a custom type (not reg or immediate) comes as
129 // the last operand before the variable_ops. This is usually a StringImm
130 // operand, but there are a few other cases.
131 switch (OpCode) {
132 case SPIRV::OpTypeImage:
133 OS << ' ';
134 printSymbolicOperand<OperandCategory::AccessQualifierOperand>(
135 MI, FirstVariableIndex, OS);
136 break;
137 case SPIRV::OpVariable:
138 OS << ' ';
139 printOperand(MI, FirstVariableIndex, OS);
140 break;
141 case SPIRV::OpEntryPoint: {
142 // Print the interface ID operands, skipping the name's string
143 // literal.
144 printRemainingVariableOps(MI, NumFixedOps, OS, false, true);
145 break;
146 }
147 case SPIRV::OpExecutionMode:
148 case SPIRV::OpExecutionModeId:
149 case SPIRV::OpLoopMerge: {
150 // Print any literals after the OPERAND_UNKNOWN argument normally.
151 printRemainingVariableOps(MI, NumFixedOps, OS);
152 break;
153 }
154 default:
155 break; // printStringImm has already been handled.
156 }
157 } else {
158 // For instructions with no fixed ops or a reg/immediate as the final
159 // fixed operand, we can usually print the rest with "printOperand", but
160 // check for a few cases with custom types first.
161 switch (OpCode) {
162 case SPIRV::OpLoad:
163 case SPIRV::OpStore:
164 OS << ' ';
165 printSymbolicOperand<OperandCategory::MemoryOperandOperand>(
166 MI, FirstVariableIndex, OS);
167 printRemainingVariableOps(MI, FirstVariableIndex + 1, OS);
168 break;
169 case SPIRV::OpImageSampleImplicitLod:
170 case SPIRV::OpImageSampleDrefImplicitLod:
171 case SPIRV::OpImageSampleProjImplicitLod:
172 case SPIRV::OpImageSampleProjDrefImplicitLod:
173 case SPIRV::OpImageFetch:
174 case SPIRV::OpImageGather:
175 case SPIRV::OpImageDrefGather:
176 case SPIRV::OpImageRead:
177 case SPIRV::OpImageWrite:
178 case SPIRV::OpImageSparseSampleImplicitLod:
179 case SPIRV::OpImageSparseSampleDrefImplicitLod:
180 case SPIRV::OpImageSparseSampleProjImplicitLod:
181 case SPIRV::OpImageSparseSampleProjDrefImplicitLod:
182 case SPIRV::OpImageSparseFetch:
183 case SPIRV::OpImageSparseGather:
184 case SPIRV::OpImageSparseDrefGather:
185 case SPIRV::OpImageSparseRead:
186 case SPIRV::OpImageSampleFootprintNV:
187 OS << ' ';
188 printSymbolicOperand<OperandCategory::ImageOperandOperand>(
189 MI, FirstVariableIndex, OS);
190 printRemainingVariableOps(MI, NumFixedOps + 1, OS);
191 break;
192 case SPIRV::OpCopyMemory:
193 case SPIRV::OpCopyMemorySized: {
194 const unsigned NumOps = MI->getNumOperands();
195 for (unsigned i = NumFixedOps; i < NumOps; ++i) {
196 OS << ' ';
197 printSymbolicOperand<OperandCategory::MemoryOperandOperand>(MI, i,
198 OS);
199 if (MI->getOperand(i).getImm() & MemoryOperand::Aligned) {
200 assert(i + 1 < NumOps && "Missing alignment operand");
201 OS << ' ';
202 printOperand(MI, i + 1, OS);
203 i += 1;
204 }
205 }
206 break;
207 }
208 case SPIRV::OpConstantI:
209 case SPIRV::OpConstantF:
210 // The last fixed operand along with any variadic operands that follow
211 // are part of the variable value.
212 printOpConstantVarOps(MI, NumFixedOps - 1, OS);
213 break;
214 default:
215 printRemainingVariableOps(MI, NumFixedOps, OS);
216 break;
217 }
218 }
219 }
220 }
221
222 printAnnotation(OS, Annot);
223}
224
226 // The fixed operands have already been printed, so just need to decide what
227 // type of ExtInst operands to print based on the instruction set and number.
228 const MCInstrDesc &MCDesc = MII.get(MI->getOpcode());
229 unsigned NumFixedOps = MCDesc.getNumOperands();
230 const auto NumOps = MI->getNumOperands();
231 if (NumOps == NumFixedOps)
232 return;
233
234 O << ' ';
235
236 // TODO: implement special printing for OpenCLExtInst::vstor*.
237 printRemainingVariableOps(MI, NumFixedOps, O, true);
238}
239
241 // The fixed operands have already been printed, so just need to decide what
242 // type of decoration operands to print based on the Decoration type.
243 const MCInstrDesc &MCDesc = MII.get(MI->getOpcode());
244 unsigned NumFixedOps = MCDesc.getNumOperands();
245
246 if (NumFixedOps != MI->getNumOperands()) {
247 auto DecOp = MI->getOperand(NumFixedOps - 1);
248 auto Dec = static_cast<Decoration::Decoration>(DecOp.getImm());
249
250 O << ' ';
251
252 switch (Dec) {
253 case Decoration::BuiltIn:
254 printSymbolicOperand<OperandCategory::BuiltInOperand>(MI, NumFixedOps, O);
255 break;
256 case Decoration::UniformId:
257 printSymbolicOperand<OperandCategory::ScopeOperand>(MI, NumFixedOps, O);
258 break;
259 case Decoration::FuncParamAttr:
260 printSymbolicOperand<OperandCategory::FunctionParameterAttributeOperand>(
261 MI, NumFixedOps, O);
262 break;
263 case Decoration::FPRoundingMode:
264 printSymbolicOperand<OperandCategory::FPRoundingModeOperand>(
265 MI, NumFixedOps, O);
266 break;
267 case Decoration::FPFastMathMode:
268 printSymbolicOperand<OperandCategory::FPFastMathModeOperand>(
269 MI, NumFixedOps, O);
270 break;
271 case Decoration::LinkageAttributes:
272 case Decoration::UserSemantic:
273 printStringImm(MI, NumFixedOps, O);
274 break;
275 default:
276 printRemainingVariableOps(MI, NumFixedOps, O, true);
277 break;
278 }
279 }
280}
281
282static void printExpr(const MCExpr *Expr, raw_ostream &O) {
283#ifndef NDEBUG
284 const MCSymbolRefExpr *SRE;
285
286 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
287 SRE = cast<MCSymbolRefExpr>(BE->getLHS());
288 else
289 SRE = cast<MCSymbolRefExpr>(Expr);
290
292
294#endif
295 O << *Expr;
296}
297
298void SPIRVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
299 raw_ostream &O, const char *Modifier) {
300 assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
301 if (OpNo < MI->getNumOperands()) {
302 const MCOperand &Op = MI->getOperand(OpNo);
303 if (Op.isReg())
304 O << '%' << (Register::virtReg2Index(Op.getReg()) + 1);
305 else if (Op.isImm())
306 O << formatImm((int64_t)Op.getImm());
307 else if (Op.isDFPImm())
308 O << formatImm((double)Op.getDFPImm());
309 else if (Op.isExpr())
310 printExpr(Op.getExpr(), O);
311 else
312 llvm_unreachable("Unexpected operand type");
313 }
314}
315
316void SPIRVInstPrinter::printStringImm(const MCInst *MI, unsigned OpNo,
317 raw_ostream &O) {
318 const unsigned NumOps = MI->getNumOperands();
319 unsigned StrStartIndex = OpNo;
320 while (StrStartIndex < NumOps) {
321 if (MI->getOperand(StrStartIndex).isReg())
322 break;
323
324 std::string Str = getSPIRVStringOperand(*MI, OpNo);
325 if (StrStartIndex != OpNo)
326 O << ' '; // Add a space if we're starting a new string/argument.
327 O << '"';
328 for (char c : Str) {
329 if (c == '"')
330 O.write('\\'); // Escape " characters (might break for complex UTF-8).
331 O.write(c);
332 }
333 O << '"';
334
335 unsigned numOpsInString = (Str.size() / 4) + 1;
336 StrStartIndex += numOpsInString;
337
338 // Check for final Op of "OpDecorate %x %stringImm %linkageAttribute".
339 if (MI->getOpcode() == SPIRV::OpDecorate &&
340 MI->getOperand(1).getImm() ==
341 static_cast<unsigned>(Decoration::LinkageAttributes)) {
342 O << ' ';
343 printSymbolicOperand<OperandCategory::LinkageTypeOperand>(
344 MI, StrStartIndex, O);
345 break;
346 }
347 }
348}
349
350void SPIRVInstPrinter::printExtension(const MCInst *MI, unsigned OpNo,
351 raw_ostream &O) {
352 auto SetReg = MI->getOperand(2).getReg();
353 auto Set = ExtInstSetIDs[SetReg];
354 auto Op = MI->getOperand(OpNo).getImm();
355 O << getExtInstName(Set, Op);
356}
357
358template <OperandCategory::OperandCategory category>
360 raw_ostream &O) {
361 if (OpNo < MI->getNumOperands()) {
362 O << getSymbolicOperandMnemonic(category, MI->getOperand(OpNo).getImm());
363 }
364}
This file declares a class to represent arbitrary precision floating point values and provide a varie...
static void printExpr(const MCExpr *Expr, const MCAsmInfo *MAI, raw_ostream &OS)
std::string Name
IRTranslator LLVM IR MI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Class for arbitrary precision integers.
Definition: APInt.h:76
This class represents an Operation in the Expression.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Binary assembler expressions.
Definition: MCExpr.h:490
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
const MCInstrInfo & MII
Definition: MCInstPrinter.h:52
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Definition: MCInstrDesc.h:237
ArrayRef< MCOperandInfo > operands() const
Definition: MCInstrDesc.h:239
bool isVariadic() const
Return true if this instruction can have a variable number of operands.
Definition: MCInstrDesc.h:261
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition: MCInstrInfo.h:63
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
VariantKind getKind() const
Definition: MCExpr.h:410
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
static unsigned virtReg2Index(Register Reg)
Convert a virtual register number to a 0-based index.
Definition: Register.h:77
void printExtension(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printStringImm(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=nullptr)
void printInstruction(const MCInst *MI, uint64_t Address, 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 printOpExtInst(const MCInst *MI, raw_ostream &O)
void printOpConstantVarOps(const MCInst *MI, unsigned StartIndex, raw_ostream &O)
void printSymbolicOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printRemainingVariableOps(const MCInst *MI, unsigned StartIndex, raw_ostream &O, bool SkipFirstSpace=false, bool SkipImmediates=false)
void printOpDecorate(const MCInst *MI, raw_ostream &O)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ OPERAND_UNKNOWN
Definition: MCInstrDesc.h:59
Lowers a builtin funtion call using the provided DemangledCall skeleton and external instruction Set.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::string getExtInstName(SPIRV::InstructionSet::InstructionSet Set, uint32_t InstructionNumber)
std::string getSPIRVStringOperand(const InstType &MI, unsigned StartIndex)
SPIRV::InstructionSet::InstructionSet getExtInstSetFromString(std::string SetName)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
std::string getSymbolicOperandMnemonic(SPIRV::OperandCategory::OperandCategory Category, int32_t Value)