Skip to content

Commit f906e3d

Browse files
committed
[RISCV] Fold processSingleLetterExtension/processMultiLetterExtension into RISCVISAInfo::parseArchString.
The end of both functions was very similar. Merging reduces the duplication. I'm planning to make additional changes to this code soon.
1 parent e55aa02 commit f906e3d

File tree

1 file changed

+42
-92
lines changed

1 file changed

+42
-92
lines changed

llvm/lib/TargetParser/RISCVISAInfo.cpp

Lines changed: 42 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -521,87 +521,6 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
521521
return std::move(ISAInfo);
522522
}
523523

524-
static Error processMultiLetterExtension(
525-
StringRef RawExt,
526-
MapVector<std::string, RISCVISAUtils::ExtensionVersion,
527-
std::map<std::string, unsigned>> &SeenExtMap,
528-
bool IgnoreUnknown, bool EnableExperimentalExtension,
529-
bool ExperimentalExtensionVersionCheck) {
530-
StringRef Type = getExtensionType(RawExt);
531-
StringRef Desc = getExtensionTypeDesc(RawExt);
532-
auto Pos = findLastNonVersionCharacter(RawExt) + 1;
533-
StringRef Name(RawExt.substr(0, Pos));
534-
StringRef Vers(RawExt.substr(Pos));
535-
536-
if (Type.empty()) {
537-
if (IgnoreUnknown)
538-
return Error::success();
539-
return createStringError(errc::invalid_argument,
540-
"invalid extension prefix '" + RawExt + "'");
541-
}
542-
543-
if (!IgnoreUnknown && Name.size() == Type.size())
544-
return createStringError(errc::invalid_argument,
545-
Desc + " name missing after '" + Type + "'");
546-
547-
unsigned Major, Minor, ConsumeLength;
548-
if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
549-
EnableExperimentalExtension,
550-
ExperimentalExtensionVersionCheck)) {
551-
if (IgnoreUnknown) {
552-
consumeError(std::move(E));
553-
return Error::success();
554-
}
555-
return E;
556-
}
557-
558-
// Check if duplicated extension.
559-
if (!IgnoreUnknown && SeenExtMap.contains(Name.str()))
560-
return createStringError(errc::invalid_argument,
561-
"duplicated " + Desc + " '" + Name + "'");
562-
563-
if (IgnoreUnknown && !RISCVISAInfo::isSupportedExtension(Name))
564-
return Error::success();
565-
566-
SeenExtMap[Name.str()] = {Major, Minor};
567-
return Error::success();
568-
}
569-
570-
static Error processSingleLetterExtension(
571-
StringRef &RawExt,
572-
MapVector<std::string, RISCVISAUtils::ExtensionVersion,
573-
std::map<std::string, unsigned>> &SeenExtMap,
574-
bool IgnoreUnknown, bool EnableExperimentalExtension,
575-
bool ExperimentalExtensionVersionCheck) {
576-
unsigned Major, Minor, ConsumeLength;
577-
StringRef Name = RawExt.take_front(1);
578-
RawExt.consume_front(Name);
579-
if (auto E = getExtensionVersion(Name, RawExt, Major, Minor, ConsumeLength,
580-
EnableExperimentalExtension,
581-
ExperimentalExtensionVersionCheck)) {
582-
if (IgnoreUnknown) {
583-
consumeError(std::move(E));
584-
RawExt = RawExt.substr(ConsumeLength);
585-
return Error::success();
586-
}
587-
return E;
588-
}
589-
590-
RawExt = RawExt.substr(ConsumeLength);
591-
592-
// Check if duplicated extension.
593-
if (!IgnoreUnknown && SeenExtMap.contains(Name.str()))
594-
return createStringError(errc::invalid_argument,
595-
"duplicated standard user-level extension '" +
596-
Name + "'");
597-
598-
if (IgnoreUnknown && !RISCVISAInfo::isSupportedExtension(Name))
599-
return Error::success();
600-
601-
SeenExtMap[Name.str()] = {Major, Minor};
602-
return Error::success();
603-
}
604-
605524
llvm::Expected<std::unique_ptr<RISCVISAInfo>>
606525
RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
607526
bool ExperimentalExtensionVersionCheck,
@@ -738,11 +657,12 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
738657
Exts = Exts.slice(Idx, StringRef::npos);
739658

740659
do {
660+
StringRef Name, Vers, Desc;
741661
if (RISCVISAUtils::AllStdExts.contains(Ext.front())) {
742-
if (auto E = processSingleLetterExtension(
743-
Ext, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
744-
ExperimentalExtensionVersionCheck))
745-
return std::move(E);
662+
Name = Ext.take_front(1);
663+
Ext = Ext.drop_front();
664+
Vers = Ext;
665+
Desc = "standard user-level extension";
746666
} else if (Ext.front() == 'z' || Ext.front() == 's' ||
747667
Ext.front() == 'x') {
748668
// Handle other types of extensions other than the standard
@@ -753,19 +673,49 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
753673
// These extensions start with 'z', 's', 'x' prefixes, might have a
754674
// version number (major, minor) and are separated by a single
755675
// underscore '_'. We do not enforce a canonical order for them.
756-
if (auto E = processMultiLetterExtension(
757-
Ext, SeenExtMap, IgnoreUnknown, EnableExperimentalExtension,
758-
ExperimentalExtensionVersionCheck))
759-
return std::move(E);
760-
// Multi-letter extension must be separate following extension with
761-
// underscore
762-
break;
676+
StringRef Type = getExtensionType(Ext);
677+
Desc = getExtensionTypeDesc(Ext);
678+
auto Pos = findLastNonVersionCharacter(Ext) + 1;
679+
Name = Ext.substr(0, Pos);
680+
Vers = Ext.substr(Pos);
681+
Ext = StringRef();
682+
683+
assert(!Type.empty() && "Empty type?");
684+
if (!IgnoreUnknown && Name.size() == Type.size())
685+
return createStringError(errc::invalid_argument,
686+
Desc + " name missing after '" + Type + "'");
763687
} else {
764688
// FIXME: Could it be ignored by IgnoreUnknown?
765689
return createStringError(errc::invalid_argument,
766690
"invalid standard user-level extension '" +
767691
Twine(Ext.front()) + "'");
768692
}
693+
694+
unsigned Major, Minor, ConsumeLength;
695+
if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
696+
EnableExperimentalExtension,
697+
ExperimentalExtensionVersionCheck)) {
698+
if (IgnoreUnknown) {
699+
consumeError(std::move(E));
700+
if (Name.size() == 1)
701+
Ext = Ext.substr(ConsumeLength);
702+
continue;
703+
}
704+
return E;
705+
}
706+
707+
if (Name.size() == 1)
708+
Ext = Ext.substr(ConsumeLength);
709+
710+
// Check if duplicated extension.
711+
if (!IgnoreUnknown && SeenExtMap.contains(Name.str()))
712+
return createStringError(errc::invalid_argument,
713+
"duplicated " + Desc + " '" + Name + "'");
714+
715+
if (IgnoreUnknown && !RISCVISAInfo::isSupportedExtension(Name))
716+
continue;
717+
718+
SeenExtMap[Name.str()] = {Major, Minor};
769719
} while (!Ext.empty());
770720
}
771721

0 commit comments

Comments
 (0)