Skip to content

Commit 91b9bde

Browse files
committed
[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
1 parent 3900860 commit 91b9bde

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def err_drv_hipspv_no_hip_path : Error<
8282
def err_drv_undetermined_gpu_arch : Error<
8383
"cannot determine %0 architecture: %1; consider passing it via "
8484
"'%2'">;
85+
def warn_drv_multi_gpu_arch : Warning<
86+
"multiple %0 architectures are detected: %1; only the first one is used for "
87+
"'%2'">, InGroup<MultiGPU>;
8588
def err_drv_cuda_version_unsupported : Error<
8689
"GPU arch %0 is supported by CUDA versions between %1 and %2 (inclusive), "
8790
"but installation at %3 is %4; use '--cuda-path' to specify a different CUDA "

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,9 @@ def HIPOnly : DiagGroup<"hip-only">;
13361336
// Warning about mixed HIP and OpenMP compilation / target offloading.
13371337
def HIPOpenMPOffloading: DiagGroup<"hip-omp-target-directives">;
13381338

1339+
// Warning about multiple GPUs are detected.
1340+
def MultiGPU: DiagGroup<"multi-gpu">;
1341+
13391342
// Warnings which cause linking of the runtime libraries like
13401343
// libc and the CRT to be skipped.
13411344
def AVRRtlibLinkingQuirks : DiagGroup<"avr-rtlib-linking-quirks">;

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,27 @@ AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
635635
for (Arg *A : Args)
636636
DAL->append(A);
637637

638+
// Replace -mcpu=native with detected GPU.
639+
Arg *LastMCPUArg = DAL->getLastArg(options::OPT_mcpu_EQ);
640+
if (LastMCPUArg && StringRef(LastMCPUArg->getValue()) == "native") {
641+
DAL->eraseArg(options::OPT_mcpu_EQ);
642+
auto GPUsOrErr = getSystemGPUArchs(Args);
643+
if (!GPUsOrErr) {
644+
getDriver().Diag(diag::err_drv_undetermined_gpu_arch)
645+
<< llvm::Triple::getArchTypeName(getArch())
646+
<< llvm::toString(GPUsOrErr.takeError()) << "-mcpu";
647+
} else {
648+
auto &GPUs = *GPUsOrErr;
649+
if (GPUs.size() > 1) {
650+
getDriver().Diag(diag::warn_drv_multi_gpu_arch)
651+
<< llvm::Triple::getArchTypeName(getArch())
652+
<< llvm::join(GPUs, ", ") << "-mcpu";
653+
}
654+
DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ),
655+
Args.MakeArgString(GPUs.front()));
656+
}
657+
}
658+
638659
checkTargetID(*DAL);
639660

640661
if (!Args.getLastArgValue(options::OPT_x).equals("cl"))

0 commit comments

Comments
 (0)