LLVM 24.0.0git
NVPTXTargetStreamer.cpp
Go to the documentation of this file.
1//=====- NVPTXTargetStreamer.cpp - NVPTXTargetStreamer class ------------=====//
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// This file implements the NVPTXTargetStreamer class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "NVPTXTargetStreamer.h"
14#include "NVPTXUtilities.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCSymbol.h"
23
24using namespace llvm;
25
26//
27// NVPTXTargetStreamer Implemenation
28//
31
33 for (const std::string &S : DwarfFiles)
35 DwarfFiles.clear();
36}
37
39 if (HasSections)
40 getStreamer().emitRawText("\t}");
41}
42
46
47static bool isDwarfSection(const MCObjectFileInfo *FI,
48 const MCSection *Section) {
49 // FIXME: the checks for the DWARF sections are very fragile and should be
50 // fixed up in a followup patch.
51 if (!Section || Section->isText())
52 return false;
53 return Section == FI->getDwarfAbbrevSection() ||
54 Section == FI->getDwarfInfoSection() ||
55 Section == FI->getDwarfMacinfoSection() ||
56 Section == FI->getDwarfFrameSection() ||
57 Section == FI->getDwarfAddrSection() ||
58 Section == FI->getDwarfRangesSection() ||
59 Section == FI->getDwarfARangesSection() ||
60 Section == FI->getDwarfLocSection() ||
61 Section == FI->getDwarfStrSection() ||
62 Section == FI->getDwarfLineSection() ||
63 Section == FI->getDwarfStrOffSection() ||
64 Section == FI->getDwarfLineStrSection() ||
65 Section == FI->getDwarfPubNamesSection() ||
66 Section == FI->getDwarfPubTypesSection() ||
67 Section == FI->getDwarfSwiftASTSection() ||
68 Section == FI->getDwarfTypesDWOSection() ||
69 Section == FI->getDwarfAbbrevDWOSection() ||
70 Section == FI->getDwarfAccelObjCSection() ||
71 Section == FI->getDwarfAccelNamesSection() ||
72 Section == FI->getDwarfAccelTypesSection() ||
73 Section == FI->getDwarfAccelNamespaceSection() ||
74 Section == FI->getDwarfLocDWOSection() ||
75 Section == FI->getDwarfStrDWOSection() ||
76 Section == FI->getDwarfCUIndexSection() ||
77 Section == FI->getDwarfInfoDWOSection() ||
78 Section == FI->getDwarfLineDWOSection() ||
79 Section == FI->getDwarfTUIndexSection() ||
80 Section == FI->getDwarfStrOffDWOSection() ||
81 Section == FI->getDwarfDebugNamesSection() ||
82 Section == FI->getDwarfDebugInlineSection() ||
83 Section == FI->getDwarfGnuPubNamesSection() ||
84 Section == FI->getDwarfGnuPubTypesSection();
85}
86
88 MCSection *Section, uint32_t SubSection,
89 raw_ostream &OS) {
90 assert(!SubSection && "SubSection is not null!");
92 // Emit closing brace for DWARF sections only.
93 if (isDwarfSection(FI, CurSection))
94 OS << "\t}\n";
95 if (isDwarfSection(FI, Section)) {
96 // Emit DWARF .file directives in the outermost scope.
98 OS << "\t.section\t" << Section->getName() << '\n';
99 // DWARF sections are enclosed into braces - emit the open one.
100 OS << "\t{\n";
101 HasSections = true;
102 }
103}
104
107 // TODO: enable this once the bug in the ptxas with the packed bytes is
108 // resolved. Currently, (it is confirmed by NVidia) it causes a crash in
109 // ptxas.
110#if 0
111 const MCAsmInfo &MAI = Streamer.getContext().getAsmInfo();
112 const char *Directive = MAI.getData8bitsDirective();
113 unsigned NumElements = Data.size();
114 const unsigned MaxLen = 40;
115 unsigned NumChunks = 1 + ((NumElements - 1) / MaxLen);
116 // Split the very long directives into several parts if the limit is
117 // specified.
118 for (unsigned I = 0; I < NumChunks; ++I) {
120 raw_svector_ostream OS(Str);
121
122 const char *Label = Directive;
123 for (auto It = std::next(Data.bytes_begin(), I * MaxLen),
124 End = (I == NumChunks - 1)
125 ? Data.bytes_end()
126 : std::next(Data.bytes_begin(), (I + 1) * MaxLen);
127 It != End; ++It) {
128 OS << Label << (unsigned)*It;
129 if (Label == Directive)
130 Label = ",";
131 }
132 Streamer.emitRawText(OS.str());
133 }
134#endif
135}
136
138 if (Value->getKind() == MCExpr::SymbolRef) {
140 StringRef SymName = SRE.getSymbol().getName();
141 if (!SymName.starts_with(".debug")) {
142 Streamer.emitRawText(NVPTX::getValidPTXIdentifier(SymName));
143 return;
144 }
145 // Fall through to the normal printing.
146 }
147 // Otherwise, print the Value normally.
149}
150
151//
152// NVPTXAsmTargetStreamer Implementation
153//
154
159
161 OS << "//\n"
162 "// Generated by LLVM NVPTX Back-End\n"
163 "//\n"
164 "\n";
165}
166
168 OS << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n";
169}
170
172 bool TexModeIndependent,
173 bool HasDebug) {
174 OS << ".target " << Target;
175 if (TexModeIndependent)
176 OS << ", texmode_independent";
177 if (HasDebug)
178 OS << ", debug";
179 OS << "\n";
180}
181
183 OS << ".address_size " << AddrSize << "\n"
184 << "\n";
185}
186
189 const MCAsmInfo &MAI = getStreamer().getContext().getAsmInfo();
190 OS << "\t.branchtargets\n\t\t";
192 Targets, OS, [&](const MCSymbol *Target) { Target->print(OS, &MAI); },
193 ",\n\t\t");
194 OS << ";\n";
195}
196
198 StringRef Name,
199 std::optional<unsigned> Count) {
200 OS << "\t.reg ";
201 if (SizeInBits == 1)
202 OS << ".pred";
203 else
204 OS << ".b" << SizeInBits;
205
206 OS << " \t" << Name;
207 if (Count)
208 OS << "<" << *Count << ">";
209 OS << ";\n";
210}
211
213 const MCSymbol *Name,
214 uint64_t Size) {
215 const MCAsmInfo &MAI = getStreamer().getContext().getAsmInfo();
216 OS << "\t.local .align " << Alignment.value() << " .b8 \t";
217 Name->print(OS, &MAI);
218 OS << "[" << Size << "];\n";
219}
220
222 const MCSymbol *Aliasee) {
223 const MCAsmInfo &MAI = getStreamer().getContext().getAsmInfo();
224 OS << ".alias ";
225 Name->print(OS, &MAI);
226 OS << ", ";
227 Aliasee->print(OS, &MAI);
228 OS << ";\n";
229}
230
232 OS << "\t.pragma \"" << Pragma << "\";\n";
233}
234
236 OS << "\t.section\t" << Name << "\t{\t}\n";
237}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define I(x, y, z)
Definition MD5.cpp:57
static bool isDwarfSection(const MCObjectFileInfo *FI, const MCSection *Section)
This file contains some templates that are useful if you are working with the STL at all.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:67
const char * getData8bitsDirective() const
Definition MCAsmInfo.h:472
const MCObjectFileInfo * getObjectFileInfo() const
Definition MCContext.h:413
const MCAsmInfo & getAsmInfo() const
Definition MCContext.h:409
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
@ SymbolRef
References to labels and assigned expressions.
Definition MCExpr.h:43
MCSection * getDwarfAccelTypesSection() const
MCSection * getDwarfGnuPubNamesSection() const
MCSection * getDwarfStrOffDWOSection() const
MCSection * getDwarfRangesSection() const
MCSection * getDwarfAccelNamespaceSection() const
MCSection * getDwarfLineDWOSection() const
MCSection * getDwarfStrOffSection() const
MCSection * getDwarfInfoDWOSection() const
MCSection * getDwarfTypesDWOSection() const
MCSection * getDwarfPubNamesSection() const
MCSection * getDwarfStrSection() const
MCSection * getDwarfLineStrSection() const
MCSection * getDwarfTUIndexSection() const
MCSection * getDwarfDebugNamesSection() const
MCSection * getDwarfAddrSection() const
MCSection * getDwarfLineSection() const
MCSection * getDwarfInfoSection() const
MCSection * getDwarfFrameSection() const
MCSection * getDwarfPubTypesSection() const
const MCSection * getDwarfDebugInlineSection() const
MCSection * getDwarfGnuPubTypesSection() const
MCSection * getDwarfStrDWOSection() const
MCSection * getDwarfAccelNamesSection() const
MCSection * getDwarfAbbrevDWOSection() const
MCSection * getDwarfAbbrevSection() const
MCSection * getDwarfSwiftASTSection() const
MCSection * getDwarfCUIndexSection() const
MCSection * getDwarfMacinfoSection() const
MCSection * getDwarfLocDWOSection() const
MCSection * getDwarfARangesSection() const
MCSection * getDwarfAccelObjCSection() const
MCSection * getDwarfLocSection() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:573
Streaming machine code generation interface.
Definition MCStreamer.h:222
MCContext & getContext() const
Definition MCStreamer.h:326
void emitRawText(const Twine &String)
If this file is backed by a assembly streamer, this dumps the specified string in the output ....
Represent a reference to a symbol from inside an expression.
Definition MCExpr.h:190
const MCSymbol & getSymbol() const
Definition MCExpr.h:226
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition MCSymbol.cpp:59
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
virtual void emitValue(const MCExpr *Value)
MCStreamer & getStreamer()
Definition MCStreamer.h:103
virtual void emitRawBytes(StringRef Data)
Emit the bytes in Data into the output.
MCStreamer & Streamer
Definition MCStreamer.h:97
MCTargetStreamer(MCStreamer &S)
void emitBanner() override
Emit the banner which specifies details of PTX generator.
void emitVersionDirective(unsigned PTXVersion) override
Emit the PTX ISA version number.
void emitTargetDirective(StringRef Target, bool TexModeIndependent, bool HasDebug) override
Emit architecture and platform target.
void emitPragmaDirective(StringRef Pragma) override
Emit a pragma governing the code that follows it.
void emitBranchTargetsDirective(ArrayRef< const MCSymbol * > Targets) override
Emit the list of branch targets a brx.idx may jump to.
void emitRegDirective(unsigned SizeInBits, StringRef Name, std::optional< unsigned > Count) override
Declare a register SizeInBits wide.
void emitEmptySectionDirective(StringRef Name) override
Emit a section with an empty body.
void emitLocalDirective(Align Alignment, const MCSymbol *Name, uint64_t Size) override
Declare Size bytes of local (stack) memory named Name.
void emitAddressSizeDirective(unsigned AddrSize) override
Emit address size used for this PTX module.
void emitAliasDirective(const MCSymbol *Name, const MCSymbol *Aliasee) override
Emit an alias from Name to Aliasee.
NVPTXAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS)
void emitDwarfFileDirective(StringRef Directive) override
Record DWARF file directives for later output.
void outputDwarfFileDirectives()
Outputs the list of the DWARF '.file' directives to the streamer.
void emitRawBytes(StringRef Data) override
Emit the bytes in Data into the output.
void changeSection(const MCSection *CurSection, MCSection *Section, uint32_t SubSection, raw_ostream &OS) override
Update streamer for a new active section.
void emitValue(const MCExpr *Value) override
Makes sure that labels are mangled the same way as the actual symbols.
void closeLastSection()
Close last section.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an SmallVector or SmallString.
StringRef str() const
Return a StringRef for the vector contents.
std::string getValidPTXIdentifier(StringRef Name)
This is an optimization pass for GlobalISel generic memory operations.
void interleave(ForwardIterator begin, ForwardIterator end, UnaryFunctor each_fn, NullaryFunctor between_fn)
An STL-style algorithm similar to std::for_each that applies a second functor between every pair of e...
Definition STLExtras.h:2275
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77