[llvm] [RFC][CodeGen] Add generic target feature checks for intrinsics (PR #201470)

Pierre van Houtryve via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 12 04:29:00 PDT 2026


================
@@ -340,6 +340,98 @@ bool MCSubtargetInfo::checkFeatures(StringRef FS) const {
   });
 }
 
+static bool hasFeature(StringRef Feature, const FeatureBitset &FeatureBits,
+                       ArrayRef<SubtargetFeatureKV> ProcFeatures) {
+  bool ShouldBeEnabled = true;
+  if (Feature.consume_front("+"))
+    ShouldBeEnabled = true;
+  else if (Feature.consume_front("-"))
+    ShouldBeEnabled = false;
+
+  const SubtargetFeatureKV *FeatureEntry = Find(Feature, ProcFeatures);
+  if (!FeatureEntry) {
+    report_fatal_error(Twine("'") + Feature +
+                       "' is not a recognized feature for this target");
+  }
+
+  return FeatureBits.test(FeatureEntry->Value) == ShouldBeEnabled;
+}
+
+namespace {
+class FeatureExpressionParser {
+  StringRef Expr;
+  const FeatureBitset &FeatureBits;
+  ArrayRef<SubtargetFeatureKV> ProcFeatures;
+  size_t Pos = 0;
+
+public:
+  FeatureExpressionParser(StringRef Expr, const FeatureBitset &FeatureBits,
+                          ArrayRef<SubtargetFeatureKV> ProcFeatures)
+      : Expr(Expr), FeatureBits(FeatureBits), ProcFeatures(ProcFeatures) {}
+
+  bool parse() {
+    bool Result = parseOr();
+    if (Pos != Expr.size())
+      report_fatal_error("malformed target feature expression");
+    return Result;
+  }
+
+private:
+  bool consume(char C) {
+    if (Pos == Expr.size() || Expr[Pos] != C)
+      return false;
+    ++Pos;
+    return true;
+  }
+
+  bool parseOr() {
+    bool Result = parseAnd();
+    while (consume('|')) {
+      bool RHS = parseAnd();
+      Result = Result || RHS;
+    }
+    return Result;
+  }
+
+  bool parseAnd() {
+    bool Result = parsePrimary();
+    while (consume(',')) {
+      bool RHS = parsePrimary();
+      Result = Result && RHS;
+    }
+    return Result;
+  }
+
+  bool parsePrimary() {
+    if (consume('(')) {
+      bool Result = parseOr();
+      if (!consume(')'))
+        report_fatal_error("malformed target feature expression");
+      return Result;
+    }
+
+    size_t Start = Pos;
+    while (Pos != Expr.size() && Expr[Pos] != ',' && Expr[Pos] != '|' &&
+           Expr[Pos] != '(' && Expr[Pos] != ')')
----------------
Pierre-vh wrote:

Isn't there a helper somewhere we can use to make this less repetitive ? like a "find/contains" of some sort.
If not I feel like it wouldn't hurt to write one because this is a lot of noise just to check if the current char is one of `,|()`

Actually now that I look at the code a bit more, isn't this just a "find next occurrence in string" kind of thing?

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


More information about the llvm-commits mailing list