Skip to content

Commit 4b40ced

Browse files
committed
[RISCV] Add --print-supported-extensions support
This revision supports --print-supported-extensions, it prints out all of the extensions and corresponding version supported. Reviewed By: craig.topper, kito-cheng Differential Revision: https://reviews.llvm.org/D146054
1 parent 950f094 commit 4b40ced

File tree

7 files changed

+203
-11
lines changed

7 files changed

+203
-11
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5251,6 +5251,10 @@ def print_supported_cpus : Flag<["-", "--"], "print-supported-cpus">,
52515251
HelpText<"Print supported cpu models for the given target (if target is not specified,"
52525252
" it will print the supported cpus for the default target)">,
52535253
MarshallingInfoFlag<FrontendOpts<"PrintSupportedCPUs">>;
5254+
def print_supported_extensions : Flag<["-", "--"], "print-supported-extensions">,
5255+
Visibility<[ClangOption, CC1Option, CLOption]>,
5256+
HelpText<"Print supported extensions for RISC-V">,
5257+
MarshallingInfoFlag<FrontendOpts<"PrintSupportedExtensions">>;
52545258
def : Flag<["-"], "mcpu=help">, Alias<print_supported_cpus>;
52555259
def : Flag<["-"], "mtune=help">, Alias<print_supported_cpus>;
52565260
def time : Flag<["-"], "time">,

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ class FrontendOptions {
283283
/// print the supported cpus for the current target
284284
unsigned PrintSupportedCPUs : 1;
285285

286+
/// Print the supported extensions for the current target.
287+
unsigned PrintSupportedExtensions : 1;
288+
286289
/// Show the -version text.
287290
unsigned ShowVersion : 1;
288291

clang/lib/Driver/Driver.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,8 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
21032103

21042104
if (C.getArgs().hasArg(options::OPT_v) ||
21052105
C.getArgs().hasArg(options::OPT__HASH_HASH_HASH) ||
2106-
C.getArgs().hasArg(options::OPT_print_supported_cpus)) {
2106+
C.getArgs().hasArg(options::OPT_print_supported_cpus) ||
2107+
C.getArgs().hasArg(options::OPT_print_supported_extensions)) {
21072108
PrintVersion(C, llvm::errs());
21082109
SuppressMissingInputWarning = true;
21092110
}
@@ -4273,16 +4274,30 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
42734274
C.MakeAction<IfsMergeJobAction>(MergerInputs, types::TY_Image));
42744275
}
42754276

4276-
// If --print-supported-cpus, -mcpu=? or -mtune=? is specified, build a custom
4277-
// Compile phase that prints out supported cpu models and quits.
4278-
if (Arg *A = Args.getLastArg(options::OPT_print_supported_cpus)) {
4279-
// Use the -mcpu=? flag as the dummy input to cc1.
4280-
Actions.clear();
4281-
Action *InputAc = C.MakeAction<InputAction>(*A, types::TY_C);
4282-
Actions.push_back(
4283-
C.MakeAction<PrecompileJobAction>(InputAc, types::TY_Nothing));
4284-
for (auto &I : Inputs)
4285-
I.second->claim();
4277+
for (auto Opt : {options::OPT_print_supported_cpus,
4278+
options::OPT_print_supported_extensions}) {
4279+
// If --print-supported-cpus, -mcpu=? or -mtune=? is specified, build a
4280+
// custom Compile phase that prints out supported cpu models and quits.
4281+
//
4282+
// If --print-supported-extensions is specified, call the helper function
4283+
// RISCVMarchHelp in RISCVISAInfo.cpp that prints out supported extensions
4284+
// and quits.
4285+
if (Arg *A = Args.getLastArg(Opt)) {
4286+
if (Opt == options::OPT_print_supported_extensions &&
4287+
!C.getDefaultToolChain().getTriple().isRISCV()) {
4288+
C.getDriver().Diag(diag::err_opt_not_valid_on_target)
4289+
<< "--print-supported-extensions";
4290+
return;
4291+
}
4292+
4293+
// Use the -mcpu=? flag as the dummy input to cc1.
4294+
Actions.clear();
4295+
Action *InputAc = C.MakeAction<InputAction>(*A, types::TY_C);
4296+
Actions.push_back(
4297+
C.MakeAction<PrecompileJobAction>(InputAc, types::TY_Nothing));
4298+
for (auto &I : Inputs)
4299+
I.second->claim();
4300+
}
42864301
}
42874302

42884303
// Call validator for dxil when -Vd not in Args.

clang/tools/driver/cc1_main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/Support/ManagedStatic.h"
3939
#include "llvm/Support/Path.h"
4040
#include "llvm/Support/Process.h"
41+
#include "llvm/Support/RISCVISAInfo.h"
4142
#include "llvm/Support/Signals.h"
4243
#include "llvm/Support/TargetSelect.h"
4344
#include "llvm/Support/TimeProfiler.h"
@@ -221,6 +222,10 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
221222
if (Clang->getFrontendOpts().PrintSupportedCPUs)
222223
return PrintSupportedCPUs(Clang->getTargetOpts().Triple);
223224

225+
// --print-supported-extensions takes priority over the actual compilation.
226+
if (Clang->getFrontendOpts().PrintSupportedExtensions)
227+
return llvm::riscvExtensionsHelp(), 0;
228+
224229
// Infer the builtin include path if unspecified.
225230
if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
226231
Clang->getHeaderSearchOpts().ResourceDir.empty())

llvm/include/llvm/Support/RISCVISAInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ struct RISCVExtensionInfo {
2222
unsigned MinorVersion;
2323
};
2424

