Skip to content

Commit 8c9dd7e

Browse files
authored
Merge pull request #1820 from pbalcer/static-linking
Add support for static linking of the L0 adapter
2 parents a99dbce + 3abe18c commit 8c9dd7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3024
-1722
lines changed

.github/workflows/build-hw-reusable.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ on:
1818
required: false
1919
type: string
2020
default: OFF
21+
static_adapter:
22+
required: false
23+
type: string
24+
default: OFF
2125

2226
permissions:
2327
contents: read
@@ -36,7 +40,7 @@ jobs:
3640
strategy:
3741
matrix:
3842
adapter: [
39-
{name: "${{inputs.adapter_name}}", platform: "${{inputs.platform}}", static_Loader: "${{inputs.static_loader}}"},
43+
{name: "${{inputs.adapter_name}}", platform: "${{inputs.platform}}", static_Loader: "${{inputs.static_loader}}", static_adapter: "${{inputs.static_loader}}"},
4044
]
4145
build_type: [Debug, Release]
4246
compiler: [{c: gcc, cxx: g++}, {c: clang, cxx: clang++}]
@@ -49,6 +53,10 @@ jobs:
4953
build_type: Release
5054
- adapter: {static_Loader: ON}
5155
compiler: {c: clang, cxx: clang++}
56+
- adapter: {static_adapter: ON}
57+
build_type: Release
58+
- adapter: {static_adapter: ON}
59+
compiler: {c: clang, cxx: clang++}
5260

5361
runs-on: ${{inputs.runner_name}}
5462

@@ -76,6 +84,7 @@ jobs:
7684
-DUR_BUILD_TESTS=ON
7785
-DUR_BUILD_ADAPTER_${{matrix.adapter.name}}=ON
7886
-DUR_STATIC_LOADER=${{matrix.adapter.static_Loader}}
87+
-DUR_STATIC_ADAPTER_${{matrix.adapter.name}}=${{matrix.adapter.static_adapter}}
7988
-DUR_DPCXX=${{github.workspace}}/dpcpp_compiler/bin/clang++
8089
-DUR_SYCL_LIBRARY_DIR=${{github.workspace}}/dpcpp_compiler/lib
8190
${{ matrix.adapter.name == 'HIP' && '-DUR_CONFORMANCE_AMD_ARCH=gfx1030' || '' }}

.github/workflows/cmake.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ jobs:
155155
adapter_name: L0
156156
runner_name: L0
157157
static_loader: ON
158+
static_adapter: ON
158159

159160
opencl:
160161
name: OpenCL
@@ -216,7 +217,8 @@ jobs:
216217
os: ['windows-2019', 'windows-2022']
217218
adapter: [
218219
{name: None, var: ''}, {name: L0, var: '-DUR_BUILD_ADAPTER_L0=ON'},
219-
{name: None, var: ''}, {name: L0_V2, var: '-DUR_BUILD_ADAPTER_L0_V2=ON'}
220+
{name: None, var: ''}, {name: L0_V2, var: '-DUR_BUILD_ADAPTER_L0_V2=ON'},
221+
{name: L0, var: '-DUR_BUILD_ADAPTER_L0=ON -DUR_STATIC_ADAPTER_L0=ON'}
220222
]
221223

222224
# TODO: building level zero loader on windows-2019 and clang-cl is currently broken
@@ -225,16 +227,25 @@ jobs:
225227
adapter: {name: L0, var: '-DUR_BUILD_ADAPTER_L0=ON'}
226228
- os: 'windows-2019'
227229
adapter: {name: L0_V2, var: '-DUR_BUILD_ADAPTER_L0_V2=ON'}
230+
- os: 'windows-2019'
231+
adapter: {name: L0, var: '-DUR_BUILD_ADAPTER_L0=ON -DUR_STATIC_ADAPTER_L0=ON'}
228232
- adapter: {name: L0, var: '-DUR_BUILD_ADAPTER_L0=ON'}
229233
compiler: {c: clang-cl, cxx: clang-cl}
230234
- adapter: {name: L0_V2, var: '-DUR_BUILD_ADAPTER_L0_V2=ON'}
231235
compiler: {c: clang-cl, cxx: clang-cl}
236+
- adapter: {name: L0, var: '-DUR_BUILD_ADAPTER_L0=ON -DUR_STATIC_ADAPTER_L0=ON'}
237+
compiler: {c: clang-cl, cxx: clang-cl}
232238

