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