xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 "AMDKernelCodeT.h"
13 #include "Utils/AMDGPUPALMetadata.h"
14 #include "llvm/BinaryFormat/MsgPackDocument.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/MC/MCSubtargetInfo.h"
17 #include "llvm/Support/AMDGPUMetadata.h"
18 #include "llvm/Support/AMDHSAKernelDescriptor.h"
19 
20 namespace llvm {
21 #include "AMDGPUPTNote.h"
22 
23 class DataLayout;
24 class Function;
25 class MCELFStreamer;
26 class MCSymbol;
27 class MDNode;
28 class Module;
29 class Type;
30 
31 class AMDGPUTargetStreamer : public MCTargetStreamer {
32   AMDGPUPALMetadata PALMetadata;
33 
34 protected:
35   MCContext &getContext() const { return Streamer.getContext(); }
36 
37 public:
38   AMDGPUTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
39 
40   AMDGPUPALMetadata *getPALMetadata() { return &PALMetadata; }
41 
42   virtual void EmitDirectiveAMDGCNTarget(StringRef Target) = 0;
43 
44   virtual void EmitDirectiveHSACodeObjectVersion(uint32_t Major,
45                                                  uint32_t Minor) = 0;
46 
47   virtual void EmitDirectiveHSACodeObjectISA(uint32_t Major, uint32_t Minor,
48                                              uint32_t Stepping,
49                                              StringRef VendorName,
50                                              StringRef ArchName) = 0;
51 
52   virtual void EmitAMDKernelCodeT(const amd_kernel_code_t &Header) = 0;
53 
54   virtual void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) = 0;
55 
56   virtual void emitAMDGPULDS(MCSymbol *Symbol, unsigned Size,
57                              Align Alignment) = 0;
58 
59   /// \returns True on success, false on failure.
60   virtual bool EmitISAVersion(StringRef IsaVersionString) = 0;
61 
62   /// \returns True on success, false on failure.
63   virtual bool EmitHSAMetadataV2(StringRef HSAMetadataString);
64 
65   /// \returns True on success, false on failure.
66   virtual bool EmitHSAMetadataV3(StringRef HSAMetadataString);
67 
68   /// Emit HSA Metadata
69   ///
70   /// When \p Strict is true, known metadata elements must already be
71   /// well-typed. When \p Strict is false, known types are inferred and
72   /// the \p HSAMetadata structure is updated with the correct types.
73   ///
74   /// \returns True on success, false on failure.
75   virtual bool EmitHSAMetadata(msgpack::Document &HSAMetadata, bool Strict) = 0;
76 
77   /// \returns True on success, false on failure.
78   virtual bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) = 0;
79 
80   /// \returns True on success, false on failure.
81   virtual bool EmitCodeEnd() = 0;
82 
83   virtual void EmitAmdhsaKernelDescriptor(
84       const MCSubtargetInfo &STI, StringRef KernelName,
85       const amdhsa::kernel_descriptor_t &KernelDescriptor, uint64_t NextVGPR,
86       uint64_t NextSGPR, bool ReserveVCC, bool ReserveFlatScr,
87       bool ReserveXNACK) = 0;
88 
89   static StringRef getArchNameFromElfMach(unsigned ElfMach);
90   static unsigned getElfMach(StringRef GPU);
91 };
92 
93 class AMDGPUTargetAsmStreamer final : public AMDGPUTargetStreamer {
94   formatted_raw_ostream &OS;
95 public:
96   AMDGPUTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
97 
98   void finish() override;
99 
100   void EmitDirectiveAMDGCNTarget(StringRef Target) override;
101 
102   void EmitDirectiveHSACodeObjectVersion(uint32_t Major,
103                                          uint32_t Minor) override;
104 
105   void EmitDirectiveHSACodeObjectISA(uint32_t Major, uint32_t Minor,
106                                      uint32_t Stepping, StringRef VendorName,
107                                      StringRef ArchName) override;
108 
109   void EmitAMDKernelCodeT(const amd_kernel_code_t &Header) override;
110 
111   void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override;
112 
113   void emitAMDGPULDS(MCSymbol *Sym, unsigned Size, Align Alignment) override;
114 
115   /// \returns True on success, false on failure.
116   bool EmitISAVersion(StringRef IsaVersionString) override;
117 
118   /// \returns True on success, false on failure.
119   bool EmitHSAMetadata(msgpack::Document &HSAMetadata, bool Strict) override;
120 
121   /// \returns True on success, false on failure.
122   bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) override;
123 
124   /// \returns True on success, false on failure.
125   bool EmitCodeEnd() override;
126 
127   void EmitAmdhsaKernelDescriptor(
128       const MCSubtargetInfo &STI, StringRef KernelName,
129       const amdhsa::kernel_descriptor_t &KernelDescriptor, uint64_t NextVGPR,
130       uint64_t NextSGPR, bool ReserveVCC, bool ReserveFlatScr,
131       bool ReserveXNACK) override;
132 };
133 
134 class AMDGPUTargetELFStreamer final : public AMDGPUTargetStreamer {
135   MCStreamer &Streamer;
136   Triple::OSType Os;
137 
138   void EmitNote(StringRef Name, const MCExpr *DescSize, unsigned NoteType,
139                 function_ref<void(MCELFStreamer &)> EmitDesc);
140 
141 public:
142   AMDGPUTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI);
143 
144   MCELFStreamer &getStreamer();
145 
146   void finish() override;
147 
148   void EmitDirectiveAMDGCNTarget(StringRef Target) override;
149 
150   void EmitDirectiveHSACodeObjectVersion(uint32_t Major,
151                                          uint32_t Minor) override;
152 
153   void EmitDirectiveHSACodeObjectISA(uint32_t Major, uint32_t Minor,
154                                      uint32_t Stepping, StringRef VendorName,
155                                      StringRef ArchName) override;
156 
157   void EmitAMDKernelCodeT(const amd_kernel_code_t &Header) override;
158 
159   void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override;
160 
161   void emitAMDGPULDS(MCSymbol *Sym, unsigned Size, Align Alignment) override;
162 
163   /// \returns True on success, false on failure.
164   bool EmitISAVersion(StringRef IsaVersionString) override;
165 
166   /// \returns True on success, false on failure.
167   bool EmitHSAMetadata(msgpack::Document &HSAMetadata, bool Strict) override;
168 
169   /// \returns True on success, false on failure.
170   bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) override;
171 
172   /// \returns True on success, false on failure.
173   bool EmitCodeEnd() override;
174 
175   void EmitAmdhsaKernelDescriptor(
176       const MCSubtargetInfo &STI, StringRef KernelName,
177       const amdhsa::kernel_descriptor_t &KernelDescriptor, uint64_t NextVGPR,
178       uint64_t NextSGPR, bool ReserveVCC, bool ReserveFlatScr,
179       bool ReserveXNACK) override;
180 };
181 
182 }
183 #endif
184