Skip to content

Commit 0dc91aa

Browse files
[NFCI][SYCL] Switch to using Managed<ur_kernel_handle_t> (#19570)
Similar to what's been done with `ur_program_handle_t` in #19536 #19557
1 parent dc0e953 commit 0dc91aa

File tree

12 files changed

+92
-91
lines changed

12 files changed

+92
-91
lines changed

sycl/source/backend.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ kernel make_kernel(const context &TargetContext,
321321
const kernel_bundle<bundle_state::executable> &KernelBundle,
322322
ur_native_handle_t NativeHandle, bool KeepOwnership,
323323
backend Backend) {
324-
const auto &Adapter = getAdapter(Backend);
324+
adapter_impl &Adapter = getAdapter(Backend);
325325
context_impl &ContextImpl = *getSyclObjImpl(TargetContext);
326326
kernel_bundle_impl &KernelBundleImpl = *getSyclObjImpl(KernelBundle);
327327

@@ -347,7 +347,7 @@ kernel make_kernel(const context &TargetContext,
347347
}
348348

349349
// Create UR kernel first.
350-
ur_kernel_handle_t UrKernel = nullptr;
350+
Managed<ur_kernel_handle_t> UrKernel{Adapter};
351351
ur_kernel_native_properties_t Properties{};
352352
Properties.stype = UR_STRUCTURE_TYPE_KERNEL_NATIVE_PROPERTIES;
353353
Properties.isNativeHandleOwned = !KeepOwnership;
@@ -359,8 +359,8 @@ kernel make_kernel(const context &TargetContext,
359359
__SYCL_OCL_CALL(clRetainKernel, ur::cast<cl_kernel>(NativeHandle));
360360

361361
// Construct the SYCL queue from UR queue.
362-
return detail::createSyclObjFromImpl<kernel>(
363-
std::make_shared<kernel_impl>(UrKernel, ContextImpl, &KernelBundleImpl));
362+
return detail::createSyclObjFromImpl<kernel>(std::make_shared<kernel_impl>(
363+
std::move(UrKernel), ContextImpl, &KernelBundleImpl));
364364
}
365365

366366
kernel make_kernel(ur_native_handle_t NativeHandle,

sycl/source/detail/adapter_impl.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,14 @@ template <typename URResource> class Managed {
243243
static constexpr auto Release = []() constexpr {
244244
if constexpr (std::is_same_v<URResource, ur_program_handle_t>)
245245
return UrApiKind::urProgramRelease;
246+
if constexpr (std::is_same_v<URResource, ur_kernel_handle_t>)
247+
return UrApiKind::urKernelRelease;
246248
}();
247249
static constexpr auto Retain = []() constexpr {
248250
if constexpr (std::is_same_v<URResource, ur_program_handle_t>)
249251
return UrApiKind::urProgramRetain;
252+
if constexpr (std::is_same_v<URResource, ur_kernel_handle_t>)
253+
return UrApiKind::urKernelRetain;
250254
}();
251255

252256
public:

sycl/source/detail/device_image_impl.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,26 @@ std::shared_ptr<kernel_impl> device_image_impl::tryGetExtensionKernel(
3434
auto [UrKernel, CacheMutex, ArgMask] =
3535
PM.getOrCreateKernel(Context, AdjustedName,
3636
/*PropList=*/{}, UrProgram);
37-
return std::make_shared<kernel_impl>(UrKernel, *getSyclObjImpl(Context),
38-
shared_from_this(), OwnerBundle,
39-
ArgMask, UrProgram, CacheMutex);
37+
return std::make_shared<kernel_impl>(
38+
std::move(UrKernel), *getSyclObjImpl(Context), shared_from_this(),
39+
OwnerBundle, ArgMask, UrProgram, CacheMutex);
4040
}
4141
return nullptr;
4242
}
4343

4444
ur_program_handle_t UrProgram = get_ur_program();
4545
detail::adapter_impl &Adapter = getSyclObjImpl(Context)->getAdapter();
46-
ur_kernel_handle_t UrKernel = nullptr;
46+
Managed<ur_kernel_handle_t> UrKernel{Adapter};
4747
Adapter.call<UrApiKind::urKernelCreate>(UrProgram, AdjustedName.c_str(),
4848
&UrKernel);
49-
// Kernel created by urKernelCreate is implicitly retained.
5049

5150
const KernelArgMask *ArgMask = nullptr;
5251
if (auto ArgMaskIt = MEliminatedKernelArgMasks.find(AdjustedName);
5352
ArgMaskIt != MEliminatedKernelArgMasks.end())
5453
ArgMask = &ArgMaskIt->second;
5554

5655
return std::make_shared<kernel_impl>(
57-
UrKernel, *detail::getSyclObjImpl(Context), shared_from_this(),
56+
std::move(UrKernel), *detail::getSyclObjImpl(Context), shared_from_this(),
5857
OwnerBundle, ArgMask, UrProgram, /*CacheMutex=*/nullptr);
5958
}
6059

sycl/source/detail/kernel_bundle_impl.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,9 @@ class kernel_bundle_impl
983983
SelectedImage->get_ur_program());
984984

985985
return std::make_shared<kernel_impl>(
986-
Kernel, *detail::getSyclObjImpl(MContext), std::move(SelectedImage),
987-
*this, ArgMask, SelectedImage->get_ur_program(), CacheMutex);
986+
std::move(Kernel), *detail::getSyclObjImpl(MContext),
987+
std::move(SelectedImage), *this, ArgMask,
988+
SelectedImage->get_ur_program(), CacheMutex);
988989
}
989990

