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 "AMDGPUMCResourceInfo.h"
18#include "SIProgramInfo.h"
19#include "llvm/CodeGen/AsmPrinter.h"
20
21namespace llvm {
22
23class AMDGPUMachineFunction;
24struct AMDGPUResourceUsageAnalysis;
25class AMDGPUTargetStreamer;
26class MCCodeEmitter;
27class MCOperand;
28class MCResourceInfo;
29
30namespace AMDGPU {
31struct MCKernelDescriptor;
32struct AMDGPUMCKernelCodeT;
33namespace HSAMD {
34class MetadataStreamer;
35}
36} // namespace AMDGPU
37
38class AMDGPUAsmPrinter final : public AsmPrinter {
39public:
40 static char ID;
41
42private:
43 unsigned CodeObjectVersion;
44 void initializeTargetID(const Module &M);
45
46 AMDGPUResourceUsageAnalysis *ResourceUsage;
47
48 MCResourceInfo RI;
49
50 SIProgramInfo CurrentProgramInfo;
51
52 std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream;
53
54 MCCodeEmitter *DumpCodeInstEmitter = nullptr;
55
56 void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
57 void getAmdKernelCode(AMDGPU::AMDGPUMCKernelCodeT &Out,
58 const SIProgramInfo &KernelInfo,
59 const MachineFunction &MF) const;
60
61 /// Emit register usage information so that the GPU driver
62 /// can correctly setup the GPU state.
63 void EmitProgramInfoSI(const MachineFunction &MF,
64 const SIProgramInfo &KernelInfo);
65 void EmitPALMetadata(const MachineFunction &MF,
66 const SIProgramInfo &KernelInfo);
67 void emitPALFunctionMetadata(const MachineFunction &MF);
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
87 /// Attempts to replace the validation that is missed in getSIProgramInfo due
88 /// to MCExpr being unknown. Invoked during doFinalization such that the
89 /// MCResourceInfo symbols are known.
90 void validateMCResourceInfo(Function &F);
91
92public:
93 explicit AMDGPUAsmPrinter(TargetMachine &TM,
94 std::unique_ptr<MCStreamer> Streamer);
95
96 StringRef getPassName() const override;
97
98 const MCSubtargetInfo* getGlobalSTI() const;
99
100 AMDGPUTargetStreamer* getTargetStreamer() const;
101
102 bool doInitialization(Module &M) override;
103 bool doFinalization(Module &M) override;
104 bool runOnMachineFunction(MachineFunction &MF) override;
105
106 /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
107 /// pseudo lowering.
108 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
109
110 /// Lower the specified LLVM Constant to an MCExpr.
111 /// The AsmPrinter::lowerConstantof does not know how to lower
112 /// addrspacecast, therefore they should be lowered by this function.
113 const MCExpr *lowerConstant(const Constant *CV, const Constant *BaseCV,
114 uint64_t Offset) override;
115
116 /// tblgen'erated driver function for lowering simple MI->MC pseudo
117 /// instructions.
118 bool lowerPseudoInstExpansion(const MachineInstr *MI, MCInst &Inst);
119
120 /// Implemented in AMDGPUMCInstLower.cpp
121 void emitInstruction(const MachineInstr *MI) override;
122
123 void emitFunctionBodyStart() override;
124
125 void emitFunctionBodyEnd() override;
126
127 void emitImplicitDef(const MachineInstr *MI) const override;
128
129 void emitFunctionEntryLabel() override;
130
131 void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
132
133 void emitGlobalVariable(const GlobalVariable *GV) override;
134
135 void emitStartOfAsmFile(Module &M) override;
136
137 void emitEndOfAsmFile(Module &M) override;
138
139 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
140 const char *ExtraCode, raw_ostream &O) override;
141
142protected:
143 void getAnalysisUsage(AnalysisUsage &AU) const override;
144
145 std::vector<std::string> DisasmLines, HexLines;
146 size_t DisasmLineMaxLen;
147 bool IsTargetStreamerInitialized;
148};
149
150} // end namespace llvm
151
152#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
153