Skip to content

Commit 83799e6

Browse files
authored
Merge pull request #879 from veselypeta/petr/level_zero
Move L0 Sources from intel/llvm to UR.
2 parents 88e71c0 + ffcba56 commit 83799e6

38 files changed

+15923
-34
lines changed

source/adapters/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ add_subdirectory(null)
3333
set(INTEL_LLVM_TAG nightly-2023-09-20)
3434

3535
if(UR_BUILD_ADAPTER_L0)
36-
set(SYCL_ADAPTER_DIR "${CMAKE_CURRENT_BINARY_DIR}/external/level_zero")
37-
FetchSource(https://github.com/intel/llvm.git ${INTEL_LLVM_TAG} "sycl/plugins/unified_runtime/ur" ${SYCL_ADAPTER_DIR})
3836
add_subdirectory(level_zero)
3937
endif()
4038

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: LLVM
4+
...

source/adapters/level_zero/CMakeLists.txt

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

6-
set(L0_DIR "${SYCL_ADAPTER_DIR}/sycl/plugins/unified_runtime/ur/adapters/level_zero")
76

87
set(TARGET_NAME ur_adapter_level_zero)
98

@@ -73,36 +72,36 @@ target_include_directories(LevelZeroLoader-Headers
7372

7473
add_ur_adapter(${TARGET_NAME}
7574
SHARED
76-
${L0_DIR}/ur_interface_loader.cpp
77-
${L0_DIR}/adapter.hpp
78-
${L0_DIR}/adapter.cpp
79-
${L0_DIR}/command_buffer.hpp
80-
${L0_DIR}/command_buffer.cpp
81-
${L0_DIR}/common.hpp
82-
${L0_DIR}/context.hpp
83-
${L0_DIR}/device.hpp
84-
${L0_DIR}/event.hpp
85-
${L0_DIR}/usm.hpp
86-
${L0_DIR}/memory.hpp
87-
${L0_DIR}/kernel.hpp
88-
${L0_DIR}/platform.hpp
89-
${L0_DIR}/program.hpp
90-
${L0_DIR}/queue.hpp
91-
${L0_DIR}/sampler.hpp
92-
${L0_DIR}/ur_level_zero.cpp
93-
${L0_DIR}/common.cpp
94-
${L0_DIR}/context.cpp
95-
${L0_DIR}/device.cpp
96-
${L0_DIR}/event.cpp
97-
${L0_DIR}/usm.cpp
98-
${L0_DIR}/usm_p2p.cpp
99-
${L0_DIR}/memory.cpp
100-
${L0_DIR}/kernel.cpp
101-
${L0_DIR}/platform.cpp
102-
${L0_DIR}/program.cpp
103-
${L0_DIR}/queue.cpp
104-
${L0_DIR}/sampler.cpp
105-
${L0_DIR}/../../ur.cpp
75+
${CMAKE_CURRENT_SOURCE_DIR}/ur_interface_loader.cpp
76+
${CMAKE_CURRENT_SOURCE_DIR}/adapter.hpp
77+
${CMAKE_CURRENT_SOURCE_DIR}/adapter.cpp
78+
${CMAKE_CURRENT_SOURCE_DIR}/command_buffer.hpp
79+
${CMAKE_CURRENT_SOURCE_DIR}/command_buffer.cpp
80+
${CMAKE_CURRENT_SOURCE_DIR}/common.hpp
81+
${CMAKE_CURRENT_SOURCE_DIR}/context.hpp
82+
${CMAKE_CURRENT_SOURCE_DIR}/device.hpp
83+
${CMAKE_CURRENT_SOURCE_DIR}/event.hpp
84+
${CMAKE_CURRENT_SOURCE_DIR}/usm.hpp
85+
${CMAKE_CURRENT_SOURCE_DIR}/memory.hpp
86+
${CMAKE_CURRENT_SOURCE_DIR}/kernel.hpp
87+
${CMAKE_CURRENT_SOURCE_DIR}/platform.hpp
88+
${CMAKE_CURRENT_SOURCE_DIR}/program.hpp
89+
${CMAKE_CURRENT_SOURCE_DIR}/queue.hpp
90+
${CMAKE_CURRENT_SOURCE_DIR}/sampler.hpp
91+
${CMAKE_CURRENT_SOURCE_DIR}/ur_level_zero.cpp
92+
${CMAKE_CURRENT_SOURCE_DIR}/common.cpp
93+
${CMAKE_CURRENT_SOURCE_DIR}/context.cpp
94+
${CMAKE_CURRENT_SOURCE_DIR}/device.cpp
95+
${CMAKE_CURRENT_SOURCE_DIR}/event.cpp
96+
${CMAKE_CURRENT_SOURCE_DIR}/usm.cpp
97+
${CMAKE_CURRENT_SOURCE_DIR}/usm_p2p.cpp
98+
${CMAKE_CURRENT_SOURCE_DIR}/memory.cpp
99+
${CMAKE_CURRENT_SOURCE_DIR}/kernel.cpp
100+
${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp
101+
${CMAKE_CURRENT_SOURCE_DIR}/program.cpp
102+
${CMAKE_CURRENT_SOURCE_DIR}/queue.cpp
103+
${CMAKE_CURRENT_SOURCE_DIR}/sampler.cpp
104+
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.cpp
106105
)
107106

108107
set_target_properties(${TARGET_NAME} PROPERTIES
@@ -119,6 +118,6 @@ target_link_libraries(${TARGET_NAME} PRIVATE
119118
)
120119

121120
target_include_directories(${TARGET_NAME} PRIVATE
122-
${L0_DIR}/../../../
121+
"${CMAKE_CURRENT_SOURCE_DIR}/../../"
123122
LevelZeroLoader-Headers
124123
)
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
//===--------- adapter.cpp - Level Zero Adapter ---------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "adapter.hpp"
10+
#include "ur_level_zero.hpp"
11+
12+
ur_adapter_handle_t_ Adapter{};
13+
14+
UR_APIEXPORT ur_result_t UR_APICALL
15+
urInit(ur_device_init_flags_t
16+
DeviceFlags, ///< [in] device initialization flags.
17+
///< must be 0 (default) or a combination of
18+
///< ::ur_device_init_flag_t.
19+
ur_loader_config_handle_t) {
20+
std::ignore = DeviceFlags;
21+
22+
return UR_RESULT_SUCCESS;
23+
}
24+
25+
ur_result_t adapterStateTeardown() {
26+
// reclaim ur_platform_handle_t objects here since we don't have
27+
// urPlatformRelease.
28+
for (ur_platform_handle_t Platform : *URPlatformsCache) {
29+
delete Platform;
30+
}
31+
delete URPlatformsCache;
32+
delete URPlatformsCacheMutex;
33+
34+
bool LeakFound = false;
35+
36+
// Print the balance of various create/destroy native calls.
37+
// The idea is to verify if the number of create(+) and destroy(-) calls are
38+
// matched.
39+
if (ZeCallCount && (UrL0Debug & UR_L0_DEBUG_CALL_COUNT) != 0) {
40+
// clang-format off
41+
//
42+
// The format of this table is such that each row accounts for a
43+
// specific type of objects, and all elements in the raw except the last
44+
// one are allocating objects of that type, while the last element is known
45+
// to deallocate objects of that type.
46+
//
47+
std::vector<std::vector<std::string>> CreateDestroySet = {
48+
{"zeContextCreate", "zeContextDestroy"},
49+
{"zeCommandQueueCreate", "zeCommandQueueDestroy"},
50+
{"zeModuleCreate", "zeModuleDestroy"},
51+
{"zeKernelCreate", "zeKernelDestroy"},
52+
{"zeEventPoolCreate", "zeEventPoolDestroy"},
53+
{"zeCommandListCreateImmediate", "zeCommandListCreate", "zeCommandListDestroy"},
54+
{"zeEventCreate", "zeEventDestroy"},
55+
{"zeFenceCreate", "zeFenceDestroy"},
56+
{"zeImageCreate", "zeImageDestroy"},
57+
{"zeSamplerCreate", "zeSamplerDestroy"},
58+
{"zeMemAllocDevice", "zeMemAllocHost", "zeMemAllocShared", "zeMemFree"},
59+
};
60+
61+
// A sample output aimed below is this:
62+
// ------------------------------------------------------------------------
63+
// zeContextCreate = 1 \---> zeContextDestroy = 1
64+
// zeCommandQueueCreate = 1 \---> zeCommandQueueDestroy = 1
65+
// zeModuleCreate = 1 \---> zeModuleDestroy = 1
66+
// zeKernelCreate = 1 \---> zeKernelDestroy = 1
67+
// zeEventPoolCreate = 1 \---> zeEventPoolDestroy = 1
68+
// zeCommandListCreateImmediate = 1 |
69+
// zeCommandListCreate = 1 \---> zeCommandListDestroy = 1 ---> LEAK = 1
70+
// zeEventCreate = 2 \---> zeEventDestroy = 2
71+
// zeFenceCreate = 1 \---> zeFenceDestroy = 1
72+
// zeImageCreate = 0 \---> zeImageDestroy = 0
73+
// zeSamplerCreate = 0 \---> zeSamplerDestroy = 0
74+
// zeMemAllocDevice = 0 |
75+
// zeMemAllocHost = 1 |
76+
// zeMemAllocShared = 0 \---> zeMemFree = 1
77+
//
78+
// clang-format on
79+
80+
fprintf(stderr, "ZE_DEBUG=%d: check balance of create/destroy calls\n",
81+
UR_L0_DEBUG_CALL_COUNT);
82+
fprintf(stderr,
83+
"----------------------------------------------------------\n");
84+
for (const auto &Row : CreateDestroySet) {
85+
int diff = 0;
86+
for (auto I = Row.begin(); I != Row.end();) {
87+
const char *ZeName = (*I).c_str();
88+
const auto &ZeCount = (*ZeCallCount)[*I];
89+
90+
bool First = (I == Row.begin());
91+
bool Last = (++I == Row.end());
92+
93+
if (Last) {
94+
fprintf(stderr, " \\--->");
95+
diff -= ZeCount;
96+
} else {
97+
diff += ZeCount;
98+
if (!First) {
99+
fprintf(stderr, " | \n");
100+
}
101+
}
102+
103+
fprintf(stderr, "%30s = %-5d", ZeName, ZeCount);
104+
}
105+
106+
if (diff) {
107+
LeakFound = true;
108+
fprintf(stderr, " ---> LEAK = %d", diff);
109+
}
110+
fprintf(stderr, "\n");
111+
}
112+
113+
ZeCallCount->clear();
114+
delete ZeCallCount;
115+
ZeCallCount = nullptr;
116+
}
117+
if (LeakFound)
118+
return UR_RESULT_ERROR_INVALID_MEM_OBJECT;
119+
120+
return UR_RESULT_SUCCESS;
121+
}
122+
123+
UR_APIEXPORT ur_result_t UR_APICALL urTearDown(
124+
void *Params ///< [in] pointer to tear down parameters
125+
) {
126+
std::ignore = Params;
127+
return UR_RESULT_SUCCESS;
128+
}
129+
130+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(
131+
uint32_t NumEntries, ///< [in] the number of platforms to be added to
132+
///< phAdapters. If phAdapters is not NULL, then
133+
///< NumEntries should be greater than zero, otherwise
134+
///< ::UR_RESULT_ERROR_INVALID_SIZE, will be returned.
135+
ur_adapter_handle_t
136+
*Adapters, ///< [out][optional][range(0, NumEntries)] array of handle of
137+
///< adapters. If NumEntries is less than the number of
138+
///< adapters available, then
139+
///< ::urAdapterGet shall only retrieve that number of
140+
///< platforms.
141+
uint32_t *NumAdapters ///< [out][optional] returns the total number of
142+
///< adapters available.
143+
) {
144+
if (NumEntries > 0 && Adapters) {
145+
std::lock_guard<std::mutex> Lock{Adapter.Mutex};
146+
// TODO: Some initialization that happens in urPlatformsGet could be moved
147+
// here for when RefCount reaches 1
148+
Adapter.RefCount++;
149+
*Adapters = &Adapter;
150+
}
151+
152+
if (NumAdapters) {
153+
*NumAdapters = 1;
154+
}
155+
156+
return UR_RESULT_SUCCESS;
157+
}
158+
159+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
160+
std::lock_guard<std::mutex> Lock{Adapter.Mutex};
161+
if (--Adapter.RefCount == 0) {
162+
return adapterStateTeardown();
163+
}
164+
165+
return UR_RESULT_SUCCESS;
166+
}
167+
168+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
169+
std::lock_guard<std::mutex> Lock{Adapter.Mutex};
170+
Adapter.RefCount++;
171+
172+
return UR_RESULT_SUCCESS;
173+
}
174+
175+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetLastError(
176+
ur_adapter_handle_t Adapter, ///< [in] handle of the platform instance
177+
const char **Message, ///< [out] pointer to a C string where the adapter
178+
///< specific error message will be stored.
179+
int32_t *Error ///< [out] pointer to an integer where the adapter specific
180+
///< error code will be stored.
181+
) {
182+
std::ignore = Adapter;
183+
std::ignore = Message;
184+
std::ignore = Error;
185+
urPrint("[UR][L0] %s function not implemented!\n", __FUNCTION__);
186+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
187+
}
188+
189+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t,
190+
ur_adapter_info_t PropName,
191+
size_t PropSize,
192+
void *PropValue,
193+
size_t *PropSizeRet) {
194+
UrReturnHelper ReturnValue(PropSize, PropValue, PropSizeRet);
195+
196+
switch (PropName) {
197+
case UR_ADAPTER_INFO_BACKEND:
198+
return ReturnValue(UR_ADAPTER_BACKEND_LEVEL_ZERO);
199+
case UR_ADAPTER_INFO_REFERENCE_COUNT:
200+
return ReturnValue(Adapter.RefCount.load());
201+
default:
202+
return UR_RESULT_ERROR_INVALID_ENUMERATION;
203+
}
204+
205+
return UR_RESULT_SUCCESS;
206+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===--------- adapters.hpp - Level Zero Adapter --------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <atomic>
10+
#include <mutex>
11+
12+
struct ur_adapter_handle_t_ {
13+
std::atomic<uint32_t> RefCount = 0;
14+
std::mutex Mutex;
15+
};
16+
17+
extern ur_adapter_handle_t_ Adapter;

0 commit comments

Comments
 (0)