xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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 
31 public:
32   // Read the amdgpu.pal.metadata supplied by the frontend, ready for
33   // per-function modification.
34   void readFromIR(Module &M);
35 
36   // Set PAL metadata from a binary blob from the applicable .note record.
37   // Returns false if bad format.  Blob must remain valid for the lifetime of
38   // the Metadata.
39   bool setFromBlob(unsigned Type, StringRef Blob);
40 
41   // Set the rsrc1 register in the metadata for a particular shader stage.
42   // In fact this ORs the value into any previous setting of the register.
43   void setRsrc1(unsigned CC, unsigned Val);
44 
45   // Set the rsrc2 register in the metadata for a particular shader stage.
46   // In fact this ORs the value into any previous setting of the register.
47   void setRsrc2(unsigned CC, unsigned Val);
48 
49   // Set the SPI_PS_INPUT_ENA register in the metadata.
50   // In fact this ORs the value into any previous setting of the register.
51   void setSpiPsInputEna(unsigned Val);
52 
53   // Set the SPI_PS_INPUT_ADDR register in the metadata.
54   // In fact this ORs the value into any previous setting of the register.
55   void setSpiPsInputAddr(unsigned Val);
56 
57   // Get a register from the metadata, or 0 if not currently set.
58   unsigned getRegister(unsigned Reg);
59 
60   // Set a register in the metadata.
61   // In fact this ORs the value into any previous setting of the register.
62   void setRegister(unsigned Reg, unsigned Val);
63 
64   // Set the entry point name for one shader.
65   void setEntryPoint(unsigned CC, StringRef Name);
66 
67   // Set the number of used vgprs in the metadata. This is an optional advisory
68   // record for logging etc; wave dispatch actually uses the rsrc1 register for
69   // the shader stage to determine the number of vgprs to allocate.
70   void setNumUsedVgprs(unsigned CC, unsigned Val);
71 
72   // Set the number of used sgprs 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 sgprs to allocate.
75   void setNumUsedSgprs(unsigned CC, unsigned Val);
76 
77   // Set the scratch size in the metadata.
78   void setScratchSize(unsigned CC, unsigned Val);
79 
80   // Set the stack frame size of a function in the metadata.
81   void setFunctionScratchSize(const MachineFunction &MF, unsigned Val);
82 
83   // Set the amount of LDS used in bytes in the metadata. This is an optional
84   // advisory record for logging etc; wave dispatch actually uses the rsrc1
85   // register for the shader stage to determine the amount of LDS to allocate.
86   void setFunctionLdsSize(const MachineFunction &MF, unsigned Val);
87 
88   // Set the number of used vgprs in the metadata. This is an optional advisory
89   // record for logging etc; wave dispatch actually uses the rsrc1 register for
90   // the shader stage to determine the number of vgprs to allocate.
91   void setFunctionNumUsedVgprs(const MachineFunction &MF, unsigned Val);
92 
93   // Set the number of used sgprs in the metadata. This is an optional advisory
94   // record for logging etc; wave dispatch actually uses the rsrc1 register for
95   // the shader stage to determine the number of sgprs to allocate.
96   void setFunctionNumUsedSgprs(const MachineFunction &MF, unsigned Val);
97 
98   // Set the hardware register bit in PAL metadata to enable wave32 on the
99   // shader of the given calling convention.
100   void setWave32(unsigned CC);
101 
102   // Emit the accumulated PAL metadata as asm directives.
103   // This is called from AMDGPUTargetAsmStreamer::Finish().
104   void toString(std::string &S);
105 
106   // Set PAL metadata from YAML text.
107   bool setFromString(StringRef S);
108 
109   // Get .note record vendor name of metadata blob to be emitted.
110   const char *getVendor() const;
111 
112   // Get .note record type of metadata blob to be emitted:
113   // ELF::NT_AMD_PAL_METADATA (legacy key=val format), or
114   // ELF::NT_AMDGPU_METADATA (MsgPack format), or
115   // 0 (no PAL metadata).
116   unsigned getType() const;
117 
118   // Emit the accumulated PAL metadata as a binary blob.
119   // This is called from AMDGPUTargetELFStreamer::Finish().
120   void toBlob(unsigned Type, std::string &S);
121 
122   // Get the msgpack::Document for the PAL metadata.
123   msgpack::Document *getMsgPackDoc() { return &MsgPackDoc; }
124 
125   // Set legacy PAL metadata format.
126   void setLegacy();
127 
128   // Erase all PAL metadata.
129   void reset();
130 
131 private:
132   // Return whether the blob type is legacy PAL metadata.
133   bool isLegacy() const;
134 
135   // Reference (create if necessary) the node for the registers map.
136   msgpack::DocNode &refRegisters();
137 
138   // Get (create if necessary) the registers map.
139   msgpack::MapDocNode getRegisters();
140 
141   // Reference (create if necessary) the node for the shader functions map.
142   msgpack::DocNode &refShaderFunctions();
143 
144   // Get (create if necessary) the shader functions map.
145   msgpack::MapDocNode getShaderFunctions();
146 
147   // Get (create if necessary) a function in the shader functions map.
148   msgpack::MapDocNode getShaderFunction(StringRef Name);
149 
150   // Get (create if necessary) the .hardware_stages entry for the given calling
151   // convention.
152   msgpack::MapDocNode getHwStage(unsigned CC);
153 
154   bool setFromLegacyBlob(StringRef Blob);
155   bool setFromMsgPackBlob(StringRef Blob);
156   void toLegacyBlob(std::string &Blob);
157   void toMsgPackBlob(std::string &Blob);
158 };
159 
160 } // end namespace llvm
161 
162 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H
163