[clang] [llvm] [AArch64][Driver] Fix behavior of +nofeat modifier on -mcpu (PR #203458)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 28 23:52:50 PDT 2026
https://github.com/yamash-fj updated https://github.com/llvm/llvm-project/pull/203458
>From a443cce306bd40fe24155cd1b057c3399961f3f6 Mon Sep 17 00:00:00 2001
From: yamash-fj <yamashita.ta-16 at fujitsu.com>
Date: Fri, 12 Jun 2026 13:52:40 +0900
Subject: [PATCH 1/3] [AArch64][Driver] Fix behavior of +nofeat modifier
Fix #192186.
An explicit ``+nofeat`` of ``-mcpu`` option is ignored when the feature is implied by the base-architecture.
Two approaches were considerd:
1. include architechture-implied features in the ``CPUExtension`` set
2. preserve explicit user ``+nofeat`` even for architecture-implied features
, and this patch implements the latter.
When checking CPU extensions, simultaneously set ``Enabled`` flags for the base-architecture's default extensions. By setting ``Enabled``, an explicit +nofeat disables the feature and marks it as ``Touched``, and it prevents ``-feat`` from going away.
Behaivior changes:
For architecuture-default features:
- preserve explicit ``+nofeat``(#192186).
- remove redundant ``+feat``.
---
clang/test/Driver/aarch64-no-feat.c | 31 +++++++++++++++++++
llvm/lib/TargetParser/AArch64TargetParser.cpp | 9 +++++-
2 files changed, 39 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Driver/aarch64-no-feat.c
diff --git a/clang/test/Driver/aarch64-no-feat.c b/clang/test/Driver/aarch64-no-feat.c
new file mode 100644
index 0000000000000..ef3472b3a63e0
--- /dev/null
+++ b/clang/test/Driver/aarch64-no-feat.c
@@ -0,0 +1,31 @@
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2 %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-DEFAULT
+// NEOVERSE-V2-DEFAULT-NOT: "-target-feature" "+sb"
+// NEOVERSE-V2-DEFAULT-NOT: "-target-feature" "-sb"
+// NEOVERSE-V2-DEFAULT: "-target-feature" "+rand"
+// NEOVERSE-V2-DEFAULT-SAME: "-target-feature" "+sve"
+// NEOVERSE-V2-DEFAULT-NOT: "-target-feature" "+sha2"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+nosb %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NOSB
+// NEOVERSE-V2-NOSB: "-target-feature" "-sb"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+sb %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-SB
+// NEOVERSE-V2-SB-NOT: "-target-feature" "+sb"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+nosve %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NOSVE
+// NEOVERSE-V2-NOSVE: "-target-feature" "-sve"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+sve %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-SVE
+// NEOVERSE-V2-SVE: "-target-feature" "+sve"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+norng %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NORNG
+// NEOVERSE-V2-NORNG: "-target-feature" "-rand"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+rng %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-RNG
+// NEOVERSE-V2-RNG: "-target-feature" "+rand"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+nosha2 %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NOSHA2
+// NEOVERSE-V2-NOSHA2-NOT: "-target-feature" "+sha2"
+// NEOVERSE-V2-NOSHA2-NOT: "-target-feature" "-sha2"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+sha2 %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-SHA2
+// NEOVERSE-V2-SHA2: "-target-feature" "+sha2"
diff --git a/llvm/lib/TargetParser/AArch64TargetParser.cpp b/llvm/lib/TargetParser/AArch64TargetParser.cpp
index 2c0211b3a2919..2938c40139eb7 100644
--- a/llvm/lib/TargetParser/AArch64TargetParser.cpp
+++ b/llvm/lib/TargetParser/AArch64TargetParser.cpp
@@ -340,14 +340,21 @@ void AArch64::ExtensionSet::disable(ArchExtKind E) {
disable(Dep.Later);
}
+static void setEnableIfMandatory(AArch64::ExtensionSet &Exts, AArch64::ExtensionInfo E) {
+ if (Exts.BaseArch->DefaultExts.test(E.ID))
+ Exts.Enabled.set(E.ID);
+}
+
void AArch64::ExtensionSet::addCPUDefaults(const CpuInfo &CPU) {
LLVM_DEBUG(llvm::dbgs() << "addCPUDefaults(" << CPU.Name << ")\n");
BaseArch = &CPU.Arch;
AArch64::ExtensionBitset CPUExtensions = CPU.getImpliedExtensions();
- for (const auto &E : Extensions)
+ for (const auto &E : Extensions) {
if (CPUExtensions.test(E.ID))
enable(E.ID);
+ setEnableIfMandatory(*this, E);
+ }
}
void AArch64::ExtensionSet::addArchDefaults(const ArchInfo &Arch) {
>From 9e7859092b94006ec391b6df29b69a579b575962 Mon Sep 17 00:00:00 2001
From: yamash-fj <yamashita.ta-16 at fujitsu.com>
Date: Thu, 18 Jun 2026 14:50:05 +0900
Subject: [PATCH 2/3] [AArch64][Driver] Fix clang-format
---
llvm/lib/TargetParser/AArch64TargetParser.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/TargetParser/AArch64TargetParser.cpp b/llvm/lib/TargetParser/AArch64TargetParser.cpp
index 2938c40139eb7..86853499920c4 100644
--- a/llvm/lib/TargetParser/AArch64TargetParser.cpp
+++ b/llvm/lib/TargetParser/AArch64TargetParser.cpp
@@ -340,9 +340,10 @@ void AArch64::ExtensionSet::disable(ArchExtKind E) {
disable(Dep.Later);
}
-static void setEnableIfMandatory(AArch64::ExtensionSet &Exts, AArch64::ExtensionInfo E) {
- if (Exts.BaseArch->DefaultExts.test(E.ID))
- Exts.Enabled.set(E.ID);
+static void setEnableIfMandatory(AArch64::ExtensionSet &Exts,
+ AArch64::ExtensionInfo E) {
+ if (Exts.BaseArch->DefaultExts.test(E.ID))
+ Exts.Enabled.set(E.ID);
}
void AArch64::ExtensionSet::addCPUDefaults(const CpuInfo &CPU) {
>From 14d4c8a8fe1e232c474ef4c54f39801fd0e9aad2 Mon Sep 17 00:00:00 2001
From: yamash-fj <yamashita.ta-16 at fujitsu.com>
Date: Mon, 29 Jun 2026 15:50:41 +0900
Subject: [PATCH 3/3] review tests; delete or move some tests
---
clang/test/Driver/aarch64-mcpu-no-feat.c | 17 +++++++++++++
clang/test/Driver/aarch64-no-feat.c | 31 ------------------------
clang/test/Driver/arm-sb.c | 4 +++
3 files changed, 21 insertions(+), 31 deletions(-)
create mode 100644 clang/test/Driver/aarch64-mcpu-no-feat.c
delete mode 100644 clang/test/Driver/aarch64-no-feat.c
diff --git a/clang/test/Driver/aarch64-mcpu-no-feat.c b/clang/test/Driver/aarch64-mcpu-no-feat.c
new file mode 100644
index 0000000000000..b04e99f37b963
--- /dev/null
+++ b/clang/test/Driver/aarch64-mcpu-no-feat.c
@@ -0,0 +1,17 @@
+// Check that explicit -mcpu=<cpu>+no<feature> preserves -<feature> option and removes the feature when the feature is implied by the CPU or its base architecture.
+// SVE and RandGen are default features of neoverse-v2 (defined in AArch64Processors.td).
+// SVE and SB are default features of armv9 (defined in AArch64Features.td).
+// SHA2 is not part of the default feature set for either Neoverse V2 or Armv9.
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+nosve %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NOSVE
+// NEOVERSE-V2-NOSVE: "-target-feature" "-sve"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+norng %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NORNG
+// NEOVERSE-V2-NORNG: "-target-feature" "-rand"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+nosb %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NOSB
+// NEOVERSE-V2-NOSB: "-target-feature" "-sb"
+
+// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+nosha2 %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NOSHA2
+// NEOVERSE-V2-NOSHA2-NOT: "-target-feature" "+sha2"
+// NEOVERSE-V2-NOSHA2-NOT: "-target-feature" "-sha2"
diff --git a/clang/test/Driver/aarch64-no-feat.c b/clang/test/Driver/aarch64-no-feat.c
deleted file mode 100644
index ef3472b3a63e0..0000000000000
--- a/clang/test/Driver/aarch64-no-feat.c
+++ /dev/null
@@ -1,31 +0,0 @@
-// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2 %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-DEFAULT
-// NEOVERSE-V2-DEFAULT-NOT: "-target-feature" "+sb"
-// NEOVERSE-V2-DEFAULT-NOT: "-target-feature" "-sb"
-// NEOVERSE-V2-DEFAULT: "-target-feature" "+rand"
-// NEOVERSE-V2-DEFAULT-SAME: "-target-feature" "+sve"
-// NEOVERSE-V2-DEFAULT-NOT: "-target-feature" "+sha2"
-
-// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+nosb %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NOSB
-// NEOVERSE-V2-NOSB: "-target-feature" "-sb"
-
-// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+sb %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-SB
-// NEOVERSE-V2-SB-NOT: "-target-feature" "+sb"
-
-// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+nosve %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NOSVE
-// NEOVERSE-V2-NOSVE: "-target-feature" "-sve"
-
-// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+sve %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-SVE
-// NEOVERSE-V2-SVE: "-target-feature" "+sve"
-
-// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+norng %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NORNG
-// NEOVERSE-V2-NORNG: "-target-feature" "-rand"
-
-// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+rng %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-RNG
-// NEOVERSE-V2-RNG: "-target-feature" "+rand"
-
-// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+nosha2 %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-NOSHA2
-// NEOVERSE-V2-NOSHA2-NOT: "-target-feature" "+sha2"
-// NEOVERSE-V2-NOSHA2-NOT: "-target-feature" "-sha2"
-
-// RUN: %clang --target=aarch64-linux-gnu -mcpu=neoverse-v2+sha2 %s -### 2>&1 | FileCheck %s --check-prefix=NEOVERSE-V2-SHA2
-// NEOVERSE-V2-SHA2: "-target-feature" "+sha2"
diff --git a/clang/test/Driver/arm-sb.c b/clang/test/Driver/arm-sb.c
index 9c0f381171cb6..a7987d582e2a6 100644
--- a/clang/test/Driver/arm-sb.c
+++ b/clang/test/Driver/arm-sb.c
@@ -14,3 +14,7 @@
// RUN: %clang -### -target aarch64-none-elf -march=armv8.5a+nosb %s 2>&1 | FileCheck %s --check-prefix=NOSB
// ABSENT-NOT: "-target-feature" "+sb"
// ABSENT-NOT: "-target-feature" "-sb"
+
+// RUN: %clang -### -target aarch64-none-elf -mcpu=neoverse-v2+sb %s 2>&1 | FileCheck %s --check-prefix=REDUNDANT
+// REDUNDANT-NOT: "-target-feature" "+sb"
+// REDUNDANT-NOT: "-target-feature" "-sb"
More information about the cfe-commits
mailing list