Skip to content

Commit 1aaab33

Browse files
authored
[RISCV] Don't use std::vector<std::string> for split extensions in RISCVISAInfo::parseArchString. NFC (llvm#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 llvm#91532 which also removed the std::vector, but that PR may be more controversial.
1 parent 96568f3 commit 1aaab33

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

llvm/lib/TargetParser/RISCVISAInfo.cpp

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -500,24 +500,6 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
500500
return std::move(ISAInfo);
501501
}
502502

503-
static Error splitExtsByUnderscore(StringRef Exts,
504-
std::vector<std::string> &SplitExts) {
505-
SmallVector<StringRef, 8> Split;
506-
if (Exts.empty())
507-
return Error::success();
508-
509-
Exts.split(Split, "_");
510-
511-
for (auto Ext : Split) {
512-
if (Ext.empty())
513-
return createStringError(errc::invalid_argument,
514-
"extension name missing after separator '_'");
515-
516-
SplitExts.push_back(Ext.str());
517-
}
518-
return Error::success();
519-
}
520-
521503
static Error processMultiLetterExtension(
522504
StringRef RawExt,
523505
MapVector<std::string, RISCVISAUtils::ExtensionVersion,
@@ -714,20 +696,25 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
714696
Exts = Exts.drop_front(ConsumeLength);
715697
Exts.consume_front("_");
716698

717-
std::vector<std::string> SplitExts;
718-
if (auto E = splitExtsByUnderscore(Exts, SplitExts))
719-
return std::move(E);
699+
SmallVector<StringRef, 8> SplitExts;
700+
// Only split if the string is not empty. Otherwise the split will push an
701+
// empty string into the vector.
702+
if (!Exts.empty())
703+
Exts.split(SplitExts, '_');
704+
705+
for (auto Ext : SplitExts) {
706+
if (Ext.empty())
707+
return createStringError(errc::invalid_argument,
708+
"extension name missing after separator '_'");
720709

721-
for (auto &Ext : SplitExts) {
722-
StringRef CurrExt = Ext;
723-
while (!CurrExt.empty()) {
724-
if (RISCVISAUtils::AllStdExts.contains(CurrExt.front())) {
710+
do {
711+
if (RISCVISAUtils::AllStdExts.contains(Ext.front())) {
725712
if (auto E = processSingleLetterExtension(
726-
CurrExt, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
713+
Ext, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
727714
ExperimentalExtensionVersionCheck))
728715
return std::move(E);
729-
} else if (CurrExt.front() == 'z' || CurrExt.front() == 's' ||
730-
CurrExt.front() == 'x') {
716+
} else if (Ext.front() == 'z' || Ext.front() == 's' ||
717+
Ext.front() == 'x') {
731718
// Handle other types of extensions other than the standard
732719
// general purpose and standard user-level extensions.
733720
// Parse the ISA string containing non-standard user-level
@@ -737,7 +724,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
737724
// version number (major, minor) and are separated by a single
738725
// underscore '_'. We do not enforce a canonical order for them.
739726
if (auto E = processMultiLetterExtension(
740-
CurrExt, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
727+
Ext, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
741728
ExperimentalExtensionVersionCheck))
742729
return std::move(E);
743730
// Multi-letter extension must be seperate following extension with
@@ -747,9 +734,9 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
747734
// FIXME: Could it be ignored by IgnoreUnknown?
748735
return createStringError(errc::invalid_argument,
749736
"invalid standard user-level extension '" +
750-
Twine(CurrExt.front()) + "'");
737+
Twine(Ext.front()) + "'");
751738
}
752-
}
739+
} while (!Ext.empty());
753740
}
754741

755742
// Check all Extensions are supported.

0 commit comments

Comments
 (0)