1 //===-- AMDGPUPALMetadata.cpp - Accumulate and print AMDGPU PAL metadata  -===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 ///
11 /// This class has methods called by AMDGPUAsmPrinter to accumulate and print
12 /// the PAL metadata.
13 //
14 //===----------------------------------------------------------------------===//
15 //
16 
17 #include "AMDGPUPALMetadata.h"
18 #include "AMDGPU.h"
19 #include "AMDGPUAsmPrinter.h"
20 #include "MCTargetDesc/AMDGPUTargetStreamer.h"
21 #include "SIDefines.h"
22 #include "llvm/BinaryFormat/ELF.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Support/AMDGPUMetadata.h"
27 #include "llvm/Support/EndianStream.h"
28 
29 using namespace llvm;
30 using namespace llvm::AMDGPU;
31 
32 // Read the PAL metadata from IR metadata, where it was put by the frontend.
33 void AMDGPUPALMetadata::readFromIR(Module &M) {
34   auto NamedMD = M.getNamedMetadata("amdgpu.pal.metadata.msgpack");
35   if (NamedMD && NamedMD->getNumOperands()) {
36     // This is the new msgpack format for metadata. It is a NamedMD containing
37     // an MDTuple containing an MDString containing the msgpack data.
38     BlobType = ELF::NT_AMDGPU_METADATA;
39     auto MDN = dyn_cast<MDTuple>(NamedMD->getOperand(0));
40     if (MDN && MDN->getNumOperands()) {
41       if (auto MDS = dyn_cast<MDString>(MDN->getOperand(0)))
42         setFromMsgPackBlob(MDS->getString());
43     }
44     return;
45   }
46   BlobType = ELF::NT_AMD_AMDGPU_PAL_METADATA;
47   NamedMD = M.getNamedMetadata("amdgpu.pal.metadata");
48   if (!NamedMD || !NamedMD->getNumOperands()) {
49     // Emit msgpack metadata by default
50     BlobType = ELF::NT_AMDGPU_METADATA;
51     return;
52   }
53   // This is the old reg=value pair format for metadata. It is a NamedMD
54   // containing an MDTuple containing a number of MDNodes each of which is an
55   // integer value, and each two integer values forms a key=value pair that we
56   // store as Registers[key]=value in the map.
57   auto Tuple = dyn_cast<MDTuple>(NamedMD->getOperand(0));
58   if (!Tuple)
59     return;
60   for (unsigned I = 0, E = Tuple->getNumOperands() & -2; I != E; I += 2) {
61     auto Key = mdconst::dyn_extract<ConstantInt>(Tuple->getOperand(I));
62     auto Val = mdconst::dyn_extract<ConstantInt>(Tuple->getOperand(I + 1));
63     if (!Key || !Val)
64       continue;
65     setRegister(Key->getZExtValue(), Val->getZExtValue());
66   }
67 }
68 
69 // Set PAL metadata from a binary blob from the applicable .note record.
70 // Returns false if bad format.  Blob must remain valid for the lifetime of the
71 // Metadata.
72 bool AMDGPUPALMetadata::setFromBlob(unsigned Type, StringRef Blob) {
73   BlobType = Type;
74   if (Type == ELF::NT_AMD_AMDGPU_PAL_METADATA)
75     return setFromLegacyBlob(Blob);
76   return setFromMsgPackBlob(Blob);
77 }
78 
79 // Set PAL metadata from legacy (array of key=value pairs) blob.
80 bool AMDGPUPALMetadata::setFromLegacyBlob(StringRef Blob) {
81   auto Data = reinterpret_cast<const uint32_t *>(Blob.data());
82   for (unsigned I = 0; I != Blob.size() / sizeof(uint32_t) / 2; ++I)
83     setRegister(Data[I * 2], Data[I * 2 + 1]);
84   return true;
85 }
86 
87 // Set PAL metadata from msgpack blob.
88 bool AMDGPUPALMetadata::setFromMsgPackBlob(StringRef Blob) {
89   msgpack::Reader Reader(Blob);
90   return MsgPackDoc.readFromBlob(Blob, /*Multi=*/false);
91 }
92 
93 // Given the calling convention, calculate the register number for rsrc1. In
94 // principle the register number could change in future hardware, but we know
95 // it is the same for gfx6-9 (except that LS and ES don't exist on gfx9), so
96 // we can use fixed values.
97 static unsigned getRsrc1Reg(CallingConv::ID CC) {
98   switch (CC) {
99   default:
100     return PALMD::R_2E12_COMPUTE_PGM_RSRC1;
101   case CallingConv::AMDGPU_LS:
102     return PALMD::R_2D4A_SPI_SHADER_PGM_RSRC1_LS;
103   case CallingConv::AMDGPU_HS:
104     return PALMD::R_2D0A_SPI_SHADER_PGM_RSRC1_HS;
105   case CallingConv::AMDGPU_ES:
106     return PALMD::R_2CCA_SPI_SHADER_PGM_RSRC1_ES;
107   case CallingConv::AMDGPU_GS:
108     return PALMD::R_2C8A_SPI_SHADER_PGM_RSRC1_GS;
109   case CallingConv::AMDGPU_VS:
110     return PALMD::R_2C4A_SPI_SHADER_PGM_RSRC1_VS;
111   case CallingConv::AMDGPU_PS:
112     return PALMD::R_2C0A_SPI_SHADER_PGM_RSRC1_PS;
113   }
114 }
115 
116 // Calculate the PAL metadata key for *S_SCRATCH_SIZE. It can be used
117 // with a constant offset to access any non-register shader-specific PAL
118 // metadata key.
119 static unsigned getScratchSizeKey(CallingConv::ID CC) {
120   switch (CC) {
121   case CallingConv::AMDGPU_PS:
122     return PALMD::Key::PS_SCRATCH_SIZE;
123   case CallingConv::AMDGPU_VS:
124     return PALMD::Key::VS_SCRATCH_SIZE;
125   case CallingConv::AMDGPU_GS:
126     return PALMD::Key::GS_SCRATCH_SIZE;
127   case CallingConv::AMDGPU_ES:
128     return PALMD::Key::ES_SCRATCH_SIZE;
129   case CallingConv::AMDGPU_HS:
130     return PALMD::Key::HS_SCRATCH_SIZE;
131   case CallingConv::AMDGPU_LS:
132     return PALMD::Key::LS_SCRATCH_SIZE;
133   default:
134     return PALMD::Key::CS_SCRATCH_SIZE;
135   }
136 }
137 
138 // Set the rsrc1 register in the metadata for a particular shader stage.
139 // In fact this ORs the value into any previous setting of the register.
140 void AMDGPUPALMetadata::setRsrc1(CallingConv::ID CC, unsigned Val) {
141   setRegister(getRsrc1Reg(CC), Val);
142 }
143 
144 // Set the rsrc2 register in the metadata for a particular shader stage.
145 // In fact this ORs the value into any previous setting of the register.
146 void AMDGPUPALMetadata::setRsrc2(CallingConv::ID CC, unsigned Val) {
147   setRegister(getRsrc1Reg(CC) + 1, Val);
148 }
149 
150 // Set the SPI_PS_INPUT_ENA register in the metadata.
151 // In fact this ORs the value into any previous setting of the register.
152 void AMDGPUPALMetadata::setSpiPsInputEna(unsigned Val) {
153   setRegister(PALMD::R_A1B3_SPI_PS_INPUT_ENA, Val);
154 }
155 
156 // Set the SPI_PS_INPUT_ADDR register in the metadata.
157 // In fact this ORs the value into any previous setting of the register.
158 void AMDGPUPALMetadata::setSpiPsInputAddr(unsigned Val) {
159   setRegister(PALMD::R_A1B4_SPI_PS_INPUT_ADDR, Val);
160 }
161 
162 // Get a register from the metadata, or 0 if not currently set.
163 unsigned AMDGPUPALMetadata::getRegister(unsigned Reg) {
164   auto Regs = getRegisters();
165   auto It = Regs.find(MsgPackDoc.getNode(Reg));
166   if (It == Regs.end())
167     return 0;
168   auto N = It->second;
169   if (N.getKind() != msgpack::Type::UInt)
170     return 0;
171   return N.getUInt();
172 }
173 
174 // Set a register in the metadata.
175 // In fact this ORs the value into any previous setting of the register.
176 void AMDGPUPALMetadata::setRegister(unsigned Reg, unsigned Val) {
177   if (!isLegacy()) {
178     // In the new MsgPack format, ignore register numbered >= 0x10000000. It
179     // is a PAL ABI pseudo-register in the old non-MsgPack format.
180     if (Reg >= 0x10000000)
181       return;
182   }
183   auto &N = getRegisters()[MsgPackDoc.getNode(Reg)];
184   if (N.getKind() == msgpack::Type::UInt)
185     Val |= N.getUInt();
186   N = N.getDocument()->getNode(Val);
187 }
188 
189 // Set the entry point name for one shader.
190 void AMDGPUPALMetadata::setEntryPoint(unsigned CC, StringRef Name) {
191   if (isLegacy())
192     return;
193   // Msgpack format.
194   getHwStage(CC)[".entry_point"] = MsgPackDoc.getNode(Name, /*Copy=*/true);
195 }
196 
197 // Set the number of used vgprs in the metadata. This is an optional
198 // advisory record for logging etc; wave dispatch actually uses the rsrc1
199 // register for the shader stage to determine the number of vgprs to
200 // allocate.
201 void AMDGPUPALMetadata::setNumUsedVgprs(CallingConv::ID CC, unsigned Val) {
202   if (isLegacy()) {
203     // Old non-msgpack format.
204     unsigned NumUsedVgprsKey = getScratchSizeKey(CC) +
205                                PALMD::Key::VS_NUM_USED_VGPRS -
206                                PALMD::Key::VS_SCRATCH_SIZE;
207     setRegister(NumUsedVgprsKey, Val);
208     return;
209   }
210   // Msgpack format.
211   getHwStage(CC)[".vgpr_count"] = MsgPackDoc.getNode(Val);
212 }
213 
214 // Set the number of used sgprs in the metadata. This is an optional advisory
215 // record for logging etc; wave dispatch actually uses the rsrc1 register for
216 // the shader stage to determine the number of sgprs to allocate.
217 void AMDGPUPALMetadata::setNumUsedSgprs(CallingConv::ID CC, unsigned Val) {
218   if (isLegacy()) {
219     // Old non-msgpack format.
220     unsigned NumUsedSgprsKey = getScratchSizeKey(CC) +
221                                PALMD::Key::VS_NUM_USED_SGPRS -
222                                PALMD::Key::VS_SCRATCH_SIZE;
223     setRegister(NumUsedSgprsKey, Val);
224     return;
225   }
226   // Msgpack format.
227   getHwStage(CC)[".sgpr_count"] = MsgPackDoc.getNode(Val);
228 }
229 
230 // Set the scratch size in the metadata.
231 void AMDGPUPALMetadata::setScratchSize(CallingConv::ID CC, unsigned Val) {
232   if (isLegacy()) {
233     // Old non-msgpack format.
234     setRegister(getScratchSizeKey(CC), Val);
235     return;
236   }
237   // Msgpack format.
238   getHwStage(CC)[".scratch_memory_size"] = MsgPackDoc.getNode(Val);
239 }
240 
241 // Set the scratch size in the metadata.
242 void AMDGPUPALMetadata::setStackFrameSize(const MachineFunction &MF,
243                                           unsigned Val) {
244   auto Node = MsgPackDoc.getMapNode();
245   Node[".stack_frame_size_in_bytes"] = MsgPackDoc.getNode(Val);
246   getShaderFunctions()[MF.getFunction().getName()] = Node;
247 }
248 
249 // Set the hardware register bit in PAL metadata to enable wave32 on the
250 // shader of the given calling convention.
251 void AMDGPUPALMetadata::setWave32(unsigned CC) {
252   switch (CC) {
253   case CallingConv::AMDGPU_HS:
254     setRegister(PALMD::R_A2D5_VGT_SHADER_STAGES_EN, S_028B54_HS_W32_EN(1));
255     break;
256   case CallingConv::AMDGPU_GS:
257     setRegister(PALMD::R_A2D5_VGT_SHADER_STAGES_EN, S_028B54_GS_W32_EN(1));
258     break;
259   case CallingConv::AMDGPU_VS:
260     setRegister(PALMD::R_A2D5_VGT_SHADER_STAGES_EN, S_028B54_VS_W32_EN(1));
261     break;
262   case CallingConv::AMDGPU_PS:
263     setRegister(PALMD::R_A1B6_SPI_PS_IN_CONTROL, S_0286D8_PS_W32_EN(1));
264     break;
265   case CallingConv::AMDGPU_CS:
266     setRegister(PALMD::R_2E00_COMPUTE_DISPATCH_INITIATOR,
267                 S_00B800_CS_W32_EN(1));
268     break;
269   }
270 }
271 
272 // Convert a register number to name, for display by toString().
273 // Returns nullptr if none.
274 static const char *getRegisterName(unsigned RegNum) {
275   // Table of registers.
276   static const struct RegInfo {
277     unsigned Num;
278     const char *Name;
279   } RegInfoTable[] = {
280       // Registers that code generation sets/modifies metadata for.
281       {PALMD::R_2C4A_SPI_SHADER_PGM_RSRC1_VS, "SPI_SHADER_PGM_RSRC1_VS"},
282       {PALMD::R_2C4A_SPI_SHADER_PGM_RSRC1_VS + 1, "SPI_SHADER_PGM_RSRC2_VS"},
283       {PALMD::R_2D4A_SPI_SHADER_PGM_RSRC1_LS, "SPI_SHADER_PGM_RSRC1_LS"},
284       {PALMD::R_2D4A_SPI_SHADER_PGM_RSRC1_LS + 1, "SPI_SHADER_PGM_RSRC2_LS"},
285       {PALMD::R_2D0A_SPI_SHADER_PGM_RSRC1_HS, "SPI_SHADER_PGM_RSRC1_HS"},
286       {PALMD::R_2D0A_SPI_SHADER_PGM_RSRC1_HS + 1, "SPI_SHADER_PGM_RSRC2_HS"},
287       {PALMD::R_2CCA_SPI_SHADER_PGM_RSRC1_ES, "SPI_SHADER_PGM_RSRC1_ES"},
288       {PALMD::R_2CCA_SPI_SHADER_PGM_RSRC1_ES + 1, "SPI_SHADER_PGM_RSRC2_ES"},
289       {PALMD::R_2C8A_SPI_SHADER_PGM_RSRC1_GS, "SPI_SHADER_PGM_RSRC1_GS"},
290       {PALMD::R_2C8A_SPI_SHADER_PGM_RSRC1_GS + 1, "SPI_SHADER_PGM_RSRC2_GS"},
291       {PALMD::R_2E00_COMPUTE_DISPATCH_INITIATOR, "COMPUTE_DISPATCH_INITIATOR"},
292       {PALMD::R_2E12_COMPUTE_PGM_RSRC1, "COMPUTE_PGM_RSRC1"},
293       {PALMD::R_2E12_COMPUTE_PGM_RSRC1 + 1, "COMPUTE_PGM_RSRC2"},
294       {PALMD::R_2C0A_SPI_SHADER_PGM_RSRC1_PS, "SPI_SHADER_PGM_RSRC1_PS"},
295       {PALMD::R_2C0A_SPI_SHADER_PGM_RSRC1_PS + 1, "SPI_SHADER_PGM_RSRC2_PS"},
296       {PALMD::R_A1B3_SPI_PS_INPUT_ENA, "SPI_PS_INPUT_ENA"},
297       {PALMD::R_A1B4_SPI_PS_INPUT_ADDR, "SPI_PS_INPUT_ADDR"},
298       {PALMD::R_A1B6_SPI_PS_IN_CONTROL, "SPI_PS_IN_CONTROL"},
299       {PALMD::R_A2D5_VGT_SHADER_STAGES_EN, "VGT_SHADER_STAGES_EN"},
300 
301       // Registers not known to code generation.
302       {0x2c07, "SPI_SHADER_PGM_RSRC3_PS"},
303       {0x2c46, "SPI_SHADER_PGM_RSRC3_VS"},
304       {0x2c87, "SPI_SHADER_PGM_RSRC3_GS"},
305       {0x2cc7, "SPI_SHADER_PGM_RSRC3_ES"},
306       {0x2d07, "SPI_SHADER_PGM_RSRC3_HS"},
307       {0x2d47, "SPI_SHADER_PGM_RSRC3_LS"},
308 
309       {0xa1c3, "SPI_SHADER_POS_FORMAT"},
310       {0xa1b1, "SPI_VS_OUT_CONFIG"},
311       {0xa207, "PA_CL_VS_OUT_CNTL"},
312       {0xa204, "PA_CL_CLIP_CNTL"},
313       {0xa206, "PA_CL_VTE_CNTL"},
314       {0xa2f9, "PA_SU_VTX_CNTL"},
315       {0xa293, "PA_SC_MODE_CNTL_1"},
316       {0xa2a1, "VGT_PRIMITIVEID_EN"},
317       {0x2c81, "SPI_SHADER_PGM_RSRC4_GS"},
318       {0x2e18, "COMPUTE_TMPRING_SIZE"},
319       {0xa1b5, "SPI_INTERP_CONTROL_0"},
320       {0xa1ba, "SPI_TMPRING_SIZE"},
321       {0xa1c4, "SPI_SHADER_Z_FORMAT"},
322       {0xa1c5, "SPI_SHADER_COL_FORMAT"},
323       {0xa203, "DB_SHADER_CONTROL"},
324       {0xa08f, "CB_SHADER_MASK"},
325       {0xa191, "SPI_PS_INPUT_CNTL_0"},
326       {0xa192, "SPI_PS_INPUT_CNTL_1"},
327       {0xa193, "SPI_PS_INPUT_CNTL_2"},
328       {0xa194, "SPI_PS_INPUT_CNTL_3"},
329       {0xa195, "SPI_PS_INPUT_CNTL_4"},
330       {0xa196, "SPI_PS_INPUT_CNTL_5"},
331       {0xa197, "SPI_PS_INPUT_CNTL_6"},
332       {0xa198, "SPI_PS_INPUT_CNTL_7"},
333       {0xa199, "SPI_PS_INPUT_CNTL_8"},
334       {0xa19a, "SPI_PS_INPUT_CNTL_9"},
335       {0xa19b, "SPI_PS_INPUT_CNTL_10"},
336       {0xa19c, "SPI_PS_INPUT_CNTL_11"},
337       {0xa19d, "SPI_PS_INPUT_CNTL_12"},
338       {0xa19e, "SPI_PS_INPUT_CNTL_13"},
339       {0xa19f, "SPI_PS_INPUT_CNTL_14"},
340       {0xa1a0, "SPI_PS_INPUT_CNTL_15"},
341       {0xa1a1, "SPI_PS_INPUT_CNTL_16"},
342       {0xa1a2, "SPI_PS_INPUT_CNTL_17"},
343       {0xa1a3, "SPI_PS_INPUT_CNTL_18"},
344       {0xa1a4, "SPI_PS_INPUT_CNTL_19"},
345       {0xa1a5, "SPI_PS_INPUT_CNTL_20"},
346       {0xa1a6, "SPI_PS_INPUT_CNTL_21"},
347       {0xa1a7, "SPI_PS_INPUT_CNTL_22"},
348       {0xa1a8, "SPI_PS_INPUT_CNTL_23"},
349       {0xa1a9, "SPI_PS_INPUT_CNTL_24"},
350       {0xa1aa, "SPI_PS_INPUT_CNTL_25"},
351       {0xa1ab, "SPI_PS_INPUT_CNTL_26"},
352       {0xa1ac, "SPI_PS_INPUT_CNTL_27"},
353       {0xa1ad, "SPI_PS_INPUT_CNTL_28"},
354       {0xa1ae, "SPI_PS_INPUT_CNTL_29"},
355       {0xa1af, "SPI_PS_INPUT_CNTL_30"},
356       {0xa1b0, "SPI_PS_INPUT_CNTL_31"},
357 
358       {0xa2ce, "VGT_GS_MAX_VERT_OUT"},
359       {0xa2ab, "VGT_ESGS_RING_ITEMSIZE"},
360       {0xa290, "VGT_GS_MODE"},
361       {0xa291, "VGT_GS_ONCHIP_CNTL"},
362       {0xa2d7, "VGT_GS_VERT_ITEMSIZE"},
363       {0xa2d8, "VGT_GS_VERT_ITEMSIZE_1"},
364       {0xa2d9, "VGT_GS_VERT_ITEMSIZE_2"},
365       {0xa2da, "VGT_GS_VERT_ITEMSIZE_3"},
366       {0xa298, "VGT_GSVS_RING_OFFSET_1"},
367       {0xa299, "VGT_GSVS_RING_OFFSET_2"},
368       {0xa29a, "VGT_GSVS_RING_OFFSET_3"},
369 
370       {0xa2e4, "VGT_GS_INSTANCE_CNT"},
371       {0xa297, "VGT_GS_PER_VS"},
372       {0xa29b, "VGT_GS_OUT_PRIM_TYPE"},
373       {0xa2ac, "VGT_GSVS_RING_ITEMSIZE"},
374 
375       {0xa2ad, "VGT_REUSE_OFF"},
376       {0xa1b8, "SPI_BARYC_CNTL"},
377 
378       {0x2c4c, "SPI_SHADER_USER_DATA_VS_0"},
379       {0x2c4d, "SPI_SHADER_USER_DATA_VS_1"},
380       {0x2c4e, "SPI_SHADER_USER_DATA_VS_2"},
381       {0x2c4f, "SPI_SHADER_USER_DATA_VS_3"},
382       {0x2c50, "SPI_SHADER_USER_DATA_VS_4"},
383       {0x2c51, "SPI_SHADER_USER_DATA_VS_5"},
384       {0x2c52, "SPI_SHADER_USER_DATA_VS_6"},
385       {0x2c53, "SPI_SHADER_USER_DATA_VS_7"},
386       {0x2c54, "SPI_SHADER_USER_DATA_VS_8"},
387       {0x2c55, "SPI_SHADER_USER_DATA_VS_9"},
388       {0x2c56, "SPI_SHADER_USER_DATA_VS_10"},
389       {0x2c57, "SPI_SHADER_USER_DATA_VS_11"},
390       {0x2c58, "SPI_SHADER_USER_DATA_VS_12"},
391       {0x2c59, "SPI_SHADER_USER_DATA_VS_13"},
392       {0x2c5a, "SPI_SHADER_USER_DATA_VS_14"},
393       {0x2c5b, "SPI_SHADER_USER_DATA_VS_15"},
394       {0x2c5c, "SPI_SHADER_USER_DATA_VS_16"},
395       {0x2c5d, "SPI_SHADER_USER_DATA_VS_17"},
396       {0x2c5e, "SPI_SHADER_USER_DATA_VS_18"},
397       {0x2c5f, "SPI_SHADER_USER_DATA_VS_19"},
398       {0x2c60, "SPI_SHADER_USER_DATA_VS_20"},
399       {0x2c61, "SPI_SHADER_USER_DATA_VS_21"},
400       {0x2c62, "SPI_SHADER_USER_DATA_VS_22"},
401       {0x2c63, "SPI_SHADER_USER_DATA_VS_23"},
402       {0x2c64, "SPI_SHADER_USER_DATA_VS_24"},
403       {0x2c65, "SPI_SHADER_USER_DATA_VS_25"},
404       {0x2c66, "SPI_SHADER_USER_DATA_VS_26"},
405       {0x2c67, "SPI_SHADER_USER_DATA_VS_27"},
406       {0x2c68, "SPI_SHADER_USER_DATA_VS_28"},
407       {0x2c69, "SPI_SHADER_USER_DATA_VS_29"},
408       {0x2c6a, "SPI_SHADER_USER_DATA_VS_30"},
409       {0x2c6b, "SPI_SHADER_USER_DATA_VS_31"},
410 
411       {0x2c8c, "SPI_SHADER_USER_DATA_GS_0"},
412       {0x2c8d, "SPI_SHADER_USER_DATA_GS_1"},
413       {0x2c8e, "SPI_SHADER_USER_DATA_GS_2"},
414       {0x2c8f, "SPI_SHADER_USER_DATA_GS_3"},
415       {0x2c90, "SPI_SHADER_USER_DATA_GS_4"},
416       {0x2c91, "SPI_SHADER_USER_DATA_GS_5"},
417       {0x2c92, "SPI_SHADER_USER_DATA_GS_6"},
418       {0x2c93, "SPI_SHADER_USER_DATA_GS_7"},
419       {0x2c94, "SPI_SHADER_USER_DATA_GS_8"},
420       {0x2c95, "SPI_SHADER_USER_DATA_GS_9"},
421       {0x2c96, "SPI_SHADER_USER_DATA_GS_10"},
422       {0x2c97, "SPI_SHADER_USER_DATA_GS_11"},
423       {0x2c98, "SPI_SHADER_USER_DATA_GS_12"},
424       {0x2c99, "SPI_SHADER_USER_DATA_GS_13"},
425       {0x2c9a, "SPI_SHADER_USER_DATA_GS_14"},
426       {0x2c9b, "SPI_SHADER_USER_DATA_GS_15"},
427       {0x2c9c, "SPI_SHADER_USER_DATA_GS_16"},
428       {0x2c9d, "SPI_SHADER_USER_DATA_GS_17"},
429       {0x2c9e, "SPI_SHADER_USER_DATA_GS_18"},
430       {0x2c9f, "SPI_SHADER_USER_DATA_GS_19"},
431       {0x2ca0, "SPI_SHADER_USER_DATA_GS_20"},
432       {0x2ca1, "SPI_SHADER_USER_DATA_GS_21"},
433       {0x2ca2, "SPI_SHADER_USER_DATA_GS_22"},
434       {0x2ca3, "SPI_SHADER_USER_DATA_GS_23"},
435       {0x2ca4, "SPI_SHADER_USER_DATA_GS_24"},
436       {0x2ca5, "SPI_SHADER_USER_DATA_GS_25"},
437       {0x2ca6, "SPI_SHADER_USER_DATA_GS_26"},
438       {0x2ca7, "SPI_SHADER_USER_DATA_GS_27"},
439       {0x2ca8, "SPI_SHADER_USER_DATA_GS_28"},
440       {0x2ca9, "SPI_SHADER_USER_DATA_GS_29"},
441       {0x2caa, "SPI_SHADER_USER_DATA_GS_30"},
442       {0x2cab, "SPI_SHADER_USER_DATA_GS_31"},
443 
444       {0x2ccc, "SPI_SHADER_USER_DATA_ES_0"},
445       {0x2ccd, "SPI_SHADER_USER_DATA_ES_1"},
446       {0x2cce, "SPI_SHADER_USER_DATA_ES_2"},
447       {0x2ccf, "SPI_SHADER_USER_DATA_ES_3"},
448       {0x2cd0, "SPI_SHADER_USER_DATA_ES_4"},
449       {0x2cd1, "SPI_SHADER_USER_DATA_ES_5"},
450       {0x2cd2, "SPI_SHADER_USER_DATA_ES_6"},
451       {0x2cd3, "SPI_SHADER_USER_DATA_ES_7"},
452       {0x2cd4, "SPI_SHADER_USER_DATA_ES_8"},
453       {0x2cd5, "SPI_SHADER_USER_DATA_ES_9"},
454       {0x2cd6, "SPI_SHADER_USER_DATA_ES_10"},
455       {0x2cd7, "SPI_SHADER_USER_DATA_ES_11"},
456       {0x2cd8, "SPI_SHADER_USER_DATA_ES_12"},
457       {0x2cd9, "SPI_SHADER_USER_DATA_ES_13"},
458       {0x2cda, "SPI_SHADER_USER_DATA_ES_14"},
459       {0x2cdb, "SPI_SHADER_USER_DATA_ES_15"},
460       {0x2cdc, "SPI_SHADER_USER_DATA_ES_16"},
461       {0x2cdd, "SPI_SHADER_USER_DATA_ES_17"},
462       {0x2cde, "SPI_SHADER_USER_DATA_ES_18"},
463       {0x2cdf, "SPI_SHADER_USER_DATA_ES_19"},
464       {0x2ce0, "SPI_SHADER_USER_DATA_ES_20"},
465       {0x2ce1, "SPI_SHADER_USER_DATA_ES_21"},
466       {0x2ce2, "SPI_SHADER_USER_DATA_ES_22"},
467       {0x2ce3, "SPI_SHADER_USER_DATA_ES_23"},
468       {0x2ce4, "SPI_SHADER_USER_DATA_ES_24"},
469       {0x2ce5, "SPI_SHADER_USER_DATA_ES_25"},
470       {0x2ce6, "SPI_SHADER_USER_DATA_ES_26"},
471       {0x2ce7, "SPI_SHADER_USER_DATA_ES_27"},
472       {0x2ce8, "SPI_SHADER_USER_DATA_ES_28"},
473       {0x2ce9, "SPI_SHADER_USER_DATA_ES_29"},
474       {0x2cea, "SPI_SHADER_USER_DATA_ES_30"},
475       {0x2ceb, "SPI_SHADER_USER_DATA_ES_31"},
476 
477       {0x2c0c, "SPI_SHADER_USER_DATA_PS_0"},
478       {0x2c0d, "SPI_SHADER_USER_DATA_PS_1"},
479       {0x2c0e, "SPI_SHADER_USER_DATA_PS_2"},
480       {0x2c0f, "SPI_SHADER_USER_DATA_PS_3"},
481       {0x2c10, "SPI_SHADER_USER_DATA_PS_4"},
482       {0x2c11, "SPI_SHADER_USER_DATA_PS_5"},
483       {0x2c12, "SPI_SHADER_USER_DATA_PS_6"},
484       {0x2c13, "SPI_SHADER_USER_DATA_PS_7"},
485       {0x2c14, "SPI_SHADER_USER_DATA_PS_8"},
486       {0x2c15, "SPI_SHADER_USER_DATA_PS_9"},
487       {0x2c16, "SPI_SHADER_USER_DATA_PS_10"},
488       {0x2c17, "SPI_SHADER_USER_DATA_PS_11"},
489       {0x2c18, "SPI_SHADER_USER_DATA_PS_12"},
490       {0x2c19, "SPI_SHADER_USER_DATA_PS_13"},
491       {0x2c1a, "SPI_SHADER_USER_DATA_PS_14"},
492       {0x2c1b, "SPI_SHADER_USER_DATA_PS_15"},
493       {0x2c1c, "SPI_SHADER_USER_DATA_PS_16"},
494       {0x2c1d, "SPI_SHADER_USER_DATA_PS_17"},
495       {0x2c1e, "SPI_SHADER_USER_DATA_PS_18"},
496       {0x2c1f, "SPI_SHADER_USER_DATA_PS_19"},
497       {0x2c20, "SPI_SHADER_USER_DATA_PS_20"},
498       {0x2c21, "SPI_SHADER_USER_DATA_PS_21"},
499       {0x2c22, "SPI_SHADER_USER_DATA_PS_22"},
500       {0x2c23, "SPI_SHADER_USER_DATA_PS_23"},
501       {0x2c24, "SPI_SHADER_USER_DATA_PS_24"},
502       {0x2c25, "SPI_SHADER_USER_DATA_PS_25"},
503       {0x2c26, "SPI_SHADER_USER_DATA_PS_26"},
504       {0x2c27, "SPI_SHADER_USER_DATA_PS_27"},
505       {0x2c28, "SPI_SHADER_USER_DATA_PS_28"},
506       {0x2c29, "SPI_SHADER_USER_DATA_PS_29"},
507       {0x2c2a, "SPI_SHADER_USER_DATA_PS_30"},
508       {0x2c2b, "SPI_SHADER_USER_DATA_PS_31"},
509 
510       {0x2e40, "COMPUTE_USER_DATA_0"},
511       {0x2e41, "COMPUTE_USER_DATA_1"},
512       {0x2e42, "COMPUTE_USER_DATA_2"},
513       {0x2e43, "COMPUTE_USER_DATA_3"},
514       {0x2e44, "COMPUTE_USER_DATA_4"},
515       {0x2e45, "COMPUTE_USER_DATA_5"},
516       {0x2e46, "COMPUTE_USER_DATA_6"},
517       {0x2e47, "COMPUTE_USER_DATA_7"},
518       {0x2e48, "COMPUTE_USER_DATA_8"},
519       {0x2e49, "COMPUTE_USER_DATA_9"},
520       {0x2e4a, "COMPUTE_USER_DATA_10"},
521       {0x2e4b, "COMPUTE_USER_DATA_11"},
522       {0x2e4c, "COMPUTE_USER_DATA_12"},
523       {0x2e4d, "COMPUTE_USER_DATA_13"},
524       {0x2e4e, "COMPUTE_USER_DATA_14"},
525       {0x2e4f, "COMPUTE_USER_DATA_15"},
526 
527       {0x2e07, "COMPUTE_NUM_THREAD_X"},
528       {0x2e08, "COMPUTE_NUM_THREAD_Y"},
529       {0x2e09, "COMPUTE_NUM_THREAD_Z"},
530       {0xa2db, "VGT_TF_PARAM"},
531       {0xa2d6, "VGT_LS_HS_CONFIG"},
532       {0xa287, "VGT_HOS_MIN_TESS_LEVEL"},
533       {0xa286, "VGT_HOS_MAX_TESS_LEVEL"},
534       {0xa2f8, "PA_SC_AA_CONFIG"},
535       {0xa310, "PA_SC_SHADER_CONTROL"},
536       {0xa313, "PA_SC_CONSERVATIVE_RASTERIZATION_CNTL"},
537 
538       {0x2d0c, "SPI_SHADER_USER_DATA_HS_0"},
539       {0x2d0d, "SPI_SHADER_USER_DATA_HS_1"},
540       {0x2d0e, "SPI_SHADER_USER_DATA_HS_2"},
541       {0x2d0f, "SPI_SHADER_USER_DATA_HS_3"},
542       {0x2d10, "SPI_SHADER_USER_DATA_HS_4"},
543       {0x2d11, "SPI_SHADER_USER_DATA_HS_5"},
544       {0x2d12, "SPI_SHADER_USER_DATA_HS_6"},
545       {0x2d13, "SPI_SHADER_USER_DATA_HS_7"},
546       {0x2d14, "SPI_SHADER_USER_DATA_HS_8"},
547       {0x2d15, "SPI_SHADER_USER_DATA_HS_9"},
548       {0x2d16, "SPI_SHADER_USER_DATA_HS_10"},
549       {0x2d17, "SPI_SHADER_USER_DATA_HS_11"},
550       {0x2d18, "SPI_SHADER_USER_DATA_HS_12"},
551       {0x2d19, "SPI_SHADER_USER_DATA_HS_13"},
552       {0x2d1a, "SPI_SHADER_USER_DATA_HS_14"},
553       {0x2d1b, "SPI_SHADER_USER_DATA_HS_15"},
554       {0x2d1c, "SPI_SHADER_USER_DATA_HS_16"},
555       {0x2d1d, "SPI_SHADER_USER_DATA_HS_17"},
556       {0x2d1e, "SPI_SHADER_USER_DATA_HS_18"},
557       {0x2d1f, "SPI_SHADER_USER_DATA_HS_19"},
558       {0x2d20, "SPI_SHADER_USER_DATA_HS_20"},
559       {0x2d21, "SPI_SHADER_USER_DATA_HS_21"},
560       {0x2d22, "SPI_SHADER_USER_DATA_HS_22"},
561       {0x2d23, "SPI_SHADER_USER_DATA_HS_23"},
562       {0x2d24, "SPI_SHADER_USER_DATA_HS_24"},
563       {0x2d25, "SPI_SHADER_USER_DATA_HS_25"},
564       {0x2d26, "SPI_SHADER_USER_DATA_HS_26"},
565       {0x2d27, "SPI_SHADER_USER_DATA_HS_27"},
566       {0x2d28, "SPI_SHADER_USER_DATA_HS_28"},
567       {0x2d29, "SPI_SHADER_USER_DATA_HS_29"},
568       {0x2d2a, "SPI_SHADER_USER_DATA_HS_30"},
569       {0x2d2b, "SPI_SHADER_USER_DATA_HS_31"},
570 
571       {0x2d4c, "SPI_SHADER_USER_DATA_LS_0"},
572       {0x2d4d, "SPI_SHADER_USER_DATA_LS_1"},
573       {0x2d4e, "SPI_SHADER_USER_DATA_LS_2"},
574       {0x2d4f, "SPI_SHADER_USER_DATA_LS_3"},
575       {0x2d50, "SPI_SHADER_USER_DATA_LS_4"},
576       {0x2d51, "SPI_SHADER_USER_DATA_LS_5"},
577       {0x2d52, "SPI_SHADER_USER_DATA_LS_6"},
578       {0x2d53, "SPI_SHADER_USER_DATA_LS_7"},
579       {0x2d54, "SPI_SHADER_USER_DATA_LS_8"},
580       {0x2d55, "SPI_SHADER_USER_DATA_LS_9"},
581       {0x2d56, "SPI_SHADER_USER_DATA_LS_10"},
582       {0x2d57, "SPI_SHADER_USER_DATA_LS_11"},
583       {0x2d58, "SPI_SHADER_USER_DATA_LS_12"},
584       {0x2d59, "SPI_SHADER_USER_DATA_LS_13"},
585       {0x2d5a, "SPI_SHADER_USER_DATA_LS_14"},
586       {0x2d5b, "SPI_SHADER_USER_DATA_LS_15"},
587 
588       {0xa2aa, "IA_MULTI_VGT_PARAM"},
589       {0xa2a5, "VGT_GS_MAX_PRIMS_PER_SUBGROUP"},
590       {0xa2e6, "VGT_STRMOUT_BUFFER_CONFIG"},
591       {0xa2e5, "VGT_STRMOUT_CONFIG"},
592       {0xa2b5, "VGT_STRMOUT_VTX_STRIDE_0"},
593       {0xa2b9, "VGT_STRMOUT_VTX_STRIDE_1"},
594       {0xa2bd, "VGT_STRMOUT_VTX_STRIDE_2"},
595       {0xa2c1, "VGT_STRMOUT_VTX_STRIDE_3"},
596       {0xa316, "VGT_VERTEX_REUSE_BLOCK_CNTL"},
597 
598       {0, nullptr}};
599   auto Entry = RegInfoTable;
600   for (; Entry->Num && Entry->Num != RegNum; ++Entry)
601     ;
602   return Entry->Name;
603 }
604 
605 // Convert the accumulated PAL metadata into an asm directive.
606 void AMDGPUPALMetadata::toString(std::string &String) {
607   String.clear();
608   if (!BlobType)
609     return;
610   raw_string_ostream Stream(String);
611   if (isLegacy()) {
612     if (MsgPackDoc.getRoot().getKind() == msgpack::Type::Nil)
613       return;
614     // Old linear reg=val format.
615     Stream << '\t' << AMDGPU::PALMD::AssemblerDirective << ' ';
616     auto Regs = getRegisters();
617     for (auto I = Regs.begin(), E = Regs.end(); I != E; ++I) {
618       if (I != Regs.begin())
619         Stream << ',';
620       unsigned Reg = I->first.getUInt();
621       unsigned Val = I->second.getUInt();
622       Stream << "0x" << Twine::utohexstr(Reg) << ",0x" << Twine::utohexstr(Val);
623     }
624     Stream << '\n';
625     return;
626   }
627 
628   // New msgpack-based format -- output as YAML (with unsigned numbers in hex),
629   // but first change the registers map to use names.
630   MsgPackDoc.setHexMode();
631   auto &RegsObj = refRegisters();
632   auto OrigRegs = RegsObj.getMap();
633   RegsObj = MsgPackDoc.getMapNode();
634   for (auto I : OrigRegs) {
635     auto Key = I.first;
636     if (const char *RegName = getRegisterName(Key.getUInt())) {
637       std::string KeyName = Key.toString();
638       KeyName += " (";
639       KeyName += RegName;
640       KeyName += ')';
641       Key = MsgPackDoc.getNode(KeyName, /*Copy=*/true);
642     }
643     RegsObj.getMap()[Key] = I.second;
644   }
645 
646   // Output as YAML.
647   Stream << '\t' << AMDGPU::PALMD::AssemblerDirectiveBegin << '\n';
648   MsgPackDoc.toYAML(Stream);
649   Stream << '\t' << AMDGPU::PALMD::AssemblerDirectiveEnd << '\n';
650 
651   // Restore original registers map.
652   RegsObj = OrigRegs;
653 }
654 
655 // Convert the accumulated PAL metadata into a binary blob for writing as
656 // a .note record of the specified AMD type. Returns an empty blob if
657 // there is no PAL metadata,
658 void AMDGPUPALMetadata::toBlob(unsigned Type, std::string &Blob) {
659   if (Type == ELF::NT_AMD_AMDGPU_PAL_METADATA)
660     toLegacyBlob(Blob);
661   else if (Type)
662     toMsgPackBlob(Blob);
663 }
664 
665 void AMDGPUPALMetadata::toLegacyBlob(std::string &Blob) {
666   Blob.clear();
667   auto Registers = getRegisters();
668   if (Registers.getMap().empty())
669     return;
670   raw_string_ostream OS(Blob);
671   support::endian::Writer EW(OS, support::endianness::little);
672   for (auto I : Registers.getMap()) {
673     EW.write(uint32_t(I.first.getUInt()));
674     EW.write(uint32_t(I.second.getUInt()));
675   }
676 }
677 
678 void AMDGPUPALMetadata::toMsgPackBlob(std::string &Blob) {
679   Blob.clear();
680   MsgPackDoc.writeToBlob(Blob);
681 }
682 
683 // Set PAL metadata from YAML text. Returns false if failed.
684 bool AMDGPUPALMetadata::setFromString(StringRef S) {
685   BlobType = ELF::NT_AMDGPU_METADATA;
686   if (!MsgPackDoc.fromYAML(S))
687     return false;
688 
689   // In the registers map, some keys may be of the form "0xa191
690   // (SPI_PS_INPUT_CNTL_0)", in which case the YAML input code made it a
691   // string. We need to turn it into a number.
692   auto &RegsObj = refRegisters();
693   auto OrigRegs = RegsObj;
694   RegsObj = MsgPackDoc.getMapNode();
695   Registers = RegsObj.getMap();
696   bool Ok = true;
697   for (auto I : OrigRegs.getMap()) {
698     auto Key = I.first;
699     if (Key.getKind() == msgpack::Type::String) {
700       StringRef S = Key.getString();
701       uint64_t Val;
702       if (S.consumeInteger(0, Val)) {
703         Ok = false;
704         errs() << "Unrecognized PAL metadata register key '" << S << "'\n";
705         continue;
706       }
707       Key = MsgPackDoc.getNode(uint64_t(Val));
708     }
709     Registers.getMap()[Key] = I.second;
710   }
711   return Ok;
712 }
713 
714 // Reference (create if necessary) the node for the registers map.
715 msgpack::DocNode &AMDGPUPALMetadata::refRegisters() {
716   auto &N =
717       MsgPackDoc.getRoot()
718           .getMap(/*Convert=*/true)[MsgPackDoc.getNode("amdpal.pipelines")]
719           .getArray(/*Convert=*/true)[0]
720           .getMap(/*Convert=*/true)[MsgPackDoc.getNode(".registers")];
721   N.getMap(/*Convert=*/true);
722   return N;
723 }
724 
725 // Get (create if necessary) the registers map.
726 msgpack::MapDocNode AMDGPUPALMetadata::getRegisters() {
727   if (Registers.isEmpty())
728     Registers = refRegisters();
729   return Registers.getMap();
730 }
731 
732 // Reference (create if necessary) the node for the shader functions map.
733 msgpack::DocNode &AMDGPUPALMetadata::refShaderFunctions() {
734   auto &N =
735       MsgPackDoc.getRoot()
736           .getMap(/*Convert=*/true)[MsgPackDoc.getNode("amdpal.pipelines")]
737           .getArray(/*Convert=*/true)[0]
738           .getMap(/*Convert=*/true)[MsgPackDoc.getNode(".shader_functions")];
739   N.getMap(/*Convert=*/true);
740   return N;
741 }
742 
743 // Get (create if necessary) the shader functions map.
744 msgpack::MapDocNode AMDGPUPALMetadata::getShaderFunctions() {
745   if (ShaderFunctions.isEmpty())
746     ShaderFunctions = refShaderFunctions();
747   return ShaderFunctions.getMap();
748 }
749 
750 // Return the PAL metadata hardware shader stage name.
751 static const char *getStageName(CallingConv::ID CC) {
752   switch (CC) {
753   case CallingConv::AMDGPU_PS:
754     return ".ps";
755   case CallingConv::AMDGPU_VS:
756     return ".vs";
757   case CallingConv::AMDGPU_GS:
758     return ".gs";
759   case CallingConv::AMDGPU_ES:
760     return ".es";
761   case CallingConv::AMDGPU_HS:
762     return ".hs";
763   case CallingConv::AMDGPU_LS:
764     return ".ls";
765   case CallingConv::AMDGPU_Gfx:
766     llvm_unreachable("Callable shader has no hardware stage");
767   default:
768     return ".cs";
769   }
770 }
771 
772 // Get (create if necessary) the .hardware_stages entry for the given calling
773 // convention.
774 msgpack::MapDocNode AMDGPUPALMetadata::getHwStage(unsigned CC) {
775   if (HwStages.isEmpty())
776     HwStages = MsgPackDoc.getRoot()
777                    .getMap(/*Convert=*/true)["amdpal.pipelines"]
778                    .getArray(/*Convert=*/true)[0]
779                    .getMap(/*Convert=*/true)[".hardware_stages"]
780                    .getMap(/*Convert=*/true);
781   return HwStages.getMap()[getStageName(CC)].getMap(/*Convert=*/true);
782 }
783 
784 // Get .note record vendor name of metadata blob to be emitted.
785 const char *AMDGPUPALMetadata::getVendor() const {
786   return isLegacy() ? ElfNote::NoteNameV2 : ElfNote::NoteNameV3;
787 }
788 
789 // Get .note record type of metadata blob to be emitted:
790 // ELF::NT_AMD_AMDGPU_PAL_METADATA (legacy key=val format), or
791 // ELF::NT_AMDGPU_METADATA (MsgPack format), or
792 // 0 (no PAL metadata).
793 unsigned AMDGPUPALMetadata::getType() const {
794   return BlobType;
795 }
796 
797 // Return whether the blob type is legacy PAL metadata.
798 bool AMDGPUPALMetadata::isLegacy() const {
799   return BlobType == ELF::NT_AMD_AMDGPU_PAL_METADATA;
800 }
801 
802 // Set legacy PAL metadata format.
803 void AMDGPUPALMetadata::setLegacy() {
804   BlobType = ELF::NT_AMD_AMDGPU_PAL_METADATA;
805 }
806 
807 // Erase all PAL metadata.
808 void AMDGPUPALMetadata::reset() {
809   MsgPackDoc.clear();
810   Registers = MsgPackDoc.getEmptyNode();
811   HwStages = MsgPackDoc.getEmptyNode();
812 }
813