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
17#include "llvm/ADT/StringRef.h"
18#include "llvm/BinaryFormat/MsgPackDocument.h"
19#include <map>
20
21namespace llvm {
22
23class AMDGPUTargetStreamer;
24class formatted_raw_ostream;
25class MCStreamer;
26class Module;
27
28class AMDGPUPALMetadata {
29  unsigned BlobType = 0;
30  msgpack::Document MsgPackDoc;
31  msgpack::DocNode Registers;
32  msgpack::DocNode HwStages;
33
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
67  // Set the entry point name for one shader.
68  void setEntryPoint(unsigned CC, StringRef Name);
69
70  // 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
83  // Set the hardware register bit in PAL metadata to enable wave32 on the
84  // shader of the given calling convention.
85  void setWave32(unsigned CC);
86
87  // Emit the accumulated PAL metadata as asm directives.
88  // This is called from AMDGPUTargetAsmStreamer::Finish().
89  void toString(std::string &S);
90
91  // Set PAL metadata from YAML text.
92  bool setFromString(StringRef S);
93
94  // Get .note record vendor name of metadata blob to be emitted.
95  const char *getVendor() const;
96
97  // Get .note record type of metadata blob to be emitted:
98  // ELF::NT_AMD_AMDGPU_PAL_METADATA (legacy key=val format), or
99  // ELF::NT_AMDGPU_METADATA (MsgPack format), or
100  // 0 (no PAL metadata).
101  unsigned getType() const;
102
103  // Emit the accumulated PAL metadata as a binary blob.
104  // This is called from AMDGPUTargetELFStreamer::Finish().
105  void toBlob(unsigned Type, std::string &S);
106
107  // Get the msgpack::Document for the PAL metadata.
108  msgpack::Document *getMsgPackDoc() { return &MsgPackDoc; }
109
110  // Set legacy PAL metadata format.
111  void setLegacy();
112
113private:
114  // Return whether the blob type is legacy PAL metadata.
115  bool isLegacy() const;
116
117  // Reference (create if necessary) the node for the registers map.
118  msgpack::DocNode &refRegisters();
119
120  // Get (create if necessary) the registers map.
121  msgpack::MapDocNode getRegisters();
122
123  // Get (create if necessary) the .hardware_stages entry for the given calling
124  // convention.
125  msgpack::MapDocNode getHwStage(unsigned CC);
126
127  bool setFromLegacyBlob(StringRef Blob);
128  bool setFromMsgPackBlob(StringRef Blob);
129  void toLegacyBlob(std::string &Blob);
130  void toMsgPackBlob(std::string &Blob);
131};
132
133} // end namespace llvm
134
135#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUPALMETADATA_H
136