https://github.com/topperc created 
https://github.com/llvm/llvm-project/pull/243697

Clang has multiple -mtune only CPU names like -mtune=sifive-7-series. These 
should not be printed by -mcpu=help. There also RV32 and RV64 only CPUs.

Previously we used the -mcpu=help from LLVM's MC layer to print the names, but 
it doesn't have this granularity of information.

This patch special cases RISC-V in the clang driver to get the lists from 
RISCVTargetParser. A new -print-supported-tune-cpus option is added to request 
the tune CPU list. -mtune=help aliases this option.

The behavior for other targets is unchanged.

Assisted-by: Claude

>From 2761c9afe183387563a728e6b3c9b76cf3c44ee8 Mon Sep 17 00:00:00 2001
From: Craig Topper <[email protected]>
Date: Tue, 17 Nov 2026 18:01:42 -0800
Subject: [PATCH] [RISCV] Make clang's -mcpu=help and -mtune=help accurate for
 RISC-V.

Clang has multiple -mtune only CPU names like -mtune=sifive-7-series.
These should not be printed by -mcpu=help. There also RV32 and RV64
only CPUs.

Previously we used the -mcpu=help from LLVM's MC layer to print the
names, but it doesn't have this granularity of information.

This patch special cases RISC-V in the clang driver to get the lists
from RISCVTargetParser. A new -print-supported-tune-cpus option is
added to request the tune CPU list. -mtune=help aliases this option.

The behavior for other targets is unchanged.

Assisted-by: Claude
---
 .../include/clang/Frontend/FrontendOptions.h  |   4 +
 clang/include/clang/Options/Options.td        |   9 +-
 clang/lib/Driver/Driver.cpp                   |   4 +
 .../test/Driver/print-supported-cpus-riscv.c  | 169 ++++++++++++++++++
 clang/test/Driver/print-supported-cpus.c      |   3 +
 clang/tools/driver/cc1_main.cpp               |  31 +++-
 6 files changed, 217 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Driver/print-supported-cpus-riscv.c

diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 087a96a13af3f0..bf9647d9259340 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -302,6 +302,10 @@ class FrontendOptions {
   LLVM_PREFERRED_TYPE(bool)
   unsigned PrintSupportedCPUs : 1;
 
+  /// Print the supported tuning cpus for the current target.
+  LLVM_PREFERRED_TYPE(bool)
+  unsigned PrintSupportedTuneCPUs : 1;
+
   /// Print the supported extensions for the current target.
   LLVM_PREFERRED_TYPE(bool)
   unsigned PrintSupportedExtensions : 1;
diff --git a/clang/include/clang/Options/Options.td 
b/clang/include/clang/Options/Options.td
index 9156640ff67c79..d9f396593e31fd 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -7049,8 +7049,15 @@ def print_supported_cpus : Flag<["-", "--"], 
"print-supported-cpus">,
            "specified,it will print the supported cpus for the default 
target)">,
   MarshallingInfoFlag<FrontendOpts<"PrintSupportedCPUs">>;
 
+def print_supported_tune_cpus : Flag<["-", "--"], "print-supported-tune-cpus">,
+  Group<CompileOnly_Group>,
+  HelpText<"Print supported tuning cpu models for the given target (if target "
+           "is not specified,it will print the supported tuning cpus for the "
+           "default target)">,
+  MarshallingInfoFlag<FrontendOpts<"PrintSupportedTuneCPUs">>;
+
 def : Flag<["-"], "mcpu=help">, Alias<print_supported_cpus>;
-def : Flag<["-"], "mtune=help">, Alias<print_supported_cpus>;
+def : Flag<["-"], "mtune=help">, Alias<print_supported_tune_cpus>;
 
 } // let Visibility = [ClangOption, CC1Option, CLOption, FlangOption, 
