Skip to content

Commit db78c30

Browse files
authored
[RISCV] Deduplicate RISCVISAInfo::toFeatures/toFeatureVector. NFC (#76942)
toFeatures and toFeatureVector both output a list of target feature flags, just with a slightly different interface. toFeatures keeps any unsupported extensions, and also provides a way to append negative extensions (AddAllExtensions=true). This patch combines them into one function, so that a later patch will be be able to get a std::vector of features that includes all the negative extensions, which was previously only possible through the StrAlloc interface.
1 parent 2357e89 commit db78c30

File tree

6 files changed

+47
-42
lines changed

6 files changed

+47
-42
lines changed

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ bool RISCVTargetInfo::initFeatureMap(
296296
}
297297

298298
// RISCVISAInfo makes implications for ISA features
299-
std::vector<std::string> ImpliedFeatures = (*ParseResult)->toFeatureVector();
299+
std::vector<std::string> ImpliedFeatures = (*ParseResult)->toFeatures();
300300

301301
// parseFeatures normalizes the feature set by dropping any explicit
302302
// negatives, and non-extension features. We need to preserve the later
@@ -413,7 +413,7 @@ static void handleFullArchString(StringRef FullArchStr,
413413
// Forward the invalid FullArchStr.
414414
Features.push_back("+" + FullArchStr.str());
415415
} else {
416-
std::vector<std::string> FeatStrings = (*RII)->toFeatureVector();
416+
std::vector<std::string> FeatStrings = (*RII)->toFeatures();
417417
Features.insert(Features.end(), FeatStrings.begin(), FeatStrings.end());
418418
}
419419
}

clang/lib/Driver/ToolChains/Arch/RISCV.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ static bool getArchFeatures(const Driver &D, StringRef Arch,
4242
return false;
4343
}
4444

45-
(*ISAInfo)->toFeatures(
46-
Features, [&Args](const Twine &Str) { return Args.MakeArgString(Str); },
47-
/*AddAllExtensions=*/true);
45+
for (const std::string &Str : (*ISAInfo)->toFeatures(/*AddAllExtension=*/true,
46+
/*IgnoreUnknown=*/false))
47+
Features.push_back(Args.MakeArgString(Str));
4848

4949
if (EnableExperimentalExtensions)
5050
Features.push_back(Args.MakeArgString("+experimental"));

