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