Skip to content

Commit b0304e1

Browse files
[NFC][SYCL] Minor refactoring in source/detail/kernel_program_cache.hpp (#19555)
* Inline type aliases that don't bring much value * Init `MParentContext` in ctor, change to be a reference
1 parent 1eed77b commit b0304e1

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

sycl/source/detail/context_impl.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ context_impl::context_impl(const std::vector<sycl::device> Devices,
3535
: MOwnedByRuntime(true), MAsyncHandler(std::move(AsyncHandler)),
3636
MDevices(std::move(Devices)), MContext(nullptr),
3737
MPlatform(detail::getSyclObjImpl(MDevices[0].get_platform())),
38-
MPropList(PropList), MSupportBufferLocationByDevices(NotChecked) {
38+
MPropList(PropList), MKernelProgramCache(*this),
39+
MSupportBufferLocationByDevices(NotChecked) {
3940
verifyProps(PropList);
4041
std::vector<ur_device_handle_t> DeviceIds;
4142
for (const auto &D : MDevices) {
@@ -56,8 +57,6 @@ context_impl::context_impl(const std::vector<sycl::device> Devices,
5657

5758
getAdapter().call<UrApiKind::urContextCreate>(
5859
DeviceIds.size(), DeviceIds.data(), nullptr, &MContext);
59-
60-
MKernelProgramCache.setContextPtr(this);
6160
}
6261

6362
context_impl::context_impl(ur_context_handle_t UrContext,
@@ -66,7 +65,7 @@ context_impl::context_impl(ur_context_handle_t UrContext,
6665
bool OwnedByRuntime, private_tag)
6766
: MOwnedByRuntime(OwnedByRuntime), MAsyncHandler(std::move(AsyncHandler)),
6867
MDevices(DeviceList), MContext(UrContext), MPlatform(),
69-
MSupportBufferLocationByDevices(NotChecked) {
68+
MKernelProgramCache(*this), MSupportBufferLocationByDevices(NotChecked) {
7069
if (!MDevices.empty()) {
7170
MPlatform = detail::getSyclObjImpl(MDevices[0].get_platform());
7271
} else {
@@ -104,7 +103,6 @@ context_impl::context_impl(ur_context_handle_t UrContext,
104103
if (getBackend() == sycl::backend::opencl) {
105104
getAdapter().call<UrApiKind::urContextRetain>(MContext);
106105
}
107-
MKernelProgramCache.setContextPtr(this);
108106
}
109107

110108
cl_context context_impl::get() const {
@@ -501,7 +499,7 @@ std::optional<ur_program_handle_t> context_impl::getProgramForDevImgs(
501499
const device &Device, const std::set<std::uintptr_t> &ImgIdentifiers,
502500
const std::string &ObjectTypeName) {
503501

504-
KernelProgramCache::ProgramBuildResultPtr BuildRes = nullptr;
502+
std::shared_ptr<KernelProgramCache::ProgramBuildResult> BuildRes = nullptr;
505503
{
506504
auto LockedCache = MKernelProgramCache.acquireCachedPrograms();
507505
auto &KeyMap = LockedCache.get().KeyMap;

sycl/source/detail/kernel_program_cache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ namespace sycl {
1313
inline namespace _V1 {
1414
namespace detail {
1515
adapter_impl &KernelProgramCache::getAdapter() {
16-
return MParentContext->getAdapter();
16+
return MParentContext.getAdapter();
1717
}
1818

1919
ur_context_handle_t KernelProgramCache::getURContext() const {
20-
return MParentContext->getHandleRef();
20+
return MParentContext.getHandleRef();
2121
}
2222

2323
} // namespace detail

sycl/source/detail/kernel_program_cache.hpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ class KernelProgramCache {
134134
#pragma warning(pop)
135135
#endif
136136
};
137-
using ProgramBuildResultPtr = std::shared_ptr<ProgramBuildResult>;
138137

139138
/* Drop LinkOptions and CompileOptions from CacheKey since they are only used
140139
* when debugging environment variables are set and we can just ignore them
@@ -179,7 +178,8 @@ class KernelProgramCache {
179178
};
180179

181180
struct ProgramCache {
182-
emhash8::HashMap<ProgramCacheKeyT, ProgramBuildResultPtr> Cache;
181+
emhash8::HashMap<ProgramCacheKeyT, std::shared_ptr<ProgramBuildResult>>
182+
Cache;
183183
UnorderedMultimap<CommonProgramKeyT, ProgramCacheKeyT> KeyMap;
184184
// Mapping between a UR program and its size.
185185
std::unordered_map<ur_program_handle_t, size_t> ProgramSizeMap;
@@ -193,11 +193,9 @@ class KernelProgramCache {
193193
size_t size() const noexcept { return Cache.size(); }
194194
};
195195

196-
using ContextPtr = context_impl *;
197-
198-
using KernelArgMaskPairT =
199-
std::pair<ur_kernel_handle_t, const KernelArgMask *>;
200-
struct KernelBuildResult : public BuildResult<KernelArgMaskPairT> {
196+
struct KernelBuildResult
197+
: public BuildResult<
198+
std::pair<ur_kernel_handle_t, const KernelArgMask *>> {
201199
const adapter_impl &MAdapter;
202200
KernelBuildResult(const adapter_impl &Adapter) : MAdapter(Adapter) {
203201
Val.first = nullptr;
@@ -214,10 +212,10 @@ class KernelProgramCache {
214212
}
215213
}
216214
};
217-
using KernelBuildResultPtr = std::shared_ptr<KernelBuildResult>;
218215

219-
using KernelByNameT = emhash8::HashMap<KernelNameStrT, KernelBuildResultPtr>;
220-
using KernelCacheT = emhash8::HashMap<ur_program_handle_t, KernelByNameT>;
216+
using KernelCacheT = emhash8::HashMap<
217+
ur_program_handle_t,
218+
emhash8::HashMap<KernelNameStrT, std::shared_ptr<KernelBuildResult>>>;
221219

222220
class FastKernelSubcacheWrapper {
223221
public:
@@ -339,8 +337,7 @@ class KernelProgramCache {
339337
};
340338

341339
~KernelProgramCache() = default;
342-
343-
void setContextPtr(const ContextPtr &AContext) { MParentContext = AContext; }
340+
KernelProgramCache(context_impl &Ctx) : MParentContext(Ctx) {}
344341

345342
// Sends message to std:cerr stream when SYCL_CACHE_TRACE environemnt is
346343
// set.
@@ -404,7 +401,7 @@ class KernelProgramCache {
404401
return {MEvictionList, MProgramEvictionListMutex};
405402
}
406403

407-
std::pair<ProgramBuildResultPtr, bool>
404+
std::pair<std::shared_ptr<ProgramBuildResult>, bool>
408405
getOrInsertProgram(const ProgramCacheKeyT &CacheKey) {
409406
auto LockedCache = acquireCachedPrograms();
410407
auto &ProgCache = LockedCache.get();
@@ -444,7 +441,7 @@ class KernelProgramCache {
444441
return DidInsert;
445442
}
446443

447-
std::pair<KernelBuildResultPtr, bool>
444+
std::pair<std::shared_ptr<KernelBuildResult>, bool>
448445
getOrInsertKernel(ur_program_handle_t Program, KernelNameStrRefT KernelName) {
449446
auto LockedCache = acquireKernelsPerProgramCache();
450447
auto &Cache = LockedCache.get()[Program];
@@ -850,7 +847,7 @@ class KernelProgramCache {
850847

851848
ProgramCache MCachedPrograms;
852849
KernelCacheT MKernelsPerProgramCache;
853-
ContextPtr MParentContext;
850+
context_impl &MParentContext;
854851

855852
using FastKernelCacheMutexT = SpinLock;
856853
using FastKernelCacheReadLockT = std::lock_guard<FastKernelCacheMutexT>;

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,6 @@ FastKernelCacheValPtr ProgramManager::getOrCreateKernel(
11041104
<< ", " << &DeviceImpl << ", " << KernelName << ")\n";
11051105
}
11061106

1107-
using KernelArgMaskPairT = KernelProgramCache::KernelArgMaskPairT;
1108-
11091107
KernelProgramCache &Cache = ContextImpl.getKernelProgramCache();
11101108
ur_device_handle_t UrDevice = DeviceImpl.getHandleRef();
11111109
FastKernelSubcacheT *CacheHintPtr =
@@ -1160,7 +1158,8 @@ FastKernelCacheValPtr ProgramManager::getOrCreateKernel(
11601158
auto BuildResult = Cache.getOrBuild<errc::invalid>(GetCachedBuildF, BuildF);
11611159
// getOrBuild is not supposed to return nullptr
11621160
assert(BuildResult != nullptr && "Invalid build result");
1163-
const KernelArgMaskPairT &KernelArgMaskPair = BuildResult->Val;
1161+
const std::pair<ur_kernel_handle_t, const KernelArgMask *>
1162+
&KernelArgMaskPair = BuildResult->Val;
11641163
auto ret_val = std::make_shared<FastKernelCacheVal>(
11651164
KernelArgMaskPair.first, &(BuildResult->MBuildResultMutex),
11661165
KernelArgMaskPair.second, Program, ContextImpl.getAdapter());

0 commit comments

Comments
 (0)