Skip to content

Commit 73779bb

Browse files
authored
[clang] Enable descriptions for --print-supported-extensions (#66715)
Enables summary descriptions along with the names of the feature. Descriptions here are simply looked up via the available llvm tablegen data.
1 parent 4c14638 commit 73779bb

File tree

11 files changed

+212
-146
lines changed

11 files changed

+212
-146
lines changed

clang/test/Driver/print-supported-extensions.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
// RUN: %if aarch64-registered-target %{ %clang --target=aarch64-linux-gnu \
55
// RUN: --print-supported-extensions 2>&1 | FileCheck %s --check-prefix AARCH64 %}
66
// AARCH64: All available -march extensions for AArch64
7+
// AARCH64: Name Description
8+
// AARCH64: aes Enable AES support (FEAT_AES, FEAT_PMULL)
79

810
// RUN: %if riscv-registered-target %{ %clang --target=riscv64-linux-gnu \
911
// RUN: --print-supported-extensions 2>&1 | FileCheck %s --check-prefix RISCV %}
1012
// RISCV: All available -march extensions for RISC-V
13+
// RISCV: Name Version Description
14+
// RISCV: i 2.1
1115

1216
// RUN: %if arm-registered-target %{ %clang --target=arm-linux-gnu \
1317
// RUN: --print-supported-extensions 2>&1 | FileCheck %s --check-prefix ARM %}
1418
// ARM: All available -march extensions for ARM
19+
// ARM: Name Description
20+
// ARM: crc Enable support for CRC instructions
1521

1622
// RUN: %if x86-registered-target %{ not %clang --target=x86_64-linux-gnu \
1723
// RUN: --print-supported-extensions 2>&1 | FileCheck %s --check-prefix X86 %}

clang/tools/driver/cc1_main.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/ADT/Statistic.h"
2929
#include "llvm/Config/llvm-config.h"
3030
#include "llvm/LinkAllPasses.h"
31+
#include "llvm/MC/MCSubtargetInfo.h"
3132
#include "llvm/MC/TargetRegistry.h"
3233
#include "llvm/Option/Arg.h"
3334
#include "llvm/Option/ArgList.h"
@@ -198,13 +199,20 @@ static int PrintSupportedExtensions(std::string TargetStr) {
198199
std::unique_ptr<llvm::TargetMachine> TheTargetMachine(
199200
TheTarget->createTargetMachine(TargetStr, "", "", Options, std::nullopt));
200201
const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple();
202+
const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo();
203+
const llvm::ArrayRef<llvm::SubtargetFeatureKV> Features =
204+
MCInfo->getAllProcessorFeatures();
205+
206+
llvm::StringMap<llvm::StringRef> DescMap;
207+
for (const llvm::SubtargetFeatureKV &feature : Features)
208+
DescMap.insert({feature.Key, feature.Desc});
201209

202210
if (MachineTriple.isRISCV())
203-
llvm::riscvExtensionsHelp();
211+
llvm::riscvExtensionsHelp(DescMap);
204212
else if (MachineTriple.isAArch64())
205-
llvm::AArch64::PrintSupportedExtensions();
213+
llvm::AArch64::PrintSupportedExtensions(DescMap);
206214
else if (MachineTriple.isARM())
207-
llvm::ARM::PrintSupportedExtensions();
215+
llvm::ARM::PrintSupportedExtensions(DescMap);
208216
else {
209217
// The option was already checked in Driver::HandleImmediateArgs,
210218
// so we do not expect to get here if we are not a supported architecture.

llvm/include/llvm/MC/MCSubtargetInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,16 @@ class MCSubtargetInfo {
230230
return Found != ProcDesc.end() && StringRef(Found->Key) == CPU;
231231
}
232232

233+
/// Return processor descriptions.
233234
ArrayRef<SubtargetSubTypeKV> getAllProcessorDescriptions() const {
234235
return ProcDesc;
235236
}
236237

238+
/// Return processor features.
239+
ArrayRef<SubtargetFeatureKV> getAllProcessorFeatures() const {
240+
return ProcFeatures;
241+
}
242+
237243
virtual unsigned getHwMode() const { return 0; }
238244

239245
/// Return the cache size in bytes for the given level of cache.

llvm/include/llvm/Support/RISCVISAInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_SUPPORT_RISCVISAINFO_H
1010
#define LLVM_SUPPORT_RISCVISAINFO_H
1111

12+
#include "llvm/ADT/StringMap.h"
1213
#include "llvm/ADT/StringRef.h"
1314
#include "llvm/Support/Error.h"
1415

@@ -22,7 +23,7 @@ struct RISCVExtensionInfo {
2223
unsigned MinorVersion;
2324
};
2425

25-
void riscvExtensionsHelp();
26+
void riscvExtensionsHelp(StringMap<StringRef> DescMap);
2627

2728
class RISCVISAInfo {
2829
public:

llvm/include/llvm/TargetParser/AArch64TargetParser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm/ADT/ArrayRef.h"
1818
#include "llvm/ADT/Bitset.h"
19+
#include "llvm/ADT/StringMap.h"
1920
#include "llvm/ADT/StringRef.h"
2021
#include "llvm/Support/VersionTuple.h"
2122
#include <array>
@@ -663,7 +664,7 @@ bool isX18ReservedByDefault(const Triple &TT);
663664
// themselves, they are sequential (0, 1, 2, 3, ...).
664665
uint64_t getCpuSupportsMask(ArrayRef<StringRef> FeatureStrs);
665666

666-
void PrintSupportedExtensions();
667+
void PrintSupportedExtensions(StringMap<StringRef> DescMap);
667668

668669
} // namespace AArch64
669670
} // namespace llvm

llvm/include/llvm/TargetParser/ARMTargetParser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_TARGETPARSER_ARMTARGETPARSER_H
1515
#define LLVM_TARGETPARSER_ARMTARGETPARSER_H
1616

17+
#include "llvm/ADT/StringMap.h"
1718
#include "llvm/ADT/StringRef.h"
1819
#include "llvm/Support/ARMBuildAttributes.h"
1920
#include "llvm/TargetParser/ARMTargetParserCommon.h"
@@ -259,7 +260,7 @@ StringRef computeDefaultTargetABI(const Triple &TT, StringRef CPU);
259260
/// string then the triple's arch name is used.
260261
StringRef getARMCPUForArch(const llvm::Triple &Triple, StringRef MArch = {});
261262

262-
void PrintSupportedExtensions();
263+
void PrintSupportedExtensions(StringMap<StringRef> DescMap);
263264

264265
} // namespace ARM
265266
} // namespace llvm

