clang 20.0.0git
Cuda.cpp
Go to the documentation of this file.
1#include "clang/Basic/Cuda.h"
2
3#include "llvm/ADT/StringRef.h"
4#include "llvm/ADT/Twine.h"
5#include "llvm/Support/ErrorHandling.h"
6#include "llvm/Support/VersionTuple.h"
7
8namespace clang {
9
11 const char *Name;
13 llvm::VersionTuple TVersion;
14};
15#define CUDA_ENTRY(major, minor) \
16 { \
17 #major "." #minor, CudaVersion::CUDA_##major##minor, \
18 llvm::VersionTuple(major, minor) \
19 }
20
22 CUDA_ENTRY(7, 0),
23 CUDA_ENTRY(7, 5),
24 CUDA_ENTRY(8, 0),
25 CUDA_ENTRY(9, 0),
26 CUDA_ENTRY(9, 1),
27 CUDA_ENTRY(9, 2),
28 CUDA_ENTRY(10, 0),
29 CUDA_ENTRY(10, 1),
30 CUDA_ENTRY(10, 2),
31 CUDA_ENTRY(11, 0),
32 CUDA_ENTRY(11, 1),
33 CUDA_ENTRY(11, 2),
34 CUDA_ENTRY(11, 3),
35 CUDA_ENTRY(11, 4),
36 CUDA_ENTRY(11, 5),
37 CUDA_ENTRY(11, 6),
38 CUDA_ENTRY(11, 7),
39 CUDA_ENTRY(11, 8),
40 CUDA_ENTRY(12, 0),
41 CUDA_ENTRY(12, 1),
42 CUDA_ENTRY(12, 2),
43 CUDA_ENTRY(12, 3),
44 CUDA_ENTRY(12, 4),
45 CUDA_ENTRY(12, 5),
46 CUDA_ENTRY(12, 6),
47 {"", CudaVersion::NEW, llvm::VersionTuple(std::numeric_limits<int>::max())},
48 {"unknown", CudaVersion::UNKNOWN, {}} // End of list tombstone.
49};
50#undef CUDA_ENTRY
51
53 for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
54 if (I->Version == V)
55 return I->Name;
56
58}
59
60CudaVersion CudaStringToVersion(const llvm::Twine &S) {
61 std::string VS = S.str();
62 for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
63 if (I->Name == VS)
64 return I->Version;
66}
67
68CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
69 for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
70 if (I->TVersion == Version)
71 return I->Version;
73}
74
75namespace {
76struct OffloadArchToStringMap {
78 const char *arch_name;
79 const char *virtual_arch_name;
80};
81} // namespace
82
83#define SM2(sm, ca) {OffloadArch::SM_##sm, "sm_" #sm, ca}
84#define SM(sm) SM2(sm, "compute_" #sm)
85#define GFX(gpu) {OffloadArch::GFX##gpu, "gfx" #gpu, "compute_amdgcn"}
86static const OffloadArchToStringMap arch_names[] = {
87 // clang-format off
88 {OffloadArch::UNUSED, "", ""},
89 SM2(20, "compute_20"), SM2(21, "compute_20"), // Fermi
90 SM(30), {OffloadArch::SM_32_, "sm_32", "compute_32"}, SM(35), SM(37), // Kepler
91 SM(50), SM(52), SM(53), // Maxwell
92 SM(60), SM(61), SM(62), // Pascal
93 SM(70), SM(72), // Volta
94 SM(75), // Turing
95 SM(80), SM(86), // Ampere
96 SM(87), // Jetson/Drive AGX Orin
97 SM(89), // Ada Lovelace
98 SM(90), // Hopper
99 SM(90a), // Hopper
100 SM(100), // Blackwell
101 GFX(600), // gfx600
102 GFX(601), // gfx601
103 GFX(602), // gfx602
104 GFX(700), // gfx700
105 GFX(701), // gfx701
106 GFX(702), // gfx702
107 GFX(703), // gfx703
108 GFX(704), // gfx704
109 GFX(705), // gfx705
110 GFX(801), // gfx801
111 GFX(802), // gfx802
112 GFX(803), // gfx803
113 GFX(805), // gfx805
114 GFX(810), // gfx810
115 {OffloadArch::GFX9_GENERIC, "gfx9-generic", "compute_amdgcn"},
116 GFX(900), // gfx900
117 GFX(902), // gfx902
118 GFX(904), // gfx903
119 GFX(906), // gfx906
120 GFX(908), // gfx908
121 GFX(909), // gfx909
122 GFX(90a), // gfx90a
123 GFX(90c), // gfx90c
124 {OffloadArch::GFX9_4_GENERIC, "gfx9-4-generic", "compute_amdgcn"},
125 GFX(940), // gfx940
126 GFX(941), // gfx941
127 GFX(942), // gfx942
128 GFX(950), // gfx950
129 {OffloadArch::GFX10_1_GENERIC, "gfx10-1-generic", "compute_amdgcn"},
130 GFX(1010), // gfx1010
131 GFX(1011), // gfx1011
132 GFX(1012), // gfx1012
133 GFX(1013), // gfx1013
134 {OffloadArch::GFX10_3_GENERIC, "gfx10-3-generic", "compute_amdgcn"},
135 GFX(1030), // gfx1030
136 GFX(1031), // gfx1031
137 GFX(1032), // gfx1032
138 GFX(1033), // gfx1033
139 GFX(1034), // gfx1034
140 GFX(1035), // gfx1035
141 GFX(1036), // gfx1036
142 {OffloadArch::GFX11_GENERIC, "gfx11-generic", "compute_amdgcn"},
143 GFX(1100), // gfx1100
144 GFX(1101), // gfx1101
145 GFX(1102), // gfx1102
146 GFX(1103), // gfx1103
147 GFX(1150), // gfx1150
148 GFX(1151), // gfx1151
149 GFX(1152), // gfx1152
150 GFX(1153), // gfx1153
151 {OffloadArch::GFX12_GENERIC, "gfx12-generic", "compute_amdgcn"},
152 GFX(1200), // gfx1200
153 GFX(1201), // gfx1201
154 {OffloadArch::AMDGCNSPIRV, "amdgcnspirv", "compute_amdgcn"},
155 {OffloadArch::Generic, "generic", ""},
156 // clang-format on
157};
158#undef SM
159#undef SM2
160#undef GFX
161
163 auto result = std::find_if(
164 std::begin(arch_names), std::end(arch_names),
165 [A](const OffloadArchToStringMap &map) { return A == map.arch; });
166 if (result == std::end(arch_names))
167 return "unknown";
168 return result->arch_name;
169}
170
172 auto result = std::find_if(
173 std::begin(arch_names), std::end(arch_names),
174 [A](const OffloadArchToStringMap &map) { return A == map.arch; });
175 if (result == std::end(arch_names))
176 return "unknown";
177 return result->virtual_arch_name;
178}
179
181 auto result = std::find_if(
182 std::begin(arch_names), std::end(arch_names),
183 [S](const OffloadArchToStringMap &map) { return S == map.arch_name; });
184 if (result == std::end(arch_names))
186 return result->arch;
187}
188
190 if (A == OffloadArch::UNKNOWN)
192
193 // AMD GPUs do not depend on CUDA versions.
194 if (IsAMDOffloadArch(A))
196
197 switch (A) {
230 return CudaVersion::NEW; // TODO: use specific CUDA version once it's
231 // public.
232 default:
233 llvm_unreachable("invalid enum");
234 }
235}
236
238 // AMD GPUs do not depend on CUDA versions.
239 if (IsAMDOffloadArch(A))
240 return CudaVersion::NEW;
241
242 switch (A) {
254 default:
255 return CudaVersion::NEW;
256 }
257}
258
259bool CudaFeatureEnabled(llvm::VersionTuple Version, CudaFeature Feature) {
260 return CudaFeatureEnabled(ToCudaVersion(Version), Feature);
261}
262
264 switch (Feature) {
266 return Version >= CudaVersion::CUDA_92;
268 return Version >= CudaVersion::CUDA_101;
269 }
270 llvm_unreachable("Unknown CUDA feature.");
271}
272} // namespace clang
#define V(N, I)
Definition: ASTContext.h:3443
const char * arch_name
Definition: Cuda.cpp:78
#define SM2(sm, ca)
Definition: Cuda.cpp:83
#define GFX(gpu)
Definition: Cuda.cpp:85
OffloadArch arch
Definition: Cuda.cpp:77
#define SM(sm)
Definition: Cuda.cpp:84
const char * virtual_arch_name
Definition: Cuda.cpp:79
#define CUDA_ENTRY(major, minor)
Definition: Cuda.cpp:15
__device__ __2f16 float c
The JSON file list parser is used to communicate input to InstallAPI.
static const OffloadArchToStringMap arch_names[]
Definition: Cuda.cpp:86
CudaVersion MaxVersionForOffloadArch(OffloadArch A)
Get the latest CudaVersion that supports the given OffloadArch.
Definition: Cuda.cpp:237
static bool IsAMDOffloadArch(OffloadArch A)
Definition: Cuda.h:157
OffloadArch
Definition: Cuda.h:56
CudaVersion ToCudaVersion(llvm::VersionTuple)
Definition: Cuda.cpp:68
CudaVersion CudaStringToVersion(const llvm::Twine &S)
Definition: Cuda.cpp:60
bool CudaFeatureEnabled(llvm::VersionTuple, CudaFeature)
Definition: Cuda.cpp:259
const char * CudaVersionToString(CudaVersion V)
Definition: Cuda.cpp:52
static const CudaVersionMapEntry CudaNameVersionMap[]
Definition: Cuda.cpp:21
const char * OffloadArchToVirtualArchString(OffloadArch A)
Definition: Cuda.cpp:171
OffloadArch StringToOffloadArch(llvm::StringRef S)
Definition: Cuda.cpp:180
CudaVersion
Definition: Cuda.h:20
CudaFeature
Definition: Cuda.h:175
const char * OffloadArchToString(OffloadArch A)
Definition: Cuda.cpp:162
CudaVersion MinVersionForOffloadArch(OffloadArch A)
Get the earliest CudaVersion that supports the given OffloadArch.
Definition: Cuda.cpp:189
llvm::VersionTuple TVersion
Definition: Cuda.cpp:13
const char * Name
Definition: Cuda.cpp:11
CudaVersion Version
Definition: Cuda.cpp:12