Skip to content

Commit 7f2bdd9

Browse files
gzadica1gzadicario
andauthored
Dynamic load ze_loader, remove link with ze_loader.{so/dll} (#485)
* Dynamic load ze_loader, remove link with ze_loader.{so/dll} Changed all zeXXX function calls to use ZE_FUNC(zeXXX) macro defined in src/levelzero/ze_loader.h * Added optional variable to specify level-zero commit. Used tag v1.21.6 for unitrace build, which is the current master. --------- Co-authored-by: Guy Zadicario <[email protected]>
1 parent a1ebf07 commit 7f2bdd9

File tree

13 files changed

+621
-189
lines changed

13 files changed

+621
-189
lines changed

build_utils/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ macro(GetLevelZeroHeaders TARGET)
626626
${L0_INC_PATH}/level_zero/zet_api.h
627627
${L0_INC_PATH}/level_zero/layers/zel_tracing_api.h
628628
${L0_INC_PATH}/level_zero/layers/zel_tracing_register_cb.h
629-
COMMAND "${PYTHON_EXECUTABLE}" "${PTI_CMAKE_MACRO_DIR}/get_ze_headers.py" ${L0_INC_PATH} ${CMAKE_BINARY_DIR})
629+
COMMAND "${PYTHON_EXECUTABLE}" "${PTI_CMAKE_MACRO_DIR}/get_ze_headers.py" ${L0_INC_PATH} ${CMAKE_BINARY_DIR} ${LEVEL_ZERO_COMMIT_ID})
630630
endif()
631631

