1 | //===-- AMDGPUAsmPrinter.h - Print AMDGPU assembly code ---------*- 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 | /// \file |
10 | /// AMDGPU Assembly printer class. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H |
15 | #define LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H |
16 | |
17 | #include "SIProgramInfo.h" |
18 | #include "llvm/CodeGen/AsmPrinter.h" |
19 | |
20 | struct amd_kernel_code_t; |
21 | |
22 | namespace llvm { |
23 | |
24 | class AMDGPUMachineFunction; |
25 | struct AMDGPUResourceUsageAnalysis; |
26 | class AMDGPUTargetStreamer; |
27 | class MCCodeEmitter; |
28 | class MCOperand; |
29 | |
30 | namespace AMDGPU { |
31 | struct MCKernelDescriptor; |
32 | namespace HSAMD { |
33 | class MetadataStreamer; |
34 | } |
35 | } // namespace AMDGPU |
36 | |
37 | class AMDGPUAsmPrinter final : public AsmPrinter { |
38 | private: |
39 | unsigned CodeObjectVersion; |
40 | void initializeTargetID(const Module &M); |
41 | |
42 | AMDGPUResourceUsageAnalysis *ResourceUsage; |
43 | |
44 | SIProgramInfo CurrentProgramInfo; |
45 | |
46 | std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream; |
47 | |
48 | MCCodeEmitter *DumpCodeInstEmitter = nullptr; |
49 | |
50 | uint64_t getFunctionCodeSize(const MachineFunction &MF) const; |
51 | |
52 | void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF); |
53 | void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo, |
54 | const MachineFunction &MF) const; |
55 | |
56 | /// Emit register usage information so that the GPU driver |
57 | /// can correctly setup the GPU state. |
58 | void EmitProgramInfoSI(const MachineFunction &MF, |
59 | const SIProgramInfo &KernelInfo); |
60 | void EmitPALMetadata(const MachineFunction &MF, |
61 | const SIProgramInfo &KernelInfo); |
62 | void emitPALFunctionMetadata(const MachineFunction &MF); |
63 | void (uint32_t NumVGPR, |
64 | std::optional<uint32_t> NumAGPR, |
65 | uint32_t TotalNumVGPR, uint32_t NumSGPR, |
66 | uint64_t ScratchSize, uint64_t CodeSize, |
67 | const AMDGPUMachineFunction *MFI); |
68 | void (const MachineFunction &MF, |
69 | const SIProgramInfo &CurrentProgramInfo, |
70 | bool isModuleEntryFunction, bool hasMAIInsts); |
71 | |
72 | uint16_t getAmdhsaKernelCodeProperties( |
73 | const MachineFunction &MF) const; |
74 | |
75 | AMDGPU::MCKernelDescriptor |
76 | getAmdhsaKernelDescriptor(const MachineFunction &MF, |
77 | const SIProgramInfo &PI) const; |
78 | |
79 | void initTargetStreamer(Module &M); |
80 | |
81 | public: |
82 | explicit AMDGPUAsmPrinter(TargetMachine &TM, |
83 | std::unique_ptr<MCStreamer> Streamer); |
84 | |
85 | StringRef getPassName() const override; |
86 | |
87 | const MCSubtargetInfo* getGlobalSTI() const; |
88 | |
89 | AMDGPUTargetStreamer* getTargetStreamer() const; |
90 | |
91 | bool doInitialization(Module &M) override; |
92 | bool doFinalization(Module &M) override; |
93 | bool runOnMachineFunction(MachineFunction &MF) override; |
94 | |
95 | /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated |
96 | /// pseudo lowering. |
97 | bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const; |
98 | |
99 | /// Lower the specified LLVM Constant to an MCExpr. |
100 | /// The AsmPrinter::lowerConstantof does not know how to lower |
101 | /// addrspacecast, therefore they should be lowered by this function. |
102 | const MCExpr *lowerConstant(const Constant *CV) override; |
103 | |
104 | /// tblgen'erated driver function for lowering simple MI->MC pseudo |
105 | /// instructions. |
106 | bool emitPseudoExpansionLowering(MCStreamer &OutStreamer, |
107 | const MachineInstr *MI); |
108 | |
109 | /// Implemented in AMDGPUMCInstLower.cpp |
110 | void emitInstruction(const MachineInstr *MI) override; |
111 | |
112 | void emitFunctionBodyStart() override; |
113 | |
114 | void emitFunctionBodyEnd() override; |
115 | |
116 | void emitImplicitDef(const MachineInstr *MI) const override; |
117 | |
118 | void emitFunctionEntryLabel() override; |
119 | |
120 | void emitBasicBlockStart(const MachineBasicBlock &MBB) override; |
121 | |
122 | void emitGlobalVariable(const GlobalVariable *GV) override; |
123 | |
124 | void emitStartOfAsmFile(Module &M) override; |
125 | |
126 | void emitEndOfAsmFile(Module &M) override; |
127 | |
128 | bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
129 | const char *, raw_ostream &O) override; |
130 | |
131 | protected: |
132 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
133 | |
134 | std::vector<std::string> DisasmLines, HexLines; |
135 | size_t DisasmLineMaxLen; |
136 | bool IsTargetStreamerInitialized; |
137 | }; |
138 | |
139 | } // end namespace llvm |
140 | |
141 | #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H |
142 | |