Skip to content

Commit 03478d6

Browse files
authored
[GlobalISel] Prefix MatchTable Lines with their Index (llvm#125845)
I tried to keep it readable by making the width of the column with the index always enough to contain the largest number. That way things don't shift to the right every time a new digit appears, it remains consistent. Tests don't break because this only affects the beginning of the line and FileCheck doesn't care about what comes before for the most part. Example of the new output: ``` /* 758359 */ // Label 9988: @758359 /* 758359 */ GIM_Try, /*On fail goto*//*Label 9989*/ GIMT_Encode4(758435), // Rule ID 6715 // /* 758364 */ GIM_CheckConstantInt8, /*MI*/0, /*Op*/2, 0, /* 758368 */ // MIs[0] offset ``` Fixes llvm#119177
1 parent 2464f4b commit 03478d6

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,28 @@ MatchTableRecord MatchTable::JumpTarget(unsigned LabelID) {
286286
void MatchTable::emitUse(raw_ostream &OS) const { OS << "MatchTable" << ID; }
287287

288288
void MatchTable::emitDeclaration(raw_ostream &OS) const {
289-
unsigned Indentation = 4;
289+
static constexpr unsigned BaseIndent = 4;
290+
unsigned Indentation = 0;
290291
OS << " constexpr static uint8_t MatchTable" << ID << "[] = {";
291292
LineBreak.emit(OS, true, *this);
292-
OS << std::string(Indentation, ' ');
293293

294+
// We want to display the table index of each line in a consistent
295+
// manner. It has to appear as a column on the left side of the table.
296+
// To determine how wide the column needs to be, check how many characters
297+
// we need to fit the largest possible index in the current table.
298+
const unsigned NumColsForIdx = llvm::to_string(CurrentSize).size();
299+
300+
unsigned CurIndex = 0;
301+
const auto BeginLine = [&]() {
302+
OS.indent(BaseIndent);
303+
std::string IdxStr = llvm::to_string(CurIndex);
304+
// Pad the string with spaces to keep the size of the prefix consistent.
305+
OS << " /* ";
306+
OS.indent(NumColsForIdx - IdxStr.size()) << IdxStr << " */ ";
307+
OS.indent(Indentation);
308+
};
309+
310+
BeginLine();
294311
for (auto I = Contents.begin(), E = Contents.end(); I != E; ++I) {
295312
bool LineBreakIsNext = false;
296313
const auto &NextI = std::next(I);
@@ -306,11 +323,14 @@ void MatchTable::emitDeclaration(raw_ostream &OS) const {
306323

307324
I->emit(OS, LineBreakIsNext, *this);
308325
if (I->Flags & MatchTableRecord::MTRF_LineBreakFollows)
309-
OS << std::string(Indentation, ' ');
326+
BeginLine();
310327

311328
if (I->Flags & MatchTableRecord::MTRF_Outdent)
312329
Indentation -= 2;
330+
331+
CurIndex += I->size();
313332
}
333+
assert(CurIndex == CurrentSize);
314334
OS << "}; // Size: " << CurrentSize << " bytes\n";
315335
}
316336

0 commit comments

Comments
 (0)