[clang] 7dc23ab - [CUDA] Add a flag to manually specify the target feature to use with CUDA

Joseph Huber via cfe-commits cfe-commits at lists.llvm.org
Fri May 13 13:31:08 PDT 2022


Author: Joseph Huber
Date: 2022-05-13T16:30:58-04:00
New Revision: 7dc23abbd3b298016cc6ae49a124f414e76903b6

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

LOG: [CUDA] Add a flag to manually specify the target feature to use with CUDA

Summary:
Normally we parse through the CUDA installation to disover the needed
features. However, we may want to build libraries on targets that do not
currently have CUDA installed but still need to know which features to
make use of when creating the PTX or bitcode. This flag is a simple way
to specify this so we can compile certain codes withotu a valid CUDA
installation.

Ideally this could be done via an -Xarch or simimlar flag but currently
they cannot handle this. We would need to support using an -Xarch flag
that takes multiple arguments that then pass them to the -Xclang
functionality.

Added: 
    

Modified: 
    clang/include/clang/Driver/Options.td
    clang/lib/Driver/ToolChains/Cuda.cpp
    clang/test/Driver/cuda-openmp-driver.cu

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index ecdddff5b683..4be0b574fa44 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -917,6 +917,7 @@ def offload_arch_EQ : Joined<["--"], "offload-arch=">, Flags<[NoXarchOption]>,
            "specified more than once.">;
 def cuda_gpu_arch_EQ : Joined<["--"], "cuda-gpu-arch=">, Flags<[NoXarchOption]>,
   Alias<offload_arch_EQ>;
+def cuda_feature_EQ : Joined<["--"], "cuda-feature=">, HelpText<"Manually specify the CUDA feature to use">;
 def hip_link : Flag<["--"], "hip-link">,
   HelpText<"Link clang-offload-bundler bundles for HIP">;
 def no_hip_rt: Flag<["-"], "no-hip-rt">,

diff  --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index 666f568187b2..9ce52cd34a3f 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -637,7 +637,12 @@ void NVPTX::getNVPTXTargetFeatures(const Driver &D, const llvm::Triple &Triple,
                                    const llvm::opt::ArgList &Args,
                                    std::vector<StringRef> &Features,
                                    Optional<clang::CudaVersion> Version) {
-  if (!Version) {
+  if (Args.hasArg(options::OPT_cuda_feature_EQ)) {
+    StringRef PtxFeature =
+        Args.getAllArgValues(options::OPT_cuda_feature_EQ).back();
+    Features.push_back(Args.MakeArgString(PtxFeature));
+    return;
+  } else if (!Version) {
     CudaInstallationDetector CudaInstallation(D, Triple, Args);
     Version = CudaInstallation.version();
   }

diff  --git a/clang/test/Driver/cuda-openmp-driver.cu b/clang/test/Driver/cuda-openmp-driver.cu
index e10bc72a252a..dec0288db263 100644
--- a/clang/test/Driver/cuda-openmp-driver.cu
+++ b/clang/test/Driver/cuda-openmp-driver.cu
@@ -32,3 +32,6 @@
 // BINDINGS-DEVICE: # "nvptx64-nvidia-cuda" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[PTX:.+]]"
 // BINDINGS-DEVICE: # "nvptx64-nvidia-cuda" - "NVPTX::Assembler", inputs: ["[[PTX]]"], output: "[[CUBIN:.+]]"
 // BINDINGS-DEVICE: # "nvptx64-nvidia-cuda" - "NVPTX::Linker", inputs: ["[[CUBIN]]", "[[PTX]]"], output: "{{.*}}.fatbin"
+
+// RUN: %clang -### -target x86_64-linux-gnu -nocudalib --cuda-feature=+ptx61 --offload-arch=sm_70 %s 2>&1 | FileCheck -check-prefix MANUAL-FEATURE %s
+// MANUAL-FEATURE: -cc1{{.*}}-target-feature{{.*}}+ptx61


        


More information about the cfe-commits mailing list