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