| 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 "Utils/AMDGPUDelayedMCExpr.h" |
| 19 | #include "llvm/BinaryFormat/MsgPackDocument.h" |
| 20 | #include "llvm/Support/AMDGPUMetadata.h" |
| 21 | #include "llvm/Support/Alignment.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class AMDGPUTargetMachine; |
| 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 | |
| 39 | namespace IsaInfo { |
| 40 | class AMDGPUTargetID; |
| 41 | } |
| 42 | |
| 43 | namespace HSAMD { |
| 44 | |
| 45 | class MetadataStreamer { |
| 46 | public: |
| 47 | virtual ~MetadataStreamer() = default; |
| 48 | |
| 49 | virtual bool emitTo(AMDGPUTargetStreamer &TargetStreamer) = 0; |
| 50 | |
| 51 | virtual void begin(const Module &Mod, |
| 52 | const IsaInfo::AMDGPUTargetID &TargetID) = 0; |
| 53 | |
| 54 | virtual void end() = 0; |
| 55 | |
| 56 | virtual void emitKernel(const MachineFunction &MF, |
| 57 | const SIProgramInfo &ProgramInfo) = 0; |
| 58 | |
| 59 | protected: |
| 60 | virtual void emitVersion() = 0; |
| 61 | virtual void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, |
| 62 | msgpack::ArrayDocNode Args) = 0; |
| 63 | virtual void emitKernelAttrs(const AMDGPUTargetMachine &TM, |
| 64 | const Function &Func, |
| 65 | msgpack::MapDocNode Kern) = 0; |
| 66 | }; |
| 67 | |
| 68 | class LLVM_EXTERNAL_VISIBILITY MetadataStreamerMsgPackV4 |
| 69 | : public MetadataStreamer { |
| 70 | protected: |
| 71 | std::unique_ptr<DelayedMCExprs> DelayedExprs = |
| 72 | std::make_unique<DelayedMCExprs>(); |
| 73 | |
| 74 | std::unique_ptr<msgpack::Document> HSAMetadataDoc = |
| 75 | std::make_unique<msgpack::Document>(); |
| 76 | |
| 77 | void dump(StringRef HSAMetadataString) const; |
| 78 | |
| 79 | void verify(StringRef HSAMetadataString) const; |
| 80 | |
| 81 | std::optional<StringRef> getAccessQualifier(StringRef AccQual) const; |
| 82 | |
| 83 | std::optional<StringRef> |
| 84 | getAddressSpaceQualifier(unsigned AddressSpace) const; |
| 85 | |
| 86 | StringRef getValueKind(Type *Ty, StringRef TypeQual, |
| 87 | StringRef BaseTypeName) const; |
| 88 | |
| 89 | std::string getTypeName(Type *Ty, bool Signed) const; |
| 90 | |
| 91 | msgpack::ArrayDocNode getWorkGroupDimensions(MDNode *Node) const; |
| 92 | |
| 93 | msgpack::MapDocNode getHSAKernelProps(const MachineFunction &MF, |
| 94 | const SIProgramInfo &ProgramInfo, |
| 95 | unsigned CodeObjectVersion) const; |
| 96 | |
| 97 | void emitVersion() override; |
| 98 | |
| 99 | void emitTargetID(const IsaInfo::AMDGPUTargetID &TargetID); |
| 100 | |
| 101 | void emitPrintf(const Module &Mod); |
| 102 | |
| 103 | void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern); |
| 104 | |
| 105 | void emitKernelAttrs(const AMDGPUTargetMachine &TM, const Function &Func, |
| 106 | msgpack::MapDocNode Kern) override; |
| 107 | |
| 108 | void emitKernelArgs(const MachineFunction &MF, msgpack::MapDocNode Kern); |
| 109 | |
| 110 | void emitKernelArg(const Argument &Arg, unsigned &Offset, |
| 111 | msgpack::ArrayDocNode Args); |
| 112 | |
| 113 | void emitKernelArg(const DataLayout &DL, Type *Ty, Align Alignment, |
| 114 | StringRef ValueKind, unsigned &Offset, |
| 115 | msgpack::ArrayDocNode Args, |
| 116 | MaybeAlign PointeeAlign = std::nullopt, |
| 117 | StringRef Name = "" , StringRef TypeName = "" , |
| 118 | StringRef BaseTypeName = "" , StringRef ActAccQual = "" , |
| 119 | StringRef AccQual = "" , StringRef TypeQual = "" ); |
| 120 | |
| 121 | void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, |
| 122 | msgpack::ArrayDocNode Args) override; |
| 123 | |
| 124 | msgpack::DocNode &getRootMetadata(StringRef Key) { |
| 125 | return HSAMetadataDoc->getRoot().getMap(/*Convert=*/Convert: true)[Key]; |
| 126 | } |
| 127 | |
| 128 | msgpack::DocNode &getHSAMetadataRoot() { |
| 129 | return HSAMetadataDoc->getRoot(); |
| 130 | } |
| 131 | |
| 132 | public: |
| 133 | MetadataStreamerMsgPackV4() = default; |
| 134 | ~MetadataStreamerMsgPackV4() = default; |
| 135 | |
| 136 | bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override; |
| 137 | |
| 138 | void begin(const Module &Mod, |
| 139 | const IsaInfo::AMDGPUTargetID &TargetID) override; |
| 140 | |
| 141 | void end() override; |
| 142 | |
| 143 | void emitKernel(const MachineFunction &MF, |
| 144 | const SIProgramInfo &ProgramInfo) override; |
| 145 | }; |
| 146 | |
| 147 | class MetadataStreamerMsgPackV5 : public MetadataStreamerMsgPackV4 { |
| 148 | protected: |
| 149 | void emitVersion() override; |
| 150 | void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, |
| 151 | msgpack::ArrayDocNode Args) override; |
| 152 | void emitKernelAttrs(const AMDGPUTargetMachine &TM, const Function &Func, |
| 153 | msgpack::MapDocNode Kern) override; |
| 154 | |
| 155 | public: |
| 156 | MetadataStreamerMsgPackV5() = default; |
| 157 | ~MetadataStreamerMsgPackV5() = default; |
| 158 | }; |
| 159 | |
| 160 | class MetadataStreamerMsgPackV6 final : public MetadataStreamerMsgPackV5 { |
| 161 | protected: |
| 162 | void emitVersion() override; |
| 163 | |
| 164 | public: |
| 165 | MetadataStreamerMsgPackV6() = default; |
| 166 | ~MetadataStreamerMsgPackV6() = default; |
| 167 | }; |
| 168 | |
| 169 | } // end namespace HSAMD |
| 170 | } // end namespace AMDGPU |
| 171 | } // end namespace llvm |
| 172 | |
| 173 | #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H |
| 174 | |