Skip to content

Commit 381cc48

Browse files
authored
[OpenMP] Implicitly include the 'cgpu' and 'mgpu' libraries for OpenMP (#67557)
Summary: [The LLVM C library for GPUs](https://libc.llvm.org/gpu/) supports standard function calls on the GPU that users are familiar with. Currently, this requires that users include these manually. The support for this library is dependent upon whether or not associated LLVM build was built with the GPU C library support. This patch implicitly adds these for an OpenMP offloading compilation if we find that the toolchain contains the GPU declarations that allow us to use this. I do not know how to test this, given that it requires information from the resource directory in the install. That means it won't be present for any internal tests. It works when I test it and the idea is simple enough so it should be simple enough.
1 parent 050bb26 commit 381cc48

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5055,6 +5055,9 @@ def nogpulib : Flag<["-"], "nogpulib">, MarshallingInfoFlag<LangOpts<"NoGPULib">
50555055
Visibility<[ClangOption, CC1Option]>,
50565056
HelpText<"Do not link device library for CUDA/HIP device compilation">;
50575057
def : Flag<["-"], "nocudalib">, Alias<nogpulib>;
5058+
def gpulibc : Flag<["-"], "gpulibc">, Visibility<[ClangOption, CC1Option]>,
5059+
HelpText<"Link the LLVM C Library for GPUs">;
5060+
def nogpulibc : Flag<["-"], "nogpulibc">, Visibility<[ClangOption, CC1Option]>;
50585061
def nodefaultlibs : Flag<["-"], "nodefaultlibs">;
50595062
def nodriverkitlib : Flag<["-"], "nodriverkitlib">;
50605063
def nofixprebinding : Flag<["-"], "nofixprebinding">;

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,26 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
869869
/*IsLTO=*/true, PluginOptPrefix);
870870
}
871871

872+
/// Adds the '-lcgpu' and '-lmgpu' libraries to the compilation to include the
873+
/// LLVM C library for GPUs.
874+
static void addOpenMPDeviceLibC(const ToolChain &TC, const ArgList &Args,
875+
ArgStringList &CmdArgs) {
876+
if (Args.hasArg(options::OPT_nogpulib) || Args.hasArg(options::OPT_nolibc))
877+
return;
878+
879+
// Check the resource directory for the LLVM libc GPU declarations. If it's
880+
// found we can assume that LLVM was built with support for the GPU libc.
881+
SmallString<256> LibCDecls(TC.getDriver().ResourceDir);
882+
llvm::sys::path::append(LibCDecls, "include", "llvm_libc_wrappers",
883+
"llvm-libc-decls");
884+
bool HasLibC = llvm::sys::fs::exists(LibCDecls) &&
885+
llvm::sys::fs::is_directory(LibCDecls);
886+
if (Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, HasLibC)) {
887+
CmdArgs.push_back("-lcgpu");
888+
CmdArgs.push_back("-lmgpu");
889+
}
890+
}
891+
872892
void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,
873893
const ArgList &Args,
874894
ArgStringList &CmdArgs) {
@@ -939,6 +959,9 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
939959
if (IsOffloadingHost && !Args.hasArg(options::OPT_nogpulib))
940960
CmdArgs.push_back("-lomptarget.devicertl");
941961

962+
if (IsOffloadingHost)
963+
addOpenMPDeviceLibC(TC, Args, CmdArgs);
964+
942965
addArchSpecificRPath(TC, Args, CmdArgs);
943966
addOpenMPRuntimeLibraryPath(TC, Args, CmdArgs);
944967

clang/test/Driver/openmp-offload-gpu.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,20 @@
387387
// RUN: | FileCheck --check-prefix=XARCH-DEVICE %s
388388
// XARCH-DEVICE: "-cc1" "-triple" "nvptx64-nvidia-cuda"{{.*}}"-O3"
389389
// XARCH-DEVICE-NOT: "-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-O3"
390+
391+
//
392+
// Check that `-gpulibc` includes the LLVM C libraries for the GPU.
393+
//
394+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
395+
// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-nvptx-test.bc \
396+
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
397+
// RUN: --offload-arch=sm_52 -gpulibc -nogpuinc %s 2>&1 \
398+
// RUN: | FileCheck --check-prefix=LIBC-GPU %s
399+
// LIBC-GPU: "-lcgpu"{{.*}}"-lmgpu"
400+
401+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
402+
// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-nvptx-test.bc \
403+
// RUN: --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
404+
// RUN: --offload-arch=sm_52 -nogpulibc -nogpuinc %s 2>&1 \
405+
// RUN: | FileCheck --check-prefix=NO-LIBC-GPU %s
406+
// NO-LIBC-GPU-NOT: "-lcgpu"{{.*}}"-lmgpu"

0 commit comments

Comments
 (0)