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