LLVM 19.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"
35#include "llvm/MC/MCStreamer.h"
36#include "llvm/MC/MCSymbol.h"
39
40using namespace llvm;
41
42#define DEBUG_TYPE "asm-printer"
43
44namespace {
45class SPIRVAsmPrinter : public AsmPrinter {
46 unsigned NLabels = 0;
47
48public:
49 explicit SPIRVAsmPrinter(TargetMachine &TM,
50 std::unique_ptr<MCStreamer> Streamer)
51 : AsmPrinter(TM, std::move(Streamer)), ST(nullptr), TII(nullptr) {}
52 bool ModuleSectionsEmitted;
53 const SPIRVSubtarget *ST;
54 const SPIRVInstrInfo *TII;
55
56 StringRef getPassName() const override { return "SPIRV Assembly Printer"; }
57 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
58 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
59 const char *ExtraCode, raw_ostream &O) override;
60
61 void outputMCInst(MCInst &Inst);
62 void outputInstruction(const MachineInstr *MI);
63 void outputModuleSection(SPIRV::ModuleSectionType MSType);
64 void outputGlobalRequirements();
65 void outputEntryPoints();
66 void outputDebugSourceAndStrings(const Module &M);
67 void outputOpExtInstImports(const Module &M);
68 void outputOpMemoryModel();
69 void outputOpFunctionEnd();
70 void outputExtFuncDecls();
71 void outputExecutionModeFromMDNode(Register Reg, MDNode *Node,
72 SPIRV::ExecutionMode::ExecutionMode EM,
73 unsigned ExpectMDOps, int64_t DefVal);
74 void outputExecutionModeFromNumthreadsAttribute(
75 const Register &Reg, const Attribute &Attr,
76 SPIRV::ExecutionMode::ExecutionMode EM);
77 void outputExecutionMode(const Module &M);
78 void outputAnnotations(const Module &M);
79 void outputModuleSections();
80
81 void emitInstruction(const MachineInstr *MI) override;
82 void emitFunctionEntryLabel() override {}
83 void emitFunctionHeader() override;
84 void emitFunctionBodyStart() override {}
85 void emitFunctionBodyEnd() override;
86 void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
87 void emitBasicBlockEnd(const MachineBasicBlock &MBB) override {}
88 void emitGlobalVariable(const GlobalVariable *GV) override {}
89 void emitOpLabel(const MachineBasicBlock &MBB);
90 void emitEndOfAsmFile(Module &M) override;
91 bool doInitialization(Module &M) override;
92
93 void getAnalysisUsage(AnalysisUsage &AU) const override;
95};
96} // namespace
97
98void SPIRVAsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
102}
103
104// If the module has no functions, we need output global info anyway.
105void SPIRVAsmPrinter::emitEndOfAsmFile(Module &M) {
106 if (ModuleSectionsEmitted == false) {
107 outputModuleSections();
108 ModuleSectionsEmitted = true;
109 }
110
111 ST = static_cast<const SPIRVTargetMachine &>(TM).getSubtargetImpl();
112 VersionTuple SPIRVVersion = ST->getSPIRVVersion();
113 uint32_t Major = SPIRVVersion.getMajor();
114 uint32_t Minor = SPIRVVersion.getMinor().value_or(0);
115 // Bound is an approximation that accounts for the maximum used register
116 // number and number of generated OpLabels
117 unsigned Bound = 2 * (ST->getBound() + 1) + NLabels;
118 if (MCAssembler *Asm = OutStreamer->getAssemblerPtr())
119 Asm->setBuildVersion(static_cast<MachO::PlatformType>(0), Major, Minor,
120 Bound, VersionTuple(Major, Minor, 0, Bound));
121}
122
123void SPIRVAsmPrinter::emitFunctionHeader() {
124 if (ModuleSectionsEmitted == false) {
125 outputModuleSections();
126 ModuleSectionsEmitted = true;
127 }
128 // Get the subtarget from the current MachineFunction.
129 ST = &MF->getSubtarget<SPIRVSubtarget>();
130 TII = ST->getInstrInfo();
131 const Function &F = MF->getFunction();
132
133 if (isVerbose()) {
134 OutStreamer->getCommentOS()
135 << "-- Begin function "
136 << GlobalValue::dropLLVMManglingEscape(F.getName()) << '\n';
137 }
138
139 auto Section = getObjFileLowering().SectionForGlobal(&F, TM);
140 MF->setSection(Section);
141}
142
143void SPIRVAsmPrinter::outputOpFunctionEnd() {
144 MCInst FunctionEndInst;
145 FunctionEndInst.setOpcode(SPIRV::OpFunctionEnd);
146 outputMCInst(FunctionEndInst);
147}
148
149// Emit OpFunctionEnd at the end of MF and clear BBNumToRegMap.
150void SPIRVAsmPrinter::emitFunctionBodyEnd() {
151 outputOpFunctionEnd();
152 MAI->BBNumToRegMap.clear();
153}
154
155void SPIRVAsmPrinter::emitOpLabel(const MachineBasicBlock &MBB) {
156 MCInst LabelInst;
157 LabelInst.setOpcode(SPIRV::OpLabel);
158 LabelInst.addOperand(MCOperand::createReg(MAI->getOrCreateMBBRegister(MBB)));
159 outputMCInst(LabelInst);
160 ++NLabels;
161}
162
163void SPIRVAsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
164 assert(!MBB.empty() && "MBB is empty!");
165
166 // If it's the first MBB in MF, it has OpFunction and OpFunctionParameter, so
167 // OpLabel should be output after them.
168 if (MBB.getNumber() == MF->front().getNumber()) {
169 for (const MachineInstr &MI : MBB)
170 if (MI.getOpcode() == SPIRV::OpFunction)
171 return;
172 // TODO: this case should be checked by the verifier.
173 report_fatal_error("OpFunction is expected in the front MBB of MF");
174 }
175 emitOpLabel(MBB);
176}
177
178void SPIRVAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
179 raw_ostream &O) {
180 const MachineOperand &MO = MI->getOperand(OpNum);
181
182 switch (MO.getType()) {
185 break;
186
188 O << MO.getImm();
189 break;
190
192 O << MO.getFPImm();
193 break;
194
196 O << *MO.getMBB()->getSymbol();
197 break;
198
200 O << *getSymbol(MO.getGlobal());
201 break;
202
204 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
205 O << BA->getName();
206 break;
207 }
208
210 O << *GetExternalSymbolSymbol(MO.getSymbolName());
211 break;
212
215 default:
216 llvm_unreachable("<unknown operand type>");
217 }
218}
219
220bool SPIRVAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
221 const char *ExtraCode, raw_ostream &O) {
222 if (ExtraCode && ExtraCode[0])
223 return true; // Invalid instruction - SPIR-V does not have special modifiers
224
225 printOperand(MI, OpNo, O);
226 return false;
227}
228
230 const SPIRVInstrInfo *TII) {
231 return TII->isHeaderInstr(*MI) || MI->getOpcode() == SPIRV::OpFunction ||
232 MI->getOpcode() == SPIRV::OpFunctionParameter;
233}
234
235void SPIRVAsmPrinter::outputMCInst(MCInst &Inst) {
236 OutStreamer->emitInstruction(Inst, *OutContext.getSubtargetInfo());
237}
238
239void SPIRVAsmPrinter::outputInstruction(const MachineInstr *MI) {
240 SPIRVMCInstLower MCInstLowering;
241 MCInst TmpInst;
242 MCInstLowering.lower(MI, TmpInst, MAI);
243 outputMCInst(TmpInst);
244}
245
246void SPIRVAsmPrinter::emitInstruction(const MachineInstr *MI) {
247 SPIRV_MC::verifyInstructionPredicates(MI->getOpcode(),
248 getSubtargetInfo().getFeatureBits());
249
250 if (!MAI->getSkipEmission(MI))
251 outputInstruction(MI);
252
253 // Output OpLabel after OpFunction and OpFunctionParameter in the first MBB.
254 const MachineInstr *NextMI = MI->getNextNode();
255 if (!MAI->hasMBBRegister(*MI->getParent()) && isFuncOrHeaderInstr(MI, TII) &&
256 (!NextMI || !isFuncOrHeaderInstr(NextMI, TII))) {
257 assert(MI->getParent()->getNumber() == MF->front().getNumber() &&
258 "OpFunction is not in the front MBB of MF");
259 emitOpLabel(*MI->getParent());
260 }
261}
262
263void SPIRVAsmPrinter::outputModuleSection(SPIRV::ModuleSectionType MSType) {
264 for (MachineInstr *MI : MAI->getMSInstrs(MSType))
265 outputInstruction(MI);
266}
267
268void SPIRVAsmPrinter::outputDebugSourceAndStrings(const Module &M) {
269 // Output OpSourceExtensions.
270 for (auto &Str : MAI->SrcExt) {
271 MCInst Inst;
272 Inst.setOpcode(SPIRV::OpSourceExtension);
273 addStringImm(Str.first(), Inst);
274 outputMCInst(Inst);
275 }
276 // Output OpSource.
277 MCInst Inst;
278 Inst.setOpcode(SPIRV::OpSource);
279 Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(MAI->SrcLang)));
280 Inst.addOperand(
281 MCOperand::createImm(static_cast<unsigned>(MAI->SrcLangVersion)));
282 outputMCInst(Inst);
283}
284
285void SPIRVAsmPrinter::outputOpExtInstImports(const Module &M) {
286 for (auto &CU : MAI->ExtInstSetMap) {
287 unsigned Set = CU.first;
288 Register Reg = CU.second;
289 MCInst Inst;
290 Inst.setOpcode(SPIRV::OpExtInstImport);
293 static_cast<SPIRV::InstructionSet::InstructionSet>(Set)),
294 Inst);
295 outputMCInst(Inst);
296 }
297}
298
299void SPIRVAsmPrinter::outputOpMemoryModel() {
300 MCInst Inst;
301 Inst.setOpcode(SPIRV::OpMemoryModel);
302 Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(MAI->Addr)));
303 Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(MAI->Mem)));
304 outputMCInst(Inst);
305}
306
307// Before the OpEntryPoints' output, we need to add the entry point's
308// interfaces. The interface is a list of IDs of global OpVariable instructions.
309// These declare the set of global variables from a module that form
310// the interface of this entry point.
311void SPIRVAsmPrinter::outputEntryPoints() {
312 // Find all OpVariable IDs with required StorageClass.
313 DenseSet<Register> InterfaceIDs;
314 for (MachineInstr *MI : MAI->GlobalVarList) {
315 assert(MI->getOpcode() == SPIRV::OpVariable);
316 auto SC = static_cast<SPIRV::StorageClass::StorageClass>(
317 MI->getOperand(2).getImm());
318 // Before version 1.4, the interface's storage classes are limited to
319 // the Input and Output storage classes. Starting with version 1.4,
320 // the interface's storage classes are all storage classes used in
321 // declaring all global variables referenced by the entry point call tree.
322 if (ST->isAtLeastSPIRVVer(VersionTuple(1, 4)) ||
323 SC == SPIRV::StorageClass::Input || SC == SPIRV::StorageClass::Output) {
324 MachineFunction *MF = MI->getMF();
325 Register Reg = MAI->getRegisterAlias(MF, MI->getOperand(0).getReg());
326 InterfaceIDs.insert(Reg);
327 }
328 }
329
330 // Output OpEntryPoints adding interface args to all of them.
331 for (MachineInstr *MI : MAI->getMSInstrs(SPIRV::MB_EntryPoints)) {
332 SPIRVMCInstLower MCInstLowering;
333 MCInst TmpInst;
334 MCInstLowering.lower(MI, TmpInst, MAI);
335 for (Register Reg : InterfaceIDs) {
336 assert(Reg.isValid());
337 TmpInst.addOperand(MCOperand::createReg(Reg));
338 }
339 outputMCInst(TmpInst);
340 }
341}
342
343// Create global OpCapability instructions for the required capabilities.
344void SPIRVAsmPrinter::outputGlobalRequirements() {
345 // Abort here if not all requirements can be satisfied.
346 MAI->Reqs.checkSatisfiable(*ST);
347
348 for (const auto &Cap : MAI->Reqs.getMinimalCapabilities()) {
349 MCInst Inst;
350 Inst.setOpcode(SPIRV::OpCapability);
352 outputMCInst(Inst);
353 }
354
355 // Generate the final OpExtensions with strings instead of enums.
356 for (const auto &Ext : MAI->Reqs.getExtensions()) {
357 MCInst Inst;
358 Inst.setOpcode(SPIRV::OpExtension);
360 SPIRV::OperandCategory::ExtensionOperand, Ext),
361 Inst);
362 outputMCInst(Inst);
363 }
364 // TODO add a pseudo instr for version number.
365}
366
367void SPIRVAsmPrinter::outputExtFuncDecls() {
368 // Insert OpFunctionEnd after each declaration.
370 I = MAI->getMSInstrs(SPIRV::MB_ExtFuncDecls).begin(),
371 E = MAI->getMSInstrs(SPIRV::MB_ExtFuncDecls).end();
372 for (; I != E; ++I) {
373 outputInstruction(*I);
374 if ((I + 1) == E || (*(I + 1))->getOpcode() == SPIRV::OpFunction)
375 outputOpFunctionEnd();
376 }
377}
378
379// Encode LLVM type by SPIR-V execution mode VecTypeHint.
380static unsigned encodeVecTypeHint(Type *Ty) {
381 if (Ty->isHalfTy())
382 return 4;
383 if (Ty->isFloatTy())
384 return 5;
385 if (Ty->isDoubleTy())
386 return 6;
387 if (IntegerType *IntTy = dyn_cast<IntegerType>(Ty)) {
388 switch (IntTy->getIntegerBitWidth()) {
389 case 8:
390 return 0;
391 case 16:
392 return 1;
393 case 32:
394 return 2;
395 case 64:
396 return 3;
397 default:
398 llvm_unreachable("invalid integer type");
399 }
400 }
401 if (FixedVectorType *VecTy = dyn_cast<FixedVectorType>(Ty)) {
402 Type *EleTy = VecTy->getElementType();
403 unsigned Size = VecTy->getNumElements();
404 return Size << 16 | encodeVecTypeHint(EleTy);
405 }
406 llvm_unreachable("invalid type");
407}
408
409static void addOpsFromMDNode(MDNode *MDN, MCInst &Inst,
411 for (const MDOperand &MDOp : MDN->operands()) {
412 if (auto *CMeta = dyn_cast<ConstantAsMetadata>(MDOp)) {
413 Constant *C = CMeta->getValue();
414 if (ConstantInt *Const = dyn_cast<ConstantInt>(C)) {
415 Inst.addOperand(MCOperand::createImm(Const->getZExtValue()));
416 } else if (auto *CE = dyn_cast<Function>(C)) {
417 Register FuncReg = MAI->getFuncReg(CE);
418 assert(FuncReg.isValid());
419 Inst.addOperand(MCOperand::createReg(FuncReg));
420 }
421 }
422 }
423}
424
425void SPIRVAsmPrinter::outputExecutionModeFromMDNode(
426 Register Reg, MDNode *Node, SPIRV::ExecutionMode::ExecutionMode EM,
427 unsigned ExpectMDOps, int64_t DefVal) {
428 MCInst Inst;
429 Inst.setOpcode(SPIRV::OpExecutionMode);
431 Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(EM)));
432 addOpsFromMDNode(Node, Inst, MAI);
433 // reqd_work_group_size and work_group_size_hint require 3 operands,
434 // if metadata contains less operands, just add a default value
435 unsigned NodeSz = Node->getNumOperands();
436 if (ExpectMDOps > 0 && NodeSz < ExpectMDOps)
437 for (unsigned i = NodeSz; i < ExpectMDOps; ++i)
438 Inst.addOperand(MCOperand::createImm(DefVal));
439 outputMCInst(Inst);
440}
441
442void SPIRVAsmPrinter::outputExecutionModeFromNumthreadsAttribute(
443 const Register &Reg, const Attribute &Attr,
444 SPIRV::ExecutionMode::ExecutionMode EM) {
445 assert(Attr.isValid() && "Function called with an invalid attribute.");
446
447 MCInst Inst;
448 Inst.setOpcode(SPIRV::OpExecutionMode);
450 Inst.addOperand(MCOperand::createImm(static_cast<unsigned>(EM)));
451
452 SmallVector<StringRef> NumThreads;
453 Attr.getValueAsString().split(NumThreads, ',');
454 assert(NumThreads.size() == 3 && "invalid numthreads");
455 for (uint32_t i = 0; i < 3; ++i) {
456 uint32_t V;
457 [[maybe_unused]] bool Result = NumThreads[i].getAsInteger(10, V);
458 assert(!Result && "Failed to parse numthreads");
460 }
461
462 outputMCInst(Inst);
463}
464
465void SPIRVAsmPrinter::outputExecutionMode(const Module &M) {
466 NamedMDNode *Node = M.getNamedMetadata("spirv.ExecutionMode");
467 if (Node) {
468 for (unsigned i = 0; i < Node->getNumOperands(); i++) {
469 MCInst Inst;
470 Inst.setOpcode(SPIRV::OpExecutionMode);
471 addOpsFromMDNode(cast<MDNode>(Node->getOperand(i)), Inst, MAI);
472 outputMCInst(Inst);
473 }
474 }
475 for (auto FI = M.begin(), E = M.end(); FI != E; ++FI) {
476 const Function &F = *FI;
477 // Only operands of OpEntryPoint instructions are allowed to be
478 // <Entry Point> operands of OpExecutionMode
479 if (F.isDeclaration() || !isEntryPoint(F))
480 continue;
481 Register FReg = MAI->getFuncReg(&F);
482 assert(FReg.isValid());
483 if (MDNode *Node = F.getMetadata("reqd_work_group_size"))
484 outputExecutionModeFromMDNode(FReg, Node, SPIRV::ExecutionMode::LocalSize,
485 3, 1);
486 if (Attribute Attr = F.getFnAttribute("hlsl.numthreads"); Attr.isValid())
487 outputExecutionModeFromNumthreadsAttribute(
488 FReg, Attr, SPIRV::ExecutionMode::LocalSize);
489 if (MDNode *Node = F.getMetadata("work_group_size_hint"))
490 outputExecutionModeFromMDNode(FReg, Node,
491 SPIRV::ExecutionMode::LocalSizeHint, 3, 1);
492 if (MDNode *Node = F.getMetadata("intel_reqd_sub_group_size"))
493 outputExecutionModeFromMDNode(FReg, Node,
494 SPIRV::ExecutionMode::SubgroupSize, 0, 0);
495 if (MDNode *Node = F.getMetadata("vec_type_hint")) {
496 MCInst Inst;
497 Inst.setOpcode(SPIRV::OpExecutionMode);
499 unsigned EM = static_cast<unsigned>(SPIRV::ExecutionMode::VecTypeHint);
501 unsigned TypeCode = encodeVecTypeHint(getMDOperandAsType(Node, 0));
502 Inst.addOperand(MCOperand::createImm(TypeCode));
503 outputMCInst(Inst);
504 }
505 if (ST->isOpenCLEnv() && !M.getNamedMetadata("spirv.ExecutionMode") &&
506 !M.getNamedMetadata("opencl.enable.FP_CONTRACT")) {
507 MCInst Inst;
508 Inst.setOpcode(SPIRV::OpExecutionMode);
510 unsigned EM = static_cast<unsigned>(SPIRV::ExecutionMode::ContractionOff);
512 outputMCInst(Inst);
513 }
514 }
515}
516
517void SPIRVAsmPrinter::outputAnnotations(const Module &M) {
518 outputModuleSection(SPIRV::MB_Annotations);
519 // Process llvm.global.annotations special global variable.
520 for (auto F = M.global_begin(), E = M.global_end(); F != E; ++F) {
521 if ((*F).getName() != "llvm.global.annotations")
522 continue;
523 const GlobalVariable *V = &(*F);
524 const ConstantArray *CA = cast<ConstantArray>(V->getOperand(0));
525 for (Value *Op : CA->operands()) {
526 ConstantStruct *CS = cast<ConstantStruct>(Op);
527 // The first field of the struct contains a pointer to
528 // the annotated variable.
529 Value *AnnotatedVar = CS->getOperand(0)->stripPointerCasts();
530 if (!isa<Function>(AnnotatedVar))
531 report_fatal_error("Unsupported value in llvm.global.annotations");
532 Function *Func = cast<Function>(AnnotatedVar);
533 Register Reg = MAI->getFuncReg(Func);
534 if (!Reg.isValid()) {
535 std::string DiagMsg;
536 raw_string_ostream OS(DiagMsg);
537 AnnotatedVar->print(OS);
538 DiagMsg = "Unknown function in llvm.global.annotations: " + DiagMsg;
539 report_fatal_error(DiagMsg.c_str());
540 }
541
542 // The second field contains a pointer to a global annotation string.
543 GlobalVariable *GV =
544 cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts());
545
546 StringRef AnnotationString;
547 getConstantStringInfo(GV, AnnotationString);
548 MCInst Inst;
549 Inst.setOpcode(SPIRV::OpDecorate);
551 unsigned Dec = static_cast<unsigned>(SPIRV::Decoration::UserSemantic);
553 addStringImm(AnnotationString, Inst);
554 outputMCInst(Inst);
555 }
556 }
557}
558
559void SPIRVAsmPrinter::outputModuleSections() {
560 const Module *M = MMI->getModule();
561 // Get the global subtarget to output module-level info.
562 ST = static_cast<const SPIRVTargetMachine &>(TM).getSubtargetImpl();
563 TII = ST->getInstrInfo();
565 assert(ST && TII && MAI && M && "Module analysis is required");
566 // Output instructions according to the Logical Layout of a Module:
567 // 1,2. All OpCapability instructions, then optional OpExtension instructions.
568 outputGlobalRequirements();
569 // 3. Optional OpExtInstImport instructions.
570 outputOpExtInstImports(*M);
571 // 4. The single required OpMemoryModel instruction.
572 outputOpMemoryModel();
573 // 5. All entry point declarations, using OpEntryPoint.
574 outputEntryPoints();
575 // 6. Execution-mode declarations, using OpExecutionMode or OpExecutionModeId.
576 outputExecutionMode(*M);
577 // 7a. Debug: all OpString, OpSourceExtension, OpSource, and
578 // OpSourceContinued, without forward references.
579 outputDebugSourceAndStrings(*M);
580 // 7b. Debug: all OpName and all OpMemberName.
581 outputModuleSection(SPIRV::MB_DebugNames);
582 // 7c. Debug: all OpModuleProcessed instructions.
583 outputModuleSection(SPIRV::MB_DebugModuleProcessed);
584 // 8. All annotation instructions (all decorations).
585 outputAnnotations(*M);
586 // 9. All type declarations (OpTypeXXX instructions), all constant
587 // instructions, and all global variable declarations. This section is
588 // the first section to allow use of: OpLine and OpNoLine debug information;
589 // non-semantic instructions with OpExtInst.
590 outputModuleSection(SPIRV::MB_TypeConstVars);
591 // 10. All function declarations (functions without a body).
592 outputExtFuncDecls();
593 // 11. All function definitions (functions with a body).
594 // This is done in regular function output.
595}
596
597bool SPIRVAsmPrinter::doInitialization(Module &M) {
598 ModuleSectionsEmitted = false;
599 // We need to call the parent's one explicitly.
601}
602
603// Force static initialization.
608}
MachineBasicBlock & MBB
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:135
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")
const char LLVMTargetMachineRef TM
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:555
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:539
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:535
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:543
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:391
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:1067
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1426
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:889
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:1729
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:154
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:143
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:157
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:5022
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
PlatformType
Definition: MachO.h:500
@ 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)