1//===-- AMDGPUPALMetadata.h - PAL metadata handling -------------*- C++ -*-===//
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/// PAL metadata handling
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H
15#define LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H
16#include "AMDGPUDelayedMCExpr.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/BinaryFormat/MsgPackDocument.h"
19#include "llvm/MC/MCContext.h"
20
21namespace llvm {
22
23class Module;
24
25class AMDGPUPALMetadata {
26public:
27 using RegisterExprMap = DenseMap<unsigned, const MCExpr *>;
28
29private:
30 unsigned BlobType = 0;
31 msgpack::Document MsgPackDoc;
32 msgpack::DocNode Registers;
33 msgpack::DocNode HwStages;
34 msgpack::DocNode ShaderFunctions;
35 bool VersionChecked = false;
36 msgpack::DocNode Version;
37 // From PAL version >= 3.0
38 msgpack::DocNode ComputeRegisters;
39 msgpack::DocNode GraphicsRegisters;
40
41 DelayedMCExprs DelayedExprs;
42 RegisterExprMap REM;
43 bool ResolvedAll = true;
44
45public:
46 // Read the amdgpu.pal.metadata supplied by the frontend, ready for
47 // per-function modification.
48 void readFromIR(Module &M);
49
50 // Set PAL metadata from a binary blob from the applicable .note record.
51 // Returns false if bad format. Blob must remain valid for the lifetime of
52 // the Metadata.
53 // Not used in-tree, but used by the out-of-tree amdgpu-dis disassembler.
54 bool setFromBlob(unsigned Type, StringRef Blob);
55
56 // Set the rsrc1 register in the metadata for a particular shader stage.
57 // In fact this ORs the value into any previous setting of the register.
58 void setRsrc1(unsigned CC, unsigned Val);
59 void setRsrc1(unsigned CC, const MCExpr *Val, MCContext &Ctx);
60
61 // Set the rsrc2 register in the metadata for a particular shader stage.
62 // In fact this ORs the value into any previous setting of the register.
63 void setRsrc2(unsigned CC, unsigned Val);
64 void setRsrc2(unsigned CC, const MCExpr *Val, MCContext &Ctx);
65
66 // Set the SPI_PS_INPUT_ENA register in the metadata.
67 // In fact this ORs the value into any previous setting of the register.
68 void setSpiPsInputEna(unsigned Val);
69
70 // Set the SPI_PS_INPUT_ADDR register in the metadata.
71 // In fact this ORs the value into any previous setting of the register.
72 void setSpiPsInputAddr(unsigned Val);
73
74 // Get a register from the metadata, or 0 if not currently set.
75 unsigned getRegister(unsigned Reg);
76
77 // Set a register in the metadata.
78 // In fact this ORs the value into any previous setting of the register.
79 void setRegister(unsigned Reg, unsigned Val);
80 void setRegister(unsigned Reg, const MCExpr *Val, MCContext &Ctx);
81
82 // Set the entry point name for one shader.
83 void setEntryPoint(unsigned CC, StringRef Name);
84
85 // Set the number of used vgprs in the metadata. This is an optional advisory
86 // record for logging etc; wave dispatch actually uses the rsrc1 register for
87 // the shader stage to determine the number of vgprs to allocate.
88 void setNumUsedVgprs(unsigned CC, unsigned Val);
89 void setNumUsedVgprs(unsigned CC, const MCExpr *Val, MCContext &Ctx);
90
91 // Set the number of used agprs in the metadata. This is an optional advisory
92 // record for logging etc;
93 void setNumUsedAgprs(unsigned CC, unsigned Val);
94 void setNumUsedAgprs(unsigned CC, const MCExpr *Val);
95
96 // Set the number of used sgprs in the metadata. This is an optional advisory
97 // record for logging etc; wave dispatch actually uses the rsrc1 register for
98 // the shader stage to determine the number of sgprs to allocate.
99 void setNumUsedSgprs(unsigned CC, unsigned Val);
100 void setNumUsedSgprs(unsigned CC, const MCExpr *Val, MCContext &Ctx);
101
102 // Set the scratch size in the metadata.
103 void setScratchSize(unsigned CC, unsigned Val);
104 void setScratchSize(unsigned CC, const MCExpr *Val, MCContext &Ctx);
105
106 // Set the backend stack size metadata, including the frames of the known
107 // non-chain callees (and unknown if `-amdgpu-assume-external-call-stack-size`
108 // is used). Recursion and indirect/external calls must be handled by
109 // the consumer.
110 void setBackendStackSize(unsigned CC, const MCExpr *Val);
111
112 // Set the stack frame size of a function in the metadata. This only covers
113 // the frame of the function itself, not the frames of any of its callees.
114 void setFunctionStackFrameSize(StringRef FnName, uint64_t Val);
115
116 // Set the backend stack size metadata, including the frames of its known
117 // non-chain callees (and unknown if `-amdgpu-assume-external-call-stack-size`
118 // is used). Recursion and indirect/external calls must be handled by the
119 // consumer.
120 void setFunctionBackendStackSize(StringRef FnName, const MCExpr *Val);
121
122 // Set the amount of LDS used in bytes in the metadata. This is an optional
123 // advisory record for logging etc; wave dispatch actually uses the rsrc1
124 // register for the shader stage to determine the amount of LDS to allocate.
125 void setFunctionLdsSize(StringRef FnName, unsigned Val);
126
127 // Set the number of used vgprs in the metadata. This is an optional advisory
128 // record for logging etc; wave dispatch actually uses the rsrc1 register for
129 // the shader stage to determine the number of vgprs to allocate.
130 void setFunctionNumUsedVgprs(StringRef FnName, unsigned Val);
131 void setFunctionNumUsedVgprs(StringRef FnName, const MCExpr *Val);
132
133 // Set the number of used sgprs in the metadata. This is an optional advisory
134 // record for logging etc; wave dispatch actually uses the rsrc1 register for
135 // the shader stage to determine the number of sgprs to allocate.
136 void setFunctionNumUsedSgprs(StringRef FnName, unsigned Val);
137 void setFunctionNumUsedSgprs(StringRef FnName, const MCExpr *Val);
138
139 // Set the hardware register bit in PAL metadata to enable wave32 on the
140 // shader of the given calling convention.
141 void setWave32(unsigned CC);
142
143 // Emit the accumulated PAL metadata as asm directives.
144 // This is called from AMDGPUTargetAsmStreamer::Finish().
145 void toString(std::string &S);
146
147 // Set PAL metadata from YAML text.
148 bool setFromString(StringRef S);
149
150 // Get .note record vendor name of metadata blob to be emitted.
151 const char *getVendor() const;
152
153 // Get .note record type of metadata blob to be emitted:
154 // ELF::NT_AMD_PAL_METADATA (legacy key=val format), or
155 // ELF::NT_AMDGPU_METADATA (MsgPack format), or
156 // 0 (no PAL metadata).
157 unsigned getType() const;
158
159 // Emit the accumulated PAL metadata as a binary blob.
160 // This is called from AMDGPUTargetELFStreamer::Finish().
161 void toBlob(unsigned Type, std::string &S);
162
163 // Set legacy PAL metadata format.
164 void setLegacy();
165
166 unsigned getPALMajorVersion();
167 VersionTuple getPALVersion();
168
169 void updateHwStageMaximum(unsigned CC, StringRef field, unsigned Val);
170 void setHwStage(unsigned CC, StringRef field, unsigned Val);
171 void setHwStage(unsigned CC, StringRef field, bool Val);
172 void setHwStage(unsigned CC, StringRef field, msgpack::Type Type,
173 const MCExpr *Val);
174
175 void setComputeRegisters(StringRef field, unsigned Val);
176 void setComputeRegisters(StringRef field, bool Val);
177
178 // If the field does not exist will return nullptr rather than creating a new
179 // entry (which is the behaviour of the other functions).
180 msgpack::DocNode *refComputeRegister(StringRef field);
181 bool checkComputeRegisters(StringRef field, unsigned Val);
182 bool checkComputeRegisters(StringRef field, bool Val);
183
184 void setGraphicsRegisters(StringRef field, unsigned Val);
185 void setGraphicsRegisters(StringRef field, bool Val);
186 void setGraphicsRegisters(StringRef field1, StringRef field2, unsigned Val);
187 void setGraphicsRegisters(StringRef field1, StringRef field2, bool Val);
188
189 // Erase all PAL metadata.
190 void reset();
191
192 bool resolvedAllMCExpr();
193
194private:
195 // Return whether the blob type is legacy PAL metadata.
196 bool isLegacy() const;
197
198 // Reference (create if necessary) the node for the registers map.
199 msgpack::DocNode &refRegisters();
200
201 // Get (create if necessary) the registers map.
202 msgpack::MapDocNode getRegisters();
203
204 // Reference (create if necessary) the node for the shader functions map.
205 msgpack::DocNode &refShaderFunctions();
206
207 // Get (create if necessary) the shader functions map.
208 msgpack::MapDocNode getShaderFunctions();
209
210 // Get (create if necessary) a function in the shader functions map.
211 msgpack::MapDocNode getShaderFunction(StringRef Name);
212
213 // Reference (create if necessary) the node for the compute_registers map.
214 msgpack::DocNode &refComputeRegisters();
215
216 // Get (create if necessary) the .compute_registers entry.
217 msgpack::MapDocNode getComputeRegisters();
218
219 // Reference (create if necessary) the node for the graphics registers map.
220 msgpack::DocNode &refGraphicsRegisters();
221
222 // Get (create if necessary) the .graphics_registers entry.
223 msgpack::MapDocNode getGraphicsRegisters();
224
225 // Reference (create if necessary) the node for the hardware_stages map.
226 msgpack::DocNode &refHwStage();
227
228 // Get (create if necessary) the .hardware_stages entry for the given calling
229 // convention.
230 msgpack::MapDocNode getHwStage(unsigned CC);
231
232 // Get the PAL version major (idx 0) or minor (idx 1). This is an internal
233 // helper for the public wrapper functions that request Major or Minor
234 unsigned getPALVersion(unsigned idx);
235
236 bool setFromLegacyBlob(StringRef Blob);
237 bool setFromMsgPackBlob(StringRef Blob);
238 void toLegacyBlob(std::string &Blob);
239 void toMsgPackBlob(std::string &Blob);
240};
241
242} // end namespace llvm
243
244#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H
245