Skip to content

Commit

Permalink
[RISCV] Print a better error message when a rv32 CPU is used on rv64 …
Browse files Browse the repository at this point in the history
…and vice versa.

Instead of rejecting the CPU outright with no information, try
to diagnose that it doesn't match the triple.

Differential Revision: https://reviews.llvm.org/D147986
  • Loading branch information
topperc committed Apr 11, 2023
1 parent 5e2d8a3 commit 88d6311
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def err_drv_invalid_arch_name : Error<
"invalid arch name '%0'">;
def err_drv_invalid_riscv_arch_name : Error<
"invalid arch name '%0', %1">;
def err_drv_invalid_riscv_cpu_name_for_target : Error<
"cpu '%0' does not support rv%select{32|64}1">;
def warn_drv_invalid_arch_name_with_suggestion : Warning<
"ignoring invalid /arch: argument '%0'; for %select{64|32}1-bit expected one of %2">,
InGroup<UnusedCommandLineArgument>;
Expand Down
18 changes: 13 additions & 5 deletions clang/lib/Driver/ToolChains/Arch/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,20 @@ static bool getArchFeatures(const Driver &D, StringRef Arch,
}

// Get features except standard extension feature
static bool getRISCFeaturesFromMcpu(const llvm::Triple &Triple, StringRef Mcpu,
static void getRISCFeaturesFromMcpu(const Driver &D, const Arg *A,
const llvm::Triple &Triple,
StringRef Mcpu,
std::vector<StringRef> &Features) {
bool Is64Bit = Triple.isRISCV64();
llvm::RISCV::CPUKind CPUKind = llvm::RISCV::parseCPUKind(Mcpu);
return llvm::RISCV::checkCPUKind(CPUKind, Is64Bit);
if (!llvm::RISCV::checkCPUKind(CPUKind, Is64Bit)) {
// Try inverting Is64Bit in case the CPU is valid, but for the wrong target.
if (llvm::RISCV::checkCPUKind(CPUKind, !Is64Bit))
D.Diag(clang::diag::err_drv_invalid_riscv_cpu_name_for_target) << Mcpu << Is64Bit;
else
D.Diag(clang::diag::err_drv_unsupported_option_argument)
<< A->getSpelling() << Mcpu;
}
}

void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
Expand All @@ -70,9 +79,8 @@ void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
StringRef CPU = A->getValue();
if (CPU == "native")
CPU = llvm::sys::getHostCPUName();
if (!getRISCFeaturesFromMcpu(Triple, CPU, Features))
D.Diag(clang::diag::err_drv_unsupported_option_argument)
<< A->getSpelling() << CPU;

getRISCFeaturesFromMcpu(D, A, Triple, CPU, Features);
}

// Handle features corresponding to "-ffixed-X" options
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Driver/riscv-cpus.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
// FAIL-MCPU-NAME: error: unsupported argument 'generic-rv321' to option '-mcpu='

// RUN: %clang --target=riscv32 -### -c %s 2>&1 -mcpu=generic-rv32 -march=rv64i | FileCheck -check-prefix=MISMATCH-ARCH %s
// MISMATCH-ARCH: error: unsupported argument 'generic-rv32' to option '-mcpu='
// MISMATCH-ARCH: cpu 'generic-rv32' does not support rv64

// RUN: %clang --target=riscv32 -### -c %s 2>&1 -mcpu=generic-rv64 | FileCheck -check-prefix=MISMATCH-MCPU %s
// MISMATCH-MCPU: error: unsupported argument 'generic-rv64' to option '-mcpu='
// MISMATCH-MCPU: error: cpu 'generic-rv64' does not support rv32

0 comments on commit 88d6311

Please sign in to comment.