Skip to content

Commit 8fd011e

Browse files
authored
[RISCV] Add getFeaturesForCPU function support (#83269)
This function parse the cpu and return it's supported features placed in EnabledFeatures. It is same as the one in X86TargetParser and also is used in IREE.
1 parent 4551f53 commit 8fd011e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

llvm/include/llvm/TargetParser/RISCVTargetParser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ namespace RISCV {
2525
// We use 64 bits as the known part in the scalable vector types.
2626
static constexpr unsigned RVVBitsPerBlock = 64;
2727

28+
void getFeaturesForCPU(StringRef CPU,
29+
SmallVectorImpl<std::string> &EnabledFeatures,
30+
bool NeedPlus = false);
2831
bool parseCPU(StringRef CPU, bool IsRV64);
2932
bool parseTuneCPU(StringRef CPU, bool IsRV64);
3033
StringRef getMArchFromMcpu(StringRef CPU);

llvm/lib/TargetParser/RISCVTargetParser.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/TargetParser/RISCVTargetParser.h"
1515
#include "llvm/ADT/SmallVector.h"
1616
#include "llvm/ADT/StringSwitch.h"
17+
#include "llvm/Support/RISCVISAInfo.h"
1718
#include "llvm/TargetParser/Triple.h"
1819

1920
namespace llvm {
@@ -95,5 +96,28 @@ void fillValidTuneCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64) {
9596
#include "llvm/TargetParser/RISCVTargetParserDef.inc"
9697
}
9798

99+
// This function is currently used by IREE, so it's not dead code.
100+
void getFeaturesForCPU(StringRef CPU,
101+
SmallVectorImpl<std::string> &EnabledFeatures,
102+
bool NeedPlus) {
103+
StringRef MarchFromCPU = llvm::RISCV::getMArchFromMcpu(CPU);
104+
if (MarchFromCPU == "")
105+
return;
106+
107+
EnabledFeatures.clear();
108+
auto RII = RISCVISAInfo::parseArchString(
109+
MarchFromCPU, /* EnableExperimentalExtension */ true);
110+
111+
if (llvm::errorToBool(RII.takeError()))
112+
return;
113+
114+
std::vector<std::string> FeatStrings =
115+
(*RII)->toFeatures(/* AddAllExtensions */ false);
116+
for (const auto &F : FeatStrings)
117+
if (NeedPlus)
118+
EnabledFeatures.push_back(F);
119+
else
120+
EnabledFeatures.push_back(F.substr(1));
121+
}
98122
} // namespace RISCV
99123
} // namespace llvm

0 commit comments

Comments
 (0)