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
20namespace llvm {
21
22class AMDGPUMachineFunction;
23struct AMDGPUResourceUsageAnalysis;
24class AMDGPUTargetStreamer;
25class MCCodeEmitter;
26class MCOperand;
27
28namespace AMDGPU {
29struct MCKernelDescriptor;
30struct AMDGPUMCKernelCodeT;
31namespace HSAMD {
32class MetadataStreamer;
33}
34} // namespace AMDGPU
35
36class AMDGPUAsmPrinter final : public AsmPrinter {
37private:
38 unsigned CodeObjectVersion;
39 void initializeTargetID(const Module &M);
40
41 AMDGPUResourceUsageAnalysis *ResourceUsage;
42
43 SIProgramInfo CurrentProgramInfo;
44
45 std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream;
46
47 MCCodeEmitter *DumpCodeInstEmitter = nullptr;
48
49 uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
50
51 void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
52 void getAmdKernelCode(AMDGPU::AMDGPUMCKernelCodeT &Out,
53 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 emitCommonFunctionComments(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 emitCommonFunctionComments(const MCExpr *NumVGPR, const MCExpr *NumAGPR,
69 const MCExpr *TotalNumVGPR,
70 const MCExpr *NumSGPR,
71 const MCExpr *ScratchSize, uint64_t CodeSize,
72 const AMDGPUMachineFunction *MFI);
73 void emitResourceUsageRemarks(const MachineFunction &MF,
74 const SIProgramInfo &CurrentProgramInfo,
75 bool isModuleEntryFunction, bool hasMAIInsts);
76
77 const MCExpr *getAmdhsaKernelCodeProperties(const MachineFunction &MF) const;
78
79 AMDGPU::MCKernelDescriptor
80 getAmdhsaKernelDescriptor(const MachineFunction &MF,
81 const SIProgramInfo &PI) const;
82
83 void initTargetStreamer(Module &M);
84
85 SmallString<128> getMCExprStr(const MCExpr *Value);
86
87public:
88 explicit AMDGPUAsmPrinter(TargetMachine &TM,
89 std::unique_ptr<MCStreamer> Streamer);
90
91 StringRef getPassName() const override;
92
93 const MCSubtargetInfo* getGlobalSTI() const;
94
95 AMDGPUTargetStreamer* getTargetStreamer() const;
96
97 bool doInitialization(Module &M) override;
98 bool doFinalization(Module &M) override;
99 bool runOnMachineFunction(MachineFunction &MF) override;
100
101 /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
102 /// pseudo lowering.
103 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
104
105 /// Lower the specified LLVM Constant to an MCExpr.
106 /// The AsmPrinter::lowerConstantof does not know how to lower
107 /// addrspacecast, therefore they should be lowered by this function.
108 const MCExpr *lowerConstant(const Constant *CV) override;
109
110 /// tblgen'erated driver function for lowering simple MI->MC pseudo
111 /// instructions.
112 bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
113 const MachineInstr *MI);
114
115 /// Implemented in AMDGPUMCInstLower.cpp
116 void emitInstruction(const MachineInstr *MI) override;
117
118 void emitFunctionBodyStart() override;
119
120 void emitFunctionBodyEnd() override;
121
122 void emitImplicitDef(const MachineInstr *MI) const override;
123
124 void emitFunctionEntryLabel() override;
125
126 void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
127
128 void emitGlobalVariable(const GlobalVariable *GV) override;
129
130 void emitStartOfAsmFile(Module &M) override;
131
132 void emitEndOfAsmFile(Module &M) override;
133
134 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
135 const char *ExtraCode, raw_ostream &O) override;
136
137protected:
138 void getAnalysisUsage(AnalysisUsage &AU) const override;
139
140 std::vector<std::string> DisasmLines, HexLines;
141 size_t DisasmLineMaxLen;
142 bool IsTargetStreamerInitialized;
143};
144
145} // end namespace llvm
146
147#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
148