[clang] [CUDA] Pass the SDK version to the device compilation even with -nocudalib (PR #246143)
Stanislav Bardyuk via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 12:46:01 PST 2026
https://github.com/kodlan created https://github.com/llvm/llvm-project/pull/246143
CudaToolChain::addClangTargetOptions returns early when -nocudalib (-nogpulib, --no-offloadlib) is given, and the code that adds -target-sdk-version= sits below that return, after the libdevice handling. So the device-side cc1 never learns the CUDA version, assumes the legacy launch API, and every <<<...>>> kernel launch fails with use of undeclared identifier cudaConfigureCall on any SDK that only declares __cudaPushCallConfiguration (9.2 and newer). The host-side cc1 gets the version from separate code in Clang.cpp and is not affected.
The version has nothing to do with libdevice, so emit it before the early returns. This is the same kind of fix as #111682 did for -fcuda-short-ptr in this function. The condition now matches the host side: skip it when offloading via LLVM and when no version was detected. The old >= CudaVersion::UNKNOWN check was always true, which was fine only because that line was reached after libdevice had been found. Now that it can run without an installation (-nocudainc -nocudalib), it has to be != UNKNOWN, otherwise cc1 would be handed an invalid version.
A side effect is that OpenMP offloading to nvptx64 with -S or -nogpulib now passes the version to the device compilation as well, like it already did for a plain -c. With an SDK older than 10.0 that means the NVPTX alias check is applied there too, same as without -nogpulib.
Fixes #244194 Fixes #86660
>From b6b8cf27c19f9d8dfefba670906473e8ba6c9916 Mon Sep 17 00:00:00 2001
From: Stanislav Bardyuk <sbardyuk at google.com>
Date: Wed, 25 Nov 2026 20:44:04 +0000
Subject: [PATCH] [CUDA] Pass the SDK version to the device compilation even
with -nocudalib
CudaToolChain::addClangTargetOptions added -target-sdk-version only after
the libdevice handling, below the early return taken for -nocudalib
(-nogpulib, --no-offloadlib). The device-side cc1 then assumed the legacy
launch API and rejected every kernel launch with "use of undeclared
identifier cudaConfigureCall".
Emit the version before the early returns, under the same condition as the
host side: not when offloading via LLVM and not when no version was
detected. The old ">= CudaVersion::UNKNOWN" check was always true and
would pass an invalid version now that the code can run without a CUDA
installation.
OpenMP offloading to nvptx64 with -S or -nogpulib now passes the version to
the device compilation as well, as it already did for -c.
Fixes #244194
Fixes #86660
---
clang/docs/ReleaseNotes.md | 5 +++++
clang/lib/Driver/ToolChains/Cuda.cpp | 14 +++++++-------
clang/test/Driver/cuda-detect.cu | 19 +++++++++++++++++++
clang/test/Driver/openmp-offload-gpu.c | 10 ++++++++++
4 files changed, 41 insertions(+), 7 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 31faeace476760..f6aa7b71d76a54 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1609,6 +1609,11 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
`cooperative_groups.h` include them. The `-nogpuinc` option disables this
path together with the other CUDA include paths.
+- Fixed kernel launches failing with `use of undeclared identifier
+ cudaConfigureCall` when compiling with `-nocudalib`: the detected CUDA SDK
+ version is now passed to the device compilation even when libdevice is not
+ linked. (#GH244194)
+
#### AIX Support
#### NetBSD Support
diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index 57470c18db3930..b7961cdd05320c 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -928,6 +928,13 @@ void CudaToolChain::addClangTargetOptions(
options::OPT_fno_cuda_short_ptr, false))
CC1Args.append({"-target-abi", "shortptr"});
+ // The kernel launch API depends on the CUDA version, even without libdevice.
+ clang::CudaVersion CudaInstallationVersion = CudaInstallation.version();
+ if (!UsesLLVMOffloading && CudaInstallationVersion != CudaVersion::UNKNOWN)
+ CC1Args.push_back(
+ DriverArgs.MakeArgString(Twine("-target-sdk-version=") +
+ CudaVersionToString(CudaInstallationVersion)));
+
if (!DriverArgs.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib,
true))
return;
@@ -948,13 +955,6 @@ void CudaToolChain::addClangTargetOptions(
CC1Args.push_back("-mlink-builtin-bitcode");
CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile));
- clang::CudaVersion CudaInstallationVersion = CudaInstallation.version();
-
- if (CudaInstallationVersion >= CudaVersion::UNKNOWN)
- CC1Args.push_back(
- DriverArgs.MakeArgString(Twine("-target-sdk-version=") +
- CudaVersionToString(CudaInstallationVersion)));
-
if (DeviceOffloadingKind == Action::OFK_OpenMP) {
if (CudaInstallationVersion < CudaVersion::CUDA_92) {
getDriver().Diag(
diff --git a/clang/test/Driver/cuda-detect.cu b/clang/test/Driver/cuda-detect.cu
index 4adc6cb15734b0..bbb58e07615201 100644
--- a/clang/test/Driver/cuda-detect.cu
+++ b/clang/test/Driver/cuda-detect.cu
@@ -157,6 +157,22 @@
// RUN: --cuda-path=%S/Inputs/CUDA/usr/local/cuda %s 2>&1 \
// RUN: | FileCheck %s -check-prefix CUDA70
+// Verify that the SDK version is still propagated when libdevice is not linked.
+// RUN: %clang -### -v --target=x86_64-linux-gnu --cuda-gpu-arch=sm_50 \
+// RUN: -nocudalib --cuda-path=%S/Inputs/CUDA_80/usr/local/cuda %s 2>&1 \
+// RUN: | FileCheck %s -check-prefix CUDA80
+
+// Verify that no SDK version is passed when there is no CUDA installation.
+// RUN: %clang -### -v --target=x86_64-linux-gnu --cuda-gpu-arch=sm_50 \
+// RUN: -nocudainc -nocudalib --sysroot=%S/no-cuda-there \
+// RUN: --cuda-path-ignore-env %s 2>&1 \
+// RUN: | FileCheck %s -check-prefix NOSDKVERSION
+
+// Verify that no SDK version is passed when CUDA is offloaded via LLVM.
+// RUN: %clang -### -v --target=x86_64-linux-gnu --cuda-gpu-arch=sm_50 \
+// RUN: -foffload-via-llvm --cuda-path=%S/Inputs/CUDA_80/usr/local/cuda \
+// RUN: %s 2>&1 | FileCheck %s -check-prefix NOSDKVERSION
+
// CHECK: Found CUDA installation: {{.*}}/Inputs/CUDA/usr/local/cuda
// NO-LIBDEVICE: Found CUDA installation: {{.*}}/Inputs/CUDA-nolibdevice/usr/local/cuda
// NOCUDA-NOT: Found CUDA installation:
@@ -200,3 +216,6 @@
// CUDA70: "-cc1" "-triple" "x86_64-unknown-linux-gnu"
// CUDA70-SAME: -target-sdk-version=7.0
// CUDA70: ld{{.*}}"
+
+// NOSDKVERSION: "-cc1" "-triple" "nvptx64-nvidia-cuda{{(-llvm)?}}"
+// NOSDKVERSION-NOT: -target-sdk-version
diff --git a/clang/test/Driver/openmp-offload-gpu.c b/clang/test/Driver/openmp-offload-gpu.c
index bfedc06bb19e5c..38f0ec8cc2f76d 100644
--- a/clang/test/Driver/openmp-offload-gpu.c
+++ b/clang/test/Driver/openmp-offload-gpu.c
@@ -68,6 +68,16 @@
/// ###########################################################################
+/// Check that the CUDA SDK version is passed to the device compilation even when libdevice is not linked.
+// RUN: %clang -### -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 \
+// RUN: -nogpulib --cuda-path=%S/Inputs/CUDA_111/usr/local/cuda %s 2>&1 \
+// RUN: | FileCheck -check-prefix=CHK-NOLIBDEVICE-SDK-VERSION %s
+
+// CHK-NOLIBDEVICE-SDK-VERSION: "-cc1" "-triple" "nvptx64-nvidia-cuda"
+// CHK-NOLIBDEVICE-SDK-VERSION-SAME: "-target-sdk-version=11.1"
+
+/// ###########################################################################
+
/// Check that the runtime bitcode library is part of the compile line.
/// Create a bogus bitcode library and specify it with libomptarget-nvptx-bc-path
// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda \
More information about the cfe-commits
mailing list