Skip to content

Commit 482e890

Browse files
committed
[tablegen] Emit string literals instead of char arrays
This changes the generated (Instr|Asm|Reg|Regclass)Name tables from this form: extern const char HexagonInstrNameData[] = { /* 0 */ 'G', '_', 'F', 'L', 'O', 'G', '1', '0', 0, /* 9 */ 'E', 'N', 'D', 'L', 'O', 'O', 'P', '0', 0, /* 18 */ 'V', '6', '_', 'v', 'd', 'd', '0', 0, /* 26 */ 'P', 'S', '_', 'v', 'd', 'd', '0', 0, [...] }; ...to this: extern const char HexagonInstrNameData[] = { /* 0 */ "G_FLOG10\0" /* 9 */ "ENDLOOP0\0" /* 18 */ "V6_vdd0\0" /* 26 */ "PS_vdd0\0" [...] }; This should make debugging and exploration a lot easier for mortals, while providing a significant compile-time reduction for common compilers. To avoid issues with low implementation limits, this is disabled by default for visual studio. To force output one way or the other, pass `--long-string-literals=<bool>` to `tablegen` Reviewers: mstorsjo, rnk Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D73044 A variation of this patch was originally committed in ce23515 and then reverted in e464b31 due to build failures.
1 parent 3ed88b0 commit 482e890

File tree

6 files changed

+110
-28
lines changed

6 files changed

+110
-28
lines changed

llvm/cmake/modules/TableGen.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ function(tablegen project ofn)
5858
endif()
5959
endif()
6060

61+
# MSVC can't support long string literals ("long" > 65534 bytes)[1], so if there's
62+
# a possibility of generated tables being consumed by MSVC, generate arrays of
63+
# char literals, instead. If we're cross-compiling, then conservatively assume
64+
# that the source might be consumed by MSVC.
65+
# [1] https://docs.microsoft.com/en-us/cpp/cpp/compiler-limits?view=vs-2017
66+
if (MSVC AND project STREQUAL LLVM)
67+
list(APPEND LLVM_TABLEGEN_FLAGS "--long-string-literals=0")
68+
endif()
6169
if (CMAKE_GENERATOR MATCHES "Visual Studio")
6270
# Visual Studio has problems with llvm-tblgen's native --write-if-changed
6371
# behavior. Since it doesn't do restat optimizations anyway, just don't

llvm/utils/TableGen/AsmWriterEmitter.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,7 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
380380
}
381381

382382
// Emit the string table itself.
383-
O << " static const char AsmStrs[] = {\n";
384-
StringTable.emit(O, printChar);
385-
O << " };\n\n";
383+
StringTable.emitStringLiteralDef(O, " static const char AsmStrs[]");
386384

387385
// Emit the lookup tables in pieces to minimize wasted bytes.
388386
unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
@@ -537,9 +535,8 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
537535
}
538536

539537
StringTable.layout();
540-
O << " static const char AsmStrs" << AltName << "[] = {\n";
541-
StringTable.emit(O, printChar);
542-
O << " };\n\n";
538+
StringTable.emitStringLiteralDef(O, Twine(" static const char AsmStrs") +
539+
AltName + "[]");
543540

544541
O << " static const " << getMinimalTypeForRange(StringTable.size() - 1, 32)
545542
<< " RegAsmOffset" << AltName << "[] = {";

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,8 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
569569

570570
// Emit the array of instruction names.
571571
InstrNames.layout();
572-
OS << "extern const char " << TargetName << "InstrNameData[] = {\n";
573-
InstrNames.emit(OS, printChar);
574-
OS << "};\n\n";
572+
InstrNames.emitStringLiteralDef(OS, Twine("extern const char ") + TargetName +
573+
"InstrNameData[]");
575574

576575
OS << "extern const unsigned " << TargetName <<"InstrNameIndices[] = {";
577576
Num = 0;

llvm/utils/TableGen/RegisterInfoEmitter.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,8 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
992992

993993
// Emit the string table.
994994
RegStrings.layout();
995-
OS << "extern const char " << TargetName << "RegStrings[] = {\n";
996-
RegStrings.emit(OS, printChar);
997-
OS << "};\n\n";
995+
RegStrings.emitStringLiteralDef(OS, Twine("extern const char ") + TargetName +
996+
"RegStrings[]");
998997

