1//===--- SYCL.cpp - SYCL Tool and 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#include "SYCL.h"
9#include "SPIRV.h"
10#include "clang/Driver/CommonArgs.h"
11#include "llvm/Support/VirtualFileSystem.h"
12
13using namespace clang::driver;
14using namespace clang::driver::toolchains;
15using namespace clang::driver::tools;
16using namespace clang;
17using namespace llvm::opt;
18
19SYCLInstallationDetector::SYCLInstallationDetector(
20 const Driver &D, const llvm::Triple &HostTriple,
21 const llvm::opt::ArgList &Args)
22 : D(D) {
23 // When -fsycl is active, locate the SYCL runtime library and record its
24 // directory in SYCLRTLibPath for use by the linker.
25 SmallString<128> DriverDir(D.Dir);
26
27 if (HostTriple.isWindowsMSVCEnvironment() ||
28 HostTriple.isWindowsItaniumEnvironment()) {
29 // Windows: Check for LLVMSYCL.lib
30 // NOTE: Only checks for LLVMSYCL.lib existence (release variant).
31 // Debug vs release library selection happens at link time based on CRT
32 // flags.
33 if (Args.hasFlag(Pos: options::OPT_fsycl, Neg: options::OPT_fno_sycl, Default: false)) {
34 SmallString<128> LibDir(DriverDir);
35 llvm::sys::path::append(path&: LibDir, a: "..", b: "lib");
36
37 // Verify SYCL runtime library exists
38 SmallString<128> SYCLLibPath(LibDir);
39 llvm::sys::path::append(path&: SYCLLibPath, a: "LLVMSYCL.lib");
40
41 if (D.getVFS().exists(Path: SYCLLibPath))
42 SYCLRTLibPath = LibDir;
43 }
44 } else {
45 // Linux/Unix: Check for libLLVMSYCL.so
46 SmallString<128> LibPath(DriverDir);
47 llvm::sys::path::append(path&: LibPath, a: "..", b: "lib", c: HostTriple.str(),
48 d: "libLLVMSYCL.so");
49 // Flat lib path for LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF builds,
50 // where the library is installed directly in lib/ with no triple subdir.
51 SmallString<128> FlatLibPath(DriverDir);
52 llvm::sys::path::append(path&: FlatLibPath, a: "..", b: "lib", c: "libLLVMSYCL.so");
53
54 if (Args.hasFlag(Pos: options::OPT_fsycl, Neg: options::OPT_fno_sycl, Default: false)) {
55 // LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON: library is in lib/<triple>/
56 if (D.getVFS().exists(Path: LibPath))
57 llvm::sys::path::append(path&: DriverDir, a: "..", b: "lib", c: HostTriple.str());
58 // LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF: library is in lib/
59 else if (D.getVFS().exists(Path: FlatLibPath))
60 llvm::sys::path::append(path&: DriverDir, a: "..", b: "lib");
61 else
62 return; // Neither path exists : broken install, leave SYCLRTLibPath
63 // unset
64
65 SYCLRTLibPath = DriverDir;
66 }
67 }
68}
69
70void SYCLInstallationDetector::addSYCLIncludeArgs(
71 const ArgList &DriverArgs, ArgStringList &CC1Args) const {
72 if (DriverArgs.hasArg(Ids: options::OPT_nobuiltininc))
73 return;
74
75 // Add the SYCL header search locations.
76 // These are included for both SYCL host and device compilations.
77 SmallString<128> IncludePath(D.Dir);
78 llvm::sys::path::append(path&: IncludePath, a: "..", b: "include");
79 CC1Args.push_back(Elt: "-internal-isystem");
80 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: IncludePath));
81}
82
83// Unsupported options for SYCL device compilation.
84static ArrayRef<options::ID> getUnsupportedOpts() {
85 static constexpr options::ID UnsupportedOpts[] = {
86 options::OPT_fsanitize_EQ, // -fsanitize
87 options::OPT_fcf_protection_EQ, // -fcf-protection
88 options::OPT_fprofile_generate,
89 options::OPT_fprofile_generate_EQ,
90 options::OPT_fno_profile_generate, // -f[no-]profile-generate
91 options::OPT_ftest_coverage,
92 options::OPT_fno_test_coverage, // -f[no-]test-coverage
93 options::OPT_fcoverage_mapping,
94 options::OPT_fno_coverage_mapping, // -f[no-]coverage-mapping
95 options::OPT_coverage, // --coverage
96 options::OPT_fprofile_instr_generate,
97 options::OPT_fprofile_instr_generate_EQ,
98 options::OPT_fno_profile_instr_generate, // -f[no-]profile-instr-generate
99 options::OPT_fprofile_arcs,
100 options::OPT_fno_profile_arcs, // -f[no-]profile-arcs
101 options::OPT_fcreate_profile, // -fcreate-profile
102 options::OPT_fprofile_instr_use,
103 options::OPT_fprofile_instr_use_EQ, // -fprofile-instr-use
104 options::OPT_fcs_profile_generate, // -fcs-profile-generate
105 options::OPT_fcs_profile_generate_EQ,
106 };
107 return UnsupportedOpts;
108}
109
110SYCLToolChain::SYCLToolChain(const Driver &D, const llvm::Triple &Triple,
111 const ToolChain &HostTC, const ArgList &Args)
112 : ToolChain(D, Triple, Args), HostTC(HostTC),
113 SYCLInstallation(D, Triple, Args) {
114 // Lookup binaries into the driver directory, this is used to discover any
115 // dependent SYCL offload compilation tools.
116 getProgramPaths().push_back(Elt: getDriver().Dir);
117
118 // Diagnose unsupported options only once.
119 for (OptSpecifier Opt : getUnsupportedOpts()) {
120 if (const Arg *A = Args.getLastArg(Ids: Opt)) {
121 D.Diag(DiagID: clang::diag::warn_drv_unsupported_option_for_target)
122 << A->getAsString(Args) << getTriple().str();
123 }
124 }
125}
126
127void SYCLToolChain::addClangTargetOptions(
128 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
129 BoundArch BA, Action::OffloadKind DeviceOffloadingKind) const {
130 HostTC.addClangTargetOptions(DriverArgs, CC1Args, BA, DeviceOffloadKind: DeviceOffloadingKind);
131}
132
133llvm::opt::DerivedArgList *
134SYCLToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
135 BoundArch BA,
136 Action::OffloadKind DeviceOffloadKind) const {
137 DerivedArgList *DAL = HostTC.TranslateArgs(Args, BA, DeviceOffloadKind);
138
139 bool IsNewDAL = false;
140 if (!DAL) {
141 DAL = new DerivedArgList(Args.getBaseArgs());
142 IsNewDAL = true;
143 }
144
145 for (Arg *A : Args) {
146 // Filter out any options we do not want to pass along to the device
147 // compilation.
148 auto Opt(A->getOption());
149 bool Unsupported = false;
150 for (OptSpecifier UnsupportedOpt : getUnsupportedOpts()) {
151 if (Opt.matches(ID: UnsupportedOpt)) {
152 if (Opt.getID() == options::OPT_fsanitize_EQ &&
153 A->getValues().size() == 1) {
154 std::string SanitizeVal = A->getValue();
155 if (SanitizeVal == "address") {
156 if (IsNewDAL)
157 DAL->append(A);
158 continue;
159 }
160 }
161 if (!IsNewDAL)
162 DAL->eraseArg(Id: Opt.getID());
163 Unsupported = true;
164 }
165 }
166 if (Unsupported)
167 continue;
168 if (IsNewDAL)
169 DAL->append(A);
170 }
171
172 const OptTable &Opts = getDriver().getOpts();
173 if (BA) {
174 DAL->eraseArg(Id: options::OPT_march_EQ);
175 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_march_EQ),
176 Value: BA.ArchName);
177 }
178 return DAL;
179}
180
181void SYCLToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
182 HostTC.addClangWarningOptions(CC1Args);
183}
184
185Tool *SYCLToolChain::buildLinker() const {
186 // The SPIR-V linker dispatches to clang-sycl-linker for SYCL offloading.
187 return new tools::SPIRV::Linker(*this);
188}
189
190ToolChain::CXXStdlibType
191SYCLToolChain::GetCXXStdlibType(const ArgList &Args) const {
192 return HostTC.GetCXXStdlibType(Args);
193}
194
195void SYCLToolChain::addSYCLIncludeArgs(const ArgList &DriverArgs,
196 ArgStringList &CC1Args) const {
197 SYCLInstallation.addSYCLIncludeArgs(DriverArgs, CC1Args);
198}
199
200void SYCLToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
201 ArgStringList &CC1Args) const {
202 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
203}
204
205void SYCLToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
206 ArgStringList &CC1Args) const {
207 HostTC.AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args);
208}
209
210VersionTuple SYCLToolChain::computeMSVCVersion(const Driver *D,
211 const ArgList &Args) const {
212 return HostTC.computeMSVCVersion(D, Args);
213}
214