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