Skip to content

Commit b2a7eca

Browse files
committed
[SYCL] Add online compilation API interface (Intel extension).
This API is aimed at compiling various source languages (e.g. OpenCL) to binary format. Only SPIRV is supported for now. Signed-off-by: Konstantin S Bobrovsky <[email protected]>
1 parent d44aa3f commit b2a7eca

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
#include <CL/sycl/detail/defines_elementary.hpp> // for __SYCL_INLINE_NAMESPACE
10+
#include <CL/sycl/context.hpp>
11+
#include <CL/sycl/device.hpp>
12+
13+
#include <memory>
14+
#include <vector>
15+
16+
__SYCL_INLINE_NAMESPACE(cl) {
17+
namespace sycl {
18+
namespace INTEL {
19+
20+
using byte = unsigned char;
21+
22+
enum class compiled_code_format {
23+
SPIRV // the only format supported for now
24+
};
25+
26+
class device_arch {
27+
public:
28+
static constexpr int any = 0;
29+
30+
device_arch(int Val) : Val(Val) {}
31+
32+
enum gpu {
33+
gpu_any = 1,
34+
gpu_gen9,
35+
gpu_skl = gpu_gen9,
36+
gpu_gen9_5,
37+
gpu_kbl = gpu_gen9_5,
38+
gpu_cfl = gpu_gen9_5,
39+
gpu_gen11,
40+
gpu_icl = gpu_gen11,
41+
gpu_gen12
42+
};
43+
44+
enum cpu {
45+
cpu_any = 1,
46+
};
47+
48+
enum fpga {
49+
fpga_any = 1,
50+
};
51+
52+
operator int() { return Val; }
53+
54+
private:
55+
int Val;
56+
};
57+
58+
/// Represents an error happend during online compilation.
59+
class online_compile_error : public sycl::exception {
60+
// TBD
61+
};
62+
63+
/// Designates a source language for the online compiler.
64+
enum class source_language { OpenCL_C, CM };
65+
66+
/// Represents an online compiler for the language given as template
67+
/// parameter.
68+
template <source_language Lang> class online_compiler {
69+
public:
70+
/// Constructs online compiler which can target any device and produces
71+
/// given compiled code format. Produces device code is 64-bit.
72+
/// The created compiler is "optimistic" - it assumes all applicable SYCL
73+
/// device capabilities are supported by the target device(s).
74+
online_compiler(compiled_code_format fmt = compiled_code_format::SPIRV)
75+
: OutputFormat(fmt), OutputFormatVersion({0, 0}),
76+
DeviceArch(device_arch::any), Is64Bit(true), DeviceStepping("") {}
77+
78+
/// Constructs online compiler which targets given architecture and produces
79+
/// given compiled code format. Produces device code is 64-bit.
80+
/// Throws online_compile_error if values of constructor arguments are
81+
/// contradictory or not supported - e.g. if the source language is not
82+
/// supported for given device type.
83+
online_compiler(sycl::info::device_type dev_type, device_arch arch,
84+
compiled_code_format fmt = compiled_code_format::SPIRV)
85+
: OutputFormat(fmt), OutputFormatVersion({0, 0}), DeviceArch(arch),
86+
Is64Bit(true), DeviceStepping("") {}
87+
88+
/// Constructs online compiler for the target specified by given SYCL device.
89+
online_compiler(const sycl::device &dev);
90+
91+
/// Compiles given in-memory \c Lang source to a binary blob. Blob format,
92+
/// other parameters are set in the constructor by the compilation target
93+
/// specification parameters.
94+
/// Specialization for each language will provide exact signatures, which
95+
/// can be different for different languages.
96+
/// Throws online_compile_error if compilation is not successful.
97+
template <typename... Tys>
98+
std::vector<byte> compile(const std::string &src, const Tys&... args);
99+
100+
/// Sets the compiled code format of the compilation target and returns *this.
101+
online_compiler<Lang> &setOutputFormat(compiled_code_format fmt);
102+
103+
/// Sets the compiled code format version of the compilation target and
104+
/// returns *this.
105+
online_compiler<Lang> &setOutputFormatVersion(int major, int minor);
106+
107+
/// Sets the device type of the compilation target and returns *this.
108+
online_compiler<Lang> &setTargetDeviceType(sycl::info::device_type type);
109+
110+
/// Sets the device architecture of the compilation target and returns *this.
111+
online_compiler<Lang> &setTargetDeviceArch(device_arch arch);
112+
113+
/// Makes the compilation target 32-bit and returns *this.
114+
online_compiler<Lang> &set32bitTarget();
115+
116+
/// Makes the compilation target 64-bit and returns *this.
117+
online_compiler<Lang> &set64bitTarget();
118+
119+
/// Sets implementation-defined target device stepping of the compilation
120+
/// target and returns *this.
121+
online_compiler<Lang> &setTargetDeviceStepping(const std::string &id);
122+
123+
private:
124+
// Compilation target specification fields: {
125+
126+
/// Compiled code format.
127+
compiled_code_format OutputFormat;
128+
129+
/// Compiled code format version - a pair of "major" and "minor" components
130+
std::pair<int, int> OutputFormatVersion;
131+
132+
/// Target device type
133+
sycl::info::device_type DeviceType;
134+
135+
/// Target device architecture
136+
device_arch DeviceArch;
137+
138+
/// Whether the target device architecture is 64-bit
139+
bool Is64Bit;
140+
141+
/// Target device stepping (implementation defined)
142+
std::string DeviceStepping;
143+
// }
144+
};
145+
146+
// Specializations of the online_compiler class and 'compile' function for
147+
// particular languages and parameter types.
148+
149+
/// Compiles given OpenCL source. May throw \c online_compile_error.
150+
/// @param src - contents of the source
151+
template <>
152+
template <>
153+
std::vector<byte> online_compiler<source_language::OpenCL_C>::compile(
154+
const std::string &src) {
155+
// real implementation will call some non-templated impl function here
156+
return std::vector<byte>{};
157+
}
158+
159+
/// Compiles given OpenCL source. May throw \c online_compile_error.
160+
/// @param src - contents of the source
161+
/// @param options - compilation options (implementation defined); standard
162+
/// OpenCL JIT compiler options must be supported
163+
template <>
164+
template <>
165+
std::vector<byte> online_compiler<source_language::OpenCL_C>::compile(
166+
const std::string &src, const std::vector<std::string> &options) {
167+
// real implementation will call some non-templated impl function here
168+
return std::vector<byte>{};
169+
}
170+
171+
/// Compiles given CM source.
172+
template <>
173+
template <>
174+
std::vector<byte> online_compiler<source_language::CM>::compile(
175+
const std::string &src) {
176+
// real implementation will call some non-templated impl function here
177+
return std::vector<byte>{};
178+
}
179+
180+
/// Compiles given CM source.
181+
/// @param options - compilation options (implementation defined)
182+
template <>
183+
template <>
184+
std::vector<byte> online_compiler<source_language::CM>::compile(
185+
const std::string &src, const std::vector<std::string> &options) {
186+
// real implementation will call some non-templated impl function here
187+
return std::vector<byte>{};
188+
}
189+
190+
} // namespace sycl
191+
} // namespace INTEL
192+
} // __SYCL_INLINE_NAMESPACE(cl)

0 commit comments

Comments
 (0)