blob: 51d48a0c7320c380f6cf16c0a84c3233ca7cf416 [file] [log] [blame]
Matt Arsenaultd82c1832013-11-10 01:03:59 +00001//===-- AMDGPUAsmPrinter.h - Print AMDGPU assembly code ---------*- C++ -*-===//
Tom Stellard75aadc22012-12-11 21:25:42 +00002//
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
Matt Arsenault6b6a2c32016-03-11 08:00:27 +000015#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
16#define LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
Tom Stellard75aadc22012-12-11 21:25:42 +000017
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000018#include "AMDGPU.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "AMDKernelCodeT.h"
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +000020#include "MCTargetDesc/AMDGPUHSAMetadataStreamer.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000021#include "llvm/ADT/StringRef.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000022#include "llvm/CodeGen/AsmPrinter.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000023#include <cstddef>
24#include <cstdint>
25#include <limits>
26#include <memory>
27#include <string>
Tom Stellarded699252013-10-12 05:02:51 +000028#include <vector>
Tom Stellard75aadc22012-12-11 21:25:42 +000029
30namespace llvm {
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000031
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000032class AMDGPUTargetStreamer;
Matt Arsenault11f74022016-10-06 17:19:11 +000033class MCOperand;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +000034class SISubtarget;
Tom Stellard75aadc22012-12-11 21:25:42 +000035
Matt Arsenault6b6a2c32016-03-11 08:00:27 +000036class AMDGPUAsmPrinter final : public AsmPrinter {
Matt Arsenault89cc49f2013-12-05 05:15:35 +000037private:
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +000038 // Track resource usage for callee functions.
39 struct SIFunctionResourceInfo {
40 // Track the number of explicitly used VGPRs. Special registers reserved at
41 // the end are tracked separately.
42 int32_t NumVGPR = 0;
43 int32_t NumExplicitSGPR = 0;
Matt Arsenault9ba465a2017-11-14 20:33:14 +000044 uint64_t PrivateSegmentSize = 0;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +000045 bool UsesVCC = false;
46 bool UsesFlatScratch = false;
47 bool HasDynamicallySizedStack = false;
48 bool HasRecursion = false;
49
50 int32_t getTotalNumSGPRs(const SISubtarget &ST) const;
51 };
52
53 // Track resource usage for kernels / entry functions.
Matt Arsenault89cc49f2013-12-05 05:15:35 +000054 struct SIProgramInfo {
Matt Arsenault0989d512014-06-26 17:22:30 +000055 // Fields set in PGM_RSRC1 pm4 packet.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000056 uint32_t VGPRBlocks = 0;
57 uint32_t SGPRBlocks = 0;
58 uint32_t Priority = 0;
59 uint32_t FloatMode = 0;
60 uint32_t Priv = 0;
61 uint32_t DX10Clamp = 0;
62 uint32_t DebugMode = 0;
63 uint32_t IEEEMode = 0;
Matt Arsenault9ba465a2017-11-14 20:33:14 +000064 uint64_t ScratchSize = 0;
Matt Arsenault0989d512014-06-26 17:22:30 +000065
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000066 uint64_t ComputePGMRSrc1 = 0;
Tom Stellard4df465b2014-12-02 21:28:53 +000067
68 // Fields set in PGM_RSRC2 pm4 packet.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000069 uint32_t LDSBlocks = 0;
70 uint32_t ScratchBlocks = 0;
Tom Stellard4df465b2014-12-02 21:28:53 +000071
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000072 uint64_t ComputePGMRSrc2 = 0;
Tom Stellard4df465b2014-12-02 21:28:53 +000073
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000074 uint32_t NumVGPR = 0;
75 uint32_t NumSGPR = 0;
Matt Arsenaulta3566f22017-04-17 19:48:30 +000076 uint32_t LDSSize = 0;
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000077 bool FlatUsed = false;
Matt Arsenault3f981402014-09-15 15:41:53 +000078
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000079 // Number of SGPRs that meets number of waves per execution unit request.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000080 uint32_t NumSGPRsForWavesPerEU = 0;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000081
82 // Number of VGPRs that meets number of waves per execution unit request.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000083 uint32_t NumVGPRsForWavesPerEU = 0;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000084
Konstantin Zhuravlyov71515e52016-04-26 17:24:40 +000085 // If ReservedVGPRCount is 0 then must be 0. Otherwise, this is the first
86 // fixed VGPR number reserved.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000087 uint16_t ReservedVGPRFirst = 0;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000088
Konstantin Zhuravlyov71515e52016-04-26 17:24:40 +000089 // The number of consecutive VGPRs reserved.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000090 uint16_t ReservedVGPRCount = 0;
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +000091
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +000092 // Fixed SGPR number used to hold wave scratch offset for entire kernel
Eugene Zelenkoa63528c2017-01-23 23:41:16 +000093 // execution, or std::numeric_limits<uint16_t>::max() if the register is not
94 // used or not known.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000095 uint16_t DebuggerWavefrontPrivateSegmentOffsetSGPR =
96 std::numeric_limits<uint16_t>::max();
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000097
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +000098 // Fixed SGPR number of the first 4 SGPRs used to hold scratch V# for entire
Eugene Zelenkoa63528c2017-01-23 23:41:16 +000099 // kernel execution, or std::numeric_limits<uint16_t>::max() if the register
100 // is not used or not known.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000101 uint16_t DebuggerPrivateSegmentBufferSGPR =
102 std::numeric_limits<uint16_t>::max();
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000103
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000104 // Whether there is recursion, dynamic allocas, indirect calls or some other
105 // reason there may be statically unknown stack usage.
106 bool DynamicCallStack = false;
107
Matt Arsenault0989d512014-06-26 17:22:30 +0000108 // Bonus information for debugging.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000109 bool VCCUsed = false;
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000110
111 SIProgramInfo() = default;
Matt Arsenault89cc49f2013-12-05 05:15:35 +0000112 };
113
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000114 SIProgramInfo CurrentProgramInfo;
115 DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo;
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000116
117 AMDGPU::HSAMD::MetadataStreamer HSAMetadataStream;
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000118 std::map<uint32_t, uint32_t> PALMetadataMap;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000119
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000120 uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000121 SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF) const;
122
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000123 void readPALMetadata(Module &M);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000124 void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +0000125 void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo,
126 const MachineFunction &MF) const;
Matt Arsenaultd32dbb62014-07-13 03:06:43 +0000127 void findNumUsedRegistersSI(const MachineFunction &MF,
Matt Arsenault89cc49f2013-12-05 05:15:35 +0000128 unsigned &NumSGPR,
129 unsigned &NumVGPR) const;
130
Konstantin Zhuravlyova01d8b02017-10-14 19:03:51 +0000131 AMDGPU::HSAMD::Kernel::CodeProps::Metadata getHSACodeProps(
132 const MachineFunction &MF,
133 const SIProgramInfo &ProgramInfo) const;
134 AMDGPU::HSAMD::Kernel::DebugProps::Metadata getHSADebugProps(
135 const MachineFunction &MF,
136 const SIProgramInfo &ProgramInfo) const;
137
Matt Arsenault89cc49f2013-12-05 05:15:35 +0000138 /// \brief Emit register usage information so that the GPU driver
139 /// can correctly setup the GPU state.
Matt Arsenaultd32dbb62014-07-13 03:06:43 +0000140 void EmitProgramInfoR600(const MachineFunction &MF);
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000141 void EmitProgramInfoSI(const MachineFunction &MF,
142 const SIProgramInfo &KernelInfo);
143 void EmitPALMetadata(const MachineFunction &MF,
144 const SIProgramInfo &KernelInfo);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000145 void emitCommonFunctionComments(uint32_t NumVGPR,
146 uint32_t NumSGPR,
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000147 uint64_t ScratchSize,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000148 uint64_t CodeSize);
Tom Stellard75aadc22012-12-11 21:25:42 +0000149
150public:
David Blaikie94598322015-01-18 20:29:04 +0000151 explicit AMDGPUAsmPrinter(TargetMachine &TM,
152 std::unique_ptr<MCStreamer> Streamer);
Tom Stellard75aadc22012-12-11 21:25:42 +0000153
Mehdi Amini117296c2016-10-01 02:56:57 +0000154 StringRef getPassName() const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000155
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000156 const MCSubtargetInfo* getSTI() const;
157
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000158 AMDGPUTargetStreamer* getTargetStreamer() const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000159
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000160 bool doFinalization(Module &M) override;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000161 bool runOnMachineFunction(MachineFunction &MF) override;
162
Matt Arsenault11f74022016-10-06 17:19:11 +0000163 /// \brief Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
164 /// pseudo lowering.
165 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
166
Yaxun Liu8f844f32017-02-07 00:43:21 +0000167 /// \brief Lower the specified LLVM Constant to an MCExpr.
168 /// The AsmPrinter::lowerConstantof does not know how to lower
169 /// addrspacecast, therefore they should be lowered by this function.
170 const MCExpr *lowerConstant(const Constant *CV) override;
171
Matt Arsenault11f74022016-10-06 17:19:11 +0000172 /// \brief tblgen'erated driver function for lowering simple MI->MC pseudo
173 /// instructions.
174 bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
175 const MachineInstr *MI);
176
Tom Stellard75aadc22012-12-11 21:25:42 +0000177 /// Implemented in AMDGPUMCInstLower.cpp
Craig Topper5656db42014-04-29 07:57:24 +0000178 void EmitInstruction(const MachineInstr *MI) override;
Tom Stellarded699252013-10-12 05:02:51 +0000179
Tom Stellardf151a452015-06-26 21:14:58 +0000180 void EmitFunctionBodyStart() override;
181
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000182 void EmitFunctionEntryLabel() override;
183
Tim Renoufcead41d2017-12-08 14:09:34 +0000184 void EmitBasicBlockStart(const MachineBasicBlock &MBB) const override;
185
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000186 void EmitGlobalVariable(const GlobalVariable *GV) override;
187
Tom Stellardf4218372016-01-12 17:18:17 +0000188 void EmitStartOfAsmFile(Module &M) override;
189
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000190 void EmitEndOfAsmFile(Module &M) override;
191
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000192 bool isBlockOnlyReachableByFallthrough(
193 const MachineBasicBlock *MBB) const override;
194
Tom Stellardd7e6f132015-04-08 01:09:26 +0000195 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
196 unsigned AsmVariant, const char *ExtraCode,
Tom Stellard80e169a2015-04-08 02:07:05 +0000197 raw_ostream &O) override;
Tom Stellardd7e6f132015-04-08 01:09:26 +0000198
Tom Stellarded699252013-10-12 05:02:51 +0000199protected:
Tim Renoufcead41d2017-12-08 14:09:34 +0000200 mutable std::vector<std::string> DisasmLines, HexLines;
201 mutable size_t DisasmLineMaxLen;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000202 AMDGPUAS AMDGPUASI;
Tom Stellard75aadc22012-12-11 21:25:42 +0000203};
204
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000205} // end namespace llvm
Tom Stellard75aadc22012-12-11 21:25:42 +0000206
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000207#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H