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) const; 88 89 void emitVersion() override; 90 91 void emitPrintf(const Module &Mod); 92 93 void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern); 94 95 void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override; 96 97 void emitKernelArgs(const MachineFunction &MF, msgpack::MapDocNode Kern); 98 99 void emitKernelArg(const Argument &Arg, unsigned &Offset, 100 msgpack::ArrayDocNode Args); 101 102 void emitKernelArg(const DataLayout &DL, Type *Ty, Align Alignment, 103 StringRef ValueKind, unsigned &Offset, 104 msgpack::ArrayDocNode Args, 105 MaybeAlign PointeeAlign = std::nullopt, 106 StringRef Name = "", StringRef TypeName = "", 107 StringRef BaseTypeName = "", StringRef AccQual = "", 108 StringRef TypeQual = ""); 109 110 void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, 111 msgpack::ArrayDocNode Args) override; 112 113 msgpack::DocNode &getRootMetadata(StringRef Key) { 114 return HSAMetadataDoc->getRoot().getMap(/*Convert=*/true)[Key]; 115 } 116 117 msgpack::DocNode &getHSAMetadataRoot() { 118 return HSAMetadataDoc->getRoot(); 119 } 120 121 public: 122 MetadataStreamerMsgPackV3() = default; 123 ~MetadataStreamerMsgPackV3() = default; 124 125 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override; 126 127 void begin(const Module &Mod, 128 const IsaInfo::AMDGPUTargetID &TargetID) override; 129 130 void end() override; 131 132 void emitKernel(const MachineFunction &MF, 133 const SIProgramInfo &ProgramInfo) override; 134 }; 135 136 class MetadataStreamerMsgPackV4 : public MetadataStreamerMsgPackV3 { 137 protected: 138 void emitVersion() override; 139 void emitTargetID(const IsaInfo::AMDGPUTargetID &TargetID); 140 141 public: 142 MetadataStreamerMsgPackV4() = default; 143 ~MetadataStreamerMsgPackV4() = default; 144 145 void begin(const Module &Mod, 146 const IsaInfo::AMDGPUTargetID &TargetID) override; 147 }; 148 149 class MetadataStreamerMsgPackV5 final : public MetadataStreamerMsgPackV4 { 150 protected: 151 void emitVersion() override; 152 void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, 153 msgpack::ArrayDocNode Args) override; 154 void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override; 155 156 public: 157 MetadataStreamerMsgPackV5() = default; 158 ~MetadataStreamerMsgPackV5() = default; 159 }; 160 161 // TODO: Rename MetadataStreamerV2 -> MetadataStreamerYamlV2. 162 class MetadataStreamerYamlV2 final : public MetadataStreamer { 163 private: 164 Metadata HSAMetadata; 165 166 void dump(StringRef HSAMetadataString) const; 167 168 void verify(StringRef HSAMetadataString) const; 169 170 AccessQualifier getAccessQualifier(StringRef AccQual) const; 171 172 AddressSpaceQualifier getAddressSpaceQualifier(unsigned AddressSpace) const; 173 174 ValueKind getValueKind(Type *Ty, StringRef TypeQual, 175 StringRef BaseTypeName) const; 176 177 std::string getTypeName(Type *Ty, bool Signed) const; 178 179 std::vector<uint32_t> getWorkGroupDimensions(MDNode *Node) const; 180 181 Kernel::CodeProps::Metadata getHSACodeProps( 182 const MachineFunction &MF, 183 const SIProgramInfo &ProgramInfo) const; 184 Kernel::DebugProps::Metadata getHSADebugProps( 185 const MachineFunction &MF, 186 const SIProgramInfo &ProgramInfo) const; 187 188 void emitPrintf(const Module &Mod); 189 190 void emitKernelLanguage(const Function &Func); 191 192 void emitKernelAttrs(const Function &Func); 193 194 void emitKernelArgs(const Function &Func, const GCNSubtarget &ST); 195 196 void emitKernelArg(const Argument &Arg); 197 198 void emitKernelArg(const DataLayout &DL, Type *Ty, Align Alignment, 199 ValueKind ValueKind, 200 MaybeAlign PointeeAlign = std::nullopt, 201 StringRef Name = "", StringRef TypeName = "", 202 StringRef BaseTypeName = "", StringRef AccQual = "", 203 StringRef TypeQual = ""); 204 205 void emitHiddenKernelArgs(const Function &Func, const GCNSubtarget &ST); 206 207 const Metadata &getHSAMetadata() const { 208 return HSAMetadata; 209 } 210 211 protected: 212 void emitVersion() override; 213 void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, 214 msgpack::ArrayDocNode Args) override { 215 llvm_unreachable("Dummy override should not be invoked!"); 216 } 217 void emitKernelAttrs(const Function &Func, 218 msgpack::MapDocNode Kern) override { 219 llvm_unreachable("Dummy override should not be invoked!"); 220 } 221 222 public: 223 MetadataStreamerYamlV2() = default; 224 ~MetadataStreamerYamlV2() = default; 225 226 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override; 227 228 void begin(const Module &Mod, 229 const IsaInfo::AMDGPUTargetID &TargetID) override; 230 231 void end() override; 232 233 void emitKernel(const MachineFunction &MF, 234 const SIProgramInfo &ProgramInfo) override; 235 }; 236 237 } // end namespace HSAMD 238 } // end namespace AMDGPU 239 } // end namespace llvm 240 241 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H 242