Skip to content

Commit

Permalink
[RISCV] Add function that check extension name with version
Browse files Browse the repository at this point in the history
Check whether a extension string with version is valid, and get the targetfeature from it.

New functions be used in RISCVISAInfo for https://reviews.llvm.org/D151730.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D152423
  • Loading branch information
BeMg committed Aug 21, 2023
1 parent dc10bd4 commit 4b60e1e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions llvm/include/llvm/Support/RISCVISAInfo.h
Expand Up @@ -85,10 +85,12 @@ class RISCVISAInfo {

static bool isSupportedExtensionFeature(StringRef Ext);
static bool isSupportedExtension(StringRef Ext);
static bool isSupportedExtensionWithVersion(StringRef Ext);
static bool isSupportedExtension(StringRef Ext, unsigned MajorVersion,
unsigned MinorVersion);
static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo);
static std::string getTargetFeatureForExtension(StringRef Ext);

private:
RISCVISAInfo(unsigned XLen)
Expand Down
37 changes: 37 additions & 0 deletions llvm/lib/Support/RISCVISAInfo.cpp
Expand Up @@ -1253,3 +1253,40 @@ StringRef RISCVISAInfo::computeDefaultABI() const {
}
llvm_unreachable("Invalid XLEN");
}

bool RISCVISAInfo::isSupportedExtensionWithVersion(StringRef Ext) {
if (Ext.empty())
return false;

auto Pos = findLastNonVersionCharacter(Ext) + 1;
StringRef Name = Ext.substr(0, Pos);
StringRef Vers = Ext.substr(Pos);
if (Vers.empty())
return false;

unsigned Major, Minor, ConsumeLength;
if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
true, true)) {
consumeError(std::move(E));
return false;
}

return true;
}

std::string RISCVISAInfo::getTargetFeatureForExtension(StringRef Ext) {
if (Ext.empty())
return std::string();

auto Pos = findLastNonVersionCharacter(Ext) + 1;
StringRef Name = Ext.substr(0, Pos);

if (Pos != Ext.size() && !isSupportedExtensionWithVersion(Ext))
return std::string();

if (!isSupportedExtension(Name))
return std::string();

return isExperimentalExtension(Name) ? "experimental-" + Name.str()
: Name.str();
}
22 changes: 22 additions & 0 deletions llvm/unittests/Support/RISCVISAInfoTest.cpp
Expand Up @@ -604,3 +604,25 @@ TEST(ParseArchString, ZceImplication) {
EXPECT_EQ(ExtsRV64IDZce.count("zcmp"), 1U);
EXPECT_EQ(ExtsRV64IDZce.count("zcmt"), 1U);
}

TEST(isSupportedExtensionWithVersion, AcceptsSingleExtensionWithVersion) {
EXPECT_TRUE(RISCVISAInfo::isSupportedExtensionWithVersion("zbb1p0"));
EXPECT_FALSE(RISCVISAInfo::isSupportedExtensionWithVersion("zbb"));
EXPECT_FALSE(RISCVISAInfo::isSupportedExtensionWithVersion("zfoo1p0"));
EXPECT_FALSE(RISCVISAInfo::isSupportedExtensionWithVersion("zfoo"));
EXPECT_FALSE(RISCVISAInfo::isSupportedExtensionWithVersion(""));
EXPECT_FALSE(RISCVISAInfo::isSupportedExtensionWithVersion("c2p0zbb1p0"));
}

TEST(getTargetFeatureForExtension, RetrieveTargetFeatureFromOneExt) {
EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension("zbb"), "zbb");
EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension("zicond1p0"),
"experimental-zicond");
EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension("zicond"),
"experimental-zicond");
EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension("zihintntl1234p4321"),
"");
EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension("zfoo"), "");
EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension(""), "");
EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension("zbbzihintntl"), "");
}

0 comments on commit 4b60e1e

Please sign in to comment.