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