1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! **CUDA** — everything the analysis knows about it, in one module.
//!
//! ```text
//! .cppls.toml [compile.cuda] enable = true
//! <toolkit>/include cuda_runtime.h, the vector types, the CCCL include from 12.8 on
//! .cu / .cuh the files the grammar reads with the CUDA rules
//! ```
//!
//! # What is here today
//!
//! [`CudaSection`] — the `[compile.cuda]` section of `.cppls.toml` — and the one function that turns it into a
//! [`CompilerConfig`](crate::include::config::CompilerConfig): the toolkit's include directories, the host compiler's
//! dialect, extra includes, extra flags.
//!
//! # Why the section and its application live together
//!
//! Because they are one statement: a project that names a toolkit means "and read the files as CUDA", and splitting
//! that across three call sites is how the include path would one day be added without the switch. The session calls
//! [`CudaSection::applied_to`] once, after the toolchain, and nothing else in the analysis needs to know the keys.
//!
//! # What will be here
//!
//! The rest of the CUDA-specific reading, as it is asked for:
//!
//! ```text
//! discovery the toolkit's version (`nvcc --version`), the `<toolkit>/include/cccl` layout, and the flags a
//! project's `-arch=` implies — none of which the host toolchain can answer
//! passes which lines are the device pass, when `__CUDA_ARCH__` may and may not be defined
//! kernels the launch bracket `<<<…>>>`, its grid/block argument types, and the kernel's own parameter rules
//! ```
//!
//! The rules that judge CUDA code are checks like any other and live in [`crate::sema::check`](crate::sema::check); this
//! module owns the configuration and the discovery, so that the grammar's CUDA flag has one source.
use PathBuf;
use Deserialize;
/// `[compile.cuda]` — a CUDA project's extra configuration.
///
/// ```toml
/// [compile.cuda]
/// enable = true # read this project as CUDA even if nothing says so
/// toolkit = "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.4"
/// host_compiler = "cl" # the host dialect: cl (MSVC) or g++/clang++ (GNU)
/// include = ["third_party/cutlass/include"] # extra include directories for CUDA units
/// args = ["-arch=sm_80"] # extra flags, appended to whatever won
/// ```
///
/// # What each key is for, and what is deliberately absent
///
/// `enable` is the escape hatch of the detection: the analysis reads a project as CUDA when its toolchain is `nvcc`
/// (`__CUDACC__` in the macro table, or the compiler's name), and a project that has `.cu` files but no compile
/// database has neither. Saying so here is one line, and it is the same switch — the grammar's CUDA rules, on.
///
/// `toolkit` is the key that makes `<cuda_runtime.h>` resolve: the toolkit installs its headers under
/// `<toolkit>/include` (and, from CUDA 12.8, a CCCL include beside it), and **nothing on the system path knows
/// that**. The directory is added as a system include path, so `#include <cuda_runtime.h>` and the vector types it
/// defines (`float4`, `dim3`) become visible without a single line in the project's own headers.
///
/// `host_compiler` decides the **dialect**, which is a different question from the CUDA flag: on Windows `nvcc`
/// drives `cl.exe`, on Linux `g++`, so `__int64` and `__cdecl` are MSVC's names in one project and GNU's in the
/// other. Only the name is read here — `cl` (and `cl.exe`) means MSVC, anything with `g++`, `gcc` or `clang` in it
/// means GNU — because the compiler itself is discovered and asked elsewhere.
///
/// **`__CUDA_ARCH__` is deliberately not a key.** It is defined **only in the device compilation pass**, so a
/// language server that defined it for the whole file would evaluate `#if __CUDA_ARCH__ >= 800` in host code that a
/// compiler never sees. An `arch` key would be a lie of exactly that shape; a project that needs it can put it in
/// `args` and accept the same lie knowingly.