999998
OS << "extern const MCRegisterDesc " << TargetName
1000999
<< "RegDesc[] = { // Descriptors\n";
@@ -1065,9 +1064,8 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
10651064
OS << "} // end anonymous namespace\n\n";
10661065

10671066
RegClassStrings.layout();
1068-
OS << "extern const char " << TargetName << "RegClassStrings[] = {\n";
1069-
RegClassStrings.emit(OS, printChar);
1070-
OS << "};\n\n";
1067+
RegClassStrings.emitStringLiteralDef(
1068+
OS, Twine("extern const char ") + TargetName + "RegClassStrings[]");
10711069

10721070
OS << "extern const MCRegisterClass " << TargetName
10731071
<< "MCRegisterClasses[] = {\n";

llvm/utils/TableGen/SequenceToOffsetTable.h

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef LLVM_UTILS_TABLEGEN_SEQUENCETOOFFSETTABLE_H
1616
#define LLVM_UTILS_TABLEGEN_SEQUENCETOOFFSETTABLE_H
1717

18+
#include "llvm/Support/CommandLine.h"
1819
#include "llvm/Support/raw_ostream.h"
1920
#include <algorithm>
2021
#include <cassert>
@@ -23,6 +24,61 @@
2324
#include <map>
2425

2526
namespace llvm {
27+
extern llvm::cl::opt<bool> EmitLongStrLiterals;
28+
29+
// Helper function for SequenceToOffsetTable<string>.
30+
static inline void printStrLitEscChar(raw_ostream &OS, char C) {
31+
const char *Escapes[] = {
32+
"\\000", "\\001", "\\002", "\\003", "\\004", "\\005", "\\006", "\\007",
33+
"\\010", "\\t", "\\n", "\\013", "\\014", "\\r", "\\016", "\\017",
34+
"\\020", "\\021", "\\022", "\\023", "\\024", "\\025", "\\026", "\\027",
35+
"\\030", "\\031", "\\032", "\\033", "\\034", "\\035", "\\036", "\\037",
36+
" ", "!", "\\\"", "#", "$", "%", "&", "'",
37+
"(", ")", "*", "+", ",", "-", ".", "/",
38+
"0", "1", "2", "3", "4", "5", "6", "7",
39+
"8", "9", ":", ";", "<", "=", ">", "?",
40+
"@", "A", "B", "C", "D", "E", "F", "G",
41+
"H", "I", "J", "K", "L", "M", "N", "O",
42+
"P", "Q", "R", "S", "T", "U", "V", "W",
43+
"X", "Y", "Z", "[", "\\\\", "]", "^", "_",
44+
"`", "a", "b", "c", "d", "e", "f", "g",
45+
"h", "i", "j", "k", "l", "m", "n", "o",
46+
"p", "q", "r", "s", "t", "u", "v", "w",
47+
"x", "y", "z", "{", "|", "}", "~", "\\177",
48+
"\\200", "\\201", "\\202", "\\203", "\\204", "\\205", "\\206", "\\207",
49+
"\\210", "\\211", "\\212", "\\213", "\\214", "\\215", "\\216", "\\217",
50+
"\\220", "\\221", "\\222", "\\223", "\\224", "\\225", "\\226", "\\227",
51+
"\\230", "\\231", "\\232", "\\233", "\\234", "\\235", "\\236", "\\237",
52+
"\\240", "\\241", "\\242", "\\243", "\\244", "\\245", "\\246", "\\247",
53+
"\\250", "\\251", "\\252", "\\253", "\\254", "\\255", "\\256", "\\257",
54+
"\\260", "\\261", "\\262", "\\263", "\\264", "\\265", "\\266", "\\267",
55+
"\\270", "\\271", "\\272", "\\273", "\\274", "\\275", "\\276", "\\277",
56+
"\\300", "\\301", "\\302", "\\303", "\\304", "\\305", "\\306", "\\307",
57+
"\\310", "\\311", "\\312", "\\313", "\\314", "\\315", "\\316", "\\317",
58+
"\\320", "\\321", "\\322", "\\323", "\\324", "\\325", "\\326", "\\327",
59+
"\\330", "\\331", "\\332", "\\333", "\\334", "\\335", "\\336", "\\337",
60+
"\\340", "\\341", "\\342", "\\343", "\\344", "\\345", "\\346", "\\347",
61+
"\\350", "\\351", "\\352", "\\353", "\\354", "\\355", "\\356", "\\357",
62+
"\\360", "\\361", "\\362", "\\363", "\\364", "\\365", "\\366", "\\367",
63+
"\\370", "\\371", "\\372", "\\373", "\\374", "\\375", "\\376", "\\377"};
64+
65+
static_assert(sizeof Escapes / sizeof Escapes[0] ==
66+
std::numeric_limits<unsigned char>::max() + 1,
67+
"unsupported character type");
68+
OS << Escapes[static_cast<unsigned char>(C)];
69+
}
70+
71+
static inline void printChar(raw_ostream &OS, char C) {
72+
unsigned char UC(C);
73+
if (isalnum(UC) || ispunct(UC)) {
74+
OS << '\'';
75+
if (C == '\\' || C == '\'')
76+
OS << '\\';
77+
OS << C << '\'';
78+
} else {
79+
OS << unsigned(UC);
80+
}
81+
}
2682

2783
/// SequenceToOffsetTable - Collect a number of terminated sequences of T.
2884
/// Compute the layout of a table that contains all the sequences, possibly by
@@ -108,6 +164,37 @@ class SequenceToOffsetTable {
108164
return I->second + (I->first.size() - Seq.size());
109165
}
110166

167+
/// `emitStringLiteralDef` - Print out the table as the body of an array
168+
/// initializer, where each element is a C string literal terminated by
169+
/// `\0`. Falls back to emitting a comma-separated integer list if
170+
/// `EmitLongStrLiterals` is false
171+
void emitStringLiteralDef(raw_ostream &OS, const llvm::Twine &Decl) const {
172+
assert(Entries && "Call layout() before emitStringLiteralDef()");
173+
if (EmitLongStrLiterals) {
174+
OS << "\n#ifdef __GNUC__\n"
175+
<< "#pragma GCC diagnostic push\n"
176+
<< "#pragma GCC diagnostic ignored \"-Woverlength-strings\"\n"
177+
<< "#endif\n"
178+
<< Decl << " = {\n";
179+
} else {
180+
OS << Decl << " = {\n";
181+
emit(OS, printChar, "0");
182+
OS << "\n};\n\n";
183+
return;
184+
}
185+
for (auto I : Seqs) {
186+
OS << " /* " << I.second << " */ \"";
187+
for (auto C : I.first) {
188+
printStrLitEscChar(OS, C);
189+
}
190+
OS << "\\0\"\n";
191+
}
192+
OS << "};\n"
193+
<< "#ifdef __GNUC__\n"
194+
<< "#pragma GCC diagnostic pop\n"
195+
<< "#endif\n\n";
196+
}
197+
111198
/// emit - Print out the table as the body of an array initializer.
112199
/// Use the Print function to print elements.
113200
void emit(raw_ostream &OS,
@@ -127,19 +214,6 @@ class SequenceToOffsetTable {
127214
}
128215
};
129216

130-
// Helper function for SequenceToOffsetTable<string>.
131-
static inline void printChar(raw_ostream &OS, char C) {
132-
unsigned char UC(C);
133-
if (isalnum(UC) || ispunct(UC)) {
134-
OS << '\'';
135-
if (C == '\\' || C == '\'')
136-
OS << '\\';
137-
OS << C << '\'';
138-
} else {
139-
OS << unsigned(UC);
140-
}
141-
}
142-
143217
} // end namespace llvm
144218

145219
#endif

llvm/utils/TableGen/TableGen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ namespace llvm {
6060
/// Storage for TimeRegionsOpt as a global so that backends aren't required to
6161
/// include CommandLine.h
6262
bool TimeRegions = false;
63+
cl::opt<bool> EmitLongStrLiterals(
64+
"long-string-literals",
65+
cl::desc("when emitting large string tables, prefer string literals over "
66+
"comma-separated char literals. This can be a readability and "
67+
"compile-time performance win, but upsets some compilers"),
68+
cl::Hidden, cl::init(true));
6369
} // end namespace llvm
6470

6571
namespace {

0 commit comments

Comments
 (0)