[llvm] [TableGen] Improvements to Named operands in InstrInfoEmitter (PR #124960)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 29 17:48:08 PST 2025


================
@@ -283,22 +241,52 @@ void InstrInfoEmitter::emitOperandNameMappings(
     raw_ostream &OS, const CodeGenTarget &Target,
     ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
   StringRef Namespace = Target.getInstNamespace();
-  // Map of operand names to their enumeration value.  This will be used to
-  // generate the OpName enum.
-  std::map<std::string, unsigned> Operands;
-  OpNameMapTy OperandMap;
 
-  initOperandMapData(NumberedInstructions, Namespace, Operands, OperandMap);
+  /// To facilitate assigning OpName enum values in the sorted alphabetical
+  /// order, we go through an indirection from OpName -> ID, and Enum -> ID.
+  /// This allows us to build the OpList and assign IDs to OpNames in a single
+  /// scan of the instructions below.
+
+  // Map of operand names to their ID.
+  std::map<std::string, unsigned> OperandNameToID;
+  // Map from operand name enum value -> ID.
+  std::vector<unsigned> OperandEnumToID;
+
+  /// The keys of this map is a map which have OpName ID values as their keys
+  /// and instruction operand indices as their values. The values of this map
+  /// are lists of instruction names. This map helps to unique entries among
+  /// instructions that have identical OpName -> Operand index mapping.
+  std::map<std::map<unsigned, unsigned>, std::vector<std::string>> OperandMap;
+
+  // Max operand index seen.
+  unsigned MaxOperandNo = 0;
+  unsigned NumOperandNames = 0;
+
+  for (const CodeGenInstruction *Inst : NumberedInstructions) {
+    if (!Inst->TheDef->getValueAsBit("UseNamedOperandTable"))
+      continue;
+    std::map<unsigned, unsigned> OpList;
+    for (const auto &Info : Inst->Operands) {
+      auto [I, Inserted] = OperandNameToID.try_emplace(Info.Name, 0);
+      if (Inserted)
+        I->second = NumOperandNames++;
+      OpList[I->second] = Info.MIOperandNo;
+      MaxOperandNo = std::max(MaxOperandNo, Info.MIOperandNo);
+    }
+    OperandMap[OpList].push_back(Inst->TheDef->getName().str());
+  }
+
+  OperandEnumToID.reserve(NumOperandNames);
----------------
jurahul wrote:

I'm not sure. The intent is to emit the OpName values in alphabetical order and match the enum values. Are you suggesting that instead we do away with alphabetical order and instead emit them in their ID order? I think that would work as well (since it will essentially just reorder the current enum definitions in their enum value order in the emitted  code). 

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


More information about the llvm-commits mailing list