From 8198d6a5ab1bb0f16874f197f830e1eecd2ecf37 Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Mon, 25 Sep 2023 12:59:16 -0400 Subject: [PATCH 1/5] wip: Add support for CUDA file extension --- indexer/CompilationDatabase.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/indexer/CompilationDatabase.cc b/indexer/CompilationDatabase.cc index e61b6697..d1cec63a 100644 --- a/indexer/CompilationDatabase.cc +++ b/indexer/CompilationDatabase.cc @@ -693,8 +693,10 @@ void ResumableParser::initialize(compdb::File compdb, ParseOptions options) { this->reader.IterativeParseInit(); this->options = options; std::vector extensions; - // Via https://stackoverflow.com/a/3223792/2682729 - for (auto ext : {"c", "C", "cc", "cpp", "CPP", "cxx", "c++"}) { + // Via https://stackoverflow.com/a/3223792/2682729 (for C and C++) + // For CUDA, see https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#basics-cdp1 + // and https://github.com/github-linguist/linguist/blob/master/lib/linguist/languages.yml#L1342-L1346 + for (auto ext : {"c", "C", "cc", "cpp", "CPP", "cxx", "c++", "cu"}) { extensions.emplace_back(llvm::Regex::escape(fmt::format(".{}", ext))); }; this->fileExtensionRegex = From f35cd8b89be24f4b05224535dbe343bd44988da4 Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Thu, 28 Sep 2023 15:40:40 -0400 Subject: [PATCH 2/5] feat: Detect nvcc toolchain and include CUDA dir based on that --- indexer/CompilationDatabase.cc | 47 +++++++++++++++++++++++++++++++++- indexer/CompilationDatabase.h | 1 + 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/indexer/CompilationDatabase.cc b/indexer/CompilationDatabase.cc index d1cec63a..0a68a02a 100644 --- a/indexer/CompilationDatabase.cc +++ b/indexer/CompilationDatabase.cc @@ -118,6 +118,38 @@ struct GccToolchainInfo : ToolchainInfo { } }; +struct NvccToolchainInfo : ToolchainInfo { + scip_clang::AbsolutePath cudaDir; + + NvccToolchainInfo(scip_clang::AbsolutePath cudaDir) + : ToolchainInfo(), cudaDir(cudaDir) {} + + virtual CompilerKind kind() const override { + return CompilerKind::Nvcc; + } + + virtual bool isWellFormed() const override { + auto path = scip_clang::joinPath(cudaDir.asStringRef(), "include"); + if (!std::filesystem::exists(path)) { + spdlog::error( + "directory '{}' does not exist; expected to find CUDA SDK headers" + " there because nvcc was found at {}", + path, + scip_clang::joinPath(cudaDir.asStringRef(), + scip_clang::joinPath("bin", "nvcc"))); + return false; + } + return true; + } + + virtual void + adjustCommandLine(std::vector &commandLine) const override { + commandLine.push_back( + fmt::format("-isystem{}{}include", this->cudaDir.asStringRef(), + std::filesystem::path::preferred_separator)); + } +}; + } // namespace static CompletedProcess runProcess(std::vector &args, @@ -231,7 +263,20 @@ ToolchainInfo::infer(const scip_clang::AbsolutePath &compilerPath) { findSearchDirsInvocation); } - spdlog::warn("compiler at '{}' is not one of clang/clang++/gcc/g++", + std::vector argv = {compilerPath.asStringRef(), "--version"}; + auto compilerVersionResult = ::runProcess(argv, "checking for NVCC"); + if (compilerVersionResult.isSuccess() + && !compilerVersionResult.stdoutLines.empty() + && absl::StrContains(compilerVersionResult.stdoutLines[0], "NVIDIA")) { + if (auto binDir = compilerPath.asRef().prefix()) { + if (auto cudaDir = binDir->prefix()) { + return std::make_unique( + scip_clang::AbsolutePath(*cudaDir)); + } + } + } + + spdlog::warn("compiler at '{}' is not one of clang/clang++/gcc/g++/nvcc", compilerPath.asStringRef()); noteStdlib(); return failure; diff --git a/indexer/CompilationDatabase.h b/indexer/CompilationDatabase.h index b548c0f2..0f1a4b87 100644 --- a/indexer/CompilationDatabase.h +++ b/indexer/CompilationDatabase.h @@ -108,6 +108,7 @@ class CommandObjectHandler enum class CompilerKind { Gcc, Clang, + Nvcc, }; struct ToolchainInfo { From 4958027f1d969d43ee902750bbc048793988e889 Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Thu, 28 Sep 2023 15:50:20 -0400 Subject: [PATCH 3/5] docs: Mention how to test with NVCC --- docs/Development.md | 9 +++++++++ tools/vm-setup-cuda.sh | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100755 tools/vm-setup-cuda.sh diff --git a/docs/Development.md b/docs/Development.md index c835a11f..c879bc51 100644 --- a/docs/Development.md +++ b/docs/Development.md @@ -11,6 +11,7 @@ - [Stacktraces](#stacktraces) - [Attaching a debugger](#attaching-a-debugger) - [Debugging on Linux](#debugging-on-linux) + - [Testing CUDA/NVCC support](#testing-cudanvcc-support) - [Inspecting Clang ASTs](#inspecting-clang-asts) - [Automated test case reduction](#automated-test-case-reduction) - [Debugging preprocessor issues](#debugging-preprocessor-issues) @@ -162,6 +163,14 @@ There is a [VM setup script](/tools/vm-setup.sh) available to configure a GCP VM for building scip-clang. We recommend using Ubuntu 20.04+ with 16 cores or more. +#### Testing CUDA/NVCC support + +There is a [CUDA-specific VM setup script](/tools/vm-setup-cuda.sh) +which installs the CUDA SDK. Use it in a GCP VM +which has a GPU attached. + +You may need to restart your shell for changes to take effect. + ### Inspecting Clang ASTs Print the AST nodes: diff --git a/tools/vm-setup-cuda.sh b/tools/vm-setup-cuda.sh new file mode 100755 index 00000000..d597f60f --- /dev/null +++ b/tools/vm-setup-cuda.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb +sudo dpkg -i cuda-keyring_1.1-1_all.deb +rm -rf cuda-keyring_1.1-1_all.deb +sudo apt-get update +sudo apt-get -y install cuda + +ZSH_INIT="$HOME/.zshrc" +if [ -f "$ZSH_INIT" ] && ! grep -q "cuda" "$ZSH_INIT"; then + echo 'export PATH="/usr/local/cuda/bin:$PATH"' >> "$ZSH_INIT" + echo "Added CUDA path to $ZSH_INIT" +fi + +BASH_INIT="$HOME/.bashrc" +if [ -f "$BASH_INIT" ] && ! grep -q "cuda" "$BASH_INIT"; then + echo 'export PATH="/usr/local/cuda/bin:$PATH"' >> "$BASH_INIT" + echo "Added CUDA path to $BASH_INIT" +fi + +FISH_INIT="$HOME/.config/fish/config.fish" +if [ -f "$FISH_INIT" ] && ! grep -q "cuda" "$FISH_INIT"; then + echo "fish_add_path /usr/local/cuda/bin" >> "$FISH_INIT" + echo "Added CUDA path to $FISH_INIT" +fi From aa5ea4e0d55aea9d098bbcfb437f36a5ce2f6a2c Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Thu, 28 Sep 2023 17:04:17 -0400 Subject: [PATCH 4/5] docs: Describe how to index apache/mxnet for CUDA demo --- docs/IndexingProjects.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/IndexingProjects.md b/docs/IndexingProjects.md index 33b60d59..d2326803 100644 --- a/docs/IndexingProjects.md +++ b/docs/IndexingProjects.md @@ -6,6 +6,7 @@ - [Redpanda](#redpanda) - [Postgres](#postgres) - [Boost](#boost) +- [Apache MXNet](#apache-mxnet) ## scip-clang @@ -224,3 +225,25 @@ for d in dirs: print("Indexing failed for {}; skipping upload".format(d)) continue ``` + +## Apache MXNet + +Apache MXNet is an interesting project to test because +it has over 100 CUDA files. + +The indexing steps below are based on the [official instructions](https://mxnet.apache.org/versions/1.9.1/get_started/build_from_source.html). + +``` +git clone --recursive https://github.com/apache/mxnet +cd mxnet +sudo apt-get update +sudo apt-get install -y build-essential git ninja-build ccache libopenblas-dev libopencv-dev cmake gfortran clang++-14 clang libomp-dev +``` + +``` +cmake -B build -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DUSE_CUDA=ON + +cat build/compile_commands.json | jq --arg rdir "$(clang -print-resource-dir)" 'map(if .command | contains("nvcc") then .command += " -resource-dir " + $rdir else . end)' > build/modified.json + +scip-clang --compdb-path build/modified.json +``` From ece823dad5e72b0230e0ef9de6dbe045f014a2c9 Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Thu, 28 Sep 2023 17:07:04 -0400 Subject: [PATCH 5/5] cleanup: Turn off auto-formatting for comment --- indexer/CompilationDatabase.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/indexer/CompilationDatabase.cc b/indexer/CompilationDatabase.cc index 0a68a02a..43f86b43 100644 --- a/indexer/CompilationDatabase.cc +++ b/indexer/CompilationDatabase.cc @@ -738,9 +738,11 @@ void ResumableParser::initialize(compdb::File compdb, ParseOptions options) { this->reader.IterativeParseInit(); this->options = options; std::vector extensions; + // clang-format off // Via https://stackoverflow.com/a/3223792/2682729 (for C and C++) // For CUDA, see https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#basics-cdp1 // and https://github.com/github-linguist/linguist/blob/master/lib/linguist/languages.yml#L1342-L1346 + // clang-format on for (auto ext : {"c", "C", "cc", "cpp", "CPP", "cxx", "c++", "cu"}) { extensions.emplace_back(llvm::Regex::escape(fmt::format(".{}", ext))); };