Skip to content

Commit d382787

Browse files
committed
Add OSModule handle
1 parent a6b25dd commit d382787

12 files changed

+137
-144
lines changed

sycl/include/CL/sycl/handler.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,7 @@ class __SYCL_EXPORT handler {
314314
///
315315
/// \param Queue is a SYCL queue.
316316
/// \param IsHost indicates if this handler is created for SYCL host device.
317-
handler(shared_ptr_class<detail::queue_impl> Queue, bool IsHost)
318-
: MQueue(std::move(Queue)), MIsHost(IsHost) {
319-
MSharedPtrStorage.emplace_back(
320-
std::make_shared<std::vector<detail::ExtendedMemberT>>());
321-
}
317+
handler(const shared_ptr_class<detail::queue_impl> &Queue, bool IsHost);
322318

323319
/// Stores copy of Arg passed to the MArgsStorage.
324320
template <typename T, typename F = typename detail::remove_const_t<

sycl/include/CL/sycl/kernel_bundle.hpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ namespace detail {
321321
// public onces
322322
__SYCL_EXPORT detail::KernelBundleImplPtr
323323
get_kernel_bundle_impl(const context &Ctx, const std::vector<device> &Devs,
324-
bundle_state State);
324+
bundle_state State, OSModuleHandle M);
325325
} // namespace detail
326326

327327
/// A kernel bundle in state State which contains all of the kernels in the
@@ -331,8 +331,10 @@ get_kernel_bundle_impl(const context &Ctx, const std::vector<device> &Devs,
331331
template <bundle_state State>
332332
kernel_bundle<State> get_kernel_bundle(const context &Ctx,
333333
const std::vector<device> &Devs) {
334-
detail::KernelBundleImplPtr Impl =
335-
detail::get_kernel_bundle_impl(Ctx, Devs, State);
334+
static const int Anchor = 0;
335+
detail::KernelBundleImplPtr Impl = detail::get_kernel_bundle_impl(
336+
Ctx, Devs, State, detail::OSUtil::getOSModuleHandle(&Anchor));
337+
336338
return detail::createSyclObjFromImpl<kernel_bundle<State>>(Impl);
337339
}
338340

@@ -348,7 +350,7 @@ namespace detail {
348350
__SYCL_EXPORT detail::KernelBundleImplPtr
349351
get_kernel_bundle_impl(const context &Ctx, const std::vector<device> &Devs,
350352
const std::vector<kernel_id> &KernelIDs,
351-
bundle_state State);
353+
bundle_state State, OSModuleHandle M);
352354
} // namespace detail
353355

354356
/// \returns a kernel bundle in state State which contains all of the device
@@ -363,8 +365,9 @@ template <bundle_state State>
363365
kernel_bundle<State>
364366
get_kernel_bundle(const context &Ctx, const std::vector<device> &Devs,
365367
const std::vector<kernel_id> &KernelIDs) {
366-
detail::KernelBundleImplPtr Impl =
367-
detail::get_kernel_bundle_impl(Ctx, Devs, KernelIDs, State);
368+
static const int Anchor = 0;
369+
detail::KernelBundleImplPtr Impl = detail::get_kernel_bundle_impl(
370+
Ctx, Devs, KernelIDs, State, detail::OSUtil::getOSModuleHandle(&Anchor));
368371
return detail::createSyclObjFromImpl<kernel_bundle<State>>(Impl);
369372
}
370373

@@ -396,7 +399,8 @@ using DevImgSelectorImpl =
396399
// public onces
397400
__SYCL_EXPORT detail::KernelBundleImplPtr
398401
get_kernel_bundle_impl(const context &Ctx, const std::vector<device> &Devs,
399-
bundle_state State, const DevImgSelectorImpl &Selector);
402+
bundle_state State, const DevImgSelectorImpl &Selector,
403+
OSModuleHandle M);
400404
} // namespace detail
401405

402406
/// A kernel bundle in state State which contains all of the device images for
@@ -411,9 +415,11 @@ kernel_bundle<State> get_kernel_bundle(const context &Ctx,
411415
detail::createSyclObjFromImpl<sycl::device_image<State>>(DevImg));
412416
};
413417

414-
std::vector<kernel_id> EmptyKernelIDs;
415-
detail::KernelBundleImplPtr Impl =
416-
detail::get_kernel_bundle_impl(Ctx, Devs, State, SelectorWrapper);
418+
static const int Anchor = 0;
419+
420+
detail::KernelBundleImplPtr Impl = detail::get_kernel_bundle_impl(
421+
Ctx, Devs, State, SelectorWrapper,
422+
detail::OSUtil::getOSModuleHandle(&Anchor));
417423

418424
return detail::createSyclObjFromImpl<sycl::kernel_bundle<State>>(Impl);
419425
}
@@ -431,12 +437,12 @@ namespace detail {
431437

432438
__SYCL_EXPORT bool has_kernel_bundle_impl(const context &Ctx,
433439
const std::vector<device> &Devs,
434-
bundle_state State);
440+
bundle_state State, OSModuleHandle M);
435441

