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