FC1Option]
 
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 54daa1c4fb5c5d..0c4e96abbb5f43 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -379,6 +379,8 @@ phases::ID Driver::getFinalPhase(const ArgList &Args,
     // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
   } else if ((PhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
              (PhaseArg = Args.getLastArg(options::OPT_print_supported_cpus)) ||
+             (PhaseArg =
+                  Args.getLastArg(options::OPT_print_supported_tune_cpus)) ||
              (PhaseArg =
                   Args.getLastArg(options::OPT_print_enabled_extensions)) ||
              (PhaseArg = Args.getLastArg(options::OPT_module_file_info)) ||
@@ -2723,6 +2725,7 @@ bool Driver::HandleImmediateArgs(Compilation &C) {
   if (C.getArgs().hasArg(options::OPT_v) ||
       C.getArgs().hasArg(options::OPT__HASH_HASH_HASH) ||
       C.getArgs().hasArg(options::OPT_print_supported_cpus) ||
+      C.getArgs().hasArg(options::OPT_print_supported_tune_cpus) ||
       C.getArgs().hasArg(options::OPT_print_supported_extensions) ||
       C.getArgs().hasArg(options::OPT_print_enabled_extensions)) {
     PrintVersion(C, llvm::errs());
@@ -3856,6 +3859,7 @@ void Driver::BuildActions(Compilation &C, DerivedArgList 
&Args,
   }
 
   for (auto Opt : {options::OPT_print_supported_cpus,
+                   options::OPT_print_supported_tune_cpus,
                    options::OPT_print_supported_extensions,
                    options::OPT_print_enabled_extensions}) {
     // If --print-supported-cpus, -mcpu=? or -mtune=? is specified, build a
diff --git a/clang/test/Driver/print-supported-cpus-riscv.c 
b/clang/test/Driver/print-supported-cpus-riscv.c
new file mode 100644
index 00000000000000..d5fafa2a302b73
--- /dev/null
+++ b/clang/test/Driver/print-supported-cpus-riscv.c
@@ -0,0 +1,169 @@
+// Test that -mcpu=help and -mtune=help list different names for RISC-V.
+
+// REQUIRES: riscv-registered-target
+
+// RUN: %clang --target=riscv32-unknown-linux-gnu --print-supported-cpus 2>&1 
| \
+// RUN:   FileCheck %s --check-prefix=CHECK-RV32-CPU
+// RUN: %clang --target=riscv32-unknown-linux-gnu -mcpu=help 2>&1 | \
+// RUN:   FileCheck %s --check-prefix=CHECK-RV32-CPU
+
+// RUN: %clang --target=riscv32-unknown-linux-gnu --print-supported-tune-cpus 
2>&1 | \
+// RUN:   FileCheck %s --check-prefix=CHECK-RV32-TUNE
+// RUN: %clang --target=riscv32-unknown-linux-gnu -mtune=help 2>&1 | \
+// RUN:   FileCheck %s --check-prefix=CHECK-RV32-TUNE
+
+// RUN: %clang --target=riscv64-unknown-linux-gnu --print-supported-cpus 2>&1 
| \
+// RUN:   FileCheck %s --check-prefix=CHECK-RV64-CPU
+// RUN: %clang --target=riscv64-unknown-linux-gnu -mcpu=help 2>&1 | \
+// RUN:   FileCheck %s --check-prefix=CHECK-RV64-CPU
+
+// RUN: %clang --target=riscv64-unknown-linux-gnu --print-supported-tune-cpus 
2>&1 | \
+// RUN:   FileCheck %s --check-prefix=CHECK-RV64-TUNE
+// RUN: %clang --target=riscv64-unknown-linux-gnu -mtune=help 2>&1 | \
+// RUN:   FileCheck %s --check-prefix=CHECK-RV64-TUNE
+
+// CHECK-RV32-CPU: Target: riscv32-unknown-linux-gnu
+// CHECK-RV32-CPU: Available CPUs for this target:
+// CHECK-RV32-CPU-EMPTY:
+// CHECK-RV32-CPU-NEXT: andes-a25
+// CHECK-RV32-CPU-NEXT: andes-a45
+// CHECK-RV32-CPU-NEXT: andes-n45
+// CHECK-RV32-CPU-NEXT: chipsalliance-veer-eh1
+// CHECK-RV32-CPU-NEXT: generic-rv32
+// CHECK-RV32-CPU-NEXT: rocket-rv32
+// CHECK-RV32-CPU-NEXT: rp2350-hazard3
+// CHECK-RV32-CPU-NEXT: sifive-e20
+// CHECK-RV32-CPU-NEXT: sifive-e21
+// CHECK-RV32-CPU-NEXT: sifive-e24
+// CHECK-RV32-CPU-NEXT: sifive-e31
+// CHECK-RV32-CPU-NEXT: sifive-e34
+// CHECK-RV32-CPU-NEXT: sifive-e76
+// CHECK-RV32-CPU-NEXT: sifive-x160
+// CHECK-RV32-CPU-NEXT: syntacore-scr1-base
+// CHECK-RV32-CPU-NEXT: syntacore-scr1-max
+// CHECK-RV32-CPU-NEXT: syntacore-scr3-rv32
+// CHECK-RV32-CPU-NEXT: syntacore-scr4-rv32
+// CHECK-RV32-CPU-NEXT: syntacore-scr5-rv32
+// CHECK-RV32-CPU-EMPTY:
+// CHECK-RV32-CPU-NEXT: Use -mcpu or -mtune to specify the target's processor.
+
+// CHECK-RV32-TUNE: Target: riscv32-unknown-linux-gnu
+// CHECK-RV32-TUNE: Available tuning CPUs for this target:
+// CHECK-RV32-TUNE-EMPTY:
+// CHECK-RV32-TUNE-NEXT: andes-a25
+// CHECK-RV32-TUNE-NEXT: andes-a45
+// CHECK-RV32-TUNE-NEXT: andes-n45
+// CHECK-RV32-TUNE-NEXT: chipsalliance-veer-eh1
+// CHECK-RV32-TUNE-NEXT: generic-rv32
+// CHECK-RV32-TUNE-NEXT: rocket-rv32
+// CHECK-RV32-TUNE-NEXT: rp2350-hazard3
+// CHECK-RV32-TUNE-NEXT: sifive-e20
+// CHECK-RV32-TUNE-NEXT: sifive-e21
+// CHECK-RV32-TUNE-NEXT: sifive-e24
+// CHECK-RV32-TUNE-NEXT: sifive-e31
+// CHECK-RV32-TUNE-NEXT: sifive-e34
+// CHECK-RV32-TUNE-NEXT: sifive-e76
+// CHECK-RV32-TUNE-NEXT: sifive-x160
+// CHECK-RV32-TUNE-NEXT: syntacore-scr1-base
+// CHECK-RV32-TUNE-NEXT: syntacore-scr1-max
+// CHECK-RV32-TUNE-NEXT: syntacore-scr3-rv32
+// CHECK-RV32-TUNE-NEXT: syntacore-scr4-rv32
+// CHECK-RV32-TUNE-NEXT: syntacore-scr5-rv32
+// CHECK-RV32-TUNE-NEXT: andes-45-series
+// CHECK-RV32-TUNE-NEXT: generic
+// CHECK-RV32-TUNE-NEXT: generic-ooo
+// CHECK-RV32-TUNE-NEXT: rocket
+// CHECK-RV32-TUNE-NEXT: sifive-7-series
+// CHECK-RV32-TUNE-EMPTY:
+// CHECK-RV32-TUNE-NEXT: Use -mtune to specify the target's tuning processor.
+
+// CHECK-RV64-CPU: Target: riscv64-unknown-linux-gnu
+// CHECK-RV64-CPU: Available CPUs for this target:
+// CHECK-RV64-CPU-EMPTY:
+// CHECK-RV64-CPU-NEXT: an-erbium
+// CHECK-RV64-CPU-NEXT: andes-ax25
+// CHECK-RV64-CPU-NEXT: andes-ax45
+// CHECK-RV64-CPU-NEXT: andes-ax45mpv
+// CHECK-RV64-CPU-NEXT: andes-nx45
+// CHECK-RV64-CPU-NEXT: et-soc1
+// CHECK-RV64-CPU-NEXT: gaisler-gr765
+// CHECK-RV64-CPU-NEXT: generic-rv64
+// CHECK-RV64-CPU-NEXT: mips-p8700
+// CHECK-RV64-CPU-NEXT: rocket-rv64
+// CHECK-RV64-CPU-NEXT: sifive-p450
+// CHECK-RV64-CPU-NEXT: sifive-p470
+// CHECK-RV64-CPU-NEXT: sifive-p550
+// CHECK-RV64-CPU-NEXT: sifive-p670
+// CHECK-RV64-CPU-NEXT: sifive-p870-d
+// CHECK-RV64-CPU-NEXT: sifive-s21
+// CHECK-RV64-CPU-NEXT: sifive-s51
+// CHECK-RV64-CPU-NEXT: sifive-s54
+// CHECK-RV64-CPU-NEXT: sifive-s76
+// CHECK-RV64-CPU-NEXT: sifive-u54
+// CHECK-RV64-CPU-NEXT: sifive-u74
+// CHECK-RV64-CPU-NEXT: sifive-x180
+// CHECK-RV64-CPU-NEXT: sifive-x280
+// CHECK-RV64-CPU-NEXT: sifive-x390
+// CHECK-RV64-CPU-NEXT: spacemit-a100
+// CHECK-RV64-CPU-NEXT: spacemit-x60
+// CHECK-RV64-CPU-NEXT: spacemit-x100
+// CHECK-RV64-CPU-NEXT: syntacore-scr3-rv64
+// CHECK-RV64-CPU-NEXT: syntacore-scr4-rv64
+// CHECK-RV64-CPU-NEXT: syntacore-scr5-rv64
+// CHECK-RV64-CPU-NEXT: syntacore-scr7
+// CHECK-RV64-CPU-NEXT: tt-ascalon-x
+// CHECK-RV64-CPU-NEXT: tt-ascalon-xg
+// CHECK-RV64-CPU-NEXT: xiangshan-kunminghu
+// CHECK-RV64-CPU-NEXT: xiangshan-nanhu
+// CHECK-RV64-CPU-NEXT: xt-c910v2
+// CHECK-RV64-CPU-NEXT: xt-c920v2
+// CHECK-RV64-CPU-EMPTY:
+// CHECK-RV64-CPU-NEXT: Use -mcpu or -mtune to specify the target's processor.
+
+// CHECK-RV64-TUNE: Target: riscv64-unknown-linux-gnu
+// CHECK-RV64-TUNE: Available tuning CPUs for this target:
+// CHECK-RV64-TUNE-EMPTY:
+// CHECK-RV64-TUNE-NEXT: an-erbium
+// CHECK-RV64-TUNE-NEXT: andes-ax25
+// CHECK-RV64-TUNE-NEXT: andes-ax45
+// CHECK-RV64-TUNE-NEXT: andes-ax45mpv
+// CHECK-RV64-TUNE-NEXT: andes-nx45
+// CHECK-RV64-TUNE-NEXT: et-soc1
+// CHECK-RV64-TUNE-NEXT: gaisler-gr765
+// CHECK-RV64-TUNE-NEXT: generic-rv64
+// CHECK-RV64-TUNE-NEXT: mips-p8700
+// CHECK-RV64-TUNE-NEXT: rocket-rv64
+// CHECK-RV64-TUNE-NEXT: sifive-p450
+// CHECK-RV64-TUNE-NEXT: sifive-p470
+// CHECK-RV64-TUNE-NEXT: sifive-p550
+// CHECK-RV64-TUNE-NEXT: sifive-p670
+// CHECK-RV64-TUNE-NEXT: sifive-p870-d
+// CHECK-RV64-TUNE-NEXT: sifive-s21
+// CHECK-RV64-TUNE-NEXT: sifive-s51
+// CHECK-RV64-TUNE-NEXT: sifive-s54
+// CHECK-RV64-TUNE-NEXT: sifive-s76
+// CHECK-RV64-TUNE-NEXT: sifive-u54
+// CHECK-RV64-TUNE-NEXT: sifive-u74
+// CHECK-RV64-TUNE-NEXT: sifive-x180
+// CHECK-RV64-TUNE-NEXT: sifive-x280
+// CHECK-RV64-TUNE-NEXT: sifive-x390
+// CHECK-RV64-TUNE-NEXT: spacemit-a100
+// CHECK-RV64-TUNE-NEXT: spacemit-x60
+// CHECK-RV64-TUNE-NEXT: spacemit-x100
+// CHECK-RV64-TUNE-NEXT: syntacore-scr3-rv64
+// CHECK-RV64-TUNE-NEXT: syntacore-scr4-rv64
+// CHECK-RV64-TUNE-NEXT: syntacore-scr5-rv64
+// CHECK-RV64-TUNE-NEXT: syntacore-scr7
+// CHECK-RV64-TUNE-NEXT: tt-ascalon-x
+// CHECK-RV64-TUNE-NEXT: tt-ascalon-xg
+// CHECK-RV64-TUNE-NEXT: xiangshan-kunminghu
+// CHECK-RV64-TUNE-NEXT: xiangshan-nanhu
+// CHECK-RV64-TUNE-NEXT: xt-c910v2
+// CHECK-RV64-TUNE-NEXT: xt-c920v2
+// CHECK-RV64-TUNE-NEXT: andes-45-series
+// CHECK-RV64-TUNE-NEXT: generic
+// CHECK-RV64-TUNE-NEXT: generic-ooo
+// CHECK-RV64-TUNE-NEXT: rocket
+// CHECK-RV64-TUNE-NEXT: sifive-7-series
+// CHECK-RV64-TUNE-EMPTY:
+// CHECK-RV64-TUNE-NEXT: Use -mtune to specify the target's tuning processor.
diff --git a/clang/test/Driver/print-supported-cpus.c 
b/clang/test/Driver/print-supported-cpus.c
index 762c8b5bac1431..47e2015015fc24 100644
--- a/clang/test/Driver/print-supported-cpus.c
+++ b/clang/test/Driver/print-supported-cpus.c
@@ -13,6 +13,9 @@
 // RUN: %clang --target=x86_64-unknown-linux-gnu -mtune=help -fuse-ld=dummy 
2>&1 | \
 // RUN:   FileCheck %s --check-prefix=CHECK-X86
 
+// RUN: %clang --target=x86_64-unknown-linux-gnu --print-supported-tune-cpus 
2>&1 | \
+// RUN:   FileCheck %s --check-prefix=CHECK-X86
+
 // CHECK-NOT: warning: argument unused during compilation
 // CHECK-X86: Target: x86_64-unknown-linux-gnu
 // CHECK-X86: corei7
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index c49648cbf969d8..a038cc46348b11 100644
--- a/clang/tools/driver/cc1_main.cpp
+++ b/clang/tools/driver/cc1_main.cpp
@@ -53,6 +53,7 @@
 #include "llvm/TargetParser/AArch64TargetParser.h"
 #include "llvm/TargetParser/ARMTargetParser.h"
 #include "llvm/TargetParser/RISCVISAInfo.h"
+#include "llvm/TargetParser/RISCVTargetParser.h"
 #include <cstdio>
 
 #ifdef CLANG_HAVE_RLIMITS
@@ -111,9 +112,31 @@ static void ensureSufficientStack() {
 static void ensureSufficientStack() {}
 #endif
 
-/// Print supported cpus of the given target.
-static int PrintSupportedCPUs(std::string TargetStr) {
+/// Print supported cpus (or tuning cpus if Tune is set) of the given target.
+static int PrintSupportedCPUs(std::string TargetStr, bool Tune = false) {
   llvm::Triple Triple(TargetStr);
+
+  // RISC-V has different lists of names for -mcpu/-mtune and RV32/RV64.
+  if (Triple.isRISCV()) {
+    llvm::SmallVector<llvm::StringRef, 64> Values;
+    if (Tune)
+      llvm::RISCV::fillValidTuneCPUArchList(Values, Triple.isRISCV64());
+    else
+      llvm::RISCV::fillValidCPUArchList(Values, Triple.isRISCV64());
+
+    llvm::errs() << (Tune ? "Available tuning CPUs for this target:\n\n"
+                          : "Available CPUs for this target:\n\n");
+    for (llvm::StringRef Name : Values)
+      llvm::errs() << "\t" << Name << "\n";
+    llvm::errs() << "\n";
+    if (Tune)
+      llvm::errs() << "Use -mtune to specify the target's tuning processor.\n";
+    else
+      llvm::errs() << "Use -mcpu or -mtune to specify the target's "
+                      "processor.\n";
+    return 0;
+  }
+
   std::string Error;
   const llvm::Target *TheTarget =
       llvm::TargetRegistry::lookupTarget(Triple, Error);
@@ -264,6 +287,10 @@ int cc1_main(ArrayRef<const char *> Argv, const char 
*Argv0, void *MainAddr) {
   if (Clang->getFrontendOpts().PrintSupportedCPUs)
     return PrintSupportedCPUs(Clang->getTargetOpts().Triple);
 
+  // --print-supported-tune-cpus takes priority over the actual compilation.
+  if (Clang->getFrontendOpts().PrintSupportedTuneCPUs)
+    return PrintSupportedCPUs(Clang->getTargetOpts().Triple, /*Tune=*/true);
+
   // --print-supported-extensions takes priority over the actual compilation.
   if (Clang->getFrontendOpts().PrintSupportedExtensions)
     return PrintSupportedExtensions(Clang->getTargetOpts().Triple);

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to