xref: /freebsd/contrib/llvm-project/clang/lib/Basic/Cuda.cpp (revision 66fd12cf4896eb08ad8e7a2627537f84ead84dd3)
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 
8 namespace clang {
9 
10 struct CudaVersionMapEntry {
11   const char *Name;
12   CudaVersion Version;
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 
21 static const CudaVersionMapEntry CudaNameVersionMap[] = {
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     {"", CudaVersion::NEW, llvm::VersionTuple(std::numeric_limits<int>::max())},
41     {"unknown", CudaVersion::UNKNOWN, {}} // End of list tombstone.
42 };
43 #undef CUDA_ENTRY
44 
45 const char *CudaVersionToString(CudaVersion V) {
46   for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
47     if (I->Version == V)
48       return I->Name;
49 
50   return CudaVersionToString(CudaVersion::UNKNOWN);
51 }
52 
53 CudaVersion CudaStringToVersion(const llvm::Twine &S) {
54   std::string VS = S.str();
55   for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
56     if (I->Name == VS)
57       return I->Version;
58   return CudaVersion::UNKNOWN;
59 }
60 
61 CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
62   for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
63     if (I->TVersion == Version)
64       return I->Version;
65   return CudaVersion::UNKNOWN;
66 }
67 
68 namespace {
69 struct CudaArchToStringMap {
70   CudaArch arch;
71   const char *arch_name;
72   const char *virtual_arch_name;
73 };
74 } // namespace
75 
76 #define SM2(sm, ca)                                                            \
77   { CudaArch::SM_##sm, "sm_" #sm, ca }
78 #define SM(sm) SM2(sm, "compute_" #sm)
79 #define GFX(gpu)                                                               \
80   { CudaArch::GFX##gpu, "gfx" #gpu, "compute_amdgcn" }
81 static const CudaArchToStringMap arch_names[] = {
82     // clang-format off
83     {CudaArch::UNUSED, "", ""},
84     SM2(20, "compute_20"), SM2(21, "compute_20"), // Fermi
85     SM(30), SM(32), SM(35), SM(37),  // Kepler
86     SM(50), SM(52), SM(53),          // Maxwell
87     SM(60), SM(61), SM(62),          // Pascal
88     SM(70), SM(72),                  // Volta
89     SM(75),                          // Turing
90     SM(80), SM(86),                  // Ampere
91     SM(87),                          // Jetson/Drive AGX Orin
92     SM(89),                          // Ada Lovelace
93     SM(90),                          // Hopper
94     GFX(600),  // gfx600
95     GFX(601),  // gfx601
96     GFX(602),  // gfx602
97     GFX(700),  // gfx700
98     GFX(701),  // gfx701
99     GFX(702),  // gfx702
100     GFX(703),  // gfx703
101     GFX(704),  // gfx704
102     GFX(705),  // gfx705
103     GFX(801),  // gfx801
104     GFX(802),  // gfx802
105     GFX(803),  // gfx803
106     GFX(805),  // gfx805
107     GFX(810),  // gfx810
108     GFX(900),  // gfx900
109     GFX(902),  // gfx902
110     GFX(904),  // gfx903
111     GFX(906),  // gfx906
112     GFX(908),  // gfx908
113     GFX(909),  // gfx909
114     GFX(90a),  // gfx90a
115     GFX(90c),  // gfx90c
116     GFX(940),  // gfx940
117     GFX(1010), // gfx1010
118     GFX(1011), // gfx1011
119     GFX(1012), // gfx1012
120     GFX(1013), // gfx1013
121     GFX(1030), // gfx1030
122     GFX(1031), // gfx1031
123     GFX(1032), // gfx1032
124     GFX(1033), // gfx1033
125     GFX(1034), // gfx1034
126     GFX(1035), // gfx1035
127     GFX(1036), // gfx1036
128     GFX(1100), // gfx1100
129     GFX(1101), // gfx1101
130     GFX(1102), // gfx1102
131     GFX(1103), // gfx1103
132     {CudaArch::Generic, "generic", ""},
133     // clang-format on
134 };
135 #undef SM
136 #undef SM2
137 #undef GFX
138 
139 const char *CudaArchToString(CudaArch A) {
140   auto result = std::find_if(
141       std::begin(arch_names), std::end(arch_names),
142       [A](const CudaArchToStringMap &map) { return A == map.arch; });
143   if (result == std::end(arch_names))
144     return "unknown";
145   return result->arch_name;
146 }
147 
148 const char *CudaArchToVirtualArchString(CudaArch A) {
149   auto result = std::find_if(
150       std::begin(arch_names), std::end(arch_names),
151       [A](const CudaArchToStringMap &map) { return A == map.arch; });
152   if (result == std::end(arch_names))
153     return "unknown";
154   return result->virtual_arch_name;
155 }
156 
157 CudaArch StringToCudaArch(llvm::StringRef S) {
158   auto result = std::find_if(
159       std::begin(arch_names), std::end(arch_names),
160       [S](const CudaArchToStringMap &map) { return S == map.arch_name; });
161   if (result == std::end(arch_names))
162     return CudaArch::UNKNOWN;
163   return result->arch;
164 }
165 
166 CudaVersion MinVersionForCudaArch(CudaArch A) {
167   if (A == CudaArch::UNKNOWN)
168     return CudaVersion::UNKNOWN;
169 
170   // AMD GPUs do not depend on CUDA versions.
171   if (IsAMDGpuArch(A))
172     return CudaVersion::CUDA_70;
173 
174   switch (A) {
175   case CudaArch::SM_20:
176   case CudaArch::SM_21:
177   case CudaArch::SM_30:
178   case CudaArch::SM_32:
179   case CudaArch::SM_35:
180   case CudaArch::SM_37:
181   case CudaArch::SM_50:
182   case CudaArch::SM_52:
183   case CudaArch::SM_53:
184     return CudaVersion::CUDA_70;
185   case CudaArch::SM_60:
186   case CudaArch::SM_61:
187   case CudaArch::SM_62:
188     return CudaVersion::CUDA_80;
189   case CudaArch::SM_70:
190     return CudaVersion::CUDA_90;
191   case CudaArch::SM_72:
192     return CudaVersion::CUDA_91;
193   case CudaArch::SM_75:
194     return CudaVersion::CUDA_100;
195   case CudaArch::SM_80:
196     return CudaVersion::CUDA_110;
197   case CudaArch::SM_86:
198     return CudaVersion::CUDA_111;
199   case CudaArch::SM_87:
200     return CudaVersion::CUDA_114;
201   case CudaArch::SM_89:
202   case CudaArch::SM_90:
203     return CudaVersion::CUDA_118;
204   default:
205     llvm_unreachable("invalid enum");
206   }
207 }
208 
209 CudaVersion MaxVersionForCudaArch(CudaArch A) {
210   // AMD GPUs do not depend on CUDA versions.
211   if (IsAMDGpuArch(A))
212     return CudaVersion::NEW;
213 
214   switch (A) {
215   case CudaArch::UNKNOWN:
216     return CudaVersion::UNKNOWN;
217   case CudaArch::SM_20:
218   case CudaArch::SM_21:
219     return CudaVersion::CUDA_80;
220   case CudaArch::SM_30:
221     return CudaVersion::CUDA_110;
222   default:
223     return CudaVersion::NEW;
224   }
225 }
226 
227 bool CudaFeatureEnabled(llvm::VersionTuple  Version, CudaFeature Feature) {
228   return CudaFeatureEnabled(ToCudaVersion(Version), Feature);
229 }
230 
231 bool CudaFeatureEnabled(CudaVersion Version, CudaFeature Feature) {
232   switch (Feature) {
233   case CudaFeature::CUDA_USES_NEW_LAUNCH:
234     return Version >= CudaVersion::CUDA_92;
235   case CudaFeature::CUDA_USES_FATBIN_REGISTER_END:
236     return Version >= CudaVersion::CUDA_101;
237   }
238   llvm_unreachable("Unknown CUDA feature.");
239 }
240 } // namespace clang
241