1 //===--- Cuda.h - Cuda ToolChain Implementations ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_CUDA_H
11 #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_CUDA_H
13 #include "clang/Basic/Cuda.h"
14 #include "clang/Driver/Action.h"
15 #include "clang/Driver/Multilib.h"
16 #include "clang/Driver/Tool.h"
17 #include "clang/Driver/ToolChain.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/VersionTuple.h"
28 /// A class to find a viable CUDA installation
29 class CudaInstallationDetector {
33 CudaVersion Version = CudaVersion::UNKNOWN;
34 std::string InstallPath;
37 std::string LibDevicePath;
38 std::string IncludePath;
39 llvm::StringMap<std::string> LibDeviceMap;
41 // CUDA architectures for which we have raised an error in
42 // CheckCudaVersionSupportsArch.
43 mutable llvm::SmallSet<CudaArch, 4> ArchsWithBadVersion;
46 CudaInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
47 const llvm::opt::ArgList &Args);
49 void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
50 llvm::opt::ArgStringList &CC1Args) const;
52 /// Emit an error if Version does not support the given Arch.
54 /// If either Version or Arch is unknown, does not emit an error. Emits at
55 /// most one error per Arch.
56 void CheckCudaVersionSupportsArch(CudaArch Arch) const;
58 /// Check whether we detected a valid Cuda install.
59 bool isValid() const { return IsValid; }
60 /// Print information about the detected CUDA installation.
61 void print(raw_ostream &OS) const;
63 /// Get the detected Cuda install's version.
64 CudaVersion version() const { return Version; }
65 /// Get the detected Cuda installation path.
66 StringRef getInstallPath() const { return InstallPath; }
67 /// Get the detected path to Cuda's bin directory.
68 StringRef getBinPath() const { return BinPath; }
69 /// Get the detected Cuda Include path.
70 StringRef getIncludePath() const { return IncludePath; }
71 /// Get the detected Cuda library path.
72 StringRef getLibPath() const { return LibPath; }
73 /// Get the detected Cuda device library path.
74 StringRef getLibDevicePath() const { return LibDevicePath; }
75 /// Get libdevice file for given architecture
76 std::string getLibDeviceFile(StringRef Gpu) const {
77 return LibDeviceMap.lookup(Gpu);
84 // Run ptxas, the NVPTX assembler.
85 class LLVM_LIBRARY_VISIBILITY Assembler : public Tool {
87 Assembler(const ToolChain &TC)
88 : Tool("NVPTX::Assembler", "ptxas", TC, RF_Full, llvm::sys::WEM_UTF8,
91 bool hasIntegratedCPP() const override { return false; }
93 void ConstructJob(Compilation &C, const JobAction &JA,
94 const InputInfo &Output, const InputInfoList &Inputs,
95 const llvm::opt::ArgList &TCArgs,
96 const char *LinkingOutput) const override;
99 // Runs fatbinary, which combines GPU object files ("cubin" files) and/or PTX
100 // assembly into a single output file.
101 class LLVM_LIBRARY_VISIBILITY Linker : public Tool {
103 Linker(const ToolChain &TC)
104 : Tool("NVPTX::Linker", "fatbinary", TC, RF_Full, llvm::sys::WEM_UTF8,
107 bool hasIntegratedCPP() const override { return false; }
109 void ConstructJob(Compilation &C, const JobAction &JA,
110 const InputInfo &Output, const InputInfoList &Inputs,
111 const llvm::opt::ArgList &TCArgs,
112 const char *LinkingOutput) const override;
115 class LLVM_LIBRARY_VISIBILITY OpenMPLinker : public Tool {
117 OpenMPLinker(const ToolChain &TC)
118 : Tool("NVPTX::OpenMPLinker", "nvlink", TC, RF_Full, llvm::sys::WEM_UTF8,
121 bool hasIntegratedCPP() const override { return false; }
123 void ConstructJob(Compilation &C, const JobAction &JA,
124 const InputInfo &Output, const InputInfoList &Inputs,
125 const llvm::opt::ArgList &TCArgs,
126 const char *LinkingOutput) const override;
129 } // end namespace NVPTX
130 } // end namespace tools
132 namespace toolchains {
134 class LLVM_LIBRARY_VISIBILITY CudaToolChain : public ToolChain {
136 CudaToolChain(const Driver &D, const llvm::Triple &Triple,
137 const ToolChain &HostTC, const llvm::opt::ArgList &Args,
138 const Action::OffloadKind OK);
140 const llvm::Triple *getAuxTriple() const override {
141 return &HostTC.getTriple();
144 std::string getInputFilename(const InputInfo &Input) const override;
146 llvm::opt::DerivedArgList *
147 TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
148 Action::OffloadKind DeviceOffloadKind) const override;
149 void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
150 llvm::opt::ArgStringList &CC1Args,
151 Action::OffloadKind DeviceOffloadKind) const override;
153 // Never try to use the integrated assembler with CUDA; always fork out to
155 bool useIntegratedAs() const override { return false; }
156 bool isCrossCompiling() const override { return true; }
157 bool isPICDefault() const override { return false; }
158 bool isPIEDefault() const override { return false; }
159 bool isPICDefaultForced() const override { return false; }
160 bool SupportsProfiling() const override { return false; }
161 bool supportsDebugInfoOption(const llvm::opt::Arg *A) const override;
162 bool IsMathErrnoDefault() const override { return false; }
164 void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
165 llvm::opt::ArgStringList &CC1Args) const override;
167 void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override;
168 CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override;
170 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
171 llvm::opt::ArgStringList &CC1Args) const override;
172 void AddClangCXXStdlibIncludeArgs(
173 const llvm::opt::ArgList &Args,
174 llvm::opt::ArgStringList &CC1Args) const override;
175 void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,
176 llvm::opt::ArgStringList &CC1Args) const override;
178 SanitizerMask getSupportedSanitizers() const override;
181 computeMSVCVersion(const Driver *D,
182 const llvm::opt::ArgList &Args) const override;
184 unsigned GetDefaultDwarfVersion() const override { return 2; }
186 const ToolChain &HostTC;
187 CudaInstallationDetector CudaInstallation;
190 Tool *buildAssembler() const override; // ptxas
191 Tool *buildLinker() const override; // fatbinary (ok, not really a linker)
194 const Action::OffloadKind OK;
197 } // end namespace toolchains
198 } // end namespace driver
199 } // end namespace clang
201 #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_CUDA_H