Skip to content

Commit 7a259f0

Browse files
[SYCL] Refactor kernel name based cache approach
1 parent ea192fd commit 7a259f0

26 files changed

+334
-227
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//==--------------------- get_kernel_name_based_data.hpp -------------------==//
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+
#pragma once
9+
10+
#include <sycl/detail/export.hpp>
11+
#include <sycl/detail/kernel_name_str_t.hpp>
12+
13+
namespace sycl {
14+
inline namespace _V1 {
15+
namespace detail {
16+
17+
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
18+
class KernelNameBasedCacheT;
19+
__SYCL_EXPORT KernelNameBasedCacheT *createKernelNameBasedCache();
20+
#endif
21+
22+
class KernelNameBasedData;
23+
24+
__SYCL_EXPORT KernelNameBasedData *
25+
getKernelNameBasedDataImpl(detail::ABINeutralKernelNameStrRefT KernelName);
26+
27+
// Retrieves and caches a data pointer to avoid kernel name based lookup
28+
// overhead.
29+
template <typename KernelNameT>
30+
KernelNameBasedData *
31+
getKernelNameBasedData(detail::ABINeutralKernelNameStrRefT KernelName) {
32+
static KernelNameBasedData *Instance = getKernelNameBasedDataImpl(KernelName);
33+
return Instance;
34+
}
35+
36+
} // namespace detail
37+
} // namespace _V1
38+
} // namespace sycl

sycl/include/sycl/detail/kernel_name_based_cache.hpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

