Skip to content

Commit 40cece3

Browse files
[SYCL][L0] Cache the result of zeKernelGetName (#6053)
Signed-off-by: Sergey V Maslov <[email protected]>
1 parent e688fa5 commit 40cece3

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4636,18 +4636,31 @@ pi_result piKernelCreate(pi_program Program, const char *KernelName,
46364636
return PI_ERROR_UNKNOWN;
46374637
}
46384638

4639-
// Update the refcount of the program and context to show it's used by this
4640-
// kernel.
4639+
PI_CALL((*RetKernel)->initialize());
4640+
return PI_SUCCESS;
4641+
}
4642+
4643+
pi_result _pi_kernel::initialize() {
4644+
// Retain the program and context to show it's used by this kernel.
46414645
PI_CALL(piProgramRetain(Program));
46424646
if (IndirectAccessTrackingEnabled)
46434647
// TODO: do piContextRetain without the guard
46444648
PI_CALL(piContextRetain(Program->Context));
46454649

46464650
// Set up how to obtain kernel properties when needed.
4647-
(*RetKernel)->ZeKernelProperties.Compute =
4648-
[ZeKernel](ze_kernel_properties_t &Properties) {
4649-
ZE_CALL_NOCHECK(zeKernelGetProperties, (ZeKernel, &Properties));
4650-
};
4651+
ZeKernelProperties.Compute = [this](ze_kernel_properties_t &Properties) {
4652+
ZE_CALL_NOCHECK(zeKernelGetProperties, (ZeKernel, &Properties));
4653+
};
4654+
4655+
// Cache kernel name.
4656+
ZeKernelName.Compute = [this](std::string &Name) {
4657+
size_t Size = 0;
4658+
ZE_CALL_NOCHECK(zeKernelGetName, (ZeKernel, &Size, nullptr));
4659+
char *KernelName = new char[Size];
4660+
ZE_CALL_NOCHECK(zeKernelGetName, (ZeKernel, &Size, KernelName));
4661+
Name = KernelName;
4662+
delete[] KernelName;
4663+
};
46514664

46524665
return PI_SUCCESS;
46534666
}
@@ -4727,13 +4740,8 @@ pi_result piKernelGetInfo(pi_kernel Kernel, pi_kernel_info ParamName,
47274740
return ReturnValue(pi_program{Kernel->Program});
47284741
case PI_KERNEL_INFO_FUNCTION_NAME:
47294742
try {
4730-
size_t Size = 0;
4731-
ZE_CALL(zeKernelGetName, (Kernel->ZeKernel, &Size, nullptr));
4732-
char *KernelName = new char[Size];
4733-
ZE_CALL(zeKernelGetName, (Kernel->ZeKernel, &Size, KernelName));
4734-
pi_result Res = ReturnValue(static_cast<const char *>(KernelName));
4735-
delete[] KernelName;
4736-
return Res;
4743+
std::string &KernelName = *Kernel->ZeKernelName.operator->();
4744+
return ReturnValue(static_cast<const char *>(KernelName.c_str()));
47374745
} catch (const std::bad_alloc &) {
47384746
return PI_OUT_OF_HOST_MEMORY;
47394747
} catch (...) {
@@ -5086,20 +5094,7 @@ pi_result piextKernelCreateWithNativeHandle(pi_native_handle NativeHandle,
50865094

50875095
auto ZeKernel = pi_cast<ze_kernel_handle_t>(NativeHandle);
50885096
*Kernel = new _pi_kernel(ZeKernel, OwnNativeHandle, Program);
5089-
5090-
// Update the refcount of the program and context to show it's used by this
5091-
// kernel.
5092-
PI_CALL(piProgramRetain(Program));
5093-
if (IndirectAccessTrackingEnabled)
5094-
// TODO: do piContextRetain without the guard
5095-
PI_CALL(piContextRetain(Program->Context));
5096-
5097-
// Set up how to obtain kernel properties when needed.
5098-
(*Kernel)->ZeKernelProperties.Compute =
5099-
[ZeKernel](ze_kernel_properties_t &Properties) {
5100-
ZE_CALL_NOCHECK(zeKernelGetProperties, (ZeKernel, &Properties));
5101-
};
5102-
5097+
PI_CALL((*Kernel)->initialize());
51035098
return PI_SUCCESS;
51045099
}
51055100

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ template <class T> struct ZeCache : private T {
185185
// order to disallow access other than through "->".
186186
//
187187
typedef std::function<void(T &)> InitFunctionType;
188-
InitFunctionType Compute;
188+
InitFunctionType Compute{nullptr};
189189
bool Computed{false};
190190

191191
ZeCache() : T{} {}
@@ -1379,6 +1379,9 @@ struct _pi_kernel : _pi_object {
13791379
: ZeKernel{Kernel}, OwnZeKernel{OwnZeKernel}, Program{Program},
13801380
MemAllocs{}, SubmissionsCount{0} {}
13811381

1382+
// Completed initialization of PI kernel. Must be called after construction.
1383+
pi_result initialize();
1384+
13821385
// Returns true if kernel has indirect access, false otherwise.
13831386
bool hasIndirectAccess() {
13841387
// Currently indirect access flag is set for all kernels and there is no API
@@ -1445,6 +1448,7 @@ struct _pi_kernel : _pi_object {
14451448

14461449
// Cache of the kernel properties.
14471450
ZeCache<ZeStruct<ze_kernel_properties_t>> ZeKernelProperties;
1451+
ZeCache<std::string> ZeKernelName;
14481452
};
14491453

14501454
struct _pi_sampler : _pi_object {

0 commit comments

Comments
 (0)