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