Skip to content

Commit ccb2df5

Browse files
[SYCL] Reject unsuitable old ocloc from Kernel Compile by passing device IPVersions when loading (#18170)
For Kernel Compiler Open CL C support the `device.ext_can_compile` now passes the device down so we can make sure OCLOC supports commands with the -dev IPVersionStr . --------- Signed-off-by: Chris Perkins <[email protected]>
1 parent 17b287a commit ccb2df5

File tree

8 files changed

+98
-37
lines changed

8 files changed

+98
-37
lines changed

sycl/include/sycl/kernel_bundle.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,8 +1101,6 @@ struct is_property_key_of<registered_names_key,
11011101

11021102
namespace detail {
11031103
// forward decls
1104-
__SYCL_EXPORT bool is_source_kernel_bundle_supported(backend BE,
1105-
source_language Language);
11061104

11071105
__SYCL_EXPORT kernel_bundle<bundle_state::ext_oneapi_source>
11081106
make_kernel_bundle_from_source(

sycl/source/detail/device_impl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,12 @@ bool device_impl::isGetDeviceAndHostTimerSupported() {
891891
bool device_impl::extOneapiCanCompile(
892892
ext::oneapi::experimental::source_language Language) {
893893
try {
894+
// Get the shared_ptr to this object from the platform that owns it.
895+
std::shared_ptr<device_impl> Self = MPlatform->getOrMakeDeviceImpl(MDevice);
894896
return sycl::ext::oneapi::experimental::detail::
895-
is_source_kernel_bundle_supported(getBackend(), Language);
897+
is_source_kernel_bundle_supported(Language,
898+
std::vector<DeviceImplPtr>{Self});
899+
896900
} catch (sycl::exception &) {
897901
return false;
898902
}

sycl/source/detail/kernel_bundle_impl.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#pragma once
1010

1111
#include <detail/device_image_impl.hpp>
12+
#include <detail/device_impl.hpp>
1213
#include <detail/kernel_impl.hpp>
1314
#include <detail/program_manager/program_manager.hpp>
1415
#include <sycl/backend_types.hpp>
@@ -30,6 +31,18 @@
3031

3132
namespace sycl {
3233
inline namespace _V1 {
34+
35+
namespace ext::oneapi::experimental::detail {
36+
using DeviceImplPtr = std::shared_ptr<sycl::detail::device_impl>;
37+
bool is_source_kernel_bundle_supported(
38+
sycl::ext::oneapi::experimental::source_language Language,
39+
const context &Ctx);
40+
41+
bool is_source_kernel_bundle_supported(
42+
sycl::ext::oneapi::experimental::source_language Language,
43+
const std::vector<DeviceImplPtr> &Devices);
44+
} // namespace ext::oneapi::experimental::detail
45+
3346
namespace detail {
3447

3548
static bool checkAllDevicesAreInContext(const std::vector<device> &Devices,

sycl/source/detail/kernel_compiler/kernel_compiler_opencl.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ inline namespace _V1 {
2525
namespace ext::oneapi::experimental {
2626
namespace detail {
2727

28+
// forward declaration
29+
std::string InvokeOclocQuery(const std::vector<uint32_t> &IPVersionVec,
30+
const char *identifier);
31+
2832
// ensures the OclocLibrary has the right version, etc.
2933
void checkOclocLibrary(void *OclocLibrary) {
3034
void *OclocVersionHandle =
@@ -64,35 +68,40 @@ static std::unique_ptr<void, std::function<void(void *)>>
6468
std::ignore = sycl::detail::ur::unloadOsLibrary(StoredPtr);
6569
});
6670

67-
// load the ocloc shared library, check it.
68-
void loadOclocLibrary() {
71+
void loadOclocLibrary(const std::vector<uint32_t> &IPVersionVec) {
6972
#ifdef __SYCL_RT_OS_WINDOWS
70-
static const std::string OclocLibraryName = "ocloc64.dll";
73+
static const std::string OclocPath = "ocloc64.dll";
7174
#else
72-
static const std::string OclocLibraryName = "libocloc.so";
75+
static const std::string OclocPath = "libocloc.so";
7376
#endif
74-
void *tempPtr = OclocLibrary.get();
75-
if (tempPtr == nullptr) {
76-
tempPtr = sycl::detail::ur::loadOsLibrary(OclocLibraryName);
77+
78+
// set OclocLibrary value by side effect.
79+
try {
80+
// Load then perform checks. Each check throws.
81+
void *tempPtr = sycl::detail::ur::loadOsLibrary(OclocPath);
82+
OclocLibrary.reset(tempPtr);
7783

7884
if (tempPtr == nullptr)
7985
throw sycl::exception(make_error_code(errc::build),
80-
"Unable to load ocloc library " + OclocLibraryName);
86+
"Unable to load ocloc from " + OclocPath);
8187

8288
checkOclocLibrary(tempPtr);
8389

84-
OclocLibrary.reset(tempPtr);
90+
InvokeOclocQuery(IPVersionVec, "CL_DEVICE_OPENCL_C_ALL_VERSIONS");
91+
} catch (const sycl::exception &) {
92+
OclocLibrary.reset(nullptr);
93+
std::rethrow_exception(std::current_exception());
8594
}
8695
}
8796

88-
bool OpenCLC_Compilation_Available() {
97+
bool OpenCLC_Compilation_Available(const std::vector<uint32_t> &IPVersionVec) {
8998
// Already loaded?
9099
if (OclocLibrary != nullptr)
91100
return true;
92101

93102
try {
94103
// loads and checks version
95-
loadOclocLibrary();
104+
loadOclocLibrary(IPVersionVec);
96105
return true;
97106
} catch (...) {
98107
return false;
@@ -102,11 +111,12 @@ bool OpenCLC_Compilation_Available() {
102111
using voidPtr = void *;
103112

104113
void SetupLibrary(voidPtr &oclocInvokeHandle, voidPtr &oclocFreeOutputHandle,
105-
std::error_code the_errc) {
106-
if (!oclocInvokeHandle) {
107-
if (OclocLibrary == nullptr)
108-
loadOclocLibrary();
114+
std::error_code the_errc,
115+
const std::vector<uint32_t> &IPVersionVec) {
116+
if (OclocLibrary == nullptr)
117+
loadOclocLibrary(IPVersionVec);
109118

119+
if (!oclocInvokeHandle) {
110120
oclocInvokeHandle = sycl::detail::ur::getOsLibraryFuncAddress(
111121
OclocLibrary.get(), "oclocInvoke");
112122
if (!oclocInvokeHandle)
@@ -145,7 +155,8 @@ std::string InvokeOclocQuery(const std::vector<uint32_t> &IPVersionVec,
145155
static void *oclocFreeOutputHandle = nullptr;
146156
std::error_code the_errc = make_error_code(errc::runtime);
147157

148-
SetupLibrary(oclocInvokeHandle, oclocFreeOutputHandle, the_errc);
158+
SetupLibrary(oclocInvokeHandle, oclocFreeOutputHandle, the_errc,
159+
IPVersionVec);
149160

150161
uint32_t NumOutputs = 0;
151162
uint8_t **Outputs = nullptr;
@@ -205,7 +216,8 @@ spirv_vec_t OpenCLC_to_SPIRV(const std::string &Source,
205216
static void *oclocFreeOutputHandle = nullptr;
206217
std::error_code build_errc = make_error_code(errc::build);
207218

208-
SetupLibrary(oclocInvokeHandle, oclocFreeOutputHandle, build_errc);
219+
SetupLibrary(oclocInvokeHandle, oclocFreeOutputHandle, build_errc,
220+
IPVersionVec);
209221

210222
// assemble ocloc args
211223
std::string CombinedUserArgs =

sycl/source/detail/kernel_compiler/kernel_compiler_opencl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ spirv_vec_t OpenCLC_to_SPIRV(const std::string &Source,
2424
const std::vector<uint32_t> &IPVersionVec,
2525
const std::vector<std::string> &UserArgs,
2626
std::string *LogPtr);
27-
28-
bool OpenCLC_Compilation_Available();
27+
// IPVersionVec gets flattened and passed to ocloc as the -dev flag.
28+
bool OpenCLC_Compilation_Available(const std::vector<uint32_t> &IPVersionVec);
2929

3030
bool OpenCLC_Feature_Available(const std::string &Feature, uint32_t IPVersion);
3131

sycl/source/kernel_bundle.cpp

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -387,24 +387,62 @@ namespace detail {
387387
/////////////////////////
388388
// syclex::detail::is_source_kernel_bundle_supported
389389
/////////////////////////
390-
bool is_source_kernel_bundle_supported(backend BE, source_language Language) {
390+
391+
bool is_source_kernel_bundle_supported(
392+
sycl::ext::oneapi::experimental::source_language Language,
393+
const std::vector<DeviceImplPtr> &DeviceImplVec) {
394+
backend BE = DeviceImplVec[0]->getBackend();
391395
// Support is limited to the opencl and level_zero backends.
392396
bool BE_Acceptable = (BE == sycl::backend::ext_oneapi_level_zero) ||
393397
(BE == sycl::backend::opencl);
394-
if (BE_Acceptable) {
395-
if (Language == source_language::opencl) {
396-
return detail::OpenCLC_Compilation_Available();
397-
} else if (Language == source_language::spirv) {
398-
return true;
399-
} else if (Language == source_language::sycl) {
400-
return detail::SYCL_JIT_Compilation_Available();
401-
}
398+
if (!BE_Acceptable)
399+
return false;
400+
401+
if (Language == source_language::spirv) {
402+
return true;
403+
} else if (Language == source_language::sycl) {
404+
return detail::SYCL_JIT_Compilation_Available();
405+
} else if (Language == source_language::opencl) {
406+
if (DeviceImplVec.empty())
407+
return false;
408+
409+
const AdapterPtr &Adapter = DeviceImplVec[0]->getAdapter();
410+
std::vector<uint32_t> IPVersionVec;
411+
IPVersionVec.reserve(DeviceImplVec.size());
412+
413+
std::transform(DeviceImplVec.begin(), DeviceImplVec.end(),
414+
std::back_inserter(IPVersionVec),
415+
[&](const DeviceImplPtr &Impl) {
416+
uint32_t ipVersion = 0;
417+
ur_device_handle_t DeviceHandle = Impl->getHandleRef();
418+
Adapter->call<UrApiKind::urDeviceGetInfo>(
419+
DeviceHandle, UR_DEVICE_INFO_IP_VERSION,
420+
sizeof(uint32_t), &ipVersion, nullptr);
421+
return ipVersion;
422+
});
423+
424+
return detail::OpenCLC_Compilation_Available(IPVersionVec);
402425
}
403426

404427
// otherwise
405428
return false;
406429
}
407430

431+
bool is_source_kernel_bundle_supported(
432+
sycl::ext::oneapi::experimental::source_language Language,
433+
const context &Ctx) {
434+
const std::vector<sycl::device> Devices = Ctx.get_devices();
435+
std::vector<DeviceImplPtr> DeviceImplVec;
436+
DeviceImplVec.reserve(Devices.size());
437+
std::transform(Devices.begin(), Devices.end(),
438+
std::back_inserter(DeviceImplVec),
439+
[](const sycl::device &dev) {
440+
return sycl::detail::getSyclObjImpl(dev);
441+
});
442+
443+
return is_source_kernel_bundle_supported(Language, DeviceImplVec);
444+
}
445+
408446
/////////////////////////
409447
// syclex::detail::create_kernel_bundle_from_source
410448
/////////////////////////
@@ -428,8 +466,7 @@ make_kernel_bundle_from_source(const context &SyclContext,
428466
for (auto &p : IncludePairViews)
429467
IncludePairs.push_back({p.first.data(), p.second.data()});
430468

431-
backend BE = SyclContext.get_backend();
432-
if (!is_source_kernel_bundle_supported(BE, Language))
469+
if (!is_source_kernel_bundle_supported(Language, SyclContext))
433470
throw sycl::exception(make_error_code(errc::invalid),
434471
"kernel_bundle creation from source not supported");
435472

@@ -448,8 +485,7 @@ source_kb make_kernel_bundle_from_source(const context &SyclContext,
448485
const std::vector<std::byte> &Bytes,
449486
include_pairs_view_t IncludePairs) {
450487
(void)IncludePairs;
451-
backend BE = SyclContext.get_backend();
452-
if (!is_source_kernel_bundle_supported(BE, Language))
488+
if (!is_source_kernel_bundle_supported(Language, SyclContext))
453489
throw sycl::exception(make_error_code(errc::invalid),
454490
"kernel_bundle creation from source not supported");
455491

sycl/test/abi/sycl_symbols_linux.dump

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3105,7 +3105,6 @@ _ZN4sycl3_V13ext6oneapi12experimental6detail24modifiable_command_graphC2ERKNS0_5
31053105
_ZN4sycl3_V13ext6oneapi12experimental6detail24modifiable_command_graphC2ERKNS0_7contextERKNS0_6deviceERKNS0_13property_listE
31063106
_ZN4sycl3_V13ext6oneapi12experimental6detail30make_kernel_bundle_from_sourceERKNS0_7contextENS3_15source_languageENS0_6detail11string_viewESt6vectorISt4pairISA_SA_ESaISD_EE
31073107
_ZN4sycl3_V13ext6oneapi12experimental6detail30make_kernel_bundle_from_sourceERKNS0_7contextENS3_15source_languageERKSt6vectorISt4byteSaISA_EES9_ISt4pairINS0_6detail11string_viewESH_ESaISI_EE
3108-
_ZN4sycl3_V13ext6oneapi12experimental6detail33is_source_kernel_bundle_supportedENS0_7backendENS3_15source_languageE
31093108
_ZN4sycl3_V13ext6oneapi12experimental6memcpyENS0_5queueEPvPKvmRKNS0_6detail13code_locationE
31103109
_ZN4sycl3_V13ext6oneapi12experimental6memsetENS0_5queueEPvimRKNS0_6detail13code_locationE
31113110
_ZN4sycl3_V13ext6oneapi12experimental9image_memC1ERKNS3_16image_descriptorERKNS0_5queueE

sycl/test/abi/sycl_symbols_windows.dump

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4226,7 +4226,6 @@
42264226
?is_gpu@device@_V1@sycl@@QEBA_NXZ
42274227
?is_in_fusion_mode@fusion_wrapper@experimental@codeplay@ext@_V1@sycl@@QEBA_NXZ
42284228
?is_in_order@queue@_V1@sycl@@QEBA_NXZ
4229-
?is_source_kernel_bundle_supported@detail@experimental@oneapi@ext@_V1@sycl@@YA_NW4backend@56@W4source_language@23456@@Z
42304229
?is_specialization_constant_set@kernel_bundle_plain@detail@_V1@sycl@@IEBA_NPEBD@Z
42314230
?join_impl@detail@_V1@sycl@@YA?AV?$shared_ptr@Vkernel_bundle_impl@detail@_V1@sycl@@@std@@AEBV?$vector@V?$shared_ptr@Vkernel_bundle_impl@detail@_V1@sycl@@@std@@V?$allocator@V?$shared_ptr@Vkernel_bundle_impl@detail@_V1@sycl@@@std@@@2@@5@W4bundle_state@23@@Z
42324231
?khr_get_default_context@platform@_V1@sycl@@QEBA?AVcontext@23@XZ

0 commit comments

Comments
 (0)