From af857b93df36a983a27a82455c0dea39f53da5b1 Mon Sep 17 00:00:00 2001 From: Ziang Wan Date: Fri, 14 Jun 2019 21:42:21 +0000 Subject: [PATCH] Add --print-supported-cpus flag for clang. This patch allows clang users to print out a list of supported CPU models using clang [--target=] --print-supported-cpus Then, users can select the CPU model to compile to using clang --target= -mcpu= a.c It is a handy feature to help cross compilation. llvm-svn: 363464 --- clang/docs/ClangCommandLineReference.rst | 4 +++ clang/docs/CommandGuide/clang.rst | 6 ++++ clang/include/clang/Driver/Options.td | 4 +++ .../include/clang/Frontend/FrontendOptions.h | 3 ++ clang/lib/Driver/Driver.cpp | 14 +++++++- clang/lib/Frontend/CompilerInvocation.cpp | 1 + clang/test/Driver/print-supported-cpus.c | 16 +++++++++ clang/tools/driver/cc1_main.cpp | 25 +++++++++++++ llvm/lib/MC/MCSubtargetInfo.cpp | 35 ++++++++++++++++++- 9 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 clang/test/Driver/print-supported-cpus.c diff --git a/clang/docs/ClangCommandLineReference.rst b/clang/docs/ClangCommandLineReference.rst index 30ac27f95f9d..bad91ff830d2 100644 --- a/clang/docs/ClangCommandLineReference.rst +++ b/clang/docs/ClangCommandLineReference.rst @@ -610,6 +610,10 @@ C++ standard library to use Generate code for the given target +.. option:: --print-supported-cpus + +Print supported cpu models for the given target + .. option:: -time Time individual commands diff --git a/clang/docs/CommandGuide/clang.rst b/clang/docs/CommandGuide/clang.rst index 84e0dddb8e1f..1ed11c77ad06 100644 --- a/clang/docs/CommandGuide/clang.rst +++ b/clang/docs/CommandGuide/clang.rst @@ -324,6 +324,12 @@ number of cross compilers, or may only support a native target. When building for iPhone OS, specify the minimum version supported by your application. +.. option:: --print-supported-cpus + + Print out a list of supported processors for the given target (specified + through --target= or -arch ). If no target is + specified, the system default target will be used. + .. option:: -march= Specify that Clang should generate code for a specific processor family diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 65c1721daa8b..07d1c220c795 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2632,6 +2632,10 @@ def : Separate<["--"], "no-system-header-prefix">, Alias, Group; def target : Joined<["--"], "target=">, Flags<[DriverOption, CoreOption]>, HelpText<"Generate code for the given target">; +def _print_supported_cpus : Flag<["-", "--"], "print-supported-cpus">, + Group, Flags<[CC1Option, CoreOption]>, + HelpText<"Print supported cpu models for the given target (if target is not specified," + " it will print the supported cpus for the default target)">; def gcc_toolchain : Joined<["--"], "gcc-toolchain=">, Flags<[DriverOption]>, HelpText<"Use the gcc toolchain at the given directory">; def time : Flag<["-"], "time">, diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index 1bbd04896748..66fbe6ae6ca7 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -260,6 +260,9 @@ public: /// Show timers for individual actions. unsigned ShowTimers : 1; + /// print the supported cpus for the current target + unsigned PrintSupportedCPUs : 1; + /// Output time trace profile. unsigned TimeTrace : 1; diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 17552b28763a..3bda71597e3a 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -1671,7 +1671,8 @@ bool Driver::HandleImmediateArgs(const Compilation &C) { } if (C.getArgs().hasArg(options::OPT_v) || - C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { + C.getArgs().hasArg(options::OPT__HASH_HASH_HASH) || + C.getArgs().hasArg(options::OPT__print_supported_cpus)) { PrintVersion(C, llvm::errs()); SuppressMissingInputWarning = true; } @@ -3375,6 +3376,17 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args, Args.ClaimAllArgs(options::OPT_cl_compile_Group); } + // If the use specify --print-supported-cpus, clang will only print out + // supported cpu names without doing compilation. + if (Arg *A = Args.getLastArg(options::OPT__print_supported_cpus)) { + Actions.clear(); + // the compilation now has only two phases: Input and Compile + // use the --prints-supported-cpus flag as the dummy input to cc1 + Action *InputAc = C.MakeAction(*A, types::TY_C); + Actions.push_back( + C.MakeAction(InputAc, types::TY_Nothing)); + } + // Claim ignored clang-cl options. Args.ClaimAllArgs(options::OPT_cl_ignored_Group); diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 5596e4b6a3c8..7ba08c6acad0 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1752,6 +1752,7 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, Opts.ShowHelp = Args.hasArg(OPT_help); Opts.ShowStats = Args.hasArg(OPT_print_stats); Opts.ShowTimers = Args.hasArg(OPT_ftime_report); + Opts.PrintSupportedCPUs = Args.hasArg(OPT__print_supported_cpus); Opts.TimeTrace = Args.hasArg(OPT_ftime_trace); Opts.ShowVersion = Args.hasArg(OPT_version); Opts.ASTMergeFiles = Args.getAllArgValues(OPT_ast_merge); diff --git a/clang/test/Driver/print-supported-cpus.c b/clang/test/Driver/print-supported-cpus.c new file mode 100644 index 000000000000..a6b9a71c6277 --- /dev/null +++ b/clang/test/Driver/print-supported-cpus.c @@ -0,0 +1,16 @@ +// Test that the --print-supported-cpus flag works + +// RUN: %clang --target=x86_64-unknown-linux-gnu \ +// RUN: --print-supported-cpus 2>&1 \ +// RUN: | FileCheck %s --check-prefix=CHECK-X86 +// CHECK-X86: Target: x86_64-unknown-linux-gnu +// CHECK-X86: corei7 +// CHECK-X86: Use -mcpu or -mtune to specify the target's processor. + +// RUN: %clang --target=arm-unknown-linux-android \ +// RUN: --print-supported-cpus 2>&1 \ +// RUN: | FileCheck %s --check-prefix=CHECK-ARM +// CHECK-ARM: Target: arm-unknown-linux-android +// CHECK-ARM: cortex-a73 +// CHECK-ARM: cortex-a75 +// CHECK-ARM: Use -mcpu or -mtune to specify the target's processor. diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp index 2ed27c22708a..01a424893333 100644 --- a/clang/tools/driver/cc1_main.cpp +++ b/clang/tools/driver/cc1_main.cpp @@ -13,6 +13,7 @@ //===----------------------------------------------------------------------===// #include "clang/Basic/Stack.h" +#include "clang/Basic/TargetOptions.h" #include "clang/CodeGen/ObjectFilePCHContainerOperations.h" #include "clang/Config/config.h" #include "clang/Driver/DriverDiagnostic.h" @@ -36,10 +37,12 @@ #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/Path.h" #include "llvm/Support/Signals.h" +#include "llvm/Support/TargetRegistry.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 #ifdef CLANG_HAVE_RLIMITS @@ -166,6 +169,23 @@ static void ensureSufficientStack() { static void ensureSufficientStack() {} #endif +/// print supported cpus of the given target +int PrintSupportedCPUs(std::string TargetStr) { + std::string Error; + const llvm::Target *TheTarget = + llvm::TargetRegistry::lookupTarget(TargetStr, Error); + if (!TheTarget) { + llvm::errs() << Error; + return 1; + } + + // the target machine will handle the mcpu printing + llvm::TargetOptions Options; + std::unique_ptr TheTargetMachine( + TheTarget->createTargetMachine(TargetStr, "", "+cpuHelp", Options, None)); + return 0; +} + int cc1_main(ArrayRef Argv, const char *Argv0, void *MainAddr) { ensureSufficientStack(); @@ -199,6 +219,11 @@ int cc1_main(ArrayRef Argv, const char *Argv0, void *MainAddr) { if (Clang->getFrontendOpts().TimeTrace) llvm::timeTraceProfilerInitialize(); + // --print-supported-cpus takes priority over the actual compilation + if (Clang->getFrontendOpts().PrintSupportedCPUs) { + return PrintSupportedCPUs(Clang->getTargetOpts().Triple); + } + // Infer the builtin include path if unspecified. if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && Clang->getHeaderSearchOpts().ResourceDir.empty()) diff --git a/llvm/lib/MC/MCSubtargetInfo.cpp b/llvm/lib/MC/MCSubtargetInfo.cpp index 9b73800978cc..9b0272cab0bd 100644 --- a/llvm/lib/MC/MCSubtargetInfo.cpp +++ b/llvm/lib/MC/MCSubtargetInfo.cpp @@ -92,9 +92,16 @@ static size_t getLongestEntryLength(ArrayRef Table) { return MaxLen; } -/// Display help for feature choices. +/// Display help for feature and mcpu choices. static void Help(ArrayRef CPUTable, ArrayRef FeatTable) { + // the static variable ensures that the help information only gets + // printed once even though a target machine creates multiple subtargets + static bool PrintOnce = false; + if (PrintOnce) { + return; + } + // Determine the length of the longest CPU and Feature entries. unsigned MaxCPULen = getLongestEntryLength(CPUTable); unsigned MaxFeatLen = getLongestEntryLength(FeatTable); @@ -114,6 +121,30 @@ static void Help(ArrayRef CPUTable, errs() << "Use +feature to enable a feature, or -feature to disable it.\n" "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n"; + + PrintOnce = true; +} + +/// Display help for mcpu choices only +static void cpuHelp(ArrayRef CPUTable) { + // the static variable ensures that the help information only gets + // printed once even though a target machine creates multiple subtargets + static bool PrintOnce = false; + if (PrintOnce) { + return; + } + + // Print the CPU table. + errs() << "Available CPUs for this target:\n\n"; + for (auto &CPU : CPUTable) + errs() << "\t" << CPU.Key << "\n"; + errs() << '\n'; + + errs() << "Use -mcpu or -mtune to specify the target's processor.\n" + "For example, clang --target=aarch64-unknown-linux-gui " + "-mcpu=cortex-a35\n"; + + PrintOnce = true; } static FeatureBitset getFeatures(StringRef CPU, StringRef FS, @@ -154,6 +185,8 @@ static FeatureBitset getFeatures(StringRef CPU, StringRef FS, // Check for help if (Feature == "+help") Help(ProcDesc, ProcFeatures); + else if (Feature == "+cpuHelp") + cpuHelp(ProcDesc); else ApplyFeatureFlag(Bits, Feature, ProcFeatures); }