Skip to content

Commit e428745

Browse files
authored
Merge pull request #1656 from pbalcer/leaks-program-kernel
fix leaks on level-zero interop program and kernel handles
2 parents fb3cbd1 + 80d46bb commit e428745

File tree

7 files changed

+124
-3
lines changed

7 files changed

+124
-3
lines changed

source/adapters/level_zero/kernel.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelCreateWithNativeHandle(
849849
try {
850850
Kernel = new ur_kernel_handle_t_(ZeKernel, Properties->isNativeHandleOwned,
851851
Context);
852+
if (Properties->isNativeHandleOwned) {
853+
// If ownership is passed to the adapter we need to pass the kernel
854+
// to this vector which is then used during ZeKernelRelease.
855+
Kernel->ZeKernels.push_back(ZeKernel);
856+
}
857+
852858
*RetKernel = reinterpret_cast<ur_kernel_handle_t>(Kernel);
853859
} catch (const std::bad_alloc &) {
854860
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;

source/adapters/level_zero/program.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -891,10 +891,15 @@ void ur_program_handle_t_::ur_release_program_resources(bool deletion) {
891891
}
892892

893893
if (ZeModule && OwnZeModule) {
894-
for (auto &ZeModulePair : this->ZeModuleMap) {
895-
ZE_CALL_NOCHECK(zeModuleDestroy, (ZeModulePair.second));
894+
if (ZeModuleMap.empty()) {
895+
// interop api
896+
ZE_CALL_NOCHECK(zeModuleDestroy, (ZeModule));
897+
} else {
898+
for (auto &ZeModulePair : this->ZeModuleMap) {
899+
ZE_CALL_NOCHECK(zeModuleDestroy, (ZeModulePair.second));
900+
}
901+
this->ZeModuleMap.clear();
896902
}
897-
this->ZeModuleMap.clear();
898903
}
899904
resourcesReleased = true;
900905
}

test/adapters/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,35 @@ function(add_adapter_test name)
4848
ENVIRONMENT "${args_ENVIRONMENT}")
4949
endfunction()
5050

51+
find_program(VALGRIND valgrind)
52+
53+
function(add_adapter_memcheck_test name)
54+
cmake_parse_arguments(args
55+
"" # options
56+
"" # one value keywords
57+
"ENVIRONMENT" # multi value keywords
58+
${ARGN})
59+
if(VALGRIND)
60+
set(target test-adapter-${name})
61+
set(test_name ${target}-memcheck)
62+
63+
add_test(NAME ${test_name}
64+
COMMAND ${CMAKE_COMMAND}
65+
-D TEST_FILE=valgrind
66+
-D TEST_ARGS="--tool=memcheck --leak-check=full $<TARGET_FILE:${target}> --devices_count=${UR_TEST_DEVICES_COUNT} --platforms_count=${UR_TEST_DEVICES_COUNT}"
67+
-D MODE=stderr
68+
-D MATCH_FILE=${CMAKE_CURRENT_SOURCE_DIR}/${name}_memcheck.match
69+
-P ${PROJECT_SOURCE_DIR}/cmake/match.cmake
70+
DEPENDS ${TEST_TARGET_NAME}
71+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
72+
)
73+
74+
set_tests_properties(${test_name} PROPERTIES
75+
LABELS "adapter-specific;${name}"
76+
ENVIRONMENT "${args_ENVIRONMENT}")
77+
endif()
78+
endfunction()
79+
5180
if(UR_BUILD_ADAPTER_CUDA OR UR_BUILD_ADAPTER_ALL)
5281
add_subdirectory(cuda)
5382
endif()

test/adapters/level_zero/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@ else()
1313
FIXTURE KERNELS
1414
SOURCES
1515
urProgramLink.cpp
16+
urKernelCreateWithNativeHandle.cpp
1617
ENVIRONMENT
1718
"UR_ADAPTERS_FORCE_LOAD=\"$<TARGET_FILE:ur_adapter_level_zero>\""
1819
)
20+
# TODO: valgrind tests require very new environment.
21+
# Enable once all L0 runners are updated.
22+
# add_adapter_memcheck_test(level_zero
23+
# ENVIRONMENT
24+
# "UR_ADAPTERS_FORCE_LOAD=\"$<TARGET_FILE:ur_adapter_level_zero>\""
25+
# )
26+
27+
target_link_libraries(test-adapter-level_zero PRIVATE
28+
LevelZeroLoader
29+
LevelZeroLoader-Headers
30+
)
1931

