1 //===--- AMDGPUHSAMetadataStreamer.h ----------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// AMDGPU HSA Metadata Streamer.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
16 #define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
19 #include "AMDKernelCodeT.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/BinaryFormat/MsgPackDocument.h"
22 #include "llvm/Support/AMDGPUMetadata.h"
23 #include "llvm/Support/Alignment.h"
27 class AMDGPUTargetStreamer;
31 class MachineFunction;
40 class MetadataStreamer {
42 virtual ~MetadataStreamer(){};
44 virtual bool emitTo(AMDGPUTargetStreamer &TargetStreamer) = 0;
46 virtual void begin(const Module &Mod) = 0;
48 virtual void end() = 0;
50 virtual void emitKernel(const MachineFunction &MF,
51 const SIProgramInfo &ProgramInfo) = 0;
54 class MetadataStreamerV3 final : public MetadataStreamer {
56 std::unique_ptr<msgpack::Document> HSAMetadataDoc =
57 std::make_unique<msgpack::Document>();
59 void dump(StringRef HSAMetadataString) const;
61 void verify(StringRef HSAMetadataString) const;
63 Optional<StringRef> getAccessQualifier(StringRef AccQual) const;
65 Optional<StringRef> getAddressSpaceQualifier(unsigned AddressSpace) const;
67 StringRef getValueKind(Type *Ty, StringRef TypeQual,
68 StringRef BaseTypeName) const;
70 std::string getTypeName(Type *Ty, bool Signed) const;
72 msgpack::ArrayDocNode getWorkGroupDimensions(MDNode *Node) const;
74 msgpack::MapDocNode getHSAKernelProps(const MachineFunction &MF,
75 const SIProgramInfo &ProgramInfo) const;
79 void emitPrintf(const Module &Mod);
81 void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern);
83 void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern);
85 void emitKernelArgs(const Function &Func, msgpack::MapDocNode Kern);
87 void emitKernelArg(const Argument &Arg, unsigned &Offset,
88 msgpack::ArrayDocNode Args);
90 void emitKernelArg(const DataLayout &DL, Type *Ty, StringRef ValueKind,
91 unsigned &Offset, msgpack::ArrayDocNode Args,
92 MaybeAlign PointeeAlign = None, StringRef Name = "",
93 StringRef TypeName = "", StringRef BaseTypeName = "",
94 StringRef AccQual = "", StringRef TypeQual = "");
96 void emitHiddenKernelArgs(const Function &Func, unsigned &Offset,
97 msgpack::ArrayDocNode Args);
99 msgpack::DocNode &getRootMetadata(StringRef Key) {
100 return HSAMetadataDoc->getRoot().getMap(/*Convert=*/true)[Key];
103 msgpack::DocNode &getHSAMetadataRoot() {
104 return HSAMetadataDoc->getRoot();
108 MetadataStreamerV3() = default;
109 ~MetadataStreamerV3() = default;
111 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
113 void begin(const Module &Mod) override;
117 void emitKernel(const MachineFunction &MF,
118 const SIProgramInfo &ProgramInfo) override;
121 class MetadataStreamerV2 final : public MetadataStreamer {
123 Metadata HSAMetadata;
125 void dump(StringRef HSAMetadataString) const;
127 void verify(StringRef HSAMetadataString) const;
129 AccessQualifier getAccessQualifier(StringRef AccQual) const;
131 AddressSpaceQualifier getAddressSpaceQualifier(unsigned AddressSpace) const;
133 ValueKind getValueKind(Type *Ty, StringRef TypeQual,
134 StringRef BaseTypeName) const;
136 std::string getTypeName(Type *Ty, bool Signed) const;
138 std::vector<uint32_t> getWorkGroupDimensions(MDNode *Node) const;
140 Kernel::CodeProps::Metadata getHSACodeProps(
141 const MachineFunction &MF,
142 const SIProgramInfo &ProgramInfo) const;
143 Kernel::DebugProps::Metadata getHSADebugProps(
144 const MachineFunction &MF,
145 const SIProgramInfo &ProgramInfo) const;
149 void emitPrintf(const Module &Mod);
151 void emitKernelLanguage(const Function &Func);
153 void emitKernelAttrs(const Function &Func);
155 void emitKernelArgs(const Function &Func);
157 void emitKernelArg(const Argument &Arg);
159 void emitKernelArg(const DataLayout &DL, Type *Ty, ValueKind ValueKind,
160 MaybeAlign PointeeAlign = None, StringRef Name = "",
161 StringRef TypeName = "", StringRef BaseTypeName = "",
162 StringRef AccQual = "", StringRef TypeQual = "");
164 void emitHiddenKernelArgs(const Function &Func);
166 const Metadata &getHSAMetadata() const {
171 MetadataStreamerV2() = default;
172 ~MetadataStreamerV2() = default;
174 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
176 void begin(const Module &Mod) override;
180 void emitKernel(const MachineFunction &MF,
181 const SIProgramInfo &ProgramInfo) override;
184 } // end namespace HSAMD
185 } // end namespace AMDGPU
186 } // end namespace llvm
188 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H