233239
build_type: [Debug, Release]
234240
compiler: [{c: cl, cxx: cl}, {c: clang-cl, cxx: clang-cl}]
235241
include:
236242
- compiler: {c: clang-cl, cxx: clang-cl}
237243
toolset: "-T ClangCL"
244+
- os: 'windows-2022'
245+
adapter: {name: L0, var: '-DUR_BUILD_ADAPTER_L0=ON -DUR_STATIC_ADAPTER_L0=ON'}
246+
build_type: 'Release'
247+
compiler: {c: cl, cxx: cl}
248+
238249
runs-on: ${{matrix.os}}
239250

240251
steps:

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ option(UR_BUILD_ADAPTER_HIP "Build the HIP adapter" OFF)
5252
option(UR_BUILD_ADAPTER_NATIVE_CPU "Build the Native-CPU adapter" OFF)
5353
option(UR_BUILD_ADAPTER_ALL "Build all currently supported adapters" OFF)
5454
option(UR_BUILD_ADAPTER_L0_V2 "Build the (experimental) Level-Zero v2 adapter" OFF)
55+
option(UR_STATIC_ADAPTER_L0 "Build the Level-Zero adapter as static and embed in the loader" OFF)
5556
option(UR_BUILD_EXAMPLE_CODEGEN "Build the codegen example." OFF)
5657
option(VAL_USE_LIBBACKTRACE_BACKTRACE "enable libbacktrace validation backtrace for linux" OFF)
5758
option(UR_ENABLE_ASSERTIONS "Enable assertions for all build types" OFF)

scripts/generate_code.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,32 @@ def generate_loader(path, section, namespace, tags, version, specs, meta):
400400
)
401401
print("Generated %s lines of code.\n"%loc)
402402

403+
"""
404+
generates c/c++ files from the specification documents
405+
"""
406+
def _mako_interface_loader_api(path, adapter, ext, namespace, tags, version, specs, meta):
407+
dstpath = os.path.join(path, adapter)
408+
os.makedirs(dstpath, exist_ok=True)
409+
410+
template = f"ur_interface_loader.{ext}.mako"
411+
fin = os.path.join(templates_dir, template)
412+
413+
name = f"ur_interface_loader"
414+
415+
filename = f"{name}.{ext}"
416+
fout = os.path.join(dstpath, filename)
417+
418+
print("Generating %s..."%fout)
419+
return util.makoWrite(
420+
fin, fout,
421+
name=name,
422+
adapter=adapter,
423+
ver=version,
424+
namespace=namespace,
425+
tags=tags,
426+
specs=specs,
427+
meta=meta,)
428+
403429
"""
404430
Entry-point:
405431
generates adapter for unified_runtime
@@ -416,6 +442,10 @@ def generate_adapters(path, section, namespace, tags, version, specs, meta):
416442
loc += _mako_linker_scripts(
417443
dstpath, "adapter", "def", namespace, tags, version, specs, meta
418444
)
445+
446+
loc += _mako_interface_loader_api(dstpath, "level_zero", "cpp", namespace, tags, version, specs, meta)
447+
loc += _mako_interface_loader_api(dstpath, "level_zero", "hpp", namespace, tags, version, specs, meta)
448+
419449
print("Generated %s lines of code.\n"%loc)
420450

421451
"""

