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 {
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,
41  gpu_skl = gpu_gen9,
42  gpu_gen9_5 = 3,
43  gpu_kbl = gpu_gen9_5,
44  gpu_cfl = gpu_gen9_5,
45  gpu_gen11 = 4,
46  gpu_icl = gpu_gen11,
47  gpu_gen12 = 5,
48  gpu_tgl = gpu_gen12,
49  gpu_tgllp = gpu_gen12
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 
67 class online_compile_error : public sycl::exception {
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> class online_compiler {
79 public:
84  online_compiler(compiled_code_format fmt = compiled_code_format::spir_v)
85  : OutputFormat(fmt), OutputFormatVersion({0, 0}),
86  DeviceType(sycl::info::device_type::all), DeviceArch(device_arch::any),
87  Is64Bit(true), DeviceStepping("") {}
88 
95  compiled_code_format fmt = compiled_code_format::spir_v)
96  : OutputFormat(fmt), OutputFormatVersion({0, 0}), DeviceType(dev_type),
97  DeviceArch(arch), Is64Bit(true), DeviceStepping("") {}
98 
100  // TODO: the initial version generates the generic code (SKL now), need
101  // to do additional device::info calls to determine the device by it's
102  // features.
103  online_compiler(const sycl::device &)
104  : OutputFormat(compiled_code_format::spir_v), OutputFormatVersion({0, 0}),
105  DeviceType(sycl::info::device_type::all), DeviceArch(device_arch::any),
106  Is64Bit(true), DeviceStepping("") {}
107 
114  template <typename... Tys>
115  std::vector<byte> compile(const std::string &src, const Tys &...args);
116 
119  OutputFormat = fmt;
120  return *this;
121  }
122 
126  OutputFormatVersion = {major, minor};
127  return *this;
128  }
129 
132  DeviceType = type;
133  return *this;
134  }
135 
138  DeviceArch = arch;
139  return *this;
140  }
141 
144  Is64Bit = false;
145  return *this;
146  };
147 
150  Is64Bit = true;
151  return *this;
152  };
153 
157  DeviceStepping = id;
158  return *this;
159  }
160 
161 private:
163  compiled_code_format OutputFormat;
164 
166  std::pair<int, int> OutputFormatVersion;
167 
169  sycl::info::device_type DeviceType;
170 
172  device_arch DeviceArch;
173 
175  bool Is64Bit;
176 
178  std::string DeviceStepping;
179 
181  void *CompileToSPIRVHandle = nullptr;
182  void *FreeSPIRVOutputsHandle = nullptr;
183 };
184 
185 // Specializations of the online_compiler class and 'compile' function for
186 // particular languages and parameter types.
187 
192 template <>
193 template <>
194 __SYCL_EXPORT std::vector<byte>
196  const std::string &src, const std::vector<std::string> &options);
197 
200 template <>
201 template <>
202 std::vector<byte>
204  return compile(src, std::vector<std::string>{});
205 }
206 
210 template <>
211 template <>
212 __SYCL_EXPORT std::vector<byte> online_compiler<source_language::cm>::compile(
213  const std::string &src, const std::vector<std::string> &options);
214 
216 template <>
217 template <>
218 std::vector<byte>
220  return compile(src, std::vector<std::string>{});
221 }
222 
223 } // namespace ext::intel::experimental
224 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
225 } // namespace sycl
Represents an error happend during online compilation.
Represents an online compiler for the language given as template parameter.
online_compiler(const sycl::device &)
Constructs online compiler for the target specified by given SYCL device.
std::vector< byte > compile(const std::string &src, const Tys &...args)
Compiles given in-memory Lang source to a binary blob.
online_compiler< Lang > & setOutputFormatVersion(int major, int minor)
Sets the compiled code format version of the compilation target and returns *this.
online_compiler< Lang > & setTargetDeviceArch(device_arch arch)
Sets the device architecture of the compilation target and returns *this.
online_compiler< Lang > & set64bitTarget()
Makes the compilation target 64-bit and returns *this.
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.
online_compiler< Lang > & setTargetDeviceType(sycl::info::device_type type)
Sets the device type of the compilation target and returns *this.
online_compiler< Lang > & setOutputFormat(compiled_code_format fmt)
Sets the compiled code format of the compilation target and returns *this.
online_compiler< Lang > & setTargetDeviceStepping(const std::string &id)
Sets implementation-defined target device stepping of the compilation target and returns *this.
online_compiler< Lang > & set32bitTarget()
Makes the compilation target 32-bit and returns *this.
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.
A unique identifier of an item in an index space.
Definition: id.hpp:32
#define __SYCL_INLINE_VER_NAMESPACE(X)
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={})
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14