DPC++ Runtime
Runtime libraries for oneAPI DPC++
program_impl.hpp
Go to the documentation of this file.
1 //==----- program_impl.hpp --- SYCL program implementation -----------------==//
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 #pragma once
9 
10 #include <detail/context_impl.hpp>
13 #include <sycl/context.hpp>
16 #include <sycl/device.hpp>
17 #include <sycl/property_list.hpp>
18 #include <sycl/stl.hpp>
19 
20 #include <algorithm>
21 #include <cassert>
22 #include <fstream>
23 #include <memory>
24 #include <mutex>
25 
26 namespace sycl {
27 inline namespace _V1 {
28 
29 // Forward declarations
30 class kernel;
31 
32 namespace detail {
33 
34 using ContextImplPtr = std::shared_ptr<detail::context_impl>;
35 
36 enum class program_state { none = 0, compiled = 1, linked = 2 };
37 
38 class program_impl {
39 public:
40  program_impl() = delete;
41 
50  explicit program_impl(ContextImplPtr Context, const property_list &PropList);
51 
61  program_impl(ContextImplPtr Context, std::vector<device> DeviceList,
62  const property_list &PropList);
63 
79  program_impl(std::vector<std::shared_ptr<program_impl>> ProgramList,
80  std::string LinkOptions, const property_list &PropList);
81 
95  program_impl(ContextImplPtr Context, pi_native_handle InteropProgram);
96 
103 
104  ~program_impl();
105 
109  template <typename propertyT> bool has_property() const {
110  return MPropList.has_property<propertyT>();
111  }
112 
119  template <typename propertyT> propertyT get_property() const {
120  return MPropList.get_property<propertyT>();
121  }
122 
130  cl_program get() const;
131 
137  const sycl::detail::pi::PiProgram &getHandleRef() const { return MProgram; }
138 
140  bool is_host() const { return MContext->is_host(); }
141 
156  void compile_with_kernel_name(std::string KernelName,
157  std::string CompileOptions);
158 
174  void build_with_kernel_name(std::string KernelName, std::string BuildOptions);
175 
187  void link(std::string LinkOptions = "");
188 
195  bool has_kernel(std::string KernelName, bool IsCreatedFromSource) const;
196 
204  kernel get_kernel(std::string KernelName,
205  std::shared_ptr<program_impl> PtrToSelf,
206  bool IsCreatedFromSource) const;
207 
216  std::vector<std::vector<char>> get_binaries() const;
217 
220  if (is_host())
221  return context();
222  return createSyclObjFromImpl<context>(MContext);
223  }
224 
226  const PluginPtr &getPlugin() const {
227  assert(!is_host() && "Plugin is not available for Host.");
228  return MContext->getPlugin();
229  }
230 
231  ContextImplPtr getContextImplPtr() const { return MContext; }
232 
234  std::vector<device> get_devices() const { return MDevices; }
235 
246  std::string get_compile_options() const { return MCompileOptions; }
247 
262  std::string get_link_options() const { return MLinkOptions; }
263 
272  std::string get_build_options() const { return MBuildOptions; }
273 
275  program_state get_state() const { return MState; }
276 
285  void
287  sycl::detail::pi::PiProgram NativePrg = nullptr) const;
288 
290  detail::stableSerializeSpecConstRegistry(SpecConstRegistry, Dst);
291  }
292 
294  bool hasSetSpecConstants() const { return !SpecConstRegistry.empty(); }
295 
297  bool is_cacheable() const { return MProgramAndKernelCachingAllowed; }
298 
300  pi_native_handle getNative() const;
301 
302  bool isInterop() const { return MIsInterop; }
303 
304 private:
305  // Deligating Constructor used in Implementation.
306  program_impl(ContextImplPtr Context, pi_native_handle InteropProgram,
314  template <typename Param>
315  void check_device_feature_support(const std::vector<device> &Devices) {
316  for (const auto &Device : Devices) {
317  if (!Device.get_info<Param>()) {
318  throw sycl::exception(
319  sycl::errc::feature_not_supported,
320  "Online compilation is not supported by this device");
321  }
322  }
323  }
324 
331  void
332  create_pi_program_with_kernel_name(const std::string &KernelName,
333  bool JITCompilationIsRequired = false);
334 
338  void compile(const std::string &Options);
339 
343  void build(const std::string &Options);
344 
346  std::vector<sycl::detail::pi::PiDevice> get_pi_devices() const;
347 
350  static bool is_cacheable_with_options(const std::string &Options) {
351  return Options.empty();
352  }
353 
356  bool has_cl_kernel(const std::string &KernelName) const;
357 
361  std::pair<sycl::detail::pi::PiKernel, const KernelArgMask *>
362  get_pi_kernel_arg_mask_pair(const std::string &KernelName) const;
363 
365  std::vector<device> sort_devices_by_cl_device_id(std::vector<device> Devices);
366 
371  void throw_if_state_is(program_state State) const;
372 
377  void throw_if_state_is_not(program_state State) const;
378 
379  sycl::detail::pi::PiProgram MProgram = nullptr;
381  std::mutex MMutex;
382  ContextImplPtr MContext;
383  bool MLinkable = false;
384  std::vector<device> MDevices;
385  property_list MPropList;
386  std::string MCompileOptions;
387  std::string MLinkOptions;
388  std::string MBuildOptions;
389 
390  // Keeps specialization constant map for this program. Spec constant name
391  // resolution to actual SPIR-V integer ID happens at build time, where the
392  // device binary image is available. Access is guarded by this context's
393  // program cache lock.
394  SpecConstRegistryT SpecConstRegistry;
395 
399  bool MProgramAndKernelCachingAllowed = false;
400 
401  bool MIsInterop = false;
402 };
403 
404 } // namespace detail
405 } // namespace _V1
406 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51
void link(std::string LinkOptions="")
Links encapsulated raw program.
void compile_with_kernel_name(std::string KernelName, std::string CompileOptions)
Compiles the SYCL kernel function into the encapsulated raw program.
std::string get_build_options() const
Returns the compile, link, or build options, from whichever of those operations was performed most re...
const sycl::detail::pi::PiProgram & getHandleRef() const
bool hasSetSpecConstants() const
Tells whether a specialization constant has been set for this program.
void stableSerializeSpecConstRegistry(SerializedObj &Dst) const
program_state get_state() const
ContextImplPtr getContextImplPtr() const
kernel get_kernel(std::string KernelName, std::shared_ptr< program_impl > PtrToSelf, bool IsCreatedFromSource) const
Returns a SYCL kernel for the SYCL kernel function defined by kernel name.
std::vector< std::vector< char > > get_binaries() const
Returns built program binaries.
std::vector< device > get_devices() const
std::string get_link_options() const
Returns compile options that were provided to the most recent invocation of link member function.
void flush_spec_constants(const RTDeviceBinaryImage &Img, sycl::detail::pi::PiProgram NativePrg=nullptr) const
Takes current values of specialization constants and "injects" them into the underlying native progra...
bool has_property() const
Checks if this program_impl has a property of type propertyT.
const PluginPtr & getPlugin() const
std::string get_compile_options() const
Returns compile options that were provided when the encapsulated program was explicitly compiled.
pi_native_handle getNative() const
Returns the native plugin handle.
void build_with_kernel_name(std::string KernelName, std::string BuildOptions)
Builds the SYCL kernel function into encapsulated raw program.
cl_program get() const
Returns a valid cl_program instance.
bool has_kernel(std::string KernelName, bool IsCreatedFromSource) const
Checks if kernel is available for this program.
sycl::detail::pi::PiProgram & getHandleRef()
propertyT get_property() const
Gets the specified property of this program_impl.
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:74
Objects of the property_list class are containers for the SYCL properties.
bool has_property() const noexcept
std::map< std::string, spec_constant_impl > SpecConstRegistryT
void stableSerializeSpecConstRegistry(const SpecConstRegistryT &Reg, SerializedObj &Dst)
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:33
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
std::vector< unsigned char > SerializedObj
Definition: util.hpp:68
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:206