Skip to content

Conversation

@sweiglbosker
Copy link
Contributor

Fixes #172647

Currently, MC assumes that all target-feature flag attributes are well formed and will crash otherwise. This change handles those cases more gracefully.

@llvmbot llvmbot added the llvm:mc Machine (object) code label Dec 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 19, 2025

@llvm/pr-subscribers-llvm-mc

Author: Stefan Weigl-Bosker (sweiglbosker)

Changes

Fixes #172647

Currently, MC assumes that all target-feature flag attributes are well formed and will crash otherwise. This change handles those cases more gracefully.


Full diff: https://github.com/llvm/llvm-project/pull/173119.diff

2 Files Affected:

  • (modified) llvm/lib/MC/MCSubtargetInfo.cpp (+5-3)
  • (added) llvm/test/CodeGen/Generic/invalid-target-feature.ll (+9)
diff --git a/llvm/lib/MC/MCSubtargetInfo.cpp b/llvm/lib/MC/MCSubtargetInfo.cpp
index 89a08327dd259..23fe9ee8d61ef 100644
--- a/llvm/lib/MC/MCSubtargetInfo.cpp
+++ b/llvm/lib/MC/MCSubtargetInfo.cpp
@@ -59,9 +59,11 @@ void ClearImpliedBits(FeatureBitset &Bits, unsigned Value,
 
 static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
                              ArrayRef<SubtargetFeatureKV> FeatureTable) {
-  assert(SubtargetFeatures::hasFlag(Feature) &&
-         "Feature flags should start with '+' or '-'");
-
+  if (!SubtargetFeatures::hasFlag(Feature)) {
+    errs() << "feature flag '" << Feature
+           << "' must start with either '+' or '-' (ignoring feature)\n";
+    return;
+  }
   // Find feature in table.
   const SubtargetFeatureKV *FeatureEntry =
       Find(SubtargetFeatures::StripFlag(Feature), FeatureTable);
diff --git a/llvm/test/CodeGen/Generic/invalid-target-feature.ll b/llvm/test/CodeGen/Generic/invalid-target-feature.ll
new file mode 100644
index 0000000000000..6592b19dbe5cd
--- /dev/null
+++ b/llvm/test/CodeGen/Generic/invalid-target-feature.ll
@@ -0,0 +1,9 @@
+; Check that we handle a malformed feature flag gracefully.
+
+; RUN: llc %s -o - 2>&1 | FileCheck %s
+
+; CHECK: feature flag 'foobar' must start with either '+' or '-' (ignoring feature)
+define void @f() "target-features"="foobar" {
+entry:
+  ret void
+}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:mc Machine (object) code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opt crashes when running on IR with `target triple x86_64-unknown-linux-gnu'

2 participants