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 const char *CudaVersionToString(CudaVersion V) { 12 switch (V) { 13 case CudaVersion::UNKNOWN: 14 return "unknown"; 15 case CudaVersion::CUDA_70: 16 return "7.0"; 17 case CudaVersion::CUDA_75: 18 return "7.5"; 19 case CudaVersion::CUDA_80: 20 return "8.0"; 21 case CudaVersion::CUDA_90: 22 return "9.0"; 23 case CudaVersion::CUDA_91: 24 return "9.1"; 25 case CudaVersion::CUDA_92: 26 return "9.2"; 27 case CudaVersion::CUDA_100: 28 return "10.0"; 29 case CudaVersion::CUDA_101: 30 return "10.1"; 31 case CudaVersion::CUDA_102: 32 return "10.2"; 33 case CudaVersion::CUDA_110: 34 return "11.0"; 35 case CudaVersion::CUDA_111: 36 return "11.1"; 37 case CudaVersion::CUDA_112: 38 return "11.2"; 39 case CudaVersion::CUDA_113: 40 return "11.3"; 41 case CudaVersion::CUDA_114: 42 return "11.4"; 43 case CudaVersion::CUDA_115: 44 return "11.5"; 45 case CudaVersion::NEW: 46 return ""; 47 } 48 llvm_unreachable("invalid enum"); 49 } 50 51 CudaVersion CudaStringToVersion(const llvm::Twine &S) { 52 return llvm::StringSwitch<CudaVersion>(S.str()) 53 .Case("7.0", CudaVersion::CUDA_70) 54 .Case("7.5", CudaVersion::CUDA_75) 55 .Case("8.0", CudaVersion::CUDA_80) 56 .Case("9.0", CudaVersion::CUDA_90) 57 .Case("9.1", CudaVersion::CUDA_91) 58 .Case("9.2", CudaVersion::CUDA_92) 59 .Case("10.0", CudaVersion::CUDA_100) 60 .Case("10.1", CudaVersion::CUDA_101) 61 .Case("10.2", CudaVersion::CUDA_102) 62 .Case("11.0", CudaVersion::CUDA_110) 63 .Case("11.1", CudaVersion::CUDA_111) 64 .Case("11.2", CudaVersion::CUDA_112) 65 .Case("11.3", CudaVersion::CUDA_113) 66 .Case("11.4", CudaVersion::CUDA_114) 67 .Case("11.5", CudaVersion::CUDA_115) 68 .Default(CudaVersion::UNKNOWN); 69 } 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 const char *CudaArchToString(CudaArch A) { 134 auto result = std::find_if( 135 std::begin(arch_names), std::end(arch_names), 136 [A](const CudaArchToStringMap &map) { return A == map.arch; }); 137 if (result == std::end(arch_names)) 138 return "unknown"; 139 return result->arch_name; 140 } 141 142 const char *CudaArchToVirtualArchString(CudaArch A) { 143 auto result = std::find_if( 144 std::begin(arch_names), std::end(arch_names), 145 [A](const CudaArchToStringMap &map) { return A == map.arch; }); 146 if (result == std::end(arch_names)) 147 return "unknown"; 148 return result->virtual_arch_name; 149 } 150 151 CudaArch StringToCudaArch(llvm::StringRef S) { 152 auto result = std::find_if( 153 std::begin(arch_names), std::end(arch_names), 154 [S](const CudaArchToStringMap &map) { return S == map.arch_name; }); 155 if (result == std::end(arch_names)) 156 return CudaArch::UNKNOWN; 157 return result->arch; 158 } 159 160 CudaVersion MinVersionForCudaArch(CudaArch A) { 161 if (A == CudaArch::UNKNOWN) 162 return CudaVersion::UNKNOWN; 163 164 // AMD GPUs do not depend on CUDA versions. 165 if (IsAMDGpuArch(A)) 166 return CudaVersion::CUDA_70; 167 168 switch (A) { 169 case CudaArch::SM_20: 170 case CudaArch::SM_21: 171 case CudaArch::SM_30: 172 case CudaArch::SM_32: 173 case CudaArch::SM_35: 174 case CudaArch::SM_37: 175 case CudaArch::SM_50: 176 case CudaArch::SM_52: 177 case CudaArch::SM_53: 178 return CudaVersion::CUDA_70; 179 case CudaArch::SM_60: 180 case CudaArch::SM_61: 181 case CudaArch::SM_62: 182 return CudaVersion::CUDA_80; 183 case CudaArch::SM_70: 184 return CudaVersion::CUDA_90; 185 case CudaArch::SM_72: 186 return CudaVersion::CUDA_91; 187 case CudaArch::SM_75: 188 return CudaVersion::CUDA_100; 189 case CudaArch::SM_80: 190 return CudaVersion::CUDA_110; 191 case CudaArch::SM_86: 192 return CudaVersion::CUDA_111; 193 default: 194 llvm_unreachable("invalid enum"); 195 } 196 } 197 198 CudaVersion MaxVersionForCudaArch(CudaArch A) { 199 // AMD GPUs do not depend on CUDA versions. 200 if (IsAMDGpuArch(A)) 201 return CudaVersion::NEW; 202 203 switch (A) { 204 case CudaArch::UNKNOWN: 205 return CudaVersion::UNKNOWN; 206 case CudaArch::SM_20: 207 case CudaArch::SM_21: 208 return CudaVersion::CUDA_80; 209 case CudaArch::SM_30: 210 return CudaVersion::CUDA_110; 211 default: 212 return CudaVersion::NEW; 213 } 214 } 215 216 CudaVersion ToCudaVersion(llvm::VersionTuple Version) { 217 int IVer = 218 Version.getMajor() * 10 + Version.getMinor().getValueOr(0); 219 switch(IVer) { 220 case 70: 221 return CudaVersion::CUDA_70; 222 case 75: 223 return CudaVersion::CUDA_75; 224 case 80: 225 return CudaVersion::CUDA_80; 226 case 90: 227 return CudaVersion::CUDA_90; 228 case 91: 229 return CudaVersion::CUDA_91; 230 case 92: 231 return CudaVersion::CUDA_92; 232 case 100: 233 return CudaVersion::CUDA_100; 234 case 101: 235 return CudaVersion::CUDA_101; 236 case 102: 237 return CudaVersion::CUDA_102; 238 case 110: 239 return CudaVersion::CUDA_110; 240 case 111: 241 return CudaVersion::CUDA_111; 242 case 112: 243 return CudaVersion::CUDA_112; 244 case 113: 245 return CudaVersion::CUDA_113; 246 case 114: 247 return CudaVersion::CUDA_114; 248 case 115: 249 return CudaVersion::CUDA_115; 250 default: 251 return CudaVersion::UNKNOWN; 252 } 253 } 254 255 bool CudaFeatureEnabled(llvm::VersionTuple Version, CudaFeature Feature) { 256 return CudaFeatureEnabled(ToCudaVersion(Version), Feature); 257 } 258 259 bool CudaFeatureEnabled(CudaVersion Version, CudaFeature Feature) { 260 switch (Feature) { 261 case CudaFeature::CUDA_USES_NEW_LAUNCH: 262 return Version >= CudaVersion::CUDA_92; 263 case CudaFeature::CUDA_USES_FATBIN_REGISTER_END: 264 return Version >= CudaVersion::CUDA_101; 265 } 266 llvm_unreachable("Unknown CUDA feature."); 267 } 268 } // namespace clang 269