[llvm-branch-commits] [llvm] TableGen: Use a compact table for CPU aliases (PR #211952)

Alexis Engelke via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Jul 24 22:01:45 PDT 2026


================
@@ -283,73 +290,92 @@ static void checkDuplicateCPUFeatures(StringRef CPUName,
 // CPUKeyValues - Emit data of all the subtarget processors.  Used by command
 // line.
 //
-std::pair<unsigned, unsigned>
+SubtargetEmitter::CPUKeyValuesInfo
 SubtargetEmitter::cpuKeyValues(raw_ostream &OS,
                                const FeatureMapTy &FeatureMap) {
+  // Gather and sort the processors. Only real processors go in the subtype
+  // table; aliases are stored in a separate, more compact table.
   std::vector<const Record *> ProcessorList =
       Records.getAllDerivedDefinitions("Processor");
-
-  StringMap<const Record *> ProcessorMap;
-  for (const Record *Processor : ProcessorList)
-    ProcessorMap[Processor->getValueAsString("Name")] = Processor;
-
-  // Maps each emitted CPU name (processor or alias) to the processor record it
-  // resolves to. Keying by name detects duplicates on insertion.
-  StringMap<const Record *> SubTypeEntries;
-  for (const Record *Processor : ProcessorList)
-    SubTypeEntries[Processor->getValueAsString("Name")] = Processor;
-
+  llvm::sort(ProcessorList, LessRecordFieldName());
+
+  // Map from processor name to its index in the sorted subtype table, so that
+  // aliases can point at the canonical processor entry.
+  StringMap<unsigned> ProcessorIndex;
+  for (const auto &[Idx, Processor] : enumerate(ProcessorList))
+    ProcessorIndex[Processor->getValueAsString("Name")] = Idx;
+
+  // Validate and resolve each alias to the index of its canonical processor.
+  struct AliasEntry {
+    StringRef Name;
+    unsigned SubTypeIdx;
+  };
+  std::vector<AliasEntry> AliasEntries;
   std::vector<const Record *> ProcessorAliasList =
       Records.getAllDerivedDefinitionsIfDefined("ProcessorAlias");
+  AliasEntries.reserve(ProcessorAliasList.size());
+
+  StringSet<> AliasNames;
   for (const Record *Rec : ProcessorAliasList) {
     StringRef Name = Rec->getValueAsString("Name");
     StringRef Alias = Rec->getValueAsString("Alias");
-    auto It = ProcessorMap.find(Alias);
-    if (It == ProcessorMap.end())
+    auto It = ProcessorIndex.find(Alias);
+    if (It == ProcessorIndex.end())
       PrintFatalError(Rec, "Alias '" + Name +
                                "' references a non-existent Processor '" +
                                Alias + "'");
-    if (!SubTypeEntries.try_emplace(Name, It->second).second)
-      PrintFatalError(Rec, "Alias '" + Name + "' duplicates an existing " +
-                               (ProcessorMap.contains(Name) ? "Processor"
-                                                            : "alias"));
+    if (ProcessorIndex.contains(Name))
+      PrintFatalError(Rec,
+                      "Alias '" + Name + "' duplicates an existing Processor");
+    if (!AliasNames.insert(Name).second)
+      PrintFatalError(Rec, "Alias '" + Name + "' duplicates an existing alias");
+    AliasEntries.push_back({Name, It->second});
   }
 
-  // The table must stay sorted by key for the binary search in the lookups.
-  std::vector<std::pair<StringRef, const Record *>> SortedEntries;
-  SortedEntries.reserve(SubTypeEntries.size());
-  for (const auto &Entry : SubTypeEntries)
-    SortedEntries.emplace_back(Entry.getKey(), Entry.getValue());
-  llvm::sort(SortedEntries, llvm::less_first());
+  // The alias table must be sorted by key for the binary search in the lookups.
+  llvm::sort(AliasEntries, [](const AliasEntry &LHS, const AliasEntry &RHS) {
+    return LHS.Name < RHS.Name;
+  });
 
   StringToOffsetTable StrTab;
-  for (const auto &[Name, Proc] : SortedEntries)
-    StrTab.GetOrAddStringOffset(Name);
+  for (const Record *Processor : ProcessorList)
+    StrTab.GetOrAddStringOffset(Processor->getValueAsString("Name"));
+  for (const AliasEntry &Entry : AliasEntries)
+    StrTab.GetOrAddStringOffset(Entry.Name);
----------------
aengelke wrote:

For the StrTab, the mix of ProcessorList and AliasEntries should be sorted. (That's why the help test fails.)

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


More information about the llvm-branch-commits mailing list