Coverage Report

Created: 2021-08-24 07:12

/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
240
const char *CudaVersionToString(CudaVersion V) {
12
240
  switch (V) {
13
0
  case CudaVersion::UNKNOWN:
14
0
    return "unknown";
15
141
  case CudaVersion::CUDA_70:
16
141
    return "7.0";
17
0
  case CudaVersion::CUDA_75:
18
0
    return "7.5";
19
39
  case CudaVersion::CUDA_80:
20
39
    return "8.0";
21
18
  case CudaVersion::CUDA_90:
22
18
    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
26
  case CudaVersion::CUDA_114:
42
26
    return "11.4";
43
16
  case CudaVersion::NEW:
44
16
    return "";
45
240
  }
46
0
  llvm_unreachable("invalid enum");
47
0
}
48
49
0
CudaVersion CudaStringToVersion(const llvm::Twine &S) {
50
0
  return llvm::StringSwitch<CudaVersion>(S.str())
51
0
      .Case("7.0", CudaVersion::CUDA_70)
52
0
      .Case("7.5", CudaVersion::CUDA_75)
53
0
      .Case("8.0", CudaVersion::CUDA_80)
54
0
      .Case("9.0", CudaVersion::CUDA_90)
55
0
      .Case("9.1", CudaVersion::CUDA_91)
56
0
      .Case("9.2", CudaVersion::CUDA_92)
57
0
      .Case("10.0", CudaVersion::CUDA_100)
58
0
      .Case("10.1", CudaVersion::CUDA_101)
59
0
      .Case("10.2", CudaVersion::CUDA_102)
60
0
      .Case("11.0", CudaVersion::CUDA_110)
61
0
      .Case("11.1", CudaVersion::CUDA_111)
62
0
      .Case("11.2", CudaVersion::CUDA_112)
63
0
      .Case("11.3", CudaVersion::CUDA_113)
64
0
      .Case("11.4", CudaVersion::CUDA_114)
65
0
      .Default(CudaVersion::UNKNOWN);
66
0
}
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
    GFX(600),  // gfx600
92
    GFX(601),  // gfx601
93
    GFX(602),  // gfx602
94
    GFX(700),  // gfx700
95
    GFX(701),  // gfx701
96
    GFX(702),  // gfx702
97
    GFX(703),  // gfx703
98
    GFX(704),  // gfx704
99
    GFX(705),  // gfx705
100
    GFX(801),  // gfx801
101
    GFX(802),  // gfx802
102
    GFX(803),  // gfx803
103
    GFX(805),  // gfx805
104
    GFX(810),  // gfx810
105
    GFX(900),  // gfx900
106
    GFX(902),  // gfx902
107
    GFX(904),  // gfx903
108
    GFX(906),  // gfx906
109
    GFX(908),  // gfx908
110
    GFX(909),  // gfx909
111
    GFX(90a),  // gfx90a
112
    GFX(90c),  // gfx90c
113
    GFX(1010), // gfx1010
114
    GFX(1011), // gfx1011
115
    GFX(1012), // gfx1012
116
    GFX(1013), // gfx1013
117
    GFX(1030), // gfx1030
118
    GFX(1031), // gfx1031
119
    GFX(1032), // gfx1032
120
    GFX(1033), // gfx1033
121
    GFX(1034), // gfx1034
122
    GFX(1035), // gfx1035
123
    // clang-format on
