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 {
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 
102  program_impl(ContextImplPtr Context, RT::PiKernel Kernel);
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 
134  RT::PiProgram &getHandleRef() { return MProgram; }
137  const RT::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  OSModuleHandle Module);
159 
173  void compile_with_source(std::string KernelSource,
174  std::string CompileOptions = "");
175 
191  void build_with_kernel_name(std::string KernelName, std::string BuildOptions,
192  OSModuleHandle M);
193 
207  void build_with_source(std::string KernelSource,
208  std::string BuildOptions = "");
209 
221  void link(std::string LinkOptions = "");
222 
229  bool has_kernel(std::string KernelName, bool IsCreatedFromSource) const;
230 
238  kernel get_kernel(std::string KernelName,
239  std::shared_ptr<program_impl> PtrToSelf,
240  bool IsCreatedFromSource) const;
241 
250  std::vector<std::vector<char>> get_binaries() const;
251 
254  if (is_host())
255  return context();
256  return createSyclObjFromImpl<context>(MContext);
257  }
258 
260  const plugin &getPlugin() const {
261  assert(!is_host() && "Plugin is not available for Host.");
262  return MContext->getPlugin();
263  }
264 
266  std::vector<device> get_devices() const { return MDevices; }
267 
278  std::string get_compile_options() const { return MCompileOptions; }
279 
294  std::string get_link_options() const { return MLinkOptions; }
295 
304  std::string get_build_options() const { return MBuildOptions; }
305 
307  program_state get_state() const { return MState; }
308 
309  void set_spec_constant_impl(const char *Name, const void *ValAddr,
310  size_t ValSize);
311 
320  void flush_spec_constants(const RTDeviceBinaryImage &Img,
321  RT::PiProgram NativePrg = nullptr) const;
322 
328  OSModuleHandle getOSModuleHandle() const { return MProgramModuleHandle; }
329 
331  detail::stableSerializeSpecConstRegistry(SpecConstRegistry, Dst);
332  }
333 
335  bool hasSetSpecConstants() const { return !SpecConstRegistry.empty(); }
336 
338  bool is_cacheable() const { return MProgramAndKernelCachingAllowed; }
339 
341  pi_native_handle getNative() const;
342 
343  bool isInterop() const { return MIsInterop; }
344 
345 private:
346  // Deligating Constructor used in Implementation.
347  program_impl(ContextImplPtr Context, pi_native_handle InteropProgram,
348  RT::PiProgram Program);
355  template <typename Param>
356  void check_device_feature_support(const std::vector<device> &Devices) {
357  for (const auto &Device : Devices) {
358  if (!Device.get_info<Param>()) {
359  throw feature_not_supported(
360  "Online compilation is not supported by this device",
361  PI_ERROR_COMPILER_NOT_AVAILABLE);
362  }
363  }
364  }
365 
372  void
373  create_pi_program_with_kernel_name(OSModuleHandle Module,
374  const std::string &KernelName,
375  bool JITCompilationIsRequired = false);
376 
380  void create_cl_program_with_source(const std::string &Source);
381 
385  void compile(const std::string &Options);
386 
390  void build(const std::string &Options);
391 
393  std::vector<RT::PiDevice> get_pi_devices() const;
394 
397  static bool is_cacheable_with_options(const std::string &Options) {
398  return Options.empty();
399  }
400 
403  bool has_cl_kernel(const std::string &KernelName) const;
404 
408  RT::PiKernel get_pi_kernel(const std::string &KernelName) const;
409 
411  std::vector<device> sort_devices_by_cl_device_id(std::vector<device> Devices);
412 
417  void throw_if_state_is(program_state State) const;
418 
423  void throw_if_state_is_not(program_state State) const;
424 
425  RT::PiProgram MProgram = nullptr;
426  program_state MState = program_state::none;
427  std::mutex MMutex;
428  ContextImplPtr MContext;
429  bool MLinkable = false;
430  std::vector<device> MDevices;
431  property_list MPropList;
432  std::string MCompileOptions;
433  std::string MLinkOptions;
434  std::string MBuildOptions;
435  OSModuleHandle MProgramModuleHandle = OSUtil::ExeModuleHandle;
436 
437  // Keeps specialization constant map for this program. Spec constant name
438  // resolution to actual SPIR-V integer ID happens at build time, where the
439  // device binary image is available. Access is guarded by this context's
440  // program cache lock.
441  SpecConstRegistryT SpecConstRegistry;
442 
446  bool MProgramAndKernelCachingAllowed = false;
447 
448  bool MIsInterop = false;
449 };
450 
451 } // namespace detail
452 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
453 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:41
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
std::string get_build_options() const
Returns the compile, link, or build options, from whichever of those operations was performed most re...
bool hasSetSpecConstants() const
Tells whether a specialization constant has been set for this program.
OSModuleHandle getOSModuleHandle() const
Returns the OS module handle this program belongs to.
void stableSerializeSpecConstRegistry(SerializedObj &Dst) const
program_state get_state() const
const RT::PiProgram & getHandleRef() const
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.
bool has_property() const
Checks if this program_impl has a property of type propertyT.
const plugin & getPlugin() const
std::string get_compile_options() const
Returns compile options that were provided when the encapsulated program was explicitly compiled.
propertyT get_property() const
Gets the specified property of this program_impl.
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:71
Objects of the property_list class are containers for the SYCL properties.
#define __SYCL_INLINE_VER_NAMESPACE(X)
constexpr tuple_element< I, tuple< Types... > >::type & get(sycl::detail::tuple< Types... > &Arg) noexcept
Definition: tuple.hpp:199
::pi_kernel PiKernel
Definition: pi.hpp:116
::pi_program PiProgram
Definition: pi.hpp:115
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:30
std::vector< unsigned char > SerializedObj
Definition: util.hpp:68
intptr_t OSModuleHandle
Uniquely identifies an operating system module (executable or a dynamic library)
Definition: os_util.hpp:48
kernel_bundle< bundle_state::object > compile(const kernel_bundle< bundle_state::input > &InputBundle, const std::vector< device > &Devs, const property_list &PropList={})
kernel_bundle< bundle_state::executable > link(const std::vector< kernel_bundle< bundle_state::object >> &ObjectBundles, const std::vector< device > &Devs, const property_list &PropList={})
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
uintptr_t pi_native_handle
Definition: pi.h:111
@ Device