[llvm] [clang] Enable descriptions for --print-supported-extensions (PR #66715)
Balint Cristian via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 18 15:35:15 PDT 2023
https://github.com/cbalint13 created https://github.com/llvm/llvm-project/pull/66715
Enables summary descriptions along with the names of the feature.
Descriptions here are simply looked up via the available llvm tablegen data.
The full prints from CLI are here: [aarch64-info.txt.gz](https://github.com/llvm/llvm-project/files/12653941/aarch64-info.txt.gz) , [arm-info.txt.gz](https://github.com/llvm/llvm-project/files/12653942/arm-info.txt.gz) , [riscv-info.txt.gz](https://github.com/llvm/llvm-project/files/12653943/riscv-info.txt.gz)
---
Cc @DavidSpickett
As a discussion: https://github.com/llvm/llvm-project/pull/66586#issuecomment-1723311672
>From f81597b89207a534deec926c7af8ec64dd81c118 Mon Sep 17 00:00:00 2001
From: Balint Cristian <cristian.balint at gmail.com>
Date: Tue, 19 Sep 2023 01:26:28 +0300
Subject: [PATCH] [clang] Enable descriptions for --print-supported-extensions
---
clang/tools/driver/cc1_main.cpp | 14 ++++++++++---
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 | 13 ++++++++----
llvm/lib/TargetParser/ARMTargetParser.cpp | 12 +++++++----
llvm/unittests/Support/RISCVISAInfoTest.cpp | 4 +++-
.../TargetParser/TargetParserTest.cpp | 7 +++++--
10 files changed, 63 insertions(+), 23 deletions(-)
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index f0d7b5c3889dc1f..923e43491a06842 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,20 @@ 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..98c2cd84fd3230b 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..e050f840fd0a483 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..dbc10c8e28c076c 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..985afddf758526f 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,14 @@ 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()) {
+ outs() << format("\t%-20s", Ext.Name.str().c_str());
+ outs() << (llvmDescMap.empty() ? "\n"
+ : ("\t\t" + llvmDescMap[Ext.Name] + "\n"));
+ }
}
}
diff --git a/llvm/lib/TargetParser/ARMTargetParser.cpp b/llvm/lib/TargetParser/ARMTargetParser.cpp
index 7bf7914e9c53163..2e416fe56513b8e 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,14 @@ 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()) {
+ outs() << format("\t%-20s", Ext.Name.str().c_str());
+ outs() << (llvmDescMap.empty() ? "\n"
+ : ("\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();
More information about the llvm-commits
mailing list