-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[NFC][TableGen] Remove small heap allocations in SearchableTableEmitter #147845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jurahul
wants to merge
5
commits into
llvm:main
Choose a base branch
from
jurahul:searchable_table_emitter_cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+41
−30
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…ter` Change `GenericEnum` to not heap allocate its entries. Instead stash them directly in the `Entries` vector. Change `EntryMap` to hold an index as opposed to a pointer to the entry (the original reason why they were unique_ptr).
6b65d8a
to
10980e6
Compare
topperc
reviewed
Jul 9, 2025
@llvm/pr-subscribers-tablegen Author: Rahul Joshi (jurahul) ChangesChange Full diff: https://github.com/llvm/llvm-project/pull/147845.diff 1 Files Affected:
diff --git a/llvm/utils/TableGen/SearchableTableEmitter.cpp b/llvm/utils/TableGen/SearchableTableEmitter.cpp
index 38fc1ee5e4020..4dd651affa874 100644
--- a/llvm/utils/TableGen/SearchableTableEmitter.cpp
+++ b/llvm/utils/TableGen/SearchableTableEmitter.cpp
@@ -17,6 +17,7 @@
#include "Common/CodeGenTarget.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/TableGen/Error.h"
@@ -44,13 +45,23 @@ static int64_t getInt(const Record *R, StringRef Field) {
namespace {
struct GenericEnum {
- using Entry = std::pair<StringRef, int64_t>;
+ struct Entry {
+ StringRef Name;
+ int64_t Value;
+ Entry(StringRef N, int64_t V) : Name(N), Value(V) {}
+ };
std::string Name;
const Record *Class = nullptr;
std::string PreprocessorGuard;
- std::vector<std::unique_ptr<Entry>> Entries;
- DenseMap<const Record *, Entry *> EntryMap;
+ MapVector<const Record *, Entry> Entries;
+
+ const Entry *getEntry(const Record *Def) const {
+ auto II = Entries.find(Def);
+ if (II == Entries.end())
+ return nullptr;
+ return &II->second;
+ }
};
struct GenericField {
@@ -129,11 +140,12 @@ class SearchableTableEmitter {
else if (Field.IsInstruction)
return I->getAsString();
else if (Field.Enum) {
- auto *Entry = Field.Enum->EntryMap[cast<DefInit>(I)->getDef()];
+ const GenericEnum::Entry *Entry =
+ Field.Enum->getEntry(cast<DefInit>(I)->getDef());
if (!Entry)
PrintFatalError(Loc,
Twine("Entry for field '") + Field.Name + "' is null");
- return Entry->first.str();
+ return Entry->Name.str();
}
PrintFatalError(Loc, Twine("invalid field type for field '") + Field.Name +
"'; expected: bit, bits, string, or code");
@@ -221,7 +233,7 @@ int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
}
if (Field.Enum) {
const Record *EnumEntry = Rec->getValueAsDef(Field.Name);
- return Field.Enum->EntryMap[EnumEntry]->second;
+ return Field.Enum->getEntry(EnumEntry)->Value;
}
assert(isa<BitsRecTy>(Field.RecType) && "unexpected field type");
@@ -272,8 +284,8 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
} else if (Field.Enum) {
auto LHSr = cast<DefInit>(LHSI)->getDef();
auto RHSr = cast<DefInit>(RHSI)->getDef();
- int64_t LHSv = Field.Enum->EntryMap[LHSr]->second;
- int64_t RHSv = Field.Enum->EntryMap[RHSr]->second;
+ int64_t LHSv = Field.Enum->getEntry(LHSr)->Value;
+ int64_t RHSv = Field.Enum->getEntry(RHSr)->Value;
if (LHSv < RHSv)
return true;
if (LHSv > RHSv)
@@ -308,8 +320,9 @@ void SearchableTableEmitter::emitGenericEnum(const GenericEnum &Enum,
emitIfdef((Twine("GET_") + Enum.PreprocessorGuard + "_DECL").str(), OS);
OS << "enum " << Enum.Name << " {\n";
- for (const auto &Entry : Enum.Entries)
- OS << " " << Entry->first << " = " << Entry->second << ",\n";
+ for (const auto &[Name, Value] :
+ make_second_range(Enum.Entries.getArrayRef()))
+ OS << " " << Name << " = " << Value << ",\n";
OS << "};\n";
OS << "#endif\n\n";
@@ -625,30 +638,29 @@ std::unique_ptr<SearchIndex> SearchableTableEmitter::parseSearchIndex(
void SearchableTableEmitter::collectEnumEntries(
GenericEnum &Enum, StringRef NameField, StringRef ValueField,
ArrayRef<const Record *> Items) {
+ Enum.Entries.reserve(Items.size());
for (const Record *EntryRec : Items) {
- StringRef Name;
- if (NameField.empty())
- Name = EntryRec->getName();
- else
- Name = EntryRec->getValueAsString(NameField);
-
- int64_t Value = 0;
- if (!ValueField.empty())
- Value = getInt(EntryRec, ValueField);
-
- Enum.Entries.push_back(std::make_unique<GenericEnum::Entry>(Name, Value));
- Enum.EntryMap.try_emplace(EntryRec, Enum.Entries.back().get());
+ StringRef Name = NameField.empty() ? EntryRec->getName()
+ : EntryRec->getValueAsString(NameField);
+ int64_t Value = ValueField.empty() ? 0 : getInt(EntryRec, ValueField);
+ Enum.Entries.try_emplace(EntryRec, Name, Value);
}
+ // If no values are provided for enums, assign values in the order of sorted
+ // enum names.
if (ValueField.empty()) {
- llvm::stable_sort(Enum.Entries,
- [](const std::unique_ptr<GenericEnum::Entry> &LHS,
- const std::unique_ptr<GenericEnum::Entry> &RHS) {
- return LHS->first < RHS->first;
- });
-
- for (size_t i = 0; i < Enum.Entries.size(); ++i)
- Enum.Entries[i]->second = i;
+ // Copy the map entries for sorting and clear the map.
+ auto SavedEntries = Enum.Entries.takeVector();
+ llvm::stable_sort(
+ SavedEntries,
+ [](const std::pair<const Record *, GenericEnum::Entry> &LHS,
+ const std::pair<const Record *, GenericEnum::Entry> &RHS) {
+ return LHS.second.Name < RHS.second.Name;
+ });
+
+ // Repopulate entries using the new sorted order.
+ for (auto [Idx, Entry] : enumerate(SavedEntries))
+ Enum.Entries.try_emplace(Entry.first, Entry.second.Name, Idx);
}
}
|
mshockwave
reviewed
Jul 10, 2025
arsenm
approved these changes
Jul 10, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Change
GenericEnum
to not heap allocate its entries. Instead stash them directly in theEntries
vector. ChangeEntryMap
to hold an index as opposed to a pointer to the entry (the original reason why they were unique_ptr).