diff options
author | Lucas Duarte Prates <lucas.prates@arm.com> | 2024-06-28 09:20:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-28 09:20:16 +0100 |
commit | bb83a3df25a8c46dd586caf26635f5658e7b3316 (patch) | |
tree | 9aa15e04ef8333b210456db1bd8e50b1a0ebb47b /clang/tools | |
parent | 23413169299b872a9d0a9b947c2d27172b5f4cb0 (diff) | |
download | llvm-bb83a3df25a8c46dd586caf26635f5658e7b3316.zip llvm-bb83a3df25a8c46dd586caf26635f5658e7b3316.tar.gz llvm-bb83a3df25a8c46dd586caf26635f5658e7b3316.tar.bz2 |
Re-land: "[AArch64] Add ability to list extensions enabled for a target" (#95805) (#96795)
This introduces the new `--print-enabled-extensions` command line option
to AArch64, which prints the list of extensions that are enabled for the
target specified by the combination of `--target`/`-march`/`-mcpu`
values.
The goal of the this option is both to enable the manual inspection of
the enabled extensions by users and to enhance the testability of
architecture versions and CPU targets implemented in the compiler.
As part of this change, a new field for `FEAT_*` architecture feature
names was added to the TableGen entries. The output of the existing
`--print-supported-extensions` option was updated accordingly to show
these in a separate column.
Diffstat (limited to 'clang/tools')
-rw-r--r-- | clang/tools/driver/cc1_main.cpp | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp index 2aebc6d..3c0599c 100644 --- a/clang/tools/driver/cc1_main.cpp +++ b/clang/tools/driver/cc1_main.cpp @@ -26,6 +26,7 @@ #include "clang/Frontend/Utils.h" #include "clang/FrontendTool/Utils.h" #include "llvm/ADT/Statistic.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Config/llvm-config.h" #include "llvm/LinkAllPasses.h" #include "llvm/MC/MCSubtargetInfo.h" @@ -148,7 +149,7 @@ static int PrintSupportedExtensions(std::string TargetStr) { if (MachineTriple.isRISCV()) llvm::riscvExtensionsHelp(DescMap); else if (MachineTriple.isAArch64()) - llvm::AArch64::PrintSupportedExtensions(DescMap); + llvm::AArch64::PrintSupportedExtensions(); else if (MachineTriple.isARM()) llvm::ARM::PrintSupportedExtensions(DescMap); else { @@ -161,6 +162,45 @@ static int PrintSupportedExtensions(std::string TargetStr) { return 0; } +static int PrintEnabledExtensions(const TargetOptions& TargetOpts) { + std::string Error; + const llvm::Target *TheTarget = + llvm::TargetRegistry::lookupTarget(TargetOpts.Triple, Error); + if (!TheTarget) { + llvm::errs() << Error; + return 1; + } + + // Create a target machine using the input features, the triple information + // and a dummy instance of llvm::TargetOptions. Note that this is _not_ the + // same as the `clang::TargetOptions` instance we have access to here. + llvm::TargetOptions BackendOptions; + std::string FeaturesStr = llvm::join(TargetOpts.FeaturesAsWritten, ","); + std::unique_ptr<llvm::TargetMachine> TheTargetMachine( + TheTarget->createTargetMachine(TargetOpts.Triple, TargetOpts.CPU, FeaturesStr, BackendOptions, std::nullopt)); + const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple(); + const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo(); + + // Extract the feature names that are enabled for the given target. + // We do that by capturing the key from the set of SubtargetFeatureKV entries + // provided by MCSubtargetInfo, which match the '-target-feature' values. + const std::vector<llvm::SubtargetFeatureKV> Features = + MCInfo->getEnabledProcessorFeatures(); + std::set<llvm::StringRef> EnabledFeatureNames; + for (const llvm::SubtargetFeatureKV &feature : Features) + EnabledFeatureNames.insert(feature.Key); + + if (!MachineTriple.isAArch64()) { + // 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-enabled-extensions option."); + return 1; + } + llvm::AArch64::printEnabledExtensions(EnabledFeatureNames); + + return 0; +} + int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { ensureSufficientStack(); @@ -204,6 +244,10 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { if (Clang->getFrontendOpts().PrintSupportedExtensions) return PrintSupportedExtensions(Clang->getTargetOpts().Triple); + // --print-enabled-extensions takes priority over the actual compilation. + if (Clang->getFrontendOpts().PrintEnabledExtensions) + return PrintEnabledExtensions(Clang->getTargetOpts()); + // Infer the builtin include path if unspecified. if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && Clang->getHeaderSearchOpts().ResourceDir.empty()) |