DPC++ Runtime
Runtime libraries for oneAPI DPC++
online_compiler.hpp
Go to the documentation of this file.
1 //===------- online_compiler.hpp - Online source compilation service ------===//
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 #pragma once
10 
11 #include <CL/sycl/detail/defines_elementary.hpp> // for __SYCL_INLINE_NAMESPACE
12 #include <CL/sycl/detail/export.hpp> // for __SYCL_EXPORT
13 #include <CL/sycl/device.hpp>
14 
15 #include <string>
16 #include <vector>
17 
19 namespace sycl {
20 namespace ext {
21 namespace intel {
22 namespace experimental {
23 
24 using byte = unsigned char;
25 
27  spir_v = 0 // the only format supported for now
28 };
29 
30 class device_arch {
31 public:
32  static constexpr int any = 0;
33 
34  device_arch(int Val) : Val(Val) {}
35 
36  enum gpu {
37  gpu_any = 1,
38  gpu_gen9 = 2,
39  gpu_skl = gpu_gen9,
40  gpu_gen9_5 = 3,
41  gpu_kbl = gpu_gen9_5,
42  gpu_cfl = gpu_gen9_5,
43  gpu_gen11 = 4,
44  gpu_icl = gpu_gen11,
45  gpu_gen12 = 5
46  };
47 
48  enum cpu {
49  cpu_any = 1,
50  };
51 
52  enum fpga {
53  fpga_any = 1,
54  };
55 
56  operator int() { return Val; }
57 
58 private:
59  int Val;
60 };
61 
64 public:
65  online_compile_error() = default;
66  online_compile_error(const std::string &Msg) : sycl::exception(Msg) {}
67 };
68 
70 enum class source_language { opencl_c = 0, cm = 1 };
71 
74 template <source_language Lang> class online_compiler {
75 public:
80  online_compiler(compiled_code_format fmt = compiled_code_format::spir_v)
81  : OutputFormat(fmt), OutputFormatVersion({0, 0}),
82  DeviceType(sycl::info::device_type::all), DeviceArch(device_arch::any),
83  Is64Bit(true), DeviceStepping("") {}
84 
91  compiled_code_format fmt = compiled_code_format::spir_v)
92  : OutputFormat(fmt), OutputFormatVersion({0, 0}), DeviceType(dev_type),
93  DeviceArch(arch), Is64Bit(true), DeviceStepping("") {}
94 
96  // TODO: the initial version generates the generic code (SKL now), need
97  // to do additional device::info calls to determine the device by it's
98  // features.
100  : OutputFormat(compiled_code_format::spir_v), OutputFormatVersion({0, 0}),
101  DeviceType(sycl::info::device_type::all), DeviceArch(device_arch::any),
102  Is64Bit(true), DeviceStepping("") {}
103 
110  template <typename... Tys>
111  std::vector<byte> compile(const std::string &src, const Tys &... args);
112 
115  OutputFormat = fmt;
116  return *this;
117  }
118 
122  OutputFormatVersion = {major, minor};
123  return *this;
124  }
125 
128  DeviceType = type;
129  return *this;
130  }
131 
134  DeviceArch = arch;
135  return *this;
136  }
137 
140  Is64Bit = false;
141  return *this;
142  };
143 
146  Is64Bit = true;
147  return *this;
148  };
149 
153  DeviceStepping = id;
154  return *this;
155  }
156 
157 private:
159  compiled_code_format OutputFormat;
160 
162  std::pair<int, int> OutputFormatVersion;
163 
165  sycl::info::device_type DeviceType;
166 
168  device_arch DeviceArch;
169 
171  bool Is64Bit;
172 
174  std::string DeviceStepping;
175 
177  void *CompileToSPIRVHandle = nullptr;
178  void *FreeSPIRVOutputsHandle = nullptr;
179 };
180 
181 // Specializations of the online_compiler class and 'compile' function for
182 // particular languages and parameter types.
183 
188 template <>
189 template <>
190 __SYCL_EXPORT std::vector<byte>
192  const std::string &src, const std::vector<std::string> &options);
193 
196 template <>
197 template <>
198 std::vector<byte>
200  return compile(src, std::vector<std::string>{});
201 }
202 
206 template <>
207 template <>
208 __SYCL_EXPORT std::vector<byte> online_compiler<source_language::cm>::compile(
209  const std::string &src, const std::vector<std::string> &options);
210 
212 template <>
213 template <>
214 std::vector<byte>
216  return compile(src, std::vector<std::string>{});
217 }
218 
219 } // namespace experimental
220 } // namespace intel
221 } // namespace ext
222 
223 namespace ext {
225  "use 'ext::intel::experimental' instead") intel {
226  using namespace ext::intel::experimental;
227 } // namespace intel
228 } // namespace ext
229 
230 } // namespace sycl
231 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::ext::intel::experimental::online_compiler::setOutputFormatVersion
online_compiler< Lang > & setOutputFormatVersion(int major, int minor)
Sets the compiled code format version of the compilation target and returns *this.
Definition: online_compiler.hpp:121
cl::sycl::ext::intel::experimental::online_compiler::online_compiler
online_compiler(const sycl::device &)
Constructs online compiler for the target specified by given SYCL device.
Definition: online_compiler.hpp:99
__SYCL2020_DEPRECATED
#define __SYCL2020_DEPRECATED(message)
Definition: defines_elementary.hpp:56
defines_elementary.hpp
device.hpp
cl::sycl::id
A unique identifier of an item in an index space.
Definition: array.hpp:17
cl::sycl::ext::intel::experimental::online_compile_error::online_compile_error
online_compile_error(const std::string &Msg)
Definition: online_compiler.hpp:66
cl::sycl::ext::intel::experimental::device_arch
Definition: online_compiler.hpp:30
cl::sycl::ext::intel::experimental::device_arch::cpu
cpu
Definition: online_compiler.hpp:48
cl::sycl::info::device_type
device_type
Definition: info_desc.hpp:180
sycl
Definition: invoke_simd.hpp:68
cl::sycl::any
detail::enable_if_t< detail::is_sigeninteger< T >::value, int > any(T x) __NOEXC
Definition: builtins.hpp:1283
export.hpp
cl::sycl::ext::intel::experimental::source_language::cm
@ cm
char
cl::sycl::ext::intel::experimental::compiled_code_format::spir_v
@ spir_v
cl::sycl::ext::intel::experimental::online_compiler::setTargetDeviceArch
online_compiler< Lang > & setTargetDeviceArch(device_arch arch)
Sets the device architecture of the compilation target and returns *this.
Definition: online_compiler.hpp:133
cl::sycl::ext::intel::experimental::online_compiler::online_compiler
online_compiler(sycl::info::device_type dev_type, device_arch arch, compiled_code_format fmt=compiled_code_format::spir_v)
Constructs online compiler which targets given architecture and produces given compiled code format.
Definition: online_compiler.hpp:90
cl::sycl::device
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:36
cl::sycl::ext::intel::experimental::online_compiler::setOutputFormat
online_compiler< Lang > & setOutputFormat(compiled_code_format fmt)
Sets the compiled code format of the compilation target and returns *this.
Definition: online_compiler.hpp:114
cl::sycl::compile
kernel_bundle< bundle_state::object > compile(const kernel_bundle< bundle_state::input > &InputBundle, const std::vector< device > &Devs, const property_list &PropList={})
Definition: kernel_bundle.hpp:632
cl::sycl::ext::intel::experimental::online_compiler::set32bitTarget
online_compiler< Lang > & set32bitTarget()
Makes the compilation target 32-bit and returns *this.
Definition: online_compiler.hpp:139
cl::sycl::ext::intel::experimental::online_compiler::set64bitTarget
online_compiler< Lang > & set64bitTarget()
Makes the compilation target 64-bit and returns *this.
Definition: online_compiler.hpp:145
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::ext::intel::experimental::online_compiler
Represents an online compiler for the language given as template parameter.
Definition: online_compiler.hpp:74
cl::sycl::ext::intel::experimental::online_compiler::setTargetDeviceStepping
online_compiler< Lang > & setTargetDeviceStepping(const std::string &id)
Sets implementation-defined target device stepping of the compilation target and returns *this.
Definition: online_compiler.hpp:152
cl::sycl::ext::intel::experimental::online_compile_error
Represents an error happend during online compilation.
Definition: online_compiler.hpp:63
cl::sycl::ext::intel::experimental::source_language::opencl_c
@ opencl_c
cl::sycl::ext::intel::experimental::online_compiler::setTargetDeviceType
online_compiler< Lang > & setTargetDeviceType(sycl::info::device_type type)
Sets the device type of the compilation target and returns *this.
Definition: online_compiler.hpp:127
cl::sycl::ext::intel::experimental::device_arch::fpga
fpga
Definition: online_compiler.hpp:52
cl::sycl::ext::intel::experimental::source_language
source_language
Designates a source language for the online compiler.
Definition: online_compiler.hpp:70
cl::sycl::ext::intel::experimental::online_compiler::online_compiler
online_compiler(compiled_code_format fmt=compiled_code_format::spir_v)
Constructs online compiler which can target any device and produces given compiled code format.
Definition: online_compiler.hpp:80
cl::sycl::ext::intel::experimental::compiled_code_format
compiled_code_format
Definition: online_compiler.hpp:26
cl::sycl::all
detail::enable_if_t< detail::is_sigeninteger< T >::value, int > all(T x) __NOEXC
Definition: builtins.hpp:1297
cl::sycl::exception
Definition: exception.hpp:64
cl::sycl::instead
std::uint8_t instead
Definition: aliases.hpp:69
cl::sycl::ext::intel::experimental::device_arch::device_arch
device_arch(int Val)
Definition: online_compiler.hpp:34
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12
cl::sycl::ext::intel::experimental::device_arch::gpu
gpu
Definition: online_compiler.hpp:36