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 @@ -82,6 +82,9 @@ def err_drv_undetermined_gpu_arch : Error< "cannot determine %0 architecture: %1; consider passing it via " "'%2'">; +def warn_drv_multi_gpu_arch : Warning< + "multiple %0 architectures are detected: %1; only the first one is used for " + "'%2'">, InGroup; def err_drv_cuda_version_unsupported : Error< "GPU arch %0 is supported by CUDA versions between %1 and %2 (inclusive), " "but installation at %3 is %4; use '--cuda-path' to specify a different CUDA " diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -1336,6 +1336,9 @@ // Warning about mixed HIP and OpenMP compilation / target offloading. def HIPOpenMPOffloading: DiagGroup<"hip-omp-target-directives">; +// Warning about multiple GPUs are detected. +def MultiGPU: DiagGroup<"multi-gpu">; + // Warnings which cause linking of the runtime libraries like // libc and the CRT to be skipped. def AVRRtlibLinkingQuirks : DiagGroup<"avr-rtlib-linking-quirks">; diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp --- a/clang/lib/Driver/ToolChains/AMDGPU.cpp +++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp @@ -635,6 +635,27 @@ for (Arg *A : Args) DAL->append(A); + // Replace -mcpu=native with detected GPU. + Arg *LastMCPUArg = DAL->getLastArg(options::OPT_mcpu_EQ); + if (LastMCPUArg && StringRef(LastMCPUArg->getValue()) == "native") { + DAL->eraseArg(options::OPT_mcpu_EQ); + auto GPUsOrErr = getSystemGPUArchs(Args); + if (!GPUsOrErr) { + getDriver().Diag(diag::err_drv_undetermined_gpu_arch) + << llvm::Triple::getArchTypeName(getArch()) + << llvm::toString(GPUsOrErr.takeError()) << "-mcpu"; + } else { + auto &GPUs = *GPUsOrErr; + if (GPUs.size() > 1) { + getDriver().Diag(diag::warn_drv_multi_gpu_arch) + << llvm::Triple::getArchTypeName(getArch()) + << llvm::join(GPUs, ", ") << "-mcpu"; + } + DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), + Args.MakeArgString(GPUs.front())); + } + } + checkTargetID(*DAL); if (!Args.getLastArgValue(options::OPT_x).equals("cl"))