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 
12 #include <sycl/detail/export.hpp> // for __SYCL_EXPORT
13 #include <sycl/device.hpp>
14 
15 #include <string>
16 #include <vector>
17 
18 namespace sycl {
19 inline namespace _V1 {
20 namespace ext::intel::experimental {
21 
22 using byte = unsigned char;
23 
25  spir_v = 0 // the only format supported for now
26 };
27 
28 class device_arch {
29 public:
30  static constexpr int any = 0;
31 
32  device_arch(int Val) : Val(Val) {}
33 
34  // TODO1: the list must be extended with a bunch of new GPUs available.
35  // TODO2: the list of supported GPUs grows rapidly.
36  // The API must allow user to define the target GPU option even if it is
37  // not listed in this enumerator below.
38  enum gpu {
39  gpu_any = 1,
40  gpu_gen9 = 2,
45  gpu_gen11 = 4,
47  gpu_gen12 = 5,
50  };
51 
52  enum cpu {
53  cpu_any = 1,
54  };
55 
56  enum fpga {
57  fpga_any = 1,
58  };
59 
60  operator int() { return Val; }
61 
62 private:
63  int Val;
64 };
65 
68 public:
69  online_compile_error() = default;
70  online_compile_error(const std::string &Msg) : sycl::exception(Msg) {}
71 };
72 
74 enum class source_language { opencl_c = 0, cm = 1 };
75 
78 template <source_language Lang>
80  "experimental online_compiler is being deprecated. See "
81  "'sycl_ext_oneapi_kernel_compiler.asciidoc' instead for new kernel "
82  "compiler extension to kernel_bundle implementation.") online_compiler {
83 public:
89  : OutputFormat(fmt), OutputFormatVersion({0, 0}),
90  DeviceType(sycl::info::device_type::all), DeviceArch(device_arch::any),
91  Is64Bit(true), DeviceStepping("") {}
92 
98  online_compiler(sycl::info::device_type dev_type, device_arch arch,
100  : OutputFormat(fmt), OutputFormatVersion({0, 0}), DeviceType(dev_type),
101  DeviceArch(arch), Is64Bit(true), DeviceStepping("") {}
102 
104  // TODO: the initial version generates the generic code (SKL now), need
105  // to do additional device::info calls to determine the device by it's
106  // features.
107  online_compiler(const sycl::device &)
108  : OutputFormat(compiled_code_format::spir_v), OutputFormatVersion({0, 0}),
109  DeviceType(sycl::info::device_type::all), DeviceArch(device_arch::any),
110  Is64Bit(true), DeviceStepping("") {}
111 
118  template <typename... Tys>
119  std::vector<byte> compile(const std::string &src, const Tys &...args);
120 
122  online_compiler<Lang> &setOutputFormat(compiled_code_format fmt) {
123  OutputFormat = fmt;
124  return *this;
125  }
126 
129  online_compiler<Lang> &setOutputFormatVersion(int major, int minor) {
130  OutputFormatVersion = {major, minor};
131  return *this;
132  }
133 
135  online_compiler<Lang> &setTargetDeviceType(sycl::info::device_type type) {
136  DeviceType = type;
137  return *this;
138  }
139 
141  online_compiler<Lang> &setTargetDeviceArch(device_arch arch) {
142  DeviceArch = arch;
143  return *this;
144  }
145 
147  online_compiler<Lang> &set32bitTarget() {
148  Is64Bit = false;
149  return *this;
150  };
151 
153  online_compiler<Lang> &set64bitTarget() {
154  Is64Bit = true;
155  return *this;
156  };
157 
160  online_compiler<Lang> &setTargetDeviceStepping(const std::string &id) {
161  DeviceStepping = id;
162  return *this;
163  }
164 
165 private:
167  compiled_code_format OutputFormat;
168 
170  std::pair<int, int> OutputFormatVersion;
171 
173  sycl::info::device_type DeviceType;
174 
176  device_arch DeviceArch;
177 
179  bool Is64Bit;
180 
182  std::string DeviceStepping;
183 
185  void *CompileToSPIRVHandle = nullptr;
186  void *FreeSPIRVOutputsHandle = nullptr;
187 };
188 
189 // Specializations of the online_compiler class and 'compile' function for
190 // particular languages and parameter types.
191 
196 template <>
197 template <>
198 __SYCL_EXPORT std::vector<byte>
200  const std::string &src, const std::vector<std::string> &options);
201 
204 template <>
205 template <>
206 std::vector<byte>
208  return compile(src, std::vector<std::string>{});
209 }
210 
214 template <>
215 template <>
216 __SYCL_EXPORT std::vector<byte> online_compiler<source_language::cm>::compile(
217  const std::string &src, const std::vector<std::string> &options);
218 
220 template <>
221 template <>
222 std::vector<byte>
223 online_compiler<source_language::cm>::compile(const std::string &src) {
224  return compile(src, std::vector<std::string>{});
225 }
226 
227 } // namespace ext::intel::experimental
228 } // namespace _V1
229 } // namespace sycl
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
Represents an error happend during online compilation.
The kernel_bundle class represents collection of device images in a particular state.
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:77
source_language
Designates a source language for the online compiler.
kernel_bundle< bundle_state::object > compile(const kernel_bundle< bundle_state::input > &InputBundle, const std::vector< device > &Devs, const property_list &PropList={})
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:94
std::uint8_t instead
Definition: aliases.hpp:93
Definition: access.hpp:18