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 34 namespace AMDGPU { 35 36 namespace IsaInfo { 37 class AMDGPUTargetID; 38 } 39 40 namespace HSAMD { 41 42 class MetadataStreamer { 43 public: 44 virtual ~MetadataStreamer() = default; 45 46 virtual bool emitTo(AMDGPUTargetStreamer &TargetStreamer) = 0; 47 48 virtual void begin(const Module &Mod, 49 const IsaInfo::AMDGPUTargetID &TargetID) = 0; 50 51 virtual void end() = 0; 52 53 virtual void emitKernel(const MachineFunction &MF, 54 const SIProgramInfo &ProgramInfo) = 0; 55 56 protected: 57 virtual void emitVersion() = 0; 58 virtual void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, 59 msgpack::ArrayDocNode Args) = 0; 60 virtual void emitKernelAttrs(const Function &Func, 61 msgpack::MapDocNode Kern) = 0; 62 }; 63 64 class MetadataStreamerMsgPackV4 : 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 std::optional<StringRef> getAccessQualifier(StringRef AccQual) const; 74 75 std::optional<StringRef> 76 getAddressSpaceQualifier(unsigned AddressSpace) const; 77 78 StringRef getValueKind(Type *Ty, StringRef TypeQual, 79 StringRef BaseTypeName) const; 80 81 std::string getTypeName(Type *Ty, bool Signed) const; 82 83 msgpack::ArrayDocNode getWorkGroupDimensions(MDNode *Node) const; 84 85 msgpack::MapDocNode getHSAKernelProps(const MachineFunction &MF, 86 const SIProgramInfo &ProgramInfo, 87 unsigned CodeObjectVersion) const; 88 89 void emitVersion() override; 90 91 void emitTargetID(const IsaInfo::AMDGPUTargetID &TargetID); 92 93 void emitPrintf(const Module &Mod); 94 95 void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern); 96 97 void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override; 98 99 void emitKernelArgs(const MachineFunction &MF, msgpack::MapDocNode Kern); 100 101 void emitKernelArg(const Argument &Arg, unsigned &Offset, 102 msgpack::ArrayDocNode Args); 103 104 void emitKernelArg(const DataLayout &DL, Type *Ty, Align Alignment, 105 StringRef ValueKind, unsigned &Offset, 106 msgpack::ArrayDocNode Args, 107 MaybeAlign PointeeAlign = std::nullopt, 108 StringRef Name = "", StringRef TypeName = "", 109 StringRef BaseTypeName = "", StringRef ActAccQual = "", 110 StringRef AccQual = "", StringRef TypeQual = ""); 111 112 void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, 113 msgpack::ArrayDocNode Args) override; 114 115 msgpack::DocNode &getRootMetadata(StringRef Key) { 116 return HSAMetadataDoc->getRoot().getMap(/*Convert=*/true)[Key]; 117 } 118 119 msgpack::DocNode &getHSAMetadataRoot() { 120 return HSAMetadataDoc->getRoot(); 121 } 122 123 public: 124 MetadataStreamerMsgPackV4() = default; 125 ~MetadataStreamerMsgPackV4() = default; 126 127 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override; 128 129 void begin(const Module &Mod, 130 const IsaInfo::AMDGPUTargetID &TargetID) override; 131 132 void end() override; 133 134 void emitKernel(const MachineFunction &MF, 135 const SIProgramInfo &ProgramInfo) override; 136 }; 137 138 class MetadataStreamerMsgPackV5 final : public MetadataStreamerMsgPackV4 { 139 protected: 140 void emitVersion() override; 141 void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, 142 msgpack::ArrayDocNode Args) override; 143 void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override; 144 145 public: 146 MetadataStreamerMsgPackV5() = default; 147 ~MetadataStreamerMsgPackV5() = default; 148 }; 149 150 } // end namespace HSAMD 151 } // end namespace AMDGPU 152 } // end namespace llvm 153 154 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H 155