Re: [PATCH] D12989: [CUDA] Added CUDA installation detector class.

2015-09-23 Thread Artem Belevich via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL248408: [CUDA] Added CUDA installation detector class. 
(authored by tra).

Changed prior to commit:
  http://reviews.llvm.org/D12989?vs=35531=35533#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D12989

Files:
  cfe/trunk/include/clang/Driver/Options.td
  cfe/trunk/lib/Driver/ToolChains.cpp
  cfe/trunk/lib/Driver/ToolChains.h
  cfe/trunk/test/Driver/Inputs/CUDA/usr/local/cuda/include/.keep
  cfe/trunk/test/Driver/Inputs/CUDA/usr/local/cuda/lib/.keep
  cfe/trunk/test/Driver/Inputs/CUDA/usr/local/cuda/lib64/.keep
  cfe/trunk/test/Driver/Inputs/CUDA/usr/local/cuda/nvvm/libdevice/.keep
  cfe/trunk/test/Driver/cuda-detect.cu

Index: cfe/trunk/include/clang/Driver/Options.td
===
--- cfe/trunk/include/clang/Driver/Options.td
+++ cfe/trunk/include/clang/Driver/Options.td
@@ -359,6 +359,8 @@
   Flags<[DriverOption, HelpHidden]>, HelpText<"CUDA GPU architecture">;
 def cuda_host_only : Flag<["--"], "cuda-host-only">,
   HelpText<"Do host-side CUDA compilation only">;
+def cuda_path_EQ : Joined<["--"], "cuda-path=">, Group,
+  HelpText<"CUDA installation path">;
 def dA : Flag<["-"], "dA">, Group;
 def dD : Flag<["-"], "dD">, Group, Flags<[CC1Option]>,
   HelpText<"Print macro definitions in -E mode in addition to normal output">;
Index: cfe/trunk/test/Driver/cuda-detect.cu
===
--- cfe/trunk/test/Driver/cuda-detect.cu
+++ cfe/trunk/test/Driver/cuda-detect.cu
@@ -0,0 +1,6 @@
+// RUN: %clang -v --sysroot=/tmp/no-cuda-there 2>&1 | FileCheck %s -check-prefix NOCUDA
+// RUN: %clang -v --sysroot=%S/Inputs/CUDA 2>&1 | FileCheck %s
+// RUN: %clang -v --cuda-path=%S/Inputs/CUDA/usr/local/cuda 2>&1 | FileCheck %s
+
+// CHECK: Found CUDA installation: {{.*}}/Inputs/CUDA/usr/local/cuda
+// NOCUDA-NOT: Found CUDA installation:
Index: cfe/trunk/lib/Driver/ToolChains.h
===
--- cfe/trunk/lib/Driver/ToolChains.h
+++ cfe/trunk/lib/Driver/ToolChains.h
@@ -157,6 +157,38 @@
 protected:
   GCCInstallationDetector GCCInstallation;
 
+  // \brief A class to find a viable CUDA installation
+
+  class CudaInstallationDetector {
+bool IsValid;
+std::string CudaInstallPath;
+std::string CudaLibPath;
+std::string CudaLibDevicePath;
+std::string CudaIncludePath;
+
+  public:
+CudaInstallationDetector() : IsValid(false) {}
+void init(const Driver , const llvm::Triple ,
+  const llvm::opt::ArgList );
+
+/// \brief Check whether we detected a valid Cuda install.
+bool isValid() const { return IsValid; }
+/// \brief Print information about the detected CUDA installation.
+void print(raw_ostream ) const;
+
+/// \brief Get the detected Cuda installation path.
+StringRef getInstallPath() const { return CudaInstallPath; }
+/// \brief Get the detected Cuda Include path.
+StringRef getIncludePath() const { return CudaIncludePath; }
+/// \brief Get the detected Cuda library path.
+StringRef getLibPath() const { return CudaLibPath; }
+/// \brief Get the detected Cuda device library path.
+StringRef getLibDevicePath() const { return CudaLibDevicePath; }
+/// \brief Get libdevice file for given architecture
+  };
+
+  CudaInstallationDetector CudaInstallation;
+
 public:
   Generic_GCC(const Driver , const llvm::Triple ,
   const llvm::opt::ArgList );
