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 {"", CudaVersion::NEW, llvm::VersionTuple(std::numeric_limits<int>::max())},
47 {"unknown", CudaVersion::UNKNOWN, {}} // End of list tombstone.
48};
49#undef CUDA_ENTRY
50
52 for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
53 if (I->Version == V)
54 return I->Name;
55
57}
58
59CudaVersion CudaStringToVersion(const llvm::Twine &S) {
60 std::string VS = S.str();
61 for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
62 if (I->Name == VS)
63 return I->Version;
65}
66
67CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
68 for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
69 if (I->TVersion == Version)
70 return I->Version;
72}
73
74namespace {
75struct OffloadArchToStringMap {
77 const char *arch_name;
78 const char *virtual_arch_name;
79};
80} // namespace
81
82#define SM2(sm, ca) {OffloadArch::SM_##sm, "sm_" #sm, ca}
83#define SM(sm) SM2(sm, "compute_" #sm)
84#define GFX(gpu) {OffloadArch::GFX##gpu, "gfx" #gpu, "compute_amdgcn"}
85static const OffloadArchToStringMap arch_names[] = {
86 // clang-format off
87 {OffloadArch::UNUSED, "", ""},
88 SM2(20, "compute_20"), SM2(21, "compute_20"), // Fermi
89 SM(30), {OffloadArch::SM_32_, "sm_32", "compute_32"}, SM(35), SM(37), // Kepler
90 SM(50), SM(52), SM(53), // Maxwell
91 SM(60), SM(61), SM(62), // Pascal
92 SM(70), SM(72), // Volta
93 SM(75), // Turing
94 SM(80), SM(86), // Ampere
95 SM(87), // Jetson/Drive AGX Orin
96 SM(89), // Ada Lovelace
97 SM(90), // Hopper
98 SM(90a), // Hopper
99 GFX(600), // gfx600
100 GFX(601), // gfx601
101 GFX(602), // gfx602
102 GFX(700), // gfx700
103 GFX(701), // gfx701
104 GFX(702), // gfx702
105 GFX(703), // gfx703
106 GFX(704), // gfx704
107 GFX(705), // gfx705
108 GFX(801), // gfx801
109 GFX(802), // gfx802
110 GFX(803), // gfx803
111 GFX(805), // gfx805
112 GFX(810), // gfx810
113 {OffloadArch::GFX9_GENERIC, "gfx9-generic", "compute_amdgcn"},
114 GFX(900), // gfx900
115 GFX(902), // gfx902
116 GFX(904), // gfx903
117 GFX(906), // gfx906
118 GFX(908), // gfx908
119 GFX(909), // gfx909
120 GFX(90a), // gfx90a
121 GFX(90c), // gfx90c
122 GFX(940), // gfx940
123 GFX(941), // gfx941
124 GFX(942), // gfx942
125 {OffloadArch::GFX10_1_GENERIC, "gfx10-1-generic", "compute_amdgcn"},
126 GFX(1010), // gfx1010
127 GFX(1011), // gfx1011
128 GFX(1012), // gfx1012
129 GFX(1013), // gfx1013
130 {OffloadArch::GFX10_3_GENERIC, "gfx10-3-generic", "compute_amdgcn"},
131 GFX(1030), // gfx1030
132 GFX(1031), // gfx1031
133 GFX(1032), // gfx1032
134 GFX(1033), // gfx1033
135 GFX(1034), // gfx1034
136 GFX(1035), // gfx1035
137 GFX(1036), // gfx1036
138 {OffloadArch::GFX11_GENERIC, "gfx11-generic", "compute_amdgcn"},
139 GFX(1100), // gfx1100
140 GFX(1101), // gfx1101
141 GFX(1102), // gfx1102
142 GFX(1103), // gfx1103
143 GFX(1150), // gfx1150
144 GFX(1151), // gfx1151
145 GFX(1152), // gfx1152
146 {OffloadArch::GFX12_GENERIC, "gfx12-generic", "compute_amdgcn"},
147 GFX(1200), // gfx1200
148 GFX(1201), // gfx1201
149 {OffloadArch::AMDGCNSPIRV, "amdgcnspirv", "compute_amdgcn"},
150 {OffloadArch::Generic, "generic", ""},
151 // clang-format on
152};
153#undef SM
154#undef SM2
155#undef GFX
156
158 auto result = std::find_if(
159 std::begin(arch_names), std::end(arch_names),
160 [A](const OffloadArchToStringMap &map) { return A == map.arch; });
161 if (result == std::end(arch_names))
162 return "unknown";
163 return result->arch_name;
164}
165
167 auto result = std::find_if(
168 std::begin(arch_names), std::end(arch_names),
169 [A](const OffloadArchToStringMap &map) { return A == map.arch; });
170 if (result == std::end(arch_names))
171 return "unknown";
172 return result->virtual_arch_name;
173}
174
176 auto result = std::find_if(
177 std::begin(arch_names), std::end(arch_names),
178 [S](const OffloadArchToStringMap &map) { return S == map.arch_name; });
179 if (result == std::end(arch_names))
181 return result->arch;
182}
183
185 if (A == OffloadArch::UNKNOWN)
187
188 // AMD GPUs do not depend on CUDA versions.
189 if (IsAMDOffloadArch(A))
191
192 switch (A) {
224 default:
225 llvm_unreachable("invalid enum");
226 }
227}
228
230 // AMD GPUs do not depend on CUDA versions.
231 if (IsAMDOffloadArch(A))
232 return CudaVersion::NEW;
233
234 switch (A) {
246 default:
247 return CudaVersion::NEW;
248 }
249}
250
251bool CudaFeatureEnabled(llvm::VersionTuple Version, CudaFeature Feature) {
252 return CudaFeatureEnabled(ToCudaVersion(Version), Feature);
253}
254
256 switch (Feature) {
258 return Version >= CudaVersion::CUDA_92;
260 return Version >= CudaVersion::CUDA_101;
261 }
262 llvm_unreachable("Unknown CUDA feature.");
263}
264} // namespace clang
#define V(N, I)
Definition: ASTContext.h:3341
const char * arch_name
Definition: Cuda.cpp:77
#define SM2(sm, ca)
Definition: Cuda.cpp:82
#define GFX(gpu)
Definition: Cuda.cpp:84
OffloadArch arch
Definition: Cuda.cpp:76
#define SM(sm)
Definition: Cuda.cpp:83
const char * virtual_arch_name
Definition: Cuda.cpp:78
#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:85
CudaVersion MaxVersionForOffloadArch(OffloadArch A)
Get the latest CudaVersion that supports the given OffloadArch.
Definition: Cuda.cpp:229
static bool IsAMDOffloadArch(OffloadArch A)
Definition: Cuda.h:152
OffloadArch
Definition: Cuda.h:55
CudaVersion ToCudaVersion(llvm::VersionTuple)
Definition: Cuda.cpp:67
CudaVersion CudaStringToVersion(const llvm::Twine &S)
Definition: Cuda.cpp:59
bool CudaFeatureEnabled(llvm::VersionTuple, CudaFeature)
Definition: Cuda.cpp:251
const char * CudaVersionToString(CudaVersion V)
Definition: Cuda.cpp:51
static const CudaVersionMapEntry CudaNameVersionMap[]
Definition: Cuda.cpp:21
const char * OffloadArchToVirtualArchString(OffloadArch A)
Definition: Cuda.cpp:166
OffloadArch StringToOffloadArch(llvm::StringRef S)
Definition: Cuda.cpp:175
CudaVersion
Definition: Cuda.h:20
CudaFeature
Definition: Cuda.h:170
const char * OffloadArchToString(OffloadArch A)
Definition: Cuda.cpp:157
CudaVersion MinVersionForOffloadArch(OffloadArch A)
Get the earliest CudaVersion that supports the given OffloadArch.
Definition: Cuda.cpp:184
llvm::VersionTuple TVersion
Definition: Cuda.cpp:13
const char * Name
Definition: Cuda.cpp:11
CudaVersion Version
Definition: Cuda.cpp:12