Skip to content

Commit

Permalink
[RISCV] Refactor profile selection in RISCVISAInfo::parseArchString. (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
topperc committed May 1, 2024
1 parent 7396ab1 commit 09f4b06
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion clang/test/Driver/riscv-arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
// RUN: not %clang --target=riscv32-unknown-elf -march=unknown -### %s \
// RUN: -fsyntax-only 2>&1 | FileCheck -check-prefix=RV32-STR %s
// RV32-STR: error: invalid arch name 'unknown',
// RV32-STR: string must begin with rv32{i,e,g} or rv64{i,e,g}
// RV32-STR: string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported profile name

// RUN: not %clang --target=riscv32-unknown-elf -march=rv32q -### %s \
// RUN: -fsyntax-only 2>&1 | FileCheck -check-prefix=RV32-LETTER %s
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Driver/riscv-profiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
// PROFILE-WITH-ADDITIONAL: "-target-feature" "+zkt"

// RUN: not %clang --target=riscv64 -### -c %s 2>&1 -march=rva19u64_zfa | FileCheck -check-prefix=INVALID-PROFILE %s
// INVALID-PROFILE: error: invalid arch name 'rva19u64_zfa', unsupported profile
// 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

// RUN: not %clang --target=riscv64 -### -c %s 2>&1 -march=rva22u64zfa | FileCheck -check-prefix=INVALID-ADDITIONAL %s
// INVALID-ADDITIONAL: error: invalid arch name 'rva22u64zfa', additional extensions must be after separator '_'
45 changes: 22 additions & 23 deletions llvm/lib/TargetParser/RISCVISAInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,40 +592,39 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
return createStringError(errc::invalid_argument,
"string must be lowercase");

if (Arch.starts_with("rvi") || Arch.starts_with("rva") ||
Arch.starts_with("rvb") || Arch.starts_with("rvm")) {
// ISA string must begin with rv32, rv64, or a profile.
unsigned XLen = 0;
if (Arch.consume_front("rv32")) {
XLen = 32;
} else if (Arch.consume_front("rv64")) {
XLen = 64;
} else {
// Try parsing as a profile.
const auto *FoundProfile =
llvm::find_if(SupportedProfiles, [Arch](const RISCVProfile &Profile) {
return Arch.starts_with(Profile.Name);
});

if (FoundProfile == std::end(SupportedProfiles))
return createStringError(errc::invalid_argument, "unsupported profile");

std::string NewArch = FoundProfile->MArch.str();
StringRef ArchWithoutProfile = Arch.substr(FoundProfile->Name.size());
if (!ArchWithoutProfile.empty()) {
if (!ArchWithoutProfile.starts_with("_"))
return createStringError(
errc::invalid_argument,
"additional extensions must be after separator '_'");
NewArch += ArchWithoutProfile.str();
if (FoundProfile != std::end(SupportedProfiles)) {
std::string NewArch = FoundProfile->MArch.str();
StringRef ArchWithoutProfile = Arch.drop_front(FoundProfile->Name.size());
if (!ArchWithoutProfile.empty()) {
if (ArchWithoutProfile.front() != '_')
return createStringError(
errc::invalid_argument,
"additional extensions must be after separator '_'");
NewArch += ArchWithoutProfile.str();
}
return parseArchString(NewArch, EnableExperimentalExtension,
ExperimentalExtensionVersionCheck, IgnoreUnknown);
}
return parseArchString(NewArch, EnableExperimentalExtension,
ExperimentalExtensionVersionCheck, IgnoreUnknown);
}

// ISA string must begin with rv32 or rv64.
unsigned XLen = 0;
if (Arch.consume_front("rv32"))
XLen = 32;
else if (Arch.consume_front("rv64"))
XLen = 64;

if (XLen == 0 || Arch.empty())
return createStringError(
errc::invalid_argument,
"string must begin with rv32{i,e,g} or rv64{i,e,g}");
"string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported "
"profile name");

std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
MapVector<std::string, RISCVISAUtils::ExtensionVersion,
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/MC/RISCV/invalid-attribute.s
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# CHECK: [[@LINE-1]]:12: error: attribute name not recognised: unknown

.attribute arch, "foo"
# CHECK: [[@LINE-1]]:18: error: invalid arch name 'foo', string must begin with rv32{i,e,g} or rv64{i,e,g}
# 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{{$}}

.attribute arch, "rv32i2p1_y2p0"
# CHECK: [[@LINE-1]]:18: error: invalid arch name 'rv32i2p1_y2p0', invalid standard user-level extension 'y'
Expand Down
6 changes: 4 additions & 2 deletions llvm/unittests/TargetParser/RISCVISAInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ TEST(ParseArchString, RejectsUpperCase) {
TEST(ParseArchString, RejectsInvalidBaseISA) {
for (StringRef Input : {"rv32", "rv64", "rv65i"}) {
EXPECT_EQ(toString(RISCVISAInfo::parseArchString(Input, true).takeError()),
"string must begin with rv32{i,e,g} or rv64{i,e,g}");
"string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported "
"profile name");
}

for (StringRef Input : {"rv32j", "rv32_i"}) {
Expand All @@ -133,7 +134,8 @@ TEST(ParseArchString, RejectsInvalidBaseISA) {
TEST(ParseArchString, RejectsUnsupportedBaseISA) {
for (StringRef Input : {"rv128i", "rv128g"}) {
EXPECT_EQ(toString(RISCVISAInfo::parseArchString(Input, true).takeError()),
"string must begin with rv32{i,e,g} or rv64{i,e,g}");
"string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported "
"profile name");
}
}

Expand Down

0 comments on commit 09f4b06

Please sign in to comment.