diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -29,6 +29,8 @@ "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; diff --git a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp --- a/clang/lib/Driver/ToolChains/Arch/RISCV.cpp +++ b/clang/lib/Driver/ToolChains/Arch/RISCV.cpp @@ -49,14 +49,20 @@ } // Get features except standard extension feature -static void getRISCFeaturesFromMcpu(const Driver &D, const llvm::Triple &Triple, +static void getRISCFeaturesFromMcpu(const Driver &D, const Arg *A, + const llvm::Triple &Triple, StringRef Mcpu, std::vector &Features) { bool Is64Bit = Triple.isRISCV64(); llvm::RISCV::CPUKind CPUKind = llvm::RISCV::parseCPUKind(Mcpu); - if (!llvm::RISCV::checkCPUKind(CPUKind, Is64Bit)) - D.Diag(clang::diag::err_drv_unsupported_option_argument) - << A->getSpelling() << CPU; + 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, @@ -74,7 +80,7 @@ if (CPU == "native") CPU = llvm::sys::getHostCPUName(); - getRISCFeaturesFromMcpu(D, Triple, CPU, Features); + getRISCFeaturesFromMcpu(D, A, Triple, CPU, Features); } // Handle features corresponding to "-ffixed-X" options diff --git a/clang/test/Driver/riscv-cpus.c b/clang/test/Driver/riscv-cpus.c --- a/clang/test/Driver/riscv-cpus.c +++ b/clang/test/Driver/riscv-cpus.c @@ -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