Skip to content

Commit

Permalink
[RISCV] Don't use std::vector<std::string> for split extensions in RI…
Browse files Browse the repository at this point in the history
…SCVISAInfo::parseArchString. NFC (#91538)

We can use a SmallVector<StringRef>.

Adjust the code so we check for empty strings in the loop instead of
making a copy of the vector returned from StringRef::split.

This overlaps with #91532 which also removed the std::vector, but
that PR may be more controversial.
  • Loading branch information
topperc authored May 9, 2024
1 parent 96568f3 commit 1aaab33
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions llvm/lib/TargetParser/RISCVISAInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,24 +500,6 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
return std::move(ISAInfo);
}

static Error splitExtsByUnderscore(StringRef Exts,
std::vector<std::string> &SplitExts) {
SmallVector<StringRef, 8> Split;
if (Exts.empty())
return Error::success();

Exts.split(Split, "_");

for (auto Ext : Split) {
if (Ext.empty())
return createStringError(errc::invalid_argument,
"extension name missing after separator '_'");

SplitExts.push_back(Ext.str());
}
return Error::success();
}

static Error processMultiLetterExtension(
StringRef RawExt,
MapVector<std::string, RISCVISAUtils::ExtensionVersion,
Expand Down Expand Up @@ -714,20 +696,25 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
Exts = Exts.drop_front(ConsumeLength);
Exts.consume_front("_");

std::vector<std::string> SplitExts;
if (auto E = splitExtsByUnderscore(Exts, SplitExts))
return std::move(E);
SmallVector<StringRef, 8> SplitExts;
// Only split if the string is not empty. Otherwise the split will push an
// empty string into the vector.
if (!Exts.empty())
Exts.split(SplitExts, '_');

for (auto Ext : SplitExts) {
if (Ext.empty())
return createStringError(errc::invalid_argument,
"extension name missing after separator '_'");

for (auto &Ext : SplitExts) {
StringRef CurrExt = Ext;
while (!CurrExt.empty()) {
if (RISCVISAUtils::AllStdExts.contains(CurrExt.front())) {
do {
if (RISCVISAUtils::AllStdExts.contains(Ext.front())) {
if (auto E = processSingleLetterExtension(
CurrExt, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
Ext, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
ExperimentalExtensionVersionCheck))
return std::move(E);
} else if (CurrExt.front() == 'z' || CurrExt.front() == 's' ||
CurrExt.front() == 'x') {
} else if (Ext.front() == 'z' || Ext.front() == 's' ||
Ext.front() == 'x') {
// Handle other types of extensions other than the standard
// general purpose and standard user-level extensions.
// Parse the ISA string containing non-standard user-level
Expand All @@ -737,7 +724,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
// version number (major, minor) and are separated by a single
// underscore '_'. We do not enforce a canonical order for them.
if (auto E = processMultiLetterExtension(
CurrExt, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
Ext, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
ExperimentalExtensionVersionCheck))
return std::move(E);
// Multi-letter extension must be seperate following extension with
Expand All @@ -747,9 +734,9 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
// FIXME: Could it be ignored by IgnoreUnknown?
return createStringError(errc::invalid_argument,
"invalid standard user-level extension '" +
Twine(CurrExt.front()) + "'");
Twine(Ext.front()) + "'");
}
}
} while (!Ext.empty());
}

// Check all Extensions are supported.
Expand Down

0 comments on commit 1aaab33

Please sign in to comment.