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

Balint Cristian via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 17 06:52:39 PDT 2023


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

>From 689b322408caff8340ffef4dd526d3640958ea03 Mon Sep 17 00:00:00 2001
From: Balint Cristian <cristian.balint at gmail.com>
Date: Sun, 17 Sep 2023 16:52:15 +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 ++---------
 .../test/Driver/print-supported-extensions.c  | 10 ++++----
 clang/tools/driver/cc1_main.cpp               | 25 +++++++------------
 llvm/include/llvm/MC/MCSubtargetInfo.h        |  6 +++++
 5 files changed, 23 insertions(+), 34 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/test/Driver/print-supported-extensions.c b/clang/test/Driver/print-supported-extensions.c
index 8daf4d8a34b8a60..d233e9fb783e2e2 100644
--- a/clang/test/Driver/print-supported-extensions.c
+++ b/clang/test/Driver/print-supported-extensions.c
@@ -3,16 +3,16 @@
 
 // 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 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 x86-registered-target %{ not %clang --target=x86_64-linux-gnu \
+// RUN: %if x86-registered-target %{ %clang --target=x86_64-linux-gnu \
 // 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
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index f0d7b5c3889dc1f..eabc2beaf04f2fb 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
@@ -197,19 +195,14 @@ 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;
+  const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo();
+
+  llvm::ArrayRef<llvm::SubtargetFeatureKV> Features =
+      MCInfo->getAllProcessorFeatures();
+  llvm::outs() << "All available -march extensions for " << TheTarget->getName()
+         << "\n\n";
+  for (const llvm::SubtargetFeatureKV &feature : Features) {
+    llvm::outs() << llvm::format("  %-40s %s\n", feature.Key, feature.Desc);
   }
 
   return 0;
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 llvm-commits mailing list