[llvm] [TableGen] Store flat source operand number in OperandMap in PseudoLoweringEmitter. NFC (PR #135886)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 15 17:04:26 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-tablegen

Author: Craig Topper (topperc)

<details>
<summary>Changes</summary>

Previously we stored the index into the source CodeGenInstruction's operand list. Any operand with sub operands stored the same index into all of the OperandMap entries for that operand. The emitting loop would look up the MIOperandNo for the source and add the sub index.

This patch moves the MIOperandNo and adding the sub index into loop that updates OperandMap and the emitting loop just prints the index.

While there, I've added a check that MIOperandNo is the same for source and destination.

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


1 Files Affected:

- (modified) llvm/utils/TableGen/PseudoLoweringEmitter.cpp (+17-12) 


``````````diff
diff --git a/llvm/utils/TableGen/PseudoLoweringEmitter.cpp b/llvm/utils/TableGen/PseudoLoweringEmitter.cpp
index 7f67c13c0bbbd..96325eac95004 100644
--- a/llvm/utils/TableGen/PseudoLoweringEmitter.cpp
+++ b/llvm/utils/TableGen/PseudoLoweringEmitter.cpp
@@ -181,24 +181,32 @@ void PseudoLoweringEmitter::evaluateExpansion(const Record *Rec) {
     SourceOperands[SrcOp.Name] = Idx;
 
   LLVM_DEBUG(dbgs() << "  Operand mapping:\n");
-  for (unsigned i = 0, e = Insn.Operands.size(); i != e; ++i) {
+  for (const auto &[Idx, Opnd] : enumerate(Insn.Operands)) {
     // We've already handled constant values. Just map instruction operands
     // here.
-    if (OperandMap[Insn.Operands[i].MIOperandNo].Kind != OpData::Operand)
+    if (OperandMap[Opnd.MIOperandNo].Kind != OpData::Operand)
       continue;
     StringMap<unsigned>::iterator SourceOp =
-        SourceOperands.find(Dag->getArgNameStr(i));
+        SourceOperands.find(Dag->getArgNameStr(Idx));
     if (SourceOp == SourceOperands.end())
       PrintFatalError(Rec, "In pseudo instruction '" + Rec->getName() +
-                               "', output operand '" + Dag->getArgNameStr(i) +
+                               "', output operand '" + Dag->getArgNameStr(Idx) +
                                "' has no matching source operand");
+    const auto &SrcOpnd = SourceInsn.Operands[SourceOp->getValue()];
+    if (Opnd.MINumOperands != SrcOpnd.MINumOperands)
+      PrintFatalError(
+          Rec,
+          "In pseudo instruction '" + Rec->getName() + "', output operand '" +
+              Opnd.Rec->getName() +
+              "' has a different number of sub operands than source operand '" +
+              SrcOpnd.Rec->getName() + "'");
+
     // Map the source operand to the destination operand index for each
     // MachineInstr operand.
-    for (unsigned I = 0, E = Insn.Operands[i].MINumOperands; I != E; ++I)
-      OperandMap[Insn.Operands[i].MIOperandNo + I].Data.Operand =
-          SourceOp->getValue();
+    for (unsigned I = 0, E = Opnd.MINumOperands; I != E; ++I)
+      OperandMap[Opnd.MIOperandNo + I].Data.Operand = SrcOpnd.MIOperandNo + I;
 
-    LLVM_DEBUG(dbgs() << "    " << SourceOp->getValue() << " ==> " << i
+    LLVM_DEBUG(dbgs() << "    " << SourceOp->getValue() << " ==> " << Idx
                       << "\n");
   }
 
@@ -236,10 +244,7 @@ void PseudoLoweringEmitter::emitLoweringEmitter(raw_ostream &o) {
           switch (Expansion.OperandMap[MIOpNo + i].Kind) {
           case OpData::Operand:
             o << "    lowerOperand(MI->getOperand("
-              << Source.Operands[Expansion.OperandMap[MIOpNo].Data.Operand]
-                         .MIOperandNo +
-                     i
-              << "), MCOp);\n"
+              << Expansion.OperandMap[MIOpNo + i].Data.Operand << "), MCOp);\n"
               << "    Inst.addOperand(MCOp);\n";
             break;
           case OpData::Imm:

``````````

</details>


https://github.com/llvm/llvm-project/pull/135886


More information about the llvm-commits mailing list