632632
target_include_directories(${TARGET}

build_utils/get_ze_headers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import build_utils
55

66
url = "https://github.com/oneapi-src/level-zero.git"
7-
commit = "a4afcb39ee265e595d3f0aa57b25b5e845fb494c"
7+
default_commit = "a4afcb39ee265e595d3f0aa57b25b5e845fb494c"
88

99
def main():
1010
if len(sys.argv) < 3:
11-
print("Usage: python get_ze_headers.py <include_path> <build_path>")
11+
print("Usage: python get_ze_headers.py <include_path> <build_path> [<commit-sha>]")
1212
return
1313

1414
dst_path = sys.argv[1]
@@ -20,6 +20,12 @@ def main():
2020

2121
clone_path = sys.argv[2]
2222
clone_path = os.path.join(clone_path, "level-zero")
23+
24+
if len(sys.argv) > 3:
25+
commit = sys.argv[3]
26+
else:
27+
commit = default_commit
28+
2329
build_utils.clone(url, commit, clone_path)
2430

2531
src_path = os.path.join(clone_path, "include")

tools/unitrace/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ find_package(Xptifw REQUIRED)
102102
if (Xptifw_FOUND)
103103
target_include_directories(unitrace_tool PRIVATE "${Xptifw_INCLUDE_DIR}")
104104
if(WIN32)
105-
target_link_libraries(unitrace_tool PRIVATE ${Xptifw_LIBRARY} "${CMAKE_BINARY_DIR}/ze_loader.lib")
105+
target_link_libraries(unitrace_tool PRIVATE ${Xptifw_LIBRARY})
106106
else()
107107
target_link_libraries(unitrace_tool PRIVATE xptifw)
108108
endif()
@@ -131,7 +131,7 @@ FindOpenCLHeaders(unitrace_tool)
131131

132132
GetOpenCLTracingHeaders(unitrace_tool)
133133

134-
FindL0Library(unitrace_tool)
134+
set (LEVEL_ZERO_COMMIT_ID "v1.21.6")
135135
GetLevelZeroHeaders(unitrace_tool)
136136

137137
FindHeadersPath(unitrace_tool "${PROJECT_SOURCE_DIR}/scripts/gen_tracing_common_header.py" "common_header.gen" gen_common_header)
@@ -216,13 +216,9 @@ if(UNIX)
216216
target_link_libraries(unitrace pthread dl)
217217
endif()
218218

219-
FindL0Library(unitrace)
220219
GetLevelZeroHeaders(unitrace)
221220
GetGitCommitHash(unitrace "${PROJECT_SOURCE_DIR}/scripts/get_commit_hash.py" "unitrace_commit_hash.h" get_git_commit_hash_unitrace)
222221
GetGitCommitHash(unitrace_tool "${PROJECT_SOURCE_DIR}/scripts/get_commit_hash.py" "unitrace_tool_commit_hash.h" get_git_commit_hash_unitrace_tool)
223-
if(WIN32)
224-
target_link_libraries(unitrace "${CMAKE_BINARY_DIR}/ze_loader.lib")
225-
endif()
226222

227223
if(UNIX)
228224
add_custom_command(TARGET unitrace POST_BUILD COMMAND "rm" "-rf" "unitrace_commit_hash.h")

tools/unitrace/scripts/gen_l0_loader.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import sys
99
import re
1010

11-
def get_func_list(f, func_list):
12-
f.seek(0)
11+
def get_func_list(file_path, func_list):
12+
f = open(file_path, "rt")
1313
for line in f.readlines():
1414
if line.find("ZE_APICALL") != -1 and line.find("ze_pfn") != -1:
1515
items = line.split("ze_pfn")
@@ -18,18 +18,38 @@ def get_func_list(f, func_list):
1818
items = items[1].split("Cb_t")
1919
assert len(items) == 2
2020
func_list.append("ze" + items[0].strip())
21+
f.close()
2122

22-
def gen_loader(f, func_list):
23+
def get_api_func_list(file_path, func_list):
24+
f = open(file_path, "rt")
25+
api_line = False
26+
for line in f.readlines():
27+
if line.find("ZE_APICALL") != -1:
28+
api_line = True
29+
elif api_line:
30+
api_line = False
31+
items = line.split("(")
32+
assert len(items) == 2
33+
func_list.append(items[0].strip())
34+
f.close()
35+
36+
def gen_loader(f, register_func_list, api_func_list):
2337
f.write("public:\n");
24-
for func in func_list:
38+
for func in register_func_list:
2539
reg_fname = "zelTracer" + func[2:] + "RegisterCallback"
2640
f.write(" decltype(&" + reg_fname + ") " + reg_fname + "_ = nullptr;\n");
41+
f.write(" decltype(&" + func + ") " + func + "_ = nullptr;\n");
42+
for func in api_func_list:
43+
f.write(" decltype(&" + func + ") " + func + "_ = nullptr;\n");
2744

2845
f.write("private:\n");
2946
f.write(" void init() {\n");
30-
for func in func_list:
47+
for func in register_func_list:
3148
reg_fname = "zelTracer" + func[2:] + "RegisterCallback"
3249
f.write(" LEVEL_ZERO_LOADER_GET_SYMBOL(" + reg_fname + ");\n");
50+
f.write(" LEVEL_ZERO_LOADER_GET_SYMBOL(" + func + ");\n");
51+
for func in api_func_list:
52+
f.write(" LEVEL_ZERO_LOADER_GET_SYMBOL(" + func + ");\n");
3353
f.write("}\n");
3454

3555
def main():
@@ -47,22 +67,18 @@ def main():
4767

4868
dst_loader_file = open(dst_loader_file_path, "wt")
4969

50-
l0_path = sys.argv[2]
51-
l0_file_path = os.path.join(l0_path, "ze_api.h")
70+
register_func_list = []
71+
api_func_list = []
5272

53-
func_list = []
54-
55-
l0_file = open(l0_file_path, "rt")
56-
get_func_list(l0_file, func_list)
57-
58-
l0_exp_path = os.path.join(l0_path, "layers", "zel_tracing_register_cb.h")
59-
l0_exp_file = open(l0_exp_path, "rt")
60-
get_func_list(l0_exp_file, func_list)
73+
l0_path = sys.argv[2]
74+
get_func_list(os.path.join(l0_path, "ze_api.h"), register_func_list)
75+
get_func_list(os.path.join(l0_path, "layers", "zel_tracing_register_cb.h"), register_func_list)
76+
get_api_func_list(os.path.join(l0_path, "zet_api.h"), api_func_list)
77+
get_api_func_list(os.path.join(l0_path, "zes_api.h"), api_func_list)
78+
get_api_func_list(os.path.join(l0_path, "layers", "zel_tracing_api.h"),api_func_list)
6179

62-
gen_loader(dst_loader_file, func_list)
80+
gen_loader(dst_loader_file, register_func_list, api_func_list)
6381

64-
l0_exp_file.close()
65-
l0_file.close()
6682
dst_loader_file.close()
6783

6884
if __name__ == "__main__":

tools/unitrace/scripts/gen_tracing_callbacks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def gen_api(f, func_list, kfunc_list):
241241
gen_register(f, kfunc_list)
242242
f.write(" }\n")
243243
f.write("\n")
244-
f.write(" status = zelTracerSetEnabled(tracer, true);\n")
244+
f.write(" status = ZE_FUNC(zelTracerSetEnabled)(tracer, true);\n")
245245
f.write(" PTI_ASSERT(status == ZE_RESULT_SUCCESS);\n")
246246
f.write("}\n")
247247

@@ -721,7 +721,7 @@ def gen_exit_callback(f, func, submission_func_list, synchronize_func_list_on_en
721721
if func == "zeModuleCreate":
722722
f.write(" bool aot = (*(params->pdesc))->format; \n");
723723
f.write(" unsigned int kcount = 0; \n")
724-
f.write(" if (zeModuleGetKernelNames(**(params->pphModule), &kcount, NULL) == ZE_RESULT_SUCCESS) {\n")
724+
f.write(" if (ZE_FUNC(zeModuleGetKernelNames)(**(params->pphModule), &kcount, NULL) == ZE_RESULT_SUCCESS) {\n")
725725
f.write(" if (aot) { \n")
726726
f.write(" str += \"AOT (AOT_BINARY) \"; \n")
727727
f.write(" }\n")
@@ -739,7 +739,7 @@ def gen_exit_callback(f, func, submission_func_list, synchronize_func_list_on_en
739739
f.write(" q += 1024;\n")
740740
f.write(" }\n")
741741

742-
f.write(" if (zeModuleGetKernelNames(**(params->pphModule), &kcount, knames) == ZE_RESULT_SUCCESS) {\n")
742+
f.write(" if (ZE_FUNC(zeModuleGetKernelNames)(**(params->pphModule), &kcount, knames) == ZE_RESULT_SUCCESS) {\n")
743743
f.write(" for (int i = 0; i < kcount; i++) {\n")
744744
f.write(" str += \"Kernel #\" + std::to_string(i) + \": \" + knames[i] + \"\\n\";\n")
745745
f.write(" }\n")

0 commit comments

Comments
 (0)