Skip to content

Commit af1a023

Browse files
authored
Partially revert "[SYCL][UR][L0] Use leak checker and API logging (fo… (#18429)
…r v2) from loader (#17536)" This reverts commit b0b48a4 (except for test changes). The internal testing failed because the L0 loader used, was older than 1.21.2 (and didn't have leak checking implemented). I did not revert changes in the tests as that doesn't make any difference and it will be easier to reapply the patch in the future.
1 parent bc36585 commit af1a023

File tree

5 files changed

+136
-66
lines changed

5 files changed

+136
-66
lines changed

sycl/ur_win_proxy_loader/ur_win_proxy_loader.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,6 @@ void *&getDllHandle() {
107107
return dllHandle;
108108
}
109109

110-
static bool shouldLoadL0V2adapter() {
111-
auto SyclEnv = std::getenv("SYCL_UR_USE_LEVEL_ZERO_V2");
112-
auto UREvn = std::getenv("UR_LOADER_USE_LEVEL_ZERO_V2");
113-
114-
try {
115-
if (SyclEnv && std::stoi(SyclEnv) == 1) {
116-
return true;
117-
}
118-
} catch (...) {
119-
}
120-
121-
try {
122-
if (UREvn && std::atoi(UREvn) == 1) {
123-
return true;
124-
}
125-
} catch (...) {
126-
}
127-
128-
return false;
129-
}
130-
131110
/// Load the adapter libraries
132111
void preloadLibraries() {
133112
// Suppress system errors.
@@ -163,8 +142,7 @@ void preloadLibraries() {
163142
getDllHandle() = loadAdapter(UR_LIBRARY_NAME(loader));
164143
loadAdapter(UR_LIBRARY_NAME(adapter_opencl));
165144
loadAdapter(UR_LIBRARY_NAME(adapter_level_zero));
166-
if (shouldLoadL0V2adapter())
167-
loadAdapter(UR_LIBRARY_NAME(adapter_level_zero_v2));
145+
loadAdapter(UR_LIBRARY_NAME(adapter_level_zero_v2));
168146
loadAdapter(UR_LIBRARY_NAME(adapter_cuda));
169147
loadAdapter(UR_LIBRARY_NAME(adapter_hip));
170148
loadAdapter(UR_LIBRARY_NAME(adapter_native_cpu));

unified-runtime/source/adapters/level_zero/adapter.cpp

Lines changed: 103 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -309,25 +309,26 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
309309

310310
if (UrL0Debug & UR_L0_DEBUG_BASIC) {
311311
logger.setLegacySink(std::make_unique<ur_legacy_sink>());
312-
#ifdef UR_ADAPTER_LEVEL_ZERO_V2
313-
setEnvVar("ZEL_ENABLE_LOADER_LOGGING", "1");
314-
setEnvVar("ZEL_LOADER_LOGGING_LEVEL", "trace");
315-
setEnvVar("ZEL_LOADER_LOG_CONSOLE", "1");
316-
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
317-
#endif
318312
};
319313

320314
if (UrL0Debug & UR_L0_DEBUG_VALIDATION) {
321315
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
322316
setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1");
323317
}
324318

325-
if (UrL0LeaksDebug) {
326-
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
327-
setEnvVar("ZEL_ENABLE_BASIC_LEAK_CHECKER", "1");
328-
}
329-
330319
PlatformCache.Compute = [](Result<PlatformVec> &result) {
320+
static std::once_flag ZeCallCountInitialized;
321+
try {
322+
std::call_once(ZeCallCountInitialized, []() {
323+
if (UrL0LeaksDebug) {
324+
ZeCallCount = new std::map<std::string, int>;
325+
}
326+
});
327+
} catch (...) {
328+
result = exceptionToResult(std::current_exception());
329+
return;
330+
}
331+
331332
uint32_t UserForcedSysManInit = 0;
332333
// Check if the user has disabled the default L0 Env initialization.
333334
const int UrSysManEnvInitEnabled = [&UserForcedSysManInit] {
@@ -418,17 +419,6 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
418419
loader_version.patch >= 2)) {
419420
useInitDrivers = true;
420421
}
421-
422-
#ifdef UR_ADAPTER_LEVEL_ZERO_V2
423-
if ((loader_version.major == 1 && loader_version.minor < 21) ||
424-
(loader_version.major == 1 && loader_version.minor == 21 &&
425-
loader_version.patch < 2)) {
426-
UR_LOG(
427-
WARN,
428-
"WARNING: Level Zero Loader version is older than 1.21.2. "
429-
"Please update to the latest version for API logging support.\n");
430-
}
431-
#endif
432422
}
433423

434424
if (useInitDrivers) {
@@ -545,6 +535,97 @@ void globalAdapterOnDemandCleanup() {
545535
}
546536

547537
ur_result_t adapterStateTeardown() {
538+
// Print the balance of various create/destroy native calls.
539+
// The idea is to verify if the number of create(+) and destroy(-) calls are
540+
// matched.
541+
if (ZeCallCount && (UrL0LeaksDebug) != 0) {
542+
bool LeakFound = false;
543+
// clang-format off
544+
//
545+
// The format of this table is such that each row accounts for a
546+
// specific type of objects, and all elements in the raw except the last
547+
// one are allocating objects of that type, while the last element is known
548+
// to deallocate objects of that type.
549+
//
550+
std::vector<std::vector<std::string>> CreateDestroySet = {
551+
{"zeContextCreate", "zeContextDestroy"},
552+
{"zeCommandQueueCreate", "zeCommandQueueDestroy"},
553+
{"zeModuleCreate", "zeModuleDestroy"},
554+
{"zeKernelCreate", "zeKernelDestroy"},
555+
{"zeEventPoolCreate", "zeEventPoolDestroy"},
556+
{"zeCommandListCreateImmediate", "zeCommandListCreate", "zeCommandListDestroy"},
557+
{"zeEventCreate", "zeEventDestroy"},
558+
{"zeFenceCreate", "zeFenceDestroy"},
559+
{"zeImageCreate","zeImageViewCreateExt", "zeImageDestroy"},
560+
{"zeSamplerCreate", "zeSamplerDestroy"},
561+
{"zeMemAllocDevice", "zeMemAllocHost", "zeMemAllocShared", "zeMemFree"},
562+
};
563+
564+
// A sample output aimed below is this:
565+
// ------------------------------------------------------------------------
566+
// zeContextCreate = 1 \---> zeContextDestroy = 1
567+
// zeCommandQueueCreate = 1 \---> zeCommandQueueDestroy = 1
568+
// zeModuleCreate = 1 \---> zeModuleDestroy = 1
569+
// zeKernelCreate = 1 \---> zeKernelDestroy = 1
570+
// zeEventPoolCreate = 1 \---> zeEventPoolDestroy = 1
571+
// zeCommandListCreateImmediate = 1 |
572+
// zeCommandListCreate = 1 \---> zeCommandListDestroy = 1 ---> LEAK = 1
573+
// zeEventCreate = 2 \---> zeEventDestroy = 2
574+
// zeFenceCreate = 1 \---> zeFenceDestroy = 1
575+
// zeImageCreate = 0 \---> zeImageDestroy = 0
576+
// zeSamplerCreate = 0 \---> zeSamplerDestroy = 0
577+
// zeMemAllocDevice = 0 |
578+
// zeMemAllocHost = 1 |
579+
// zeMemAllocShared = 0 \---> zeMemFree = 1
580+
//
581+
// clang-format on
582+
// TODO: use logger to print this messages
583+
std::cerr << "Check balance of create/destroy calls\n";
584+
std::cerr << "----------------------------------------------------------\n";
585+
std::stringstream ss;
586+
for (const auto &Row : CreateDestroySet) {
587+
int diff = 0;
588+
for (auto I = Row.begin(); I != Row.end();) {
589+
const char *ZeName = (*I).c_str();
590+
const auto &ZeCount = (*ZeCallCount)[*I];
591+
592+
bool First = (I == Row.begin());
593+
bool Last = (++I == Row.end());
594+
595+
if (Last) {
596+
ss << " \\--->";
597+
diff -= ZeCount;
598+
} else {
599+
diff += ZeCount;
600+
if (!First) {
601+
ss << " | ";
602+
std::cerr << ss.str() << "\n";
603+
ss.str("");
604+
ss.clear();
605+
}
606+
}
607+
ss << std::setw(30) << std::right << ZeName;
608+
ss << " = ";
609+
ss << std::setw(5) << std::left << ZeCount;
610+
}
611+
612+
if (diff) {
613+
LeakFound = true;
614+
ss << " ---> LEAK = " << diff;
615+
}
616+
617+
std::cerr << ss.str() << '\n';
618+
ss.str("");
619+
ss.clear();
620+
}
621+
622+
ZeCallCount->clear();
623+
delete ZeCallCount;
624+
ZeCallCount = nullptr;
625+
if (LeakFound)
626+
return UR_RESULT_ERROR_INVALID_MEM_OBJECT;
627+
}
628+
548629
// Due to multiple DLLMain definitions with SYCL, register to cleanup the
549630
// Global Adapter after refcnt is 0
550631
#if defined(_WIN32)

unified-runtime/source/adapters/level_zero/common.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,15 @@ void zeParseError(ze_result_t ZeError, const char *&ErrorString) {
137137
} // switch
138138
}
139139

140-
#ifdef UR_ADAPTER_LEVEL_ZERO_V2
141-
ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *, const char *,
142-
bool) {
143-
return ZeResult;
144-
}
145-
#else
146140
ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *ZeName,
147141
const char *ZeArgs, bool TraceError) {
148142
UR_LOG(DEBUG, "ZE ---> {}{}", ZeName, ZeArgs);
149143

150144
if (ZeResult == ZE_RESULT_SUCCESS) {
151-
return ZeResult;
145+
if (UrL0LeaksDebug) {
146+
++(*ZeCallCount)[ZeName];
147+
}
148+
return ZE_RESULT_SUCCESS;
152149
}
153150

154151
if (TraceError) {
@@ -158,7 +155,6 @@ ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *ZeName,
158155
}
159156
return ZeResult;
160157
}
161-
#endif
162158

163159
// Specializations for various L0 structures
164160
template <> ze_structure_type_t getZeStructureType<ze_event_pool_desc_t>() {

unified-runtime/source/adapters/level_zero/common.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const int UrL0LeaksDebug = [] {
7575
const char *UrRet = std::getenv("UR_L0_LEAKS_DEBUG");
7676
if (!UrRet)
7777
return 0;
78-
7978
return std::atoi(UrRet);
8079
}();
8180

unified-runtime/source/adapters/level_zero/v2/common.hpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,24 @@
1515

1616
#include "../common.hpp"
1717
#include "logger/ur_logger.hpp"
18+
namespace {
19+
#define DECLARE_DESTROY_FUNCTION(name) \
20+
template <typename ZeHandleT> ze_result_t name##_wrapped(ZeHandleT handle) { \
21+
return ZE_CALL_NOCHECK_NAME(name, (handle), #name); \
22+
}
23+
24+
#define HANDLE_WRAPPER_TYPE(handle, destroy) \
25+
ze_handle_wrapper<handle, destroy##_wrapped<handle>>
26+
} // namespace
1827

1928
namespace v2 {
29+
30+
DECLARE_DESTROY_FUNCTION(zeKernelDestroy)
31+
DECLARE_DESTROY_FUNCTION(zeEventDestroy)
32+
DECLARE_DESTROY_FUNCTION(zeEventPoolDestroy)
33+
DECLARE_DESTROY_FUNCTION(zeContextDestroy)
34+
DECLARE_DESTROY_FUNCTION(zeCommandListDestroy)
35+
DECLARE_DESTROY_FUNCTION(zeImageDestroy)
2036
namespace raii {
2137

2238
template <typename ZeHandleT, ze_result_t (*destroy)(ZeHandleT)>
@@ -92,23 +108,23 @@ struct ze_handle_wrapper {
92108
bool ownZeHandle;
93109
};
94110

95-
using ze_kernel_handle_t =
96-
ze_handle_wrapper<::ze_kernel_handle_t, zeKernelDestroy>;
111+
using ze_kernel_handle_t = HANDLE_WRAPPER_TYPE(::ze_kernel_handle_t,
112+
zeKernelDestroy);
97113

98-
using ze_event_handle_t =
99-
ze_handle_wrapper<::ze_event_handle_t, zeEventDestroy>;
114+
using ze_event_handle_t = HANDLE_WRAPPER_TYPE(::ze_event_handle_t,
115+
zeEventDestroy);
100116

101-
using ze_event_pool_handle_t =
102-
ze_handle_wrapper<::ze_event_pool_handle_t, zeEventPoolDestroy>;
117+
using ze_event_pool_handle_t = HANDLE_WRAPPER_TYPE(::ze_event_pool_handle_t,
118+
zeEventPoolDestroy);
103119

104-
using ze_context_handle_t =
105-
ze_handle_wrapper<::ze_context_handle_t, zeContextDestroy>;
120+
using ze_context_handle_t = HANDLE_WRAPPER_TYPE(::ze_context_handle_t,
121+
zeContextDestroy);
106122

107-
using ze_command_list_handle_t =
108-
ze_handle_wrapper<::ze_command_list_handle_t, zeCommandListDestroy>;
123+
using ze_command_list_handle_t = HANDLE_WRAPPER_TYPE(::ze_command_list_handle_t,
124+
zeCommandListDestroy);
109125

110-
using ze_image_handle_t =
111-
ze_handle_wrapper<::ze_image_handle_t, zeImageDestroy>;
126+
using ze_image_handle_t = HANDLE_WRAPPER_TYPE(::ze_image_handle_t,
127+
zeImageDestroy);
112128

113129
} // namespace raii
114130
} // namespace v2

0 commit comments

Comments
 (0)