[clang] 91b9bde - [AMDGPU] Support -mcpu=native for OpenCL
Yaxun Liu via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 13 13:22:19 PDT 2023
Author: Yaxun (Sam) Liu
Date: 2023-07-13T16:21:35-04:00
New Revision: 91b9bdeb92562d3030d834489185a10aa8e241ef
URL: https://github.com/llvm/llvm-project/commit/91b9bdeb92562d3030d834489185a10aa8e241ef
DIFF: https://github.com/llvm/llvm-project/commit/91b9bdeb92562d3030d834489185a10aa8e241ef.diff
LOG: [AMDGPU] Support -mcpu=native for OpenCL
When -mcpu=native is specified, try detecting GPU
on the system by using amdgpu-arch tool. If it
fails to detect GPU, emit an error about GPU
not detected. If multiple GPUs are detected,
use the first GPU and emit a warning.
Reviewed by: Matt Arsenault, Fangrui Song
Differential Revision: https://reviews.llvm.org/D154531
Added:
Modified:
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/include/clang/Basic/DiagnosticGroups.td
clang/lib/Driver/ToolChains/AMDGPU.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 095fbfe8ba2b1d..d10262826bf292 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -82,6 +82,9 @@ def err_drv_hipspv_no_hip_path : Error<
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<MultiGPU>;
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
diff erent CUDA "
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 8b15337fda8829..cfc1c9bc15bd2d 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1336,6 +1336,9 @@ def HIPOnly : DiagGroup<"hip-only">;
// 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
index 2a5b4b66275ff6..d0223322b56ba4 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -635,6 +635,27 @@ AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
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"))
More information about the cfe-commits
mailing list