235 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9/// \file10/// PAL metadata handling11//12//===----------------------------------------------------------------------===//13 14#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H15#define LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H16#include "AMDGPUDelayedMCExpr.h"17#include "llvm/ADT/DenseMap.h"18#include "llvm/BinaryFormat/MsgPackDocument.h"19#include "llvm/MC/MCContext.h"20 21namespace llvm {22 23class Module;24 25class AMDGPUPALMetadata {26public:27 using RegisterExprMap = DenseMap<unsigned, const MCExpr *>;28 29private: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.038 msgpack::DocNode ComputeRegisters;39 msgpack::DocNode GraphicsRegisters;40 41 DelayedMCExprs DelayedExprs;42 RegisterExprMap REM;43 bool ResolvedAll = true;44 45public:46 // Read the amdgpu.pal.metadata supplied by the frontend, ready for47 // 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 of52 // 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 advisory85 // record for logging etc; wave dispatch actually uses the rsrc1 register for86 // 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 advisory91 // 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 advisory96 // record for logging etc; wave dispatch actually uses the rsrc1 register for97 // 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 optional109 // advisory record for logging etc; wave dispatch actually uses the rsrc1110 // 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 advisory114 // record for logging etc; wave dispatch actually uses the rsrc1 register for115 // 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 advisory120 // record for logging etc; wave dispatch actually uses the rsrc1 register for121 // 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 the126 // 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), or141 // ELF::NT_AMDGPU_METADATA (MsgPack format), or142 // 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 updateHwStageMaximum(unsigned CC, StringRef field, unsigned Val);160 void setHwStage(unsigned CC, StringRef field, unsigned Val);161 void setHwStage(unsigned CC, StringRef field, bool Val);162 void setHwStage(unsigned CC, StringRef field, msgpack::Type Type,163 const MCExpr *Val);164 165 void setComputeRegisters(StringRef field, unsigned Val);166 void setComputeRegisters(StringRef field, bool Val);167 168 // If the field does not exist will return nullptr rather than creating a new169 // entry (which is the behaviour of the other functions).170 msgpack::DocNode *refComputeRegister(StringRef field);171 bool checkComputeRegisters(StringRef field, unsigned Val);172 bool checkComputeRegisters(StringRef field, bool Val);173 174 void setGraphicsRegisters(StringRef field, unsigned Val);175 void setGraphicsRegisters(StringRef field, bool Val);176 void setGraphicsRegisters(StringRef field1, StringRef field2, unsigned Val);177 void setGraphicsRegisters(StringRef field1, StringRef field2, bool Val);178 179 // Erase all PAL metadata.180 void reset();181 182 bool resolvedAllMCExpr();183 184private:185 // Return whether the blob type is legacy PAL metadata.186 bool isLegacy() const;187 188 // Reference (create if necessary) the node for the registers map.189 msgpack::DocNode &refRegisters();190 191 // Get (create if necessary) the registers map.192 msgpack::MapDocNode getRegisters();193 194 // Reference (create if necessary) the node for the shader functions map.195 msgpack::DocNode &refShaderFunctions();196 197 // Get (create if necessary) the shader functions map.198 msgpack::MapDocNode getShaderFunctions();199 200 // Get (create if necessary) a function in the shader functions map.201 msgpack::MapDocNode getShaderFunction(StringRef Name);202 203 // Reference (create if necessary) the node for the compute_registers map.204 msgpack::DocNode &refComputeRegisters();205 206 // Get (create if necessary) the .compute_registers entry.207 msgpack::MapDocNode getComputeRegisters();208 209 // Reference (create if necessary) the node for the graphics registers map.210 msgpack::DocNode &refGraphicsRegisters();211 212 // Get (create if necessary) the .graphics_registers entry.213 msgpack::MapDocNode getGraphicsRegisters();214 215 // Reference (create if necessary) the node for the hardware_stages map.216 msgpack::DocNode &refHwStage();217 218 // Get (create if necessary) the .hardware_stages entry for the given calling219 // convention.220 msgpack::MapDocNode getHwStage(unsigned CC);221 222 // Get the PAL version major (idx 0) or minor (idx 1). This is an internal223 // helper for the public wrapper functions that request Major or Minor224 unsigned getPALVersion(unsigned idx);225 226 bool setFromLegacyBlob(StringRef Blob);227 bool setFromMsgPackBlob(StringRef Blob);228 void toLegacyBlob(std::string &Blob);229 void toMsgPackBlob(std::string &Blob);230};231 232} // end namespace llvm233 234#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H235