xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
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   SIProgramInfo CurrentProgramInfo;
60   DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo;
61 
62   std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream;
63 
64   MCCodeEmitter *DumpCodeInstEmitter = nullptr;
65 
66   uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
67   SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF) const;
68 
69   void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
70   void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo,
71                         const MachineFunction &MF) const;
72   void findNumUsedRegistersSI(const MachineFunction &MF,
73                               unsigned &NumSGPR,
74                               unsigned &NumVGPR) const;
75 
76   /// Emit register usage information so that the GPU driver
77   /// can correctly setup the GPU state.
78   void EmitProgramInfoSI(const MachineFunction &MF,
79                          const SIProgramInfo &KernelInfo);
80   void EmitPALMetadata(const MachineFunction &MF,
81                        const SIProgramInfo &KernelInfo);
82   void emitPALFunctionMetadata(const MachineFunction &MF);
83   void emitCommonFunctionComments(uint32_t NumVGPR,
84                                   Optional<uint32_t> NumAGPR,
85                                   uint32_t TotalNumVGPR,
86                                   uint32_t NumSGPR,
87                                   uint64_t ScratchSize,
88                                   uint64_t CodeSize,
89                                   const AMDGPUMachineFunction* MFI);
90 
91   uint16_t getAmdhsaKernelCodeProperties(
92       const MachineFunction &MF) const;
93 
94   amdhsa::kernel_descriptor_t getAmdhsaKernelDescriptor(
95       const MachineFunction &MF,
96       const SIProgramInfo &PI) const;
97 
98 public:
99   explicit AMDGPUAsmPrinter(TargetMachine &TM,
100                             std::unique_ptr<MCStreamer> Streamer);
101 
102   StringRef getPassName() const override;
103 
104   const MCSubtargetInfo* getGlobalSTI() const;
105 
106   AMDGPUTargetStreamer* getTargetStreamer() const;
107 
108   bool doFinalization(Module &M) override;
109   bool runOnMachineFunction(MachineFunction &MF) override;
110 
111   /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
112   /// pseudo lowering.
113   bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
114 
115   /// Lower the specified LLVM Constant to an MCExpr.
116   /// The AsmPrinter::lowerConstantof does not know how to lower
117   /// addrspacecast, therefore they should be lowered by this function.
118   const MCExpr *lowerConstant(const Constant *CV) override;
119 
120   /// tblgen'erated driver function for lowering simple MI->MC pseudo
121   /// instructions.
122   bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
123                                    const MachineInstr *MI);
124 
125   /// Implemented in AMDGPUMCInstLower.cpp
126   void emitInstruction(const MachineInstr *MI) override;
127 
128   void emitFunctionBodyStart() override;
129 
130   void emitFunctionBodyEnd() override;
131 
132   void emitFunctionEntryLabel() override;
133 
134   void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
135 
136   void emitGlobalVariable(const GlobalVariable *GV) override;
137 
138   void emitStartOfAsmFile(Module &M) override;
139 
140   void emitEndOfAsmFile(Module &M) override;
141 
142   bool isBlockOnlyReachableByFallthrough(
143     const MachineBasicBlock *MBB) const override;
144 
145   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
146                        const char *ExtraCode, raw_ostream &O) override;
147 
148 protected:
149   std::vector<std::string> DisasmLines, HexLines;
150   size_t DisasmLineMaxLen;
151 };
152 
153 } // end namespace llvm
154 
155 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
156