Skip to content

Commit 2366573

Browse files
authored
[TableGen] Minor cleanup in StringToOffsetTable (#147712)
Make `AppendZero` a class member instead of an argument to `GetOrAddStringOffset` to reflect the intended usage that for a given `StringToOffsetTable`, all strings must use the same value of `AppendZero`. Modify `EmitStringTableDef` to drop the `Indent` argument as its always set to `""`, and to fail if it's called for a table with non-null-terminated strings.
1 parent aa27d4e commit 2366573

File tree

8 files changed

+31
-26
lines changed

8 files changed

+31
-26
lines changed

llvm/include/llvm/ADT/StringMapEntry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
110110
}
111111

112112
/// getKeyData - Return the start of the string data that is the key for this
113-
/// value. The string data is always stored immediately after the
113+
/// value. The string data is always stored immediately after the
114114
/// StringMapEntry object.
115115
const char *getKeyData() const {
116116
return reinterpret_cast<const char *>(this + 1);

llvm/include/llvm/TableGen/StringToOffsetTable.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ namespace llvm {
2323
class StringToOffsetTable {
2424
StringMap<unsigned> StringOffset;
2525
std::string AggregateString;
26+
const bool AppendZero;
2627

2728
public:
28-
StringToOffsetTable() {
29+
StringToOffsetTable(bool AppendZero = true) : AppendZero(AppendZero) {
2930
// Ensure we always put the empty string at offset zero. That lets empty
3031
// initialization also be zero initialization for offsets into the table.
3132
GetOrAddStringOffset("");
@@ -34,7 +35,7 @@ class StringToOffsetTable {
3435
bool empty() const { return StringOffset.empty(); }
3536
size_t size() const { return AggregateString.size(); }
3637

37-
unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true);
38+
unsigned GetOrAddStringOffset(StringRef Str);
3839

3940
// Returns the offset of `Str` in the table if its preset, else return
4041
// std::nullopt.
@@ -45,7 +46,7 @@ class StringToOffsetTable {
4546
return II->second;
4647
}
4748

48-
// Emit a string table definition with the provided name and indent.
49+
// Emit a string table definition with the provided name.
4950
//
5051
// When possible, this uses string-literal concatenation to emit the string
5152
// contents in a readable and searchable way. However, for (very) large string
@@ -56,8 +57,7 @@ class StringToOffsetTable {
5657
// The string table, and its input string contents, are always emitted as both
5758
// `static` and `constexpr`. Both `Name` and (`Name` + "Storage") must be
5859
// valid identifiers to declare.
59-
void EmitStringTableDef(raw_ostream &OS, const Twine &Name,
60-
const Twine &Indent = "") const;
60+
void EmitStringTableDef(raw_ostream &OS, const Twine &Name) const;
6161

6262
// Emit the string as one single string.
6363
void EmitString(raw_ostream &O) const;

llvm/lib/TableGen/StringToOffsetTable.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,37 @@
99
#include "llvm/TableGen/StringToOffsetTable.h"
1010
#include "llvm/Support/FormatVariadic.h"
1111
#include "llvm/Support/raw_ostream.h"
12+
#include "llvm/TableGen/Error.h"
1213
#include "llvm/TableGen/Main.h"
1314

1415
using namespace llvm;
1516

16-
unsigned StringToOffsetTable::GetOrAddStringOffset(StringRef Str,
17-
bool appendZero) {
17+
unsigned StringToOffsetTable::GetOrAddStringOffset(StringRef Str) {
1818
auto [II, Inserted] = StringOffset.insert({Str, size()});
1919
if (Inserted) {
2020
// Add the string to the aggregate if this is the first time found.
2121
AggregateString.append(Str.begin(), Str.end());
22-
if (appendZero)
22+
if (AppendZero)
2323
AggregateString += '\0';
2424
}
2525

2626
return II->second;
2727
}
2828

29-
void StringToOffsetTable::EmitStringTableDef(raw_ostream &OS, const Twine &Name,
30-
const Twine &Indent) const {
29+
void StringToOffsetTable::EmitStringTableDef(raw_ostream &OS,
30+
const Twine &Name) const {
31+
// This generates a `llvm::StringTable` which expects that entries are null
32+
// terminated. So fail with an error if `AppendZero` is false.
33+
if (!AppendZero)
34+
PrintFatalError("llvm::StringTable requires null terminated strings");
35+
3136
OS << formatv(R"(
3237
#ifdef __GNUC__
3338
#pragma GCC diagnostic push
3439
#pragma GCC diagnostic ignored "-Woverlength-strings"
3540
#endif
36-
{0}static constexpr char {1}Storage[] = )",
37-
Indent, Name);
41+
static constexpr char {}Storage[] = )",
42+
Name);
3843

3944
// MSVC silently miscompiles string literals longer than 64k in some
4045
// circumstances. The build system sets EmitLongStrLiterals to false when it
@@ -54,7 +59,7 @@ void StringToOffsetTable::EmitStringTableDef(raw_ostream &OS, const Twine &Name,
5459
"Expected empty string at the end due to terminators!");
5560
Strings.pop_back();
5661
for (StringRef Str : Strings) {
57-
OS << LineSep << Indent << " ";
62+
OS << LineSep << " ";
5863
// If we can, just emit this as a string literal to be concatenated.
5964
if (!UseChars) {
6065
OS << "\"";
@@ -71,17 +76,17 @@ void StringToOffsetTable::EmitStringTableDef(raw_ostream &OS, const Twine &Name,
7176
}
7277
OS << CharSep << "'\\0'";
7378
}
74-
OS << LineSep << Indent << (UseChars ? "};" : " ;");
79+
OS << LineSep << (UseChars ? "};" : " ;");
7580

7681
OS << formatv(R"(
7782
#ifdef __GNUC__
7883
#pragma GCC diagnostic pop
7984
#endif
8085
81-
{0}static constexpr llvm::StringTable {1} =
82-
{0} {1}Storage;
86+
static constexpr llvm::StringTable
87+
{0} = {0}Storage;
8388
)",
84-
Indent, Name);
89+
Name);
8590
}
8691

8792
void StringToOffsetTable::EmitString(raw_ostream &O) const {

llvm/test/TableGen/MixedCasedMnemonic.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def :MnemonicAlias<"InstB", "BInst">;
4141

4242
// Check that the matcher lower()s the mnemonics it matches.
4343
// MATCHER: static const char MnemonicTable[] =
44-
// MATCHER-NEXT: "\000\005ainst\005binst";
44+
// MATCHER-NEXT: "\005ainst\005binst";
4545

4646
// Check that aInst appears before BInst in the match table.
4747
// This shows that the mnemonics are sorted in a case-insensitive way,

llvm/test/TableGen/SDNodeInfoEmitter/basic.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def MyTarget : Target;
3535
// CHECK-NEXT: #pragma GCC diagnostic pop
3636
// CHECK-NEXT: #endif
3737
// CHECK-EMPTY:
38-
// CHECK-NEXT: static constexpr llvm::StringTable MyTargetSDNodeNames =
39-
// CHECK-NEXT: MyTargetSDNodeNamesStorage;
38+
// CHECK-NEXT: static constexpr llvm::StringTable
39+
// CHECK-NEXT: MyTargetSDNodeNames = MyTargetSDNodeNamesStorage;
4040
// CHECK-EMPTY:
4141
// CHECK-NEXT: static const SDTypeConstraint MyTargetSDTypeConstraints[] = {
4242
// CHECK-NEXT: /* dummy */ {SDTCisVT, 0, 0, MVT::INVALID_SIMPLE_VALUE_TYPE}

llvm/utils/TableGen/AsmMatcherEmitter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,7 +3440,7 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
34403440
if (!ReportMultipleNearMisses)
34413441
emitAsmTiedOperandConstraints(Target, Info, OS, HasOptionalOperands);
34423442

3443-
StringToOffsetTable StringTable;
3443+
StringToOffsetTable StringTable(/*AppendZero=*/false);
34443444

34453445
size_t MaxNumOperands = 0;
34463446
unsigned MaxMnemonicIndex = 0;
@@ -3451,8 +3451,8 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
34513451

34523452
// Store a pascal-style length byte in the mnemonic.
34533453
std::string LenMnemonic = char(MI->Mnemonic.size()) + MI->Mnemonic.lower();
3454-
MaxMnemonicIndex = std::max(
3455-
MaxMnemonicIndex, StringTable.GetOrAddStringOffset(LenMnemonic, false));
3454+
MaxMnemonicIndex = std::max(MaxMnemonicIndex,
3455+
StringTable.GetOrAddStringOffset(LenMnemonic));
34563456
}
34573457

34583458
OS << "static const char MnemonicTable[] =\n";

llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ void IntrinsicEmitter::EmitIntrinsicToNameTable(
252252
253253
)";
254254

255-
Table.EmitStringTableDef(OS, "IntrinsicNameTable", /*Indent=*/"");
255+
Table.EmitStringTableDef(OS, "IntrinsicNameTable");
256256

257257
OS << R"(
258258
static constexpr unsigned IntrinsicNameOffsetTable[] = {

llvm/utils/TableGen/OptionParserEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static void emitOptionParser(const RecordKeeper &Records, raw_ostream &OS) {
291291
OS << "/////////\n";
292292
OS << "// String table\n\n";
293293
OS << "#ifdef OPTTABLE_STR_TABLE_CODE\n";
294-
Table.EmitStringTableDef(OS, "OptionStrTable", /*Indent=*/"");
294+
Table.EmitStringTableDef(OS, "OptionStrTable");
295295
OS << "#endif // OPTTABLE_STR_TABLE_CODE\n\n";
296296

297297
// Dump prefixes.

0 commit comments

Comments
 (0)