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 
19 #include <algorithm>
20 #include <cassert>
21 #include <fstream>
22 #include <memory>
23 #include <mutex>
24 
25 namespace sycl {
26 inline namespace _V1 {
27 
28 // Forward declarations
29 class kernel;
30 
31 namespace detail {
32 
33 using ContextImplPtr = std::shared_ptr<detail::context_impl>;
34 
35 enum class program_state { none = 0, compiled = 1, linked = 2 };
36 
37 class program_impl {
38 public:
39  program_impl() = delete;
40 
49  explicit program_impl(ContextImplPtr Context, const property_list &PropList);
50 
60  program_impl(ContextImplPtr Context, std::vector<device> DeviceList,
61  const property_list &PropList);
62 
78  program_impl(std::vector<std::shared_ptr<program_impl>> ProgramList,
79  std::string LinkOptions, const property_list &PropList);
80 
94  program_impl(ContextImplPtr Context, pi_native_handle InteropProgram);
95 
102 
103  ~program_impl();
104 
108  template <typename propertyT> bool has_property() const {
109  return MPropList.has_property<propertyT>();
110  }
111 
118  template <typename propertyT> propertyT get_property() const {
119  return MPropList.get_property<propertyT>();
120  }
121 
129  cl_program get() const;
130 
136  const sycl::detail::pi::PiProgram &getHandleRef() const { return MProgram; }
137 
139  bool is_host() const { return MContext->is_host(); }
140 
155  void compile_with_kernel_name(std::string KernelName,
156  std::string CompileOptions);
157 
174 
186  void link(std::string LinkOptions = "");
187 
194  bool has_kernel(std::string KernelName, bool IsCreatedFromSource) const;
195 
203  kernel get_kernel(std::string KernelName,
204  std::shared_ptr<program_impl> PtrToSelf,
205  bool IsCreatedFromSource) const;
206 
215  std::vector<std::vector<char>> get_binaries() const;
216 
219  if (is_host())
220  return context();
221  return createSyclObjFromImpl<context>(MContext);
222  }
223 
225  const PluginPtr &getPlugin() const {
226  assert(!is_host() && "Plugin is not available for Host.");
227  return MContext->getPlugin();
228  }
229 
230  ContextImplPtr getContextImplPtr() const { return MContext; }
231 
233  std::vector<device> get_devices() const { return MDevices; }
234 
245  std::string get_compile_options() const { return MCompileOptions; }
246 
261  std::string get_link_options() const { return MLinkOptions; }
262 
271  std::string get_build_options() const { return MBuildOptions; }
272 
274  program_state get_state() const { return MState; }
275 
284  void
286  sycl::detail::pi::PiProgram NativePrg = nullptr) const;
287 
289  detail::stableSerializeSpecConstRegistry(SpecConstRegistry, Dst);
290  }
291 
293  bool hasSetSpecConstants() const { return !SpecConstRegistry.empty(); }
294 
296  bool is_cacheable() const { return MProgramAndKernelCachingAllowed; }
297 
299  pi_native_handle getNative() const;
300 
301  bool isInterop() const { return MIsInterop; }
302 
303 private:
304  // Deligating Constructor used in Implementation.
305  program_impl(ContextImplPtr Context, pi_native_handle InteropProgram,
313  template <typename Param>
314  void check_device_feature_support(const std::vector<device> &Devices) {
315  for (const auto &Device : Devices) {
316  if (!Device.get_info<Param>()) {
317  throw sycl::exception(
318  sycl::errc::feature_not_supported,
319  "Online compilation is not supported by this device");
320  }
321  }
322  }
323 
330  void
331  create_pi_program_with_kernel_name(const std::string &KernelName,
332  bool JITCompilationIsRequired = false);
333 
337  void compile(const std::string &Options);
338 
342  void build(const std::string &Options);
343 
345  std::vector<sycl::detail::pi::PiDevice> get_pi_devices() const;
346 
349  static bool is_cacheable_with_options(const std::string &Options) {
350  return Options.empty();
351  }
352 
355  bool has_cl_kernel(const std::string &KernelName) const;
356 
360  std::pair<sycl::detail::pi::PiKernel, const KernelArgMask *>
361  get_pi_kernel_arg_mask_pair(const std::string &KernelName) const;
362 
364  std::vector<device> sort_devices_by_cl_device_id(std::vector<device> Devices);
365 
370  void throw_if_state_is(program_state State) const;
371 
376  void throw_if_state_is_not(program_state State) const;
377 
378  sycl::detail::pi::PiProgram MProgram = nullptr;
380  std::mutex MMutex;
381  ContextImplPtr MContext;
382  bool MLinkable = false;
383  std::vector<device> MDevices;
384  property_list MPropList;
385  std::string MCompileOptions;
386  std::string MLinkOptions;
387  std::string MBuildOptions;
388 
389  // Keeps specialization constant map for this program. Spec constant name
390  // resolution to actual SPIR-V integer ID happens at build time, where the
391  // device binary image is available. Access is guarded by this context's
392  // program cache lock.
393  SpecConstRegistryT SpecConstRegistry;
394 
398  bool MProgramAndKernelCachingAllowed = false;
399 
400  bool MIsInterop = false;
401 };
402 
403 } // namespace detail
404 } // namespace _V1
405 } // 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:79
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:32
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
std::string string
Definition: handler.hpp:426
std::vector< unsigned char > SerializedObj
Definition: util.hpp:70
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:209