diff --git a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp index 8564bf8d2d91b..21db090f62cce 100644 --- a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp +++ b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp @@ -286,11 +286,28 @@ MatchTableRecord MatchTable::JumpTarget(unsigned LabelID) { void MatchTable::emitUse(raw_ostream &OS) const { OS << "MatchTable" << ID; } void MatchTable::emitDeclaration(raw_ostream &OS) const { - unsigned Indentation = 4; + static constexpr unsigned BaseIndent = 4; + unsigned Indentation = 0; OS << " constexpr static uint8_t MatchTable" << ID << "[] = {"; LineBreak.emit(OS, true, *this); - OS << std::string(Indentation, ' '); + // We want to display the table index of each line in a consistent + // manner. It has to appear as a column on the left side of the table. + // To determine how wide the column needs to be, check how many characters + // we need to fit the largest possible index in the current table. + const unsigned NumColsForIdx = llvm::to_string(CurrentSize).size(); + + unsigned CurIndex = 0; + const auto BeginLine = [&]() { + OS.indent(BaseIndent); + std::string IdxStr = llvm::to_string(CurIndex); + // Pad the string with spaces to keep the size of the prefix consistent. + OS << " /* "; + OS.indent(NumColsForIdx - IdxStr.size()) << IdxStr << " */ "; + OS.indent(Indentation); + }; + + BeginLine(); for (auto I = Contents.begin(), E = Contents.end(); I != E; ++I) { bool LineBreakIsNext = false; const auto &NextI = std::next(I); @@ -306,11 +323,14 @@ void MatchTable::emitDeclaration(raw_ostream &OS) const { I->emit(OS, LineBreakIsNext, *this); if (I->Flags & MatchTableRecord::MTRF_LineBreakFollows) - OS << std::string(Indentation, ' '); + BeginLine(); if (I->Flags & MatchTableRecord::MTRF_Outdent) Indentation -= 2; + + CurIndex += I->size(); } + assert(CurIndex == CurrentSize); OS << "}; // Size: " << CurrentSize << " bytes\n"; }