[AMDGPU][AsmParser] Simplify the implementation of SWZ operands.
[llvm-project.git] / llvm / lib / Target / AMDGPU / AMDGPUHSAMetadataStreamer.h
blob7d7080e920f5ca00e96aa2954f12bebfe5261f16
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 ///
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
16 #define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
18 #include "llvm/BinaryFormat/MsgPackDocument.h"
19 #include "llvm/Support/AMDGPUMetadata.h"
20 #include "llvm/Support/Alignment.h"
22 namespace llvm {
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 class GCNSubtarget;
35 namespace AMDGPU {
37 namespace IsaInfo {
38 class AMDGPUTargetID;
41 namespace HSAMD {
43 class MetadataStreamer {
44 public:
45 virtual ~MetadataStreamer() = default;
47 virtual bool emitTo(AMDGPUTargetStreamer &TargetStreamer) = 0;
49 virtual void begin(const Module &Mod,
50 const IsaInfo::AMDGPUTargetID &TargetID) = 0;
52 virtual void end() = 0;
54 virtual void emitKernel(const MachineFunction &MF,
55 const SIProgramInfo &ProgramInfo) = 0;
57 protected:
58 virtual void emitVersion() = 0;
59 virtual void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset,
60 msgpack::ArrayDocNode Args) = 0;
61 virtual void emitKernelAttrs(const Function &Func,
62 msgpack::MapDocNode Kern) = 0;
65 class MetadataStreamerMsgPackV3 : public MetadataStreamer {
66 protected:
67 std::unique_ptr<msgpack::Document> HSAMetadataDoc =
68 std::make_unique<msgpack::Document>();
70 void dump(StringRef HSAMetadataString) const;
72 void verify(StringRef HSAMetadataString) const;
74 std::optional<StringRef> getAccessQualifier(StringRef AccQual) const;
76 std::optional<StringRef>
77 getAddressSpaceQualifier(unsigned AddressSpace) const;
79 StringRef getValueKind(Type *Ty, StringRef TypeQual,
80 StringRef BaseTypeName) const;
82 std::string getTypeName(Type *Ty, bool Signed) const;
84 msgpack::ArrayDocNode getWorkGroupDimensions(MDNode *Node) const;
86 msgpack::MapDocNode getHSAKernelProps(const MachineFunction &MF,
87 const SIProgramInfo &ProgramInfo,
88 unsigned CodeObjectVersion) const;
90 void emitVersion() override;
92 void emitPrintf(const Module &Mod);
94 void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern);
96 void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override;
98 void emitKernelArgs(const MachineFunction &MF, msgpack::MapDocNode Kern);
100 void emitKernelArg(const Argument &Arg, unsigned &Offset,
101 msgpack::ArrayDocNode Args);
103 void emitKernelArg(const DataLayout &DL, Type *Ty, Align Alignment,
104 StringRef ValueKind, unsigned &Offset,
105 msgpack::ArrayDocNode Args,
106 MaybeAlign PointeeAlign = std::nullopt,
107 StringRef Name = "", StringRef TypeName = "",
108 StringRef BaseTypeName = "", StringRef AccQual = "",
109 StringRef TypeQual = "");
111 void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset,
112 msgpack::ArrayDocNode Args) override;
114 msgpack::DocNode &getRootMetadata(StringRef Key) {
115 return HSAMetadataDoc->getRoot().getMap(/*Convert=*/true)[Key];
118 msgpack::DocNode &getHSAMetadataRoot() {
119 return HSAMetadataDoc->getRoot();
122 public:
123 MetadataStreamerMsgPackV3() = default;
124 ~MetadataStreamerMsgPackV3() = default;
126 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
128 void begin(const Module &Mod,
129 const IsaInfo::AMDGPUTargetID &TargetID) override;
131 void end() override;
133 void emitKernel(const MachineFunction &MF,
134 const SIProgramInfo &ProgramInfo) override;
137 class MetadataStreamerMsgPackV4 : public MetadataStreamerMsgPackV3 {
138 protected:
139 void emitVersion() override;
140 void emitTargetID(const IsaInfo::AMDGPUTargetID &TargetID);
142 public:
143 MetadataStreamerMsgPackV4() = default;
144 ~MetadataStreamerMsgPackV4() = default;
146 void begin(const Module &Mod,
147 const IsaInfo::AMDGPUTargetID &TargetID) override;
150 class MetadataStreamerMsgPackV5 final : public MetadataStreamerMsgPackV4 {
151 protected:
152 void emitVersion() override;
153 void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset,
154 msgpack::ArrayDocNode Args) override;
155 void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override;
157 public:
158 MetadataStreamerMsgPackV5() = default;
159 ~MetadataStreamerMsgPackV5() = default;
162 // TODO: Rename MetadataStreamerV2 -> MetadataStreamerYamlV2.
163 class MetadataStreamerYamlV2 final : public MetadataStreamer {
164 private:
165 Metadata HSAMetadata;
167 void dump(StringRef HSAMetadataString) const;
169 void verify(StringRef HSAMetadataString) const;
171 AccessQualifier getAccessQualifier(StringRef AccQual) const;
173 AddressSpaceQualifier getAddressSpaceQualifier(unsigned AddressSpace) const;
175 ValueKind getValueKind(Type *Ty, StringRef TypeQual,
176 StringRef BaseTypeName) const;
178 std::string getTypeName(Type *Ty, bool Signed) const;
180 std::vector<uint32_t> getWorkGroupDimensions(MDNode *Node) const;
182 Kernel::CodeProps::Metadata getHSACodeProps(
183 const MachineFunction &MF,
184 const SIProgramInfo &ProgramInfo) const;
185 Kernel::DebugProps::Metadata getHSADebugProps(
186 const MachineFunction &MF,
187 const SIProgramInfo &ProgramInfo) const;
189 void emitPrintf(const Module &Mod);
191 void emitKernelLanguage(const Function &Func);
193 void emitKernelAttrs(const Function &Func);
195 void emitKernelArgs(const Function &Func, const GCNSubtarget &ST);
197 void emitKernelArg(const Argument &Arg);
199 void emitKernelArg(const DataLayout &DL, Type *Ty, Align Alignment,
200 ValueKind ValueKind,
201 MaybeAlign PointeeAlign = std::nullopt,
202 StringRef Name = "", StringRef TypeName = "",
203 StringRef BaseTypeName = "", StringRef AccQual = "",
204 StringRef TypeQual = "");
206 void emitHiddenKernelArgs(const Function &Func, const GCNSubtarget &ST);
208 const Metadata &getHSAMetadata() const {
209 return HSAMetadata;
212 protected:
213 void emitVersion() override;
214 void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset,
215 msgpack::ArrayDocNode Args) override {
216 llvm_unreachable("Dummy override should not be invoked!");
218 void emitKernelAttrs(const Function &Func,
219 msgpack::MapDocNode Kern) override {
220 llvm_unreachable("Dummy override should not be invoked!");
223 public:
224 MetadataStreamerYamlV2() = default;
225 ~MetadataStreamerYamlV2() = default;
227 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
229 void begin(const Module &Mod,
230 const IsaInfo::AMDGPUTargetID &TargetID) override;
232 void end() override;
234 void emitKernel(const MachineFunction &MF,
235 const SIProgramInfo &ProgramInfo) override;
238 } // end namespace HSAMD
239 } // end namespace AMDGPU
240 } // end namespace llvm
242 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H