1 //===-- AMDGPUPALMetadata.h - PAL metadata handling -------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// PAL metadata handling
12 //===----------------------------------------------------------------------===//
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"
26 class AMDGPUPALMetadata
{
28 using RegisterExprMap
= DenseMap
<unsigned, const MCExpr
*>;
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
;
42 DelayedMCExprs DelayedExprs
;
44 bool ResolvedAll
= true;
47 // Read the amdgpu.pal.metadata supplied by the frontend, ready for
48 // per-function modification.
49 void readFromIR(Module
&M
);
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
54 bool setFromBlob(unsigned Type
, StringRef Blob
);
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
);
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
);
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
);
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
);
74 // Get a register from the metadata, or 0 if not currently set.
75 unsigned getRegister(unsigned Reg
);
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
);
82 // Set the entry point name for one shader.
83 void setEntryPoint(unsigned CC
, StringRef Name
);
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
);
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
);
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
);
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
);
106 // Set the stack frame size of a function in the metadata.
107 void setFunctionScratchSize(StringRef FnName
, unsigned Val
);
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
);
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
);
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
);
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
);
130 // Emit the accumulated PAL metadata as asm directives.
131 // This is called from AMDGPUTargetAsmStreamer::Finish().
132 void toString(std::string
&S
);
134 // Set PAL metadata from YAML text.
135 bool setFromString(StringRef S
);
137 // Get .note record vendor name of metadata blob to be emitted.
138 const char *getVendor() const;
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;
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
);
150 // Get the msgpack::Document for the PAL metadata.
151 msgpack::Document
*getMsgPackDoc() { return &MsgPackDoc
; }
153 // Set legacy PAL metadata format.
156 unsigned getPALMajorVersion();
157 unsigned getPALMinorVersion();
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
,
164 void setComputeRegisters(StringRef field
, unsigned Val
);
165 void setComputeRegisters(StringRef field
, bool Val
);
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
);
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
);
178 // Erase all PAL metadata.
181 bool resolvedAllMCExpr();
184 // Return whether the blob type is legacy PAL metadata.
185 bool isLegacy() const;
187 // Reference (create if necessary) the node for the registers map.
188 msgpack::DocNode
&refRegisters();
190 // Get (create if necessary) the registers map.
191 msgpack::MapDocNode
getRegisters();
193 // Reference (create if necessary) the node for the shader functions map.
194 msgpack::DocNode
&refShaderFunctions();
196 // Get (create if necessary) the shader functions map.
197 msgpack::MapDocNode
getShaderFunctions();
199 // Get (create if necessary) a function in the shader functions map.
200 msgpack::MapDocNode
getShaderFunction(StringRef Name
);
202 // Reference (create if necessary) the node for the compute_registers map.
203 msgpack::DocNode
&refComputeRegisters();
205 // Get (create if necessary) the .compute_registers entry.
206 msgpack::MapDocNode
getComputeRegisters();
208 // Reference (create if necessary) the node for the graphics registers map.
209 msgpack::DocNode
&refGraphicsRegisters();
211 // Get (create if necessary) the .graphics_registers entry.
212 msgpack::MapDocNode
getGraphicsRegisters();
214 // Reference (create if necessary) the node for the hardware_stages map.
215 msgpack::DocNode
&refHwStage();
217 // Get (create if necessary) the .hardware_stages entry for the given calling
219 msgpack::MapDocNode
getHwStage(unsigned CC
);
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
);
225 bool setFromLegacyBlob(StringRef Blob
);
226 bool setFromMsgPackBlob(StringRef Blob
);
227 void toLegacyBlob(std::string
&Blob
);
228 void toMsgPackBlob(std::string
&Blob
);
231 } // end namespace llvm
233 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H