Skip to content

Commit f5720ef

Browse files
committed
Use MapVector
1 parent 10980e6 commit f5720ef

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

llvm/utils/TableGen/SearchableTableEmitter.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Common/CodeGenTarget.h"
1818
#include "llvm/ADT/ArrayRef.h"
1919
#include "llvm/ADT/DenseMap.h"
20+
#include "llvm/ADT/MapVector.h"
2021
#include "llvm/ADT/STLExtras.h"
2122
#include "llvm/ADT/StringExtras.h"
2223
#include "llvm/TableGen/Error.h"
@@ -47,23 +48,19 @@ struct GenericEnum {
4748
struct Entry {
4849
StringRef Name;
4950
int64_t Value;
50-
const Record *Def;
51-
Entry(StringRef N, int64_t V, const Record *D)
52-
: Name(N), Value(V), Def(D) {}
51+
Entry(StringRef N, int64_t V) : Name(N), Value(V) {}
5352
};
5453

5554
std::string Name;
5655
const Record *Class = nullptr;
5756
std::string PreprocessorGuard;
58-
std::vector<Entry> Entries;
59-
// Map from a Record to an index into the `Entries` vector.
60-
DenseMap<const Record *, uint32_t> EntryMap;
57+
MapVector<const Record *, Entry> Entries;
6158

6259
const Entry *getEntry(const Record *Def) const {
63-
auto II = EntryMap.find(Def);
64-
if (II == EntryMap.end())
60+
auto II = Entries.find(Def);
61+
if (II == Entries.end())
6562
return nullptr;
66-
return &Entries[II->second];
63+
return &II->second;
6764
}
6865
};
6966

@@ -323,8 +320,8 @@ void SearchableTableEmitter::emitGenericEnum(const GenericEnum &Enum,
323320
emitIfdef((Twine("GET_") + Enum.PreprocessorGuard + "_DECL").str(), OS);
324321

325322
OS << "enum " << Enum.Name << " {\n";
326-
for (const auto &[Name, Value, _] : Enum.Entries)
327-
OS << " " << Name << " = " << Value << ",\n";
323+
for (const auto &[_, Entry] : Enum.Entries.getArrayRef())
324+
OS << " " << Entry.Name << " = " << Entry.Value << ",\n";
328325
OS << "};\n";
329326

330327
OS << "#endif\n\n";
@@ -645,24 +642,25 @@ void SearchableTableEmitter::collectEnumEntries(
645642
StringRef Name = NameField.empty() ? EntryRec->getName()
646643
: EntryRec->getValueAsString(NameField);
647644
int64_t Value = ValueField.empty() ? 0 : getInt(EntryRec, ValueField);
648-
Enum.Entries.emplace_back(Name, Value, EntryRec);
645+
Enum.Entries.try_emplace(EntryRec, Name, Value);
649646
}
650647

651648
// If no values are provided for enums, assign values in the order of sorted
652649
// enum names.
653650
if (ValueField.empty()) {
654-
llvm::stable_sort(Enum.Entries, [](const GenericEnum::Entry &LHS,
655-
const GenericEnum::Entry &RHS) {
656-
return LHS.Name < RHS.Name;
657-
});
658-
659-
for (auto [Idx, Entry] : enumerate(Enum.Entries))
660-
Entry.Value = Idx;
651+
// Copy the map entries for sorting and clear the map.
652+
auto SavedEntries = Enum.Entries.takeVector();
653+
llvm::stable_sort(
654+
SavedEntries,
655+
[](const std::pair<const Record *, GenericEnum::Entry> &LHS,
656+
const std::pair<const Record *, GenericEnum::Entry> &RHS) {
657+
return LHS.second.Name < RHS.second.Name;
658+
});
659+
660+
// Repopulate entries using the new sorted order.
661+
for (auto [Idx, Entry] : enumerate(SavedEntries))
662+
Enum.Entries.try_emplace(Entry.first, Entry.second.Name, Idx);
661663
}
662-
663-
// Populate the entry map after the `Entries` vector is finalized.
664-
for (auto [Idx, Entry] : enumerate(Enum.Entries))
665-
Enum.EntryMap.try_emplace(Entry.Def, Idx);
666664
}
667665

668666
void SearchableTableEmitter::collectTableEntries(

0 commit comments

Comments
 (0)