llvm/lib/Support/RISCVISAInfo.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,24 +210,36 @@ static void verifyTables() {
210210
#endif
211211
}
212212

213-
void llvm::riscvExtensionsHelp() {
213+
static void PrintExtension(const std::string Name, const std::string Version,
214+
const std::string Description) {
215+
outs() << " "
216+
<< format(Description.empty() ? "%-20s%s\n" : "%-20s%-10s%s\n",
217+
Name.c_str(), Version.c_str(), Description.c_str());
218+
}
219+
220+
void llvm::riscvExtensionsHelp(StringMap<StringRef> DescMap) {
221+
214222
outs() << "All available -march extensions for RISC-V\n\n";
215-
outs() << '\t' << left_justify("Name", 20) << "Version\n";
223+
PrintExtension("Name", "Version", (DescMap.empty() ? "" : "Description"));
216224

217225
RISCVISAInfo::OrderedExtensionMap ExtMap;
218226
for (const auto &E : SupportedExtensions)
219227
ExtMap[E.Name] = {E.Version.Major, E.Version.Minor};
220-
for (const auto &E : ExtMap)
221-
outs() << format("\t%-20s%d.%d\n", E.first.c_str(), E.second.MajorVersion,
222-
E.second.MinorVersion);
228+
for (const auto &E : ExtMap) {
229+
std::string Version = std::to_string(E.second.MajorVersion) + "." +
230+
std::to_string(E.second.MinorVersion);
231+
PrintExtension(E.first, Version, DescMap[E.first].str());
232+
}
223233

224234
outs() << "\nExperimental extensions\n";
225235
ExtMap.clear();
226236
for (const auto &E : SupportedExperimentalExtensions)
227237
ExtMap[E.Name] = {E.Version.Major, E.Version.Minor};
228-
for (const auto &E : ExtMap)
229-
outs() << format("\t%-20s%d.%d\n", E.first.c_str(), E.second.MajorVersion,
230-
E.second.MinorVersion);
238+
for (const auto &E : ExtMap) {
239+
std::string Version = std::to_string(E.second.MajorVersion) + "." +
240+
std::to_string(E.second.MinorVersion);
241+
PrintExtension(E.first, Version, DescMap["experimental-" + E.first].str());
242+
}
231243

232244
outs() << "\nUse -march to specify the target's extension.\n"
233245
"For example, clang -march=rv32i_v1p0\n";

llvm/lib/TargetParser/AArch64TargetParser.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "llvm/TargetParser/AArch64TargetParser.h"
15+
#include "llvm/Support/Format.h"
1516
#include "llvm/Support/raw_ostream.h"
1617
#include "llvm/TargetParser/ARMTargetParserCommon.h"
1718
#include "llvm/TargetParser/Triple.h"
@@ -135,11 +136,17 @@ std::optional<AArch64::CpuInfo> AArch64::parseCpu(StringRef Name) {
135136
return {};
136137
}
137138

138-
void AArch64::PrintSupportedExtensions() {
139-
outs() << "All available -march extensions for AArch64\n\n";
139+
void AArch64::PrintSupportedExtensions(StringMap<StringRef> DescMap) {
140+
outs() << "All available -march extensions for AArch64\n\n"
141+
<< " " << left_justify("Name", 20)
142+
<< (DescMap.empty() ? "\n" : "Description\n");
140143
for (const auto &Ext : Extensions) {
141144
// Extensions without a feature cannot be used with -march.
142-
if (!Ext.Feature.empty())
143-
outs() << '\t' << Ext.Name << "\n";
145+
if (!Ext.Feature.empty()) {
146+
std::string Description = DescMap[Ext.Name].str();
147+
outs() << " "
148+
<< format(Description.empty() ? "%s\n" : "%-20s%s\n",
149+
Ext.Name.str().c_str(), Description.c_str());
150+
}
144151
}
145152
}

llvm/lib/TargetParser/ARMTargetParser.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "llvm/TargetParser/ARMTargetParser.h"
1515
#include "llvm/ADT/StringSwitch.h"
16+
#include "llvm/Support/Format.h"
1617
#include "llvm/Support/raw_ostream.h"
1718
#include "llvm/TargetParser/ARMTargetParserCommon.h"
1819
#include "llvm/TargetParser/Triple.h"
@@ -600,11 +601,17 @@ StringRef ARM::getARMCPUForArch(const llvm::Triple &Triple, StringRef MArch) {
600601
llvm_unreachable("invalid arch name");
601602
}
602603

603-
void ARM::PrintSupportedExtensions() {
604-
outs() << "All available -march extensions for ARM\n\n";
604+
void ARM::PrintSupportedExtensions(StringMap<StringRef> DescMap) {
605+
outs() << "All available -march extensions for ARM\n\n"
606+
<< " " << left_justify("Name", 20)
607+
<< (DescMap.empty() ? "\n" : "Description\n");
605608
for (const auto &Ext : ARCHExtNames) {
606609
// Extensions without a feature cannot be used with -march.
607-
if (!Ext.Feature.empty())
608-
outs() << '\t' << Ext.Name << "\n";
610+
if (!Ext.Feature.empty()) {
611+
std::string Description = DescMap[Ext.Name].str();
612+
outs() << " "
613+
<< format(Description.empty() ? "%s\n" : "%-20s%s\n",
614+
Ext.Name.str().c_str(), Description.c_str());
615+
}
609616
}
610617
}

0 commit comments

Comments
 (0)