LLVM 20.0.0git
SPIRVAsmPrinter.cpp
Go to the documentation of this file.
1//===-- SPIRVAsmPrinter.cpp - SPIR-V LLVM assembly writer ------*- 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 file contains a printer that converts from our internal representation
10// of machine-dependent LLVM code to the SPIR-V assembly language.
11//
12//===----------------------------------------------------------------------===//
13
15#include "SPIRV.h"
16#include "SPIRVInstrInfo.h"
17#include "SPIRVMCInstLower.h"
18#include "SPIRVModuleAnalysis.h"
19#include "SPIRVSubtarget.h"
20#include "SPIRVTargetMachine.h"
21#include "SPIRVUtils.h"
23#include "llvm/ADT/DenseMap.h"
31#include "llvm/MC/MCAsmInfo.h"
32#include "llvm/MC/MCAssembler.h"
33#include "llvm/MC/MCInst.h"
36#include "llvm/MC/MCStreamer.h"
37#include "llvm/MC/MCSymbol.h"
40
41using namespace llvm;
42
43#define DEBUG_TYPE "asm-printer"
44
45namespace {
46class SPIRVAsmPrinter : public AsmPrinter {
47 unsigned NLabels = 0;
48
49public:
50 explicit SPIRVAsmPrinter(TargetMachine &TM,
51 std::unique_ptr<MCStreamer> Streamer)
52 : AsmPrinter(TM, std::move(Streamer)), ST(nullptr), TII(nullptr) {}
53 bool ModuleSectionsEmitted;
54 const SPIRVSubtarget *ST;
55 const SPIRVInstrInfo *TII;
56
57 StringRef getPassName() const override { return "SPIRV Assembly Printer"; }
58 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
59 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
60 const char *ExtraCode, raw_ostream &O) override;
61
62 void outputMCInst(MCInst &Inst);
63 void outputInstruction(const MachineInstr *MI);
64 void outputModuleSection(SPIRV::ModuleSectionType MSType);
65 void outputGlobalRequirements();
66 void outputEntryPoints();
67 void outputDebugSourceAndStrings(const Module &M);
68 void outputOpExtInstImports(const Module &M);
69 void outputOpMemoryModel();
70 void outputOpFunctionEnd();
71 void outputExtFuncDecls();
72 void outputExecutionModeFromMDNode(Register Reg, MDNode *Node,
73 SPIRV::ExecutionMode::ExecutionMode EM,
74 unsigned ExpectMDOps, int64_t DefVal);
75 void outputExecutionModeFromNumthreadsAttribute(
76 const Register &Reg, const Attribute &Attr,
77 SPIRV::ExecutionMode::ExecutionMode EM);
78 void outputExecutionMode(const Module &M);
79 void outputAnnotations(const Module &M);
80 void outputModuleSections();
81
82 void emitInstruction(const MachineInstr *MI) override;
83 void emitFunctionEntryLabel() override {}
84 void emitFunctionHeader() override;
85 void emitFunctionBodyStart() override {}
86 void emitFunctionBodyEnd() override;
87 void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
88 void emitBasicBlockEnd(const MachineBasicBlock &MBB) override {}
89 void emitGlobalVariable(const GlobalVariable *GV) override {}
90 void emitOpLabel(const MachineBasicBlock &MBB);
91 void emitEndOfAsmFile(Module &M) override;
92 bool doInitialization(Module &M) override;
93
94 void getAnalysisUsage(AnalysisUsage &AU) const override;
96};
97} // namespace
98
99void SPIRVAsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
103}
104
105// If the module has no functions, we need output global info anyway.
106void SPIRVAsmPrinter::emitEndOfAsmFile(Module &M) {
107 if (ModuleSectionsEmitted == false) {
108 outputModuleSections();
109 ModuleSectionsEmitted = true;
110 }
111
112 ST = static_cast<const SPIRVTargetMachine &>(TM).getSubtargetImpl();
113 VersionTuple SPIRVVersion = ST->getSPIRVVersion();
114 uint32_t Major = SPIRVVersion.getMajor();
115 uint32_t Minor = SPIRVVersion.getMinor().value_or(0);
116 // Bound is an approximation that accounts for the maximum used register
117 // number and number of generated OpLabels
118 unsigned Bound = 2 * (ST->getBound() + 1) + NLabels;
119 if (MCAssembler *Asm = OutStreamer->getAssemblerPtr())
120 static_cast<SPIRVObjectWriter &>(Asm->getWriter())
121 .setBuildVersion(Major, Minor, Bound);
122}
123
124void SPIRVAsmPrinter::emitFunctionHeader() {
125 if (ModuleSectionsEmitted == false) {
126 outputModuleSections();
127 ModuleSectionsEmitted = true;
128 }
129 // Get the subtarget from the current MachineFunction.
130 ST = &MF->getSubtarget<SPIRVSubtarget>();
131 TII = ST->getInstrInfo();
132 const Function &F = MF->getFunction();
133
134 if (isVerbose()) {
135 OutStreamer->getCommentOS()
136 << "-- Begin function "
137 << GlobalValue::dropLLVMManglingEscape(F.getName()) << '\n';
138 }
139
140 auto Section = getObjFileLowering().SectionForGlobal(&F, TM);
141 MF->setSection(Section);
142}
143
144void SPIRVAsmPrinter::outputOpFunctionEnd() {
145 MCInst FunctionEndInst;
146 FunctionEndInst.setOpcode(SPIRV::OpFunctionEnd);
147 outputMCInst(FunctionEndInst);
148}
149
150// Emit OpFunctionEnd at the end of MF and clear BBNumToRegMap.
151void SPIRVAsmPrinter::emitFunctionBodyEnd() {
152 outputOpFunctionEnd();
153 MAI->BBNumToRegMap.clear();
154}
155
156void SPIRVAsmPrinter::emitOpLabel(const MachineBasicBlock &MBB) {
157 MCInst LabelInst;
158 LabelInst.setOpcode(SPIRV::OpLabel);
159 LabelInst.addOperand(MCOperand::createReg(MAI->getOrCreateMBBRegister(MBB)));
160 outputMCInst(LabelInst);
161 ++NLabels;
162}
163
164void SPIRVAsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
165 assert(!MBB.empty() && "MBB is empty!");
166
167 // If it's the first MBB in MF, it has OpFunction and OpFunctionParameter, so
168 // OpLabel should be output after them.
169 if (MBB.getNumber() == MF->front().getNumber()) {
170 for (const MachineInstr &MI : MBB)
171 if (MI.getOpcode() == SPIRV::OpFunction)
172 return;
173 // TODO: this case should be checked by the verifier.
174 report_fatal_error("OpFunction is expected in the front MBB of MF");
175 }
176 emitOpLabel(MBB);
177}
178
179void SPIRVAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
180 raw_ostream &O) {
181 const MachineOperand &MO = MI->getOperand(OpNum);
182
183 switch (MO.getType()) {
186 break;
187
189 O << MO.getImm();
190 break;
191
193 O << MO.getFPImm();
194 break;
195
197 O << *MO.getMBB()->getSymbol();
198 break;
199
201 O << *getSymbol(MO.getGlobal());
202 break;
203
205 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
206 O << BA->getName();
207 break;
208 }
209
211 O << *GetExternalSymbolSymbol(MO.getSymbolName());
212 break;
213
216 default:
217 llvm_unreachable("<unknown operand type>");
218 }
219}
220
221bool SPIRVAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
222 const char *ExtraCode, raw_ostream &O) {
223 if (ExtraCode && ExtraCode[0])
224 return true; // Invalid instruction - SPIR-V does not have special modifiers
225
226 printOperand(MI, OpNo, O);
227 return false;
228}
229
231 const SPIRVInstrInfo *TII) {
232 return TII->isHeaderInstr(*MI) || MI->getOpcode() == SPIRV::OpFunction ||
233 MI->getOpcode() == SPIRV::OpFunctionParameter;
234}
235
236void SPIRVAsmPrinter::outputMCInst(MCInst &Inst) {
237 OutStreamer->emitInstruction(Inst, *OutContext.getSubtargetInfo());
238}
239
240void SPIRVAsmPrinter::outputInstruction(const MachineInstr *MI) {
241 SPIRVMCInstLower MCInstLowering;
242 MCInst TmpInst;
243 MCInstLowering.lower(MI, TmpInst, MAI);
244 outputMCInst(TmpInst);
245}
246
247void SPIRVAsmPrinter::emitInstruction(const MachineInstr *MI) {
248 SPIRV_MC::verifyInstructionPredicates(MI->getOpcode(),
249 getSubtargetInfo().getFeatureBits());
250
251 if (!MAI->getSkipEmission(MI))
252 outputInstruction(MI);
253
254 // Output OpLabel after OpFunction and OpFunctionParameter in the first MBB.
255 const MachineInstr *NextMI = MI->getNextNode();
256 if (!MAI->hasMBBRegister(*MI->getParent()) && isFuncOrHeaderInstr(MI, TII) &&
257 (!NextMI || !isFuncOrHeaderInstr(NextMI, TII))) {
258 assert(MI->getParent()->getNumber() == MF->front().getNumber() &&
259 "OpFunction is not in the front MBB of MF");
260 emitOpLabel(*MI->getParent());
261 }
262}
263
264void SPIRVAsmPrinter::outputModuleSection(SPIRV::ModuleSectionType MSType) {
265 for (MachineInstr *MI : MAI->getMSInstrs(MSType))
266 outputInstruction(MI);
267}
268
269void SPIRVAsmPrinter::outputDebugSourceAndStrings(const Module &M) {
270 // Output OpSourceExtensions.
271 for (auto &Str : MAI->SrcExt) {
272 MCInst Inst;
273 Inst.setOpcode(SPIRV::OpSourceExtension);
274 addStringImm(Str.first(), Inst);
275 outputMCInst(Inst);
276 }
277 // Output OpSource.
278 MCInst Inst;
279 Inst.setOpcode(SPIRV::OpSource);
280 Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(MAI->SrcLang)));
281 Inst.addOperand(
282 MCOperand::createImm(static_cast<unsigned>(MAI->SrcLangVersion)));
283 outputMCInst(Inst);
284}
285
286void SPIRVAsmPrinter::outputOpExtInstImports(const Module &M) {
287 for (auto &CU : MAI->ExtInstSetMap) {
288 unsigned Set = CU.first;
289 Register Reg = CU.second;
290 MCInst Inst;
291 Inst.setOpcode(SPIRV::OpExtInstImport);
294 static_cast<SPIRV::InstructionSet::InstructionSet>(Set)),
295 Inst);
296 outputMCInst(Inst);
297 }
298}
299
300void SPIRVAsmPrinter::outputOpMemoryModel() {
301 MCInst Inst;
302 Inst.setOpcode(SPIRV::OpMemoryModel);
303 Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(MAI->Addr)));
304 Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(MAI->Mem)));
305 outputMCInst(Inst);
306}
307
308// Before the OpEntryPoints' output, we need to add the entry point's
309// interfaces. The interface is a list of IDs of global OpVariable instructions.
310// These declare the set of global variables from a module that form
311// the interface of this entry point.
312void SPIRVAsmPrinter::outputEntryPoints() {
313 // Find all OpVariable IDs with required StorageClass.
314 DenseSet<Register> InterfaceIDs;
315 for (MachineInstr *MI : MAI->GlobalVarList) {
316 assert(MI->getOpcode() == SPIRV::OpVariable);
317 auto SC = static_cast<SPIRV::StorageClass::StorageClass>(
318 MI->getOperand(2).getImm());
319 // Before version 1.4, the interface's storage classes are limited to
320 // the Input and Output storage classes. Starting with version 1.4,
321 // the interface's storage classes are all storage classes used in
322 // declaring all global variables referenced by the entry point call tree.
323 if (ST->isAtLeastSPIRVVer(VersionTuple(1, 4)) ||
324 SC == SPIRV::StorageClass::Input || SC == SPIRV::StorageClass::Output) {
325 MachineFunction *MF = MI->getMF();
326 Register Reg = MAI->getRegisterAlias(MF, MI->getOperand(0).getReg());
327 InterfaceIDs.insert(Reg);
328 }
329 }
330
331 // Output OpEntryPoints adding interface args to all of them.
332 for (MachineInstr *MI : MAI->getMSInstrs(SPIRV::MB_EntryPoints)) {
333 SPIRVMCInstLower MCInstLowering;
334 MCInst TmpInst;
335 MCInstLowering.lower(MI, TmpInst, MAI);
336 for (Register Reg : InterfaceIDs) {
337 assert(Reg.isValid());
338 TmpInst.addOperand(MCOperand::createReg(Reg));
339 }
340 outputMCInst(TmpInst);
341 }
342}
343
344// Create global OpCapability instructions for the required capabilities.
345void SPIRVAsmPrinter::outputGlobalRequirements() {
346 // Abort here if not all requirements can be satisfied.
347 MAI->Reqs.checkSatisfiable(*ST);
348
349 for (const auto &Cap : MAI->Reqs.getMinimalCapabilities()) {
350 MCInst Inst;
351 Inst.setOpcode(SPIRV::OpCapability);
353 outputMCInst(Inst);
354 }
355
356 // Generate the final OpExtensions with strings instead of enums.
357 for (const auto &Ext : MAI->Reqs.getExtensions()) {
358 MCInst Inst;
359 Inst.setOpcode(SPIRV::OpExtension);
361 SPIRV::OperandCategory::ExtensionOperand, Ext),
362 Inst);
363 outputMCInst(Inst);
364 }
365 // TODO add a pseudo instr for version number.
366}
367
368void SPIRVAsmPrinter::outputExtFuncDecls() {
369 // Insert OpFunctionEnd after each declaration.
371 I = MAI->getMSInstrs(SPIRV::MB_ExtFuncDecls).begin(),
372 E = MAI->getMSInstrs(SPIRV::MB_ExtFuncDecls).end();
373 for (; I != E; ++I) {
374 outputInstruction(*I);
375 if ((I + 1) == E || (*(I + 1))->getOpcode() == SPIRV::OpFunction)
376 outputOpFunctionEnd();
377 }
378}
379
380// Encode LLVM type by SPIR-V execution mode VecTypeHint.
381static unsigned encodeVecTypeHint(Type *Ty) {
382 if (Ty->isHalfTy())
383 return 4;
384 if (Ty->isFloatTy())
385 return 5;
386 if (Ty->isDoubleTy())
387 return 6;
388 if (IntegerType *IntTy = dyn_cast<IntegerType>(Ty)) {
389 switch (IntTy->getIntegerBitWidth()) {
390 case 8:
391 return 0;
392 case 16:
393 return 1;
394 case 32:
395 return 2;
396 case 64:
397 return 3;
398 default:
399 llvm_unreachable("invalid integer type");
400 }
401 }
402 if (FixedVectorType *VecTy = dyn_cast<FixedVectorType>(Ty)) {
403 Type *EleTy = VecTy->getElementType();
404 unsigned Size = VecTy->getNumElements();
405 return Size << 16 | encodeVecTypeHint(EleTy);
406 }
407 llvm_unreachable("invalid type");
408}
409
410static void addOpsFromMDNode(MDNode *MDN, MCInst &Inst,
412 for (const MDOperand &MDOp : MDN->operands()) {
413 if (auto *CMeta = dyn_cast<ConstantAsMetadata>(MDOp)) {
414 Constant *C = CMeta->getValue();
415 if (ConstantInt *Const = dyn_cast<ConstantInt>(C)) {
416 Inst.addOperand(MCOperand::createImm(Const->getZExtValue()));
417 } else if (auto *CE = dyn_cast<Function>(C)) {
418 Register FuncReg = MAI->getFuncReg(CE);
419 assert(FuncReg.isValid());
420 Inst.addOperand(MCOperand::createReg(FuncReg));
421 }
422 }
423 }
424}
425
426void SPIRVAsmPrinter::outputExecutionModeFromMDNode(
427 Register Reg, MDNode *Node, SPIRV::ExecutionMode::ExecutionMode EM,
428 unsigned ExpectMDOps, int64_t DefVal) {
429 MCInst Inst;
430 Inst.setOpcode(SPIRV::OpExecutionMode);
432 Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(EM)));
433 addOpsFromMDNode(Node, Inst, MAI);
434 // reqd_work_group_size and work_group_size_hint require 3 operands,
435 // if metadata contains less operands, just add a default value
436 unsigned NodeSz = Node->getNumOperands();
437 if (ExpectMDOps > 0 && NodeSz < ExpectMDOps)
438 for (unsigned i = NodeSz; i < ExpectMDOps; ++i)
439 Inst.addOperand(MCOperand::createImm(DefVal));
440 outputMCInst(Inst);
441}
442
443void SPIRVAsmPrinter::outputExecutionModeFromNumthreadsAttribute(
444 const Register &Reg, const Attribute &Attr,
445 SPIRV::ExecutionMode::ExecutionMode EM) {
446 assert(Attr.isValid() && "Function called with an invalid attribute.");
447
448 MCInst Inst;
449 Inst.setOpcode(SPIRV::OpExecutionMode);
451 Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(EM)));
452
453 SmallVector<StringRef> NumThreads;
454 Attr.getValueAsString().split(NumThreads, ',');
455 assert(NumThreads.size() == 3 && "invalid numthreads");
456 for (uint32_t i = 0; i < 3; ++i) {
457 uint32_t V;
458 [[maybe_unused]] bool Result = NumThreads[i].getAsInteger(10, V);
459 assert(!Result && "Failed to parse numthreads");
461 }
462
463 outputMCInst(Inst);
464}
465
466void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
467 NamedMDNode *Node = M.getNamedMetadata("spirv.ExecutionMode");
468 if (Node) {
469 for (unsigned i = 0; i < Node->getNumOperands(); i++) {
470 MCInst Inst;
471 Inst.setOpcode(SPIRV::OpExecutionMode);
472 addOpsFromMDNode(cast<MDNode>(Node->getOperand(i)), Inst, MAI);
473 outputMCInst(Inst);
474 }
475 }
476 for (auto FI = M.begin(), E = M.end(); FI != E; ++FI) {
477 const Function &F = *FI;
478 // Only operands of OpEntryPoint instructions are allowed to be
479 // <Entry Point> operands of OpExecutionMode
480 if (F.isDeclaration() || !isEntryPoint(F))
481 continue;
482 Register FReg = MAI->getFuncReg(&F);
483 assert(FReg.isValid());
484 if (MDNode *Node = F.getMetadata("reqd_work_group_size"))
485 outputExecutionModeFromMDNode(FReg, Node, SPIRV::ExecutionMode::LocalSize,
486 3, 1);
487 if (Attribute Attr = F.getFnAttribute("hlsl.numthreads"); Attr.isValid())
488 outputExecutionModeFromNumthreadsAttribute(
489 FReg, Attr, SPIRV::ExecutionMode::LocalSize);
490 if (MDNode *Node = F.getMetadata("work_group_size_hint"))
491 outputExecutionModeFromMDNode(FReg, Node,
492 SPIRV::ExecutionMode::LocalSizeHint, 3, 1);
493 if (MDNode *Node = F.getMetadata("intel_reqd_sub_group_size"))
494 outputExecutionModeFromMDNode(FReg, Node,
495 SPIRV::ExecutionMode::SubgroupSize, 0, 0);
496 if (MDNode *Node = F.getMetadata("vec_type_hint")) {
497 MCInst Inst;
498 Inst.setOpcode(SPIRV::OpExecutionMode);
500 unsigned EM = static_cast<unsigned>(SPIRV::ExecutionMode::VecTypeHint);
502 unsigned TypeCode = encodeVecTypeHint(getMDOperandAsType(Node, 0));
503 Inst.addOperand(MCOperand::createImm(TypeCode));
504 outputMCInst(Inst);
505 }
506 if (ST->isOpenCLEnv() && !M.getNamedMetadata("spirv.ExecutionMode") &&
507 !M.getNamedMetadata("opencl.enable.FP_CONTRACT")) {
508 MCInst Inst;
509 Inst.setOpcode(SPIRV::OpExecutionMode);
511 unsigned EM = static_cast<unsigned>(SPIRV::ExecutionMode::ContractionOff);
513 outputMCInst(Inst);
514 }
515 }
516}
517
518void SPIRVAsmPrinter::outputAnnotations(const Module &M) {
519 outputModuleSection(SPIRV::MB_Annotations);
520 // Process llvm.global.annotations special global variable.
521 for (auto F = M.global_begin(), E = M.global_end(); F != E; ++F) {
522 if ((*F).getName() != "llvm.global.annotations")
523 continue;
524 const GlobalVariable *V = &(*F);
525 const ConstantArray *CA = cast<ConstantArray>(V->getOperand(0));
526 for (Value *Op : CA->operands()) {
527 ConstantStruct *CS = cast<ConstantStruct>(Op);
528 // The first field of the struct contains a pointer to
529 // the annotated variable.
530 Value *AnnotatedVar = CS->getOperand(0)->stripPointerCasts();
531 if (!isa<Function>(AnnotatedVar))
532 report_fatal_error("Unsupported value in llvm.global.annotations");
533 Function *Func = cast<Function>(AnnotatedVar);
534 Register Reg = MAI->getFuncReg(Func);
535 if (!Reg.isValid()) {
536 std::string DiagMsg;
537 raw_string_ostream OS(DiagMsg);
538 AnnotatedVar->print(OS);
539 DiagMsg = "Unknown function in llvm.global.annotations: " + DiagMsg;
540 report_fatal_error(DiagMsg.c_str());
541 }
542
543 // The second field contains a pointer to a global annotation string.
544 GlobalVariable *GV =
545 cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts());
546
547 StringRef AnnotationString;
548 getConstantStringInfo(GV, AnnotationString);
549 MCInst Inst;
550 Inst.setOpcode(SPIRV::OpDecorate);
552 unsigned Dec = static_cast<unsigned>(SPIRV::Decoration::UserSemantic);
554 addStringImm(AnnotationString, Inst);
555 outputMCInst(Inst);
556 }
557 }
558}
559
560void SPIRVAsmPrinter::outputModuleSections() {
561 const Module *M = MMI->getModule();
562 // Get the global subtarget to output module-level info.
563 ST = static_cast<const SPIRVTargetMachine &>(TM).getSubtargetImpl();
564 TII = ST->getInstrInfo();
566 assert(ST && TII && MAI && M && "Module analysis is required");
567 // Output instructions according to the Logical Layout of a Module:
568 // 1,2. All OpCapability instructions, then optional OpExtension instructions.
569 outputGlobalRequirements();
570 // 3. Optional OpExtInstImport instructions.
571 outputOpExtInstImports(*M);
572 // 4. The single required OpMemoryModel instruction.
573 outputOpMemoryModel();
574 // 5. All entry point declarations, using OpEntryPoint.
575 outputEntryPoints();
576 // 6. Execution-mode declarations, using OpExecutionMode or OpExecutionModeId.
577 outputExecutionMode(*M);
578 // 7a. Debug: all OpString, OpSourceExtension, OpSource, and
579 // OpSourceContinued, without forward references.
580 outputDebugSourceAndStrings(*M);
581 // 7b. Debug: all OpName and all OpMemberName.
582 outputModuleSection(SPIRV::MB_DebugNames);
583 // 7c. Debug: all OpModuleProcessed instructions.
584 outputModuleSection(SPIRV::MB_DebugModuleProcessed);
585 // 8. All annotation instructions (all decorations).
586 outputAnnotations(*M);
587 // 9. All type declarations (OpTypeXXX instructions), all constant
588 // instructions, and all global variable declarations. This section is
589 // the first section to allow use of: OpLine and OpNoLine debug information;
590 // non-semantic instructions with OpExtInst.
591 outputModuleSection(SPIRV::MB_TypeConstVars);
592 // 10. All function declarations (functions without a body).
593 outputExtFuncDecls();
594 // 11. All function definitions (functions with a body).
595 // This is done in regular function output.
596}
597
598bool SPIRVAsmPrinter::doInitialization(Module &M) {
599 ModuleSectionsEmitted = false;
600 // We need to call the parent's one explicitly.
602}
603
604// Force static initialization.
609}
MachineBasicBlock & MBB
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:131
This file defines the DenseMap class.
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void addOpsFromMDNode(MDNode *MDN, MCInst &Inst, SPIRV::ModuleAnalysisInfo *MAI)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVAsmPrinter()
static bool isFuncOrHeaderInstr(const MachineInstr *MI, const SPIRVInstrInfo *TII)
static unsigned encodeVecTypeHint(Type *Ty)
raw_pwrite_stream & OS
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:191
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
virtual void emitInstruction(const MachineInstr *)
Targets should implement this to emit instructions.
Definition: AsmPrinter.h:561
virtual void emitGlobalVariable(const GlobalVariable *GV)
Emit the specified global variable to the .s file.
Definition: AsmPrinter.cpp:698
virtual void emitBasicBlockEnd(const MachineBasicBlock &MBB)
Targets can override this to emit stuff at the end of a basic block.
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:92
virtual void emitFunctionBodyStart()
Targets can override this to emit stuff before the first basic block in the function.
Definition: AsmPrinter.h:545
virtual void emitEndOfAsmFile(Module &)
This virtual method can be overridden by targets that want to emit something at the end of their file...
Definition: AsmPrinter.h:541
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
Definition: AsmPrinter.cpp:434
void getAnalysisUsage(AnalysisUsage &AU) const override
Record analysis usage.
Definition: AsmPrinter.cpp:425
virtual void emitBasicBlockStart(const MachineBasicBlock &MBB)
Targets can override this to emit stuff at the start of a basic block.
virtual void emitFunctionBodyEnd()
Targets can override this to emit stuff after the last basic block in the function.
Definition: AsmPrinter.h:549
virtual void emitFunctionEntryLabel()
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:392
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:203
ConstantArray - Constant Array Declarations.
Definition: Constants.h:424
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
This is an important base class in LLVM.
Definition: Constant.h:42
This class represents an Operation in the Expression.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Class to represent fixed width SIMD vectors.
Definition: DerivedTypes.h:539
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:567
Class to represent integer types.
Definition: DerivedTypes.h:40
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
void setOpcode(unsigned Op)
Definition: MCInst.h:197
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
Metadata node.
Definition: Metadata.h:1069
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1428
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:891
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
MachineBasicBlock * getMBB() const
const BlockAddress * getBlockAddress() const
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
const ConstantFP * getFPImm() const
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
@ MO_FPImmediate
Floating-point immediate operand.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A tuple of MDNodes.
Definition: Metadata.h:1730
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isValid() const
Definition: Register.h:116
static const char * getRegisterName(MCRegister Reg)
void lower(const MachineInstr *MI, MCInst &OutMI, SPIRV::ModuleAnalysisInfo *MAI) const
size_t size() const
Definition: SmallVector.h:91
typename SuperClass::iterator iterator
Definition: SmallVector.h:590
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:685
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:153
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:142
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:156
op_range operands()
Definition: User.h:242
Value * getOperand(unsigned i) const
Definition: User.h:169
LLVM Value Representation.
Definition: Value.h:74
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:5023
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:694
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:29
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:71
std::optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:74
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
Reg
All possible values of the reg field in the ModR/M byte.
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Target & getTheSPIRV32Target()
bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
std::string getExtInstSetName(SPIRV::InstructionSet::InstructionSet Set)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
std::string getSymbolicOperandMnemonic(SPIRV::OperandCategory::OperandCategory Category, int32_t Value)
bool isEntryPoint(const Function &F)
Definition: SPIRVUtils.cpp:390
Target & getTheSPIRV64Target()
Target & getTheSPIRVLogicalTarget()
Type * getMDOperandAsType(const MDNode *N, unsigned I)
Definition: SPIRVUtils.cpp:285
void addStringImm(const StringRef &Str, MCInst &Inst)
Definition: SPIRVUtils.cpp:51
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...
static struct SPIRV::ModuleAnalysisInfo MAI
Register getFuncReg(const Function *F)