[PATCH] D135328: [CUDA] Refactored CUDA version housekeeping to use less boilerplate.

2022-10-07 Thread Artem Belevich via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGf3a2cbcf97f5: Refactored CUDA version housekeeping to use 
less boilerplate. (authored by tra).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135328/new/

https://reviews.llvm.org/D135328

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

Index: clang/lib/Basic/Targets/NVPTX.cpp
===
--- clang/lib/Basic/Targets/NVPTX.cpp
+++ clang/lib/Basic/Targets/NVPTX.cpp
@@ -41,27 +41,11 @@
 
   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(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;
Index: clang/lib/Basic/Cuda.cpp
===
--- clang/lib/Basic/Cuda.cpp
+++ 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::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 ) {
-  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 = 

[PATCH] D135328: [CUDA] Refactored CUDA version housekeeping to use less boilerplate.

2022-10-07 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl accepted this revision.
yaxunl added a comment.
This revision is now accepted and ready to land.

LGTM. Thanks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135328/new/

https://reviews.llvm.org/D135328

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D135328: [CUDA] Refactored CUDA version housekeeping to use less boilerplate.

2022-10-07 Thread Artem Belevich via Phabricator via cfe-commits
tra added a comment.

@yaxunl - Are you OK with the patch? PTAL.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135328/new/

https://reviews.llvm.org/D135328

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D135328: [CUDA] Refactored CUDA version housekeeping to use less boilerplate.

2022-10-06 Thread Artem Belevich via Phabricator via cfe-commits
tra marked an inline comment as done.
tra added inline comments.



Comment at: clang/lib/Basic/Cuda.cpp:59
+CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
+  int IVer = Version.getMajor() * 10 + Version.getMinor().value_or(0);
+  for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)

tra wrote:
> yaxunl wrote:
> > should we assert Version.getMinor().value_or(0)<10 ?
> It's not an immediate issue, but you are correct that we may potentially have 
> CUDA 12.34 and that will mess up the integer version encoding. 
> 
> In fact, NVIDIA hit exactly this kind of problem in CUDA-11.7 when they had 
> to version some of the libraries as 11.10 and had to change the binary 
> representation and break existing ABIs. Here we're only dealing with internal 
> use, but I'll update the encoding to give us more wiggle room.
I've got rid of integer-encoded version altogether and switched to comparing 
VersionTuple directly. 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135328/new/

https://reviews.llvm.org/D135328

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D135328: [CUDA] Refactored CUDA version housekeeping to use less boilerplate.

2022-10-06 Thread Artem Belevich via Phabricator via cfe-commits
tra updated this revision to Diff 465799.
tra added a comment.

Use VersionTuple instead of a manually encoded integer version.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135328/new/

https://reviews.llvm.org/D135328

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

Index: clang/lib/Basic/Targets/NVPTX.cpp
===
--- clang/lib/Basic/Targets/NVPTX.cpp
+++ clang/lib/Basic/Targets/NVPTX.cpp
@@ -41,27 +41,11 @@
 
   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(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;
Index: clang/lib/Basic/Cuda.cpp
===
--- clang/lib/Basic/Cuda.cpp
+++ clang/lib/Basic/Cuda.cpp
@@ -8,64 +8,59 @@
 
 namespace clang {
 
+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)   \
+  }
+
+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::max())},
+{"unknown", CudaVersion::UNKNOWN, {}} // 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 "";
-  }
-  llvm_unreachable("invalid enum");
+  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 ) {
-  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)
+  return I->Version;
+ 

[PATCH] D135328: [CUDA] Refactored CUDA version housekeeping to use less boilerplate.

2022-10-05 Thread Artem Belevich via Phabricator via cfe-commits
tra added inline comments.



Comment at: clang/lib/Basic/Cuda.cpp:59
+CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
+  int IVer = Version.getMajor() * 10 + Version.getMinor().value_or(0);
+  for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)

yaxunl wrote:
> should we assert Version.getMinor().value_or(0)<10 ?
It's not an immediate issue, but you are correct that we may potentially have 
CUDA 12.34 and that will mess up the integer version encoding. 

In fact, NVIDIA hit exactly this kind of problem in CUDA-11.7 when they had to 
version some of the libraries as 11.10 and had to change the binary 
representation and break existing ABIs. Here we're only dealing with internal 
use, but I'll update the encoding to give us more wiggle room.



