LLVM 23.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/MC/MCAsmInfo.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCSymbol.h"
22
23using namespace llvm;
24
25//
26// NVPTXTargetStreamer Implemenation
27//
30
32 for (const std::string &S : DwarfFiles)
34 DwarfFiles.clear();
35}
36
38 if (HasSections)
39 getStreamer().emitRawText("\t}");
40}
41
45
46static bool isDwarfSection(const MCObjectFileInfo *FI,
47 const MCSection *Section) {
48 // FIXME: the checks for the DWARF sections are very fragile and should be
49 // fixed up in a followup patch.
50 if (!Section || Section->isText())
51 return false;
52 return Section == FI->getDwarfAbbrevSection() ||
53 Section == FI->getDwarfInfoSection() ||
54 Section == FI->getDwarfMacinfoSection() ||
55 Section == FI->getDwarfFrameSection() ||
56 Section == FI->getDwarfAddrSection() ||
57 Section == FI->getDwarfRangesSection() ||
58 Section == FI->getDwarfARangesSection() ||
59 Section == FI->getDwarfLocSection() ||
60 Section == FI->getDwarfStrSection() ||
61 Section == FI->getDwarfLineSection() ||
62 Section == FI->getDwarfStrOffSection() ||
63 Section == FI->getDwarfLineStrSection() ||
64 Section == FI->getDwarfPubNamesSection() ||
65 Section == FI->getDwarfPubTypesSection() ||
66 Section == FI->getDwarfSwiftASTSection() ||
67 Section == FI->getDwarfTypesDWOSection() ||
68 Section == FI->getDwarfAbbrevDWOSection() ||
69 Section == FI->getDwarfAccelObjCSection() ||
70 Section == FI->getDwarfAccelNamesSection() ||
71 Section == FI->getDwarfAccelTypesSection() ||
72 Section == FI->getDwarfAccelNamespaceSection() ||
73 Section == FI->getDwarfLocDWOSection() ||
74 Section == FI->getDwarfStrDWOSection() ||
75 Section == FI->getDwarfCUIndexSection() ||
76 Section == FI->getDwarfInfoDWOSection() ||
77 Section == FI->getDwarfLineDWOSection() ||
78 Section == FI->getDwarfTUIndexSection() ||
79 Section == FI->getDwarfStrOffDWOSection() ||
80 Section == FI->getDwarfDebugNamesSection() ||
81 Section == FI->getDwarfDebugInlineSection() ||
82 Section == FI->getDwarfGnuPubNamesSection() ||
83 Section == FI->getDwarfGnuPubTypesSection();
84}
85
87 MCSection *Section, uint32_t SubSection,
88 raw_ostream &OS) {
89 assert(!SubSection && "SubSection is not null!");
91 // Emit closing brace for DWARF sections only.
92 if (isDwarfSection(FI, CurSection))
93 OS << "\t}\n";
94 if (isDwarfSection(FI, Section)) {
95 // Emit DWARF .file directives in the outermost scope.
97 OS << "\t.section\t" << Section->getName() << '\n';
98 // DWARF sections are enclosed into braces - emit the open one.
99 OS << "\t{\n";
100 HasSections = true;
101 }
102}
103
106 // TODO: enable this once the bug in the ptxas with the packed bytes is
107 // resolved. Currently, (it is confirmed by NVidia) it causes a crash in
108 // ptxas.
109#if 0
110 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
111 const char *Directive = MAI->getData8bitsDirective();
112 unsigned NumElements = Data.size();
113 const unsigned MaxLen = 40;
114 unsigned NumChunks = 1 + ((NumElements - 1) / MaxLen);
115 // Split the very long directives into several parts if the limit is
116 // specified.
117 for (unsigned I = 0; I < NumChunks; ++I) {
119 raw_svector_ostream OS(Str);
120
121 const char *Label = Directive;
122 for (auto It = std::next(Data.bytes_begin(), I * MaxLen),
123 End = (I == NumChunks - 1)
124 ? Data.bytes_end()
125 : std::next(Data.bytes_begin(), (I + 1) * MaxLen);
126 It != End; ++It) {
127 OS << Label << (unsigned)*It;
128 if (Label == Directive)
129 Label = ",";
130 }
131 Streamer.emitRawText(OS.str());
132 }
133#endif
134}
135
137 if (Value->getKind() == MCExpr::SymbolRef) {
139 StringRef SymName = SRE.getSymbol().getName();
140 if (!SymName.starts_with(".debug")) {
141 Streamer.emitRawText(NVPTX::getValidPTXIdentifier(SymName));
142 return;
143 }
144 // Fall through to the normal printing.
145 }
146 // Otherwise, print the Value normally.
148}
149
150//
151// NVPTXAsmTargetStreamer Implementation
152//
153
158
160 OS << "//\n"
161 "// Generated by LLVM NVPTX Back-End\n"
162 "//\n"
163 "\n";
164}
165
167 OS << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n";
168}
169
171 bool TexModeIndependent,
172 bool HasDebug) {
173 OS << ".target " << Target;
174 if (TexModeIndependent)
175 OS << ", texmode_independent";
176 if (HasDebug)
177 OS << ", debug";
178 OS << "\n";
179}
180
182 OS << ".address_size " << AddrSize << "\n"
183 << "\n";
184}
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 class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:64
const char * getData8bitsDirective() const
Definition MCAsmInfo.h:468
const MCObjectFileInfo * getObjectFileInfo() const
Definition MCContext.h:414
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:516
Streaming machine code generation interface.
Definition MCStreamer.h:222
MCContext & getContext() const
Definition MCStreamer.h:323
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:227
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 emitAddressSizeDirective(unsigned AddrSize) override
Emit address size used for this PTX module.
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
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559