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 class GCNSubtarget; 34 35 namespace AMDGPU { 36 37 namespace IsaInfo { 38 class AMDGPUTargetID; 39 } 40 41 namespace HSAMD { 42 43 class MetadataStreamer { 44 public: 45 virtual ~MetadataStreamer() = default; 46 47 virtual bool emitTo(AMDGPUTargetStreamer &TargetStreamer) = 0; 48 49 virtual void begin(const Module &Mod, 50 const IsaInfo::AMDGPUTargetID &TargetID) = 0; 51 52 virtual void end() = 0; 53 54 virtual void emitKernel(const MachineFunction &MF, 55 const SIProgramInfo &ProgramInfo) = 0; 56 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; 63 }; 64 65 class MetadataStreamerMsgPackV3 : public MetadataStreamer { 66 protected: 67 std::unique_ptr<msgpack::Document> HSAMetadataDoc = 68 std::make_unique<msgpack::Document>(); 69 70 void dump(StringRef HSAMetadataString) const; 71 72 void verify(StringRef HSAMetadataString) const; 73 74 std::optional<StringRef> getAccessQualifier(StringRef AccQual) const; 75 76 std::optional<StringRef> 77 getAddressSpaceQualifier(unsigned AddressSpace) const; 78 79 StringRef getValueKind(Type *Ty, StringRef TypeQual, 80 StringRef BaseTypeName) const; 81 82 std::string getTypeName(Type *Ty, bool Signed) const; 83 84 msgpack::ArrayDocNode getWorkGroupDimensions(MDNode *Node) const; 85 86 msgpack::MapDocNode getHSAKernelProps(const MachineFunction &MF, 87 const SIProgramInfo &ProgramInfo, 88 unsigned CodeObjectVersion) const; 89 90 void emitVersion() override; 91 92 void emitPrintf(const Module &Mod); 93 94 void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern); 95 96 void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override; 97 98 void emitKernelArgs(const MachineFunction &MF, msgpack::MapDocNode Kern); 99 100 void emitKernelArg(const Argument &Arg, unsigned &Offset, 101 msgpack::ArrayDocNode Args); 102 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 = ""); 110 111 void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, 112 msgpack::ArrayDocNode Args) override; 113 114 msgpack::DocNode &getRootMetadata(StringRef Key) { 115 return HSAMetadataDoc->getRoot().getMap(/*Convert=*/true)[Key]; 116 } 117 118 msgpack::DocNode &getHSAMetadataRoot() { 119 return HSAMetadataDoc->getRoot(); 120 } 121 122 public: 123 MetadataStreamerMsgPackV3() = default; 124 ~MetadataStreamerMsgPackV3() = default; 125 126 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override; 127 128 void begin(const Module &Mod, 129 const IsaInfo::AMDGPUTargetID &TargetID) override; 130 131 void end() override; 132 133 void emitKernel(const MachineFunction &MF, 134 const SIProgramInfo &ProgramInfo) override; 135 }; 136 137 class MetadataStreamerMsgPackV4 : public MetadataStreamerMsgPackV3 { 138 protected: 139 void emitVersion() override; 140 void emitTargetID(const IsaInfo::AMDGPUTargetID &TargetID); 141 142 public: 143 MetadataStreamerMsgPackV4() = default; 144 ~MetadataStreamerMsgPackV4() = default; 145 146 void begin(const Module &Mod, 147 const IsaInfo::AMDGPUTargetID &TargetID) override; 148 }; 149 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; 156 157 public: 158 MetadataStreamerMsgPackV5() = default; 159 ~MetadataStreamerMsgPackV5() = default; 160 }; 161 162 // TODO: Rename MetadataStreamerV2 -> MetadataStreamerYamlV2. 163 class MetadataStreamerYamlV2 final : public MetadataStreamer { 164 private: 165 Metadata HSAMetadata; 166 167 void dump(StringRef HSAMetadataString) const; 168 169 void verify(StringRef HSAMetadataString) const; 170 171 AccessQualifier getAccessQualifier(StringRef AccQual) const; 172 173 AddressSpaceQualifier getAddressSpaceQualifier(unsigned AddressSpace) const; 174 175 ValueKind getValueKind(Type *Ty, StringRef TypeQual, 176 StringRef BaseTypeName) const; 177 178 std::string getTypeName(Type *Ty, bool Signed) const; 179 180 std::vector<uint32_t> getWorkGroupDimensions(MDNode *Node) const; 181 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; 188 189 void emitPrintf(const Module &Mod); 190 191 void emitKernelLanguage(const Function &Func); 192 193 void emitKernelAttrs(const Function &Func); 194 195 void emitKernelArgs(const Function &Func, const GCNSubtarget &ST); 196 197 void emitKernelArg(const Argument &Arg); 198 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 = ""); 205 206 void emitHiddenKernelArgs(const Function &Func, const GCNSubtarget &ST); 207 208 const Metadata &getHSAMetadata() const { 209 return HSAMetadata; 210 } 211 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!"); 217 } 218 void emitKernelAttrs(const Function &Func, 219 msgpack::MapDocNode Kern) override { 220 llvm_unreachable("Dummy override should not be invoked!"); 221 } 222 223 public: 224 MetadataStreamerYamlV2() = default; 225 ~MetadataStreamerYamlV2() = default; 226 227 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override; 228 229 void begin(const Module &Mod, 230 const IsaInfo::AMDGPUTargetID &TargetID) override; 231 232 void end() override; 233 234 void emitKernel(const MachineFunction &MF, 235 const SIProgramInfo &ProgramInfo) override; 236 }; 237 238 } // end namespace HSAMD 239 } // end namespace AMDGPU 240 } // end namespace llvm 241 242 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H 243