blob: 13425c8b2a0f5954617e1abb623aa489139d4e31 [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
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +000018#include "AMDKernelCodeT.h"
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000019#include "AMDGPU.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000020#include "llvm/ADT/StringRef.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "llvm/CodeGen/AsmPrinter.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000022#include <cstddef>
23#include <cstdint>
24#include <limits>
25#include <memory>
26#include <string>
Tom Stellarded699252013-10-12 05:02:51 +000027#include <vector>
Tom Stellard75aadc22012-12-11 21:25:42 +000028
29namespace llvm {
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000030
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000031class AMDGPUTargetStreamer;
Matt Arsenault11f74022016-10-06 17:19:11 +000032class MCOperand;
Tom Stellard75aadc22012-12-11 21:25:42 +000033
Matt Arsenault6b6a2c32016-03-11 08:00:27 +000034class AMDGPUAsmPrinter final : public AsmPrinter {
Matt Arsenault89cc49f2013-12-05 05:15:35 +000035private:
36 struct SIProgramInfo {
Matt Arsenault0989d512014-06-26 17:22:30 +000037 // Fields set in PGM_RSRC1 pm4 packet.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000038 uint32_t VGPRBlocks = 0;
39 uint32_t SGPRBlocks = 0;
40 uint32_t Priority = 0;
41 uint32_t FloatMode = 0;
42 uint32_t Priv = 0;
43 uint32_t DX10Clamp = 0;
44 uint32_t DebugMode = 0;
45 uint32_t IEEEMode = 0;
46 uint32_t ScratchSize = 0;
Matt Arsenault0989d512014-06-26 17:22:30 +000047
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000048 uint64_t ComputePGMRSrc1 = 0;
Tom Stellard4df465b2014-12-02 21:28:53 +000049
50 // Fields set in PGM_RSRC2 pm4 packet.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000051 uint32_t LDSBlocks = 0;
52 uint32_t ScratchBlocks = 0;
Tom Stellard4df465b2014-12-02 21:28:53 +000053
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000054 uint64_t ComputePGMRSrc2 = 0;
Tom Stellard4df465b2014-12-02 21:28:53 +000055
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000056 uint32_t NumVGPR = 0;
57 uint32_t NumSGPR = 0;
Tom Stellard4df465b2014-12-02 21:28:53 +000058 uint32_t LDSSize;
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000059 bool FlatUsed = false;
Matt Arsenault3f981402014-09-15 15:41:53 +000060
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000061 // Number of SGPRs that meets number of waves per execution unit request.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000062 uint32_t NumSGPRsForWavesPerEU = 0;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000063
64 // Number of VGPRs that meets number of waves per execution unit request.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000065 uint32_t NumVGPRsForWavesPerEU = 0;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000066
Konstantin Zhuravlyov71515e52016-04-26 17:24:40 +000067 // If ReservedVGPRCount is 0 then must be 0. Otherwise, this is the first
68 // fixed VGPR number reserved.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000069 uint16_t ReservedVGPRFirst = 0;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000070
Konstantin Zhuravlyov71515e52016-04-26 17:24:40 +000071 // The number of consecutive VGPRs reserved.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000072 uint16_t ReservedVGPRCount = 0;
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +000073
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +000074 // Fixed SGPR number used to hold wave scratch offset for entire kernel
Eugene Zelenkoa63528c2017-01-23 23:41:16 +000075 // execution, or std::numeric_limits<uint16_t>::max() if the register is not
76 // used or not known.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000077 uint16_t DebuggerWavefrontPrivateSegmentOffsetSGPR =
78 std::numeric_limits<uint16_t>::max();
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +000079
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +000080 // Fixed SGPR number of the first 4 SGPRs used to hold scratch V# for entire
Eugene Zelenkoa63528c2017-01-23 23:41:16 +000081 // kernel execution, or std::numeric_limits<uint16_t>::max() if the register
82 // is not used or not known.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000083 uint16_t DebuggerPrivateSegmentBufferSGPR =
84 std::numeric_limits<uint16_t>::max();
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +000085
Matt Arsenault0989d512014-06-26 17:22:30 +000086 // Bonus information for debugging.
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000087 bool VCCUsed = false;
88 uint64_t CodeLen = 0;
89
90 SIProgramInfo() = default;
Matt Arsenault89cc49f2013-12-05 05:15:35 +000091 };
92
Matt Arsenaultd32dbb62014-07-13 03:06:43 +000093 void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF) const;
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +000094 void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo,
95 const MachineFunction &MF) const;
Matt Arsenaultd32dbb62014-07-13 03:06:43 +000096 void findNumUsedRegistersSI(const MachineFunction &MF,
Matt Arsenault89cc49f2013-12-05 05:15:35 +000097 unsigned &NumSGPR,
98 unsigned &NumVGPR) const;
99
100 /// \brief Emit register usage information so that the GPU driver
101 /// can correctly setup the GPU state.
Matt Arsenaultd32dbb62014-07-13 03:06:43 +0000102 void EmitProgramInfoR600(const MachineFunction &MF);
103 void EmitProgramInfoSI(const MachineFunction &MF, const SIProgramInfo &KernelInfo);
Tom Stellard75aadc22012-12-11 21:25:42 +0000104
105public:
David Blaikie94598322015-01-18 20:29:04 +0000106 explicit AMDGPUAsmPrinter(TargetMachine &TM,
107 std::unique_ptr<MCStreamer> Streamer);
Tom Stellard75aadc22012-12-11 21:25:42 +0000108
Mehdi Amini117296c2016-10-01 02:56:57 +0000109 StringRef getPassName() const override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000110
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000111 const MCSubtargetInfo* getSTI() const;
112
113 AMDGPUTargetStreamer& getTargetStreamer() const;
114
115 bool runOnMachineFunction(MachineFunction &MF) override;
116
Matt Arsenault11f74022016-10-06 17:19:11 +0000117 /// \brief Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
118 /// pseudo lowering.
119 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
120
Yaxun Liu8f844f32017-02-07 00:43:21 +0000121 /// \brief Lower the specified LLVM Constant to an MCExpr.
122 /// The AsmPrinter::lowerConstantof does not know how to lower
123 /// addrspacecast, therefore they should be lowered by this function.
124 const MCExpr *lowerConstant(const Constant *CV) override;
125
Matt Arsenault11f74022016-10-06 17:19:11 +0000126 /// \brief tblgen'erated driver function for lowering simple MI->MC pseudo
127 /// instructions.
128 bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
129 const MachineInstr *MI);
130
Tom Stellard75aadc22012-12-11 21:25:42 +0000131 /// Implemented in AMDGPUMCInstLower.cpp
Craig Topper5656db42014-04-29 07:57:24 +0000132 void EmitInstruction(const MachineInstr *MI) override;
Tom Stellarded699252013-10-12 05:02:51 +0000133
Tom Stellardf151a452015-06-26 21:14:58 +0000134 void EmitFunctionBodyStart() override;
135
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000136 void EmitFunctionEntryLabel() override;
137
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000138 void EmitGlobalVariable(const GlobalVariable *GV) override;
139
Tom Stellardf4218372016-01-12 17:18:17 +0000140 void EmitStartOfAsmFile(Module &M) override;
141
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000142 void EmitEndOfAsmFile(Module &M) override;
143
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000144 bool isBlockOnlyReachableByFallthrough(
145 const MachineBasicBlock *MBB) const override;
146
Tom Stellardd7e6f132015-04-08 01:09:26 +0000147 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
148 unsigned AsmVariant, const char *ExtraCode,
Tom Stellard80e169a2015-04-08 02:07:05 +0000149 raw_ostream &O) override;
Tom Stellardd7e6f132015-04-08 01:09:26 +0000150
Tom Stellarded699252013-10-12 05:02:51 +0000151protected:
Tom Stellarded699252013-10-12 05:02:51 +0000152 std::vector<std::string> DisasmLines, HexLines;
153 size_t DisasmLineMaxLen;
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000154 AMDGPUAS AMDGPUASI;
Tom Stellard75aadc22012-12-11 21:25:42 +0000155};
156
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000157} // end namespace llvm
Tom Stellard75aadc22012-12-11 21:25:42 +0000158
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000159#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H