1 //===--- AMDGPUHSAMetadataStreamer.h ----------------------------*- 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 /// AMDGPU HSA Metadata Streamer.
11 ///
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
16 #define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
17 
18 #include "AMDGPU.h"
19 #include "AMDKernelCodeT.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/BinaryFormat/MsgPackDocument.h"
22 #include "llvm/Support/AMDGPUMetadata.h"
23 
24 namespace llvm {
25 
26 class AMDGPUTargetStreamer;
27 class Argument;
28 class DataLayout;
29 class Function;
30 class MachineFunction;
31 class MDNode;
32 class Module;
33 struct SIProgramInfo;
34 class Type;
35 
36 namespace AMDGPU {
37 namespace HSAMD {
38 
39 class MetadataStreamer {
40 public:
41   virtual ~MetadataStreamer(){};
42 
43   virtual bool emitTo(AMDGPUTargetStreamer &TargetStreamer) = 0;
44 
45   virtual void begin(const Module &Mod) = 0;
46 
47   virtual void end() = 0;
48 
49   virtual void emitKernel(const MachineFunction &MF,
50                           const SIProgramInfo &ProgramInfo) = 0;
51 };
52 
53 class MetadataStreamerV3 final : public MetadataStreamer {
54 private:
55   std::unique_ptr<msgpack::Document> HSAMetadataDoc =
56       std::make_unique<msgpack::Document>();
57 
58   void dump(StringRef HSAMetadataString) const;
59 
60   void verify(StringRef HSAMetadataString) const;
61 
62   Optional<StringRef> getAccessQualifier(StringRef AccQual) const;
63 
64   Optional<StringRef> getAddressSpaceQualifier(unsigned AddressSpace) const;
65 
66   StringRef getValueKind(Type *Ty, StringRef TypeQual,
67                          StringRef BaseTypeName) const;
68 
69   StringRef getValueType(Type *Ty, StringRef TypeName) const;
70 
71   std::string getTypeName(Type *Ty, bool Signed) const;
72 
73   msgpack::ArrayDocNode getWorkGroupDimensions(MDNode *Node) const;
74 
75   msgpack::MapDocNode getHSAKernelProps(const MachineFunction &MF,
76                                         const SIProgramInfo &ProgramInfo) const;
77 
78   void emitVersion();
79 
80   void emitPrintf(const Module &Mod);
81 
82   void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern);
83 
84   void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern);
85 
86   void emitKernelArgs(const Function &Func, msgpack::MapDocNode Kern);
87 
88   void emitKernelArg(const Argument &Arg, unsigned &Offset,
89                      msgpack::ArrayDocNode Args);
90 
91   void emitKernelArg(const DataLayout &DL, Type *Ty, StringRef ValueKind,
92                      unsigned &Offset, msgpack::ArrayDocNode Args,
93                      unsigned PointeeAlign = 0, StringRef Name = "",
94                      StringRef TypeName = "", StringRef BaseTypeName = "",
95                      StringRef AccQual = "", StringRef TypeQual = "");
96 
97   void emitHiddenKernelArgs(const Function &Func, unsigned &Offset,
98                             msgpack::ArrayDocNode Args);
99 
100   msgpack::DocNode &getRootMetadata(StringRef Key) {
101     return HSAMetadataDoc->getRoot().getMap(/*Convert=*/true)[Key];
102   }
103 
104   msgpack::DocNode &getHSAMetadataRoot() {
105     return HSAMetadataDoc->getRoot();
106   }
107 
108 public:
109   MetadataStreamerV3() = default;
110   ~MetadataStreamerV3() = default;
111 
112   bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
113 
114   void begin(const Module &Mod) override;
115 
116   void end() override;
117 
118   void emitKernel(const MachineFunction &MF,
119                   const SIProgramInfo &ProgramInfo) override;
120 };
121 
122 class MetadataStreamerV2 final : public MetadataStreamer {
123 private:
124   Metadata HSAMetadata;
125 
126   void dump(StringRef HSAMetadataString) const;
127 
128   void verify(StringRef HSAMetadataString) const;
129 
130   AccessQualifier getAccessQualifier(StringRef AccQual) const;
131 
132   AddressSpaceQualifier getAddressSpaceQualifier(unsigned AddressSpace) const;
133 
134   ValueKind getValueKind(Type *Ty, StringRef TypeQual,
135                          StringRef BaseTypeName) const;
136 
137   ValueType getValueType(Type *Ty, StringRef TypeName) const;
138 
139   std::string getTypeName(Type *Ty, bool Signed) const;
140 
141   std::vector<uint32_t> getWorkGroupDimensions(MDNode *Node) const;
142 
143   Kernel::CodeProps::Metadata getHSACodeProps(
144       const MachineFunction &MF,
145       const SIProgramInfo &ProgramInfo) const;
146   Kernel::DebugProps::Metadata getHSADebugProps(
147       const MachineFunction &MF,
148       const SIProgramInfo &ProgramInfo) const;
149 
150   void emitVersion();
151 
152   void emitPrintf(const Module &Mod);
153 
154   void emitKernelLanguage(const Function &Func);
155 
156   void emitKernelAttrs(const Function &Func);
157 
158   void emitKernelArgs(const Function &Func);
159 
160   void emitKernelArg(const Argument &Arg);
161 
162   void emitKernelArg(const DataLayout &DL, Type *Ty, ValueKind ValueKind,
163                      unsigned PointeeAlign = 0,
164                      StringRef Name = "", StringRef TypeName = "",
165                      StringRef BaseTypeName = "", StringRef AccQual = "",
166                      StringRef TypeQual = "");
167 
168   void emitHiddenKernelArgs(const Function &Func);
169 
170   const Metadata &getHSAMetadata() const {
171     return HSAMetadata;
172   }
173 
174 public:
175   MetadataStreamerV2() = default;
176   ~MetadataStreamerV2() = default;
177 
178   bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
179 
180   void begin(const Module &Mod) override;
181 
182   void end() override;
183 
184   void emitKernel(const MachineFunction &MF,
185                   const SIProgramInfo &ProgramInfo) override;
186 };
187 
188 } // end namespace HSAMD
189 } // end namespace AMDGPU
190 } // end namespace llvm
191 
192 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
193