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