[clang] [RISCV] Replace 'native' before applying features for -mtune=native:<feature_list> (PR #215891)

Craig Topper via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 14 14:48:45 PDT 2026


https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/215891

>From a1ab08be4b3f356ed476229207d2a496f5138abd Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 12 Aug 2026 10:55:36 -0700
Subject: [PATCH 1/3] [RISCV] Diagnose -mtune CPU with a : and no features as
 requiring -mexperimental-mtune-syntax.

Without -mexperimental-mtune-syntax -mtune=sifive-x280: was considering
sifive-x280: as the full CPU name. With this patch we now diagnose any use
of the : even if there's nothing after it.
---
 clang/lib/Driver/ToolChains/Arch/RISCV.cpp    | 17 ++++++++++-------
 clang/test/Driver/riscv-mtune-tune-features.c |  5 +++++
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
index c90d771e87a23..a1bf62f80589c 100644
--- a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
@@ -404,22 +404,25 @@ riscv::getRISCVTuneCPU(const Driver &D, const llvm::opt::ArgList &Args,
   if (!MTuneArg)
     return "";
 
-  StringRef MTune = MTuneArg->getValue();
-  // Split the CPU name part from the tune features string.
-  auto [TuneCPU, TFString] = MTune.split(':');
-  if (!Args.hasFlag(options::OPT_mexperimental_mtune_syntax,
+  StringRef TuneCPU = MTuneArg->getValue();
+  StringRef TFString;
+
+  auto Idx = TuneCPU.find(':');
+  if (Idx != StringRef::npos) {
+    if (!Args.hasFlag(options::OPT_mexperimental_mtune_syntax,
                     options::OPT_mno_experimental_mtune_syntax, false)) {
-    if (!TFString.empty()) {
       // Only print this diagnostics if it's used for retrieving tune features
       // to avoid printing the same error message multiple times.
       if (TuneFeatures)
         D.Diag(diag::err_drv_invalid_riscv_mtune_string)
-            << 0 << MTune
+            << 0 << TuneCPU
             << "require '-mexperimental-mtune-syntax' to use with tune feature "
                "string";
       return std::nullopt;
     }
-    return MTune;
+
+    TFString = TuneCPU.substr(Idx + 1);
+    TuneCPU = TuneCPU.slice(0, Idx);
   }
 
   if (!TuneFeatures || TFString.empty())
diff --git a/clang/test/Driver/riscv-mtune-tune-features.c b/clang/test/Driver/riscv-mtune-tune-features.c
index e31500cf9d20a..bb55443b51c8e 100644
--- a/clang/test/Driver/riscv-mtune-tune-features.c
+++ b/clang/test/Driver/riscv-mtune-tune-features.c
@@ -19,6 +19,11 @@
 // RUN:     FileCheck --check-prefix=NO-EXPERIMENTAL %s
 // NO-EXPERIMENTAL: invalid -mtune string 'sifive-x390:full-vec-fp64':
 // NO-EXPERIMENTAL-SAME: require '-mexperimental-mtune-syntax' to use with tune feature string
+//
+// RUN: not %clang --target=riscv64 -mtune=sifive-x390: -c %s 2>&1 | \
+// RUN:     FileCheck --check-prefix=NO-EXPERIMENTAL2 %s
+// NO-EXPERIMENTAL2: invalid -mtune string 'sifive-x390:':
+// NO-EXPERIMENTAL2-SAME: require '-mexperimental-mtune-syntax' to use with tune feature string
 
 // RUN: not %clang --target=riscv64 -mexperimental-mtune-syntax \
 // RUN:     -mtune=sifive-p470:full-vec-fp64 -c %s 2>&1 | \

>From 45164199aee0ff17557ae530744548141635b8ed Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 12 Aug 2026 13:31:16 -0700
Subject: [PATCH 2/3] [RISCV] Replace 'native' before applying features for
 -mtune=native:<feature_list>

We need to replace the CPU before calling parseTuneFeatureString or
it will always fails.
---
 clang/lib/Driver/ToolChains/Arch/RISCV.cpp | 3 +++
 clang/lib/Driver/ToolChains/Clang.cpp      | 9 +++------
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
index a1bf62f80589c..06dbb9d7ac0c4 100644
--- a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
@@ -425,6 +425,9 @@ riscv::getRISCVTuneCPU(const Driver &D, const llvm::opt::ArgList &Args,
     TuneCPU = TuneCPU.slice(0, Idx);
   }
 
+  if (TuneCPU == "native")
+    TuneCPU = llvm::sys::getHostCPUName();
+
   if (!TuneFeatures || TFString.empty())
     return TuneCPU;
   if (auto E = llvm::RISCV::parseTuneFeatureString(TuneCPU, TFString,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index e8760d0d7a3aa..ca89ef4a3109b 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -2034,12 +2034,9 @@ void Clang::AddRISCVTargetArgs(const ArgList &Args,
     return;
   if (!TuneCPU->empty()) {
     CmdArgs.push_back("-tune-cpu");
-    if (*TuneCPU == "native")
-      CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName()));
-    else
-      // TuneCPU might or might not be the original -mtune string, so we
-      // have to create a new copy here.
-      CmdArgs.push_back(Args.MakeArgString(*TuneCPU));
+    // TuneCPU might or might not be the original -mtune string, so we
+    // have to create a new copy here.
+    CmdArgs.push_back(Args.MakeArgString(*TuneCPU));
   }
 
   // Handle -mrvv-vector-bits=<bits>

>From b4d80952669daadabb8942111b41024d155404a7 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Fri, 14 Aug 2026 14:43:10 -0700
Subject: [PATCH 3/3] fixup! Move native after parseTuneFeatureString.

---
 clang/lib/Driver/ToolChains/Arch/RISCV.cpp    | 21 +++++++++++--------
 clang/test/Driver/riscv-mtune-tune-features.c |  6 ++++++
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
index 06dbb9d7ac0c4..f5315ebfc5f2c 100644
--- a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp
@@ -425,17 +425,20 @@ riscv::getRISCVTuneCPU(const Driver &D, const llvm::opt::ArgList &Args,
     TuneCPU = TuneCPU.slice(0, Idx);
   }
 
+  if (TuneFeatures && !TFString.empty()) {
+    if (auto E = llvm::RISCV::parseTuneFeatureString(TuneCPU, TFString,
+                                                     *TuneFeatures)) {
+      D.Diag(diag::err_drv_invalid_riscv_mtune_string)
+          << 1 << TFString << llvm::toString(std::move(E));
+      return std::nullopt;
+    }
+  }
+
+  // Apply -mtune=native after applying features. Not all features apply to
+  // all CPUs so an -mtune=native:<feature> may fail depending on what the
+  // native was expanded to.
   if (TuneCPU == "native")
     TuneCPU = llvm::sys::getHostCPUName();
 
-  if (!TuneFeatures || TFString.empty())
-    return TuneCPU;
-  if (auto E = llvm::RISCV::parseTuneFeatureString(TuneCPU, TFString,
-                                                   *TuneFeatures)) {
-    D.Diag(diag::err_drv_invalid_riscv_mtune_string)
-        << 1 << TFString << llvm::toString(std::move(E));
-    return std::nullopt;
-  }
-
   return TuneCPU;
 }
diff --git a/clang/test/Driver/riscv-mtune-tune-features.c b/clang/test/Driver/riscv-mtune-tune-features.c
index bb55443b51c8e..f674243097a8f 100644
--- a/clang/test/Driver/riscv-mtune-tune-features.c
+++ b/clang/test/Driver/riscv-mtune-tune-features.c
@@ -42,3 +42,9 @@
 // RUN:     FileCheck --check-prefix=UNSUPPORTED-DIRECTIVE %s
 // UNSUPPORTED-DIRECTIVE: invalid tune feature string 'prefer-w-inst':
 // UNSUPPORTED-DIRECTIVE-SAME: Directive 'prefer-w-inst' is not allowed to be used with processor 'sifive-x280'
+
+// RUN: not %clang --target=riscv64 -mexperimental-mtune-syntax \
+// RUN:     -mtune=native:full-vec-fp64 -c %s 2>&1 | \
+// RUN:     FileCheck --check-prefix=NO-DIRECTIVE-NATIVE %s
+// NO-DIRECTIVE-NATIVE: invalid tune feature string 'full-vec-fp64':
+// NO-DIRECTIVE-NATIVE-SAME: Processor 'native' has no configurable tuning features



More information about the cfe-commits mailing list