25+
void riscvExtensionsHelp();
26+
2527
class RISCVISAInfo {
2628
public:
2729
RISCVISAInfo(const RISCVISAInfo &) = delete;

llvm/lib/Support/RISCVISAInfo.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,29 @@ static void verifyTables() {
210210
#endif
211211
}
212212

213+
void llvm::riscvExtensionsHelp() {
214+
outs() << "All available -march extensions for RISC-V\n\n";
215+
outs() << '\t' << left_justify("Name", 20) << "Version\n";
216+
217+
RISCVISAInfo::OrderedExtensionMap ExtMap;
218+
for (const auto &E : SupportedExtensions)
219+
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);
223+
224+
outs() << "\nExperimental extensions\n";
225+
ExtMap.clear();
226+
for (const auto &E : SupportedExperimentalExtensions)
227+
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);
231+
232+
outs() << "\nUse -march to specify the target's extension.\n"
233+
"For example, clang -march=rv32i_v1p0\n";
234+
}
235+
213236
static bool stripExperimentalPrefix(StringRef &Ext) {
214237
return Ext.consume_front("experimental-");
215238
}

llvm/unittests/Support/RISCVISAInfoTest.cpp

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,143 @@ TEST(getTargetFeatureForExtension, RetrieveTargetFeatureFromOneExt) {
626626
EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension(""), "");
627627
EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension("zbbzihintntl"), "");
628628
}
629+
630+
TEST(RiscvExtensionsHelp, CheckExtensions) {
631+
std::string ExpectedOutput =
632+
R"(All available -march extensions for RISC-V
633+
634+
Name Version
635+
i 2.1
636+
e 2.0
637+
m 2.0
638+
a 2.1
639+
f 2.2
640+
d 2.2
641+
c 2.0
642+
v 1.0
643+
h 1.0
644+
zicbom 1.0
645+
zicbop 1.0
646+
zicboz 1.0
647+
zicntr 1.0
648+
zicsr 2.0
649+
zifencei 2.0
650+
zihintntl 1.0
651+
zihintpause 2.0
652+
zihpm 1.0
653+
zmmul 1.0
654+
zawrs 1.0
655+
zfh 1.0
656+
zfhmin 1.0
657+
zfinx 1.0
658+
zdinx 1.0
659+
zca 1.0
660+
zcb 1.0
661+
zcd 1.0
662+
zce 1.0
663+
zcf 1.0
664+
zcmp 1.0
665+
zcmt 1.0
666+
zba 1.0
667+
zbb 1.0
668+
zbc 1.0
669+
zbkb 1.0
670+
zbkc 1.0
671+
zbkx 1.0
672+
zbs 1.0
673+
zk 1.0
674+
zkn 1.0
675+
zknd 1.0
676+
zkne 1.0
677+
zknh 1.0
678+
zkr 1.0
679+
zks 1.0
680+
zksed 1.0
681+
zksh 1.0
682+
zkt 1.0
683+
zve32f 1.0
684+
zve32x 1.0
685+
zve64d 1.0
686+
zve64f 1.0
687+
zve64x 1.0
688+
zvfh 1.0
689+
zvfhmin 1.0
690+
zvl1024b 1.0
691+
zvl128b 1.0
692+
zvl16384b 1.0
693+
zvl2048b 1.0
694+
zvl256b 1.0
695+
zvl32768b 1.0
696+
zvl32b 1.0
697+
zvl4096b 1.0
698+
zvl512b 1.0
699+
zvl64b 1.0
700+
zvl65536b 1.0
701+
zvl8192b 1.0
702+
zhinx 1.0
703+
zhinxmin 1.0
704+
svinval 1.0
705+
svnapot 1.0
706+
svpbmt 1.0
707+
xcvalu 1.0
708+
xcvbi 1.0
709+
xcvbitmanip 1.0
710+
xcvmac 1.0
711+
xcvsimd 1.0
712+
xsfcie 1.0
713+
xsfvcp 1.0
714+
xtheadba 1.0
715+
xtheadbb 1.0
716+
xtheadbs 1.0
717+
xtheadcmo 1.0
718+
xtheadcondmov 1.0
719+
xtheadfmemidx 1.0
720+
xtheadmac 1.0
721+
xtheadmemidx 1.0
722+
xtheadmempair 1.0
723+
xtheadsync 1.0
724+
xtheadvdot 1.0
725+
xventanacondops 1.0
726+
727+
Experimental extensions
728+
zicfilp 0.2
729+
zicond 1.0
730+
zacas 1.0
731+
zfa 0.2
732+
zfbfmin 0.8
733+
ztso 0.1
734+
zvbb 1.0
735+
zvbc 1.0
736+
zvfbfmin 0.8
737+
zvfbfwma 0.8
738+
zvkb 1.0
739+
zvkg 1.0
740+
zvkn 1.0
741+
zvknc 1.0
742+
zvkned 1.0
743+
zvkng 1.0
744+
zvknha 1.0
745+
zvknhb 1.0
746+
zvks 1.0
747+
zvksc 1.0
748+
zvksed 1.0
749+
zvksg 1.0
750+
zvksh 1.0
751+
zvkt 1.0
752+
smaia 1.0
753+
ssaia 1.0
754+
755+
Use -march to specify the target's extension.
756+
For example, clang -march=rv32i_v1p0)";
757+
758+
outs().flush();
759+
testing::internal::CaptureStdout();
760+
761+
llvm::riscvExtensionsHelp();
762+
outs().flush();
763+
764+
std::string CapturedOutput = testing::internal::GetCapturedStdout();
765+
EXPECT_TRUE([](std::string &Captured, std::string &Expected) {
766+
return Captured.find(Expected) != std::string::npos;
767+
}(CapturedOutput, ExpectedOutput));
768+
}

0 commit comments

Comments
 (0)