diff --git a/llvm/include/llvm/Support/RISCVISAInfo.h b/llvm/include/llvm/Support/RISCVISAInfo.h --- a/llvm/include/llvm/Support/RISCVISAInfo.h +++ b/llvm/include/llvm/Support/RISCVISAInfo.h @@ -91,6 +91,7 @@ static llvm::Expected> postProcessAndChecking(std::unique_ptr &&ISAInfo); static std::string getTargetFeatureForExtension(StringRef Ext); + static unsigned getExtensionSerial(StringRef ExtName); private: RISCVISAInfo(unsigned XLen) diff --git a/llvm/lib/Support/RISCVISAInfo.cpp b/llvm/lib/Support/RISCVISAInfo.cpp --- a/llvm/lib/Support/RISCVISAInfo.cpp +++ b/llvm/lib/Support/RISCVISAInfo.cpp @@ -1290,3 +1290,22 @@ return isExperimentalExtension(Name) ? "experimental-" + Name.str() : Name.str(); } + + + +unsigned RISCVISAInfo::getExtensionSerial(StringRef ExtName) { + verifyTables(); + + unsigned Offset = 1; + if (stripExperimentalPrefix(ExtName)) + Offset += sizeof(SupportedExtensions) / sizeof(SupportedExtensions[0]); + + for (auto ExtInfo : {ArrayRef(SupportedExtensions), + ArrayRef(SupportedExperimentalExtensions)}) { + auto I = llvm::lower_bound(ExtInfo, ExtName, LessExtName()); + if (I != ExtInfo.end() && I->Name == ExtName) + return (I - ExtInfo.begin()) + Offset; + } + + return 0; +} diff --git a/llvm/unittests/Support/RISCVISAInfoTest.cpp b/llvm/unittests/Support/RISCVISAInfoTest.cpp --- a/llvm/unittests/Support/RISCVISAInfoTest.cpp +++ b/llvm/unittests/Support/RISCVISAInfoTest.cpp @@ -626,3 +626,10 @@ EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension(""), ""); EXPECT_EQ(RISCVISAInfo::getTargetFeatureForExtension("zbbzihintntl"), ""); } + +TEST(getExtensionSerial, RetrieveAUniqueNumberForExt) { + EXPECT_EQ(RISCVISAInfo::getExtensionSerial("f"), 5U); + EXPECT_EQ(RISCVISAInfo::getExtensionSerial("a"), 1U); + EXPECT_EQ(RISCVISAInfo::getExtensionSerial("foo"), 0U); + EXPECT_EQ(RISCVISAInfo::getExtensionSerial("experimental-zicond"), 97U); +}