Skip to content

Commit 9de0818

Browse files
KFilipekkswiecicki
authored andcommitted
[UR][L0v2] Add initial record & replay implementation
1 parent 726f8d2 commit 9de0818

File tree

8 files changed

+372
-15
lines changed

8 files changed

+372
-15
lines changed

unified-runtime/source/adapters/level_zero/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ if(UR_BUILD_ADAPTER_L0_V2)
149149
${CMAKE_CURRENT_SOURCE_DIR}/physical_mem.cpp
150150
${CMAKE_CURRENT_SOURCE_DIR}/program.cpp
151151
${CMAKE_CURRENT_SOURCE_DIR}/helpers/kernel_helpers.cpp
152-
${CMAKE_CURRENT_SOURCE_DIR}/graph.cpp
153152
${CMAKE_CURRENT_SOURCE_DIR}/helpers/memory_helpers.cpp
154153
${CMAKE_CURRENT_SOURCE_DIR}/helpers/mutable_helpers.cpp
155154
${CMAKE_CURRENT_SOURCE_DIR}/usm_p2p.cpp
@@ -169,6 +168,7 @@ if(UR_BUILD_ADAPTER_L0_V2)
169168
${CMAKE_CURRENT_SOURCE_DIR}/v2/event_provider_normal.hpp
170169
${CMAKE_CURRENT_SOURCE_DIR}/v2/event_provider.hpp
171170
${CMAKE_CURRENT_SOURCE_DIR}/v2/event.hpp
171+
${CMAKE_CURRENT_SOURCE_DIR}/v2/graph.cpp
172172
${CMAKE_CURRENT_SOURCE_DIR}/v2/kernel.hpp
173173
${CMAKE_CURRENT_SOURCE_DIR}/v2/memory.hpp
174174
${CMAKE_CURRENT_SOURCE_DIR}/v2/lockable.hpp

unified-runtime/source/adapters/level_zero/platform.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,42 @@ ur_result_t ur_platform_handle_t_::initialize() {
526526
ZeMemGetPitchFor2dImageExt.Supported =
527527
ZeMemGetPitchFor2dImageExt.zeMemGetPitchFor2dImage != nullptr;
528528

529+
// Populate Graph Extension structure.
530+
std::unordered_map<std::string, void **> ZeGraphFuncNameToAddrMap = {
531+
{"zeGraphCreateExp",
532+
reinterpret_cast<void **>(&ZeGraphExt.zeGraphCreateExp)},
533+
{"zeCommandListBeginGraphCaptureExp",
534+
reinterpret_cast<void **>(
535+
&ZeGraphExt.zeCommandListBeginGraphCaptureExp)},
536+
{"zeCommandListBeginCaptureIntoGraphExp",
537+
reinterpret_cast<void **>(
538+
&ZeGraphExt.zeCommandListBeginCaptureIntoGraphExp)},
539+
{"zeCommandListEndGraphCaptureExp",
540+
reinterpret_cast<void **>(&ZeGraphExt.zeCommandListEndGraphCaptureExp)},
541+
{"zeCommandListInstantiateGraphExp",
542+
reinterpret_cast<void **>(&ZeGraphExt.zeCommandListInstantiateGraphExp)},
543+
{"zeCommandListAppendGraphExp",
544+
reinterpret_cast<void **>(&ZeGraphExt.zeCommandListAppendGraphExp)},
545+
{"zeGraphDestroyExp",
546+
reinterpret_cast<void **>(&ZeGraphExt.zeGraphDestroyExp)},
547+
{"zeExecutableGraphDestroyExp",
548+
reinterpret_cast<void **>(&ZeGraphExt.zeExecutableGraphDestroyExp)},
549+
{"zeCommandListIsGraphCaptureEnabledExp",
550+
reinterpret_cast<void **>(
551+
&ZeGraphExt.zeCommandListIsGraphCaptureEnabledExp)},
552+
{"zeGraphIsEmptyExp",
553+
reinterpret_cast<void **>(&ZeGraphExt.zeGraphIsEmptyExp)},
554+
{"zeGraphDumpContentsExp",
555+
reinterpret_cast<void **>(&ZeGraphExt.zeGraphDumpContentsExp)},
556+
};
557+
558+
ZeGraphExt.Supported = true;
559+
for (auto &[funcName, funcAddr] : ZeGraphFuncNameToAddrMap) {
560+
ZE_CALL_NOCHECK(zeDriverGetExtensionFunctionAddress,
561+
(ZeDriver, funcName.c_str(), funcAddr));
562+
ZeGraphExt.Supported &= (*funcAddr != nullptr);
563+
}
564+
529565
if (this->isDriverVersionNewerOrSimilar(1, 14, 36035)) {
530566
ZeCommandListAppendLaunchKernelWithArgumentsExt.Supported = true;
531567
} else {

unified-runtime/source/adapters/level_zero/platform.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#pragma once
1111

1212
#include "common.hpp"
13+
#include "level_zero/driver_experimental/zex_api.h"
1314
#include "ur_api.h"
1415
#include "ze_api.h"
1516
#include "ze_ddi.h"
@@ -168,4 +169,34 @@ struct ur_platform_handle_t_ : ur::handle_base<ur::level_zero::ddi_getter>,
168169
bool DriverSupportsCooperativeKernelLaunchWithArgs = false;
169170
bool DisableZeLaunchKernelWithArgs = false;
170171
} ZeCommandListAppendLaunchKernelWithArgumentsExt;
172+
173+
struct ZeGraphExtension {
174+
bool Supported = false;
175+
ze_result_t (*zeGraphCreateExp)(ze_context_handle_t hContext,
176+
ze_graph_handle_t *phGraph, void *pNext);
177+
ze_result_t (*zeCommandListBeginGraphCaptureExp)(
178+
ze_command_list_handle_t hCommandList, void *pNext);
179+
ze_result_t (*zeCommandListBeginCaptureIntoGraphExp)(
180+
ze_command_list_handle_t hCommandList, ze_graph_handle_t hGraph,
181+
void *pNext);
182+
ze_result_t (*zeCommandListEndGraphCaptureExp)(
183+
ze_command_list_handle_t hCommandList, ze_graph_handle_t *phGraph,
184+
void *pNext);
185+
ze_result_t (*zeCommandListInstantiateGraphExp)(
186+
ze_graph_handle_t hGraph,
187+
ze_executable_graph_handle_t *phExecutableGraph, void *pNext);
188+
ze_result_t (*zeCommandListAppendGraphExp)(
189+
ze_command_list_handle_t hCommandList,
190+
ze_executable_graph_handle_t hGraph, void *pNext,
191+
ze_event_handle_t hSignalEvent, uint32_t numWaitEvents,
192+
ze_event_handle_t *phWaitEvents);
193+
ze_result_t (*zeGraphDestroyExp)(ze_graph_handle_t hGraph);
194+
ze_result_t (*zeExecutableGraphDestroyExp)(
195+
ze_executable_graph_handle_t hGraph);
196+
ze_result_t (*zeCommandListIsGraphCaptureEnabledExp)(
197+
ze_command_list_handle_t hCommandList);
198+
ze_result_t (*zeGraphIsEmptyExp)(ze_graph_handle_t hGraph);
199+
ze_result_t (*zeGraphDumpContentsExp)(ze_graph_handle_t hGraph,
200+
const char *filePath, void *pNext);
201+
} ZeGraphExt;
171202
};

