xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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/AMDGPUDelayedMCExpr.h"
19 #include "llvm/BinaryFormat/MsgPackDocument.h"
20 #include "llvm/Support/AMDGPUMetadata.h"
21 #include "llvm/Support/Alignment.h"
22 #include "llvm/Support/Compiler.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 
38 namespace IsaInfo {
39 class AMDGPUTargetID;
40 }
41 
42 namespace HSAMD {
43 
44 class MetadataStreamer {
45 public:
46   virtual ~MetadataStreamer() = default;
47 
48   virtual bool emitTo(AMDGPUTargetStreamer &TargetStreamer) = 0;
49 
50   virtual void begin(const Module &Mod,
51                      const IsaInfo::AMDGPUTargetID &TargetID) = 0;
52 
53   virtual void end() = 0;
54 
55   virtual void emitKernel(const MachineFunction &MF,
56                           const SIProgramInfo &ProgramInfo) = 0;
57 
58 protected:
59   virtual void emitVersion() = 0;
60   virtual void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset,
61                                     msgpack::ArrayDocNode Args) = 0;
62   virtual void emitKernelAttrs(const Function &Func,
63                                msgpack::MapDocNode Kern) = 0;
64 };
65 
66 class LLVM_EXTERNAL_VISIBILITY MetadataStreamerMsgPackV4
67     : public MetadataStreamer {
68 protected:
69   std::unique_ptr<DelayedMCExprs> DelayedExprs =
70       std::make_unique<DelayedMCExprs>();
71 
72   std::unique_ptr<msgpack::Document> HSAMetadataDoc =
73       std::make_unique<msgpack::Document>();
74 
75   void dump(StringRef HSAMetadataString) const;
76 
77   void verify(StringRef HSAMetadataString) const;
78 
79   std::optional<StringRef> getAccessQualifier(StringRef AccQual) const;
80 
81   std::optional<StringRef>
82   getAddressSpaceQualifier(unsigned AddressSpace) const;
83 
84   StringRef getValueKind(Type *Ty, StringRef TypeQual,
85                          StringRef BaseTypeName) const;
86 
87   std::string getTypeName(Type *Ty, bool Signed) const;
88 
89   msgpack::ArrayDocNode getWorkGroupDimensions(MDNode *Node) const;
90 
91   msgpack::MapDocNode getHSAKernelProps(const MachineFunction &MF,
92                                         const SIProgramInfo &ProgramInfo,
93                                         unsigned CodeObjectVersion) const;
94 
95   void emitVersion() override;
96 
97   void emitTargetID(const IsaInfo::AMDGPUTargetID &TargetID);
98 
99   void emitPrintf(const Module &Mod);
100 
101   void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern);
102 
103   void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override;
104 
105   void emitKernelArgs(const MachineFunction &MF, msgpack::MapDocNode Kern);
106 
107   void emitKernelArg(const Argument &Arg, unsigned &Offset,
108                      msgpack::ArrayDocNode Args);
109 
110   void emitKernelArg(const DataLayout &DL, Type *Ty, Align Alignment,
111                      StringRef ValueKind, unsigned &Offset,
112                      msgpack::ArrayDocNode Args,
113                      MaybeAlign PointeeAlign = std::nullopt,
114                      StringRef Name = "", StringRef TypeName = "",
115                      StringRef BaseTypeName = "", StringRef ActAccQual = "",
116                      StringRef AccQual = "", StringRef TypeQual = "");
117 
118   void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset,
119                             msgpack::ArrayDocNode Args) override;
120 
getRootMetadata(StringRef Key)121   msgpack::DocNode &getRootMetadata(StringRef Key) {
122     return HSAMetadataDoc->getRoot().getMap(/*Convert=*/true)[Key];
123   }
124 
getHSAMetadataRoot()125   msgpack::DocNode &getHSAMetadataRoot() {
126     return HSAMetadataDoc->getRoot();
127   }
128 
129 public:
130   MetadataStreamerMsgPackV4() = default;
131   ~MetadataStreamerMsgPackV4() = default;
132 
133   bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
134 
135   void begin(const Module &Mod,
136              const IsaInfo::AMDGPUTargetID &TargetID) override;
137 
138   void end() override;
139 
140   void emitKernel(const MachineFunction &MF,
141                   const SIProgramInfo &ProgramInfo) override;
142 };
143 
144 class MetadataStreamerMsgPackV5 : public MetadataStreamerMsgPackV4 {
145 protected:
146   void emitVersion() override;
147   void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset,
148                             msgpack::ArrayDocNode Args) override;
149   void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override;
150 
151 public:
152   MetadataStreamerMsgPackV5() = default;
153   ~MetadataStreamerMsgPackV5() = default;
154 };
155 
156 class MetadataStreamerMsgPackV6 final : public MetadataStreamerMsgPackV5 {
157 protected:
158   void emitVersion() override;
159 
160 public:
161   MetadataStreamerMsgPackV6() = default;
162   ~MetadataStreamerMsgPackV6() = default;
163 };
164 
165 } // end namespace HSAMD
166 } // end namespace AMDGPU
167 } // end namespace llvm
168 
169 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
170