Index: cfe/trunk/lib/Driver/ToolChains.cpp
===
--- cfe/trunk/lib/Driver/ToolChains.cpp
+++ cfe/trunk/lib/Driver/ToolChains.cpp
@@ -1482,6 +1482,48 @@
 BiarchTripleAliases.push_back(BiarchTriple.str());
 }
 
+// \brief -- try common CUDA installation paths looking for files we need for
+// CUDA compilation.
+
+void
+Generic_GCC::CudaInstallationDetector::init(const Driver ,
+const llvm::Triple ,
+const llvm::opt::ArgList ) {
+  SmallVector CudaPathCandidates;
+
+  if (Args.hasArg(options::OPT_cuda_path_EQ))
+CudaPathCandidates.push_back(
+Args.getLastArgValue(options::OPT_cuda_path_EQ));
+  else {
+CudaPathCandidates.push_back(D.SysRoot + "/usr/local/cuda");
+CudaPathCandidates.push_back(D.SysRoot + "/usr/local/cuda-7.0");
+  }
+
+  for (const auto CudaPath : CudaPathCandidates) {
+if (CudaPath.empty() || !llvm::sys::fs::exists(CudaPath))
+  continue;
+
+CudaInstallPath = CudaPath;
+CudaIncludePath = CudaInstallPath + "/include";
+CudaLibDevicePath = CudaInstallPath + "/nvvm/libdevice";
+CudaLibPath =
+CudaInstallPath + (TargetTriple.isArch64Bit() ? "/lib64" : "/lib");
+
+if (!(llvm::sys::fs::exists(CudaIncludePath) &&
+  llvm::sys::fs::exists(CudaLibPath) &&
+  

Re: [PATCH] D12989: [CUDA] Added CUDA installation detector class.

2015-09-23 Thread NAKAMURA Takumi via cfe-commits
chapuni added a subscriber: chapuni.


Comment at: cfe/trunk/lib/Driver/ToolChains.cpp:1492
@@ +1491,3 @@
+const llvm::opt::ArgList ) {
+  SmallVector CudaPathCandidates;
+

Fixed in r248459.


Comment at: cfe/trunk/lib/Driver/ToolChains.cpp:1498
@@ +1497,3 @@
+  else {
+CudaPathCandidates.push_back(D.SysRoot + "/usr/local/cuda");
+CudaPathCandidates.push_back(D.SysRoot + "/usr/local/cuda-7.0");

You were trying to make StringRef from temporary string concatenator.


Repository:
  rL LLVM

http://reviews.llvm.org/D12989



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


Re: [PATCH] D12989: [CUDA] Added CUDA installation detector class.

2015-09-23 Thread Eric Christopher via cfe-commits
echristo added a subscriber: echristo.
echristo added a comment.

Aha, nice catch!

-eric


Repository:
  rL LLVM

http://reviews.llvm.org/D12989



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


Re: [PATCH] D12989: [CUDA] Added CUDA installation detector class.

2015-09-23 Thread Eric Christopher via cfe-commits
Aha, nice catch!

-eric

On Wed, Sep 23, 2015 at 9:32 PM NAKAMURA Takumi 
wrote:

> chapuni added a subscriber: chapuni.
>
> 
> Comment at: cfe/trunk/lib/Driver/ToolChains.cpp:1492
> @@ +1491,3 @@
> +const llvm::opt::ArgList
> ) {
> +  SmallVector CudaPathCandidates;
> +
> 
> Fixed in r248459.
>
> 
> Comment at: cfe/trunk/lib/Driver/ToolChains.cpp:1498
> @@ +1497,3 @@
> +  else {
> +CudaPathCandidates.push_back(D.SysRoot + "/usr/local/cuda");
> +CudaPathCandidates.push_back(D.SysRoot + "/usr/local/cuda-7.0");
> 
> You were trying to make StringRef from temporary string concatenator.
>
>
> Repository:
>   rL LLVM
>
> http://reviews.llvm.org/D12989
>
>
>
>
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D12989: [CUDA] Added CUDA installation detector class.

2015-09-22 Thread Eric Christopher via cfe-commits
echristo accepted this revision.
echristo added a comment.
This revision is now accepted and ready to land.

LGTM.

Thanks!

-eric


http://reviews.llvm.org/D12989



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


Re: [PATCH] D12989: [CUDA] Added CUDA installation detector class.

2015-09-21 Thread Artem Belevich via cfe-commits
tra updated the summary for this revision.
tra updated this revision to Diff 35333.
tra added a comment.

Added test case for CUDA detection.
Removed libdevice file detection for now.


http://reviews.llvm.org/D12989

Files:
  include/clang/Driver/Options.td
  lib/Driver/ToolChains.cpp
  lib/Driver/ToolChains.h
  test/Driver/Inputs/CUDA/usr/local/cuda/include/.keep
  test/Driver/Inputs/CUDA/usr/local/cuda/lib/.keep
  test/Driver/Inputs/CUDA/usr/local/cuda/lib64/.keep
  test/Driver/Inputs/CUDA/usr/local/cuda/nvvm/libdevice/.keep
  test/Driver/cuda-detect.cu

Index: test/Driver/cuda-detect.cu
===
--- /dev/null
+++ test/Driver/cuda-detect.cu
@@ -0,0 +1,6 @@
+// RUN: %clang -v --sysroot=/tmp/no-cuda-there 2>&1 | FileCheck %s -check-prefix NOCUDA
+// RUN: %clang -v --sysroot=%S/Inputs/CUDA 2>&1 | FileCheck %s
+// RUN: %clang -v --cuda-path=%S/Inputs/CUDA/usr/local/cuda 2>&1 | FileCheck %s
+
+// CHECK: Found CUDA installation: {{.*}}/Inputs/CUDA/usr/local/cuda
+// NOCUDA-NOT: Found CUDA installation:
Index: lib/Driver/ToolChains.h
===
--- lib/Driver/ToolChains.h
+++ lib/Driver/ToolChains.h
@@ -157,6 +157,38 @@
 protected:
   GCCInstallationDetector GCCInstallation;
 
+  // \brief A class to find a viable CUDA installation
+
+  class CudaInstallationDetector {
+bool IsValid;
+std::string CudaInstallPath;
+std::string CudaLibPath;
+std::string CudaLibDevicePath;
+std::string CudaIncludePath;
+
+  public:
+CudaInstallationDetector() : IsValid(false) {}
+void init(const Driver , const llvm::Triple ,
+  const llvm::opt::ArgList );
+
+/// \brief Check whether we detected a valid Cuda install.
+bool isValid() const { return IsValid; }
+/// \brief Print information about the detected CUDA installation.
+void print(raw_ostream ) const;
+
+/// \brief Get the detected Cuda installation path.
+StringRef getInstallPath() const { return CudaInstallPath; }
+/// \brief Get the detected Cuda Include path.
+StringRef getIncludePath() const { return CudaIncludePath; }
+/// \brief Get the detected Cuda library path.
+StringRef getLibPath() const { return CudaLibPath; }
+/// \brief Get the detected Cuda device library path.
+StringRef getLibDevicePath() const { return CudaLibDevicePath; }
+/// \brief Get libdevice file for given architecture
+  };
+
+  CudaInstallationDetector CudaInstallation;
+
 public:
   Generic_GCC(const Driver , const llvm::Triple ,
   const llvm::opt::ArgList );
Index: lib/Driver/ToolChains.cpp
===
--- lib/Driver/ToolChains.cpp
+++ lib/Driver/ToolChains.cpp
@@ -1482,6 +1482,49 @@
 BiarchTripleAliases.push_back(BiarchTriple.str());
 }
 
+// \brief -- try common CUDA installation paths looking for files we need for
+// CUDA compilation.
+
+void
+Generic_GCC::CudaInstallationDetector::init(const Driver ,
+const llvm::Triple ,
+const llvm::opt::ArgList ) {
+  SmallVector CudaPathCandidates;
+
+  if (Args.hasArg(options::OPT_cuda_path_EQ))
+CudaPathCandidates.push_back(
+Args.getLastArgValue(options::OPT_cuda_path_EQ));
+  else {
+std::string Prefix = D.SysRoot.empty() ? "" : D.SysRoot;
+CudaPathCandidates.push_back(Prefix + "/usr/local/cuda");
+CudaPathCandidates.push_back(Prefix + "/usr/local/cuda-7.0");
+  }
+
+  for (const auto CudaPath : CudaPathCandidates) {
+if (CudaPath.empty() || !llvm::sys::fs::exists(CudaPath))
+  continue;
+
+CudaInstallPath = CudaPath;
+CudaIncludePath = CudaInstallPath + "/include";
+CudaLibDevicePath = CudaInstallPath + "/nvvm/libdevice";
+CudaLibPath =
+CudaInstallPath + (TargetTriple.isArch64Bit() ? "/lib64" : "/lib");
+
+if (!(llvm::sys::fs::exists(CudaIncludePath) &&
+  llvm::sys::fs::exists(CudaLibPath) &&
+  llvm::sys::fs::exists(CudaLibDevicePath)))
+  continue;
+
+IsValid = true;
+break;
+  }
+}
+
+void Generic_GCC::CudaInstallationDetector::print(raw_ostream ) const {
+  if (isValid())
+OS << "Found CUDA installation: " << CudaInstallPath << "\n";
+}
+
 namespace {
 // Filter to remove Multilibs that don't exist as a suffix to Path
 class FilterNonExistent {
@@ -2053,7 +2096,7 @@
 
 Generic_GCC::Generic_GCC(const Driver , const llvm::Triple ,
  const ArgList )
-: ToolChain(D, Triple, Args), GCCInstallation() {
+: ToolChain(D, Triple, Args), GCCInstallation(), CudaInstallation() {
   getProgramPaths().push_back(getDriver().getInstalledDir());
   if (getDriver().getInstalledDir() != getDriver().Dir)
 getProgramPaths().push_back(getDriver().Dir);
@@ -2085,6 +2128,7 @@
 void Generic_GCC::printVerboseInfo(raw_ostream ) const {
   // Print the information 

Re: [PATCH] D12989: [CUDA] Added CUDA installation detector class.

2015-09-21 Thread Artem Belevich via cfe-commits
tra added a comment.

I'll add tests for install dir detection. As for detection of bitcode files, 
perhaps I should remove it from this patch and commit it along with the code 
that's going to use it and where I can test it.


http://reviews.llvm.org/D12989



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


Re: [PATCH] D12989: [CUDA] Added CUDA installation detector class.

2015-09-21 Thread Eric Christopher via cfe-commits
echristo added a comment.

In http://reviews.llvm.org/D12989#250247, @tra wrote:

> I'll add tests for install dir detection. As for detection of bitcode files, 
> perhaps I should remove it from this patch and commit it along with the code 
> that's going to use it and where I can test it.


Seems reasonable.

Thanks!

-eric


http://reviews.llvm.org/D12989



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


Re: [PATCH] D12989: [CUDA] Added CUDA installation detector class.

2015-09-18 Thread Artem Belevich via cfe-commits
tra added a comment.

Any suggestions how to test this?


http://reviews.llvm.org/D12989



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


[PATCH] D12989: [CUDA] Added CUDA installation detector class.

2015-09-18 Thread Artem Belevich via cfe-commits
tra created this revision.
tra added a reviewer: echristo.
tra added a subscriber: cfe-commits.

The class searches for a CUDA installation directory and provides accessors for 
relevant bits of info about it.

Added new option --cuda-path= which allows overriding default search 
paths. If it's not specified we look for CUDA installation in
/usr/include/cuda and /usr/include/cuda-7.0

Once CUDA installation is found we look for libdevice bitcode files and 
associate them with appropriate GPU type.



http://reviews.llvm.org/D12989

Files:
  include/clang/Driver/Options.td
  lib/Driver/ToolChains.cpp
  lib/Driver/ToolChains.h

Index: lib/Driver/ToolChains.h
===
--- lib/Driver/ToolChains.h
+++ lib/Driver/ToolChains.h
@@ -157,6 +157,42 @@
 protected:
   GCCInstallationDetector GCCInstallation;
 
+  // \brief A class to find a viable CUDA installation
+
+  class CudaInstallationDetector {
+bool IsValid;
+std::string CudaInstallPath;
+std::string CudaLibPath;
+std::string CudaLibDevicePath;
+std::string CudaIncludePath;
+llvm::StringMap CudaLibDeviceMap;
+
+  public:
+CudaInstallationDetector() : IsValid(false) {}
+void init(const Driver , const llvm::Triple ,
+  const llvm::opt::ArgList );
+
+/// \brief Check whether we detected a valid Cuda install.
+bool isValid() const { return IsValid; }
+/// \brief Print information about the detected CUDA installation.
+void print(raw_ostream ) const;
+
+/// \brief Get the detected Cuda installation path.
+StringRef getInstallPath() const { return CudaInstallPath; }
+/// \brief Get the detected Cuda Include path.
+StringRef getIncludePath() const { return CudaIncludePath; }
+/// \brief Get the detected Cuda library path.
+StringRef getLibPath() const { return CudaLibPath; }
+/// \brief Get the detected Cuda device library path.
+StringRef getLibDevicePath() const { return CudaLibDevicePath; }
+/// \brief Get libdevice file for given architecture
+StringRef getLibDeviceFile(StringRef Gpu) const {
+  return CudaLibDeviceMap.lookup(Gpu);
+}
+  };
+
+  CudaInstallationDetector CudaInstallation;
+
 public:
   Generic_GCC(const Driver , const llvm::Triple ,
   const llvm::opt::ArgList );
Index: lib/Driver/ToolChains.cpp
===
--- lib/Driver/ToolChains.cpp
+++ lib/Driver/ToolChains.cpp
@@ -1482,6 +1482,74 @@
 BiarchTripleAliases.push_back(BiarchTriple.str());
 }
 
+// \brief -- try common CUDA installation paths looking for files we need for
+// CUDA compilation.
+
+void
+Generic_GCC::CudaInstallationDetector::init(const Driver ,
+const llvm::Triple ,
+const llvm::opt::ArgList ) {
+  SmallVector CudaPathCandidates;
+
+  if (Args.hasArg(options::OPT_cuda_path_EQ))
+CudaPathCandidates.push_back(
+Args.getLastArgValue(options::OPT_cuda_path_EQ));
+  else {
+std::string Prefix = D.SysRoot.empty() ? "" : D.SysRoot;
+CudaPathCandidates.push_back(Prefix + "/usr/local/cuda");
+CudaPathCandidates.push_back(Prefix + "/usr/local/cuda-7.0");
+  }
+
+  for (const auto CudaPath : CudaPathCandidates) {
+if (CudaPath.empty() || !llvm::sys::fs::exists(CudaPath))
+  continue;
+
+CudaInstallPath = CudaPath;
+CudaIncludePath = CudaInstallPath + "/include";
+CudaLibDevicePath = CudaInstallPath + "/nvvm/libdevice";
+CudaLibPath =
+CudaInstallPath + (TargetTriple.isArch64Bit() ? "/lib64" : "/lib");
+
+if (!(llvm::sys::fs::exists(CudaIncludePath) &&
+  llvm::sys::fs::exists(CudaLibPath) &&
+  llvm::sys::fs::exists(CudaLibDevicePath)))
+  continue;
+
+const StringRef LibDeviceName = "libdevice.";
+std::error_code EC;
+for (llvm::sys::fs::directory_iterator LI(CudaLibDevicePath, EC), LE;
+ !EC && LI != LE; LI = LI.increment(EC)) {
+  StringRef FilePath = LI->path();
+  StringRef FileName = llvm::sys::path::filename(FilePath);
+  // Process all bitcode filenames that look like libdevice.compute_XX.YY.bc
+  if (!(FileName.startswith(LibDeviceName) && FileName.endswith(".bc")))
+continue;
+  StringRef GpuArch = FileName.slice(
+  LibDeviceName.size(), FileName.find('.', LibDeviceName.size()));
+  CudaLibDeviceMap[GpuArch] = FilePath.str();
+  // Insert map entries for specifc devices with this compute capability.
+  if (GpuArch == "compute_20") {
+CudaLibDeviceMap["sm_20"] = FilePath;
+CudaLibDeviceMap["sm_21"] = FilePath;
+  } else if (GpuArch == "compute_30") {
+CudaLibDeviceMap["sm_30"] = FilePath;
+CudaLibDeviceMap["sm_32"] = FilePath;
+  } else if (GpuArch == "compute_35") {
+CudaLibDeviceMap["sm_35"] = FilePath;
+CudaLibDeviceMap["sm_37"] = FilePath;
+