Coverage Report

Created: 2021-01-23 06:44

/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
157
const char *CudaVersionToString(CudaVersion V) {
12
157
  switch (V) {
13
0
  case CudaVersion::UNKNOWN:
14
0
    return "unknown";
15
78
  case CudaVersion::CUDA_70:
16
78
    return "7.0";
17
0
  case CudaVersion::CUDA_75:
18
0
    return "7.5";
19
48
  case CudaVersion::CUDA_80:
20
48
    return "8.0";
21
12
  case CudaVersion::CUDA_90:
22
12
    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
3
  case CudaVersion::CUDA_101:
30
3
    return "10.1";
31
3
  case CudaVersion::CUDA_102:
32
3
    return "10.2";
33
13
  case CudaVersion::CUDA_110:
34
13
    return "11.0";
35
0
  }
36
0
  llvm_unreachable("invalid enum");
37
0
}
38
39
41
CudaVersion CudaStringToVersion(const llvm::Twine &S) {
40
41
  return llvm::StringSwitch<CudaVersion>(S.str())
41
41
      .Case("7.0", CudaVersion::CUDA_70)
42
41
      .Case("7.5", CudaVersion::CUDA_75)
43
41
      .Case("8.0", CudaVersion::CUDA_80)
44
41
      .Case("9.0", CudaVersion::CUDA_90)
45
41
      .Case("9.1", CudaVersion::CUDA_91)
46
41
      .Case("9.2", CudaVersion::CUDA_92)
47
41
      .Case("10.0", CudaVersion::CUDA_100)
48
41
      .Case("10.1", CudaVersion::CUDA_101)
49
41
      .Case("10.2", CudaVersion::CUDA_102)
50
41
      .Case("11.0", CudaVersion::CUDA_110)
51
41
      .Default(CudaVersion::UNKNOWN);
52
41
}
53
54
struct CudaArchToStringMap {
55
  CudaArch arch;
56
  const char *arch_name;
57
  const char *virtual_arch_name;
58
};
59
60
#define SM2(sm, ca)                                                            \
61
  { CudaArch::SM_##sm, "sm_" #sm, ca }
62
#define SM(sm) SM2(sm, "compute_" #sm)
63
#define GFX(gpu)                                                               \
64
  { CudaArch::GFX##gpu, "gfx" #gpu, "compute_amdgcn" }
65
CudaArchToStringMap arch_names[] = {
66
    // clang-format off
67
    {CudaArch::UNUSED, "", ""},
68
    SM2(20, "compute_20"), SM2(21, "compute_20"), // Fermi
69
    SM(30), SM(32), SM(35), SM(37),  // Kepler
70
    SM(50), SM(52), SM(53),          // Maxwell
71
    SM(60), SM(61), SM(62),          // Pascal
72
    SM(70), SM(72),                  // Volta
73
    SM(75),                          // Turing
74
    SM(80),                          // Ampere
75
    GFX(600),  // gfx600
76
    GFX(601),  // gfx601
77
    GFX(602),  // gfx602
78
    GFX(700),  // gfx700
79
    GFX(701),  // gfx701
80
    GFX(702),  // gfx702
81
    GFX(703),  // gfx703
82
    GFX(704),  // gfx704
83
    GFX(705),  // gfx705
84
    GFX(801),  // gfx801
85
    GFX(802),  // gfx802
86
    GFX(803),  // gfx803
87
    GFX(805),  // gfx805
88
    GFX(810),  // gfx810
89
    GFX(900),  // gfx900
90
    GFX(902),  // gfx902
91
    GFX(904),  // gfx903
92
    GFX(906),  // gfx906
93
    GFX(908),  // gfx908
94
    GFX(909),  // gfx909
95
    GFX(90c),  // gfx90c
96
    GFX(1010), // gfx1010
97
    GFX(1011), // gfx1011
98
    GFX(1012), // gfx1012
99
    GFX(1030), // gfx1030
100
    GFX(1031), // gfx1031
101
    GFX(1032), // gfx1032
102
    GFX(1033), // gfx1033
103
    // clang-format on
104
};
105
#undef SM
106
#undef SM2
107
#undef GFX
108
109
1.47k
const char *CudaArchToString(CudaArch A) {
110
1.47k
  auto result = std::find_if(
111
1.47k
      std::begin(arch_names), std::end(arch_names),
112
8.99k
      [A](const CudaArchToStringMap &map) { return A == map.arch; });
113
1.47k
  if (result == std::end(arch_names))
114
0
    return "unknown";
115
1.47k
  return result->arch_name;
116
1.47k
}
117
118
190
const char *CudaArchToVirtualArchString(CudaArch A) {
119
190
  auto result = std::find_if(
120
190
      std::begin(arch_names), std::end(arch_names),
121
1.23k
      [A](const CudaArchToStringMap &map) { return A == map.arch; });
122
190
  if (result == std::end(arch_names))
123
0
    return "unknown";
124
190
  return result->virtual_arch_name;
125
190
}
126
127
1.21k
CudaArch StringToCudaArch(llvm::StringRef S) {
128
1.21k
  auto result = std::find_if(
129
1.21k
      std::begin(arch_names), std::end(arch_names),
130
8.89k
      [S](const CudaArchToStringMap &map) { return S == map.arch_name; });
131
1.21k
  if (result == std::end(arch_names))
132
22
    return CudaArch::UNKNOWN;
133
1.19k
  return result->arch;
134
1.19k
}
135
136
69
CudaVersion MinVersionForCudaArch(CudaArch A) {
137
69
  if (A == CudaArch::UNKNOWN)
138
0
    return CudaVersion::UNKNOWN;
139
140
  // AMD GPUs do not depend on CUDA versions.
141
69
  if (IsAMDGpuArch(A))
142
0
    return CudaVersion::CUDA_70;
143
144
69
  switch (A) {
145
20
  case CudaArch::SM_20:
146
23
  case CudaArch::SM_21:
147
27
  case CudaArch::SM_30:
148
29
  case CudaArch::SM_32:
149
39
  case CudaArch::SM_35:
150
41
  case CudaArch::SM_37:
151
51
  case CudaArch::SM_50:
152
51
  case CudaArch::SM_52:
153
51
  case CudaArch::SM_53:
154
51
    return CudaVersion::CUDA_70;
155
17
  case CudaArch::SM_60:
156
18
  case CudaArch::SM_61:
157
18
  case CudaArch::SM_62:
158
18
    return CudaVersion::CUDA_80;
159
0
  case CudaArch::SM_70:
160
0
    return CudaVersion::CUDA_90;
161
0
  case CudaArch::SM_72:
162
0
    return CudaVersion::CUDA_91;
163
0
  case CudaArch::SM_75:
164
0
    return CudaVersion::CUDA_100;
165
0
  case CudaArch::SM_80:
166
0
    return CudaVersion::CUDA_110;
167
0
  default:
168
0
    llvm_unreachable("invalid enum");
169
69
  }
170
69
}
171
172
69
CudaVersion MaxVersionForCudaArch(CudaArch A) {
173
  // AMD GPUs do not depend on CUDA versions.
174
69
  if (IsAMDGpuArch(A))
175
0
    return CudaVersion::LATEST;
176
177
69
  switch (A) {
178
0
  case CudaArch::UNKNOWN:
179
0
    return CudaVersion::UNKNOWN;
180
20
  case CudaArch::SM_20:
181
23
  case CudaArch::SM_21:
182
23
    return CudaVersion::CUDA_80;
183
46
  default:
184
46
    return CudaVersion::LATEST;
185
69
  }
186
69
}
187
188
2.35k
CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
189
2.35k
  int IVer =
190
2.35k
      Version.getMajor() * 10 + Version.getMinor().getValueOr(0);
191
2.35k
  switch(IVer) {
192
4
  case 70:
193
4
    return CudaVersion::CUDA_70;
194
0
  case 75:
195
0
    return CudaVersion::CUDA_75;
196
86
  case 80:
197
86
    return CudaVersion::CUDA_80;
198
0
  case 90:
199
0
    return CudaVersion::CUDA_90;
200
0
  case 91:
201
0
    return CudaVersion::CUDA_91;
202
50
  case 92:
203
50
    return CudaVersion::CUDA_92;
204
0
  case 100:
205
0
    return CudaVersion::CUDA_100;
206
0
  case 101:
207
0
    return CudaVersion::CUDA_101;
208
0
  case 102:
209
0
    return CudaVersion::CUDA_102;
210
0
  case 110:
211
0
    return CudaVersion::CUDA_110;
212
2.21k
  default:
213
2.21k
    return CudaVersion::UNKNOWN;
214
2.35k
  }
215
2.35k
}
216
217
2.34k
bool CudaFeatureEnabled(llvm::VersionTuple  Version, CudaFeature Feature) {
218
2.34k
  return CudaFeatureEnabled(ToCudaVersion(Version), Feature);
219
2.34k
}
220
221
2.34k
bool CudaFeatureEnabled(CudaVersion Version, CudaFeature Feature) {
222
2.34k
  switch (Feature) {
223
2.34k
  case CudaFeature::CUDA_USES_NEW_LAUNCH:
224
2.34k
    return Version >= CudaVersion::CUDA_92;
225
7
  case CudaFeature::CUDA_USES_FATBIN_REGISTER_END:
226
7
    return Version >= CudaVersion::CUDA_101;
227
0
  }
228
0
  llvm_unreachable("Unknown CUDA feature.");
229
0
}
230
} // namespace clang