[llvm] [TableGen] Improvements to Named operands in InstrInfoEmitter (PR #124960)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 29 12:00:37 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);
----------------
topperc wrote:
Could we change `OperandNameToID` to a `MapVector<std::string, unsigned>`? Iterating over that will be the order they were inserted. Then I don't think you would need OperandEnumToID.
https://github.com/llvm/llvm-project/pull/124960
More information about the llvm-commits
mailing list