[clang] [clang] Enable --print-supported-extensions for all targets (PR #66586)

Balint Cristian via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 17 04:16:42 PDT 2023


https://github.com/cbalint13 updated https://github.com/llvm/llvm-project/pull/66586

>From 633e258d41bf15e77f592f4e95eda729e6da5a4b Mon Sep 17 00:00:00 2001
From: Balint Cristian <cristian.balint at gmail.com>
Date: Sun, 17 Sep 2023 14:16:13 +0300
Subject: [PATCH] [clang] Enable --print-supported-extensions for all targets

Signed-off-by: Balint Cristian <cristian.balint at gmail.com>
---
 clang/include/clang/Driver/Options.td         |  2 +-
 clang/lib/Driver/Driver.cpp                   | 14 +------
 clang/lib/Driver/ToolChains/Clang.cpp         |  7 ++++
 .../test/Driver/print-supported-extensions.c  | 18 +++++---
 clang/tools/driver/cc1_main.cpp               | 42 ++++++++++---------
 llvm/include/llvm/MC/MCSubtargetInfo.h        |  6 +++
 6 files changed, 51 insertions(+), 38 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 553c7928c4f949e..431f2f5c4109232 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5277,7 +5277,7 @@ def print_supported_cpus : Flag<["-", "--"], "print-supported-cpus">,
   MarshallingInfoFlag<FrontendOpts<"PrintSupportedCPUs">>;
 def print_supported_extensions : Flag<["-", "--"], "print-supported-extensions">,
   Visibility<[ClangOption, CC1Option, CLOption]>,
-  HelpText<"Print supported -march extensions (RISC-V, AArch64 and ARM only)">,
+  HelpText<"Print supported -march extensions">,
   MarshallingInfoFlag<FrontendOpts<"PrintSupportedExtensions">>;
 def : Flag<["-"], "mcpu=help">, Alias<print_supported_cpus>;
 def : Flag<["-"], "mtune=help">, Alias<print_supported_cpus>;
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 84b8fc7685fed42..5b6b14d175f5299 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4279,19 +4279,9 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
     // If --print-supported-cpus, -mcpu=? or -mtune=? is specified, build a
     // custom Compile phase that prints out supported cpu models and quits.
     //
-    // If --print-supported-extensions is specified, call the helper function
-    // RISCVMarchHelp in RISCVISAInfo.cpp that prints out supported extensions
-    // and quits.
+    // If --print-supported-extensions is specified, list all supported flags
+    // within the target and quit.
     if (Arg *A = Args.getLastArg(Opt)) {
-      if (Opt == options::OPT_print_supported_extensions &&
-          !C.getDefaultToolChain().getTriple().isRISCV() &&
-          !C.getDefaultToolChain().getTriple().isAArch64() &&
-          !C.getDefaultToolChain().getTriple().isARM()) {
-        C.getDriver().Diag(diag::err_opt_not_valid_on_target)
-            << "--print-supported-extensions";
-        return;
-      }
-
       // Use the -mcpu=? flag as the dummy input to cc1.
       Actions.clear();
       Action *InputAc = C.MakeAction<InputAction>(*A, types::TY_C);
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 40e60585a8b8d6e..ed756ed7ea785c5 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5733,6 +5733,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
     CmdArgs.push_back(Args.MakeArgString(CPU));
   }
 
+  // Track -mcpu presence
+  // it's absence marks -target-cpu generic
+  if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
+    CmdArgs.push_back("-mcpu");
+    CmdArgs.push_back(A->getValue());
+  }
+
   RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs);
 
   // Add clang-cl arguments.
diff --git a/clang/test/Driver/print-supported-extensions.c b/clang/test/Driver/print-supported-extensions.c
index 8daf4d8a34b8a60..d85d5bc50347144 100644
--- a/clang/test/Driver/print-supported-extensions.c
+++ b/clang/test/Driver/print-supported-extensions.c
@@ -3,16 +3,24 @@
 
 // RUN: %if aarch64-registered-target %{ %clang --target=aarch64-linux-gnu \
 // RUN:   --print-supported-extensions 2>&1 | FileCheck %s --check-prefix AARCH64 %}
-// AARCH64: All available -march extensions for AArch64
+// AARCH64: All available -march extensions for aarch64
+
+// RUN: %if aarch64-registered-target %{ %clang --target=aarch64-linux-gnu -mcpu=cortex-a55 \
+// RUN:   --print-supported-extensions 2>&1 | FileCheck %s --check-prefix AARCH64 %}
+// AARCH64: All available -march extensions for aarch64 and -mcpu cortex-a55
 
 // RUN: %if riscv-registered-target %{ %clang --target=riscv64-linux-gnu \
 // RUN:   --print-supported-extensions 2>&1 | FileCheck %s --check-prefix RISCV %}
-// RISCV: All available -march extensions for RISC-V
+// RISCV: All available -march extensions for riscv64
 
 // RUN: %if arm-registered-target %{ %clang --target=arm-linux-gnu \
 // RUN:   --print-supported-extensions 2>&1 | FileCheck %s --check-prefix ARM %}
-// ARM: All available -march extensions for ARM
+// ARM: All available -march extensions for arm
+
+// RUN: %if aarch64-registered-target %{ %clang --target=x86_64-linux-gnu \
+// RUN:   --print-supported-extensions 2>&1 | FileCheck %s --check-prefix X86 %}
+// X86: All available -march extensions for x86-64
 
