Skip to content

Commit 242875a

Browse files
[SYCL] Support online_compiler::compile compiled with pre-C++11 ABI
This is an updated version of intel#16179. Run-time ABI mismatch resulting in a segmentation fault was uncovered why enabling extra testing in intel#16235.
1 parent 12efeae commit 242875a

File tree

5 files changed

+216
-153
lines changed

5 files changed

+216
-153
lines changed

sycl/include/sycl/detail/string_view.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ class string_view {
3737

3838
const char *data() const noexcept { return str; }
3939

40-
friend bool operator==(const string_view &lhs,
41-
std::string_view rhs) noexcept {
40+
friend bool operator==(string_view lhs, std::string_view rhs) noexcept {
4241
return rhs == lhs.data();
4342
}
44-
friend bool operator==(std::string_view lhs,
45-
const string_view &rhs) noexcept {
43+
friend bool operator==(std::string_view lhs, string_view rhs) noexcept {
4644
return lhs == rhs.data();
4745
}
46+
47+
friend bool operator!=(string_view lhs, std::string_view rhs) noexcept {
48+
return rhs != lhs.data();
49+
}
50+
friend bool operator!=(std::string_view lhs, string_view rhs) noexcept {
51+
return lhs != rhs.data();
52+
}
4853
};
4954

5055
} // namespace detail

sycl/include/sycl/ext/intel/experimental/online_compiler.hpp

Lines changed: 154 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
namespace sycl {
1919
inline namespace _V1 {
2020
namespace ext::intel::experimental {
21+
namespace detail {
22+
using namespace sycl::detail;
23+
}
2124

2225
using byte = unsigned char;
2326

@@ -81,112 +84,139 @@ class __SYCL2020_DEPRECATED(
8184
"experimental online_compiler is being deprecated. See "
8285
"'sycl_ext_oneapi_kernel_compiler.asciidoc' instead for new kernel "
8386
"compiler extension to kernel_bundle implementation.") online_compiler {
84-
public:
85-
/// Constructs online compiler which can target any device and produces
86-
/// given compiled code format. Produces 64-bit device code.
87-
/// The created compiler is "optimistic" - it assumes all applicable SYCL
88-
/// device capabilities are supported by the target device(s).
89-
online_compiler(compiled_code_format fmt = compiled_code_format::spir_v)
90-
: OutputFormat(fmt), OutputFormatVersion({0, 0}),
91-
DeviceType(sycl::info::device_type::all), DeviceArch(device_arch::any),
92-
Is64Bit(true), DeviceStepping("") {}
93-
94-
/// Constructs online compiler which targets given architecture and produces
95-
/// given compiled code format. Produces 64-bit device code.
96-
/// Throws online_compile_error if values of constructor arguments are
97-
/// contradictory or not supported - e.g. if the source language is not
98-
/// supported for given device type.
99-
online_compiler(sycl::info::device_type dev_type, device_arch arch,
100-
compiled_code_format fmt = compiled_code_format::spir_v)
101-
: OutputFormat(fmt), OutputFormatVersion({0, 0}), DeviceType(dev_type),
102-
DeviceArch(arch), Is64Bit(true), DeviceStepping("") {}
103-
104-
/// Constructs online compiler for the target specified by given SYCL device.
105-
// TODO: the initial version generates the generic code (SKL now), need
106-
// to do additional device::info calls to determine the device by it's
107-
// features.
108-
online_compiler(const sycl::device &)
109-
: OutputFormat(compiled_code_format::spir_v), OutputFormatVersion({0, 0}),
110-
DeviceType(sycl::info::device_type::all), DeviceArch(device_arch::any),
111-
Is64Bit(true), DeviceStepping("") {}
112-
113-
/// Compiles given in-memory \c Lang source to a binary blob. Blob format,
114-
/// other parameters are set in the constructor by the compilation target
115-
/// specification parameters.
116-
/// Specialization for each language will provide exact signatures, which
117-
/// can be different for different languages.
118-
/// Throws online_compile_error if compilation is not successful.
119-
template <typename... Tys>
120-
std::vector<byte> compile(const std::string &src, const Tys &...args);
121-
122-
/// Sets the compiled code format of the compilation target and returns *this.
123-
online_compiler<Lang> &setOutputFormat(compiled_code_format fmt) {
124-
OutputFormat = fmt;
125-
return *this;
126-
}
127-
128-
/// Sets the compiled code format version of the compilation target and
129-
/// returns *this.
130-
online_compiler<Lang> &setOutputFormatVersion(int major, int minor) {
131-
OutputFormatVersion = {major, minor};
132-
return *this;
133-
}
134-
135-
/// Sets the device type of the compilation target and returns *this.
136-
online_compiler<Lang> &setTargetDeviceType(sycl::info::device_type type) {
137-
DeviceType = type;
138-
return *this;
87+
#if __INTEL_PREVIEW_BREAKING_CHANGES
88+
// Refactor this during next ABI Breaking window. We have an `std::string`
89+
// data member so cannot be accessing `this` when crossing ABI boundary.
90+
#endif
91+
__SYCL_EXPORT static std::vector<byte>
92+
compile_impl(detail::string_view Src,
93+
const std::vector<detail::string_view> &Options,
94+
std::pair<int, int> OutputFormatVersion,
95+
sycl::info::device_type DeviceType, device_arch DeviceArch,
96+
bool Is64Bit, detail::string_view DeviceStepping,
97+
void *&CompileToSPIRVHandle, void *&FreeSPIRVOutputsHandle);
98+
99+
std::vector<byte> compile_impl(const std::string &Source,
100+
const std::vector<std::string> &UserArgs) {
101+
std::vector<sycl::detail::string_view> Args;
102+
for (auto &&Arg : UserArgs)
103+
Args.emplace_back(Arg);
104+
105+
return compile_impl(std::string_view{Source}, Args, OutputFormatVersion,
106+
DeviceType, DeviceArch, Is64Bit,
107+
std::string_view{DeviceStepping}, CompileToSPIRVHandle,
108+
FreeSPIRVOutputsHandle);
139109
}
140110

141-
/// Sets the device architecture of the compilation target and returns *this.
142-
online_compiler<Lang> &setTargetDeviceArch(device_arch arch) {
143-
DeviceArch = arch;
144-
return *this;
145-
}
146-
147-
/// Makes the compilation target 32-bit and returns *this.
148-
online_compiler<Lang> &set32bitTarget() {
149-
Is64Bit = false;
150-
return *this;
151-
};
152-
153-
/// Makes the compilation target 64-bit and returns *this.
154-
online_compiler<Lang> &set64bitTarget() {
155-
Is64Bit = true;
156-
return *this;
111+
public:
112+
/// Constructs online compiler which can target any device and produces
113+
/// given compiled code format. Produces 64-bit device code.
114+
/// The created compiler is "optimistic" - it assumes all applicable SYCL
115+
/// device capabilities are supported by the target device(s).
116+
online_compiler(compiled_code_format fmt = compiled_code_format::spir_v)
117+
: OutputFormat(fmt), OutputFormatVersion({0, 0}),
118+
DeviceType(sycl::info::device_type::all),
119+
DeviceArch(device_arch::any), Is64Bit(true), DeviceStepping("") {}
120+
121+
/// Constructs online compiler which targets given architecture and produces
122+
/// given compiled code format. Produces 64-bit device code.
123+
/// Throws online_compile_error if values of constructor arguments are
124+
/// contradictory or not supported - e.g. if the source language is not
125+
/// supported for given device type.
126+
online_compiler(sycl::info::device_type dev_type, device_arch arch,
127+
compiled_code_format fmt = compiled_code_format::spir_v)
128+
: OutputFormat(fmt), OutputFormatVersion({0, 0}), DeviceType(dev_type),
129+
DeviceArch(arch), Is64Bit(true), DeviceStepping("") {}
130+
131+
/// Constructs online compiler for the target specified by given SYCL
132+
/// device.
133+
// TODO: the initial version generates the generic code (SKL now), need
134+
// to do additional device::info calls to determine the device by it's
135+
// features.
136+
online_compiler(const sycl::device &)
137+
: OutputFormat(compiled_code_format::spir_v),
138+
OutputFormatVersion({0, 0}), DeviceType(sycl::info::device_type::all),
139+
DeviceArch(device_arch::any), Is64Bit(true), DeviceStepping("") {}
140+
141+
/// Compiles given in-memory \c Lang source to a binary blob. Blob format,
142+
/// other parameters are set in the constructor by the compilation target
143+
/// specification parameters.
144+
/// Specialization for each language will provide exact signatures, which
145+
/// can be different for different languages.
146+
/// Throws online_compile_error if compilation is not successful.
147+
template <typename... Tys>
148+
std::vector<byte> compile(const std::string &src, const Tys &...args);
149+
150+
/// Sets the compiled code format of the compilation target and returns
151+
/// *this.
152+
online_compiler<Lang> &setOutputFormat(compiled_code_format fmt) {
153+
OutputFormat = fmt;
154+
return *this;
155+
}
156+
157+
/// Sets the compiled code format version of the compilation target and
158+
/// returns *this.
159+
online_compiler<Lang> &setOutputFormatVersion(int major, int minor) {
160+
OutputFormatVersion = {major, minor};
161+
return *this;
162+
}
163+
164+
/// Sets the device type of the compilation target and returns *this.
165+
online_compiler<Lang> &setTargetDeviceType(sycl::info::device_type type) {
166+
DeviceType = type;
167+
return *this;
168+
}
169+
170+
/// Sets the device architecture of the compilation target and returns
171+
/// *this.
172+
online_compiler<Lang> &setTargetDeviceArch(device_arch arch) {
173+
DeviceArch = arch;
174+
return *this;
175+
}
176+
177+
/// Makes the compilation target 32-bit and returns *this.
178+
online_compiler<Lang> &set32bitTarget() {
179+
Is64Bit = false;
180+
return *this;
181+
};
182+
183+
/// Makes the compilation target 64-bit and returns *this.
184+
online_compiler<Lang> &set64bitTarget() {
185+
Is64Bit = true;
186+
return *this;
187+
};
188+
189+
/// Sets implementation-defined target device stepping of the compilation
190+
/// target and returns *this.
191+
online_compiler<Lang> &setTargetDeviceStepping(const std::string &id) {
192+
DeviceStepping = id;
193+
return *this;
194+
}
195+
196+
private:
197+
/// Compiled code format.
198+
compiled_code_format OutputFormat;
199+
200+
/// Compiled code format version - a pair of "major" and "minor" components
201+
std::pair<int, int> OutputFormatVersion;
202+
203+
/// Target device type
204+
sycl::info::device_type DeviceType;
205+
206+
/// Target device architecture
207+
device_arch DeviceArch;
208+
209+
/// Whether the target device architecture is 64-bit
210+
bool Is64Bit;
211+
212+
/// Target device stepping (implementation defined)
213+
std::string DeviceStepping;
214+
215+
/// Handles to helper functions used by the implementation.
216+
void *CompileToSPIRVHandle = nullptr;
217+
void *FreeSPIRVOutputsHandle = nullptr;
157218
};
158219

159-
/// Sets implementation-defined target device stepping of the compilation
160-
/// target and returns *this.
161-
online_compiler<Lang> &setTargetDeviceStepping(const std::string &id) {
162-
DeviceStepping = id;
163-
return *this;
164-
}
165-
166-
private:
167-
/// Compiled code format.
168-
compiled_code_format OutputFormat;
169-
170-
/// Compiled code format version - a pair of "major" and "minor" components
171-
std::pair<int, int> OutputFormatVersion;
172-
173-
/// Target device type
174-
sycl::info::device_type DeviceType;
175-
176-
/// Target device architecture
177-
device_arch DeviceArch;
178-
179-
/// Whether the target device architecture is 64-bit
180-
bool Is64Bit;
181-
182-
/// Target device stepping (implementation defined)
183-
std::string DeviceStepping;
184-
185-
/// Handles to helper functions used by the implementation.
186-
void *CompileToSPIRVHandle = nullptr;
187-
void *FreeSPIRVOutputsHandle = nullptr;
188-
};
189-
190220
// Specializations of the online_compiler class and 'compile' function for
191221
// particular languages and parameter types.
192222

@@ -196,9 +226,17 @@ class __SYCL2020_DEPRECATED(
196226
/// OpenCL JIT compiler options must be supported.
197227
template <>
198228
template <>
199-
__SYCL_EXPORT std::vector<byte>
200-
online_compiler<source_language::opencl_c>::compile(
201-
const std::string &src, const std::vector<std::string> &options);
229+
#if !defined(__SYCL_ONLINE_COMPILER_CPP) || \
230+
defined(__INTEL_PREVIEW_BREAKING_CHANGES)
231+
inline
232+
#else
233+
__SYCL_EXPORT
234+
#endif
235+
std::vector<byte>
236+
online_compiler<source_language::opencl_c>::compile(
237+
const std::string &src, const std::vector<std::string> &options) {
238+
return compile_impl(src, options);
239+
}
202240

203241
/// Compiles the given OpenCL source. May throw \c online_compile_error.
204242
/// @param src - contents of the source.
@@ -214,8 +252,17 @@ online_compiler<source_language::opencl_c>::compile(const std::string &src) {
214252
/// @param options - compilation options (implementation defined).
215253
template <>
216254
template <>
217-
__SYCL_EXPORT std::vector<byte> online_compiler<source_language::cm>::compile(
218-
const std::string &src, const std::vector<std::string> &options);
255+
#if !defined(__SYCL_ONLINE_COMPILER_CPP) || \
256+
defined(__INTEL_PREVIEW_BREAKING_CHANGES)
257+
inline
258+
#else
259+
__SYCL_EXPORT
260+
#endif
261+
std::vector<byte>
262+
online_compiler<source_language::cm>::compile(
263+
const std::string &src, const std::vector<std::string> &options) {
264+
return compile_impl(src, options);
265+
}
219266

220267
/// Compiles the given CM source \p src.
221268
template <>

0 commit comments

Comments
 (0)