OSDN Git Service

[AMDGPU] Get address space mapping by target triple environment
[android-x86/external-llvm.git] / lib / Target / AMDGPU / Utils / AMDGPUBaseInfo.h
1 //===- AMDGPUBaseInfo.h - Top level definitions for AMDGPU ------*- 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 #ifndef LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
11 #define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
12
13 #include "AMDGPU.h"
14 #include "AMDKernelCodeT.h"
15 #include "SIDefines.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/CallingConv.h"
18 #include "llvm/MC/MCInstrDesc.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include <cstdint>
22 #include <utility>
23
24 #define GET_INSTRINFO_OPERAND_ENUM
25 #include "AMDGPUGenInstrInfo.inc"
26 #undef GET_INSTRINFO_OPERAND_ENUM
27
28 namespace llvm {
29
30 class FeatureBitset;
31 class Function;
32 class GlobalValue;
33 class MachineMemOperand;
34 class MCContext;
35 class MCRegisterClass;
36 class MCRegisterInfo;
37 class MCSection;
38 class MCSubtargetInfo;
39 class Triple;
40
41 namespace AMDGPU {
42 namespace IsaInfo {
43
44 enum {
45   // The closed Vulkan driver sets 96, which limits the wave count to 8 but
46   // doesn't spill SGPRs as much as when 80 is set.
47   FIXED_NUM_SGPRS_FOR_INIT_BUG = 96
48 };
49
50 /// \brief Instruction set architecture version.
51 struct IsaVersion {
52   unsigned Major;
53   unsigned Minor;
54   unsigned Stepping;
55 };
56
57 /// \returns Isa version for given subtarget \p Features.
58 IsaVersion getIsaVersion(const FeatureBitset &Features);
59
60 /// \returns Wavefront size for given subtarget \p Features.
61 unsigned getWavefrontSize(const FeatureBitset &Features);
62
63 /// \returns Local memory size in bytes for given subtarget \p Features.
64 unsigned getLocalMemorySize(const FeatureBitset &Features);
65
66 /// \returns Number of execution units per compute unit for given subtarget \p
67 /// Features.
68 unsigned getEUsPerCU(const FeatureBitset &Features);
69
70 /// \returns Maximum number of work groups per compute unit for given subtarget
71 /// \p Features and limited by given \p FlatWorkGroupSize.
72 unsigned getMaxWorkGroupsPerCU(const FeatureBitset &Features,
73                                unsigned FlatWorkGroupSize);
74
75 /// \returns Maximum number of waves per compute unit for given subtarget \p
76 /// Features without any kind of limitation.
77 unsigned getMaxWavesPerCU(const FeatureBitset &Features);
78
79 /// \returns Maximum number of waves per compute unit for given subtarget \p
80 /// Features and limited by given \p FlatWorkGroupSize.
81 unsigned getMaxWavesPerCU(const FeatureBitset &Features,
82                           unsigned FlatWorkGroupSize);
83
84 /// \returns Minimum number of waves per execution unit for given subtarget \p
85 /// Features.
86 unsigned getMinWavesPerEU(const FeatureBitset &Features);
87
88 /// \returns Maximum number of waves per execution unit for given subtarget \p
89 /// Features without any kind of limitation.
90 unsigned getMaxWavesPerEU(const FeatureBitset &Features);
91
92 /// \returns Maximum number of waves per execution unit for given subtarget \p
93 /// Features and limited by given \p FlatWorkGroupSize.
94 unsigned getMaxWavesPerEU(const FeatureBitset &Features,
95                           unsigned FlatWorkGroupSize);
96
97 /// \returns Minimum flat work group size for given subtarget \p Features.
98 unsigned getMinFlatWorkGroupSize(const FeatureBitset &Features);
99
100 /// \returns Maximum flat work group size for given subtarget \p Features.
101 unsigned getMaxFlatWorkGroupSize(const FeatureBitset &Features);
102
103 /// \returns Number of waves per work group for given subtarget \p Features and
104 /// limited by given \p FlatWorkGroupSize.
105 unsigned getWavesPerWorkGroup(const FeatureBitset &Features,
106                               unsigned FlatWorkGroupSize);
107
108 /// \returns SGPR allocation granularity for given subtarget \p Features.
109 unsigned getSGPRAllocGranule(const FeatureBitset &Features);
110
111 /// \returns SGPR encoding granularity for given subtarget \p Features.
112 unsigned getSGPREncodingGranule(const FeatureBitset &Features);
113
114 /// \returns Total number of SGPRs for given subtarget \p Features.
115 unsigned getTotalNumSGPRs(const FeatureBitset &Features);
116
117 /// \returns Addressable number of SGPRs for given subtarget \p Features.
118 unsigned getAddressableNumSGPRs(const FeatureBitset &Features);
119
120 /// \returns Minimum number of SGPRs that meets the given number of waves per
121 /// execution unit requirement for given subtarget \p Features.
122 unsigned getMinNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
123
124 /// \returns Maximum number of SGPRs that meets the given number of waves per
125 /// execution unit requirement for given subtarget \p Features.
126 unsigned getMaxNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU,
127                         bool Addressable);
128
129 /// \returns VGPR allocation granularity for given subtarget \p Features.
130 unsigned getVGPRAllocGranule(const FeatureBitset &Features);
131
132 /// \returns VGPR encoding granularity for given subtarget \p Features.
133 unsigned getVGPREncodingGranule(const FeatureBitset &Features);
134
135 /// \returns Total number of VGPRs for given subtarget \p Features.
136 unsigned getTotalNumVGPRs(const FeatureBitset &Features);
137
138 /// \returns Addressable number of VGPRs for given subtarget \p Features.
139 unsigned getAddressableNumVGPRs(const FeatureBitset &Features);
140
141 /// \returns Minimum number of VGPRs that meets given number of waves per
142 /// execution unit requirement for given subtarget \p Features.
143 unsigned getMinNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
144
145 /// \returns Maximum number of VGPRs that meets given number of waves per
146 /// execution unit requirement for given subtarget \p Features.
147 unsigned getMaxNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
148
149 } // end namespace IsaInfo
150
151 LLVM_READONLY
152 int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx);
153
154 void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
155                                const FeatureBitset &Features);
156 MCSection *getHSATextSection(MCContext &Ctx);
157
158 MCSection *getHSADataGlobalAgentSection(MCContext &Ctx);
159
160 MCSection *getHSADataGlobalProgramSection(MCContext &Ctx);
161
162 MCSection *getHSARodataReadonlyAgentSection(MCContext &Ctx);
163
164 bool isGroupSegment(const GlobalValue *GV, AMDGPUAS AS);
165 bool isGlobalSegment(const GlobalValue *GV, AMDGPUAS AS);
166 bool isReadOnlySegment(const GlobalValue *GV, AMDGPUAS AS);
167
168 /// \returns True if constants should be emitted to .text section for given
169 /// target triple \p TT, false otherwise.
170 bool shouldEmitConstantsToTextSection(const Triple &TT);
171
172 /// \returns Integer value requested using \p F's \p Name attribute.
173 ///
174 /// \returns \p Default if attribute is not present.
175 ///
176 /// \returns \p Default and emits error if requested value cannot be converted
177 /// to integer.
178 int getIntegerAttribute(const Function &F, StringRef Name, int Default);
179
180 /// \returns A pair of integer values requested using \p F's \p Name attribute
181 /// in "first[,second]" format ("second" is optional unless \p OnlyFirstRequired
182 /// is false).
183 ///
184 /// \returns \p Default if attribute is not present.
185 ///
186 /// \returns \p Default and emits error if one of the requested values cannot be
187 /// converted to integer, or \p OnlyFirstRequired is false and "second" value is
188 /// not present.
189 std::pair<int, int> getIntegerPairAttribute(const Function &F,
190                                             StringRef Name,
191                                             std::pair<int, int> Default,
192                                             bool OnlyFirstRequired = false);
193
194 /// \returns Vmcnt bit mask for given isa \p Version.
195 unsigned getVmcntBitMask(const IsaInfo::IsaVersion &Version);
196
197 /// \returns Expcnt bit mask for given isa \p Version.
198 unsigned getExpcntBitMask(const IsaInfo::IsaVersion &Version);
199
200 /// \returns Lgkmcnt bit mask for given isa \p Version.
201 unsigned getLgkmcntBitMask(const IsaInfo::IsaVersion &Version);
202
203 /// \returns Waitcnt bit mask for given isa \p Version.
204 unsigned getWaitcntBitMask(const IsaInfo::IsaVersion &Version);
205
206 /// \returns Decoded Vmcnt from given \p Waitcnt for given isa \p Version.
207 unsigned decodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
208
209 /// \returns Decoded Expcnt from given \p Waitcnt for given isa \p Version.
210 unsigned decodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
211
212 /// \returns Decoded Lgkmcnt from given \p Waitcnt for given isa \p Version.
213 unsigned decodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
214
215 /// \brief Decodes Vmcnt, Expcnt and Lgkmcnt from given \p Waitcnt for given isa
216 /// \p Version, and writes decoded values into \p Vmcnt, \p Expcnt and
217 /// \p Lgkmcnt respectively.
218 ///
219 /// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are decoded as follows:
220 ///     \p Vmcnt = \p Waitcnt[3:0]                      (pre-gfx9 only)
221 ///     \p Vmcnt = \p Waitcnt[3:0] | \p Waitcnt[15:14]  (gfx9+ only)
222 ///     \p Expcnt = \p Waitcnt[6:4]
223 ///     \p Lgkmcnt = \p Waitcnt[11:8]
224 void decodeWaitcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
225                    unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt);
226
227 /// \returns \p Waitcnt with encoded \p Vmcnt for given isa \p Version.
228 unsigned encodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
229                      unsigned Vmcnt);
230
231 /// \returns \p Waitcnt with encoded \p Expcnt for given isa \p Version.
232 unsigned encodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
233                       unsigned Expcnt);
234
235 /// \returns \p Waitcnt with encoded \p Lgkmcnt for given isa \p Version.
236 unsigned encodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
237                        unsigned Lgkmcnt);
238
239 /// \brief Encodes \p Vmcnt, \p Expcnt and \p Lgkmcnt into Waitcnt for given isa
240 /// \p Version.
241 ///
242 /// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are encoded as follows:
243 ///     Waitcnt[3:0]   = \p Vmcnt       (pre-gfx9 only)
244 ///     Waitcnt[3:0]   = \p Vmcnt[3:0]  (gfx9+ only)
245 ///     Waitcnt[6:4]   = \p Expcnt
246 ///     Waitcnt[11:8]  = \p Lgkmcnt
247 ///     Waitcnt[15:14] = \p Vmcnt[5:4]  (gfx9+ only)
248 ///
249 /// \returns Waitcnt with encoded \p Vmcnt, \p Expcnt and \p Lgkmcnt for given
250 /// isa \p Version.
251 unsigned encodeWaitcnt(const IsaInfo::IsaVersion &Version,
252                        unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt);
253
254 unsigned getInitialPSInputAddr(const Function &F);
255
256 bool isShader(CallingConv::ID cc);
257 bool isCompute(CallingConv::ID cc);
258
259 bool isSI(const MCSubtargetInfo &STI);
260 bool isCI(const MCSubtargetInfo &STI);
261 bool isVI(const MCSubtargetInfo &STI);
262
263 /// If \p Reg is a pseudo reg, return the correct hardware register given
264 /// \p STI otherwise return \p Reg.
265 unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI);
266
267 /// \brief Convert hardware register \p Reg to a pseudo register
268 LLVM_READNONE
269 unsigned mc2PseudoReg(unsigned Reg);
270
271 /// \brief Can this operand also contain immediate values?
272 bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo);
273
274 /// \brief Is this floating-point operand?
275 bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo);
276
277 /// \brief Does this opearnd support only inlinable literals?
278 bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo);
279
280 /// \brief Get the size in bits of a register from the register class \p RC.
281 unsigned getRegBitWidth(unsigned RCID);
282
283 /// \brief Get the size in bits of a register from the register class \p RC.
284 unsigned getRegBitWidth(const MCRegisterClass &RC);
285
286 /// \brief Get size of register operand
287 unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
288                            unsigned OpNo);
289
290 LLVM_READNONE
291 inline unsigned getOperandSize(const MCOperandInfo &OpInfo) {
292   switch (OpInfo.OperandType) {
293   case AMDGPU::OPERAND_REG_IMM_INT32:
294   case AMDGPU::OPERAND_REG_IMM_FP32:
295   case AMDGPU::OPERAND_REG_INLINE_C_INT32:
296   case AMDGPU::OPERAND_REG_INLINE_C_FP32:
297     return 4;
298
299   case AMDGPU::OPERAND_REG_IMM_INT64:
300   case AMDGPU::OPERAND_REG_IMM_FP64:
301   case AMDGPU::OPERAND_REG_INLINE_C_INT64:
302   case AMDGPU::OPERAND_REG_INLINE_C_FP64:
303     return 8;
304
305   case AMDGPU::OPERAND_REG_IMM_INT16:
306   case AMDGPU::OPERAND_REG_IMM_FP16:
307   case AMDGPU::OPERAND_REG_INLINE_C_INT16:
308   case AMDGPU::OPERAND_REG_INLINE_C_FP16:
309   case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
310   case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
311     return 2;
312
313   default:
314     llvm_unreachable("unhandled operand type");
315   }
316 }
317
318 LLVM_READNONE
319 inline unsigned getOperandSize(const MCInstrDesc &Desc, unsigned OpNo) {
320   return getOperandSize(Desc.OpInfo[OpNo]);
321 }
322
323 /// \brief Is this literal inlinable
324 LLVM_READNONE
325 bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi);
326
327 LLVM_READNONE
328 bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi);
329
330 LLVM_READNONE
331 bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi);
332
333 LLVM_READNONE
334 bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi);
335
336 bool isUniformMMO(const MachineMemOperand *MMO);
337
338 /// \returns The encoding that will be used for \p ByteOffset in the SMRD
339 /// offset field.
340 int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
341
342 /// \returns true if this offset is small enough to fit in the SMRD
343 /// offset field.  \p ByteOffset should be the offset in bytes and
344 /// not the encoded offset.
345 bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
346
347 } // end namespace AMDGPU
348 } // end namespace llvm
349
350 #endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H