diff --git a/clang/lib/Basic/Cuda.cpp b/clang/lib/Basic/Cuda.cpp --- a/clang/lib/Basic/Cuda.cpp +++ b/clang/lib/Basic/Cuda.cpp @@ -8,64 +8,59 @@ namespace clang { +struct CudaVersionMapEntry { + const char *Name; + CudaVersion Version; + int IVersion; +}; +#define CUDA_ENTRY(major, minor) \ + { \ +#major "." #minor, CudaVersion::CUDA_##major##minor, major * 10 + minor \ + } + +static const CudaVersionMapEntry CudaNameVersionMap[] = { + CUDA_ENTRY(7, 0), + CUDA_ENTRY(7, 5), + CUDA_ENTRY(8, 0), + CUDA_ENTRY(9, 0), + CUDA_ENTRY(9, 1), + CUDA_ENTRY(9, 2), + CUDA_ENTRY(10, 0), + CUDA_ENTRY(10, 1), + CUDA_ENTRY(10, 2), + CUDA_ENTRY(11, 0), + CUDA_ENTRY(11, 1), + CUDA_ENTRY(11, 2), + CUDA_ENTRY(11, 3), + CUDA_ENTRY(11, 4), + CUDA_ENTRY(11, 5), + {"", CudaVersion::NEW, 999999}, + {"unknown", CudaVersion::UNKNOWN, 0} // End of list tombstone. +}; +#undef CUDA_ENTRY + const char *CudaVersionToString(CudaVersion V) { - switch (V) { - case CudaVersion::UNKNOWN: - return "unknown"; - case CudaVersion::CUDA_70: - return "7.0"; - case CudaVersion::CUDA_75: - return "7.5"; - case CudaVersion::CUDA_80: - return "8.0"; - case CudaVersion::CUDA_90: - return "9.0"; - case CudaVersion::CUDA_91: - return "9.1"; - case CudaVersion::CUDA_92: - return "9.2"; - case CudaVersion::CUDA_100: - return "10.0"; - case CudaVersion::CUDA_101: - return "10.1"; - case CudaVersion::CUDA_102: - return "10.2"; - case CudaVersion::CUDA_110: - return "11.0"; - case CudaVersion::CUDA_111: - return "11.1"; - case CudaVersion::CUDA_112: - return "11.2"; - case CudaVersion::CUDA_113: - return "11.3"; - case CudaVersion::CUDA_114: - return "11.4"; - case CudaVersion::CUDA_115: - return "11.5"; - case CudaVersion::NEW: - return ""; - } + for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I) + if (I->Version == V || I->Version == CudaVersion::UNKNOWN) + return I->Name; + llvm_unreachable("invalid enum"); } CudaVersion CudaStringToVersion(const llvm::Twine &S) { - return llvm::StringSwitch(S.str()) - .Case("7.0", CudaVersion::CUDA_70) - .Case("7.5", CudaVersion::CUDA_75) - .Case("8.0", CudaVersion::CUDA_80) - .Case("9.0", CudaVersion::CUDA_90) - .Case("9.1", CudaVersion::CUDA_91) - .Case("9.2", CudaVersion::CUDA_92) - .Case("10.0", CudaVersion::CUDA_100) - .Case("10.1", CudaVersion::CUDA_101) - .Case("10.2", CudaVersion::CUDA_102) - .Case("11.0", CudaVersion::CUDA_110) - .Case("11.1", CudaVersion::CUDA_111) - .Case("11.2", CudaVersion::CUDA_112) - .Case("11.3", CudaVersion::CUDA_113) - .Case("11.4", CudaVersion::CUDA_114) - .Case("11.5", CudaVersion::CUDA_115) - .Default(CudaVersion::UNKNOWN); + std::string VS = S.str(); + for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I) + if (I->Name == VS || I->Version == CudaVersion::UNKNOWN) + return I->Version; + llvm_unreachable("invalid version"); +} + +CudaVersion ToCudaVersion(llvm::VersionTuple Version) { + int IVer = Version.getMajor() * 10 + Version.getMinor().value_or(0); + for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I) + if (I->IVersion == IVer || I->Version == CudaVersion::UNKNOWN) + return I->Version; + llvm_unreachable("invalid version"); } namespace { @@ -219,44 +214,6 @@ } } -CudaVersion ToCudaVersion(llvm::VersionTuple Version) { - int IVer = Version.getMajor() * 10 + Version.getMinor().value_or(0); - switch(IVer) { - case 70: - return CudaVersion::CUDA_70; - case 75: - return CudaVersion::CUDA_75; - case 80: - return CudaVersion::CUDA_80; - case 90: - return CudaVersion::CUDA_90; - case 91: - return CudaVersion::CUDA_91; - case 92: - return CudaVersion::CUDA_92; - case 100: - return CudaVersion::CUDA_100; - case 101: - return CudaVersion::CUDA_101; - case 102: - return CudaVersion::CUDA_102; - case 110: - return CudaVersion::CUDA_110; - case 111: - return CudaVersion::CUDA_111; - case 112: - return CudaVersion::CUDA_112; - case 113: - return CudaVersion::CUDA_113; - case 114: - return CudaVersion::CUDA_114; - case 115: - return CudaVersion::CUDA_115; - default: - return CudaVersion::UNKNOWN; - } -} - bool CudaFeatureEnabled(llvm::VersionTuple Version, CudaFeature Feature) { return CudaFeatureEnabled(ToCudaVersion(Version), Feature); } diff --git a/clang/lib/Basic/Targets/NVPTX.cpp b/clang/lib/Basic/Targets/NVPTX.cpp --- a/clang/lib/Basic/Targets/NVPTX.cpp +++ b/clang/lib/Basic/Targets/NVPTX.cpp @@ -43,25 +43,7 @@ for (const StringRef Feature : Opts.FeaturesAsWritten) { if (!Feature.startswith("+ptx")) continue; - PTXVersion = llvm::StringSwitch(Feature) - .Case("+ptx75", 75) - .Case("+ptx74", 74) - .Case("+ptx73", 73) - .Case("+ptx72", 72) - .Case("+ptx71", 71) - .Case("+ptx70", 70) - .Case("+ptx65", 65) - .Case("+ptx64", 64) - .Case("+ptx63", 63) - .Case("+ptx61", 61) - .Case("+ptx60", 60) - .Case("+ptx50", 50) - .Case("+ptx43", 43) - .Case("+ptx42", 42) - .Case("+ptx41", 41) - .Case("+ptx40", 40) - .Case("+ptx32", 32) - .Default(32); + Feature.drop_front(4).getAsInteger(10, PTXVersion); } TLSSupported = false;