llvm/include/llvm/Support/RISCVISAInfo.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ class RISCVISAInfo {
6868
parseFeatures(unsigned XLen, const std::vector<std::string> &Features);
6969

7070
/// Convert RISC-V ISA info to a feature vector.
71-
void toFeatures(std::vector<StringRef> &Features,
72-
llvm::function_ref<StringRef(const Twine &)> StrAlloc,
73-
bool AddAllExtensions) const;
71+
std::vector<std::string> toFeatures(bool AddAllExtensions = false,
72+
bool IgnoreUnknown = true) const;
7473

7574
const OrderedExtensionMap &getExtensions() const { return Exts; };
7675

@@ -83,7 +82,6 @@ class RISCVISAInfo {
8382

8483
bool hasExtension(StringRef Ext) const;
8584
std::string toString() const;
86-
std::vector<std::string> toFeatureVector() const;
8785
StringRef computeDefaultABI() const;
8886

8987
static bool isSupportedExtensionFeature(StringRef Ext);

llvm/lib/Object/ELFObjectFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
315315
else
316316
llvm_unreachable("XLEN should be 32 or 64.");
317317

318-
Features.addFeaturesVector(ISAInfo->toFeatureVector());
318+
Features.addFeaturesVector(ISAInfo->toFeatures());
319319
}
320320

321321
return Features;

llvm/lib/Support/RISCVISAInfo.cpp

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -466,35 +466,38 @@ bool RISCVISAInfo::compareExtension(const std::string &LHS,
466466
return LHS < RHS;
467467
}
468468

469-
void RISCVISAInfo::toFeatures(
470-
std::vector<StringRef> &Features,
471-
llvm::function_ref<StringRef(const Twine &)> StrAlloc,
472-
bool AddAllExtensions) const {
473-
for (auto const &Ext : Exts) {
474-
StringRef ExtName = Ext.first;
475-
469+
std::vector<std::string> RISCVISAInfo::toFeatures(bool AddAllExtensions,
470+
bool IgnoreUnknown) const {
471+
std::vector<std::string> Features;
472+
for (const auto &[ExtName, _] : Exts) {
473+
// i is a base instruction set, not an extension (see
474+
// https://github.com/riscv/riscv-isa-manual/blob/main/src/naming.adoc#base-integer-isa)
475+
// and is not recognized in clang -cc1
476476
if (ExtName == "i")
477477
continue;
478+
if (IgnoreUnknown && !isSupportedExtension(ExtName))
479+
continue;
478480

479481
if (isExperimentalExtension(ExtName)) {
480-
Features.push_back(StrAlloc("+experimental-" + ExtName));
482+
Features.push_back((llvm::Twine("+experimental-") + ExtName).str());
481483
} else {
482-
Features.push_back(StrAlloc("+" + ExtName));
484+
Features.push_back((llvm::Twine("+") + ExtName).str());
483485
}
484486
}
485487
if (AddAllExtensions) {
486488
for (const RISCVSupportedExtension &Ext : SupportedExtensions) {
487489
if (Exts.count(Ext.Name))
488490
continue;
489-
Features.push_back(StrAlloc(Twine("-") + Ext.Name));
491+
Features.push_back((llvm::Twine("-") + Ext.Name).str());
490492
}
491493

492494
for (const RISCVSupportedExtension &Ext : SupportedExperimentalExtensions) {
493495
if (Exts.count(Ext.Name))
494496
continue;
495-
Features.push_back(StrAlloc(Twine("-experimental-") + Ext.Name));
497+
Features.push_back((llvm::Twine("-experimental-") + Ext.Name).str());
496498
}
497499
}
500+
return Features;
498501
}
499502

500503
// Extensions may have a version number, and may be separated by
@@ -1269,22 +1272,6 @@ std::string RISCVISAInfo::toString() const {
12691272
return Arch.str();
12701273
}
12711274

1272-
std::vector<std::string> RISCVISAInfo::toFeatureVector() const {
1273-
std::vector<std::string> FeatureVector;
1274-
for (auto const &Ext : Exts) {
1275-
std::string ExtName = Ext.first;
1276-
if (ExtName == "i") // i is not recognized in clang -cc1
1277-
continue;
1278-
if (!isSupportedExtension(ExtName))
1279-
continue;
1280-
std::string Feature = isExperimentalExtension(ExtName)
1281-
? "+experimental-" + ExtName
1282-
: "+" + ExtName;
1283-
FeatureVector.push_back(Feature);
1284-
}
1285-
return FeatureVector;
1286-
}
1287-
12881275
llvm::Expected<std::unique_ptr<RISCVISAInfo>>
12891276
RISCVISAInfo::postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo) {
12901277
ISAInfo->updateImplication();

llvm/unittests/Support/RISCVISAInfoTest.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,25 +477,45 @@ TEST(ParseArchString, RejectsConflictingExtensions) {
477477
}
478478
}
479479

480-
TEST(ToFeatureVector, IIsDroppedAndExperimentalExtensionsArePrefixed) {
480+
TEST(ToFeatures, IIsDroppedAndExperimentalExtensionsArePrefixed) {
481481
auto MaybeISAInfo1 =
482482
RISCVISAInfo::parseArchString("rv64im_zicond", true, false);
483483
ASSERT_THAT_EXPECTED(MaybeISAInfo1, Succeeded());
484-
EXPECT_THAT((*MaybeISAInfo1)->toFeatureVector(),
484+
EXPECT_THAT((*MaybeISAInfo1)->toFeatures(),
485485
ElementsAre("+m", "+experimental-zicond"));
486486

487487
auto MaybeISAInfo2 = RISCVISAInfo::parseArchString(
488488
"rv32e_zicond_xventanacondops", true, false);
489489
ASSERT_THAT_EXPECTED(MaybeISAInfo2, Succeeded());
490-
EXPECT_THAT((*MaybeISAInfo2)->toFeatureVector(),
490+
EXPECT_THAT((*MaybeISAInfo2)->toFeatures(),
491491
ElementsAre("+e", "+experimental-zicond", "+xventanacondops"));
492492
}
493493

494-
TEST(ToFeatureVector, UnsupportedExtensionsAreDropped) {
494+
TEST(ToFeatures, UnsupportedExtensionsAreDropped) {
495495
auto MaybeISAInfo =
496496
RISCVISAInfo::parseNormalizedArchString("rv64i2p0_m2p0_xmadeup1p0");
497497
ASSERT_THAT_EXPECTED(MaybeISAInfo, Succeeded());
498-
EXPECT_THAT((*MaybeISAInfo)->toFeatureVector(), ElementsAre("+m"));
498+
EXPECT_THAT((*MaybeISAInfo)->toFeatures(), ElementsAre("+m"));
499+
}
500+
501+
TEST(ToFeatures, UnsupportedExtensionsAreKeptIfIgnoreUnknownIsFalse) {
502+
auto MaybeISAInfo =
503+
RISCVISAInfo::parseNormalizedArchString("rv64i2p0_m2p0_xmadeup1p0");
504+
ASSERT_THAT_EXPECTED(MaybeISAInfo, Succeeded());
505+
EXPECT_THAT((*MaybeISAInfo)->toFeatures(false, false),
506+
ElementsAre("+m", "+xmadeup"));
507+
}
508+
509+
TEST(ToFeatures, AddAllExtensionsAddsNegativeExtensions) {
510+
auto MaybeISAInfo = RISCVISAInfo::parseNormalizedArchString("rv64i2p0_m2p0");
511+
ASSERT_THAT_EXPECTED(MaybeISAInfo, Succeeded());
512+
513+
auto Features = (*MaybeISAInfo)->toFeatures(true);
514+
EXPECT_GT(Features.size(), 1UL);
515+
EXPECT_EQ(Features.front(), "+m");
516+
// Every feature after should be a negative feature
517+
for (auto &NegativeExt : llvm::drop_begin(Features))
518+
EXPECT_TRUE(NegativeExt.substr(0, 1) == "-");
499519
}
500520

501521
TEST(OrderedExtensionMap, ExtensionsAreCorrectlyOrdered) {

0 commit comments

Comments
 (0)