| 1 | //===-- AMDGPUTargetStreamer.h - AMDGPU Target Streamer --------*- 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 | #ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUTARGETSTREAMER_H |
| 10 | #define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUTARGETSTREAMER_H |
| 11 | |
| 12 | #include "Utils/AMDGPUBaseInfo.h" |
| 13 | #include "Utils/AMDGPUPALMetadata.h" |
| 14 | #include "llvm/ADT/SmallVector.h" |
| 15 | #include "llvm/MC/MCStreamer.h" |
| 16 | #include <string> |
| 17 | #include <utility> |
| 18 | |
| 19 | namespace llvm { |
| 20 | |
| 21 | class MCELFStreamer; |
| 22 | class MCSymbol; |
| 23 | class formatted_raw_ostream; |
| 24 | |
| 25 | namespace AMDGPU { |
| 26 | |
| 27 | struct AMDGPUMCKernelCodeT; |
| 28 | struct MCKernelDescriptor; |
| 29 | namespace HSAMD { |
| 30 | struct Metadata; |
| 31 | } |
| 32 | |
| 33 | struct FuncInfo { |
| 34 | uint32_t NumSGPR = 0; |
| 35 | uint32_t NumArchVGPR = 0; |
| 36 | uint32_t NumAccVGPR = 0; |
| 37 | uint32_t PrivateSegmentSize = 0; |
| 38 | bool UsesVCC = false; |
| 39 | bool UsesFlatScratch = false; |
| 40 | bool HasDynStack = false; |
| 41 | |
| 42 | MCSymbol *Sym = nullptr; |
| 43 | }; |
| 44 | |
| 45 | struct InfoSectionData { |
| 46 | SmallVector<FuncInfo, 8> Funcs; |
| 47 | SmallVector<std::pair<MCSymbol *, MCSymbol *>, 4> Uses; |
| 48 | SmallVector<std::pair<MCSymbol *, MCSymbol *>, 8> Calls; |
| 49 | SmallVector<std::pair<MCSymbol *, std::string>, 4> IndirectCalls; |
| 50 | SmallVector<std::pair<MCSymbol *, std::string>, 4> TypeIds; |
| 51 | }; |
| 52 | |
| 53 | } // namespace AMDGPU |
| 54 | |
| 55 | class AMDGPUTargetStreamer : public MCTargetStreamer { |
| 56 | AMDGPUPALMetadata PALMetadata; |
| 57 | bool AMDGCNTargetEmitted = false; |
| 58 | |
| 59 | protected: |
| 60 | // TODO: Move HSAMetadataStream to AMDGPUTargetStreamer. |
| 61 | std::optional<AMDGPU::TargetID> TargetID; |
| 62 | unsigned CodeObjectVersion; |
| 63 | |
| 64 | MCContext &getContext() const { return Streamer.getContext(); } |
| 65 | |
| 66 | public: |
| 67 | AMDGPUTargetStreamer(MCStreamer &S) |
| 68 | : MCTargetStreamer(S), |
| 69 | // Assume the default COV for now, EmitDirectiveAMDHSACodeObjectVersion |
| 70 | // will update this if it is encountered. |
| 71 | CodeObjectVersion(AMDGPU::getDefaultAMDHSACodeObjectVersion()) {} |
| 72 | |
| 73 | AMDGPUPALMetadata *getPALMetadata() { return &PALMetadata; } |
| 74 | |
| 75 | virtual void EmitDirectiveAMDGCNTarget(){}; |
| 76 | |
| 77 | /// Emit the .amdgcn_target directive unless it has already been emitted to |
| 78 | /// this streamer. The code generator and the parsers for any inline assembly |
| 79 | /// in the module share one streamer, and the directive must appear only once |
| 80 | /// per output file. |
| 81 | void emitDirectiveAMDGCNTargetOnce() { |
| 82 | if (AMDGCNTargetEmitted) |
| 83 | return; |
| 84 | AMDGCNTargetEmitted = true; |
| 85 | EmitDirectiveAMDGCNTarget(); |
| 86 | } |
| 87 | |
| 88 | virtual void EmitDirectiveAMDHSACodeObjectVersion(unsigned COV) { |
| 89 | CodeObjectVersion = COV; |
| 90 | } |
| 91 | |
| 92 | virtual void EmitAMDKernelCodeT(AMDGPU::AMDGPUMCKernelCodeT &) {}; |
| 93 | |
| 94 | virtual void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type){}; |
| 95 | |
| 96 | virtual void emitAMDGPULDS(MCSymbol *Symbol, unsigned Size, Align Alignment) { |
| 97 | } |
| 98 | |
| 99 | virtual void EmitMCResourceInfo( |
| 100 | const MCSymbol *NumVGPR, const MCSymbol *NumAGPR, |
| 101 | const MCSymbol *NumExplicitSGPR, const MCSymbol *NumNamedBarrier, |
| 102 | const MCSymbol *PrivateSegmentSize, const MCSymbol *UsesVCC, |
| 103 | const MCSymbol *UsesFlatScratch, const MCSymbol *HasDynamicallySizedStack, |
| 104 | const MCSymbol *HasRecursion, const MCSymbol *HasIndirectCall) {}; |
| 105 | |
| 106 | virtual void EmitMCResourceMaximums(const MCSymbol *MaxVGPR, |
| 107 | const MCSymbol *MaxAGPR, |
| 108 | const MCSymbol *MaxSGPR, |
| 109 | const MCSymbol *MaxNamedBarrier) {}; |
| 110 | |
| 111 | /// \returns True on success, false on failure. |
| 112 | virtual bool EmitISAVersion() { return true; } |
| 113 | |
| 114 | /// \returns True on success, false on failure. |
| 115 | virtual bool EmitHSAMetadataV3(StringRef HSAMetadataString); |
| 116 | |
| 117 | /// Emit HSA Metadata |
| 118 | /// |
| 119 | /// When \p Strict is true, known metadata elements must already be |
| 120 | /// well-typed. When \p Strict is false, known types are inferred and |
| 121 | /// the \p HSAMetadata structure is updated with the correct types. |
| 122 | /// |
| 123 | /// \returns True on success, false on failure. |
| 124 | virtual bool EmitHSAMetadata(msgpack::Document &HSAMetadata, bool Strict) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | /// \returns True on success, false on failure. |
| 129 | virtual bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) { |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | /// \returns True on success, false on failure. |
| 134 | virtual bool EmitCodeEnd(const MCSubtargetInfo &STI) { return true; } |
| 135 | |
| 136 | virtual void |
| 137 | EmitAmdhsaKernelDescriptor(const MCSubtargetInfo &STI, StringRef KernelName, |
| 138 | const AMDGPU::MCKernelDescriptor &KernelDescriptor, |
| 139 | const MCExpr *NextVGPR, const MCExpr *NextSGPR, |
| 140 | const MCExpr *ReserveVCC, |
| 141 | const MCExpr *ReserveFlatScr) {} |
| 142 | |
| 143 | virtual void emitAMDGPUInfo(const AMDGPU::InfoSectionData &Data) {} |
| 144 | |
| 145 | static unsigned getElfMach(StringRef GPU); |
| 146 | |
| 147 | const std::optional<AMDGPU::TargetID> &getTargetID() const { |
| 148 | return TargetID; |
| 149 | } |
| 150 | std::optional<AMDGPU::TargetID> &getTargetID() { return TargetID; } |
| 151 | void initializeTargetID(const MCSubtargetInfo &STI, |
| 152 | bool ApplyFeatureString = false); |
| 153 | }; |
| 154 | |
| 155 | class AMDGPUTargetAsmStreamer final : public AMDGPUTargetStreamer { |
| 156 | formatted_raw_ostream &OS; |
| 157 | public: |
| 158 | AMDGPUTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS); |
| 159 | |
| 160 | void finish() override; |
| 161 | |
| 162 | void EmitDirectiveAMDGCNTarget() override; |
| 163 | |
| 164 | void EmitDirectiveAMDHSACodeObjectVersion(unsigned COV) override; |
| 165 | |
| 166 | void EmitAMDKernelCodeT(AMDGPU::AMDGPUMCKernelCodeT &) override; |
| 167 | |
| 168 | void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override; |
| 169 | |
| 170 | void emitAMDGPULDS(MCSymbol *Sym, unsigned Size, Align Alignment) override; |
| 171 | |
| 172 | void EmitMCResourceInfo( |
| 173 | const MCSymbol *NumVGPR, const MCSymbol *NumAGPR, |
| 174 | const MCSymbol *NumExplicitSGPR, const MCSymbol *NumNamedBarrier, |
| 175 | const MCSymbol *PrivateSegmentSize, const MCSymbol *UsesVCC, |
| 176 | const MCSymbol *UsesFlatScratch, const MCSymbol *HasDynamicallySizedStack, |
| 177 | const MCSymbol *HasRecursion, const MCSymbol *HasIndirectCall) override; |
| 178 | |
| 179 | void EmitMCResourceMaximums(const MCSymbol *MaxVGPR, const MCSymbol *MaxAGPR, |
| 180 | const MCSymbol *MaxSGPR, |
| 181 | const MCSymbol *MaxNamedBarrier) override; |
| 182 | |
| 183 | /// \returns True on success, false on failure. |
| 184 | bool EmitISAVersion() override; |
| 185 | |
| 186 | /// \returns True on success, false on failure. |
| 187 | bool EmitHSAMetadata(msgpack::Document &HSAMetadata, bool Strict) override; |
| 188 | |
| 189 | /// \returns True on success, false on failure. |
| 190 | bool EmitCodeEnd(const MCSubtargetInfo &STI) override; |
| 191 | |
| 192 | void |
| 193 | EmitAmdhsaKernelDescriptor(const MCSubtargetInfo &STI, StringRef KernelName, |
| 194 | const AMDGPU::MCKernelDescriptor &KernelDescriptor, |
| 195 | const MCExpr *NextVGPR, const MCExpr *NextSGPR, |
| 196 | const MCExpr *ReserveVCC, |
| 197 | const MCExpr *ReserveFlatScr) override; |
| 198 | |
| 199 | void emitAMDGPUInfo(const AMDGPU::InfoSectionData &Data) override; |
| 200 | }; |
| 201 | |
| 202 | class AMDGPUTargetELFStreamer final : public AMDGPUTargetStreamer { |
| 203 | const MCSubtargetInfo &STI; |
| 204 | MCStreamer &Streamer; |
| 205 | |
| 206 | void EmitNote(StringRef Name, const MCExpr *DescSize, unsigned NoteType, |
| 207 | function_ref<void(MCELFStreamer &)> EmitDesc); |
| 208 | |
| 209 | unsigned getEFlags(); |
| 210 | |
| 211 | unsigned getEFlagsR600(); |
| 212 | unsigned getEFlagsAMDGCN(); |
| 213 | |
| 214 | unsigned getEFlagsUnknownOS(); |
| 215 | unsigned getEFlagsAMDHSA(); |
| 216 | unsigned getEFlagsAMDPAL(); |
| 217 | unsigned getEFlagsMesa3D(); |
| 218 | |
| 219 | unsigned getEFlagsV3(); |
| 220 | unsigned getEFlagsV4(); |
| 221 | unsigned getEFlagsV6(); |
| 222 | |
| 223 | public: |
| 224 | AMDGPUTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI); |
| 225 | |
| 226 | MCELFStreamer &getStreamer(); |
| 227 | |
| 228 | void finish() override; |
| 229 | |
| 230 | void EmitDirectiveAMDGCNTarget() override; |
| 231 | |
| 232 | void EmitAMDKernelCodeT(AMDGPU::AMDGPUMCKernelCodeT &) override; |
| 233 | |
| 234 | void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override; |
| 235 | |
| 236 | void emitAMDGPULDS(MCSymbol *Sym, unsigned Size, Align Alignment) override; |
| 237 | |
| 238 | /// \returns True on success, false on failure. |
| 239 | bool EmitISAVersion() override; |
| 240 | |
| 241 | /// \returns True on success, false on failure. |
| 242 | bool EmitHSAMetadata(msgpack::Document &HSAMetadata, bool Strict) override; |
| 243 | |
| 244 | /// \returns True on success, false on failure. |
| 245 | bool EmitCodeEnd(const MCSubtargetInfo &STI) override; |
| 246 | |
| 247 | void |
| 248 | EmitAmdhsaKernelDescriptor(const MCSubtargetInfo &STI, StringRef KernelName, |
| 249 | const AMDGPU::MCKernelDescriptor &KernelDescriptor, |
| 250 | const MCExpr *NextVGPR, const MCExpr *NextSGPR, |
| 251 | const MCExpr *ReserveVCC, |
| 252 | const MCExpr *ReserveFlatScr) override; |
| 253 | |
| 254 | void emitAMDGPUInfo(const AMDGPU::InfoSectionData &Data) override; |
| 255 | }; |
| 256 | } |
| 257 | #endif |
| 258 | |