| 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 "AMDGPUDelayedMCExpr.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/BinaryFormat/MsgPackDocument.h" |
| 19 | #include "llvm/MC/MCContext.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | |
| 23 | class Module; |
| 24 | |
| 25 | class AMDGPUPALMetadata { |
| 26 | public: |
| 27 | using RegisterExprMap = DenseMap<unsigned, const MCExpr *>; |
| 28 | |
| 29 | private: |
| 30 | unsigned BlobType = 0; |
| 31 | msgpack::Document MsgPackDoc; |
| 32 | msgpack::DocNode Registers; |
| 33 | msgpack::DocNode HwStages; |
| 34 | msgpack::DocNode ShaderFunctions; |
| 35 | bool VersionChecked = false; |
| 36 | msgpack::DocNode Version; |
| 37 | // From PAL version >= 3.0 |
| 38 | msgpack::DocNode ComputeRegisters; |
| 39 | msgpack::DocNode GraphicsRegisters; |
| 40 | |
| 41 | DelayedMCExprs DelayedExprs; |
| 42 | RegisterExprMap REM; |
| 43 | bool ResolvedAll = true; |
| 44 | |
| 45 | public: |
| 46 | // Read the amdgpu.pal.metadata supplied by the frontend, ready for |
| 47 | // per-function modification. |
| 48 | void readFromIR(Module &M); |
| 49 | |
| 50 | // Set PAL metadata from a binary blob from the applicable .note record. |
| 51 | // Returns false if bad format. Blob must remain valid for the lifetime of |
| 52 | // the Metadata. |
| 53 | bool setFromBlob(unsigned Type, StringRef Blob); |
| 54 | |
| 55 | // Set the rsrc1 register in the metadata for a particular shader stage. |
| 56 | // In fact this ORs the value into any previous setting of the register. |
| 57 | void setRsrc1(unsigned CC, unsigned Val); |
| 58 | void setRsrc1(unsigned CC, const MCExpr *Val, MCContext &Ctx); |
| 59 | |
| 60 | // Set the rsrc2 register in the metadata for a particular shader stage. |
| 61 | // In fact this ORs the value into any previous setting of the register. |
| 62 | void setRsrc2(unsigned CC, unsigned Val); |
| 63 | void setRsrc2(unsigned CC, const MCExpr *Val, MCContext &Ctx); |
| 64 | |
| 65 | // Set the SPI_PS_INPUT_ENA register in the metadata. |
| 66 | // In fact this ORs the value into any previous setting of the register. |
| 67 | void setSpiPsInputEna(unsigned Val); |
| 68 | |
| 69 | // Set the SPI_PS_INPUT_ADDR register in the metadata. |
| 70 | // In fact this ORs the value into any previous setting of the register. |
| 71 | void setSpiPsInputAddr(unsigned Val); |
| 72 | |
| 73 | // Get a register from the metadata, or 0 if not currently set. |
| 74 | unsigned getRegister(unsigned Reg); |
| 75 | |
| 76 | // Set a register in the metadata. |
| 77 | // In fact this ORs the value into any previous setting of the register. |
| 78 | void setRegister(unsigned Reg, unsigned Val); |
| 79 | void setRegister(unsigned Reg, const MCExpr *Val, MCContext &Ctx); |
| 80 | |
| 81 | // Set the entry point name for one shader. |
| 82 | void setEntryPoint(unsigned CC, StringRef Name); |
| 83 | |
| 84 | // Set the number of used vgprs in the metadata. This is an optional advisory |
| 85 | // record for logging etc; wave dispatch actually uses the rsrc1 register for |
| 86 | // the shader stage to determine the number of vgprs to allocate. |
| 87 | void setNumUsedVgprs(unsigned CC, unsigned Val); |
| 88 | void setNumUsedVgprs(unsigned CC, const MCExpr *Val, MCContext &Ctx); |
| 89 | |
| 90 | // Set the number of used agprs in the metadata. This is an optional advisory |
| 91 | // record for logging etc; |
| 92 | void setNumUsedAgprs(unsigned CC, unsigned Val); |
| 93 | void setNumUsedAgprs(unsigned CC, const MCExpr *Val); |
| 94 | |
| 95 | // Set the number of used sgprs in the metadata. This is an optional advisory |
| 96 | // record for logging etc; wave dispatch actually uses the rsrc1 register for |
| 97 | // the shader stage to determine the number of sgprs to allocate. |
| 98 | void setNumUsedSgprs(unsigned CC, unsigned Val); |
| 99 | void setNumUsedSgprs(unsigned CC, const MCExpr *Val, MCContext &Ctx); |
| 100 | |
| 101 | // Set the scratch size in the metadata. |
| 102 | void setScratchSize(unsigned CC, unsigned Val); |
| 103 | void setScratchSize(unsigned CC, const MCExpr *Val, MCContext &Ctx); |
| 104 | |
| 105 | // Set the stack frame size of a function in the metadata. |
| 106 | void setFunctionScratchSize(StringRef FnName, unsigned Val); |
| 107 | |
| 108 | // Set the amount of LDS used in bytes in the metadata. This is an optional |
| 109 | // advisory record for logging etc; wave dispatch actually uses the rsrc1 |
| 110 | // register for the shader stage to determine the amount of LDS to allocate. |
| 111 | void setFunctionLdsSize(StringRef FnName, unsigned Val); |
| 112 | |
| 113 | // Set the number of used vgprs in the metadata. This is an optional advisory |
| 114 | // record for logging etc; wave dispatch actually uses the rsrc1 register for |
| 115 | // the shader stage to determine the number of vgprs to allocate. |
| 116 | void setFunctionNumUsedVgprs(StringRef FnName, unsigned Val); |
| 117 | void setFunctionNumUsedVgprs(StringRef FnName, const MCExpr *Val); |
| 118 | |
| 119 | // Set the number of used sgprs in the metadata. This is an optional advisory |
| 120 | // record for logging etc; wave dispatch actually uses the rsrc1 register for |
| 121 | // the shader stage to determine the number of sgprs to allocate. |
| 122 | void setFunctionNumUsedSgprs(StringRef FnName, unsigned Val); |
| 123 | void setFunctionNumUsedSgprs(StringRef FnName, const MCExpr *Val); |
| 124 | |
| 125 | // Set the hardware register bit in PAL metadata to enable wave32 on the |
| 126 | // shader of the given calling convention. |
| 127 | void setWave32(unsigned CC); |
| 128 | |
| 129 | // Emit the accumulated PAL metadata as asm directives. |
| 130 | // This is called from AMDGPUTargetAsmStreamer::Finish(). |
| 131 | void toString(std::string &S); |
| 132 | |
| 133 | // Set PAL metadata from YAML text. |
| 134 | bool setFromString(StringRef S); |
| 135 | |
| 136 | // Get .note record vendor name of metadata blob to be emitted. |
| 137 | const char *getVendor() const; |
| 138 | |
| 139 | // Get .note record type of metadata blob to be emitted: |
| 140 | // ELF::NT_AMD_PAL_METADATA (legacy key=val format), or |
| 141 | // ELF::NT_AMDGPU_METADATA (MsgPack format), or |
| 142 | // 0 (no PAL metadata). |
| 143 | unsigned getType() const; |
| 144 | |
| 145 | // Emit the accumulated PAL metadata as a binary blob. |
| 146 | // This is called from AMDGPUTargetELFStreamer::Finish(). |
| 147 | void toBlob(unsigned Type, std::string &S); |
| 148 | |
| 149 | // Get the msgpack::Document for the PAL metadata. |
| 150 | msgpack::Document *getMsgPackDoc() { return &MsgPackDoc; } |
| 151 | |
| 152 | // Set legacy PAL metadata format. |
| 153 | void setLegacy(); |
| 154 | |
| 155 | unsigned getPALMajorVersion(); |
| 156 | unsigned getPALMinorVersion(); |
| 157 | VersionTuple getPALVersion(); |
| 158 | |
| 159 | void setHwStage(unsigned CC, StringRef field, unsigned Val); |
| 160 | void setHwStage(unsigned CC, StringRef field, bool Val); |
| 161 | void setHwStage(unsigned CC, StringRef field, msgpack::Type Type, |
| 162 | const MCExpr *Val); |
| 163 | |
| 164 | void setComputeRegisters(StringRef field, unsigned Val); |
| 165 | void setComputeRegisters(StringRef field, bool Val); |
| 166 | |
| 167 | // If the field does not exist will return nullptr rather than creating a new |
| 168 | // entry (which is the behaviour of the other functions). |
| 169 | msgpack::DocNode *refComputeRegister(StringRef field); |
| 170 | bool checkComputeRegisters(StringRef field, unsigned Val); |
| 171 | bool checkComputeRegisters(StringRef field, bool Val); |
| 172 | |
| 173 | void setGraphicsRegisters(StringRef field, unsigned Val); |
| 174 | void setGraphicsRegisters(StringRef field, bool Val); |
| 175 | void setGraphicsRegisters(StringRef field1, StringRef field2, unsigned Val); |
| 176 | void setGraphicsRegisters(StringRef field1, StringRef field2, bool Val); |
| 177 | |
| 178 | // Erase all PAL metadata. |
| 179 | void reset(); |
| 180 | |
| 181 | bool resolvedAllMCExpr(); |
| 182 | |
| 183 | private: |
| 184 | // Return whether the blob type is legacy PAL metadata. |
| 185 | bool isLegacy() const; |
| 186 | |
| 187 | // Reference (create if necessary) the node for the registers map. |
| 188 | msgpack::DocNode &refRegisters(); |
| 189 | |
| 190 | // Get (create if necessary) the registers map. |
| 191 | msgpack::MapDocNode getRegisters(); |
| 192 | |
| 193 | // Reference (create if necessary) the node for the shader functions map. |
| 194 | msgpack::DocNode &refShaderFunctions(); |
| 195 | |
| 196 | // Get (create if necessary) the shader functions map. |
| 197 | msgpack::MapDocNode getShaderFunctions(); |
| 198 | |
| 199 | // Get (create if necessary) a function in the shader functions map. |
| 200 | msgpack::MapDocNode getShaderFunction(StringRef Name); |
| 201 | |
| 202 | // Reference (create if necessary) the node for the compute_registers map. |
| 203 | msgpack::DocNode &refComputeRegisters(); |
| 204 | |
| 205 | // Get (create if necessary) the .compute_registers entry. |
| 206 | msgpack::MapDocNode getComputeRegisters(); |
| 207 | |
| 208 | // Reference (create if necessary) the node for the graphics registers map. |
| 209 | msgpack::DocNode &refGraphicsRegisters(); |
| 210 | |
| 211 | // Get (create if necessary) the .graphics_registers entry. |
| 212 | msgpack::MapDocNode getGraphicsRegisters(); |
| 213 | |
| 214 | // Reference (create if necessary) the node for the hardware_stages map. |
| 215 | msgpack::DocNode &refHwStage(); |
| 216 | |
| 217 | // Get (create if necessary) the .hardware_stages entry for the given calling |
| 218 | // convention. |
| 219 | msgpack::MapDocNode getHwStage(unsigned CC); |
| 220 | |
| 221 | // Get the PAL version major (idx 0) or minor (idx 1). This is an internal |
| 222 | // helper for the public wrapper functions that request Major or Minor |
| 223 | unsigned getPALVersion(unsigned idx); |
| 224 | |
| 225 | bool setFromLegacyBlob(StringRef Blob); |
| 226 | bool setFromMsgPackBlob(StringRef Blob); |
| 227 | void toLegacyBlob(std::string &Blob); |
| 228 | void toMsgPackBlob(std::string &Blob); |
| 229 | }; |
| 230 | |
| 231 | } // end namespace llvm |
| 232 | |
| 233 | #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H |
| 234 | |