[clang] f3a2cbc - Refactored CUDA version housekeeping to use less boilerplate.

Artem Belevich via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 7 14:00:11 PDT 2022


Author: Artem Belevich
Date: 2022-10-07T13:59:23-07:00
New Revision: f3a2cbcf97f5c7a58f9d4c5588c2bea8028f8c58

URL: https://github.com/llvm/llvm-project/commit/f3a2cbcf97f5c7a58f9d4c5588c2bea8028f8c58
DIFF: https://github.com/llvm/llvm-project/commit/f3a2cbcf97f5c7a58f9d4c5588c2bea8028f8c58.diff

LOG: Refactored CUDA version housekeeping to use less boilerplate.

Differential Revision: https://reviews.llvm.org/D135328

Added: 
    

Modified: 
    clang/lib/Basic/Cuda.cpp
    clang/lib/Basic/Targets/NVPTX.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Basic/Cuda.cpp b/clang/lib/Basic/Cuda.cpp
index d75cc410f2d9d..142e7941eb4db 100644
--- a/clang/lib/Basic/Cuda.cpp
+++ b/clang/lib/Basic/Cuda.cpp
@@ -8,64 +8,59 @@
 
 namespace clang {
 
-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 "";
+struct CudaVersionMapEntry {
+  const char *Name;
+  CudaVersion Version;
+  llvm::VersionTuple TVersion;
+};
+#define CUDA_ENTRY(major, minor)                                               \
+  {                                                                            \
+#major "." #minor, CudaVersion::CUDA_##major##minor,                       \
+        llvm::VersionTuple(major, minor)                                       \
   }
-  llvm_unreachable("invalid enum");
+
+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, llvm::VersionTuple(std::numeric_limits<int>::max())},
+    {"unknown", CudaVersion::UNKNOWN, {}} // End of list tombstone.
+};
+#undef CUDA_ENTRY
+
+const char *CudaVersionToString(CudaVersion V) {
+  for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
+    if (I->Version == V)
+      return I->Name;
+
+  return CudaVersionToString(CudaVersion::UNKNOWN);
 }
 
 CudaVersion CudaStringToVersion(const llvm::Twine &S) {
-  return llvm::StringSwitch<CudaVersion>(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)
+      return I->Version;
+  return CudaVersion::UNKNOWN;
+}
+
+CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
+  for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)
+    if (I->TVersion == Version)
+      return I->Version;
+  return CudaVersion::UNKNOWN;
 }
 
 namespace {
@@ -219,44 +214,6 @@ CudaVersion MaxVersionForCudaArch(CudaArch A) {
   }
 }
 
-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
index 9dd60adb00fb3..de06675d4cc1e 100644
--- a/clang/lib/Basic/Targets/NVPTX.cpp
+++ b/clang/lib/Basic/Targets/NVPTX.cpp
@@ -41,27 +41,11 @@ NVPTXTargetInfo::NVPTXTargetInfo(const llvm::Triple &Triple,
 
   PTXVersion = 32;
   for (const StringRef Feature : Opts.FeaturesAsWritten) {
-    if (!Feature.startswith("+ptx"))
+    int PTXV;
+    if (!Feature.startswith("+ptx") ||
+        Feature.drop_front(4).getAsInteger(10, PTXV))
       continue;
-    PTXVersion = llvm::StringSwitch<unsigned>(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);
+    PTXVersion = PTXV; // TODO: should it be max(PTXVersion, PTXV)?
   }
 
   TLSSupported = false;


        


More information about the cfe-commits mailing list