Skip to content

Commit 9112252

Browse files
authored
[SYCL] Implement INTEL feature class online_compiler (#2930)
Signed-off-by: Vyacheslav N Klochkov <[email protected]>
1 parent 30db15f commit 9112252

File tree

9 files changed

+704
-45
lines changed

9 files changed

+704
-45
lines changed

sycl/include/CL/sycl/INTEL/online_compiler.hpp

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
#pragma once
1010

11-
#include <CL/sycl/context.hpp>
1211
#include <CL/sycl/detail/defines_elementary.hpp> // for __SYCL_INLINE_NAMESPACE
12+
#include <CL/sycl/detail/export.hpp> // for __SYCL_EXPORT
1313
#include <CL/sycl/device.hpp>
1414

15-
#include <memory>
15+
#include <string>
1616
#include <vector>
1717

1818
__SYCL_INLINE_NAMESPACE(cl) {
@@ -59,7 +59,9 @@ class device_arch {
5959

6060
/// Represents an error happend during online compilation.
6161
class online_compile_error : public sycl::exception {
62-
// TBD
62+
public:
63+
online_compile_error() = default;
64+
online_compile_error(const string_class &Msg) : sycl::exception(Msg) {}
6365
};
6466

6567
/// Designates a source language for the online compiler.
@@ -70,25 +72,32 @@ enum class source_language { opencl_c, cm };
7072
template <source_language Lang> class online_compiler {
7173
public:
7274
/// Constructs online compiler which can target any device and produces
73-
/// given compiled code format. Produces device code is 64-bit.
75+
/// given compiled code format. Produces 64-bit device code.
7476
/// The created compiler is "optimistic" - it assumes all applicable SYCL
7577
/// device capabilities are supported by the target device(s).
7678
online_compiler(compiled_code_format fmt = compiled_code_format::spir_v)
7779
: OutputFormat(fmt), OutputFormatVersion({0, 0}),
78-
DeviceArch(device_arch::any), Is64Bit(true), DeviceStepping("") {}
80+
DeviceType(sycl::info::device_type::all), DeviceArch(device_arch::any),
81+
Is64Bit(true), DeviceStepping("") {}
7982

8083
/// Constructs online compiler which targets given architecture and produces
81-
/// given compiled code format. Produces device code is 64-bit.
84+
/// given compiled code format. Produces 64-bit device code.
8285
/// Throws online_compile_error if values of constructor arguments are
8386
/// contradictory or not supported - e.g. if the source language is not
8487
/// supported for given device type.
8588
online_compiler(sycl::info::device_type dev_type, device_arch arch,
8689
compiled_code_format fmt = compiled_code_format::spir_v)
87-
: OutputFormat(fmt), OutputFormatVersion({0, 0}), DeviceArch(arch),
88-
Is64Bit(true), DeviceStepping("") {}
90+
: OutputFormat(fmt), OutputFormatVersion({0, 0}), DeviceType(dev_type),
91+
DeviceArch(arch), Is64Bit(true), DeviceStepping("") {}
8992

9093
/// Constructs online compiler for the target specified by given SYCL device.
91-
online_compiler(const sycl::device &dev);
94+
// TODO: the initial version generates the generic code (SKL now), need
95+
// to do additional device::info calls to determine the device by it's
96+
// features.
97+
online_compiler(const sycl::device &dev)
98+
: OutputFormat(compiled_code_format::spir_v), OutputFormatVersion({0, 0}),
99+
DeviceType(sycl::info::device_type::all), DeviceArch(device_arch::any),
100+
Is64Bit(true), DeviceStepping("") {}
92101

93102
/// Compiles given in-memory \c Lang source to a binary blob. Blob format,
94103
/// other parameters are set in the constructor by the compilation target
@@ -100,31 +109,50 @@ template <source_language Lang> class online_compiler {
100109
std::vector<byte> compile(const std::string &src, const Tys &... args);
101110

102111
/// Sets the compiled code format of the compilation target and returns *this.
103-
online_compiler<Lang> &setOutputFormat(compiled_code_format fmt);
112+
online_compiler<Lang> &setOutputFormat(compiled_code_format fmt) {
113+
OutputFormat = fmt;
114+
return *this;
115+
}
104116

105117
/// Sets the compiled code format version of the compilation target and
106118
/// returns *this.
107-
online_compiler<Lang> &setOutputFormatVersion(int major, int minor);
119+
online_compiler<Lang> &setOutputFormatVersion(int major, int minor) {
120+
OutputFormatVersion = {major, minor};
121+
return *this;
122+
}
108123

109124
/// Sets the device type of the compilation target and returns *this.
110-
online_compiler<Lang> &setTargetDeviceType(sycl::info::device_type type);
125+
online_compiler<Lang> &setTargetDeviceType(sycl::info::device_type type) {
126+
DeviceType = type;
127+
return *this;
128+
}
111129

112130
/// Sets the device architecture of the compilation target and returns *this.
113-
online_compiler<Lang> &setTargetDeviceArch(device_arch arch);
131+
online_compiler<Lang> &setTargetDeviceArch(device_arch arch) {
132+
DeviceArch = arch;
133+
return *this;
134+
}
114135

115136
/// Makes the compilation target 32-bit and returns *this.
116-
online_compiler<Lang> &set32bitTarget();
137+
online_compiler<Lang> &set32bitTarget() {
138+
Is64Bit = false;
139+
return *this;
140+
};
117141

118142
/// Makes the compilation target 64-bit and returns *this.
119-
online_compiler<Lang> &set64bitTarget();
143+
online_compiler<Lang> &set64bitTarget() {
144+
Is64Bit = true;
145+
return *this;
146+
};
120147

121148
/// Sets implementation-defined target device stepping of the compilation
122149
/// target and returns *this.
123-
online_compiler<Lang> &setTargetDeviceStepping(const std::string &id);
150+
online_compiler<Lang> &setTargetDeviceStepping(const std::string &id) {
151+
DeviceStepping = id;
152+
return *this;
153+
}
124154

125155
private:
126-
// Compilation target specification fields: {
127-
128156
/// Compiled code format.
129157
compiled_code_format OutputFormat;
130158

@@ -142,51 +170,48 @@ template <source_language Lang> class online_compiler {
142170

143171
/// Target device stepping (implementation defined)
144172
std::string DeviceStepping;
145-
// }
173+
174+
/// Handles to helper functions used by the implementation.
175+
void *CompileToSPIRVHandle = nullptr;
176+
void *FreeSPIRVOutputsHandle = nullptr;
146177
};
147178

148179
// Specializations of the online_compiler class and 'compile' function for
149180
// particular languages and parameter types.
150181

151-
/// Compiles given OpenCL source. May throw \c online_compile_error.
152-
/// @param src - contents of the source
182+
/// Compiles the given OpenCL source. May throw \c online_compile_error.
183+
/// @param src - contents of the source.
184+
/// @param options - compilation options (implementation defined); standard
185+
/// OpenCL JIT compiler options must be supported.
186+
template <>
187+
template <>
188+
__SYCL_EXPORT std::vector<byte>
189+
online_compiler<source_language::opencl_c>::compile(
190+
const std::string &src, const std::vector<std::string> &options);
191+
192+
/// Compiles the given OpenCL source. May throw \c online_compile_error.
193+
/// @param src - contents of the source.
153194
template <>
154195
template <>
155196
std::vector<byte>
156197
online_compiler<source_language::opencl_c>::compile(const std::string &src) {
157-
// real implementation will call some non-templated impl function here
158-
return std::vector<byte>{};
198+
return compile(src, std::vector<std::string>{});
159199
}
160200

161-
/// Compiles given OpenCL source. May throw \c online_compile_error.
162-
/// @param src - contents of the source
163-
/// @param options - compilation options (implementation defined); standard
164-
/// OpenCL JIT compiler options must be supported
201+
/// Compiles the given CM source \p src.
202+
/// @param src - contents of the source.
203+
/// @param options - compilation options (implementation defined).
165204
template <>
166205
template <>
167-
std::vector<byte> online_compiler<source_language::opencl_c>::compile(
168-
const std::string &src, const std::vector<std::string> &options) {
169-
// real implementation will call some non-templated impl function here
170-
return std::vector<byte>{};
171-
}
206+
__SYCL_EXPORT std::vector<byte> online_compiler<source_language::cm>::compile(
207+
const std::string &src, const std::vector<std::string> &options);
172208

173-
/// Compiles given CM source.
209+
/// Compiles the given CM source \p src.
174210
template <>
175211
template <>
176212
std::vector<byte>
177213
online_compiler<source_language::cm>::compile(const std::string &src) {
178-
// real implementation will call some non-templated impl function here
179-
return std::vector<byte>{};
180-
}
181-
182-
/// Compiles given CM source.
183-
/// @param options - compilation options (implementation defined)
184-
template <>
185-
template <>
186-
std::vector<byte> online_compiler<source_language::cm>::compile(
187-
const std::string &src, const std::vector<std::string> &options) {
188-
// real implementation will call some non-templated impl function here
189-
return std::vector<byte>{};
214+
return compile(src, std::vector<std::string>{});
190215
}
191216

192217
} // namespace INTEL

sycl/include/CL/sycl/exception.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class __SYCL_EXPORT exception : public std::exception {
5252
shared_ptr_class<context> Context = nullptr)
5353
: MMsg(Msg + " " + detail::codeToString(CLErr)), MCLErr(CLErr),
5454
MContext(Context) {}
55+
56+
exception(const string_class &Msg) : MMsg(Msg), MContext(nullptr) {}
5557
};
5658

5759
class runtime_error : public exception {

sycl/source/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ set(SYCL_SOURCES
132132
"detail/program_impl.cpp"
133133
"detail/program_manager/program_manager.cpp"
134134
"detail/queue_impl.cpp"
135+
"detail/online_compiler/online_compiler.cpp"
135136
"detail/os_util.cpp"
136137
"detail/platform_util.cpp"
137138
"detail/reduction.cpp"
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//===------- ocloc_api.h --------------------------------------------------===//
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+
// This file is copied from
10+
// https://github.com/intel/compute-runtime/blob/master/shared/offline_compiler/source/ocloc_api.h
11+
12+
#include <cstdint>
13+
14+
#ifndef OCLOC_MAKE_VERSION
15+
/// Generates ocloc API versions
16+
#define OCLOC_MAKE_VERSION(_major, _minor) \
17+
((_major << 16) | (_minor & 0x0000ffff))
18+
#endif // OCLOC_MAKE_VERSION
19+
20+
typedef enum _ocloc_version_t {
21+
OCLOC_VERSION_1_0 = OCLOC_MAKE_VERSION(1, 0), ///< version 1.0
22+
OCLOC_VERSION_CURRENT = OCLOC_MAKE_VERSION(1, 0), ///< latest known version
23+
OCLOC_VERSION_FORCE_UINT32 = 0x7fffffff
24+
} ocloc_version_t;
25+
26+
#ifdef _WIN32
27+
#define SIGNATURE __declspec(dllexport) int __cdecl
28+
#else
29+
#define SIGNATURE int
30+
#endif
31+
32+
extern "C" {
33+
/// Invokes ocloc API using C interface. Supported commands match
34+
/// the functionality of ocloc executable (check ocloc's "help"
35+
/// for reference : shared/offline_compiler/source/ocloc_api.cpp)
36+
/// at https://github.com/intel/compute-runtime.
37+
///
38+
/// NumArgs and argv params represent the command line.
39+
/// Remaining params represent I/O.
40+
/// Output params should be freed using oclocFreeOutput() when
41+
/// no longer needed.
42+
/// List and names of outputs match outputs of ocloc executable.
43+
///
44+
/// \param NumArgs is the number of arguments to pass to ocloc.
45+
///
46+
/// \param Argv is an array of arguments to be passed to ocloc.
47+
///
48+
/// \param NumSources is the number of in-memory representations
49+
/// of source files to be passed to ocloc.
50+
///
51+
/// \param DataSources is an array of in-memory representations
52+
/// of source files to be passed to ocloc.
53+
///
54+
/// \param LenSources is an array of sizes of in-memory representations
55+
/// of source files passed to ocloc as DataSources.
56+
///
57+
/// \param NameSources is an array of names of in-memory representations
58+
/// of source files passed to ocloc as DataSources.
59+
///
60+
/// \param NumInputHeaders is the number of in-memory representations
61+
/// of header files to be passed to ocloc.
62+
///
63+
/// \param DataInputHeaders is an array of in-memory representations
64+
/// of header files to be passed to ocloc.
65+
///
66+
/// \param LenInputHeaders is an array of sizes of in-memory representations
67+
/// of header files passed to ocloc as DataInputHeaders.
68+
///
69+
/// \param NameInputHeaders is an array of names of in-memory representations
70+
/// of header files passed to ocloc as DataInputHeaders.
71+
///
72+
/// \param NumOutputs returns the number of outputs.
73+
///
74+
/// \param DataOutputs returns an array of in-memory representations
75+
/// of output files.
76+
///
77+
/// \param LenOutputs returns an array of sizes of in-memory representations
78+
/// of output files.
79+
///
80+
/// \param NameOutputs returns an array of names of in-memory representations
81+
/// of output files. Special name stdout.log describes output that contains
82+
/// messages generated by ocloc (e.g. compiler errors/warnings).
83+
///
84+
/// \returns 0 on succes. Returns non-0 in case of failure.
85+
SIGNATURE oclocInvoke(uint32_t NumArgs, const char *Argv[], uint32_t NumSources,
86+
const uint8_t **DataSources, const uint64_t *LenSources,
87+
const char **NameSources, uint32_t NumInputHeaders,
88+
const uint8_t **DataInputHeaders,
89+
const uint64_t *LenInputHeaders,
90+
const char **NameInputHeaders, uint32_t *NumOutputs,
91+
uint8_t ***DataOutputs, uint64_t **LenOutputs,
92+
char ***NameOutputs);
93+
94+
/// Frees results of oclocInvoke
95+
///
96+
/// \param NumOutputs is number of outputs as returned by oclocInvoke().
97+
///
98+
/// \param DataOutputs is array of outputs as returned by oclocInvoke().
99+
///
100+
/// \param LenOutputs is array of sizes of outputs as returned by oclocInvoke().
101+
///
102+
/// \param NameOutputs is array of names of outputs as returned by oclocInvoke()
103+
///
104+
/// \returns 0 on succes. Returns non-0 in case of failure.
105+
SIGNATURE oclocFreeOutput(uint32_t *NumOutputs, uint8_t ***DataOutputs,
106+
uint64_t **LenOutputs, char ***NameOutputs);
107+
108+
/// Returns the current version of ocloc.
109+
SIGNATURE oclocVersion();
110+
}

0 commit comments

Comments
 (0)