[llvm] [TableGen] Print a warning when a Processor contains duplicate Features / TuneFeatures (PR #137864)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 29 12:11:25 PDT 2025


https://github.com/mshockwave created https://github.com/llvm/llvm-project/pull/137864

I don't think a processor should normally contains duplicate features. I'm open to make this an error rather than a warning.

>From a5144cc04a8a17e5b09cf3c3a845ae31cfd14fd1 Mon Sep 17 00:00:00 2001
From: Min-Yih Hsu <min.hsu at sifive.com>
Date: Tue, 29 Apr 2025 11:57:02 -0700
Subject: [PATCH] [TableGen] Print a warning when a Processor contains
 duplicate Features / TuneFeatures

---
 llvm/test/TableGen/DuplicateProcessorFeatures.td | 16 ++++++++++++++++
 llvm/utils/TableGen/SubtargetEmitter.cpp         | 12 ++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 llvm/test/TableGen/DuplicateProcessorFeatures.td

diff --git a/llvm/test/TableGen/DuplicateProcessorFeatures.td b/llvm/test/TableGen/DuplicateProcessorFeatures.td
new file mode 100644
index 0000000000000..324a70f2a9d9e
--- /dev/null
+++ b/llvm/test/TableGen/DuplicateProcessorFeatures.td
@@ -0,0 +1,16 @@
+// RUN: llvm-tblgen -gen-subtarget -I %p/../../include %s 2>&1 | FileCheck %s
+
+include "llvm/Target/Target.td"
+
+def MyTarget : Target;
+
+def FeatureA : SubtargetFeature<"NameA", "", "", "">;
+def FeatureB : SubtargetFeature<"NameB", "", "", "">;
+def FeatureC : SubtargetFeature<"NameC", "", "", "">;
+
+// CHECK: warning: Processor CPU0 has duplicate Features: NameA
+def P0 : ProcessorModel<"CPU0", NoSchedModel, [FeatureA, FeatureB, FeatureA]>;
+// CHECK: warning: Processor CPU1 has duplicate TuneFeatures: NameB
+def P1 : ProcessorModel<"CPU1", NoSchedModel,
+                        /*Features=*/[FeatureA, FeatureB, FeatureC],
+                        /*TuneFeatures=*/[FeatureB, FeatureC, FeatureB]>;
diff --git a/llvm/utils/TableGen/SubtargetEmitter.cpp b/llvm/utils/TableGen/SubtargetEmitter.cpp
index 8d2fc99f84670..c608e85cddffa 100644
--- a/llvm/utils/TableGen/SubtargetEmitter.cpp
+++ b/llvm/utils/TableGen/SubtargetEmitter.cpp
@@ -333,6 +333,16 @@ unsigned SubtargetEmitter::cpuNames(raw_ostream &OS) {
   return Names.size();
 }
 
+static void checkDuplicateCPUFeatures(StringRef CPUName, StringRef FeatureKind,
+                                      ConstRecVec Features) {
+  SmallPtrSet<const Record *, 8> FeatureSet;
+  for (const auto *FeatureRec : Features) {
+    if (!FeatureSet.insert(FeatureRec).second)
+      PrintWarning("Processor " + CPUName + " has duplicate " + FeatureKind +
+                   ": " + FeatureRec->getValueAsString("Name"));
+  }
+}
+
 //
 // CPUKeyValues - Emit data of all the subtarget processors.  Used by command
 // line.
@@ -357,8 +367,10 @@ unsigned SubtargetEmitter::cpuKeyValues(raw_ostream &OS,
   for (const Record *Processor : ProcessorList) {
     StringRef Name = Processor->getValueAsString("Name");
     ConstRecVec FeatureList = Processor->getValueAsListOfDefs("Features");
+    checkDuplicateCPUFeatures(Name, "Features", FeatureList);
     ConstRecVec TuneFeatureList =
         Processor->getValueAsListOfDefs("TuneFeatures");
+    checkDuplicateCPUFeatures(Name, "TuneFeatures", TuneFeatureList);
 
     // Emit as "{ "cpu", "description", 0, { f1 , f2 , ... fn } },".
     OS << " { "



More information about the llvm-commits mailing list