990991
std::shared_ptr<kernel_impl>

sycl/source/detail/kernel_impl.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ namespace sycl {
1616
inline namespace _V1 {
1717
namespace detail {
1818

19-
kernel_impl::kernel_impl(ur_kernel_handle_t Kernel, context_impl &Context,
19+
kernel_impl::kernel_impl(Managed<ur_kernel_handle_t> &&Kernel,
20+
context_impl &Context,
2021
kernel_bundle_impl *KernelBundleImpl,
2122
const KernelArgMask *ArgMask)
22-
: MKernel(Kernel), MContext(Context.shared_from_this()),
23-
MProgram(ProgramManager::getInstance().getUrProgramFromUrKernel(Kernel,
23+
: MKernel(std::move(Kernel)), MContext(Context.shared_from_this()),
24+
MProgram(ProgramManager::getInstance().getUrProgramFromUrKernel(MKernel,
2425
Context)),
2526
MCreatedFromSource(true),
2627
MKernelBundleImpl(KernelBundleImpl ? KernelBundleImpl->shared_from_this()
@@ -39,12 +40,13 @@ kernel_impl::kernel_impl(ur_kernel_handle_t Kernel, context_impl &Context,
3940
enableUSMIndirectAccess();
4041
}
4142

42-
kernel_impl::kernel_impl(ur_kernel_handle_t Kernel, context_impl &ContextImpl,
43+
kernel_impl::kernel_impl(Managed<ur_kernel_handle_t> &&Kernel,
44+
context_impl &ContextImpl,
4345
std::shared_ptr<device_image_impl> &&DeviceImageImpl,
4446
const kernel_bundle_impl &KernelBundleImpl,
4547
const KernelArgMask *ArgMask,
4648
ur_program_handle_t Program, std::mutex *CacheMutex)
47-
: MKernel(Kernel), MContext(ContextImpl.shared_from_this()),
49+
: MKernel(std::move(Kernel)), MContext(ContextImpl.shared_from_this()),
4850
MProgram(Program),
4951
MCreatedFromSource(DeviceImageImpl->isNonSYCLSourceBased()),
5052
MDeviceImageImpl(std::move(DeviceImageImpl)),
@@ -58,14 +60,21 @@ kernel_impl::kernel_impl(ur_kernel_handle_t Kernel, context_impl &ContextImpl,
5860
enableUSMIndirectAccess();
5961
}
6062

61-
kernel_impl::~kernel_impl() {
62-
try {
63-
// TODO catch an exception and put it to list of asynchronous exceptions
64-
getAdapter().call<UrApiKind::urKernelRelease>(MKernel);
65-
} catch (std::exception &e) {
66-
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~kernel_impl", e);
67-
}
63+
#ifdef _MSC_VER
64+
#pragma warning(push)
65+
// https://developercommunity.visualstudio.com/t/False-C4297-warning-while-using-function/1130300
66+
// https://godbolt.org/z/xsMvKf84f
67+
#pragma warning(disable : 4297)
68+
#endif
69+
kernel_impl::~kernel_impl() try {
70+
} catch (std::exception &e) {
71+
// TODO put it to list of asynchronous exceptions
72+
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~kernel_impl", e);
73+
return; // Don't re-throw.
6874
}
75+
#ifdef _MSC_VER
76+
#pragma warning(pop)
77+
#endif
6978

7079
bool kernel_impl::isCreatedFromSource() const {
7180
// TODO it is not clear how to understand whether the SYCL kernel is created

sycl/source/detail/kernel_impl.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class kernel_impl {
3939
/// \param Kernel is a valid UrKernel instance
4040
/// \param Context is a valid SYCL context
4141
/// \param KernelBundleImpl is a valid instance of kernel_bundle_impl
42-
kernel_impl(ur_kernel_handle_t Kernel, context_impl &Context,
42+
kernel_impl(Managed<ur_kernel_handle_t> &&Kernel, context_impl &Context,
4343
kernel_bundle_impl *KernelBundleImpl,
4444
const KernelArgMask *ArgMask = nullptr);
4545

@@ -49,7 +49,7 @@ class kernel_impl {
4949
/// \param Kernel is a valid UrKernel instance
5050
/// \param ContextImpl is a valid SYCL context
5151
/// \param KernelBundleImpl is a valid instance of kernel_bundle_impl
52-
kernel_impl(ur_kernel_handle_t Kernel, context_impl &ContextImpl,
52+
kernel_impl(Managed<ur_kernel_handle_t> &&Kernel, context_impl &ContextImpl,
5353
std::shared_ptr<device_image_impl> &&DeviceImageImpl,
5454
const kernel_bundle_impl &KernelBundleImpl,
5555
const KernelArgMask *ArgMask, ur_program_handle_t Program,
@@ -198,11 +198,7 @@ class kernel_impl {
198198
typename Param::return_type ext_oneapi_get_info(queue Queue,
199199
const range<1> &WG) const;
200200

201-
/// Get a constant reference to a raw kernel object.
202-
///
203-
/// \return a constant reference to a valid UrKernel instance with raw
204-
/// kernel object.
205-
const ur_kernel_handle_t &getHandleRef() const { return MKernel; }
201+
ur_kernel_handle_t getHandleRef() const { return MKernel; }
206202

207203
/// Check if kernel was created from a program that had been created from
208204
/// source.
@@ -243,7 +239,7 @@ class kernel_impl {
243239
std::string_view getName() const;
244240

245241
private:
246-
ur_kernel_handle_t MKernel = nullptr;
242+
Managed<ur_kernel_handle_t> MKernel;
247243
const std::shared_ptr<context_impl> MContext;
248244
const ur_program_handle_t MProgram = nullptr;
249245
bool MCreatedFromSource = true;

sycl/source/detail/kernel_name_based_cache_t.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace detail {
2222
using FastKernelCacheKeyT = std::pair<ur_device_handle_t, ur_context_handle_t>;
2323

2424
struct FastKernelCacheVal {
25-
ur_kernel_handle_t MKernelHandle; /* UR kernel handle pointer. */
25+
Managed<ur_kernel_handle_t> MKernelHandle; /* UR kernel. */
2626
std::mutex *MMutex; /* Mutex guarding this kernel. When
2727
caching is disabled, the pointer is
2828
nullptr. */
@@ -34,18 +34,15 @@ struct FastKernelCacheVal {
3434
cache is destroyed deliberately before the
3535
adapter. */
3636

37-
FastKernelCacheVal(ur_kernel_handle_t KernelHandle, std::mutex *Mutex,
38-
const KernelArgMask *KernelArgMask,
37+
FastKernelCacheVal(Managed<ur_kernel_handle_t> &&KernelHandle,
38+
std::mutex *Mutex, const KernelArgMask *KernelArgMask,
3939
Managed<ur_program_handle_t> &&ProgramHandle,
4040
adapter_impl &Adapter)
41-
: MKernelHandle(KernelHandle), MMutex(Mutex),
41+
: MKernelHandle(std::move(KernelHandle)), MMutex(Mutex),
4242
MKernelArgMask(KernelArgMask), MProgramHandle(std::move(ProgramHandle)),
4343
MAdapter(Adapter) {}
4444

4545
~FastKernelCacheVal() {
46-
if (MKernelHandle)
47-
MAdapter.call<sycl::detail::UrApiKind::urKernelRelease>(MKernelHandle);
48-
MKernelHandle = nullptr;
4946
MMutex = nullptr;
5047
MKernelArgMask = nullptr;
5148
}

sycl/source/detail/kernel_program_cache.hpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,22 +194,23 @@ class KernelProgramCache {
194194

195195
struct KernelBuildResult
196196
: public BuildResult<
197-
std::pair<ur_kernel_handle_t, const KernelArgMask *>> {
198-
const adapter_impl &MAdapter;
199-
KernelBuildResult(const adapter_impl &Adapter) : MAdapter(Adapter) {
200-
Val.first = nullptr;
201-
}
202-
~KernelBuildResult() {
203-
try {
204-
if (Val.first) {
205-
ur_result_t Err =
206-
MAdapter.call_nocheck<UrApiKind::urKernelRelease>(Val.first);
207-
__SYCL_CHECK_UR_CODE_NO_EXC(Err, MAdapter.getBackend());
208-
}
209-
} catch (std::exception &e) {
210-
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~KernelBuildResult", e);
211-
}
197+
std::pair<Managed<ur_kernel_handle_t>, const KernelArgMask *>> {
198+
KernelBuildResult() = default;
199+
200+
#ifdef _MSC_VER
201+
#pragma warning(push)
202+
// https://developercommunity.visualstudio.com/t/False-C4297-warning-while-using-function/1130300
203+
// https://godbolt.org/z/xsMvKf84f
204+
#pragma warning(disable : 4297)
205+
#endif
206+
~KernelBuildResult() try {
207+
} catch (std::exception &e) {
208+
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~KernelBuildResult", e);
209+
return; // Don't re-throw.
212210
}
211+
#ifdef _MSC_VER
212+
#pragma warning(pop)
213+
#endif
213214
};
214215

215216
using KernelCacheT = emhash8::HashMap<
@@ -445,7 +446,7 @@ class KernelProgramCache {
445446
auto &Cache = LockedCache.get()[Program];
446447
auto [It, DidInsert] = Cache.try_emplace(KernelName, nullptr);
447448
if (DidInsert) {
448-
It->second = std::make_shared<KernelBuildResult>(getAdapter());
449+
It->second = std::make_shared<KernelBuildResult>();
449450
traceKernel("Kernel inserted.", KernelName);
450451
} else
451452
traceKernel("Kernel fetched.", KernelName);

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,9 +1107,8 @@ FastKernelCacheValPtr ProgramManager::getOrCreateKernel(
11071107
getBuiltURProgram(ContextImpl, DeviceImpl, KernelName, NDRDesc);
11081108

11091109
auto BuildF = [this, &Program, &KernelName, &ContextImpl] {
1110-
ur_kernel_handle_t Kernel = nullptr;
1111-
11121110
adapter_impl &Adapter = ContextImpl.getAdapter();
1111+
Managed<ur_kernel_handle_t> Kernel{Adapter};
11131112
Adapter.call<errc::kernel_not_supported, UrApiKind::urKernelCreate>(
11141113
Program, KernelName.data(), &Kernel);
11151114

@@ -1126,7 +1125,7 @@ FastKernelCacheValPtr ProgramManager::getOrCreateKernel(
11261125
const KernelArgMask *ArgMask = nullptr;
11271126
if (!m_UseSpvFile)
11281127
ArgMask = getEliminatedKernelArgMask(Program, KernelName);
1129-
return std::make_pair(Kernel, ArgMask);
1128+
return std::make_pair(std::move(Kernel), ArgMask);
11301129
};
11311130

11321131
auto GetCachedBuildF = [&Cache, &KernelName, &Program]() {
@@ -1138,24 +1137,19 @@ FastKernelCacheValPtr ProgramManager::getOrCreateKernel(
11381137
// threads when caching is disabled, so we can return
11391138
// nullptr for the mutex.
11401139
auto [Kernel, ArgMask] = BuildF();
1141-
return std::make_shared<FastKernelCacheVal>(
1142-
Kernel, nullptr, ArgMask, std::move(Program), ContextImpl.getAdapter());
1140+
return std::make_shared<FastKernelCacheVal>(std::move(Kernel), nullptr,
1141+
ArgMask, std::move(Program),
1142+
ContextImpl.getAdapter());
11431143
}
11441144

11451145
std::shared_ptr<KernelProgramCache::KernelBuildResult> BuildResult =
11461146
Cache.getOrBuild<errc::invalid>(GetCachedBuildF, BuildF);
11471147
assert(BuildResult && "getOrBuild isn't supposed to return nullptr!");
1148-
const std::pair<ur_kernel_handle_t, const KernelArgMask *>
1148+
std::pair<Managed<ur_kernel_handle_t>, const KernelArgMask *>
11491149
&KernelArgMaskPair = BuildResult->Val;
11501150
auto ret_val = std::make_shared<FastKernelCacheVal>(
1151-
KernelArgMaskPair.first, &(BuildResult->MBuildResultMutex),
1151+
KernelArgMaskPair.first.retain(), &(BuildResult->MBuildResultMutex),
11521152
KernelArgMaskPair.second, std::move(Program), ContextImpl.getAdapter());
1153-
// If caching is enabled, one copy of the kernel handle will be
1154-
// stored in FastKernelCacheVal, and one is in
1155-
// KernelProgramCache::MKernelsPerProgramCache. To cover
1156-
// MKernelsPerProgramCache, we need to increase the ref count of the kernel.
1157-
ContextImpl.getAdapter().call<UrApiKind::urKernelRetain>(
1158-
KernelArgMaskPair.first);
11591153
Cache.saveKernel(KernelName, UrDevice, ret_val, CacheHintPtr);
11601154
return ret_val;
11611155
}
@@ -3116,7 +3110,7 @@ ProgramManager::build(const DevImgPlainWithDeps &DevImgWithDeps,
31163110

31173111
// When caching is enabled, the returned UrKernel will already have
31183112
// its ref count incremented.
3119-
std::tuple<ur_kernel_handle_t, std::mutex *, const KernelArgMask *>
3113+
std::tuple<Managed<ur_kernel_handle_t>, std::mutex *, const KernelArgMask *>
31203114
ProgramManager::getOrCreateKernel(const context &Context,
31213115
KernelNameStrRefT KernelName,
31223116
const property_list &PropList,
@@ -3133,9 +3127,9 @@ ProgramManager::getOrCreateKernel(const context &Context,
31333127
KernelProgramCache &Cache = Ctx.getKernelProgramCache();
31343128

31353129
auto BuildF = [this, &Program, &KernelName, &Ctx] {
3136-
ur_kernel_handle_t Kernel = nullptr;
3137-
31383130
adapter_impl &Adapter = Ctx.getAdapter();
3131+
Managed<ur_kernel_handle_t> Kernel{Adapter};
3132+
31393133
Adapter.call<UrApiKind::urKernelCreate>(Program, KernelName.data(),
31403134
&Kernel);
31413135

@@ -3152,7 +3146,7 @@ ProgramManager::getOrCreateKernel(const context &Context,
31523146
const KernelArgMask *KernelArgMask =
31533147
getEliminatedKernelArgMask(Program, KernelName);
31543148

3155-
return std::make_pair(Kernel, KernelArgMask);
3149+
return std::make_pair(std::move(Kernel), KernelArgMask);
31563150
};
31573151

31583152
auto GetCachedBuildF = [&Cache, &KernelName, Program]() {
@@ -3164,7 +3158,7 @@ ProgramManager::getOrCreateKernel(const context &Context,
31643158
// threads when caching is disabled, so we can return
31653159
// nullptr for the mutex.
31663160
auto [Kernel, ArgMask] = BuildF();
3167-
return make_tuple(Kernel, nullptr, ArgMask);
3161+
return make_tuple(std::move(Kernel), nullptr, ArgMask);
31683162
}
31693163

31703164
std::shared_ptr<KernelProgramCache::KernelBuildResult> BuildResult =
@@ -3174,8 +3168,7 @@ ProgramManager::getOrCreateKernel(const context &Context,
31743168
// stored in the cache, and one handle is returned to the
31753169
// caller. In that case, we need to increase the ref count of the
31763170
// kernel.
3177-
Ctx.getAdapter().call<UrApiKind::urKernelRetain>(BuildResult->Val.first);
3178-
return std::make_tuple(BuildResult->Val.first,
3171+
return std::make_tuple(BuildResult->Val.first.retain(),
31793172
&(BuildResult->MBuildResultMutex),
31803173
BuildResult->Val.second);
31813174
}
@@ -3242,15 +3235,17 @@ ur_kernel_handle_t ProgramManager::getOrCreateMaterializedKernel(
32423235
build(std::move(ProgramManaged), ContextImpl, CompileOpts, LinkOpts, Devs,
32433236
/*For non SPIR-V devices DeviceLibReqdMask is always 0*/ 0,
32443237
ExtraProgramsToLink);
3245-
ur_kernel_handle_t UrKernel{nullptr};
3238+
Managed<ur_kernel_handle_t> UrKernel{Adapter};
32463239
Adapter.call<errc::kernel_not_supported, UrApiKind::urKernelCreate>(
32473240
BuildProgram, KernelName.data(), &UrKernel);
3241+
ur_kernel_handle_t RawUrKernel = UrKernel;
32483242
{
32493243
std::lock_guard<std::mutex> KernelIDsGuard(m_KernelIDsMutex);
3250-
m_MaterializedKernels[KernelName][SpecializationConsts] = UrKernel;
3244+
m_MaterializedKernels[KernelName][SpecializationConsts] =
3245+
std::move(UrKernel);
32513246
}
32523247

3253-
return UrKernel;
3248+
return RawUrKernel;
32543249
}
32553250

32563251
bool doesDevSupportDeviceRequirements(const device_impl &Dev,

0 commit comments

Comments
 (0)