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