2032
target_include_directories(test-adapter-level_zero PRIVATE
2133
${PROJECT_SOURCE_DIR}/source
2234
${PROJECT_SOURCE_DIR}/source/adapters/level_zero
35+
LevelZeroLoader-Headers
2336
)
2437

2538
add_dependencies(test-adapter-level_zero
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{IGNORE}}
2+
{{.*}} ERROR SUMMARY: 0 errors from 0 contexts {{.*}}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (C) 2024 Intel Corporation
2+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See LICENSE.TXT
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
#include "level_zero/ze_api.h"
7+
#include "ur_api.h"
8+
#include "uur/checks.h"
9+
#include <uur/fixtures.h>
10+
11+
using urLevelZeroKernelNativeHandleTest = uur::urContextTest;
12+
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urLevelZeroKernelNativeHandleTest);
13+
14+
TEST_P(urLevelZeroKernelNativeHandleTest, OwnedHandleRelease) {
15+
ze_context_handle_t native_context;
16+
urContextGetNativeHandle(context, (ur_native_handle_t *)&native_context);
17+
18+
ze_device_handle_t native_device;
19+
urDeviceGetNativeHandle(device, (ur_native_handle_t *)&native_device);
20+
21+
std::shared_ptr<std::vector<char>> il_binary;
22+
uur::KernelsEnvironment::instance->LoadSource("foo", il_binary);
23+
24+
auto kernel_name =
25+
uur::KernelsEnvironment::instance->GetEntryPointNames("foo")[0];
26+
27+
ze_module_desc_t moduleDesc = {ZE_STRUCTURE_TYPE_MODULE_DESC};
28+
moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
29+
moduleDesc.inputSize = il_binary->size();
30+
moduleDesc.pInputModule =
31+
reinterpret_cast<const uint8_t *>(il_binary->data());
32+
moduleDesc.pBuildFlags = "";
33+
ze_module_handle_t module;
34+
35+
ASSERT_EQ(zeModuleCreate(native_context, native_device, &moduleDesc,
36+
&module, NULL),
37+
ZE_RESULT_SUCCESS);
38+
39+
ze_kernel_desc_t kernelDesc = {ZE_STRUCTURE_TYPE_KERNEL_DESC};
40+
kernelDesc.pKernelName = kernel_name.c_str();
41+
42+
ze_kernel_handle_t native_kernel;
43+
44+
ASSERT_EQ(zeKernelCreate(module, &kernelDesc, &native_kernel),
45+
ZE_RESULT_SUCCESS);
46+
47+
ur_program_native_properties_t pprops = {
48+
UR_STRUCTURE_TYPE_PROGRAM_NATIVE_PROPERTIES, nullptr, 1};
49+
50+
ur_program_handle_t program;
51+
ASSERT_SUCCESS(urProgramCreateWithNativeHandle((ur_native_handle_t)module,
52+
context, &pprops, &program));
53+
54+
ur_kernel_native_properties_t kprops = {
55+
UR_STRUCTURE_TYPE_KERNEL_NATIVE_PROPERTIES, nullptr, 1};
56+
57+
ur_kernel_handle_t kernel;
58+
ASSERT_SUCCESS(urKernelCreateWithNativeHandle(
59+
(ur_native_handle_t)native_kernel, context, program, &kprops, &kernel));
60+
61+
ASSERT_SUCCESS(urKernelRelease(kernel));
62+
ASSERT_SUCCESS(urProgramRelease(program));
63+
}

test/adapters/level_zero/urProgramLink.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See LICENSE.TXT
44
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55

6+
#include "ur_api.h"
67
#include <uur/fixtures.h>
78

89
using urLevelZeroProgramLinkTest = uur::urProgramTest;
@@ -28,4 +29,6 @@ TEST_P(urLevelZeroProgramLinkTest, InvalidLinkOptionsPrintedInLog) {
2829
log.data(), nullptr));
2930
ASSERT_EQ(log[logSize - 1], '\0');
3031
ASSERT_NE(std::string{log.data()}.find("-foo"), std::string::npos);
32+
33+
ASSERT_SUCCESS(urProgramRelease(linked_program));
3134
}

0 commit comments

Comments
 (0)