436442
__SYCL_EXPORT bool
437443
has_kernel_bundle_impl(const context &Ctx, const std::vector<device> &Devs,
438444
const std::vector<kernel_id> &kernelIds,
439-
bundle_state State);
445+
bundle_state State, OSModuleHandle M);
440446
} // namespace detail
441447

442448
/// \returns true if the following is true:
@@ -451,13 +457,18 @@ has_kernel_bundle_impl(const context &Ctx, const std::vector<device> &Devs,
451457
/// aspect::online_linker.
452458
template <bundle_state State>
453459
bool has_kernel_bundle(const context &Ctx, const std::vector<device> &Devs) {
454-
return detail::has_kernel_bundle_impl(Ctx, Devs, State);
460+
461+
static const int Anchor = 0;
462+
return detail::has_kernel_bundle_impl(
463+
Ctx, Devs, State, detail::OSUtil::getOSModuleHandle(&Anchor));
455464
}
456465

457466
template <bundle_state State>
458467
bool has_kernel_bundle(const context &Ctx, const std::vector<device> &Devs,
459468
const std::vector<kernel_id> &KernelIDs) {
460-
return detail::has_kernel_bundle_impl(Ctx, Devs, KernelIDs, State);
469+
static const int Anchor = 0;
470+
return detail::has_kernel_bundle_impl(
471+
Ctx, Devs, KernelIDs, State, detail::OSUtil::getOSModuleHandle(&Anchor));
461472
}
462473

463474
template <bundle_state State> bool has_kernel_bundle(const context &Ctx) {

sycl/source/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#cmake_policy(SET CMP0057 NEW)
55
#include(AddLLVM)
66

7-
add_definitions(-g -O0)
87
configure_file(
98
${CMAKE_CURRENT_SOURCE_DIR}/version.rc.in
109
${CMAKE_CURRENT_BINARY_DIR}/version.rc

sycl/source/detail/device_image_impl.hpp

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <CL/sycl/detail/pi.h>
1414
#include <CL/sycl/device.hpp>
1515
#include <CL/sycl/kernel_bundle.hpp>
16+
#include <detail/context_impl.hpp>
1617
#include <detail/device_impl.hpp>
1718
#include <detail/kernel_id_impl.hpp>
1819
#include <detail/program_manager/program_manager.hpp>
@@ -28,46 +29,18 @@ __SYCL_INLINE_NAMESPACE(cl) {
2829
namespace sycl {
2930
namespace detail {
3031

31-
// Used for sorting vector of kernel_id's
32-
struct LessByNameComp {
33-
bool operator()(const sycl::kernel_id &LHS, const sycl::kernel_id &RHS) {
34-
return std::strcmp(LHS.get_name(), RHS.get_name()) < 0;
35-
}
36-
};
37-
3832
// The class is impl counterpart for sycl::device_image
3933
// It can represent a program in different states, kernel_id's it has and state
4034
// of specialization constants for it
4135
class device_image_impl {
4236
public:
43-
device_image_impl(context Context, std::vector<device> Devices,
44-
bundle_state State)
45-
: MBinImage(nullptr), MContext(std::move(Context)),
46-
MDevices(std::move(Devices)), MState(State) {}
4737

4838
device_image_impl(RTDeviceBinaryImage *BinImage, context Context,
49-
std::vector<device> Devices, bundle_state State)
50-
: MBinImage(BinImage), MContext(std::move(Context)),
51-
MDevices(std::move(Devices)), MState(State) {
52-
53-
// Collect kernel names for the image
54-
pi_device_binary DevBin =
55-
const_cast<pi_device_binary>(&MBinImage->getRawData());
56-
for (_pi_offload_entry EntriesIt = DevBin->EntriesBegin;
57-
EntriesIt != DevBin->EntriesEnd; ++EntriesIt) {
58-
59-
std::shared_ptr<detail::kernel_id_impl> KernleIDImpl =
60-
std::make_shared<detail::kernel_id_impl>(EntriesIt->name);
61-
62-
sycl::kernel_id KernelID =
63-
detail::createSyclObjFromImpl<sycl::kernel_id>(KernleIDImpl);
64-
65-
// Insert new element keeping MKernelIDs sorted.
66-
auto It = std::lower_bound(MKernelIDs.begin(), MKernelIDs.end(), KernelID,
67-
LessByNameComp{});
68-
MKernelIDs.insert(It, std::move(KernelID));
69-
}
70-
}
39+
std::vector<device> Devices, bundle_state State,
40+
std::vector<kernel_id> KernelIDs, OSModuleHandle M)
41+
: MBinImage(BinImage), MOSModuleHandle(std::move(M)),
42+
MContext(std::move(Context)), MDevices(std::move(Devices)),
43+
MState(State), MKernelIDs(std::move(KernelIDs)) {}
7144

7245
bool has_kernel(const kernel_id &KernelIDCand) const noexcept {
7346
return std::binary_search(MKernelIDs.begin(), MKernelIDs.end(),
@@ -171,10 +144,18 @@ class device_image_impl {
171144
return MSpecConstDescs;
172145
}
173146

174-
OSModuleHandle get_OS_module_handle() const noexcept {
147+
OSModuleHandle &get_OS_module_handle_ref() noexcept {
175148
return MOSModuleHandle;
176149
}
177150

151+
~device_image_impl() {
152+
153+
if (MProgram) {
154+
const detail::plugin &Plugin = getSyclObjImpl(MContext)->getPlugin();
155+
Plugin.call<PiApiKind::piProgramRelease>(MProgram);
156+
}
157+
}
158+
178159
private:
179160
RTDeviceBinaryImage *MBinImage = nullptr;
180161
OSModuleHandle MOSModuleHandle = 0;

sycl/source/detail/global_handler.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ GlobalHandler::getDeviceFilterList(const std::string &InitValue) {
114114
return *MDeviceFilterList;
115115
}
116116

117+
std::mutex &GlobalHandler::getHandlerExtendedMembersMutex() {
118+
if (MHandlerExtendedMembersMutex)
119+
return *MHandlerExtendedMembersMutex;
120+
121+
const std::lock_guard<SpinLock> Lock{MFieldsLock};
122+
if (!MHandlerExtendedMembersMutex)
123+
MHandlerExtendedMembersMutex = std::make_unique<std::mutex>();
124+
125+
return *MHandlerExtendedMembersMutex;
126+
}
127+
117128
void shutdown() {
118129
// First, release resources, that may access plugins.
119130
GlobalHandler::instance().MScheduler.reset(nullptr);

sycl/source/detail/global_handler.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class GlobalHandler {
5656
std::mutex &getFilterMutex();
5757
std::vector<plugin> &getPlugins();
5858
device_filter_list &getDeviceFilterList(const std::string &InitValue);
59+
std::mutex &getHandlerExtendedMembersMutex();
5960

6061
private:
6162
friend void shutdown();
@@ -76,6 +77,7 @@ class GlobalHandler {
7677
std::unique_ptr<std::mutex> MFilterMutex;
7778
std::unique_ptr<std::vector<plugin>> MPlugins;
7879
std::unique_ptr<device_filter_list> MDeviceFilterList;
80+
std::unique_ptr<std::mutex> MHandlerExtendedMembersMutex;
7981
};
8082
} // namespace detail
8183
} // namespace sycl

sycl/source/detail/kernel_bundle_impl.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ template <class T> struct LessByHash {
3939
class kernel_bundle_impl {
4040

4141
public:
42-
kernel_bundle_impl(context Ctx, std::vector<device> Devs, bundle_state State)
42+
kernel_bundle_impl(context Ctx, std::vector<device> Devs, bundle_state State,
43+
OSModuleHandle &M)
4344
: MContext(std::move(Ctx)), MDevices(std::move(Devs)) {
4445

4546
MDeviceImages = detail::ProgramManager::getInstance().getSYCLDeviceImages(
46-
MContext, MDevices, State);
47+
MContext, MDevices, State, M);
4748
}
4849

4950
// Matches sycl::build
@@ -110,8 +111,8 @@ class kernel_bundle_impl {
110111

111112
kernel_bundle_impl(const context &Ctx, const std::vector<device> &Devs,
112113
const std::vector<kernel_id> &KernelIDs,
113-
bundle_state State)
114-
: kernel_bundle_impl(Ctx, Devs, State) {
114+
bundle_state State, OSModuleHandle &M)
115+
: kernel_bundle_impl(Ctx, Devs, State, M) {
115116

116117
// Filter out images that have no kernel_ids specified
117118
auto It = std::remove_if(MDeviceImages.begin(), MDeviceImages.end(),
@@ -126,8 +127,9 @@ class kernel_bundle_impl {
126127
}
127128

128129
kernel_bundle_impl(const context &Ctx, const std::vector<device> &Devs,
129-
const DevImgSelectorImpl &Selector, bundle_state State)
130-
: kernel_bundle_impl(Ctx, Devs, State) {
130+
const DevImgSelectorImpl &Selector, bundle_state State,
131+
OSModuleHandle &M)
132+
: kernel_bundle_impl(Ctx, Devs, State, M) {
131133

132134
// Filter out images that are rejected by Selector
133135
auto It = std::remove_if(MDeviceImages.begin(), MDeviceImages.end(),

sycl/source/detail/kernel_id_impl.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ __SYCL_INLINE_NAMESPACE(cl) {
1212
namespace sycl {
1313
namespace detail {
1414

15+
// Used for sorting vector of kernel_id's
16+
struct LessByNameComp {
17+
bool operator()(const sycl::kernel_id &LHS, const sycl::kernel_id &RHS) {
18+
return std::strcmp(LHS.get_name(), RHS.get_name()) < 0;
19+
}
20+
};
21+
1522
// The class is impl counterpart for sycl::kernel_id which represent a kernel
1623
// identificator
1724
class kernel_id_impl {

0 commit comments

Comments
 (0)