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 MachineFunction; 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 msgpack::DocNode ShaderFunctions; 30 31 public: 32 // Read the amdgpu.pal.metadata supplied by the frontend, ready for 33 // per-function modification. 34 void readFromIR(Module &M); 35 36 // Set PAL metadata from a binary blob from the applicable .note record. 37 // Returns false if bad format. Blob must remain valid for the lifetime of 38 // the Metadata. 39 bool setFromBlob(unsigned Type, StringRef Blob); 40 41 // Set the rsrc1 register in the metadata for a particular shader stage. 42 // In fact this ORs the value into any previous setting of the register. 43 void setRsrc1(unsigned CC, unsigned Val); 44 45 // Set the rsrc2 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 setRsrc2(unsigned CC, unsigned Val); 48 49 // Set the SPI_PS_INPUT_ENA register in the metadata. 50 // In fact this ORs the value into any previous setting of the register. 51 void setSpiPsInputEna(unsigned Val); 52 53 // Set the SPI_PS_INPUT_ADDR register in the metadata. 54 // In fact this ORs the value into any previous setting of the register. 55 void setSpiPsInputAddr(unsigned Val); 56 57 // Get a register from the metadata, or 0 if not currently set. 58 unsigned getRegister(unsigned Reg); 59 60 // Set a register in the metadata. 61 // In fact this ORs the value into any previous setting of the register. 62 void setRegister(unsigned Reg, unsigned Val); 63 64 // Set the entry point name for one shader. 65 void setEntryPoint(unsigned CC, StringRef Name); 66 67 // Set the number of used vgprs in the metadata. This is an optional advisory 68 // record for logging etc; wave dispatch actually uses the rsrc1 register for 69 // the shader stage to determine the number of vgprs to allocate. 70 void setNumUsedVgprs(unsigned CC, unsigned Val); 71 72 // Set the number of used agprs in the metadata. This is an optional advisory 73 // record for logging etc; 74 void setNumUsedAgprs(unsigned CC, unsigned Val); 75 76 // Set the number of used sgprs in the metadata. This is an optional advisory 77 // record for logging etc; wave dispatch actually uses the rsrc1 register for 78 // the shader stage to determine the number of sgprs to allocate. 79 void setNumUsedSgprs(unsigned CC, unsigned Val); 80 81 // Set the scratch size in the metadata. 82 void setScratchSize(unsigned CC, unsigned Val); 83 84 // Set the stack frame size of a function in the metadata. 85 void setFunctionScratchSize(const MachineFunction &MF, unsigned Val); 86 87 // Set the amount of LDS used in bytes in the metadata. This is an optional 88 // advisory record for logging etc; wave dispatch actually uses the rsrc1 89 // register for the shader stage to determine the amount of LDS to allocate. 90 void setFunctionLdsSize(const MachineFunction &MF, unsigned Val); 91 92 // Set the number of used vgprs in the metadata. This is an optional advisory 93 // record for logging etc; wave dispatch actually uses the rsrc1 register for 94 // the shader stage to determine the number of vgprs to allocate. 95 void setFunctionNumUsedVgprs(const MachineFunction &MF, unsigned Val); 96 97 // Set the number of used sgprs in the metadata. This is an optional advisory 98 // record for logging etc; wave dispatch actually uses the rsrc1 register for 99 // the shader stage to determine the number of sgprs to allocate. 100 void setFunctionNumUsedSgprs(const MachineFunction &MF, unsigned Val); 101 102 // Set the hardware register bit in PAL metadata to enable wave32 on the 103 // shader of the given calling convention. 104 void setWave32(unsigned CC); 105 106 // Emit the accumulated PAL metadata as asm directives. 107 // This is called from AMDGPUTargetAsmStreamer::Finish(). 108 void toString(std::string &S); 109 110 // Set PAL metadata from YAML text. 111 bool setFromString(StringRef S); 112 113 // Get .note record vendor name of metadata blob to be emitted. 114 const char *getVendor() const; 115 116 // Get .note record type of metadata blob to be emitted: 117 // ELF::NT_AMD_PAL_METADATA (legacy key=val format), or 118 // ELF::NT_AMDGPU_METADATA (MsgPack format), or 119 // 0 (no PAL metadata). 120 unsigned getType() const; 121 122 // Emit the accumulated PAL metadata as a binary blob. 123 // This is called from AMDGPUTargetELFStreamer::Finish(). 124 void toBlob(unsigned Type, std::string &S); 125 126 // Get the msgpack::Document for the PAL metadata. 127 msgpack::Document *getMsgPackDoc() { return &MsgPackDoc; } 128 129 // Set legacy PAL metadata format. 130 void setLegacy(); 131 132 // Erase all PAL metadata. 133 void reset(); 134 135 private: 136 // Return whether the blob type is legacy PAL metadata. 137 bool isLegacy() const; 138 139 // Reference (create if necessary) the node for the registers map. 140 msgpack::DocNode &refRegisters(); 141 142 // Get (create if necessary) the registers map. 143 msgpack::MapDocNode getRegisters(); 144 145 // Reference (create if necessary) the node for the shader functions map. 146 msgpack::DocNode &refShaderFunctions(); 147 148 // Get (create if necessary) the shader functions map. 149 msgpack::MapDocNode getShaderFunctions(); 150 151 // Get (create if necessary) a function in the shader functions map. 152 msgpack::MapDocNode getShaderFunction(StringRef Name); 153 154 // Get (create if necessary) the .hardware_stages entry for the given calling 155 // convention. 156 msgpack::MapDocNode getHwStage(unsigned CC); 157 158 bool setFromLegacyBlob(StringRef Blob); 159 bool setFromMsgPackBlob(StringRef Blob); 160 void toLegacyBlob(std::string &Blob); 161 void toMsgPackBlob(std::string &Blob); 162 }; 163 164 } // end namespace llvm 165 166 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H 167