[clang] [OpenMP] Implicitly include the 'lcgpu' and 'lmgpu' libraries for OpenMP (PR #67557)

Joseph Huber via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 27 07:06:58 PDT 2023


https://github.com/jhuber6 created https://github.com/llvm/llvm-project/pull/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.


>From 29ef8bb5d200c97e4d94313d1eee4c96ad712988 Mon Sep 17 00:00:00 2001
From: Joseph Huber <jhuber6 at vols.utk.edu>
Date: Wed, 27 Sep 2023 09:03:16 -0500
Subject: [PATCH] [OpenMP] Implicitly include the 'lcgpu' and 'lmgpu' libraries
 for OpenMP

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.
---
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 9777672ac1f4491..037b775c67f0b52 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -869,6 +869,25 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
                          /*IsLTO=*/true, PluginOptPrefix);
 }
 
+/// Adds the '-lcgpu' and '-lmgpu' libraries to the compilation to include the
+/// LLVM C library for GPUs.
+static void addOpenMPDeviceLibC(const ToolChain &TC, const ArgList &Args,
+                                ArgStringList &CmdArgs) {
+  if (Args.hasArg(options::OPT_nogpulib))
+    return;
+
+  // Check the resource directory for the LLVM libc GPU declarations. If it's
+  // found we can assume that LLVM was built with support for the GPU libc.
+  SmallString<256> LibCDecls(TC.getDriver().ResourceDir);
+  llvm::sys::path::append(LibCDecls, "include", "llvm_libc_wrappers",
+                          "llvm-libc-decls");
+  if (llvm::sys::fs::exists(LibCDecls) &&
+      llvm::sys::fs::is_directory(LibCDecls)) {
+    CmdArgs.push_back("-lcgpu");
+    CmdArgs.push_back("-lmgpu");
+  }
+}
+
 void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,
                                         const ArgList &Args,
                                         ArgStringList &CmdArgs) {
@@ -939,6 +958,9 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
   if (IsOffloadingHost && !Args.hasArg(options::OPT_nogpulib))
     CmdArgs.push_back("-lomptarget.devicertl");
 
+  if (IsOffloadingHost)
+    addOpenMPDeviceLibC(TC, Args, CmdArgs);
+
   addArchSpecificRPath(TC, Args, CmdArgs);
   addOpenMPRuntimeLibraryPath(TC, Args, CmdArgs);
 



More information about the cfe-commits mailing list