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 class AMDGPUTargetStreamer;
26 class MCCodeEmitter;
27 class MCOperand;
28 class GCNSubtarget;
29 
30 namespace AMDGPU {
31 namespace HSAMD {
32 class MetadataStreamer;
33 }
34 } // namespace AMDGPU
35 
36 namespace amdhsa {
37 struct kernel_descriptor_t;
38 }
39 
40 class AMDGPUAsmPrinter final : public AsmPrinter {
41 private:
42   // Track resource usage for callee functions.
43   struct SIFunctionResourceInfo {
44     // Track the number of explicitly used VGPRs. Special registers reserved at
45     // the end are tracked separately.
46     int32_t NumVGPR = 0;
47     int32_t NumAGPR = 0;
48     int32_t NumExplicitSGPR = 0;
49     uint64_t PrivateSegmentSize = 0;
50     bool UsesVCC = false;
51     bool UsesFlatScratch = false;
52     bool HasDynamicallySizedStack = false;
53     bool HasRecursion = false;
54 
55     int32_t getTotalNumSGPRs(const GCNSubtarget &ST) const;
56     int32_t getTotalNumVGPRs(const GCNSubtarget &ST) const;
57   };
58 
59   void initializeTargetID(const Module &M);
60 
61   bool doInitialization(Module &M) override;
62 
63   SIProgramInfo CurrentProgramInfo;
64   DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo;
65 
66   std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream;
67 
68   MCCodeEmitter *DumpCodeInstEmitter = nullptr;
69 
70   uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
71   SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF) const;
72 
73   void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
74   void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo,
75                         const MachineFunction &MF) const;
76   void findNumUsedRegistersSI(const MachineFunction &MF,
77                               unsigned &NumSGPR,
78                               unsigned &NumVGPR) const;
79 
80   /// Emit register usage information so that the GPU driver
81   /// can correctly setup the GPU state.
82   void EmitProgramInfoSI(const MachineFunction &MF,
83                          const SIProgramInfo &KernelInfo);
84   void EmitPALMetadata(const MachineFunction &MF,
85                        const SIProgramInfo &KernelInfo);
86   void emitPALFunctionMetadata(const MachineFunction &MF);
87   void emitCommonFunctionComments(uint32_t NumVGPR,
88                                   Optional<uint32_t> NumAGPR,
89                                   uint32_t TotalNumVGPR,
90                                   uint32_t NumSGPR,
91                                   uint64_t ScratchSize,
92                                   uint64_t CodeSize,
93                                   const AMDGPUMachineFunction* MFI);
94 
95   uint16_t getAmdhsaKernelCodeProperties(
96       const MachineFunction &MF) const;
97 
98   amdhsa::kernel_descriptor_t getAmdhsaKernelDescriptor(
99       const MachineFunction &MF,
100       const SIProgramInfo &PI) const;
101 
102 public:
103   explicit AMDGPUAsmPrinter(TargetMachine &TM,
104                             std::unique_ptr<MCStreamer> Streamer);
105 
106   // To memoize max SGPR usage of non-kernel functions of the module.
107   unsigned NonKernelMaxSGPRs = 0;
108   // To memoize max VGPR usage of non-kernel functions of the module.
109   unsigned NonKernelMaxVGPRs = 0;
110 
111   StringRef getPassName() const override;
112 
113   const MCSubtargetInfo* getGlobalSTI() const;
114 
115   AMDGPUTargetStreamer* getTargetStreamer() const;
116 
117   bool doFinalization(Module &M) override;
118   bool runOnMachineFunction(MachineFunction &MF) override;
119 
120   /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
121   /// pseudo lowering.
122   bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
123 
124   /// Lower the specified LLVM Constant to an MCExpr.
125   /// The AsmPrinter::lowerConstantof does not know how to lower
126   /// addrspacecast, therefore they should be lowered by this function.
127   const MCExpr *lowerConstant(const Constant *CV) override;
128 
129   /// tblgen'erated driver function for lowering simple MI->MC pseudo
130   /// instructions.
131   bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
132                                    const MachineInstr *MI);
133 
134   /// Implemented in AMDGPUMCInstLower.cpp
135   void emitInstruction(const MachineInstr *MI) override;
136 
137   void emitFunctionBodyStart() override;
138 
139   void emitFunctionBodyEnd() override;
140 
141   void emitFunctionEntryLabel() override;
142 
143   void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
144 
145   void emitGlobalVariable(const GlobalVariable *GV) override;
146 
147   void emitStartOfAsmFile(Module &M) override;
148 
149   void emitEndOfAsmFile(Module &M) override;
150 
151   bool isBlockOnlyReachableByFallthrough(
152     const MachineBasicBlock *MBB) const override;
153 
154   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
155                        const char *ExtraCode, raw_ostream &O) override;
156 
157 protected:
158   std::vector<std::string> DisasmLines, HexLines;
159   size_t DisasmLineMaxLen;
160 };
161 
162 } // end namespace llvm
163 
164 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
165