unified-runtime/source/adapters/level_zero/v2/command_list_manager.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "command_buffer.hpp"
1717
#include "common.hpp"
1818
#include "context.hpp"
19+
#include "graph.hpp"
1920
#include "kernel.hpp"
2021
#include "memory.hpp"
2122

@@ -1302,3 +1303,78 @@ ur_result_t ur_command_list_manager::appendKernelLaunchWithArgsExp(
13021303

13031304
return UR_RESULT_SUCCESS;
13041305
}
1306+
1307+
ur_result_t ur_command_list_manager::beginGraphCapture() {
1308+
if (!checkGraphExtensionSupport(hContext.get())) {
1309+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
1310+
}
1311+
1312+
ZE2UR_CALL(hContext.get()
1313+
->getPlatform()
1314+
->ZeGraphExt.zeCommandListBeginGraphCaptureExp,
1315+
(getZeCommandList(), nullptr));
1316+
graphCapture.enableCapture();
1317+
1318+
return UR_RESULT_SUCCESS;
1319+
}
1320+
1321+
ur_result_t
1322+
ur_command_list_manager::beginCaptureIntoGraph(ur_exp_graph_handle_t hGraph) {
1323+
if (!checkGraphExtensionSupport(hContext.get())) {
1324+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
1325+
}
1326+
1327+
ZE2UR_CALL(hContext.get()
1328+
->getPlatform()
1329+
->ZeGraphExt.zeCommandListBeginCaptureIntoGraphExp,
1330+
(getZeCommandList(), hGraph->getZeHandle(), nullptr));
1331+
graphCapture.enableCapture(hGraph);
1332+
1333+
return UR_RESULT_SUCCESS;
1334+
}
1335+
1336+
ur_result_t
1337+
ur_command_list_manager::endGraphCapture(ur_exp_graph_handle_t *phGraph) {
1338+
if (!checkGraphExtensionSupport(hContext.get())) {
1339+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
1340+
}
1341+
1342+
ze_graph_handle_t zeGraph = nullptr;
1343+
ZE2UR_CALL(
1344+
hContext.get()->getPlatform()->ZeGraphExt.zeCommandListEndGraphCaptureExp,
1345+
(getZeCommandList(), &zeGraph, nullptr));
1346+
auto graph = graphCapture.getGraph();
1347+
graphCapture.disableCapture();
1348+
1349+
*phGraph =
1350+
graph ? graph : new ur_exp_graph_handle_t_(hContext.get(), zeGraph);
1351+
1352+
return UR_RESULT_SUCCESS;
1353+
}
1354+
1355+
ur_result_t
1356+
ur_command_list_manager::appendGraph(ur_exp_executable_graph_handle_t hGraph,
1357+
wait_list_view &waitListView,
1358+
ur_event_handle_t hEvent) {
1359+
if (!checkGraphExtensionSupport(hContext.get())) {
1360+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
1361+
}
1362+
1363+
auto zeSignalEvent = getSignalEvent(hEvent, UR_COMMAND_ENQUEUE_GRAPH_EXP);
1364+
ZE2UR_CALL(
1365+
hContext.get()->getPlatform()->ZeGraphExt.zeCommandListAppendGraphExp,
1366+
(getZeCommandList(), hGraph->getZeHandle(), nullptr, zeSignalEvent,
1367+
waitListView.num, waitListView.handles));
1368+
1369+
return UR_RESULT_SUCCESS;
1370+
}
1371+
1372+
ur_result_t ur_command_list_manager::isGraphCaptureActive(bool *pResult) {
1373+
if (!checkGraphExtensionSupport(hContext.get())) {
1374+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
1375+
}
1376+
1377+
*pResult = graphCapture.isActive();
1378+
1379+
return UR_RESULT_SUCCESS;
1380+
}

