clang  19.0.0git
SYCL.h
Go to the documentation of this file.
1 //===--- SYCL.h - SYCL 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 
9 #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_SYCL_H
10 #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_SYCL_H
11 
12 #include "clang/Driver/Options.h"
13 #include "clang/Driver/Tool.h"
14 #include "clang/Driver/ToolChain.h"
15 
16 namespace clang {
17 namespace driver {
18 
20 public:
23  llvm::SmallVector<llvm::SmallString<128>, 4> &DeviceLibPaths) const;
24  void print(llvm::raw_ostream &OS) const;
25 
26 private:
27  const Driver &D;
28  llvm::SmallVector<llvm::SmallString<128>, 4> InstallationCandidates;
29 };
30 
31 class Command;
32 
33 namespace tools {
34 namespace SYCL {
35 
37  std::unique_ptr<Command> InputCommand,
38  const InputInfoList &InputFiles,
39  const InputInfo &Output, const Tool *T,
40  StringRef Increment, StringRef Ext = "out",
41  StringRef ParallelJobs = "");
42 
43 // Provides a vector of device library names that are associated with the
44 // given triple and AOT information.
46  const llvm::Triple &TargetTriple,
47  bool IsSpirvAOT);
48 
50 // Runs llvm-spirv to convert spirv to bc, llvm-link, which links multiple LLVM
51 // bitcode. Converts generated bc back to spirv using llvm-spirv, wraps with
52 // offloading information. Finally compiles to object using llc
53 class LLVM_LIBRARY_VISIBILITY Linker : public Tool {
54 public:
55  Linker(const ToolChain &TC) : Tool("SYCL::Linker", "sycl-link", TC) {}
56 
57  bool hasIntegratedCPP() const override { return false; }
58 
59  void ConstructJob(Compilation &C, const JobAction &JA,
60  const InputInfo &Output, const InputInfoList &Inputs,
61  const llvm::opt::ArgList &TCArgs,
62  const char *LinkingOutput) const override;
63 
64 private:
65  /// \return llvm-link output file name.
66  const char *constructLLVMLinkCommand(Compilation &C, const JobAction &JA,
67  const InputInfo &Output,
68  const llvm::opt::ArgList &Args,
69  llvm::StringRef SubArchName,
70  llvm::StringRef OutputFilePrefix,
71  const InputInfoList &InputFiles) const;
72  void constructLlcCommand(Compilation &C, const JobAction &JA,
73  const InputInfo &Output,
74  const char *InputFile) const;
75 };
76 
77 /// Directly call FPGA Compiler and Linker
78 namespace fpga {
79 
80 class LLVM_LIBRARY_VISIBILITY BackendCompiler : public Tool {
81 public:
83  : Tool("fpga::BackendCompiler", "fpga compiler", TC) {}
84 
85  bool hasIntegratedCPP() const override { return false; }
86 
87  void ConstructJob(Compilation &C, const JobAction &JA,
88  const InputInfo &Output, const InputInfoList &Inputs,
89  const llvm::opt::ArgList &TCArgs,
90  const char *LinkingOutput) const override;
91 
92 private:
93  void constructOpenCLAOTCommand(Compilation &C, const JobAction &JA,
94  const InputInfo &Output,
95  const InputInfoList &InputFiles,
96  const llvm::opt::ArgList &Args) const;
97 };
98 
99 } // end namespace fpga
100 
101 namespace gen {
102 
103 class LLVM_LIBRARY_VISIBILITY BackendCompiler : public Tool {
104 public:
106  : Tool("gen::BackendCompiler", "gen compiler", TC) {}
107 
108  bool hasIntegratedCPP() const override { return false; }
109 
110  void ConstructJob(Compilation &C, const JobAction &JA,
111  const InputInfo &Output, const InputInfoList &Inputs,
112  const llvm::opt::ArgList &TCArgs,
113  const char *LinkingOutput) const override;
114 };
115 
116 StringRef resolveGenDevice(StringRef DeviceName);
117 SmallString<64> getGenDeviceMacro(StringRef DeviceName);
118 StringRef getGenGRFFlag(StringRef GRFMode);
119 
120 // // Prefix for GPU specific targets used for -fsycl-targets
121 constexpr char IntelGPU[] = "intel_gpu_";
122 constexpr char NvidiaGPU[] = "nvidia_gpu_";
123 constexpr char AmdGPU[] = "amd_gpu_";
124 
125 template <auto GPUArh> std::optional<StringRef> isGPUTarget(StringRef Target) {
126  // Handle target specifications that resemble '(intel, nvidia, amd)_gpu_*'
127  // here.
128  if (Target.starts_with(GPUArh)) {
129  return resolveGenDevice(Target);
130  }
131  return std::nullopt;
132 }
133 
134 } // end namespace gen
135 
136 namespace x86_64 {
137 
138 class LLVM_LIBRARY_VISIBILITY BackendCompiler : public Tool {
139 public:
141  : Tool("x86_64::BackendCompiler", "x86_64 compiler", TC) {}
142 
143  bool hasIntegratedCPP() const override { return false; }
144 
145  void ConstructJob(Compilation &C, const JobAction &JA,
146  const InputInfo &Output, const InputInfoList &Inputs,
147  const llvm::opt::ArgList &TCArgs,
148  const char *LinkingOutput) const override;
149 };
150 
151 } // end namespace x86_64
152 
153 } // end namespace SYCL
154 } // end namespace tools
155 
156 namespace toolchains {
157 
158 class LLVM_LIBRARY_VISIBILITY SYCLToolChain : public ToolChain {
159 public:
160  SYCLToolChain(const Driver &D, const llvm::Triple &Triple,
161  const ToolChain &HostTC, const llvm::opt::ArgList &Args);
162 
163  const llvm::Triple *getAuxTriple() const override {
164  return &HostTC.getTriple();
165  }
166 
167  llvm::opt::DerivedArgList *
168  TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
169  Action::OffloadKind DeviceOffloadKind) const override;
170  void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
171  llvm::opt::ArgStringList &CC1Args,
172  Action::OffloadKind DeviceOffloadKind) const override;
173  void AddImpliedTargetArgs(const llvm::Triple &Triple,
174  const llvm::opt::ArgList &Args,
175  llvm::opt::ArgStringList &CmdArgs,
176  const JobAction &JA, const ToolChain &HostTC,
177  StringRef Device = "") const;
178  void TranslateBackendTargetArgs(const llvm::Triple &Triple,
179  const llvm::opt::ArgList &Args,
180  llvm::opt::ArgStringList &CmdArgs,
181  StringRef Device = "") const;
182  void TranslateLinkerTargetArgs(const llvm::Triple &Triple,
183  const llvm::opt::ArgList &Args,
184  llvm::opt::ArgStringList &CmdArgs,
185  StringRef Device = "") const;
186  void TranslateTargetOpt(const llvm::Triple &Triple,
187  const llvm::opt::ArgList &Args,
188  llvm::opt::ArgStringList &CmdArgs,
189  llvm::opt::OptSpecifier Opt,
190  llvm::opt::OptSpecifier Opt_EQ,
191  StringRef Device) const;
192 
193  bool useIntegratedAs() const override { return true; }
194  bool isPICDefault() const override {
195  if (this->IsSYCLNativeCPU)
196  return this->HostTC.isPICDefault();
197  return false;
198  }
199  llvm::codegenoptions::DebugInfoFormat getDefaultDebugFormat() const override {
200  if (this->IsSYCLNativeCPU ||
201  this->HostTC.getTriple().isWindowsMSVCEnvironment())
202  return this->HostTC.getDefaultDebugFormat();
204  }
205  bool isPIEDefault(const llvm::opt::ArgList &Args) const override {
206  return false;
207  }
208  bool isPICDefaultForced() const override { return false; }
209 
210  void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override;
211  CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override;
212  static void AddSYCLIncludeArgs(const clang::driver::Driver &Driver,
213  const llvm::opt::ArgList &DriverArgs,
214  llvm::opt::ArgStringList &CC1Args);
215  void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
216  llvm::opt::ArgStringList &CC1Args) const override;
217  void AddClangCXXStdlibIncludeArgs(
218  const llvm::opt::ArgList &Args,
219  llvm::opt::ArgStringList &CC1Args) const override;
220 
221  SanitizerMask getSupportedSanitizers() const override;
222 
224  const bool IsSYCLNativeCPU;
225 
226 protected:
227  Tool *buildBackendCompiler() const override;
228  Tool *buildLinker() const override;
229 
230 private:
231  void TranslateGPUTargetOpt(const llvm::opt::ArgList &Args,
232  llvm::opt::ArgStringList &CmdArgs,
233  llvm::opt::OptSpecifier Opt_EQ) const;
234 };
235 
236 } // end namespace toolchains
237 
238 inline bool isSYCLNativeCPU(const llvm::opt::ArgList &Args) {
239  if (auto SYCLTargets = Args.getLastArg(options::OPT_fsycl_targets_EQ)) {
240  if (SYCLTargets->containsValue("native_cpu"))
241  return true;
242  }
243  return false;
244 }
245 
246 inline bool isSYCLNativeCPU(const llvm::Triple &HostT, const llvm::Triple &DevT) {
247  return HostT == DevT;
248 }
249 
250 inline bool isSYCLNativeCPU(const ToolChain &TC) {
251  const llvm::Triple *const AuxTriple = TC.getAuxTriple();
252  return AuxTriple && isSYCLNativeCPU(TC.getTriple(), *AuxTriple);
253 }
254 } // end namespace driver
255 } // end namespace clang
256 
257 #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_SYCL_H
llvm::MachO::Target Target
Definition: MachO.h:50
Command - An executable path/name and argument vector to execute.
Definition: Job.h:107
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
void print(llvm::raw_ostream &OS) const
Definition: SYCL.cpp:45
void getSYCLDeviceLibPath(llvm::SmallVector< llvm::SmallString< 128 >, 4 > &DeviceLibPaths) const
Definition: SYCL.cpp:34
SYCLInstallationDetector(const Driver &D)
Definition: SYCL.cpp:29
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:92
virtual llvm::codegenoptions::DebugInfoFormat getDefaultDebugFormat() const
Get the default debug info format. Typically, this is DWARF.
Definition: ToolChain.h:589
const llvm::Triple & getTriple() const
Definition: ToolChain.h:271
virtual const llvm::Triple * getAuxTriple() const
Get the toolchain's aux triple, if it has one.
Definition: ToolChain.h:278
virtual bool isPICDefault() const =0
Test whether this toolchain defaults to PIC.
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
const llvm::Triple * getAuxTriple() const override
Get the toolchain's aux triple, if it has one.
Definition: SYCL.h:163
llvm::codegenoptions::DebugInfoFormat getDefaultDebugFormat() const override
Get the default debug info format. Typically, this is DWARF.
Definition: SYCL.h:199
bool isPICDefaultForced() const override
Tests whether this toolchain forces its default for PIC, PIE or non-PIC.
Definition: SYCL.h:208
bool useIntegratedAs() const override
Check if the toolchain should use the integrated assembler.
Definition: SYCL.h:193
bool isPIEDefault(const llvm::opt::ArgList &Args) const override
Test whether this toolchain defaults to PIE.
Definition: SYCL.h:205
bool isPICDefault() const override
Test whether this toolchain defaults to PIC.
Definition: SYCL.h:194
Linker(const ToolChain &TC)
Definition: SYCL.h:55
bool hasIntegratedCPP() const override
Definition: SYCL.h:57
std::optional< StringRef > isGPUTarget(StringRef Target)
Definition: SYCL.h:125
constexpr char NvidiaGPU[]
Definition: SYCL.h:122
StringRef getGenGRFFlag(StringRef GRFMode)
Definition: SYCL.cpp:949
constexpr char AmdGPU[]
Definition: SYCL.h:123
SmallString< 64 > getGenDeviceMacro(StringRef DeviceName)
Definition: SYCL.cpp:1095
StringRef resolveGenDevice(StringRef DeviceName)
Definition: SYCL.cpp:1000
constexpr char IntelGPU[]
Definition: SYCL.h:121
bool shouldDoPerObjectFileLinking(const Compilation &C)
Definition: SYCL.cpp:154
SmallVector< std::string, 8 > getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple, bool IsSpirvAOT)
Definition: SYCL.cpp:209
void constructLLVMForeachCommand(Compilation &C, const JobAction &JA, std::unique_ptr< Command > InputCommand, const InputInfoList &InputFiles, const InputInfo &Output, const Tool *T, StringRef Increment, StringRef Ext="out", StringRef ParallelJobs="")
Definition: SYCL.cpp:65
bool isSYCLNativeCPU(const llvm::opt::ArgList &Args)
Definition: SYCL.h:238
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T