Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/Cuda.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "clang/Basic/Cuda.h"
2
3
#include "llvm/ADT/StringRef.h"
4
#include "llvm/ADT/StringSwitch.h"
5
#include "llvm/ADT/Twine.h"
6
#include "llvm/Support/ErrorHandling.h"
7
#include "llvm/Support/VersionTuple.h"
8
9
namespace clang {
10
11
8
const char *CudaVersionToString(CudaVersion V) {
12
8
  switch (V) {
13
0
  case CudaVersion::UNKNOWN:
14
0
    return "unknown";
15
8
  case CudaVersion::CUDA_70:
16
8
    return "7.0";
17
0
  case CudaVersion::CUDA_75:
18
0
    return "7.5";
19
0
  case CudaVersion::CUDA_80:
20
0
    return "8.0";
21
0
  case CudaVersion::CUDA_90:
22
0
    return "9.0";
23
0
  case CudaVersion::CUDA_91:
24
0
    return "9.1";
25
0
  case CudaVersion::CUDA_92:
26
0
    return "9.2";
27
0
  case CudaVersion::CUDA_100:
28
0
    return "10.0";
29
0
  case CudaVersion::CUDA_101:
30
0
    return "10.1";
31
0
  case CudaVersion::CUDA_102:
32
0
    return "10.2";
33
0
  case CudaVersion::CUDA_110:
34
0
    return "11.0";
35
0
  case CudaVersion::CUDA_111:
36
0
    return "11.1";
37
0
  case CudaVersion::CUDA_112:
38
0
    return "11.2";
39
0
  case CudaVersion::CUDA_113:
40
0
    return "11.3";
41
0
  case CudaVersion::CUDA_114:
42
0
    return "11.4";
43
0
  case CudaVersion::CUDA_115:
44
0
    return "11.5";
45
0
  case CudaVersion::NEW:
46
0
    return "";
47
8
  }
48
0
  llvm_unreachable("invalid enum");
49
0
}
50
51
0
CudaVersion CudaStringToVersion(const llvm::Twine &S) {
52
0
  return llvm::StringSwitch<CudaVersion>(S.str())
53
0
      .Case("7.0", CudaVersion::CUDA_70)
54
0
      .Case("7.5", CudaVersion::CUDA_75)
55
0
      .Case("8.0", CudaVersion::CUDA_80)
56
0
      .Case("9.0", CudaVersion::CUDA_90)
57
0
      .Case("9.1", CudaVersion::CUDA_91)
58
0
      .Case("9.2", CudaVersion::CUDA_92)
59
0
      .Case("10.0", CudaVersion::CUDA_100)
60
0
      .Case("10.1", CudaVersion::CUDA_101)
61
0
      .Case("10.2", CudaVersion::CUDA_102)
62
0
      .Case("11.0", CudaVersion::CUDA_110)
63
0
      .Case("11.1", CudaVersion::CUDA_111)
64
0
      .Case("11.2", CudaVersion::CUDA_112)
65
0
      .Case("11.3", CudaVersion::CUDA_113)
66
0
      .Case("11.4", CudaVersion::CUDA_114)
67
0
      .Case("11.5", CudaVersion::CUDA_115)
68
0
      .Default(CudaVersion::UNKNOWN);
69
0
}
70
71
namespace {
72
struct CudaArchToStringMap {
73
  CudaArch arch;
74
  const char *arch_name;
75
  const char *virtual_arch_name;
76
};
77
} // namespace
78
79
#define SM2(sm, ca)                                                            \
80
  { CudaArch::SM_##sm, "sm_" #sm, ca }
81
#define SM(sm) SM2(sm, "compute_" #sm)
82
#define GFX(gpu)                                                               \
83
  { CudaArch::GFX##gpu, "gfx" #gpu, "compute_amdgcn" }
84
static const CudaArchToStringMap arch_names[] = {
85
    // clang-format off
86
    {CudaArch::UNUSED, "", ""},
87
    SM2(20, "compute_20"), SM2(21, "compute_20"), // Fermi
88
    SM(30), SM(32), SM(35), SM(37),  // Kepler
89
    SM(50), SM(52), SM(53),          // Maxwell
90
    SM(60), SM(61), SM(62),          // Pascal
91
    SM(70), SM(72),                  // Volta
92
    SM(75),                          // Turing
93
    SM(80), SM(86),                  // Ampere
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(1010), // gfx1010
117
    GFX(1011), // gfx1011
118
    GFX(1012), // gfx1012
119
    GFX(1013), // gfx1013
120
    GFX(1030), // gfx1030
121
    GFX(1031), // gfx1031
122
    GFX(1032), // gfx1032
123
    GFX(1033), // gfx1033
124
    GFX(1034), // gfx1034
125
    GFX(1035), // gfx1035
126
    {CudaArch::Generic, "generic", ""},
127
    // clang-format on
128
};
129
#undef SM
130
#undef SM2
131
#undef GFX
132
133
505
const char *CudaArchToString(CudaArch A) {
134
505
  auto result = std::find_if(
135
505
      std::begin(arch_names), std::end(arch_names),
136
3.39k
      [A](const CudaArchToStringMap &map) { return A == map.arch; });
137
505
  if (result == std::end(arch_names))
138
0
    return "unknown";
139
505
  return result->arch_name;
140
505
}
141
142
36
const char *CudaArchToVirtualArchString(CudaArch A) {
143
36
  auto result = std::find_if(
144
36
      std::begin(arch_names), std::end(arch_names),
145
132
      [A](const CudaArchToStringMap &map) { return A == map.arch; });
146
36
  if (result == std::end(arch_names))
147
0
    return "unknown";
148
36
  return result->virtual_arch_name;
149
36
}
150
151
251
CudaArch StringToCudaArch(llvm::StringRef S) {
152
251
  auto result = std::find_if(
153
251
      std::begin(arch_names), std::end(arch_names),
154
2.18k
      [S](const CudaArchToStringMap &map) { return S == map.arch_name; });
155
251
  if (result == std::end(arch_names))
156
19
    return CudaArch::UNKNOWN;
157
232
  return result->arch;
158
251
}
159
160
3
CudaVersion MinVersionForCudaArch(CudaArch A) {
161
3
  if (A == CudaArch::UNKNOWN)
162
0
    return CudaVersion::UNKNOWN;
163
164
  // AMD GPUs do not depend on CUDA versions.
165
3
  if (IsAMDGpuArch(A))
166
0
    return CudaVersion::CUDA_70;
167
168
3
  switch (A) {
169
0
  case CudaArch::SM_20:
170
0
  case CudaArch::SM_21:
171
0
  case CudaArch::SM_30:
172
0
  case CudaArch::SM_32:
173
3
  case CudaArch::SM_35:
174
3
  case CudaArch::SM_37:
175
3
  case CudaArch::SM_50:
176
3
  case CudaArch::SM_52:
177
3
  case CudaArch::SM_53:
178
3
    return CudaVersion::CUDA_70;
179
0
  case CudaArch::SM_60:
180
0
  case CudaArch::SM_61:
181
0
  case CudaArch::SM_62:
182
0
    return CudaVersion::CUDA_80;
183
0
  case CudaArch::SM_70:
184
0
    return CudaVersion::CUDA_90;
185
0
  case CudaArch::SM_72:
186
0
    return CudaVersion::CUDA_91;
187
0
  case CudaArch::SM_75:
188
0
    return CudaVersion::CUDA_100;
189
0
  case CudaArch::SM_80:
190
0
    return CudaVersion::CUDA_110;
191
0
  case CudaArch::SM_86:
192
0
    return CudaVersion::CUDA_111;
193
0
  default:
194
0
    llvm_unreachable("invalid enum");
195
3
  }
196
3
}
197
198
3
CudaVersion MaxVersionForCudaArch(CudaArch A) {
199
  // AMD GPUs do not depend on CUDA versions.
200
3
  if (IsAMDGpuArch(A))
201
0
    return CudaVersion::NEW;
202
203
3
  switch (A) {
204
0
  case CudaArch::UNKNOWN:
205
0
    return CudaVersion::UNKNOWN;
206
0
  case CudaArch::SM_20:
207
0
  case CudaArch::SM_21:
208
0
    return CudaVersion::CUDA_80;
209
0
  case CudaArch::SM_30:
210
0
    return CudaVersion::CUDA_110;
211
3
  default:
212
3
    return CudaVersion::NEW;
213
3
  }
214
3
}
215
216
1.57k
CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
217
1.57k
  int IVer =
218
1.57k
      Version.getMajor() * 10 + Version.getMinor().getValueOr(0);
219
1.57k
  switch(IVer) {
220
4
  case 70:
221
4
    return CudaVersion::CUDA_70;
222
0
  case 75:
223
0
    return CudaVersion::CUDA_75;
224
80
  case 80:
225
80
    return CudaVersion::CUDA_80;
226
0
  case 90:
227
0
    return CudaVersion::CUDA_90;
228
0
  case 91:
229
0
    return CudaVersion::CUDA_91;
230
65
  case 92:
231
65
    return CudaVersion::CUDA_92;
232
0
  case 100:
233
0
    return CudaVersion::CUDA_100;
234
0
  case 101:
235
0
    return CudaVersion::CUDA_101;
236
0
  case 102:
237
0
    return CudaVersion::CUDA_102;
238
0
  case 110:
239
0
    return CudaVersion::CUDA_110;
240
0
  case 111:
241
0
    return CudaVersion::CUDA_111;
242
0
  case 112:
243
0
    return CudaVersion::CUDA_112;
244
0
  case 113:
245
0
    return CudaVersion::CUDA_113;
246
0
  case 114:
247
0
    return CudaVersion::CUDA_114;
248
18
  case 115:
249
18
    return CudaVersion::CUDA_115;
250
1.41k
  default:
251
1.41k
    return CudaVersion::UNKNOWN;
252
1.57k
  }
253
1.57k
}
254
255
1.57k
bool CudaFeatureEnabled(llvm::VersionTuple  Version, CudaFeature Feature) {
256
1.57k
  return CudaFeatureEnabled(ToCudaVersion(Version), Feature);
257
1.57k
}
258
259
1.57k
bool CudaFeatureEnabled(CudaVersion Version, CudaFeature Feature) {
260
1.57k
  switch (Feature) {
261
1.56k
  case CudaFeature::CUDA_USES_NEW_LAUNCH:
262
1.56k
    return Version >= CudaVersion::CUDA_92;
263
6
  case CudaFeature::CUDA_USES_FATBIN_REGISTER_END:
264
6
    return Version >= CudaVersion::CUDA_101;
265
1.57k
  }
266
0
  llvm_unreachable("Unknown CUDA feature.");
267
0
}
268
} // namespace clang