unified-runtime/source/adapters/level_zero/v2/command_list_manager.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ struct wait_list_view {
4545
}
4646
};
4747

48+
struct graph_capture_tracking_data_t {
49+
void enableCapture(ur_exp_graph_handle_t graph) {
50+
capturedGraph = graph;
51+
enableCapture();
52+
}
53+
void enableCapture() { active = true; }
54+
void disableCapture() {
55+
active = false;
56+
capturedGraph = nullptr;
57+
}
58+
59+
ur_exp_graph_handle_t getGraph() const { return capturedGraph; }
60+
bool isActive() const { return active; }
61+
62+
private:
63+
bool active = false;
64+
ur_exp_graph_handle_t capturedGraph = nullptr;
65+
};
66+
4867
struct ur_command_list_manager {
4968
ur_command_list_manager(ur_context_handle_t context,
5069
ur_device_handle_t device,
@@ -227,6 +246,15 @@ struct ur_command_list_manager {
227246
const ur_kernel_launch_ext_properties_t *launchPropList,
228247
wait_list_view &waitListView, ur_event_handle_t phEvent);
229248

249+
/************ Graph methods *************/
250+
ur_result_t appendGraph(ur_exp_executable_graph_handle_t hGraph,
251+
wait_list_view &waitListView,
252+
ur_event_handle_t hEvent);
253+
ur_result_t beginGraphCapture();
254+
ur_result_t beginCaptureIntoGraph(ur_exp_graph_handle_t hGraph);
255+
ur_result_t endGraphCapture(ur_exp_graph_handle_t *phGraph);
256+
ur_result_t isGraphCaptureActive(bool *pResult);
257+
230258
v2::raii::command_list_unique_handle &&releaseCommandList();
231259

232260
void replaceCommandList(v2::raii::command_list_unique_handle &&cmdlist);
@@ -301,4 +329,6 @@ struct ur_command_list_manager {
301329
std::vector<ur_kernel_handle_t> submittedKernels;
302330
v2::raii::command_list_unique_handle zeCommandList;
303331
std::vector<ze_event_handle_t> waitList;
332+
333+
graph_capture_tracking_data_t graphCapture;
304334
};
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//===--------- graph.cpp - Level Zero Adapter -----------------------------===//
2+
//
3+
// Copyright (C) 2025 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
#include "graph.hpp"
12+
#include "../ur_interface_loader.hpp"
13+
#include "common.hpp"
14+
#include "context.hpp"
15+
#include "level_zero/driver_experimental/zex_api.h"
16+
17+
ur_exp_graph_handle_t_::ur_exp_graph_handle_t_(ur_context_handle_t hContext)
18+
: hContext(hContext) {
19+
ZE2UR_CALL_THROWS(hContext->getPlatform()->ZeGraphExt.zeGraphCreateExp,
20+
(hContext->getZeHandle(), &zeGraph, nullptr));
21+
}
22+
23+
ur_exp_graph_handle_t_::ur_exp_graph_handle_t_(ur_context_handle_t hContext,
24+
ze_graph_handle_t zeGraph)
25+
: hContext(hContext), zeGraph(zeGraph) {}
26+
27+
ur_exp_graph_handle_t_::~ur_exp_graph_handle_t_() {
28+
if (zeGraph) {
29+
ze_result_t ZeResult = ZE_CALL_NOCHECK(
30+
hContext->getPlatform()->ZeGraphExt.zeGraphDestroyExp, (zeGraph));
31+
if (ZeResult != ZE_RESULT_SUCCESS) {
32+
UR_LOG(WARN, "Failed to destroy graph handle: {}", ZeResult);
33+
}
34+
}
35+
}
36+
37+
ur_exp_executable_graph_handle_t_::ur_exp_executable_graph_handle_t_(
38+
ur_context_handle_t hContext, ur_exp_graph_handle_t hGraph)
39+
: hContext(hContext) {
40+
ZE2UR_CALL_THROWS(
41+
hContext->getPlatform()->ZeGraphExt.zeCommandListInstantiateGraphExp,
42+
(hGraph->getZeHandle(), &zeExGraph, nullptr));
43+
}
44+
45+
ur_exp_executable_graph_handle_t_::~ur_exp_executable_graph_handle_t_() {
46+
if (zeExGraph) {
47+
ze_result_t ZeResult = ZE_CALL_NOCHECK(
48+
hContext->getPlatform()->ZeGraphExt.zeExecutableGraphDestroyExp,
49+
(zeExGraph));
50+
if (ZeResult != ZE_RESULT_SUCCESS) {
51+
UR_LOG(WARN, "Failed to destroy executable graph handle: {}", ZeResult);
52+
}
53+
}
54+
}
55+
56+
namespace ur::level_zero {
57+
58+
ur_result_t urGraphCreateExp(ur_context_handle_t hContext,
59+
ur_exp_graph_handle_t *phGraph) try {
60+
if (!checkGraphExtensionSupport(hContext)) {
61+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
62+
}
63+
64+
*phGraph = new ur_exp_graph_handle_t_(hContext);
65+
return UR_RESULT_SUCCESS;
66+
} catch (...) {
67+
return exceptionToResult(std::current_exception());
68+
}
69+
70+
ur_result_t urGraphDestroyExp(ur_exp_graph_handle_t hGraph) try {
71+
ur_context_handle_t hContext = hGraph->getContext();
72+
if (!checkGraphExtensionSupport(hContext)) {
73+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
74+
}
75+
76+
delete hGraph;
77+
return UR_RESULT_SUCCESS;
78+
} catch (...) {
79+
return exceptionToResult(std::current_exception());
80+
}
81+
82+
ur_result_t urGraphInstantiateGraphExp(
83+
ur_exp_graph_handle_t hGraph,
84+
ur_exp_executable_graph_handle_t *phExecutableGraph) try {
85+
ur_context_handle_t hContext = hGraph->getContext();
86+
if (!checkGraphExtensionSupport(hContext)) {
87+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
88+
}
89+
90+
*phExecutableGraph = new ur_exp_executable_graph_handle_t_(hContext, hGraph);
91+
return UR_RESULT_SUCCESS;
92+
} catch (...) {
93+
return exceptionToResult(std::current_exception());
94+
}
95+
96+
ur_result_t urGraphExecutableGraphDestroyExp(
97+
ur_exp_executable_graph_handle_t hExecutableGraph) try {
98+
ur_context_handle_t hContext = hExecutableGraph->getContext();
99+
if (!checkGraphExtensionSupport(hContext)) {
100+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
101+
}
102+
103+
delete hExecutableGraph;
104+
return UR_RESULT_SUCCESS;
105+
} catch (...) {
106+
return exceptionToResult(std::current_exception());
107+
}
108+
109+
ur_result_t urGraphIsEmptyExp(ur_exp_graph_handle_t hGraph, bool *pIsEmpty) {
110+
ur_context_handle_t hContext = hGraph->getContext();
111+
if (!checkGraphExtensionSupport(hContext)) {
112+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
113+
}
114+
115+
ze_result_t zeResult =
116+
ZE_CALL_NOCHECK(hContext->getPlatform()->ZeGraphExt.zeGraphIsEmptyExp,
117+
(hGraph->getZeHandle()));
118+
if (zeResult == ZE_RESULT_ERROR_INVALID_GRAPH) {
119+
return UR_RESULT_ERROR_INVALID_GRAPH;
120+
}
121+
122+
*pIsEmpty = (zeResult == ZE_RESULT_QUERY_TRUE);
123+
return UR_RESULT_SUCCESS;
124+
}
125+
126+
ur_result_t urGraphDumpContentsExp(ur_exp_graph_handle_t, const char *) {
127+
UR_LOG(ERR, "{} function not implemented!", __FUNCTION__);
128+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
129+
}
130+
131+
} // namespace ur::level_zero

0 commit comments

Comments
 (0)