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