Skip to content

[SYCL][L0] Cache the result of zeKernelGetName #6053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 22 additions & 27 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4632,18 +4632,31 @@ pi_result piKernelCreate(pi_program Program, const char *KernelName,
return PI_ERROR_UNKNOWN;
}

// Update the refcount of the program and context to show it's used by this
// kernel.
PI_CALL((*RetKernel)->initialize());
return PI_SUCCESS;
}

pi_result _pi_kernel::initialize() {
// Retain the program and context to show it's used by this kernel.
PI_CALL(piProgramRetain(Program));
if (IndirectAccessTrackingEnabled)
// TODO: do piContextRetain without the guard
PI_CALL(piContextRetain(Program->Context));

// Set up how to obtain kernel properties when needed.
(*RetKernel)->ZeKernelProperties.Compute =
[ZeKernel](ze_kernel_properties_t &Properties) {
ZE_CALL_NOCHECK(zeKernelGetProperties, (ZeKernel, &Properties));
};
ZeKernelProperties.Compute = [this](ze_kernel_properties_t &Properties) {
ZE_CALL_NOCHECK(zeKernelGetProperties, (ZeKernel, &Properties));
};

// Cache kernel name.
ZeKernelName.Compute = [this](std::string &Name) {
size_t Size = 0;
ZE_CALL_NOCHECK(zeKernelGetName, (ZeKernel, &Size, nullptr));
char *KernelName = new char[Size];
ZE_CALL_NOCHECK(zeKernelGetName, (ZeKernel, &Size, KernelName));
Name = KernelName;
delete[] KernelName;
};

return PI_SUCCESS;
}
Expand Down Expand Up @@ -4723,13 +4736,8 @@ pi_result piKernelGetInfo(pi_kernel Kernel, pi_kernel_info ParamName,
return ReturnValue(pi_program{Kernel->Program});
case PI_KERNEL_INFO_FUNCTION_NAME:
try {
size_t Size = 0;
ZE_CALL(zeKernelGetName, (Kernel->ZeKernel, &Size, nullptr));
char *KernelName = new char[Size];
ZE_CALL(zeKernelGetName, (Kernel->ZeKernel, &Size, KernelName));
pi_result Res = ReturnValue(static_cast<const char *>(KernelName));
delete[] KernelName;
return Res;
std::string &KernelName = *Kernel->ZeKernelName.operator->();
return ReturnValue(static_cast<const char *>(KernelName.c_str()));
} catch (const std::bad_alloc &) {
return PI_OUT_OF_HOST_MEMORY;
} catch (...) {
Expand Down Expand Up @@ -5082,20 +5090,7 @@ pi_result piextKernelCreateWithNativeHandle(pi_native_handle NativeHandle,

auto ZeKernel = pi_cast<ze_kernel_handle_t>(NativeHandle);
*Kernel = new _pi_kernel(ZeKernel, OwnNativeHandle, Program);

// Update the refcount of the program and context to show it's used by this
// kernel.
PI_CALL(piProgramRetain(Program));
if (IndirectAccessTrackingEnabled)
// TODO: do piContextRetain without the guard
PI_CALL(piContextRetain(Program->Context));

// Set up how to obtain kernel properties when needed.
(*Kernel)->ZeKernelProperties.Compute =
[ZeKernel](ze_kernel_properties_t &Properties) {
ZE_CALL_NOCHECK(zeKernelGetProperties, (ZeKernel, &Properties));
};

PI_CALL((*Kernel)->initialize());
return PI_SUCCESS;
}

Expand Down
6 changes: 5 additions & 1 deletion sycl/plugins/level_zero/pi_level_zero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ template <class T> struct ZeCache : private T {
// order to disallow access other than through "->".
//
typedef std::function<void(T &)> InitFunctionType;
InitFunctionType Compute;
InitFunctionType Compute{nullptr};
bool Computed{false};

ZeCache() : T{} {}
Expand Down Expand Up @@ -1379,6 +1379,9 @@ struct _pi_kernel : _pi_object {
: ZeKernel{Kernel}, OwnZeKernel{OwnZeKernel}, Program{Program},
MemAllocs{}, SubmissionsCount{0} {}

// Completed initialization of PI kernel. Must be called after construction.
pi_result initialize();

// Returns true if kernel has indirect access, false otherwise.
bool hasIndirectAccess() {
// Currently indirect access flag is set for all kernels and there is no API
Expand Down Expand Up @@ -1445,6 +1448,7 @@ struct _pi_kernel : _pi_object {

// Cache of the kernel properties.
ZeCache<ZeStruct<ze_kernel_properties_t>> ZeKernelProperties;
ZeCache<std::string> ZeKernelName;
};

struct _pi_sampler : _pi_object {
Expand Down