[llvm] 4cd4161 - [TableGen] Emit instruction name in INSTRINFO_OPERAND_TYPE

Amir Ayupov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 20 12:24:07 PDT 2022


Author: Amir Ayupov
Date: 2022-06-20T12:24:01-07:00
New Revision: 4cd416193cc126355a22b2c9e5c1df3a49b59e50

URL: https://github.com/llvm/llvm-project/commit/4cd416193cc126355a22b2c9e5c1df3a49b59e50
DIFF: https://github.com/llvm/llvm-project/commit/4cd416193cc126355a22b2c9e5c1df3a49b59e50.diff

LOG: [TableGen] Emit instruction name in INSTRINFO_OPERAND_TYPE

Make Offsets and OpcodeOperandTypes tables human-readable by printing the
instruction name before the operand list.

In effect, this makes debugging generated `getOperandType` possible.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D127931

Added: 
    

Modified: 
    llvm/test/TableGen/get-operand-type.td
    llvm/utils/TableGen/InstrInfoEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/TableGen/get-operand-type.td b/llvm/test/TableGen/get-operand-type.td
index 27607f57a8d48..905adfad30eb5 100644
--- a/llvm/test/TableGen/get-operand-type.td
+++ b/llvm/test/TableGen/get-operand-type.td
@@ -46,7 +46,10 @@ def InstC : Instruction {
 }
 
 // CHECK: #ifdef GET_INSTRINFO_OPERAND_TYPE
-// CHECK:        OpTypes::OpA, OpTypes::OpB, OpTypes::i32imm,
-// CHECK-NEXT:   OpTypes::i32imm, -1,
-// CHECK-NEXT:   OpTypes::RegClass, OpTypes::RegOp,
+// CHECK:        /* InstA */
+// CHECK-NEXT:   OpA, OpB, i32imm,
+// CHECK-NEXT:   /* InstB */
+// CHECK-NEXT:   i32imm, -1,
+// CHECK-NEXT:   /* InstC */
+// CHECK-NEXT:   RegClass, RegOp,
 // CHECK: #endif // GET_INSTRINFO_OPERAND_TYPE

diff  --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
index 56252f0b69ccb..e33843b0c06a2 100644
--- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
+++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
@@ -379,6 +379,9 @@ void InstrInfoEmitter::emitOperandTypeMappings(
   OS << "namespace " << Namespace << " {\n";
   OS << "LLVM_READONLY\n";
   OS << "static int getOperandType(uint16_t Opcode, uint16_t OpIdx) {\n";
+  auto getInstrName = [&](int I) -> StringRef {
+    return NumberedInstructions[I]->TheDef->getName();
+  };
   // TODO: Factor out duplicate operand lists to compress the tables.
   if (!NumberedInstructions.empty()) {
     std::vector<int> OperandOffsets;
@@ -408,8 +411,10 @@ void InstrInfoEmitter::emitOperandTypeMappings(
     OS << ((OperandRecords.size() <= UINT16_MAX) ? "  const uint16_t"
                                                  : "  const uint32_t");
     OS << " Offsets[] = {\n";
-    for (int I = 0, E = OperandOffsets.size(); I != E; ++I)
+    for (int I = 0, E = OperandOffsets.size(); I != E; ++I) {
+      OS << "    /* " << getInstrName(I) << " */\n";
       OS << "    " << OperandOffsets[I] << ",\n";
+    }
     OS << "  };\n";
 
     // Add an entry for the end so that we don't need to special case it below.
@@ -419,22 +424,22 @@ void InstrInfoEmitter::emitOperandTypeMappings(
     // Size the signed integer operand type to save space.
     assert(EnumVal <= INT16_MAX &&
            "Too many operand types for operand types table");
+    OS << "\n  using namespace OpTypes;\n";
     OS << ((EnumVal <= INT8_MAX) ? "  const int8_t" : "  const int16_t");
     OS << " OpcodeOperandTypes[] = {\n    ";
-    for (int I = 0, E = OperandRecords.size(), CurOffset = 1; I != E; ++I) {
+    for (int I = 0, E = OperandRecords.size(), CurOffset = 0; I != E; ++I) {
       // We print each Opcode's operands in its own row.
       if (I == OperandOffsets[CurOffset]) {
-        OS << "\n    ";
-        // If there are empty rows, mark them with an empty comment.
+        OS << "\n    /* " << getInstrName(CurOffset) << " */\n    ";
         while (OperandOffsets[++CurOffset] == I)
-          OS << "/**/\n    ";
+          OS << "/* " << getInstrName(CurOffset) << " */\n    ";
       }
       Record *OpR = OperandRecords[I];
       if ((OpR->isSubClassOf("Operand") ||
            OpR->isSubClassOf("RegisterOperand") ||
            OpR->isSubClassOf("RegisterClass")) &&
           !OpR->isAnonymous())
-        OS << "OpTypes::" << OpR->getName();
+        OS << OpR->getName();
       else
         OS << -1;
       OS << ", ";


        


More information about the llvm-commits mailing list