[clang] [Clang] Include libc wrappers for LLVM environment CUDA / HIP (PR #208084)
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 8 05:28:16 PDT 2026
https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/208084
>From 371ae3da11d73a0298604de437a993100439f483 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Tue, 7 Jul 2026 14:55:57 -0500
Subject: [PATCH 1/2] [Clang] Include libc wrappers for LLVM environment CUDA /
HIP
Summary:
These wrappers (although they are empty right now) are used to inform
the offloading runtime of the supported libc / libm functions available
on the device. We do not include these for the standard HIP path because
they conflict with the alreaedy present utilities, but with the
LLVM-only route we should be able to use them.
---
clang/lib/Driver/ToolChains/Clang.cpp | 15 +++++++++------
clang/test/Driver/hip-device-libs-llvm-env.hip | 2 ++
2 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index a30b01a675b99..35fbe336753f1 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1163,17 +1163,20 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
});
}
- // If we are compiling for a GPU target we want to override the system headers
- // with ones created by the 'libc' project if present.
+ // If we are compiling for a GPU target with the LLVM environment we want to
+ // override the system headers with ones created by the 'libc' project if
+ // present.
// TODO: This should be moved to `AddClangSystemIncludeArgs` by passing the
// OffloadKind as an argument.
+ bool OffloadUsesLLVMLibc =
+ C.getActiveOffloadKinds() == Action::OFK_OpenMP ||
+ ((JA.isDeviceOffloading(Action::OFK_Cuda) ||
+ JA.isDeviceOffloading(Action::OFK_HIP)) &&
+ getToolChain().getTriple().getEnvironment() == llvm::Triple::LLVM);
if (!Args.hasArg(options::OPT_nostdinc) &&
Args.hasFlag(options::OPT_offload_inc, options::OPT_no_offload_inc,
true) &&
- !Args.hasArg(options::OPT_nobuiltininc) &&
- (C.getActiveOffloadKinds() == Action::OFK_OpenMP)) {
- // TODO: CUDA / HIP include their own headers for some common functions
- // implemented here. We'll need to clean those up so they do not conflict.
+ !Args.hasArg(options::OPT_nobuiltininc) && OffloadUsesLLVMLibc) {
SmallString<128> P(D.ResourceDir);
llvm::sys::path::append(P, "include");
llvm::sys::path::append(P, "llvm_libc_wrappers");
diff --git a/clang/test/Driver/hip-device-libs-llvm-env.hip b/clang/test/Driver/hip-device-libs-llvm-env.hip
index 6d7199c9b35c3..6a0b1b5cbabb5 100644
--- a/clang/test/Driver/hip-device-libs-llvm-env.hip
+++ b/clang/test/Driver/hip-device-libs-llvm-env.hip
@@ -9,6 +9,7 @@
// LLVMENV-NOT: oclc
// LLVMENV-NOT: ocml
// LLVMENV-NOT: ockl
+// LLVMENV-NOT: llvm_libc_wrappers
// RUN: %clang -### --target=x86_64-linux-gnu --rocm-path=%S/Inputs/rocm \
// RUN: --offload-targets=amdgcn-amd-amdhsa-llvm --offload-arch=gfx90a \
@@ -18,6 +19,7 @@
// INC: "-cc1" "-triple" "amdgcn-amd-amdhsa-llvm"
// INC-SAME: "-include" "__clang_gpu_device_functions.h"
+// INC-SAME: "-internal-isystem" "{{.*}}llvm_libc_wrappers"
// RUN: %clang -### --target=x86_64-linux-gnu -nogpuinc --rocm-path=%S/Inputs/rocm \
// RUN: --offload-targets=amdgcn-amd-amdhsa-llvm --offload-arch=gfx90a \
>From 7a43b66f16b721f3ed0ed4dd82eaa4af595185cf Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Tue, 7 Jul 2026 17:27:00 -0500
Subject: [PATCH 2/2] All offloading w/ llvm env
---
clang/lib/Driver/ToolChains/Clang.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 35fbe336753f1..6b11ada23f74b 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1170,8 +1170,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
// OffloadKind as an argument.
bool OffloadUsesLLVMLibc =
C.getActiveOffloadKinds() == Action::OFK_OpenMP ||
- ((JA.isDeviceOffloading(Action::OFK_Cuda) ||
- JA.isDeviceOffloading(Action::OFK_HIP)) &&
+ (C.getActiveOffloadKinds() != Action::OFK_None &&
getToolChain().getTriple().getEnvironment() == llvm::Triple::LLVM);
if (!Args.hasArg(options::OPT_nostdinc) &&
Args.hasFlag(options::OPT_offload_inc, options::OPT_no_offload_inc,
More information about the cfe-commits
mailing list