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 "AMDGPU.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" 24 25 namespace llvm { 26 27 class AMDGPUTargetStreamer; 28 class Argument; 29 class DataLayout; 30 class Function; 31 class MachineFunction; 32 class MDNode; 33 class Module; 34 struct SIProgramInfo; 35 class Type; 36 37 namespace AMDGPU { 38 namespace HSAMD { 39 40 class MetadataStreamer { 41 public: 42 virtual ~MetadataStreamer(){}; 43 44 virtual bool emitTo(AMDGPUTargetStreamer &TargetStreamer) = 0; 45 46 virtual void begin(const Module &Mod) = 0; 47 48 virtual void end() = 0; 49 50 virtual void emitKernel(const MachineFunction &MF, 51 const SIProgramInfo &ProgramInfo) = 0; 52 }; 53 54 class MetadataStreamerV3 final : public MetadataStreamer { 55 private: 56 std::unique_ptr<msgpack::Document> HSAMetadataDoc = 57 std::make_unique<msgpack::Document>(); 58 59 void dump(StringRef HSAMetadataString) const; 60 61 void verify(StringRef HSAMetadataString) const; 62 63 Optional<StringRef> getAccessQualifier(StringRef AccQual) const; 64 65 Optional<StringRef> getAddressSpaceQualifier(unsigned AddressSpace) const; 66 67 StringRef getValueKind(Type *Ty, StringRef TypeQual, 68 StringRef BaseTypeName) const; 69 70 StringRef getValueType(Type *Ty, StringRef TypeName) const; 71 72 std::string getTypeName(Type *Ty, bool Signed) const; 73 74 msgpack::ArrayDocNode getWorkGroupDimensions(MDNode *Node) const; 75 76 msgpack::MapDocNode getHSAKernelProps(const MachineFunction &MF, 77 const SIProgramInfo &ProgramInfo) const; 78 79 void emitVersion(); 80 81 void emitPrintf(const Module &Mod); 82 83 void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern); 84 85 void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern); 86 87 void emitKernelArgs(const Function &Func, msgpack::MapDocNode Kern); 88 89 void emitKernelArg(const Argument &Arg, unsigned &Offset, 90 msgpack::ArrayDocNode Args); 91 92 void emitKernelArg(const DataLayout &DL, Type *Ty, StringRef ValueKind, 93 unsigned &Offset, msgpack::ArrayDocNode Args, 94 MaybeAlign PointeeAlign = None, StringRef Name = "", 95 StringRef TypeName = "", StringRef BaseTypeName = "", 96 StringRef AccQual = "", StringRef TypeQual = ""); 97 98 void emitHiddenKernelArgs(const Function &Func, unsigned &Offset, 99 msgpack::ArrayDocNode Args); 100 101 msgpack::DocNode &getRootMetadata(StringRef Key) { 102 return HSAMetadataDoc->getRoot().getMap(/*Convert=*/true)[Key]; 103 } 104 105 msgpack::DocNode &getHSAMetadataRoot() { 106 return HSAMetadataDoc->getRoot(); 107 } 108 109 public: 110 MetadataStreamerV3() = default; 111 ~MetadataStreamerV3() = default; 112 113 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override; 114 115 void begin(const Module &Mod) override; 116 117 void end() override; 118 119 void emitKernel(const MachineFunction &MF, 120 const SIProgramInfo &ProgramInfo) override; 121 }; 122 123 class MetadataStreamerV2 final : public MetadataStreamer { 124 private: 125 Metadata HSAMetadata; 126 127 void dump(StringRef HSAMetadataString) const; 128 129 void verify(StringRef HSAMetadataString) const; 130 131 AccessQualifier getAccessQualifier(StringRef AccQual) const; 132 133 AddressSpaceQualifier getAddressSpaceQualifier(unsigned AddressSpace) const; 134 135 ValueKind getValueKind(Type *Ty, StringRef TypeQual, 136 StringRef BaseTypeName) const; 137 138 ValueType getValueType(Type *Ty, StringRef TypeName) const; 139 140 std::string getTypeName(Type *Ty, bool Signed) const; 141 142 std::vector<uint32_t> getWorkGroupDimensions(MDNode *Node) const; 143 144 Kernel::CodeProps::Metadata getHSACodeProps( 145 const MachineFunction &MF, 146 const SIProgramInfo &ProgramInfo) const; 147 Kernel::DebugProps::Metadata getHSADebugProps( 148 const MachineFunction &MF, 149 const SIProgramInfo &ProgramInfo) const; 150 151 void emitVersion(); 152 153 void emitPrintf(const Module &Mod); 154 155 void emitKernelLanguage(const Function &Func); 156 157 void emitKernelAttrs(const Function &Func); 158 159 void emitKernelArgs(const Function &Func); 160 161 void emitKernelArg(const Argument &Arg); 162 163 void emitKernelArg(const DataLayout &DL, Type *Ty, ValueKind ValueKind, 164 MaybeAlign PointeeAlign = None, StringRef Name = "", 165 StringRef TypeName = "", StringRef BaseTypeName = "", 166 StringRef AccQual = "", StringRef TypeQual = ""); 167 168 void emitHiddenKernelArgs(const Function &Func); 169 170 const Metadata &getHSAMetadata() const { 171 return HSAMetadata; 172 } 173 174 public: 175 MetadataStreamerV2() = default; 176 ~MetadataStreamerV2() = default; 177 178 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override; 179 180 void begin(const Module &Mod) override; 181 182 void end() override; 183 184 void emitKernel(const MachineFunction &MF, 185 const SIProgramInfo &ProgramInfo) override; 186 }; 187 188 } // end namespace HSAMD 189 } // end namespace AMDGPU 190 } // end namespace llvm 191 192 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H 193