Skip to content

Commit 09f4b06

Browse files
authored
[RISCV] Refactor profile selection in RISCVISAInfo::parseArchString. (#90700)
Instead of hardcoding the 4 current profile prefixes, treat profile selection as a fallback if we don't find "rv32" or "rv64". Update the error message accordingly.
1 parent 7396ab1 commit 09f4b06

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

clang/test/Driver/riscv-arch.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
// RUN: not %clang --target=riscv32-unknown-elf -march=unknown -### %s \
205205
// RUN: -fsyntax-only 2>&1 | FileCheck -check-prefix=RV32-STR %s
206206
// RV32-STR: error: invalid arch name 'unknown',
207-
// RV32-STR: string must begin with rv32{i,e,g} or rv64{i,e,g}
207+
// RV32-STR: string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported profile name
208208

209209
// RUN: not %clang --target=riscv32-unknown-elf -march=rv32q -### %s \
210210
// RUN: -fsyntax-only 2>&1 | FileCheck -check-prefix=RV32-LETTER %s

clang/test/Driver/riscv-profiles.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@
318318
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zkt"
319319

320320
// RUN: not %clang --target=riscv64 -### -c %s 2>&1 -march=rva19u64_zfa | FileCheck -check-prefix=INVALID-PROFILE %s
321-
// INVALID-PROFILE: error: invalid arch name 'rva19u64_zfa', unsupported profile
321+
// INVALID-PROFILE: error: invalid arch name 'rva19u64_zfa', string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported profile name
322322

323323
// RUN: not %clang --target=riscv64 -### -c %s 2>&1 -march=rva22u64zfa | FileCheck -check-prefix=INVALID-ADDITIONAL %s
324324
// INVALID-ADDITIONAL: error: invalid arch name 'rva22u64zfa', additional extensions must be after separator '_'

llvm/lib/TargetParser/RISCVISAInfo.cpp

+22-23
Original file line numberDiff line numberDiff line change
@@ -592,40 +592,39 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
592592
return createStringError(errc::invalid_argument,
593593
"string must be lowercase");
594594

595-
if (Arch.starts_with("rvi") || Arch.starts_with("rva") ||
596-
Arch.starts_with("rvb") || Arch.starts_with("rvm")) {
595+
// ISA string must begin with rv32, rv64, or a profile.
596+
unsigned XLen = 0;
597+
if (Arch.consume_front("rv32")) {
598+
XLen = 32;
599+
} else if (Arch.consume_front("rv64")) {
600+
XLen = 64;
601+
} else {
602+
// Try parsing as a profile.
597603
const auto *FoundProfile =
598604
llvm::find_if(SupportedProfiles, [Arch](const RISCVProfile &Profile) {
599605
return Arch.starts_with(Profile.Name);
600606
});
601607

602-
if (FoundProfile == std::end(SupportedProfiles))
603-
return createStringError(errc::invalid_argument, "unsupported profile");
604-
605-
std::string NewArch = FoundProfile->MArch.str();
606-
StringRef ArchWithoutProfile = Arch.substr(FoundProfile->Name.size());
607-
if (!ArchWithoutProfile.empty()) {
608-
if (!ArchWithoutProfile.starts_with("_"))
609-
return createStringError(
610-
errc::invalid_argument,
611-
"additional extensions must be after separator '_'");
612-
NewArch += ArchWithoutProfile.str();
608+
if (FoundProfile != std::end(SupportedProfiles)) {
609+
std::string NewArch = FoundProfile->MArch.str();
610+
StringRef ArchWithoutProfile = Arch.drop_front(FoundProfile->Name.size());
611+
if (!ArchWithoutProfile.empty()) {
612+
if (ArchWithoutProfile.front() != '_')
613+
return createStringError(
614+
errc::invalid_argument,
615+
"additional extensions must be after separator '_'");
616+
NewArch += ArchWithoutProfile.str();
617+
}
618+
return parseArchString(NewArch, EnableExperimentalExtension,
619+
ExperimentalExtensionVersionCheck, IgnoreUnknown);
613620
}
614-
return parseArchString(NewArch, EnableExperimentalExtension,
615-
ExperimentalExtensionVersionCheck, IgnoreUnknown);
616621
}
617622

618-
// ISA string must begin with rv32 or rv64.
619-
unsigned XLen = 0;
620-
if (Arch.consume_front("rv32"))
621-
XLen = 32;
622-
else if (Arch.consume_front("rv64"))
623-
XLen = 64;
624-
625623
if (XLen == 0 || Arch.empty())
626624
return createStringError(
627625
errc::invalid_argument,
628-
"string must begin with rv32{i,e,g} or rv64{i,e,g}");
626+
"string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported "
627+
"profile name");
629628

630629
std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
631630
MapVector<std::string, RISCVISAUtils::ExtensionVersion,

llvm/test/MC/RISCV/invalid-attribute.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# CHECK: [[@LINE-1]]:12: error: attribute name not recognised: unknown
1212

1313
.attribute arch, "foo"
14-
# CHECK: [[@LINE-1]]:18: error: invalid arch name 'foo', string must begin with rv32{i,e,g} or rv64{i,e,g}
14+
# CHECK: [[@LINE-1]]:18: error: invalid arch name 'foo', string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported profile name{{$}}
1515

1616
.attribute arch, "rv32i2p1_y2p0"
1717
# CHECK: [[@LINE-1]]:18: error: invalid arch name 'rv32i2p1_y2p0', invalid standard user-level extension 'y'

llvm/unittests/TargetParser/RISCVISAInfoTest.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ TEST(ParseArchString, RejectsUpperCase) {
118118
TEST(ParseArchString, RejectsInvalidBaseISA) {
119119
for (StringRef Input : {"rv32", "rv64", "rv65i"}) {
120120
EXPECT_EQ(toString(RISCVISAInfo::parseArchString(Input, true).takeError()),
121-
"string must begin with rv32{i,e,g} or rv64{i,e,g}");
121+
"string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported "
122+
"profile name");
122123
}
123124

124125
for (StringRef Input : {"rv32j", "rv32_i"}) {
@@ -133,7 +134,8 @@ TEST(ParseArchString, RejectsInvalidBaseISA) {
133134
TEST(ParseArchString, RejectsUnsupportedBaseISA) {
134135
for (StringRef Input : {"rv128i", "rv128g"}) {
135136
EXPECT_EQ(toString(RISCVISAInfo::parseArchString(Input, true).takeError()),
136-
"string must begin with rv32{i,e,g} or rv64{i,e,g}");
137+
"string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported "
138+
"profile name");
137139
}
138140
}
139141

0 commit comments

Comments
 (0)