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 hardware register bit in PAL metadata to enable wave32 on the
84   // shader of the given calling convention.
85   void setWave32(unsigned CC);
86 
87   // Emit the accumulated PAL metadata as asm directives.
88   // This is called from AMDGPUTargetAsmStreamer::Finish().
89   void toString(std::string &S);
90 
91   // Set PAL metadata from YAML text.
92   bool setFromString(StringRef S);
93 
94   // Get .note record vendor name of metadata blob to be emitted.
95   const char *getVendor() const;
96 
97   // Get .note record type of metadata blob to be emitted:
98   // ELF::NT_AMD_AMDGPU_PAL_METADATA (legacy key=val format), or
99   // ELF::NT_AMDGPU_METADATA (MsgPack format), or
100   // 0 (no PAL metadata).
101   unsigned getType() const;
102 
103   // Emit the accumulated PAL metadata as a binary blob.
104   // This is called from AMDGPUTargetELFStreamer::Finish().
105   void toBlob(unsigned Type, std::string &S);
106 
107   // Get the msgpack::Document for the PAL metadata.
108   msgpack::Document *getMsgPackDoc() { return &MsgPackDoc; }
109 
110   // Set legacy PAL metadata format.
111   void setLegacy();
112 
113   // Erase all PAL metadata.
114   void reset();
115 
116 private:
117   // Return whether the blob type is legacy PAL metadata.
118   bool isLegacy() const;
119 
120   // Reference (create if necessary) the node for the registers map.
121   msgpack::DocNode &refRegisters();
122 
123   // Get (create if necessary) the registers map.
124   msgpack::MapDocNode getRegisters();
125 
126   // Reference (create if necessary) the node for the shader functions map.
127   msgpack::DocNode &refShaderFunctions();
128 
129   // Get (create if necessary) the shader functions map.
130   msgpack::MapDocNode getShaderFunctions();
131 
132   // Get (create if necessary) a function in the shader functions map.
133   msgpack::MapDocNode getShaderFunction(StringRef Name);
134 
135   // Get (create if necessary) the .hardware_stages entry for the given calling
136   // convention.
137   msgpack::MapDocNode getHwStage(unsigned CC);
138 
139   bool setFromLegacyBlob(StringRef Blob);
140   bool setFromMsgPackBlob(StringRef Blob);
141   void toLegacyBlob(std::string &Blob);
142   void toMsgPackBlob(std::string &Blob);
143 };
144 
145 } // end namespace llvm
146 
147 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H
148