124
};
125
#undef SM
126
#undef SM2
127
#undef GFX
128
129
1.52k
const char *CudaArchToString(CudaArch A) {
130
1.52k
  auto result = std::find_if(
131
1.52k
      std::begin(arch_names), std::end(arch_names),
132
10.7k
      [A](const CudaArchToStringMap &map) { return A == map.arch; });
133
1.52k
  if (result == std::end(arch_names))
134
0
    return "unknown";
135
1.52k
  return result->arch_name;
136
1.52k
}
137
138
154
const char *CudaArchToVirtualArchString(CudaArch A) {
139
154
  auto result = std::find_if(
140
154
      std::begin(arch_names), std::end(arch_names),
141
893
      [A](const CudaArchToStringMap &map) { return A == map.arch; });
142
154
  if (result == std::end(arch_names))
143
0
    return "unknown";
144
154
  return result->virtual_arch_name;
145
154
}
146
147
1.01k
CudaArch StringToCudaArch(llvm::StringRef S) {
148
1.01k
  auto result = std::find_if(
149
1.01k
      std::begin(arch_names), std::end(arch_names),
150
7.18k
      [S](const CudaArchToStringMap &map) { return S == map.arch_name; });
151
1.01k
  if (result == std::end(arch_names))
152
21
    return CudaArch::UNKNOWN;
153
990
  return result->arch;
154
1.01k
}
155
156
123
CudaVersion MinVersionForCudaArch(CudaArch A) {
157
123
  if (A == CudaArch::UNKNOWN)
158
0
    return CudaVersion::UNKNOWN;
159
160
  // AMD GPUs do not depend on CUDA versions.
161
123
  if (IsAMDGpuArch(A))
162
0
    return CudaVersion::CUDA_70;
163
164
123
  switch (A) {
165
8
  case CudaArch::SM_20:
166
11
  case CudaArch::SM_21:
167
15
  case CudaArch::SM_30:
168
17
  case CudaArch::SM_32:
169
95
  case CudaArch::SM_35:
170
97
  case CudaArch::SM_37:
171
107
  case CudaArch::SM_50:
172
107
  case CudaArch::SM_52:
173
107
  case CudaArch::SM_53:
174
107
    return CudaVersion::CUDA_70;
175
15
  case CudaArch::SM_60:
176
16
  case CudaArch::SM_61:
177
16
  case CudaArch::SM_62:
178
16
    return CudaVersion::CUDA_80;
179
0
  case CudaArch::SM_70:
180
0
    return CudaVersion::CUDA_90;
181
0
  case CudaArch::SM_72:
182
0
    return CudaVersion::CUDA_91;
183
0
  case CudaArch::SM_75:
184
0
    return CudaVersion::CUDA_100;
185
0
  case CudaArch::SM_80:
186
0
    return CudaVersion::CUDA_110;
187
0
  case CudaArch::SM_86:
188
0
    return CudaVersion::CUDA_111;
189
0
  default:
190
0
    llvm_unreachable("invalid enum");
191
123
  }
192
123
}
193
194
123
CudaVersion MaxVersionForCudaArch(CudaArch A) {
195
  // AMD GPUs do not depend on CUDA versions.
196
123
  if (IsAMDGpuArch(A))
197
0
    return CudaVersion::NEW;
198
199
123
  switch (A) {
200
0
  case CudaArch::UNKNOWN:
201
0
    return CudaVersion::UNKNOWN;
202
8
  case CudaArch::SM_20:
203
11
  case CudaArch::SM_21:
204
11
    return CudaVersion::CUDA_80;
205
4
  case CudaArch::SM_30:
206
4
    return CudaVersion::CUDA_110;
207
108
  default:
208
108
    return CudaVersion::NEW;
209
123
  }
210
123
}
211
212
5.79k
CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
213
5.79k
  int IVer =
214
5.79k
      Version.getMajor() * 10 + Version.getMinor().getValueOr(0);
215
5.79k
  switch(IVer) {
216
4
  case 70:
217
4
    return CudaVersion::CUDA_70;
218
0
  case 75:
219
0
    return CudaVersion::CUDA_75;
220
89
  case 80:
221
89
    return CudaVersion::CUDA_80;
222
0
  case 90:
223
0
    return CudaVersion::CUDA_90;
224
0
  case 91:
225
0
    return CudaVersion::CUDA_91;
226
65
  case 92:
227
65
    return CudaVersion::CUDA_92;
228
0
  case 100:
229
0
    return CudaVersion::CUDA_100;
230
0
  case 101:
231
0
    return CudaVersion::CUDA_101;
232
0
  case 102:
233
0
    return CudaVersion::CUDA_102;
234
0
  case 110:
235
0
    return CudaVersion::CUDA_110;
236
0
  case 111:
237
0
    return CudaVersion::CUDA_111;
238
0
  case 112:
239
0
    return CudaVersion::CUDA_112;
240
0
  case 113:
241
0
    return CudaVersion::CUDA_113;
242
0
  case 114:
243
0
    return CudaVersion::CUDA_114;
244
5.63k
  default:
245
5.63k
    return CudaVersion::UNKNOWN;
246
5.79k
  }
247
5.79k
}
248
249
5.78k
bool CudaFeatureEnabled(llvm::VersionTuple  Version, CudaFeature Feature) {
250
5.78k
  return CudaFeatureEnabled(ToCudaVersion(Version), Feature);
251
5.78k
}
252
253
5.78k
bool CudaFeatureEnabled(CudaVersion Version, CudaFeature Feature) {
254
5.78k
  switch (Feature) {
255
5.77k
  case CudaFeature::CUDA_USES_NEW_LAUNCH:
256
5.77k
    return Version >= CudaVersion::CUDA_92;
257
8
  case CudaFeature::CUDA_USES_FATBIN_REGISTER_END:
258
8
    return Version >= CudaVersion::CUDA_101;
259
5.78k
  }
260
0
  llvm_unreachable("Unknown CUDA feature.");
261
0
}
262
} // namespace clang