clang 24.0.0git
SYCL.cpp
Go to the documentation of this file.
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"
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
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(options::OPT_fsycl, options::OPT_fno_sycl, false)) {
34 SmallString<128> LibDir(DriverDir);
35 llvm::sys::path::append(LibDir, "..", "lib");
36
37 // Verify SYCL runtime library exists
38 SmallString<128> SYCLLibPath(LibDir);
39 llvm::sys::path::append(SYCLLibPath, "LLVMSYCL.lib");
40
41 if (D.getVFS().exists(SYCLLibPath))
42 SYCLRTLibPath = LibDir;
43 }
44 } else {
45 // Linux/Unix: Check for libLLVMSYCL.so
46 SmallString<128> LibPath(DriverDir);
47 llvm::sys::path::append(LibPath, "..", "lib", HostTriple.str(),
48 "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(FlatLibPath, "..", "lib", "libLLVMSYCL.so");
53
54 if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) {
55 // LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON: library is in lib/<triple>/
56 if (D.getVFS().exists(LibPath))
57 llvm::sys::path::append(DriverDir, "..", "lib", HostTriple.str());
58 // LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF: library is in lib/
59 else if (D.getVFS().exists(FlatLibPath))
60 llvm::sys::path::append(DriverDir, "..", "lib");
61 else
62 return; // Neither path exists : broken install, leave SYCLRTLibPath
63 // unset
64
65 SYCLRTLibPath = DriverDir;
66 }
67 }
68}
69
71 const ArgList &DriverArgs, ArgStringList &CC1Args) const {
72 if (DriverArgs.hasArg(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(IncludePath, "..", "include");
79 CC1Args.push_back("-internal-isystem");
80 CC1Args.push_back(DriverArgs.MakeArgString(IncludePath));
81}
82
83// Unsupported options for SYCL device compilation.
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(getDriver().Dir);
117
118 // Diagnose unsupported options only once.
119 for (OptSpecifier Opt : getUnsupportedOpts()) {
120 if (const Arg *A = Args.getLastArg(Opt)) {
121 D.Diag(clang::diag::warn_drv_unsupported_option_for_target)
122 << A->getAsString(Args) << getTriple().str();
123 }
124 }
125}
126
128 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
129 BoundArch BA, Action::OffloadKind DeviceOffloadingKind) const {
130 HostTC.addClangTargetOptions(DriverArgs, CC1Args, BA, 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(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(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(options::OPT_march_EQ);
175 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
176 BA.ArchName);
177 }
178 return DAL;
179}
180
181void SYCLToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
182 HostTC.addClangWarningOptions(CC1Args);
183}
184
186 // The SPIR-V linker dispatches to clang-sycl-linker for SYCL offloading.
187 return new tools::SPIRV::Linker(*this);
188}
189
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
206 ArgStringList &CC1Args) const {
207 HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
208}
209
211 const ArgList &Args) const {
212 return HostTC.computeMSVCVersion(D, Args);
213}
static ArrayRef< options::ID > getUnsupportedOpts()
Definition SYCL.cpp:84
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:96
const llvm::opt::OptTable & getOpts() const
Definition Driver.h:409
SYCLInstallationDetector(const Driver &D, const llvm::Triple &HostTriple, const llvm::opt::ArgList &Args)
Definition SYCL.cpp:19
void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition SYCL.cpp:70
const Driver & getDriver() const
Definition ToolChain.h:286
ToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args)
Definition ToolChain.cpp:92
path_list & getProgramPaths()
Definition ToolChain.h:329
const llvm::Triple & getTriple() const
Definition ToolChain.h:288
Tool - Information on a specific compilation tool.
Definition Tool.h:32
VersionTuple computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const override
On Windows, returns the MSVC compatibility version.
Definition SYCL.cpp:210
Tool * buildLinker() const override
Definition SYCL.cpp:185
void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific SYCL includes.
Definition SYCL.cpp:195
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition SYCL.cpp:191
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition SYCL.cpp:205
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition SYCL.cpp:200
SYCLToolChain(const Driver &D, const llvm::Triple &Triple, const ToolChain &HostTC, const llvm::opt::ArgList &Args)
Definition SYCL.cpp:110
llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, BoundArch BA, Action::OffloadKind DeviceOffloadKind) const override
TranslateArgs - Create a new derived argument list for any argument translations this ToolChain may w...
Definition SYCL.cpp:134
void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override
Add warning options that need to be passed to cc1 for this target.
Definition SYCL.cpp:181
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, BoundArch BA, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition SYCL.cpp:127
Top level wrappers for InstallAPI frontend operations.
Represents a bound architecture for offload / multiple architecture compilation.
llvm::StringRef ArchName