Skip to content

[GlobalISel] Prefix MatchTable Lines with their Index #125845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 6, 2025

Conversation

Pierre-vh
Copy link
Contributor

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 #119177

@llvmbot
Copy link
Member

llvmbot commented Feb 5, 2025

@llvm/pr-subscribers-tablegen

@llvm/pr-subscribers-llvm-globalisel

Author: Pierre van Houtryve (Pierre-vh)

Changes

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 #119177


Full diff: https://github.com/llvm/llvm-project/pull/125845.diff

1 Files Affected:

  • (modified) llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp (+20-3)
diff --git a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
index 8564bf8d2d91ba5..9083ce7c86a4723 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
@@ -286,11 +286,25 @@ 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, ' ');
 
+  const unsigned NumColsForIdx = llvm::to_string(CurrentSize).size();
+
+  unsigned CurIndex = 0;
+  const auto BeginLine = [&](){
+    OS << std::string(BaseIndent, ' ');
+    // To keep the /* index */ column consistent, pad
+    // the string at the start so we can always fit the
+    // exact number of characters to print the largest possible index.
+    std::string IdxStr = llvm::to_string(CurIndex);
+    OS << " /* " << std::string(NumColsForIdx - IdxStr.size(), ' ') << IdxStr << " */ ";
+    OS << std::string(Indentation, ' ');
+  };
+
+  BeginLine();
   for (auto I = Contents.begin(), E = Contents.end(); I != E; ++I) {
     bool LineBreakIsNext = false;
     const auto &NextI = std::next(I);
@@ -306,11 +320,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";
 }
 

Copy link
Collaborator

@qcolombet qcolombet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix @Pierre-vh !
This is life changing for debugging.

@Pierre-vh Pierre-vh requested a review from arsenm February 6, 2025 11:44
@Pierre-vh Pierre-vh requested a review from arsenm February 6, 2025 11:50
@Pierre-vh Pierre-vh merged commit 03478d6 into llvm:main Feb 6, 2025
6 of 7 checks passed
@Pierre-vh Pierre-vh deleted the matchtable-debug branch February 6, 2025 13:00
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[GlobalISel] Improve the debug-ability of the match tables by adding the index as comment in the table
4 participants