-// RUN: %if x86-registered-target %{ not %clang --target=x86_64-linux-gnu \
+// RUN: %if aarch64-registered-target %{ %clang --target=x86_64-linux-gnu -mpcu=sandybridge \
 // RUN:   --print-supported-extensions 2>&1 | FileCheck %s --check-prefix X86 %}
-// X86: error: option '--print-supported-extensions' cannot be specified on this target
\ No newline at end of file
+// X86: All available -march extensions for x86-64 and -mcpu sandybridge
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index f0d7b5c3889dc1f..c6180e42e2554da 100644
--- a/clang/tools/driver/cc1_main.cpp
+++ b/clang/tools/driver/cc1_main.cpp
@@ -28,6 +28,7 @@
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Config/llvm-config.h"
 #include "llvm/LinkAllPasses.h"
+#include "llvm/MC/MCSubtargetInfo.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
@@ -38,15 +39,12 @@
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Process.h"
-#include "llvm/Support/RISCVISAInfo.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/TimeProfiler.h"
 #include "llvm/Support/Timer.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
-#include "llvm/TargetParser/AArch64TargetParser.h"
-#include "llvm/TargetParser/ARMTargetParser.h"
 #include <cstdio>
 
 #ifdef CLANG_HAVE_RLIMITS
@@ -185,7 +183,7 @@ static int PrintSupportedCPUs(std::string TargetStr) {
   return 0;
 }
 
-static int PrintSupportedExtensions(std::string TargetStr) {
+static int PrintSupportedExtensions(std::string TargetStr, std::string MCPU) {
   std::string Error;
   const llvm::Target *TheTarget =
       llvm::TargetRegistry::lookupTarget(TargetStr, Error);
@@ -196,20 +194,18 @@ static int PrintSupportedExtensions(std::string TargetStr) {
 
   llvm::TargetOptions Options;
   std::unique_ptr<llvm::TargetMachine> TheTargetMachine(
-      TheTarget->createTargetMachine(TargetStr, "", "", Options, std::nullopt));
-  const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple();
-
-  if (MachineTriple.isRISCV())
-    llvm::riscvExtensionsHelp();
-  else if (MachineTriple.isAArch64())
-    llvm::AArch64::PrintSupportedExtensions();
-  else if (MachineTriple.isARM())
-    llvm::ARM::PrintSupportedExtensions();
-  else {
-    // The option was already checked in Driver::HandleImmediateArgs,
-    // so we do not expect to get here if we are not a supported architecture.
-    assert(0 && "Unhandled triple for --print-supported-extensions option.");
-    return 1;
+      TheTarget->createTargetMachine(TargetStr, MCPU, "", Options, std::nullopt));
+  const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo();
+
+  llvm::ArrayRef<llvm::SubtargetFeatureKV> Features =
+    MCInfo->getAllProcessorFeatures();
+
+  llvm::outs() << "All available -march extensions for " << TheTarget->getName()
+    << (MCPU.empty() ? "" : (" and -mcpu " + MCPU).c_str()) << "\n\n";
+  for (const llvm::SubtargetFeatureKV &feature : Features) {
+    if (MCInfo->checkFeatures("+" + std::string(feature.Key)) || MCPU.empty()) {
+      llvm::outs() << llvm::format("  %-40s %s\n", feature.Key, feature.Desc);
+    }
   }
 
   return 0;
@@ -255,8 +251,14 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
     return PrintSupportedCPUs(Clang->getTargetOpts().Triple);
 
   // --print-supported-extensions takes priority over the actual compilation.
-  if (Clang->getFrontendOpts().PrintSupportedExtensions)
-    return PrintSupportedExtensions(Clang->getTargetOpts().Triple);
+  if (Clang->getFrontendOpts().PrintSupportedExtensions) {
+    std::string MCPU = "";
+    if (find(Argv, StringRef("-mcpu")) != Argv.end()) {
+      MCPU = find(Argv, StringRef("-mcpu"))[1];
+    }
+
+    return PrintSupportedExtensions(Clang->getTargetOpts().Triple, MCPU);
+  }
 
   // Infer the builtin include path if unspecified.
   if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
diff --git a/llvm/include/llvm/MC/MCSubtargetInfo.h b/llvm/include/llvm/MC/MCSubtargetInfo.h
index c1533ac8d0059f5..f172a799aa3331c 100644
--- a/llvm/include/llvm/MC/MCSubtargetInfo.h
+++ b/llvm/include/llvm/MC/MCSubtargetInfo.h
@@ -230,10 +230,16 @@ class MCSubtargetInfo {
     return Found != ProcDesc.end() && StringRef(Found->Key) == CPU;
   }
 
+  /// Return processor descriptions.
   ArrayRef<SubtargetSubTypeKV> getAllProcessorDescriptions() const {
     return ProcDesc;
   }
 
+  /// Return processor features.
+  ArrayRef<SubtargetFeatureKV> getAllProcessorFeatures() const {
+    return ProcFeatures;
+  }
+
   virtual unsigned getHwMode() const { return 0; }
 
   /// Return the cache size in bytes for the given level of cache.



More information about the cfe-commits mailing list