1 //===-- AMDGPUAsmPrinter.h - Print AMDGPU assembly code ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// \brief AMDGPU Assembly printer class.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
16 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
17 
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include <vector>
20 
21 namespace llvm {
22 
23 class AMDGPUAsmPrinter final : public AsmPrinter {
24 private:
25   struct SIProgramInfo {
26     SIProgramInfo() :
27       VGPRBlocks(0),
28       SGPRBlocks(0),
29       Priority(0),
30       FloatMode(0),
31       Priv(0),
32       DX10Clamp(0),
33       DebugMode(0),
34       IEEEMode(0),
35       ScratchSize(0),
36       ComputePGMRSrc1(0),
37       LDSBlocks(0),
38       ScratchBlocks(0),
39       ComputePGMRSrc2(0),
40       NumVGPR(0),
41       NumSGPR(0),
42       FlatUsed(false),
43       NumSGPRsForWavesPerEU(0),
44       NumVGPRsForWavesPerEU(0),
45       ReservedVGPRFirst(0),
46       ReservedVGPRCount(0),
47       DebuggerWavefrontPrivateSegmentOffsetSGPR((uint16_t)-1),
48       DebuggerPrivateSegmentBufferSGPR((uint16_t)-1),
49       VCCUsed(false),
50       CodeLen(0) {}
51 
52     // Fields set in PGM_RSRC1 pm4 packet.
53     uint32_t VGPRBlocks;
54     uint32_t SGPRBlocks;
55     uint32_t Priority;
56     uint32_t FloatMode;
57     uint32_t Priv;
58     uint32_t DX10Clamp;
59     uint32_t DebugMode;
60     uint32_t IEEEMode;
61     uint32_t ScratchSize;
62 
63     uint64_t ComputePGMRSrc1;
64 
65     // Fields set in PGM_RSRC2 pm4 packet.
66     uint32_t LDSBlocks;
67     uint32_t ScratchBlocks;
68 
69     uint64_t ComputePGMRSrc2;
70 
71     uint32_t NumVGPR;
72     uint32_t NumSGPR;
73     uint32_t LDSSize;
74     bool FlatUsed;
75 
76     // Number of SGPRs that meets number of waves per execution unit request.
77     uint32_t NumSGPRsForWavesPerEU;
78 
79     // Number of VGPRs that meets number of waves per execution unit request.
80     uint32_t NumVGPRsForWavesPerEU;
81 
82     // If ReservedVGPRCount is 0 then must be 0. Otherwise, this is the first
83     // fixed VGPR number reserved.
84     uint16_t ReservedVGPRFirst;
85 
86     // The number of consecutive VGPRs reserved.
87     uint16_t ReservedVGPRCount;
88 
89     // Fixed SGPR number used to hold wave scratch offset for entire kernel
90     // execution, or uint16_t(-1) if the register is not used or not known.
91     uint16_t DebuggerWavefrontPrivateSegmentOffsetSGPR;
92 
93     // Fixed SGPR number of the first 4 SGPRs used to hold scratch V# for entire
94     // kernel execution, or uint16_t(-1) if the register is not used or not
95     // known.
96     uint16_t DebuggerPrivateSegmentBufferSGPR;
97 
98     // Bonus information for debugging.
99     bool VCCUsed;
100     uint64_t CodeLen;
101   };
102 
103   void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF) const;
104   void findNumUsedRegistersSI(const MachineFunction &MF,
105                               unsigned &NumSGPR,
106                               unsigned &NumVGPR) const;
107 
108   /// \brief Emit register usage information so that the GPU driver
109   /// can correctly setup the GPU state.
110   void EmitProgramInfoR600(const MachineFunction &MF);
111   void EmitProgramInfoSI(const MachineFunction &MF, const SIProgramInfo &KernelInfo);
112   void EmitAmdKernelCodeT(const MachineFunction &MF,
113                           const SIProgramInfo &KernelInfo) const;
114 
115 public:
116   explicit AMDGPUAsmPrinter(TargetMachine &TM,
117                             std::unique_ptr<MCStreamer> Streamer);
118 
119   bool runOnMachineFunction(MachineFunction &MF) override;
120 
121   const char *getPassName() const override;
122 
123   /// Implemented in AMDGPUMCInstLower.cpp
124   void EmitInstruction(const MachineInstr *MI) override;
125 
126   void EmitFunctionBodyStart() override;
127 
128   void EmitFunctionEntryLabel() override;
129 
130   void EmitGlobalVariable(const GlobalVariable *GV) override;
131 
132   void EmitStartOfAsmFile(Module &M) override;
133 
134   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
135                        unsigned AsmVariant, const char *ExtraCode,
136                        raw_ostream &O) override;
137 
138   void emitStartOfRuntimeMetadata(const Module &M);
139 
140   void emitRuntimeMetadata(const Function &F);
141 
142 protected:
143   std::vector<std::string> DisasmLines, HexLines;
144   size_t DisasmLineMaxLen;
145 };
146 
147 } // End anonymous llvm
148 
149 #endif
150