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;
24class StringRef;
25
26class AMDGPUPALMetadata {
27public:
28 using RegisterExprMap = DenseMap<unsigned, const MCExpr *>;
29
30private:
31 unsigned BlobType = 0;
32 msgpack::Document MsgPackDoc;
33 msgpack::DocNode Registers;
34 msgpack::DocNode HwStages;
35 msgpack::DocNode ShaderFunctions;
36 bool VersionChecked = false;
37 msgpack::DocNode Version;
38 // From PAL version >= 3.0
39 msgpack::DocNode ComputeRegisters;
40 msgpack::DocNode GraphicsRegisters;
41
42 DelayedMCExprs DelayedExprs;
43 RegisterExprMap REM;
44 bool ResolvedAll = true;
45
46public:
47 // Read the amdgpu.pal.metadata supplied by the frontend, ready for
48 // per-function modification.
49 void readFromIR(Module &M);
50
51 // Set PAL metadata from a binary blob from the applicable .note record.
52 // Returns false if bad format. Blob must remain valid for the lifetime of
53 // the Metadata.
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 stack frame size of a function in the metadata.
107 void setFunctionScratchSize(StringRef FnName, unsigned Val);
108
109 // Set the amount of LDS used in bytes in the metadata. This is an optional
110 // advisory record for logging etc; wave dispatch actually uses the rsrc1
111 // register for the shader stage to determine the amount of LDS to allocate.
112 void setFunctionLdsSize(StringRef FnName, unsigned Val);
113
114 // Set the number of used vgprs in the metadata. This is an optional advisory
115 // record for logging etc; wave dispatch actually uses the rsrc1 register for
116 // the shader stage to determine the number of vgprs to allocate.
117 void setFunctionNumUsedVgprs(StringRef FnName, unsigned Val);
118 void setFunctionNumUsedVgprs(StringRef FnName, const MCExpr *Val);
119
120 // Set the number of used sgprs in the metadata. This is an optional advisory
121 // record for logging etc; wave dispatch actually uses the rsrc1 register for
122 // the shader stage to determine the number of sgprs to allocate.
123 void setFunctionNumUsedSgprs(StringRef FnName, unsigned Val);
124 void setFunctionNumUsedSgprs(StringRef FnName, const MCExpr *Val);
125
126 // Set the hardware register bit in PAL metadata to enable wave32 on the
127 // shader of the given calling convention.
128 void setWave32(unsigned CC);
129
130 // Emit the accumulated PAL metadata as asm directives.
131 // This is called from AMDGPUTargetAsmStreamer::Finish().
132 void toString(std::string &S);
133
134 // Set PAL metadata from YAML text.
135 bool setFromString(StringRef S);
136
137 // Get .note record vendor name of metadata blob to be emitted.
138 const char *getVendor() const;
139
140 // Get .note record type of metadata blob to be emitted:
141 // ELF::NT_AMD_PAL_METADATA (legacy key=val format), or
142 // ELF::NT_AMDGPU_METADATA (MsgPack format), or
143 // 0 (no PAL metadata).
144 unsigned getType() const;
145
146 // Emit the accumulated PAL metadata as a binary blob.
147 // This is called from AMDGPUTargetELFStreamer::Finish().
148 void toBlob(unsigned Type, std::string &S);
149
150 // Get the msgpack::Document for the PAL metadata.
151 msgpack::Document *getMsgPackDoc() { return &MsgPackDoc; }
152
153 // Set legacy PAL metadata format.
154 void setLegacy();
155
156 unsigned getPALMajorVersion();
157 unsigned getPALMinorVersion();
158
159 void setHwStage(unsigned CC, StringRef field, unsigned Val);
160 void setHwStage(unsigned CC, StringRef field, bool Val);
161 void setHwStage(unsigned CC, StringRef field, msgpack::Type Type,
162 const MCExpr *Val);
163
164 void setComputeRegisters(StringRef field, unsigned Val);
165 void setComputeRegisters(StringRef field, bool Val);
166
167 // If the field does not exist will return nullptr rather than creating a new
168 // entry (which is the behaviour of the other functions).
169 msgpack::DocNode *refComputeRegister(StringRef field);
170 bool checkComputeRegisters(StringRef field, unsigned Val);
171 bool checkComputeRegisters(StringRef field, bool Val);
172
173 void setGraphicsRegisters(StringRef field, unsigned Val);
174 void setGraphicsRegisters(StringRef field, bool Val);
175 void setGraphicsRegisters(StringRef field1, StringRef field2, unsigned Val);
176 void setGraphicsRegisters(StringRef field1, StringRef field2, bool Val);
177
178 // Erase all PAL metadata.
179 void reset();
180
181 bool resolvedAllMCExpr();
182
183private:
184 // Return whether the blob type is legacy PAL metadata.
185 bool isLegacy() const;
186
187 // Reference (create if necessary) the node for the registers map.
188 msgpack::DocNode &refRegisters();
189
190 // Get (create if necessary) the registers map.
191 msgpack::MapDocNode getRegisters();
192
193 // Reference (create if necessary) the node for the shader functions map.
194 msgpack::DocNode &refShaderFunctions();
195
196 // Get (create if necessary) the shader functions map.
197 msgpack::MapDocNode getShaderFunctions();
198
199 // Get (create if necessary) a function in the shader functions map.
200 msgpack::MapDocNode getShaderFunction(StringRef Name);
201
202 // Reference (create if necessary) the node for the compute_registers map.
203 msgpack::DocNode &refComputeRegisters();
204
205 // Get (create if necessary) the .compute_registers entry.
206 msgpack::MapDocNode getComputeRegisters();
207
208 // Reference (create if necessary) the node for the graphics registers map.
209 msgpack::DocNode &refGraphicsRegisters();
210
211 // Get (create if necessary) the .graphics_registers entry.
212 msgpack::MapDocNode getGraphicsRegisters();
213
214 // Reference (create if necessary) the node for the hardware_stages map.
215 msgpack::DocNode &refHwStage();
216
217 // Get (create if necessary) the .hardware_stages entry for the given calling
218 // convention.
219 msgpack::MapDocNode getHwStage(unsigned CC);
220
221 // Get the PAL version major (idx 0) or minor (idx 1). This is an internal
222 // helper for the public wrapper functions that request Major or Minor
223 unsigned getPALVersion(unsigned idx);
224
225 bool setFromLegacyBlob(StringRef Blob);
226 bool setFromMsgPackBlob(StringRef Blob);
227 void toLegacyBlob(std::string &Blob);
228 void toMsgPackBlob(std::string &Blob);
229};
230
231} // end namespace llvm
232
233#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H
234