scripts/templates/ldrddi.cpp.mako

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ ${tbl['export']['name']}(
365365
// Load the device-platform DDI tables
366366
for( auto& platform : ur_loader::getContext()->platforms )
367367
{
368+
// statically linked adapter inside of the loader
369+
if (platform.handle == nullptr)
370+
continue;
371+
368372
if(platform.initStatus != ${X}_RESULT_SUCCESS)
369373
continue;
370374
auto getTable = reinterpret_cast<${tbl['pfn']}>(

scripts/templates/queue_api.cpp.mako

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ from templates import helper as th
2424
ur_queue_handle_t_::~ur_queue_handle_t_() {}
2525

2626
## FUNCTION ###################################################################
27+
namespace ${x}::level_zero {
2728
%for obj in th.get_queue_related_functions(specs, n, tags):
28-
${X}_APIEXPORT ${x}_result_t ${X}_APICALL
29+
${x}_result_t
2930
${th.make_func_name(n, tags, obj)}(
3031
%for line in th.make_param_lines(n, tags, obj, format=["name", "type", "delim"]):
3132
${line}
@@ -35,3 +36,4 @@ ${th.make_func_name(n, tags, obj)}(
3536
return ${obj['params'][0]['name']}->${th.transform_queue_related_function_name(n, tags, obj, format=["name"])};
3637
}
3738
%endfor
39+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<%!
2+
import re
3+
from templates import helper as th
4+
%><%
5+
n=namespace
6+
N=n.upper()
7+
8+
x=tags['$x']
9+
X=x.upper()
10+
Adapter=adapter.upper()
11+
%>//===--------- ${n}_interface_loader.cpp - Level Zero Adapter ------------===//
12+
//
13+
// Copyright (C) 2024 Intel Corporation
14+
//
15+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
16+
// Exceptions. See LICENSE.TXT
17+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
18+
//
19+
//===----------------------------------------------------------------------===//
20+
#include <${n}_api.h>
21+
#include <${n}_ddi.h>
22+
23+
#include "ur_interface_loader.hpp"
24+
25+
static ur_result_t validateProcInputs(ur_api_version_t version, void *pDdiTable) {
26+
if (nullptr == pDdiTable) {
27+
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
28+
}
29+
// Pre 1.0 we enforce loader and adapter must have same version.
30+
// Post 1.0 only major version match should be required.
31+
if (version != UR_API_VERSION_CURRENT) {
32+
return UR_RESULT_ERROR_UNSUPPORTED_VERSION;
33+
}
34+
return UR_RESULT_SUCCESS;
35+
}
36+
37+
#ifdef UR_STATIC_ADAPTER_${Adapter}
38+
namespace ${n}::${adapter} {
39+
#elif defined(__cplusplus)
40+
extern "C" {
41+
#endif
42+
43+
%for tbl in th.get_pfntables(specs, meta, n, tags):
44+
${X}_APIEXPORT ${x}_result_t ${X}_APICALL ${tbl['export']['name']}(
45+
%for line in th.make_param_lines(n, tags, tbl['export'], format=["type", "name", "delim"]):
46+
${line}
47+
%endfor
48+
)
49+
{
50+
auto result = validateProcInputs(version, pDdiTable);
51+
if (UR_RESULT_SUCCESS != result) {
52+
return result;
53+
}
54+
55+
%for obj in tbl['functions']:
56+
pDdiTable->${th.append_ws(th.make_pfn_name(n, tags, obj), 43)} = ${n}::${adapter}::${th.make_func_name(n, tags, obj)};
57+
%endfor
58+
59+
return result;
60+
}
61+
62+
%endfor
63+
64+
#ifdef UR_STATIC_ADAPTER_${Adapter}
65+
} // namespace ur::${adapter}
66+
#elif defined(__cplusplus)
67+
} // extern "C"
68+
#endif
69+
70+
#ifdef UR_STATIC_ADAPTER_${Adapter}
71+
namespace ur::${adapter} {
72+
ur_result_t urAdapterGetDdiTables(ur_dditable_t *ddi) {
73+
if (ddi == nullptr) {
74+
return UR_RESULT_ERROR_INVALID_NULL_POINTER;
75+
}
76+
77+
ur_result_t result;
78+
79+
%for tbl in th.get_pfntables(specs, meta, n, tags):
80+
result = ${n}::${adapter}::${tbl['export']['name']}( ${X}_API_VERSION_CURRENT, &ddi->${tbl['name']} );
81+
if (result != UR_RESULT_SUCCESS)
82+
return result;
83+
%endfor
84+
85+
return result;
86+
}
87+
}
88+
#endif
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<%!
2+
import re
3+
from templates import helper as th
4+
%><%
5+
n=namespace
6+
N=n.upper()
7+
8+
x=tags['$x']
9+
X=x.upper()
10+
Adapter=adapter.upper()
11+
%>//===--------- ${n}_interface_loader.hpp - Level Zero Adapter ------------===//
12+
//
13+
// Copyright (C) 2024 Intel Corporation
14+
//
15+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
16+
// Exceptions. See LICENSE.TXT
17+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
18+
//
19+
//===----------------------------------------------------------------------===//
20+
#include <${n}_api.h>
21+
#include <${n}_ddi.h>
22+
23+
namespace ${n}::${adapter} {
24+
%for s in specs:
25+
%for obj in th.filter_items(s['objects'], 'type', 'function'):
26+
%if not th.obj_traits.is_loader_only(obj):
27+
${x}_result_t ${th.make_func_name(n, tags, obj)}(
28+
%for line in th.make_param_lines(n, tags, obj, format=["type", "name", "delim"]):
29+
${line}
30+
%endfor
31+
);
32+
%endif
33+
%endfor
34+
%endfor
35+
#ifdef UR_STATIC_ADAPTER_LEVEL_ZERO
36+
ur_result_t urAdapterGetDdiTables(ur_dditable_t *ddi);
37+
#endif
38+
}

source/adapters/level_zero/CMakeLists.txt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,33 @@ if (NOT DEFINED LEVEL_ZERO_LIBRARY OR NOT DEFINED LEVEL_ZERO_INCLUDE_DIR)
7373
${level-zero-loader_SOURCE_DIR}/include CACHE PATH "Path to Level Zero Headers")
7474
endif()
7575

76-
add_library (LevelZeroLoader INTERFACE)
76+
add_library(LevelZeroLoader INTERFACE)
7777
# The MSVC linker does not like / at the start of a path, so to work around this
7878
# we split it into a link library and a library path, where the path is allowed
7979
# to have leading /.
8080
get_filename_component(LEVEL_ZERO_LIBRARY_SRC "${LEVEL_ZERO_LIBRARY}" DIRECTORY)
8181
get_filename_component(LEVEL_ZERO_LIB_NAME "${LEVEL_ZERO_LIBRARY}" NAME)
8282
target_link_directories(LevelZeroLoader
83-
INTERFACE "${LEVEL_ZERO_LIBRARY_SRC}"
83+
INTERFACE "$<BUILD_INTERFACE:${LEVEL_ZERO_LIBRARY_SRC}>"
84+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
8485
)
8586
target_link_libraries(LevelZeroLoader
8687
INTERFACE "${LEVEL_ZERO_LIB_NAME}"
8788
)
8889

89-
add_library (LevelZeroLoader-Headers INTERFACE)
90+
add_library(LevelZeroLoader-Headers INTERFACE)
9091
target_include_directories(LevelZeroLoader-Headers
91-
INTERFACE "${LEVEL_ZERO_INCLUDE_DIR}"
92+
INTERFACE "$<BUILD_INTERFACE:${LEVEL_ZERO_INCLUDE_DIR}>"
93+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
9294
)
9395

9496
if(UR_BUILD_ADAPTER_L0)
95-
add_ur_adapter(ur_adapter_level_zero
96-
SHARED
97+
set(ADAPTER_LIB_TYPE SHARED)
98+
if(UR_STATIC_ADAPTER_L0)
99+
set(ADAPTER_LIB_TYPE STATIC)
100+
endif()
101+
102+
add_ur_adapter(ur_adapter_level_zero ${ADAPTER_LIB_TYPE}
97103
${CMAKE_CURRENT_SOURCE_DIR}/ur_interface_loader.cpp
98104
${CMAKE_CURRENT_SOURCE_DIR}/adapter.hpp
99105
${CMAKE_CURRENT_SOURCE_DIR}/adapter.cpp
@@ -133,6 +139,20 @@ if(UR_BUILD_ADAPTER_L0)
133139
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.cpp
134140
)
135141

142+
if(UR_STATIC_ADAPTER_L0)
143+
target_compile_definitions(ur_adapter_level_zero PUBLIC UR_STATIC_ADAPTER_LEVEL_ZERO)
144+
145+
# 'utils' target from 'level-zero-loader' includes path which is prefixed
146+
# in the source directory, this breaks the installation of 'utils' target.
147+
set_target_properties(utils PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "")
148+
install(TARGETS ur_adapter_level_zero ur_umf LevelZeroLoader LevelZeroLoader-Headers ze_loader utils
149+
EXPORT ${PROJECT_NAME}-targets
150+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
151+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
152+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
153+
)
154+
endif()
155+
136156
if(NOT WIN32)
137157
target_sources(ur_adapter_level_zero
138158
PRIVATE
@@ -152,7 +172,7 @@ if(UR_BUILD_ADAPTER_L0)
152172

153173
if (WIN32)
154174
# 0x800: Search for the DLL only in the System32 folder
155-
target_link_options(ur_adapter_level_zero PUBLIC /DEPENDENTLOADFLAG:0x800)
175+
target_link_options(ur_adapter_level_zero PRIVATE /DEPENDENTLOADFLAG:0x800)
156176
endif()
157177

158178
target_link_libraries(ur_adapter_level_zero PRIVATE

0 commit comments

Comments
 (0)