| 1 | //===--- Cuda.h - Cuda ToolChain Implementations ----------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_CUDA_H |
| 10 | #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_CUDA_H |
| 11 | |
| 12 | #include "clang/Basic/Cuda.h" |
| 13 | #include "clang/Driver/Action.h" |
| 14 | #include "clang/Driver/CudaInstallationDetector.h" |
| 15 | #include "clang/Driver/Multilib.h" |
| 16 | #include "clang/Driver/Tool.h" |
| 17 | #include "clang/Driver/ToolChain.h" |
| 18 | #include "llvm/Support/Compiler.h" |
| 19 | #include "llvm/Support/VersionTuple.h" |
| 20 | #include <bitset> |
| 21 | #include <set> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace clang { |
| 25 | namespace driver { |
| 26 | namespace tools { |
| 27 | namespace NVPTX { |
| 28 | |
| 29 | // Run ptxas, the NVPTX assembler. |
| 30 | class LLVM_LIBRARY_VISIBILITY Assembler final : public Tool { |
| 31 | public: |
| 32 | Assembler(const ToolChain &TC) : Tool("NVPTX::Assembler" , "ptxas" , TC) {} |
| 33 | |
| 34 | bool hasIntegratedCPP() const override { return false; } |
| 35 | |
| 36 | void ConstructJob(Compilation &C, const JobAction &JA, |
| 37 | const InputInfo &Output, const InputInfoList &Inputs, |
| 38 | const llvm::opt::ArgList &TCArgs, |
| 39 | const char *LinkingOutput) const override; |
| 40 | }; |
| 41 | |
| 42 | // Runs fatbinary, which combines GPU object files ("cubin" files) and/or PTX |
| 43 | // assembly into a single output file. |
| 44 | class LLVM_LIBRARY_VISIBILITY FatBinary : public Tool { |
| 45 | public: |
| 46 | FatBinary(const ToolChain &TC) : Tool("NVPTX::Linker" , "fatbinary" , TC) {} |
| 47 | |
| 48 | bool hasIntegratedCPP() const override { return false; } |
| 49 | |
| 50 | void ConstructJob(Compilation &C, const JobAction &JA, |
| 51 | const InputInfo &Output, const InputInfoList &Inputs, |
| 52 | const llvm::opt::ArgList &TCArgs, |
| 53 | const char *LinkingOutput) const override; |
| 54 | }; |
| 55 | |
| 56 | // Runs nvlink, which links GPU object files ("cubin" files) into a single file. |
| 57 | class LLVM_LIBRARY_VISIBILITY Linker final : public Tool { |
| 58 | public: |
| 59 | Linker(const ToolChain &TC) : Tool("NVPTX::Linker" , "nvlink" , TC) {} |
| 60 | |
| 61 | bool hasIntegratedCPP() const override { return false; } |
| 62 | |
| 63 | void ConstructJob(Compilation &C, const JobAction &JA, |
| 64 | const InputInfo &Output, const InputInfoList &Inputs, |
| 65 | const llvm::opt::ArgList &TCArgs, |
| 66 | const char *LinkingOutput) const override; |
| 67 | }; |
| 68 | |
| 69 | void getNVPTXTargetFeatures(const Driver &D, const llvm::Triple &Triple, |
| 70 | const llvm::opt::ArgList &Args, |
| 71 | std::vector<StringRef> &Features); |
| 72 | |
| 73 | } // end namespace NVPTX |
| 74 | } // end namespace tools |
| 75 | |
| 76 | namespace toolchains { |
| 77 | |
| 78 | class LLVM_LIBRARY_VISIBILITY NVPTXToolChain : public ToolChain { |
| 79 | public: |
| 80 | NVPTXToolChain(const Driver &D, const llvm::Triple &Triple, |
| 81 | const llvm::Triple &HostTriple, |
| 82 | const llvm::opt::ArgList &Args); |
| 83 | |
| 84 | NVPTXToolChain(const Driver &D, const llvm::Triple &Triple, |
| 85 | const llvm::opt::ArgList &Args); |
| 86 | |
| 87 | llvm::opt::DerivedArgList * |
| 88 | TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, |
| 89 | Action::OffloadKind DeviceOffloadKind) const override; |
| 90 | |
| 91 | void |
| 92 | addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
| 93 | llvm::opt::ArgStringList &CC1Args, |
| 94 | Action::OffloadKind DeviceOffloadKind) const override; |
| 95 | void |
| 96 | AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, |
| 97 | llvm::opt::ArgStringList &CC1Args) const override; |
| 98 | |
| 99 | // Never try to use the integrated assembler with CUDA; always fork out to |
| 100 | // ptxas. |
| 101 | bool useIntegratedAs() const override { return false; } |
| 102 | bool isCrossCompiling() const override { return true; } |
| 103 | bool isPICDefault() const override { return false; } |
| 104 | bool isPIEDefault(const llvm::opt::ArgList &Args) const override { |
| 105 | return false; |
| 106 | } |
| 107 | bool HasNativeLLVMSupport() const override { return true; } |
| 108 | bool isPICDefaultForced() const override { return false; } |
| 109 | bool SupportsProfiling() const override { return false; } |
| 110 | |
| 111 | bool IsMathErrnoDefault() const override { return false; } |
| 112 | |
| 113 | bool supportsDebugInfoOption(const llvm::opt::Arg *A) const override; |
| 114 | void adjustDebugInfoKind(llvm::codegenoptions::DebugInfoKind &DebugInfoKind, |
| 115 | const llvm::opt::ArgList &Args) const override; |
| 116 | |
| 117 | // NVPTX supports only DWARF2. |
| 118 | unsigned GetDefaultDwarfVersion() const override { return 2; } |
| 119 | unsigned getMaxDwarfVersion() const override { return 2; } |
| 120 | |
| 121 | /// Uses nvptx-arch tool to get arch of the system GPU. Will return error |
| 122 | /// if unable to find one. |
| 123 | virtual Expected<SmallVector<std::string>> |
| 124 | getSystemGPUArchs(const llvm::opt::ArgList &Args) const override; |
| 125 | |
| 126 | CudaInstallationDetector CudaInstallation; |
| 127 | |
| 128 | protected: |
| 129 | Tool *buildAssembler() const override; // ptxas. |
| 130 | Tool *buildLinker() const override; // nvlink. |
| 131 | }; |
| 132 | |
| 133 | class LLVM_LIBRARY_VISIBILITY CudaToolChain : public NVPTXToolChain { |
| 134 | public: |
| 135 | CudaToolChain(const Driver &D, const llvm::Triple &Triple, |
| 136 | const ToolChain &HostTC, const llvm::opt::ArgList &Args); |
| 137 | |
| 138 | const llvm::Triple *getAuxTriple() const override { |
| 139 | return &HostTC.getTriple(); |
| 140 | } |
| 141 | |
| 142 | bool HasNativeLLVMSupport() const override { return false; } |
| 143 | |
| 144 | std::string getInputFilename(const InputInfo &Input) const override; |
| 145 | |
| 146 | llvm::opt::DerivedArgList * |
| 147 | TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, |
| 148 | Action::OffloadKind DeviceOffloadKind) const override; |
| 149 | void |
| 150 | addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
| 151 | llvm::opt::ArgStringList &CC1Args, |
| 152 | Action::OffloadKind DeviceOffloadKind) const override; |
| 153 | |
| 154 | llvm::DenormalMode getDefaultDenormalModeForType( |
| 155 | const llvm::opt::ArgList &DriverArgs, const JobAction &JA, |
| 156 | const llvm::fltSemantics *FPType = nullptr) const override; |
| 157 | |
| 158 | void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, |
| 159 | llvm::opt::ArgStringList &CC1Args) const override; |
| 160 | |
| 161 | void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override; |
| 162 | CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override; |
| 163 | void |
| 164 | AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, |
| 165 | llvm::opt::ArgStringList &CC1Args) const override; |
| 166 | void AddClangCXXStdlibIncludeArgs( |
| 167 | const llvm::opt::ArgList &Args, |
| 168 | llvm::opt::ArgStringList &CC1Args) const override; |
| 169 | void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, |
| 170 | llvm::opt::ArgStringList &CC1Args) const override; |
| 171 | |
| 172 | SanitizerMask getSupportedSanitizers() const override; |
| 173 | |
| 174 | VersionTuple |
| 175 | computeMSVCVersion(const Driver *D, |
| 176 | const llvm::opt::ArgList &Args) const override; |
| 177 | |
| 178 | const ToolChain &HostTC; |
| 179 | |
| 180 | protected: |
| 181 | Tool *buildAssembler() const override; // ptxas |
| 182 | Tool *buildLinker() const override; // fatbinary (ok, not really a linker) |
| 183 | }; |
| 184 | |
| 185 | } // end namespace toolchains |
| 186 | } // end namespace driver |
| 187 | } // end namespace clang |
| 188 | |
| 189 | #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_CUDA_H |
| 190 | |