diff --git a/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp b/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp --- a/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp +++ b/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp @@ -29,32 +29,6 @@ using namespace clang; using namespace llvm::opt; -namespace { - -static bool checkSystemForAMDGPU(const ArgList &Args, const AMDGPUToolChain &TC, - std::string &GPUArch) { - auto CheckError = [&](llvm::Error Err) -> bool { - std::string ErrMsg = - llvm::formatv("{0}", llvm::fmt_consume(std::move(Err))); - TC.getDriver().Diag(diag::err_drv_undetermined_gpu_arch) - << llvm::Triple::getArchTypeName(TC.getArch()) << ErrMsg << "-march"; - return false; - }; - - auto ArchsOrErr = TC.getSystemGPUArchs(Args); - if (!ArchsOrErr) - return CheckError(ArchsOrErr.takeError()); - - if (ArchsOrErr->size() > 1) - if (!llvm::all_equal(*ArchsOrErr)) - return CheckError(llvm::createStringError( - std::error_code(), "Multiple AMD GPUs found with different archs")); - - GPUArch = ArchsOrErr->front(); - return true; -} -} // namespace - AMDGPUOpenMPToolChain::AMDGPUOpenMPToolChain(const Driver &D, const llvm::Triple &Triple, const ToolChain &HostTC, @@ -70,11 +44,8 @@ Action::OffloadKind DeviceOffloadingKind) const { HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind); - std::string GPUArch = DriverArgs.getLastArgValue(options::OPT_march_EQ).str(); - if (GPUArch.empty()) { - if (!checkSystemForAMDGPU(DriverArgs, *this, GPUArch)) - return; - } + StringRef GPUArch = DriverArgs.getLastArgValue(options::OPT_march_EQ); + assert(!GPUArch.empty() && "Must have an explicit GPU arch."); assert(DeviceOffloadingKind == Action::OFK_OpenMP && "Only OpenMP offloading kinds are supported."); @@ -115,9 +86,19 @@ DAL->append(A); if (!DAL->hasArg(options::OPT_march_EQ)) { - std::string Arch = BoundArch.str(); - if (BoundArch.empty()) - checkSystemForAMDGPU(Args, *this, Arch); + StringRef Arch = BoundArch; + if (Arch.empty()) { + auto ArchsOrErr = getSystemGPUArchs(Args); + if (!ArchsOrErr) { + std::string ErrMsg = + llvm::formatv("{0}", llvm::fmt_consume(ArchsOrErr.takeError())); + getDriver().Diag(diag::err_drv_undetermined_gpu_arch) + << llvm::Triple::getArchTypeName(getArch()) << ErrMsg << "-march"; + Arch = CudaArchToString(CudaArch::HIPDefault); + } else { + Arch = Args.MakeArgString(ArchsOrErr->front()); + } + } DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), Arch); } diff --git a/clang/test/Driver/amdgpu-openmp-system-arch-fail.c b/clang/test/Driver/amdgpu-openmp-system-arch-fail.c --- a/clang/test/Driver/amdgpu-openmp-system-arch-fail.c +++ b/clang/test/Driver/amdgpu-openmp-system-arch-fail.c @@ -17,11 +17,6 @@ // RUN: | FileCheck %s --check-prefix=NO-OUTPUT-ERROR // NO-OUTPUT-ERROR: error: cannot determine amdgcn architecture{{.*}}; consider passing it via '-march' -// case when amdgpu_arch returns multiple gpus but all are different -// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp -fopenmp-targets=amdgcn-amd-amdhsa -nogpulib --amdgpu-arch-tool=%t/amdgpu_arch_different %s 2>&1 \ -// RUN: | FileCheck %s --check-prefix=MULTIPLE-OUTPUT-ERROR -// MULTIPLE-OUTPUT-ERROR: error: cannot determine amdgcn architecture: Multiple AMD GPUs found with different archs; consider passing it via '-march' - // case when amdgpu_arch does not return anything with successful execution // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp -fopenmp-targets=amdgcn-amd-amdhsa -nogpulib --amdgpu-arch-tool=%t/amdgpu_arch_empty %s 2>&1 \ // RUN: | FileCheck %s --check-prefix=EMPTY-OUTPUT