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