xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/Utils/AMDGPUPALMetadata.h (revision fe815331bb40604ba31312acf7e4619674631777)
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 
19 namespace llvm {
20 
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 
30 public:
31   // Read the amdgpu.pal.metadata supplied by the frontend, ready for
32   // per-function modification.
33   void readFromIR(Module &M);
34 
35   // Set PAL metadata from a binary blob from the applicable .note record.
36   // Returns false if bad format.  Blob must remain valid for the lifetime of
37   // the Metadata.
38   bool setFromBlob(unsigned Type, StringRef Blob);
39 
40   // Set the rsrc1 register in the metadata for a particular shader stage.
41   // In fact this ORs the value into any previous setting of the register.
42   void setRsrc1(unsigned CC, unsigned Val);
43 
44   // Set the rsrc2 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 setRsrc2(unsigned CC, unsigned Val);
47 
48   // Set the SPI_PS_INPUT_ENA register in the metadata.
49   // In fact this ORs the value into any previous setting of the register.
50   void setSpiPsInputEna(unsigned Val);
51 
52   // Set the SPI_PS_INPUT_ADDR register in the metadata.
53   // In fact this ORs the value into any previous setting of the register.
54   void setSpiPsInputAddr(unsigned Val);
55 
56   // Get a register from the metadata, or 0 if not currently set.
57   unsigned getRegister(unsigned Reg);
58 
59   // Set a register in the metadata.
60   // In fact this ORs the value into any previous setting of the register.
61   void setRegister(unsigned Reg, unsigned Val);
62 
63   // Set the entry point name for one shader.
64   void setEntryPoint(unsigned CC, StringRef Name);
65 
66   // Set the number of used vgprs in the metadata. This is an optional advisory
67   // record for logging etc; wave dispatch actually uses the rsrc1 register for
68   // the shader stage to determine the number of vgprs to allocate.
69   void setNumUsedVgprs(unsigned CC, unsigned Val);
70 
71   // Set the number of used sgprs in the metadata. This is an optional advisory
72   // record for logging etc; wave dispatch actually uses the rsrc1 register for
73   // the shader stage to determine the number of sgprs to allocate.
74   void setNumUsedSgprs(unsigned CC, unsigned Val);
75 
76   // Set the scratch size in the metadata.
77   void setScratchSize(unsigned CC, unsigned Val);
78 
79   // Set the hardware register bit in PAL metadata to enable wave32 on the
80   // shader of the given calling convention.
81   void setWave32(unsigned CC);
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