blob: dbef2cc605dd7706d230880cdc71dab58ec375e7 [file] [log] [blame]
Tim Renoufd737b552019-03-20 17:42:00 +00001//===-- 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
17#include "llvm/ADT/StringRef.h"
Tim Renoufe7bd52f2019-03-20 18:47:21 +000018#include "llvm/BinaryFormat/MsgPackDocument.h"
Tim Renoufd737b552019-03-20 17:42:00 +000019#include <map>
20
21namespace llvm {
22
23class AMDGPUTargetStreamer;
24class formatted_raw_ostream;
25class MCStreamer;
26class Module;
27
28class AMDGPUPALMetadata {
Tim Renoufe7bd52f2019-03-20 18:47:21 +000029 unsigned BlobType = 0;
30 msgpack::Document MsgPackDoc;
31 msgpack::DocNode Registers;
32 msgpack::DocNode HwStages;
Tim Renoufd737b552019-03-20 17:42:00 +000033
34public:
35 // Read the amdgpu.pal.metadata supplied by the frontend, ready for
36 // per-function modification.
37 void readFromIR(Module &M);
38
39 // Set PAL metadata from a binary blob from the applicable .note record.
40 // Returns false if bad format. Blob must remain valid for the lifetime of
41 // the Metadata.
42 bool setFromBlob(unsigned Type, StringRef Blob);
43
44 // Set the rsrc1 register in the metadata for a particular shader stage.
45 // In fact this ORs the value into any previous setting of the register.
46 void setRsrc1(unsigned CC, unsigned Val);
47
48 // Set the rsrc2 register in the metadata for a particular shader stage.
49 // In fact this ORs the value into any previous setting of the register.
50 void setRsrc2(unsigned CC, unsigned Val);
51
52 // Set the SPI_PS_INPUT_ENA register in the metadata.
53 // In fact this ORs the value into any previous setting of the register.
54 void setSpiPsInputEna(unsigned Val);
55
56 // Set the SPI_PS_INPUT_ADDR register in the metadata.
57 // In fact this ORs the value into any previous setting of the register.
58 void setSpiPsInputAddr(unsigned Val);
59
60 // Get a register from the metadata, or 0 if not currently set.
61 unsigned getRegister(unsigned Reg);
62
63 // Set a register in the metadata.
64 // In fact this ORs the value into any previous setting of the register.
65 void setRegister(unsigned Reg, unsigned Val);
66
Tim Renoufe7bd52f2019-03-20 18:47:21 +000067 // Set the entry point name for one shader.
68 void setEntryPoint(unsigned CC, StringRef Name);
69
Tim Renoufd737b552019-03-20 17:42:00 +000070 // Set the number of used vgprs in the metadata. This is an optional advisory
71 // record for logging etc; wave dispatch actually uses the rsrc1 register for
72 // the shader stage to determine the number of vgprs to allocate.
73 void setNumUsedVgprs(unsigned CC, unsigned Val);
74
75 // Set the number of used sgprs in the metadata. This is an optional advisory
76 // record for logging etc; wave dispatch actually uses the rsrc1 register for
77 // the shader stage to determine the number of sgprs to allocate.
78 void setNumUsedSgprs(unsigned CC, unsigned Val);
79
80 // Set the scratch size in the metadata.
81 void setScratchSize(unsigned CC, unsigned Val);
82
Tim Renoufe7bd52f2019-03-20 18:47:21 +000083 // Emit the accumulated PAL metadata as asm directives.
Tim Renoufd737b552019-03-20 17:42:00 +000084 // This is called from AMDGPUTargetAsmStreamer::Finish().
85 void toString(std::string &S);
86
Tim Renoufe7bd52f2019-03-20 18:47:21 +000087 // Set PAL metadata from YAML text.
88 bool setFromString(StringRef S);
89
90 // Get .note record vendor name of metadata blob to be emitted.
91 const char *getVendor() const;
92
93 // Get .note record type of metadata blob to be emitted:
94 // ELF::NT_AMD_AMDGPU_PAL_METADATA (legacy key=val format), or
Tim Renouf2327c232019-03-20 22:02:09 +000095 // ELF::NT_AMDGPU_METADATA (MsgPack format), or
96 // 0 (no PAL metadata).
Tim Renoufe7bd52f2019-03-20 18:47:21 +000097 unsigned getType() const;
98
Tim Renoufd737b552019-03-20 17:42:00 +000099 // Emit the accumulated PAL metadata as a binary blob.
100 // This is called from AMDGPUTargetELFStreamer::Finish().
101 void toBlob(unsigned Type, std::string &S);
Tim Renoufe7bd52f2019-03-20 18:47:21 +0000102
103 // Get the msgpack::Document for the PAL metadata.
104 msgpack::Document *getMsgPackDoc() { return &MsgPackDoc; }
105
106 // Set legacy PAL metadata format.
107 void setLegacy();
108
109private:
110 // Return whether the blob type is legacy PAL metadata.
111 bool isLegacy() const;
112
113 // Reference (create if necessary) the node for the registers map.
114 msgpack::DocNode &refRegisters();
115
116 // Get (create if necessary) the registers map.
117 msgpack::MapDocNode getRegisters();
118
119 // Get (create if necessary) the .hardware_stages entry for the given calling
120 // convention.
121 msgpack::MapDocNode getHwStage(unsigned CC);
122
123 bool setFromLegacyBlob(StringRef Blob);
124 bool setFromMsgPackBlob(StringRef Blob);
125 void toLegacyBlob(std::string &Blob);
126 void toMsgPackBlob(std::string &Blob);
Tim Renoufd737b552019-03-20 17:42:00 +0000127};
128
129} // end namespace llvm
130
131#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H