Skip to content

Commit

Permalink
[DEBUGINFO, NVPTX] Try to pack bytes data into a single string.
Browse files Browse the repository at this point in the history
Summary:
If the target does not support `.asciz` and `.ascii` directives, the
strings are represented as bytes and each byte is placed on the new line
as a separate byte directive `.b8 <data>`. NVPTX target allows to
represent the vector of the data of the same type as a vector, where
values are separated using `,` symbol: `.b8 <data1>,<data2>,...`. This
allows to reduce the size of the final PTX file. Ptxas tool includes ptx
files into the resulting binary object, so reducing the size of the PTX
file is important.

Reviewers: tra, jlebar, echristo

Subscribers: jholewinski, llvm-commits

Differential Revision: https://reviews.llvm.org/D45822

llvm-svn: 345142
  • Loading branch information
alexey-bataev committed Oct 24, 2018
1 parent e8437cb commit c15c853
Show file tree
Hide file tree
Showing 11 changed files with 736 additions and 4,530 deletions.
5 changes: 5 additions & 0 deletions llvm/include/llvm/MC/MCStreamer.h
Expand Up @@ -109,6 +109,11 @@ class MCTargetStreamer {

virtual void emitValue(const MCExpr *Value);

/// Emit the bytes in \p Data into the output.
///
/// This is used to emit bytes in \p Data as sequence of .byte directives.
virtual void emitRawBytes(StringRef Data);

virtual void finish();
};

Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/CodeGen/AsmPrinter/DIE.cpp
Expand Up @@ -589,8 +589,7 @@ void DIEString::print(raw_ostream &O) const {
//===----------------------------------------------------------------------===//
void DIEInlineString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
if (Form == dwarf::DW_FORM_string) {
for (char ch : S)
AP->emitInt8(ch);
AP->OutStreamer->EmitBytes(S);
AP->emitInt8(0);
return;
}
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/MC/MCAsmStreamer.cpp
Expand Up @@ -858,10 +858,14 @@ void MCAsmStreamer::EmitBytes(StringRef Data) {
// supported, emit as vector of 8bits data.
if (Data.size() == 1 ||
!(MAI->getAscizDirective() || MAI->getAsciiDirective())) {
const char *Directive = MAI->getData8bitsDirective();
for (const unsigned char C : Data.bytes()) {
OS << Directive << (unsigned)C;
EmitEOL();
if (MCTargetStreamer *TS = getTargetStreamer()) {
TS->emitRawBytes(Data);
} else {
const char *Directive = MAI->getData8bitsDirective();
for (const unsigned char C : Data.bytes()) {
OS << Directive << (unsigned)C;
EmitEOL();
}
}
return;
}
Expand Down
12 changes: 12 additions & 0 deletions llvm/lib/MC/MCStreamer.cpp
Expand Up @@ -72,6 +72,18 @@ void MCTargetStreamer::emitValue(const MCExpr *Value) {
Streamer.EmitRawText(OS.str());
}

void MCTargetStreamer::emitRawBytes(StringRef Data) {
const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
const char *Directive = MAI->getData8bitsDirective();
for (const unsigned char C : Data.bytes()) {
SmallString<128> Str;
raw_svector_ostream OS(Str);

OS << Directive << (unsigned)C;
Streamer.EmitRawText(OS.str());
}
}

void MCTargetStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {}

MCStreamer::MCStreamer(MCContext &Ctx)
Expand Down
27 changes: 27 additions & 0 deletions llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXTargetStreamer.cpp
Expand Up @@ -92,3 +92,30 @@ void NVPTXTargetStreamer::changeSection(const MCSection *CurSection,
OS << "//\t{\n";
}
}

void NVPTXTargetStreamer::emitRawBytes(StringRef Data) {
const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
const char *Directive = MAI->getData8bitsDirective();
unsigned NumElements = Data.size();
const unsigned MaxLen = 40;
unsigned NumChunks = 1 + ((NumElements - 1) / MaxLen);
// Split the very long directives into several parts if the limit is
// specified.
for (unsigned I = 0; I < NumChunks; ++I) {
SmallString<128> Str;
raw_svector_ostream OS(Str);

const char *Label = Directive;
for (auto It = std::next(Data.bytes_begin(), I * MaxLen),
End = (I == NumChunks - 1)
? Data.bytes_end()
: std::next(Data.bytes_begin(), (I + 1) * MaxLen);
It != End; ++It) {
OS << Label << (unsigned)*It;
if (Label == Directive)
Label = ",";
}
Streamer.EmitRawText(OS.str());
}
}

4 changes: 4 additions & 0 deletions llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXTargetStreamer.h
Expand Up @@ -39,6 +39,10 @@ class NVPTXTargetStreamer : public MCTargetStreamer {
void emitDwarfFileDirective(StringRef Directive) override;
void changeSection(const MCSection *CurSection, MCSection *Section,
const MCExpr *SubSection, raw_ostream &OS) override;
/// Emit the bytes in \p Data into the output.
///
/// This is used to emit bytes in \p Data as sequence of .byte directives.
void emitRawBytes(StringRef Data) override;
};

} // end namespace llvm
Expand Down
72 changes: 5 additions & 67 deletions llvm/test/DebugInfo/NVPTX/cu-range-hole.ll
Expand Up @@ -148,75 +148,15 @@ entry:
; CHECK: // .b32 .debug_abbrev // Offset Into Abbrev. Section
; CHECK: // .b8 8 // Address Size (in bytes)
; CHECK: // .b8 1 // Abbrev [1] 0xb:0xb0 DW_TAG_compile_unit
; CHECK: // .b8 99 // DW_AT_producer
; CHECK: // .b8 108
; CHECK: // .b8 97
; CHECK: // .b8 110
; CHECK: // .b8 103
; CHECK: // .b8 32
; CHECK: // .b8 118
; CHECK: // .b8 101
; CHECK: // .b8 114
; CHECK: // .b8 115
; CHECK: // .b8 105
; CHECK: // .b8 111
; CHECK: // .b8 110
; CHECK: // .b8 32
; CHECK: // .b8 51
; CHECK: // .b8 46
; CHECK: // .b8 53
; CHECK: // .b8 46
; CHECK: // .b8 48
; CHECK: // .b8 32
; CHECK: // .b8 40
; CHECK: // .b8 116
; CHECK: // .b8 114
; CHECK: // .b8 117
; CHECK: // .b8 110
; CHECK: // .b8 107
; CHECK: // .b8 32
; CHECK: // .b8 50
; CHECK: // .b8 48
; CHECK: // .b8 52
; CHECK: // .b8 49
; CHECK: // .b8 54
; CHECK: // .b8 52
; CHECK: // .b8 41
; CHECK: // .b8 32
; CHECK: // .b8 40
; CHECK: // .b8 108
; CHECK: // .b8 108
; CHECK: // .b8 118
; CHECK: // .b8 109
; CHECK: // .b8 47
; CHECK: // .b8 116
; CHECK: // .b8 114
; CHECK: // .b8 117
; CHECK: // .b8 110
; CHECK: // .b8 107
; CHECK: // .b8 32
; CHECK: // .b8 50
; CHECK: // .b8 48
; CHECK: // .b8 52
; CHECK: // .b8 49
; CHECK: // .b8 56
; CHECK: // .b8 51
; CHECK: // .b8 41
; CHECK: // .b8 99,108,97,110,103,32,118,101,114,115,105,111,110,32,51,46,53,46,48,32,40,116,114,117,110,107,32,50,48,52,49,54,52,41,32,40,108,108,118,109 // DW_AT_producer
; CHECK: // .b8 47,116,114,117,110,107,32,50,48,52,49,56,51,41
; CHECK: // .b8 0
; CHECK: // .b8 12 // DW_AT_language
; CHECK: // .b8 0
; CHECK: // .b8 98 // DW_AT_name
; CHECK: // .b8 46
; CHECK: // .b8 99
; CHECK: // .b8 98,46,99 // DW_AT_name
; CHECK: // .b8 0
; CHECK: // .b32 .debug_line // DW_AT_stmt_list
; CHECK: // .b8 47 // DW_AT_comp_dir
; CHECK: // .b8 115
; CHECK: // .b8 111
; CHECK: // .b8 117
; CHECK: // .b8 114
; CHECK: // .b8 99
; CHECK: // .b8 101
; CHECK: // .b8 47,115,111,117,114,99,101 // DW_AT_comp_dir
; CHECK: // .b8 0
; CHECK: // .b64 Lfunc_begin0 // DW_AT_low_pc
; CHECK: // .b64 Lfunc_end2 // DW_AT_high_pc
Expand Down Expand Up @@ -259,9 +199,7 @@ entry:
; CHECK: // .b32 179 // DW_AT_type
; CHECK: // .b8 0 // End Of Children Mark
; CHECK: // .b8 4 // Abbrev [4] 0xb3:0x7 DW_TAG_base_type
; CHECK: // .b8 105 // DW_AT_name
; CHECK: // .b8 110
; CHECK: // .b8 116
; CHECK: // .b8 105,110,116 // DW_AT_name
; CHECK: // .b8 0
; CHECK: // .b8 5 // DW_AT_encoding
; CHECK: // .b8 4 // DW_AT_byte_size
Expand Down
39 changes: 6 additions & 33 deletions llvm/test/DebugInfo/NVPTX/dbg-declare-alloca.ll
Expand Up @@ -129,23 +129,14 @@
; CHECK: // .b32 .debug_abbrev // Offset Into Abbrev. Section
; CHECK: // .b8 8 // Address Size (in bytes)
; CHECK: // .b8 1 // Abbrev [1] 0xb:0x80 DW_TAG_compile_unit
; CHECK: // .b8 99 // DW_AT_producer
; CHECK: // .b8 108
; CHECK: // .b8 97
; CHECK: // .b8 110
; CHECK: // .b8 103
; CHECK: // .b8 99,108,97,110,103 // DW_AT_producer
; CHECK: // .b8 0
; CHECK: // .b8 12 // DW_AT_language
; CHECK: // .b8 0
; CHECK: // .b8 116 // DW_AT_name
; CHECK: // .b8 46
; CHECK: // .b8 99
; CHECK: // .b8 116,46,99 // DW_AT_name
; CHECK: // .b8 0
; CHECK: // .b32 .debug_line // DW_AT_stmt_list
; CHECK: // .b8 116 // DW_AT_comp_dir
; CHECK: // .b8 101
; CHECK: // .b8 115
; CHECK: // .b8 116
; CHECK: // .b8 116,101,115,116 // DW_AT_comp_dir
; CHECK: // .b8 0
; CHECK: // .b64 Lfunc_begin0 // DW_AT_low_pc
; CHECK: // .b64 Lfunc_end0 // DW_AT_high_pc
Expand All @@ -154,21 +145,7 @@
; CHECK: // .b64 Lfunc_end0 // DW_AT_high_pc
; CHECK: // .b8 1 // DW_AT_frame_base
; CHECK: // .b8 156
; CHECK: // .b8 117 // DW_AT_name
; CHECK: // .b8 115
; CHECK: // .b8 101
; CHECK: // .b8 95
; CHECK: // .b8 100
; CHECK: // .b8 98
; CHECK: // .b8 103
; CHECK: // .b8 95
; CHECK: // .b8 100
; CHECK: // .b8 101
; CHECK: // .b8 99
; CHECK: // .b8 108
; CHECK: // .b8 97
; CHECK: // .b8 114
; CHECK: // .b8 101
; CHECK: // .b8 117,115,101,95,100,98,103,95,100,101,99,108,97,114,101 // DW_AT_name
; CHECK: // .b8 0
; CHECK: // .b8 1 // DW_AT_decl_file
; CHECK: // .b8 3 // DW_AT_decl_line
Expand All @@ -187,9 +164,7 @@
; CHECK: // .b32 110 // DW_AT_type
; CHECK: // .b8 0 // End Of Children Mark
; CHECK: // .b8 4 // Abbrev [4] 0x6e:0x15 DW_TAG_structure_type
; CHECK: // .b8 70 // DW_AT_name
; CHECK: // .b8 111
; CHECK: // .b8 111
; CHECK: // .b8 70,111,111 // DW_AT_name
; CHECK: // .b8 0
; CHECK: // .b8 4 // DW_AT_byte_size
; CHECK: // .b8 1 // DW_AT_decl_file
Expand All @@ -205,9 +180,7 @@
; CHECK: // .b8 0
; CHECK: // .b8 0 // End Of Children Mark
; CHECK: // .b8 6 // Abbrev [6] 0x83:0x7 DW_TAG_base_type
; CHECK: // .b8 105 // DW_AT_name
; CHECK: // .b8 110
; CHECK: // .b8 116
; CHECK: // .b8 105,110,116 // DW_AT_name
; CHECK: // .b8 0
; CHECK: // .b8 5 // DW_AT_encoding
; CHECK: // .b8 4 // DW_AT_byte_size
Expand Down
19 changes: 2 additions & 17 deletions llvm/test/DebugInfo/NVPTX/debug-file-loc.ll
Expand Up @@ -63,25 +63,10 @@ bb:
; CHECK: // .b8 0 // DW_AT_producer
; CHECK: // .b8 4 // DW_AT_language
; CHECK: // .b8 0
; CHECK: // .b8 98 // DW_AT_name
; CHECK: // .b8 97
; CHECK: // .b8 114
; CHECK: // .b8 46
; CHECK: // .b8 99
; CHECK: // .b8 117
; CHECK: // .b8 98,97,114,46,99,117 // DW_AT_name
; CHECK: // .b8 0
; CHECK: // .b32 .debug_line // DW_AT_stmt_list
; CHECK: // .b8 47 // DW_AT_comp_dir
; CHECK: // .b8 115
; CHECK: // .b8 111
; CHECK: // .b8 117
; CHECK: // .b8 114
; CHECK: // .b8 99
; CHECK: // .b8 101
; CHECK: // .b8 47
; CHECK: // .b8 100
; CHECK: // .b8 105
; CHECK: // .b8 114
; CHECK: // .b8 47,115,111,117,114,99,101,47,100,105,114 // DW_AT_comp_dir
; CHECK: // .b8 0
; CHECK: // .b64 Lfunc_begin0 // DW_AT_low_pc
; CHECK: // .b64 Lfunc_end1 // DW_AT_high_pc
Expand Down

0 comments on commit c15c853

Please sign in to comment.