Comment at: clang/lib/Basic/Targets/NVPTX.cpp:45-46
+int PTXV;
+if (!Feature.startswith("+ptx") ||
+Feature.drop_front(4).getAsInteger(10, PTXV))
   continue;

yaxunl wrote:
> This behaves differently when seeing a +ptx value not listed in the original 
> code. Previously, it returns 32, now it will just return that value. Is this 
> intended?
Indeed, previously it would set PTXVersion=32 if the specific ptx feature was 
not listed. I believe it was a bug as it would clobber correctly parsed 
features before it.

Right now we will only update PTXVersion, if we did manage to parse the 
feature. The code looks a bit misleading. `getAsInteger()` returns `true` on 
failure and when that happens, we hit `continue` w/o setting PTXVersion.

I was considering erroring out here, but that would be a problem if we were to 
have another feature starting with `+ptx`.



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135328/new/

https://reviews.llvm.org/D135328

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D135328: [CUDA] Refactored CUDA version housekeeping to use less boilerplate.

2022-10-05 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl added inline comments.



Comment at: clang/lib/Basic/Cuda.cpp:59
+CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
+  int IVer = Version.getMajor() * 10 + Version.getMinor().value_or(0);
+  for (auto *I = CudaNameVersionMap; I->Version != CudaVersion::UNKNOWN; ++I)

should we assert Version.getMinor().value_or(0)<10 ?



Comment at: clang/lib/Basic/Targets/NVPTX.cpp:45-46
+int PTXV;
+if (!Feature.startswith("+ptx") ||
+Feature.drop_front(4).getAsInteger(10, PTXV))
   continue;

This behaves differently when seeing a +ptx value not listed in the original 
code. Previously, it returns 32, now it will just return that value. Is this 
intended?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135328/new/

https://reviews.llvm.org/D135328

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D135328: [CUDA] Refactored CUDA version housekeeping to use less boilerplate.

2022-10-05 Thread Artem Belevich via Phabricator via cfe-commits
tra updated this revision to Diff 465601.
tra added a comment.

Use int max for the "new" CUDA version value.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135328/new/

https://reviews.llvm.org/D135328

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

Index: clang/lib/Basic/Targets/NVPTX.cpp
===
--- clang/lib/Basic/Targets/NVPTX.cpp
+++ clang/lib/Basic/Targets/NVPTX.cpp
@@ -41,27 +41,11 @@
 
   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(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;
Index: clang/lib/Basic/Cuda.cpp
===
--- clang/lib/Basic/Cuda.cpp
+++ 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, std::numeric_limits::max()},
+{"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 "";
-  }
-  llvm_unreachable("invalid enum");
+  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 ) {
-  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)
+  return I->Version;
+  return CudaVersion::UNKNOWN;
+}
+
+CudaVersion ToCudaVersion(llvm::VersionTuple Version) {
+  int IVer = Version.getMajor() * 10 + 

[PATCH] D135328: [CUDA] Refactored CUDA version housekeeping to use less boilerplate.

2022-10-05 Thread Artem Belevich via Phabricator via cfe-commits
tra created this revision.
Herald added subscribers: mattd, gchakrabarti, asavonic, bixia, yaxunl.
Herald added a project: All.
tra updated this revision to Diff 465598.
tra added a comment.
tra retitled this revision from "Refactored CUDA version housekeeping to use 
less boilerplate." to "[CUDA] Refactored CUDA version housekeeping to use less 
boilerplate.".
tra added a reviewer: yaxunl.
tra added a project: clang.
tra published this revision for review.
Herald added subscribers: cfe-commits, jholewinski.

Fix handling unknown versions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135328

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

Index: clang/lib/Basic/Targets/NVPTX.cpp
===
--- clang/lib/Basic/Targets/NVPTX.cpp
+++ clang/lib/Basic/Targets/NVPTX.cpp
@@ -41,27 +41,11 @@
 
   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(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;
Index: clang/lib/Basic/Cuda.cpp
===
--- clang/lib/Basic/Cuda.cpp
+++ 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, 99},
+{"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 "";
-  }
-  llvm_unreachable("invalid enum");
+  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 ) {
-  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",