https://github.com/cbalint13 updated https://github.com/llvm/llvm-project/pull/66715
>From 231c93881e7719b447db3f15a37ad6cecede3be3 Mon Sep 17 00:00:00 2001 From: Balint Cristian <cristian.bal...@gmail.com> Date: Tue, 19 Sep 2023 02:59:40 +0300 Subject: [PATCH] [clang] Enable descriptions for --print-supported-extensions --- clang/tools/driver/cc1_main.cpp | 15 ++++++++++--- llvm/include/llvm/MC/MCSubtargetInfo.h | 7 +++++++ llvm/include/llvm/Support/RISCVISAInfo.h | 2 +- .../llvm/TargetParser/AArch64TargetParser.h | 3 ++- .../llvm/TargetParser/ARMTargetParser.h | 3 ++- llvm/lib/Support/RISCVISAInfo.cpp | 21 +++++++++++++------ llvm/lib/TargetParser/AArch64TargetParser.cpp | 16 ++++++++++---- llvm/lib/TargetParser/ARMTargetParser.cpp | 15 +++++++++---- llvm/unittests/Support/RISCVISAInfoTest.cpp | 4 +++- .../TargetParser/TargetParserTest.cpp | 7 +++++-- 10 files changed, 70 insertions(+), 23 deletions(-) diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp index f0d7b5c3889dc1f..a963324705db92d 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" @@ -198,13 +199,21 @@ static int PrintSupportedExtensions(std::string TargetStr) { std::unique_ptr<llvm::TargetMachine> TheTargetMachine( TheTarget->createTargetMachine(TargetStr, "", "", Options, std::nullopt)); const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple(); + const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo(); + const llvm::ArrayRef<llvm::SubtargetFeatureKV> Features = + MCInfo->getAllProcessorFeatures(); + + std::map<StringRef, StringRef> llvmDescMap; + for (const llvm::SubtargetFeatureKV &feature : Features) { + llvmDescMap.insert(std::make_pair(feature.Key, feature.Desc)); + } if (MachineTriple.isRISCV()) - llvm::riscvExtensionsHelp(); + llvm::riscvExtensionsHelp(llvmDescMap); else if (MachineTriple.isAArch64()) - llvm::AArch64::PrintSupportedExtensions(); + llvm::AArch64::PrintSupportedExtensions(llvmDescMap); else if (MachineTriple.isARM()) - llvm::ARM::PrintSupportedExtensions(); + llvm::ARM::PrintSupportedExtensions(llvmDescMap); else { // The option was already checked in Driver::HandleImmediateArgs, // so we do not expect to get here if we are not a supported architecture. diff --git a/llvm/include/llvm/MC/MCSubtargetInfo.h b/llvm/include/llvm/MC/MCSubtargetInfo.h index c1533ac8d0059f5..45c58988985e638 100644 --- a/llvm/include/llvm/MC/MCSubtargetInfo.h +++ b/llvm/include/llvm/MC/MCSubtargetInfo.h @@ -230,10 +230,17 @@ 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. diff --git a/llvm/include/llvm/Support/RISCVISAInfo.h b/llvm/include/llvm/Support/RISCVISAInfo.h index 9092fe5c272a994..1555f2f9909983e 100644 --- a/llvm/include/llvm/Support/RISCVISAInfo.h +++ b/llvm/include/llvm/Support/RISCVISAInfo.h @@ -22,7 +22,7 @@ struct RISCVExtensionInfo { unsigned MinorVersion; }; -void riscvExtensionsHelp(); +void riscvExtensionsHelp(std::map<StringRef, StringRef> llvmDescMap); class RISCVISAInfo { public: diff --git a/llvm/include/llvm/TargetParser/AArch64TargetParser.h b/llvm/include/llvm/TargetParser/AArch64TargetParser.h index 190f482044083c0..0d4b5e5a18458c7 100644 --- a/llvm/include/llvm/TargetParser/AArch64TargetParser.h +++ b/llvm/include/llvm/TargetParser/AArch64TargetParser.h @@ -19,6 +19,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/VersionTuple.h" #include <array> +#include <map> #include <vector> namespace llvm { @@ -663,7 +664,7 @@ bool isX18ReservedByDefault(const Triple &TT); // themselves, they are sequential (0, 1, 2, 3, ...). uint64_t getCpuSupportsMask(ArrayRef<StringRef> FeatureStrs); -void PrintSupportedExtensions(); +void PrintSupportedExtensions(std::map<StringRef, StringRef> llvmDescMap); } // namespace AArch64 } // namespace llvm diff --git a/llvm/include/llvm/TargetParser/ARMTargetParser.h b/llvm/include/llvm/TargetParser/ARMTargetParser.h index 37a358d1fa415c9..f3472b1e68233ee 100644 --- a/llvm/include/llvm/TargetParser/ARMTargetParser.h +++ b/llvm/include/llvm/TargetParser/ARMTargetParser.h @@ -17,6 +17,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/ARMBuildAttributes.h" #include "llvm/TargetParser/ARMTargetParserCommon.h" +#include <map> #include <vector> namespace llvm { @@ -259,7 +260,7 @@ StringRef computeDefaultTargetABI(const Triple &TT, StringRef CPU); /// string then the triple's arch name is used. StringRef getARMCPUForArch(const llvm::Triple &Triple, StringRef MArch = {}); -void PrintSupportedExtensions(); +void PrintSupportedExtensions(std::map<StringRef, StringRef> llvmDescMap); } // namespace ARM } // namespace llvm diff --git a/llvm/lib/Support/RISCVISAInfo.cpp b/llvm/lib/Support/RISCVISAInfo.cpp index a3045657e63b724..9a13d71297f542d 100644 --- a/llvm/lib/Support/RISCVISAInfo.cpp +++ b/llvm/lib/Support/RISCVISAInfo.cpp @@ -210,24 +210,33 @@ static void verifyTables() { #endif } -void llvm::riscvExtensionsHelp() { +void llvm::riscvExtensionsHelp(std::map<StringRef, StringRef> llvmDescMap) { + outs() << "All available -march extensions for RISC-V\n\n"; - outs() << '\t' << left_justify("Name", 20) << "Version\n"; + outs() << '\t' << left_justify("Name", 20) << "Version"; + outs() << (llvmDescMap.empty() ? "\n" : "\tDescription\n"); RISCVISAInfo::OrderedExtensionMap ExtMap; for (const auto &E : SupportedExtensions) ExtMap[E.Name] = {E.Version.Major, E.Version.Minor}; - for (const auto &E : ExtMap) - outs() << format("\t%-20s%d.%d\n", E.first.c_str(), E.second.MajorVersion, + for (const auto &E : ExtMap) { + outs() << format("\t%-20s%d.%d", E.first.c_str(), E.second.MajorVersion, E.second.MinorVersion); + outs() << (llvmDescMap.empty() ? "\n" + : ("\t\t" + llvmDescMap[E.first] + "\n")); + } outs() << "\nExperimental extensions\n"; ExtMap.clear(); for (const auto &E : SupportedExperimentalExtensions) ExtMap[E.Name] = {E.Version.Major, E.Version.Minor}; - for (const auto &E : ExtMap) - outs() << format("\t%-20s%d.%d\n", E.first.c_str(), E.second.MajorVersion, + for (const auto &E : ExtMap) { + outs() << format("\t%-20s%d.%d", E.first.c_str(), E.second.MajorVersion, E.second.MinorVersion); + outs() << (llvmDescMap.empty() + ? "\n" + : ("\t\t" + llvmDescMap["experimental-"+E.first] + "\n")); + } outs() << "\nUse -march to specify the target's extension.\n" "For example, clang -march=rv32i_v1p0\n"; diff --git a/llvm/lib/TargetParser/AArch64TargetParser.cpp b/llvm/lib/TargetParser/AArch64TargetParser.cpp index 166cf880ad4a895..ed2eec1086fcf37 100644 --- a/llvm/lib/TargetParser/AArch64TargetParser.cpp +++ b/llvm/lib/TargetParser/AArch64TargetParser.cpp @@ -11,11 +11,13 @@ // //===----------------------------------------------------------------------===// -#include "llvm/TargetParser/AArch64TargetParser.h" +#include "llvm/Support/Error.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/TargetParser/AArch64TargetParser.h" #include "llvm/TargetParser/ARMTargetParserCommon.h" #include "llvm/TargetParser/Triple.h" #include <cctype> +#include <map> using namespace llvm; @@ -135,11 +137,17 @@ std::optional<AArch64::CpuInfo> AArch64::parseCpu(StringRef Name) { return {}; } -void AArch64::PrintSupportedExtensions() { +void AArch64::PrintSupportedExtensions(std::map<StringRef, StringRef> llvmDescMap) { outs() << "All available -march extensions for AArch64\n\n"; for (const auto &Ext : Extensions) { // Extensions without a feature cannot be used with -march. - if (!Ext.Feature.empty()) - outs() << '\t' << Ext.Name << "\n"; + if (!Ext.Feature.empty()) { + if (llvmDescMap.empty()) { + outs() << '\t' << Ext.Name << "\n"; + } else { + outs() << format("\t%-20s", Ext.Name.str().c_str()); + outs() << "\t\t" << llvmDescMap[Ext.Name] << "\n"; + } + } } } diff --git a/llvm/lib/TargetParser/ARMTargetParser.cpp b/llvm/lib/TargetParser/ARMTargetParser.cpp index 7bf7914e9c53163..93d19bf4c46bc5c 100644 --- a/llvm/lib/TargetParser/ARMTargetParser.cpp +++ b/llvm/lib/TargetParser/ARMTargetParser.cpp @@ -11,9 +11,10 @@ // //===----------------------------------------------------------------------===// -#include "llvm/TargetParser/ARMTargetParser.h" #include "llvm/ADT/StringSwitch.h" +#include "llvm/Support/Error.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/TargetParser/ARMTargetParser.h" #include "llvm/TargetParser/ARMTargetParserCommon.h" #include "llvm/TargetParser/Triple.h" #include <cctype> @@ -600,11 +601,17 @@ StringRef ARM::getARMCPUForArch(const llvm::Triple &Triple, StringRef MArch) { llvm_unreachable("invalid arch name"); } -void ARM::PrintSupportedExtensions() { +void ARM::PrintSupportedExtensions(std::map<StringRef, StringRef> llvmDescMap) { outs() << "All available -march extensions for ARM\n\n"; for (const auto &Ext : ARCHExtNames) { // Extensions without a feature cannot be used with -march. - if (!Ext.Feature.empty()) - outs() << '\t' << Ext.Name << "\n"; + if (!Ext.Feature.empty()) { + if (llvmDescMap.empty()) { + outs() << '\t' << Ext.Name << "\n"; + } else { + outs() << format("\t%-20s", Ext.Name.str().c_str()); + outs() << "\t\t" << llvmDescMap[Ext.Name] << "\n"; + } + } } } diff --git a/llvm/unittests/Support/RISCVISAInfoTest.cpp b/llvm/unittests/Support/RISCVISAInfoTest.cpp index a285baa57a2a6f5..3f16b8e141927ea 100644 --- a/llvm/unittests/Support/RISCVISAInfoTest.cpp +++ b/llvm/unittests/Support/RISCVISAInfoTest.cpp @@ -9,6 +9,7 @@ #include "llvm/Support/RISCVISAInfo.h" #include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" +#include <map> using ::testing::ElementsAre; @@ -758,7 +759,8 @@ For example, clang -march=rv32i_v1p0)"; outs().flush(); testing::internal::CaptureStdout(); - llvm::riscvExtensionsHelp(); + std::map<StringRef, StringRef> EmptyMap; + llvm::riscvExtensionsHelp(EmptyMap); outs().flush(); std::string CapturedOutput = testing::internal::GetCapturedStdout(); diff --git a/llvm/unittests/TargetParser/TargetParserTest.cpp b/llvm/unittests/TargetParser/TargetParserTest.cpp index 8223f9f575135dc..d7de4696eab2dd5 100644 --- a/llvm/unittests/TargetParser/TargetParserTest.cpp +++ b/llvm/unittests/TargetParser/TargetParserTest.cpp @@ -17,6 +17,7 @@ #include "llvm/TargetParser/Triple.h" #include "gmock/gmock.h" #include "gtest/gtest.h" +#include <map> #include <optional> #include <sstream> #include <string> @@ -1016,7 +1017,8 @@ TEST(TargetParserTest, ARMPrintSupportedExtensions) { outs().flush(); testing::internal::CaptureStdout(); - ARM::PrintSupportedExtensions(); + std::map<StringRef, StringRef> EmptyMap; + ARM::PrintSupportedExtensions(EmptyMap); outs().flush(); std::string captured = testing::internal::GetCapturedStdout(); @@ -1936,7 +1938,8 @@ TEST(TargetParserTest, AArch64PrintSupportedExtensions) { outs().flush(); testing::internal::CaptureStdout(); - AArch64::PrintSupportedExtensions(); + std::map<StringRef, StringRef> EmptyMap; + AArch64::PrintSupportedExtensions(EmptyMap); outs().flush(); std::string captured = testing::internal::GetCapturedStdout(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits