Skip to content

Commit 7dd41b0

Browse files
authored
Merge pull request #339 from PatKamin/refactor-libs-load
[ur] Refactor adapters load
2 parents 4aa3649 + eb4e258 commit 7dd41b0

17 files changed

+274
-185
lines changed

scripts/templates/ldrddi.cpp.mako

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ from templates import helper as th
99
X=x.upper()
1010
%>/*
1111
*
12-
* Copyright (C) 2022 Intel Corporation
12+
* Copyright (C) 2022-2023 Intel Corporation
1313
*
1414
* SPDX-License-Identifier: MIT
1515
*
1616
* @file ${name}.cpp
1717
*
1818
*/
19+
#include "${x}_lib_loader.hpp"
1920
#include "${x}_loader.hpp"
2021

2122
namespace loader
@@ -239,7 +240,7 @@ ${tbl['export']['name']}(
239240
if(platform.initStatus != ${X}_RESULT_SUCCESS)
240241
continue;
241242
auto getTable = reinterpret_cast<${tbl['pfn']}>(
242-
GET_FUNCTION_PTR( platform.handle, "${tbl['export']['name']}") );
243+
loader::LibLoader::getFunctionPtr(platform.handle.get(), "${tbl['export']['name']}"));
243244
if(!getTable)
244245
continue;
245246
auto getTableResult = getTable( version, &platform.dditable.${n}.${tbl['name']});

scripts/templates/ldrddi.hpp.mako

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ from templates import helper as th
99
X=x.upper()
1010
%>/*
1111
*
12-
* Copyright (C) 2022 Intel Corporation
12+
* Copyright (C) 2022-2023 Intel Corporation
1313
*
1414
* SPDX-License-Identifier: MIT
1515
*
@@ -19,6 +19,8 @@ from templates import helper as th
1919
#ifndef UR_LOADER_LDRDDI_H
2020
#define UR_LOADER_LDRDDI_H 1
2121

22+
#include "${x}_object.hpp"
23+
2224
namespace loader
2325
{
2426
///////////////////////////////////////////////////////////////////////////////

source/common/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ add_subdirectory(unified_memory_allocation)
1212
target_link_libraries(common INTERFACE unified_memory_allocation)
1313

1414
target_sources(common INTERFACE uma_helpers.hpp)
15+
16+
if(WIN32)
17+
target_sources(common
18+
INTERFACE
19+
${CMAKE_CURRENT_SOURCE_DIR}/windows/ur_lib_loader.cpp
20+
)
21+
else()
22+
target_sources(common
23+
INTERFACE
24+
${CMAKE_CURRENT_SOURCE_DIR}/linux/ur_lib_loader.cpp
25+
)
26+
endif()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
*
3+
* Copyright (C) 2023 Intel Corporation
4+
*
5+
* SPDX-License-Identifier: MIT
6+
*
7+
*/
8+
#include <dlfcn.h>
9+
10+
#include "logger/ur_logger.hpp"
11+
#include "ur_lib_loader.hpp"
12+
13+
#if defined(SANITIZER_ANY)
14+
#define LOAD_DRIVER_LIBRARY(NAME) dlopen(NAME, RTLD_LAZY | RTLD_LOCAL)
15+
#else
16+
#define LOAD_DRIVER_LIBRARY(NAME) \
17+
dlopen(NAME, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND)
18+
#endif
19+
20+
namespace loader {
21+
22+
void LibLoader::freeAdapterLibrary(HMODULE handle) {
23+
if (handle) {
24+
int res = dlclose(handle);
25+
if (res) {
26+
logger::error(
27+
"Failed to unload the library with the handle at address {}",
28+
handle);
29+
}
30+
}
31+
}
32+
33+
std::unique_ptr<HMODULE, LibLoader::lib_dtor>
34+
LibLoader::loadAdapterLibrary(const char *name) {
35+
return std::unique_ptr<HMODULE, LibLoader::lib_dtor>(
36+
LOAD_DRIVER_LIBRARY(name));
37+
}
38+
39+
void *LibLoader::getFunctionPtr(HMODULE handle, const char *func_name) {
40+
return dlsym(handle, func_name);
41+
}
42+
43+
} // namespace loader

source/common/ur_lib_loader.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* Copyright (C) 2023 Intel Corporation
4+
*
5+
* SPDX-License-Identifier: MIT
6+
*
7+
*/
8+
#ifndef UR_LIB_LOADER_HPP
9+
#define UR_LIB_LOADER_HPP 1
10+
11+
#include <memory>
12+
13+
#include "ur_util.hpp"
14+
15+
namespace loader {
16+
17+
class LibLoader {
18+
public:
19+
struct lib_dtor {
20+
typedef HMODULE pointer;
21+
void operator()(HMODULE handle) { freeAdapterLibrary(handle); }
22+
};
23+
24+
static std::unique_ptr<HMODULE, lib_dtor>
25+
loadAdapterLibrary(const char *name);
26+
27+
static void freeAdapterLibrary(HMODULE handle);
28+
29+
static void *getFunctionPtr(HMODULE handle, const char *func_name);
30+
};
31+
32+
} // namespace loader
33+
34+
#endif // UR_LIB_LOADER_HPP

source/common/ur_util.hpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,13 @@
3838
#if defined(SANITIZER_MEMORY) || defined(SANITIZER_ADDRESS)
3939
#define SANITIZER_ANY
4040
#endif
41-
4241
///////////////////////////////////////////////////////////////////////////////
4342
#if defined(_WIN32)
4443
#include <Windows.h>
4544
#define MAKE_LIBRARY_NAME(NAME, VERSION) NAME ".dll"
46-
#define MAKE_LAYER_NAME(NAME) NAME ".dll"
47-
#define LOAD_DRIVER_LIBRARY(NAME) LoadLibraryExA(NAME, nullptr, 0)
48-
#define FREE_DRIVER_LIBRARY(LIB) \
49-
if (LIB) \
50-
FreeLibrary(LIB)
51-
#define GET_FUNCTION_PTR(LIB, FUNC_NAME) GetProcAddress(LIB, FUNC_NAME)
52-
#define string_copy_s strncpy_s
5345
#else
54-
#include <dlfcn.h>
5546
#define HMODULE void *
5647
#define MAKE_LIBRARY_NAME(NAME, VERSION) "lib" NAME ".so." VERSION
57-
#define MAKE_LAYER_NAME(NAME) \
58-
"lib" NAME ".so." L0_VALIDATION_LAYER_SUPPORTED_VERSION
59-
#if defined(SANITIZER_ANY)
60-
#define LOAD_DRIVER_LIBRARY(NAME) dlopen(NAME, RTLD_LAZY | RTLD_LOCAL)
61-
#else
62-
#define LOAD_DRIVER_LIBRARY(NAME) \
63-
dlopen(NAME, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND)
64-
#endif
65-
#define FREE_DRIVER_LIBRARY(LIB) \
66-
if (LIB) \
67-
dlclose(LIB)
68-
#define GET_FUNCTION_PTR(LIB, FUNC_NAME) dlsym(LIB, FUNC_NAME)
69-
#define string_copy_s strncpy
7048
#endif
7149

7250
inline std::string create_library_path(const char *name, const char *path) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* Copyright (C) 2023 Intel Corporation
4+
*
5+
* SPDX-License-Identifier: MIT
6+
*
7+
*/
8+
#include "ur_lib_loader.hpp"
9+
#include "logger/ur_logger.hpp"
10+
11+
namespace loader {
12+
13+
void LibLoader::freeAdapterLibrary(HMODULE handle) {
14+
if (handle) {
15+
BOOL res = FreeLibrary(handle);
16+
if (!res) {
17+
logger::error(
18+
"Failed to unload the library with the handle at address {}",
19+
handle);
20+
}
21+
}
22+
}
23+
24+
std::unique_ptr<HMODULE, LibLoader::lib_dtor>
25+
LibLoader::loadAdapterLibrary(const char *name) {
26+
return std::unique_ptr<HMODULE, LibLoader::lib_dtor>(
27+
LoadLibraryExA(name, nullptr, 0));
28+
}
29+
30+
void *LibLoader::getFunctionPtr(HMODULE handle, const char *func_name) {
31+
return GetProcAddress(handle, func_name);
32+
}
33+
34+
} // namespace loader

source/loader/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,13 @@ if(WIN32)
9595
target_sources(loader
9696
PRIVATE
9797
${CMAKE_CURRENT_SOURCE_DIR}/windows/lib_init.cpp
98-
${CMAKE_CURRENT_SOURCE_DIR}/windows/platform_discovery_win.cpp
9998
${CMAKE_CURRENT_SOURCE_DIR}/windows/loader_init.cpp
10099
${CMAKE_CURRENT_SOURCE_DIR}/layers/validation/backtrace_win.cpp
101100
)
102101
else()
103102
target_sources(loader
104103
PRIVATE
105104
${CMAKE_CURRENT_SOURCE_DIR}/linux/lib_init.cpp
106-
${CMAKE_CURRENT_SOURCE_DIR}/linux/platform_discovery_lin.cpp
107105
${CMAKE_CURRENT_SOURCE_DIR}/linux/loader_init.cpp
108106
${CMAKE_CURRENT_SOURCE_DIR}/layers/validation/backtrace_lin.cpp
109107
)

source/loader/linux/platform_discovery_lin.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

source/loader/platform_discovery.hpp

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)