blob: bc2cb4b608adbbba49768e50bb7ce1ea77df59ac [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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// AMDGPU Assembly printer class.
Tom Stellard75aadc22012-12-11 21:25:42 +000012//
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
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +000032class AMDGPUMachineFunction;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000033class AMDGPUTargetStreamer;
Matt Arsenault11f74022016-10-06 17:19:11 +000034class MCOperand;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +000035class SISubtarget;
Tom Stellard75aadc22012-12-11 21:25:42 +000036
Matt Arsenault6b6a2c32016-03-11 08:00:27 +000037class AMDGPUAsmPrinter final : public AsmPrinter {
Matt Arsenault89cc49f2013-12-05 05:15:35 +000038private:
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +000039 // Track resource usage for callee functions.
40 struct SIFunctionResourceInfo {
41 // Track the number of explicitly used VGPRs. Special registers reserved at
42 // the end are tracked separately.
43 int32_t NumVGPR = 0;
44 int32_t NumExplicitSGPR = 0;
Matt Arsenault9ba465a2017-11-14 20:33:14 +000045 uint64_t PrivateSegmentSize = 0;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +000046 bool UsesVCC = false;
47 bool UsesFlatScratch = false;
48 bool HasDynamicallySizedStack = false;
49 bool HasRecursion = false;
50
51 int32_t getTotalNumSGPRs(const SISubtarget &ST) const;
52 };
53
54 // Track resource usage for kernels / entry functions.
Matt Arsenault89cc49f2013-12-05 05:15:35 +000055 struct SIProgramInfo {
Matt Arsenault0989d512014-06-26 17:22:30 +000056 // Fields set in PGM_RSRC1 pm4 packet.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000057 uint32_t VGPRBlocks = 0;
58 uint32_t SGPRBlocks = 0;
59 uint32_t Priority = 0;
60 uint32_t FloatMode = 0;
61 uint32_t Priv = 0;
62 uint32_t DX10Clamp = 0;
63 uint32_t DebugMode = 0;
64 uint32_t IEEEMode = 0;
Matt Arsenault9ba465a2017-11-14 20:33:14 +000065 uint64_t ScratchSize = 0;
Matt Arsenault0989d512014-06-26 17:22:30 +000066
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000067 uint64_t ComputePGMRSrc1 = 0;
Tom Stellard4df465b2014-12-02 21:28:53 +000068
69 // Fields set in PGM_RSRC2 pm4 packet.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000070 uint32_t LDSBlocks = 0;
71 uint32_t ScratchBlocks = 0;
Tom Stellard4df465b2014-12-02 21:28:53 +000072
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000073 uint64_t ComputePGMRSrc2 = 0;
Tom Stellard4df465b2014-12-02 21:28:53 +000074
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000075 uint32_t NumVGPR = 0;
76 uint32_t NumSGPR = 0;
Matt Arsenaulta3566f22017-04-17 19:48:30 +000077 uint32_t LDSSize = 0;
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000078 bool FlatUsed = false;
Matt Arsenault3f981402014-09-15 15:41:53 +000079
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000080 // Number of SGPRs that meets number of waves per execution unit request.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000081 uint32_t NumSGPRsForWavesPerEU = 0;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000082
83 // Number of VGPRs that meets number of waves per execution unit request.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000084 uint32_t NumVGPRsForWavesPerEU = 0;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000085
Konstantin Zhuravlyov71515e52016-04-26 17:24:40 +000086 // If ReservedVGPRCount is 0 then must be 0. Otherwise, this is the first
87 // fixed VGPR number reserved.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000088 uint16_t ReservedVGPRFirst = 0;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000089
Konstantin Zhuravlyov71515e52016-04-26 17:24:40 +000090 // The number of consecutive VGPRs reserved.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000091 uint16_t ReservedVGPRCount = 0;
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +000092
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +000093 // Fixed SGPR number used to hold wave scratch offset for entire kernel
Eugene Zelenkoa63528c2017-01-23 23:41:16 +000094 // execution, or std::numeric_limits<uint16_t>::max() if the register is not
95 // used or not known.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000096 uint16_t DebuggerWavefrontPrivateSegmentOffsetSGPR =
97 std::numeric_limits<uint16_t>::max();
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000098
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +000099 // Fixed SGPR number of the first 4 SGPRs used to hold scratch V# for entire
Eugene Zelenkoa63528c2017-01-23 23:41:16 +0000100 // kernel execution, or std::numeric_limits<uint16_t>::max() if the register
101 // is not used or not known.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000102 uint16_t DebuggerPrivateSegmentBufferSGPR =
103 std::numeric_limits<uint16_t>::max();
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000104
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000105 // Whether there is recursion, dynamic allocas, indirect calls or some other
106 // reason there may be statically unknown stack usage.
107 bool DynamicCallStack = false;
108
Matt Arsenault0989d512014-06-26 17:22:30 +0000109 // Bonus information for debugging.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000110 bool VCCUsed = false;
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000111
112 SIProgramInfo() = default;
Matt Arsenault89cc49f2013-12-05 05:15:35 +0000113 };
114
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000115 SIProgramInfo CurrentProgramInfo;
116 DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo;
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000117
118 AMDGPU::HSAMD::MetadataStreamer HSAMetadataStream;
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000119 std::map<uint32_t, uint32_t> PALMetadataMap;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000120
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000121 uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000122 SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF) const;
123
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000124 void readPALMetadata(Module &M);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000125 void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +0000126 void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo,
127 const MachineFunction &MF) const;
Matt Arsenaultd32dbb62014-07-13 03:06:43 +0000128 void findNumUsedRegistersSI(const MachineFunction &MF,
Matt Arsenault89cc49f2013-12-05 05:15:35 +0000129 unsigned &NumSGPR,
130 unsigned &NumVGPR) const;
131
Konstantin Zhuravlyova01d8b02017-10-14 19:03:51 +0000132 AMDGPU::HSAMD::Kernel::CodeProps::Metadata getHSACodeProps(
133 const MachineFunction &MF,
134 const SIProgramInfo &ProgramInfo) const;
135 AMDGPU::HSAMD::Kernel::DebugProps::Metadata getHSADebugProps(
136 const MachineFunction &MF,
137 const SIProgramInfo &ProgramInfo) const;
138
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000139 /// Emit register usage information so that the GPU driver
Matt Arsenault89cc49f2013-12-05 05:15:35 +0000140 /// can correctly setup the GPU state.
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,
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000148 uint64_t CodeSize,
149 const AMDGPUMachineFunction* MFI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000150
151public:
David Blaikie94598322015-01-18 20:29:04 +0000152 explicit AMDGPUAsmPrinter(TargetMachine &TM,
153 std::unique_ptr<MCStreamer> Streamer);
Tom Stellard75aadc22012-12-11 21:25:42 +0000154
Mehdi Amini117296c2016-10-01 02:56:57 +0000155 StringRef getPassName() const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000156
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000157 const MCSubtargetInfo* getSTI() const;
158
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000159 AMDGPUTargetStreamer* getTargetStreamer() const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000160
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000161 bool doFinalization(Module &M) override;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000162 bool runOnMachineFunction(MachineFunction &MF) override;
163
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000164 /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
Matt Arsenault11f74022016-10-06 17:19:11 +0000165 /// pseudo lowering.
166 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
167
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000168 /// Lower the specified LLVM Constant to an MCExpr.
Yaxun Liu8f844f32017-02-07 00:43:21 +0000169 /// The AsmPrinter::lowerConstantof does not know how to lower
170 /// addrspacecast, therefore they should be lowered by this function.
171 const MCExpr *lowerConstant(const Constant *CV) override;
172
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000173 /// tblgen'erated driver function for lowering simple MI->MC pseudo
Matt Arsenault11f74022016-10-06 17:19:11 +0000174 /// instructions.
175 bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
176 const MachineInstr *MI);
177
Tom Stellard75aadc22012-12-11 21:25:42 +0000178 /// Implemented in AMDGPUMCInstLower.cpp
Craig Topper5656db42014-04-29 07:57:24 +0000179 void EmitInstruction(const MachineInstr *MI) override;
Tom Stellarded699252013-10-12 05:02:51 +0000180
Tom Stellardf151a452015-06-26 21:14:58 +0000181 void EmitFunctionBodyStart() override;
182
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000183 void EmitFunctionEntryLabel() override;
184
Tim Renoufcead41d2017-12-08 14:09:34 +0000185 void EmitBasicBlockStart(const MachineBasicBlock &MBB) const override;
186
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000187 void EmitGlobalVariable(const GlobalVariable *GV) override;
188
Tom Stellardf4218372016-01-12 17:18:17 +0000189 void EmitStartOfAsmFile(Module &M) override;
190
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000191 void EmitEndOfAsmFile(Module &M) override;
192
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000193 bool isBlockOnlyReachableByFallthrough(
194 const MachineBasicBlock *MBB) const override;
195
Tom Stellardd7e6f132015-04-08 01:09:26 +0000196 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
197 unsigned AsmVariant, const char *ExtraCode,
Tom Stellard80e169a2015-04-08 02:07:05 +0000198 raw_ostream &O) override;
Tom Stellardd7e6f132015-04-08 01:09:26 +0000199
Tom Stellarded699252013-10-12 05:02:51 +0000200protected:
Tim Renoufcead41d2017-12-08 14:09:34 +0000201 mutable std::vector<std::string> DisasmLines, HexLines;
202 mutable size_t DisasmLineMaxLen;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000203 AMDGPUAS AMDGPUASI;
Tom Stellard75aadc22012-12-11 21:25:42 +0000204};
205
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000206} // end namespace llvm
Tom Stellard75aadc22012-12-11 21:25:42 +0000207
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000208#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H