https://github.com/namnkahn1607 created
https://github.com/llvm/llvm-project/pull/244945
`__clang_cuda_runtime_wrapper.h` forcefully includes `curand_mtgp32_kernel.h`.
On a minimal CUDA setup (`cuda -nvcc` and `cuda-cudart-dev` installed),
compiling any CUDA code using `clang++` fails with `fatal error:
'curand_mtgp32_kernel.h' file not found`, even when the code does not use
cuRAND, see #244941
The fix guards `#include "curand_mtgp32_kernel.h"` and its surrounding
`dim3`/`uint3`, `push_macro`/`define`/`pop_macro` "hack" in
`clang/lib/Headers/__clang_cuda_runtime_wrapper.h` with `#if
__has_include("curand_mtgp32_kernel.h")`.
First, moved the stub
`clang/test/Headers/Inputs/include/curand_mtgp32_kernel.h` to
`clang/test/Headers/Inputs/curand/curand_mtgp32_kernel.h` and updated it to
mimic `blockDim`/`threadIdx` redeclarations using `dim3`/`uint3`. Keeping the
stub in the shared `Inputs/include/` mock tree made the missing-header case
really hard to test, as they would vacuously pass even on an unpatched wrapper.
Second, added `clang/test/Headers/cuda_wrapper_curand.cu` to cover 3 cases:
1. Missing header, user code does not include it -> compiles.
2. Missing header, user code explicitly includes it -> fails at the user's
`#include`.
3. Header present via `-isystem %S/Inputs/curand` -> compiles only if the
wrapper's `dim3`/`uint3` redirect is active.
>From 7d0b70d5454d24e1a5f1ea74aa36b95ef2226e35 Mon Sep 17 00:00:00 2001
From: namnkahn1607 <[email protected]>
Date: Sun, 22 Nov 2026 04:34:23 +0700
Subject: [PATCH] [Clang][CUDA] Guard curand_mtgp32_kernel.h include with
has_include
---
.../Headers/__clang_cuda_runtime_wrapper.h | 9 +++--
.../Inputs/curand/curand_mtgp32_kernel.h | 7 ++++
.../Inputs/include/curand_mtgp32_kernel.h | 2 -
clang/test/Headers/cuda_wrapper_curand.cu | 37 +++++++++++++++++++
4 files changed, 50 insertions(+), 5 deletions(-)
create mode 100644 clang/test/Headers/Inputs/curand/curand_mtgp32_kernel.h
delete mode 100644 clang/test/Headers/Inputs/include/curand_mtgp32_kernel.h
create mode 100644 clang/test/Headers/cuda_wrapper_curand.cu
diff --git a/clang/lib/Headers/__clang_cuda_runtime_wrapper.h
b/clang/lib/Headers/__clang_cuda_runtime_wrapper.h
index 29178bf0db8a1b..456ead71c864d8 100644
--- a/clang/lib/Headers/__clang_cuda_runtime_wrapper.h
+++ b/clang/lib/Headers/__clang_cuda_runtime_wrapper.h
@@ -495,9 +495,11 @@ __device__ inline __cuda_builtin_gridDim_t::operator
uint3() const {
// curand_mtgp32_kernel helpfully redeclares blockDim and threadIdx in host
// mode, giving them their "proper" types of dim3 and uint3. This is
// incompatible with the types we give in __clang_cuda_builtin_vars.h. As as
-// hack, force-include the header (nvcc doesn't include it by default) but
-// redefine dim3 and uint3 to our builtin types. (Thankfully dim3 and uint3
are
-// only used here for the redeclarations of blockDim and threadIdx.)
+// hack, force-include the header when available (nvcc doesn't include it by
+// default) but redefine dim3 and uint3 to our builtin types. (Thankfully dim3
+// and uint3 are only used here for the redeclarations of blockDim and
+// threadIdx.)
+#if __has_include("curand_mtgp32_kernel.h")
#pragma push_macro("dim3")
#pragma push_macro("uint3")
#define dim3 __cuda_builtin_blockDim_t
@@ -505,6 +507,7 @@ __device__ inline __cuda_builtin_gridDim_t::operator
uint3() const {
#include "curand_mtgp32_kernel.h"
#pragma pop_macro("dim3")
#pragma pop_macro("uint3")
+#endif
#pragma pop_macro("__USE_FAST_MATH__")
#pragma pop_macro("__CUDA_INCLUDE_COMPILER_INTERNAL_HEADERS__")
diff --git a/clang/test/Headers/Inputs/curand/curand_mtgp32_kernel.h
b/clang/test/Headers/Inputs/curand/curand_mtgp32_kernel.h
new file mode 100644
index 00000000000000..caa1f5e8ef1cec
--- /dev/null
+++ b/clang/test/Headers/Inputs/curand/curand_mtgp32_kernel.h
@@ -0,0 +1,7 @@
+// Mimics the part of NVIDIA's curand_mtgp32_kernel.h that matters here: in
host
+// mode it redeclares blockDim/threadIdx using the runtime's dim3/uint3 types.
+#pragma once
+#ifndef __CUDA_ARCH__
+extern const dim3 blockDim;
+extern const uint3 threadIdx;
+#endif
diff --git a/clang/test/Headers/Inputs/include/curand_mtgp32_kernel.h
b/clang/test/Headers/Inputs/include/curand_mtgp32_kernel.h
deleted file mode 100644
index bffa775cb28220..00000000000000
--- a/clang/test/Headers/Inputs/include/curand_mtgp32_kernel.h
+++ /dev/null
@@ -1,2 +0,0 @@
-// required for __clang_cuda_runtime_wrapper.h tests
-#pragma once
diff --git a/clang/test/Headers/cuda_wrapper_curand.cu
b/clang/test/Headers/cuda_wrapper_curand.cu
new file mode 100644
index 00000000000000..fdd3ed4f2f4a6d
--- /dev/null
+++ b/clang/test/Headers/cuda_wrapper_curand.cu
@@ -0,0 +1,37 @@
+// Check that __clang_cuda_runtime_wrapper.h does not require cuRAND's
+// curand_mtgp32_kernel.h, which is not part of every CUDA installation (for
+// example one made of only cuda-nvcc and cuda-cudart-dev), but still includes
+// it with dim3/uint3 redirected to the builtin types when it does exist.
+
+// REQUIRES: x86-registered-target
+
+// Inputs/include does not provide curand_mtgp32_kernel.h, which models a CUDA
+// installation without cuRAND. A TU that does not use cuRAND must compile.
+// RUN: %clang -target x86_64-unknown-linux-gnu -x cuda --cuda-host-only \
+// RUN: -fsyntax-only -nocudalib \
+// RUN: --cuda-path=%S/../Driver/Inputs/CUDA/usr/local/cuda \
+// RUN: -isystem %S/Inputs/include -isystem %S/../../lib/Headers %s
+
+// A TU that asks for the missing header must get the error at its own
+// #include, not at a line inside the wrapper.
+// RUN: not %clang -target x86_64-unknown-linux-gnu -x cuda --cuda-host-only \
+// RUN: -fsyntax-only -nocudalib \
+// RUN: --cuda-path=%S/../Driver/Inputs/CUDA/usr/local/cuda \
+// RUN: -isystem %S/Inputs/include -isystem %S/../../lib/Headers \
+// RUN: -DINCLUDE_CURAND %s 2>&1 | FileCheck %s --check-prefix=MISSING
+// MISSING: {{.*}}cuda_wrapper_curand.cu:{{[0-9]+}}:{{[0-9]+}}: fatal error:
'curand_mtgp32_kernel.h' file not found
+
+// With the header present, Inputs/curand redeclares blockDim and threadIdx
+// the way NVIDIA's header does. That only compiles if the wrapper has already
+// included it with dim3 and uint3 redirected to the builtin types.
+// RUN: %clang -target x86_64-unknown-linux-gnu -x cuda --cuda-host-only \
+// RUN: -fsyntax-only -nocudalib \
+// RUN: --cuda-path=%S/../Driver/Inputs/CUDA/usr/local/cuda \
+// RUN: -isystem %S/Inputs/include -isystem %S/Inputs/curand \
+// RUN: -isystem %S/../../lib/Headers -DINCLUDE_CURAND %s
+
+#ifdef INCLUDE_CURAND
+#include <curand_mtgp32_kernel.h>
+#endif
+
+__global__ void k() {}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits