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