sycl/include/sycl/detail/kernel_name_str_t.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ namespace detail {
1818
using KernelNameStrT = std::string_view;
1919
using KernelNameStrRefT = std::string_view;
2020
using ABINeutralKernelNameStrT = detail::string_view;
21+
using ABINeutralKernelNameStrRefT = detail::string_view;
2122
#else
2223
using KernelNameStrT = std::string;
2324
using KernelNameStrRefT = const std::string &;
2425
using ABINeutralKernelNameStrT = detail::string;
26+
using ABINeutralKernelNameStrRefT = const detail::string &;
2527
#endif
2628

2729
inline KernelNameStrT toKernelNameStrT(const ABINeutralKernelNameStrT &str) {

sycl/include/sycl/handler.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
#include <sycl/detail/common.hpp>
1515
#include <sycl/detail/defines_elementary.hpp>
1616
#include <sycl/detail/export.hpp>
17+
#include <sycl/detail/get_kernel_name_based_data.hpp>
1718
#include <sycl/detail/id_queries_fit_in_int.hpp>
1819
#include <sycl/detail/impl_utils.hpp>
1920
#include <sycl/detail/kernel_desc.hpp>
2021
#include <sycl/detail/kernel_launch_helper.hpp>
21-
#include <sycl/detail/kernel_name_based_cache.hpp>
2222
#include <sycl/detail/kernel_name_str_t.hpp>
2323
#include <sycl/detail/reduction_forward.hpp>
2424
#include <sycl/detail/string.hpp>
@@ -863,14 +863,15 @@ class __SYCL_EXPORT handler {
863863
constexpr std::string_view KernelNameStr =
864864
detail::getKernelName<KernelName>();
865865
MKernelName = KernelNameStr;
866+
setKernelNameBasedDataPtr(
867+
detail::getKernelNameBasedData<KernelName>(KernelNameStr));
866868
} else {
867869
// In case w/o the integration header it is necessary to process
868870
// accessors from the list(which are associated with this handler) as
869871
// arguments. We must copy the associated accessors as they are checked
870872
// later during finalize.
871873
setArgsToAssociatedAccessors();
872874
}
873-
setKernelNameBasedCachePtr(detail::getKernelNameBasedCache<KernelName>());
874875

875876
// If the kernel lambda is callable with a kernel_handler argument, manifest
876877
// the associated kernel handler.
@@ -3685,8 +3686,12 @@ class __SYCL_EXPORT handler {
36853686
sycl::handler &h, size_t size,
36863687
const ext::oneapi::experimental::memory_pool &pool);
36873688

3689+
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
36883690
void setKernelNameBasedCachePtr(
36893691
detail::KernelNameBasedCacheT *KernelNameBasedCachePtr);
3692+
#endif
3693+
void setKernelNameBasedDataPtr(
3694+
detail::KernelNameBasedData *KernelNameBasedDataPtr);
36903695

36913696
queue getQueue();
36923697

sycl/source/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ set(SYCL_COMMON_SOURCES
288288
"detail/kernel_compiler/kernel_compiler_opencl.cpp"
289289
"detail/kernel_compiler/kernel_compiler_sycl.cpp"
290290
"detail/kernel_impl.cpp"
291-
"detail/kernel_name_based_cache.cpp"
291+
"detail/get_kernel_name_based_data.cpp"
292+
"detail/kernel_name_based_data.cpp"
292293
"detail/kernel_program_cache.cpp"
293294
"detail/memory_export.cpp"
294295
"detail/memory_manager.cpp"

sycl/source/detail/cg.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class CGExecKernel : public CG {
254254
std::shared_ptr<detail::kernel_bundle_impl> MKernelBundle;
255255
std::vector<ArgDesc> MArgs;
256256
KernelNameStrT MKernelName;
257-
KernelNameBasedCacheT *MKernelNameBasedCachePtr;
257+
KernelNameBasedData *MKernelNameBasedDataPtr;
258258
std::vector<std::shared_ptr<detail::stream_impl>> MStreams;
259259
std::vector<std::shared_ptr<const void>> MAuxiliaryResources;
260260
/// Used to implement ext_oneapi_graph dynamic_command_group. Stores the list
@@ -270,7 +270,7 @@ class CGExecKernel : public CG {
270270
std::shared_ptr<detail::kernel_bundle_impl> KernelBundle,
271271
CG::StorageInitHelper CGData, std::vector<ArgDesc> Args,
272272
KernelNameStrT KernelName,
273-
KernelNameBasedCacheT *KernelNameBasedCachePtr,
273+
KernelNameBasedData *KernelNameBasedDataPtr,
274274
std::vector<std::shared_ptr<detail::stream_impl>> Streams,
275275
std::vector<std::shared_ptr<const void>> AuxiliaryResources,
276276
CGType Type, ur_kernel_cache_config_t KernelCacheConfig,
@@ -280,7 +280,7 @@ class CGExecKernel : public CG {
280280
MHostKernel(std::move(HKernel)), MSyclKernel(std::move(SyclKernel)),
281281
MKernelBundle(std::move(KernelBundle)), MArgs(std::move(Args)),
282282
MKernelName(std::move(KernelName)),
283-
MKernelNameBasedCachePtr(KernelNameBasedCachePtr),
283+
MKernelNameBasedDataPtr(KernelNameBasedDataPtr),
284284
MStreams(std::move(Streams)),
285285
MAuxiliaryResources(std::move(AuxiliaryResources)),
286286
MAlternativeKernels{}, MKernelCacheConfig(std::move(KernelCacheConfig)),

sycl/source/detail/kernel_name_based_cache.cpp renamed to sycl/source/detail/get_kernel_name_based_data.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//==--------------------- kernel_name_based_cache.cpp ----------------------==//
1+
//==-------------------- get_kernel_name_based_data.cpp --------------------==//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -7,15 +7,24 @@
77
//===----------------------------------------------------------------------===//
88

99
#include <detail/global_handler.hpp>
10-
#include <sycl/detail/kernel_name_based_cache.hpp>
10+
#include <detail/program_manager/program_manager.hpp>
11+
#include <sycl/detail/get_kernel_name_based_data.hpp>
1112

1213
namespace sycl {
1314
inline namespace _V1 {
1415
namespace detail {
1516

17+
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
1618
KernelNameBasedCacheT *createKernelNameBasedCache() {
1719
return GlobalHandler::instance().createKernelNameBasedCache();
1820
}
21+
#endif
22+
23+
KernelNameBasedData *
24+
getKernelNameBasedDataImpl(detail::ABINeutralKernelNameStrRefT KernelName) {
25+
return ProgramManager::getInstance().getOrCreateKernelNameBasedData(
26+
KernelName.data());
27+
}
1928

2029
} // namespace detail
2130
} // namespace _V1

sycl/source/detail/global_handler.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <detail/adapter_impl.hpp>
1515
#include <detail/config.hpp>
1616
#include <detail/global_handler.hpp>
17-
#include <detail/kernel_name_based_cache_t.hpp>
17+
#include <detail/kernel_name_based_data.hpp>
1818
#include <detail/platform_impl.hpp>
1919
#include <detail/program_manager/program_manager.hpp>
2020
#include <detail/scheduler/scheduler.hpp>
@@ -249,12 +249,15 @@ ThreadPool &GlobalHandler::getHostTaskThreadPool() {
249249
return TP;
250250
}
251251

252+
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
252253
KernelNameBasedCacheT *GlobalHandler::createKernelNameBasedCache() {
253-
static std::deque<KernelNameBasedCacheT> &KernelNameBasedCaches =
254-
getOrCreate(MKernelNameBasedCaches);
255-
LockGuard LG{MKernelNameBasedCaches.Lock};
256-
return &KernelNameBasedCaches.emplace_back();
254+
static std::deque<KernelNameBasedData> &KernelNameBasedDataStorage =
255+
getOrCreate(MKernelNameBasedDataStorage);
256+
LockGuard LG{MKernelNameBasedDataStorage.Lock};
257+
return reinterpret_cast<KernelNameBasedCacheT *>(
258+
&KernelNameBasedDataStorage.emplace_back());
257259
}
260+
#endif
258261

259262
void GlobalHandler::releaseDefaultContexts() {
260263
// Release shared-pointers to SYCL objects.
@@ -390,9 +393,11 @@ void shutdown_late() {
390393
Handler->MScheduler.Inst.reset(nullptr);
391394
Handler->MProgramManager.Inst.reset(nullptr);
392395

396+
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
393397
// Cache stores handles to the adapter, so clear it before
394398
// releasing adapters.
395-
Handler->MKernelNameBasedCaches.Inst.reset(nullptr);
399+
Handler->MKernelNameBasedDataStorage.Inst.reset(nullptr);
400+
#endif
396401

397402
// Clear the adapters and reset the instance if it was there.
398403
Handler->unloadAdapters();

sycl/source/detail/global_handler.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
#include <sycl/detail/spinlock.hpp>
1212
#include <sycl/detail/util.hpp>
1313

14+
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
1415
#include <deque>
16+
#endif
1517
#include <memory>
1618
#include <unordered_map>
1719

@@ -27,7 +29,10 @@ class adapter_impl;
2729
class ods_target_list;
2830
class XPTIRegistry;
2931
class ThreadPool;
30-
struct KernelNameBasedCacheT;
32+
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
33+
class KernelNameBasedCacheT;
34+
class KernelNameBasedData;
35+
#endif
3136

3237
/// Wrapper class for global data structures with non-trivial destructors.
3338
///
@@ -73,7 +78,9 @@ class GlobalHandler {
7378
ods_target_list &getOneapiDeviceSelectorTargets(const std::string &InitValue);
7479
XPTIRegistry &getXPTIRegistry();
7580
ThreadPool &getHostTaskThreadPool();
81+
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
7682
KernelNameBasedCacheT *createKernelNameBasedCache();
83+
#endif
7784
static void registerStaticVarShutdownHandler();
7885

7986
bool isOkToDefer() const;
@@ -130,7 +137,9 @@ class GlobalHandler {
130137
InstWithLock<XPTIRegistry> MXPTIRegistry;
131138
// Thread pool for host task and event callbacks execution
132139
InstWithLock<ThreadPool> MHostTaskThreadPool;
133-
InstWithLock<std::deque<KernelNameBasedCacheT>> MKernelNameBasedCaches;
140+
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
141+
InstWithLock<std::deque<KernelNameBasedData>> MKernelNameBasedDataStorage;
142+
#endif
134143
};
135144
} // namespace detail
136145
} // namespace _V1

sycl/source/detail/graph/graph_impl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ ur_exp_command_buffer_sync_point_t exec_graph_impl::enqueueNodeDirect(
741741
CGExec->MLine, CGExec->MColumn);
742742
std::tie(CmdTraceEvent, InstanceID) = emitKernelInstrumentationData(
743743
StreamID, CGExec->MSyclKernel, CodeLoc, CGExec->MIsTopCodeLoc,
744-
CGExec->MKernelName.data(), CGExec->MKernelNameBasedCachePtr, nullptr,
744+
CGExec->MKernelName.data(), CGExec->MKernelNameBasedDataPtr, nullptr,
745745
CGExec->MNDRDesc, CGExec->MKernelBundle.get(), CGExec->MArgs);
746746
if (CmdTraceEvent)
747747
sycl::detail::emitInstrumentationGeneral(
@@ -1573,9 +1573,10 @@ void exec_graph_impl::populateURKernelUpdateStructs(
15731573
UrKernel = SyclKernelImpl->getHandleRef();
15741574
EliminatedArgMask = SyclKernelImpl->getKernelArgMask();
15751575
} else {
1576+
assert(ExecCG.MKernelNameBasedDataPtr);
15761577
BundleObjs = sycl::detail::ProgramManager::getInstance().getOrCreateKernel(
15771578
ContextImpl, DeviceImpl, ExecCG.MKernelName,
1578-
ExecCG.MKernelNameBasedCachePtr);
1579+
*ExecCG.MKernelNameBasedDataPtr);
15791580
UrKernel = BundleObjs->MKernelHandle;
15801581
EliminatedArgMask = BundleObjs->MKernelArgMask;
15811582
}

0 commit comments

Comments
 (0)