Skip to content

Commit

Permalink
[RISCV] Deduplicate RISCVISAInfo::toFeatures/toFeatureVector. NFC (ll…
Browse files Browse the repository at this point in the history
…vm#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.
  • Loading branch information
lukel97 authored Jan 9, 2024
1 parent 2357e89 commit db78c30
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
4 changes: 2 additions & 2 deletions clang/lib/Basic/Targets/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ bool RISCVTargetInfo::initFeatureMap(
}

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

// parseFeatures normalizes the feature set by dropping any explicit
// negatives, and non-extension features. We need to preserve the later
Expand Down Expand Up @@ -413,7 +413,7 @@ static void handleFullArchString(StringRef FullArchStr,
// Forward the invalid FullArchStr.
Features.push_back("+" + FullArchStr.str());
} else {
std::vector<std::string> FeatStrings = (*RII)->toFeatureVector();
std::vector<std::string> FeatStrings = (*RII)->toFeatures();
Features.insert(Features.end(), FeatStrings.begin(), FeatStrings.end());
}
}
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Driver/ToolChains/Arch/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ static bool getArchFeatures(const Driver &D, StringRef Arch,
return false;
}

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

if (EnableExperimentalExtensions)
Features.push_back(Args.MakeArgString("+experimental"));
Expand Down
6 changes: 2 additions & 4 deletions llvm/include/llvm/Support/RISCVISAInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ class RISCVISAInfo {
parseFeatures(unsigned XLen, const std::vector<std::string> &Features);

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

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

Expand All @@ -83,7 +82,6 @@ class RISCVISAInfo {

bool hasExtension(StringRef Ext) const;
std::string toString() const;
std::vector<std::string> toFeatureVector() const;
StringRef computeDefaultABI() const;

static bool isSupportedExtensionFeature(StringRef Ext);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Object/ELFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
else
llvm_unreachable("XLEN should be 32 or 64.");

Features.addFeaturesVector(ISAInfo->toFeatureVector());
Features.addFeaturesVector(ISAInfo->toFeatures());
}

return Features;
Expand Down
41 changes: 14 additions & 27 deletions llvm/lib/Support/RISCVISAInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,35 +466,38 @@ bool RISCVISAInfo::compareExtension(const std::string &LHS,
return LHS < RHS;
}

void RISCVISAInfo::toFeatures(
std::vector<StringRef> &Features,
llvm::function_ref<StringRef(const Twine &)> StrAlloc,
bool AddAllExtensions) const {
for (auto const &Ext : Exts) {
StringRef ExtName = Ext.first;

std::vector<std::string> RISCVISAInfo::toFeatures(bool AddAllExtensions,
bool IgnoreUnknown) const {
std::vector<std::string> Features;
for (const auto &[ExtName, _] : Exts) {
// i is a base instruction set, not an extension (see
// https://github.com/riscv/riscv-isa-manual/blob/main/src/naming.adoc#base-integer-isa)
// and is not recognized in clang -cc1
if (ExtName == "i")
continue;
if (IgnoreUnknown && !isSupportedExtension(ExtName))
continue;

if (isExperimentalExtension(ExtName)) {
Features.push_back(StrAlloc("+experimental-" + ExtName));
Features.push_back((llvm::Twine("+experimental-") + ExtName).str());
} else {
Features.push_back(StrAlloc("+" + ExtName));
Features.push_back((llvm::Twine("+") + ExtName).str());
}
}
if (AddAllExtensions) {
for (const RISCVSupportedExtension &Ext : SupportedExtensions) {
if (Exts.count(Ext.Name))
continue;
Features.push_back(StrAlloc(Twine("-") + Ext.Name));
Features.push_back((llvm::Twine("-") + Ext.Name).str());
}

for (const RISCVSupportedExtension &Ext : SupportedExperimentalExtensions) {
if (Exts.count(Ext.Name))
continue;
Features.push_back(StrAlloc(Twine("-experimental-") + Ext.Name));
Features.push_back((llvm::Twine("-experimental-") + Ext.Name).str());
}
}
return Features;
}

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

std::vector<std::string> RISCVISAInfo::toFeatureVector() const {
std::vector<std::string> FeatureVector;
for (auto const &Ext : Exts) {
std::string ExtName = Ext.first;
if (ExtName == "i") // i is not recognized in clang -cc1
continue;
if (!isSupportedExtension(ExtName))
continue;
std::string Feature = isExperimentalExtension(ExtName)
? "+experimental-" + ExtName
: "+" + ExtName;
FeatureVector.push_back(Feature);
}
return FeatureVector;
}

llvm::Expected<std::unique_ptr<RISCVISAInfo>>
RISCVISAInfo::postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo) {
ISAInfo->updateImplication();
Expand Down
30 changes: 25 additions & 5 deletions llvm/unittests/Support/RISCVISAInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,25 +477,45 @@ TEST(ParseArchString, RejectsConflictingExtensions) {
}
}

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

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

TEST(ToFeatureVector, UnsupportedExtensionsAreDropped) {
TEST(ToFeatures, UnsupportedExtensionsAreDropped) {
auto MaybeISAInfo =
RISCVISAInfo::parseNormalizedArchString("rv64i2p0_m2p0_xmadeup1p0");
ASSERT_THAT_EXPECTED(MaybeISAInfo, Succeeded());
EXPECT_THAT((*MaybeISAInfo)->toFeatureVector(), ElementsAre("+m"));
EXPECT_THAT((*MaybeISAInfo)->toFeatures(), ElementsAre("+m"));
}

TEST(ToFeatures, UnsupportedExtensionsAreKeptIfIgnoreUnknownIsFalse) {
auto MaybeISAInfo =
RISCVISAInfo::parseNormalizedArchString("rv64i2p0_m2p0_xmadeup1p0");
ASSERT_THAT_EXPECTED(MaybeISAInfo, Succeeded());
EXPECT_THAT((*MaybeISAInfo)->toFeatures(false, false),
ElementsAre("+m", "+xmadeup"));
}

TEST(ToFeatures, AddAllExtensionsAddsNegativeExtensions) {
auto MaybeISAInfo = RISCVISAInfo::parseNormalizedArchString("rv64i2p0_m2p0");
ASSERT_THAT_EXPECTED(MaybeISAInfo, Succeeded());

auto Features = (*MaybeISAInfo)->toFeatures(true);
EXPECT_GT(Features.size(), 1UL);
EXPECT_EQ(Features.front(), "+m");
// Every feature after should be a negative feature
for (auto &NegativeExt : llvm::drop_begin(Features))
EXPECT_TRUE(NegativeExt.substr(0, 1) == "-");
}

TEST(OrderedExtensionMap, ExtensionsAreCorrectlyOrdered) {
Expand Down

0 comments on commit db78c30

Please sign in to comment.