Skip to content

Commit 442882e

Browse files
committed
TEST: Fallback to experimental API
1 parent 8acae84 commit 442882e

5 files changed

Lines changed: 129 additions & 30 deletions

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,9 +1589,18 @@ ur_result_t urDeviceGetInfo(
15891589
return ReturnValue(false);
15901590
}
15911591

1592+
// The experimental variant of the extension reports its capabilities
1593+
// through a structure with a different type value; an older driver would
1594+
// not recognize the stable one and would leave graphFlags unset. The
1595+
// structure layout (stype, pNext, graphFlags) is identical between the two
1596+
// variants, so the stable type can be reused with the experimental value.
1597+
constexpr ze_structure_type_t ZeStructTypeRecordReplayGraphExpProperties =
1598+
static_cast<ze_structure_type_t>(0x00030029);
15921599
ze_record_replay_graph_ext_properties_t GraphProperties{};
15931600
GraphProperties.stype =
1594-
ZE_STRUCTURE_TYPE_RECORD_REPLAY_GRAPH_EXT_PROPERTIES;
1601+
Device->Platform->ZeGraphExt.UsesLegacyExperimentalApi
1602+
? ZeStructTypeRecordReplayGraphExpProperties
1603+
: ZE_STRUCTURE_TYPE_RECORD_REPLAY_GRAPH_EXT_PROPERTIES;
15951604
GraphProperties.pNext = nullptr;
15961605
ZeStruct<ze_device_properties_t> DeviceProperties;
15971606
DeviceProperties.pNext = &GraphProperties;

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

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,28 @@ ur_result_t ur_platform_handle_t_::initialize() {
305305
zeDriverExtensionMap[extension.name] = extension.version;
306306
}
307307

308-
const auto GraphExtension =
308+
const auto StableGraphExtension =
309309
zeDriverExtensionMap.find(ZE_RECORD_REPLAY_GRAPH_EXT_NAME);
310+
const bool ZeStableGraphExtensionSupported =
311+
StableGraphExtension != zeDriverExtensionMap.end() &&
312+
StableGraphExtension->second >= ZE_RECORD_REPLAY_GRAPH_EXT_VERSION_1_0;
313+
314+
// Fallback for drivers that have not yet adopted the stable record & replay
315+
// graph extension and still expose only the experimental variant. The
316+
// experimental extension uses different entry-point names (Exp suffix) and a
317+
// different parameter order for a few functions; both differences are handled
318+
// when the function pointers are populated below.
319+
constexpr char ZeRecordReplayGraphExpName[] =
320+
"ZE_experimental_record_replay_graph";
321+
const auto ExpGraphExtension =
322+
zeDriverExtensionMap.find(ZeRecordReplayGraphExpName);
323+
const bool ZeExpGraphExtensionSupported =
324+
!ZeStableGraphExtensionSupported &&
325+
ExpGraphExtension != zeDriverExtensionMap.end() &&
326+
ExpGraphExtension->second >= ZE_MAKE_VERSION(1, 0);
327+
310328
const bool ZeGraphExtensionSupported =
311-
GraphExtension != zeDriverExtensionMap.end() &&
312-
GraphExtension->second >= ZE_RECORD_REPLAY_GRAPH_EXT_VERSION_1_0;
329+
ZeStableGraphExtensionSupported || ZeExpGraphExtensionSupported;
313330

314331
ZE2UR_CALL(zelLoaderTranslateHandle, (ZEL_HANDLE_DRIVER, ZeDriver,
315332
(void **)&ZeDriverHandleExpTranslated));
@@ -568,37 +585,66 @@ ur_result_t ur_platform_handle_t_::initialize() {
568585
ZeMemGetPitchFor2dImageExt.zeMemGetPitchFor2dImage != nullptr;
569586

570587
if (ZeGraphExtensionSupported) {
571-
// Populate Graph Extension structure. Mandatory graph functions.
588+
const bool UseExp = ZeExpGraphExtensionSupported;
589+
ZeGraphExt.UsesLegacyExperimentalApi = UseExp;
590+
591+
// Populate Graph Extension structure. Mandatory graph functions whose
592+
// signature is identical between the stable and experimental variants;
593+
// only the entry-point name (Exp vs Ext suffix) differs.
572594
std::unordered_map<std::string, void **> ZeGraphFuncNameToAddrMap = {
573-
{"zeGraphCreateExt",
574-
reinterpret_cast<void **>(&ZeGraphExt.zeGraphCreateExp)},
575-
{"zeCommandListBeginGraphCaptureExt",
595+
{UseExp ? "zeCommandListBeginGraphCaptureExp"
596+
: "zeCommandListBeginGraphCaptureExt",
576597
reinterpret_cast<void **>(
577598
&ZeGraphExt.zeCommandListBeginGraphCaptureExp)},
578-
{"zeCommandListBeginCaptureIntoGraphExt",
599+
{UseExp ? "zeCommandListBeginCaptureIntoGraphExp"
600+
: "zeCommandListBeginCaptureIntoGraphExt",
579601
reinterpret_cast<void **>(
580602
&ZeGraphExt.zeCommandListBeginCaptureIntoGraphExp)},
581-
{"zeCommandListEndGraphCaptureExt",
582-
reinterpret_cast<void **>(
583-
&ZeGraphExt.zeCommandListEndGraphCaptureExp)},
584-
{"zeGraphInstantiateExt",
585-
reinterpret_cast<void **>(
586-
&ZeGraphExt.zeCommandListInstantiateGraphExp)},
587-
{"zeCommandListAppendGraphExt",
603+
{UseExp ? "zeCommandListAppendGraphExp" : "zeCommandListAppendGraphExt",
588604
reinterpret_cast<void **>(&ZeGraphExt.zeCommandListAppendGraphExp)},
589-
{"zeGraphDestroyExt",
605+
{UseExp ? "zeGraphDestroyExp" : "zeGraphDestroyExt",
590606
reinterpret_cast<void **>(&ZeGraphExt.zeGraphDestroyExp)},
591-
{"zeExecutableGraphDestroyExt",
607+
{UseExp ? "zeExecutableGraphDestroyExp" : "zeExecutableGraphDestroyExt",
592608
reinterpret_cast<void **>(&ZeGraphExt.zeExecutableGraphDestroyExp)},
593-
{"zeCommandListIsGraphCaptureEnabledExt",
609+
{UseExp ? "zeCommandListIsGraphCaptureEnabledExp"
610+
: "zeCommandListIsGraphCaptureEnabledExt",
594611
reinterpret_cast<void **>(
595612
&ZeGraphExt.zeCommandListIsGraphCaptureEnabledExp)},
596-
{"zeGraphIsEmptyExt",
613+
{UseExp ? "zeGraphIsEmptyExp" : "zeGraphIsEmptyExt",
597614
reinterpret_cast<void **>(&ZeGraphExt.zeGraphIsEmptyExp)},
598-
{"zeGraphDumpContentsExt",
615+
{UseExp ? "zeGraphDumpContentsExp" : "zeGraphDumpContentsExt",
599616
reinterpret_cast<void **>(&ZeGraphExt.zeGraphDumpContentsExp)},
600617
};
601618

619+
// Mandatory functions whose parameter order differs between the variants.
620+
// They are loaded into variant-specific fields and dispatched through the
621+
// wrapper helpers on ZeGraphExtension.
622+
if (UseExp) {
623+
ZeGraphFuncNameToAddrMap.emplace(
624+
"zeGraphCreateExp",
625+
reinterpret_cast<void **>(&ZeGraphExt.zeGraphCreateExpLegacy));
626+
ZeGraphFuncNameToAddrMap.emplace(
627+
"zeCommandListEndGraphCaptureExp",
628+
reinterpret_cast<void **>(
629+
&ZeGraphExt.zeCommandListEndGraphCaptureExpLegacy));
630+
ZeGraphFuncNameToAddrMap.emplace(
631+
"zeCommandListInstantiateGraphExp",
632+
reinterpret_cast<void **>(
633+
&ZeGraphExt.zeCommandListInstantiateGraphExpLegacy));
634+
} else {
635+
ZeGraphFuncNameToAddrMap.emplace(
636+
"zeGraphCreateExt",
637+
reinterpret_cast<void **>(&ZeGraphExt.zeGraphCreateExp));
638+
ZeGraphFuncNameToAddrMap.emplace(
639+
"zeCommandListEndGraphCaptureExt",
640+
reinterpret_cast<void **>(
641+
&ZeGraphExt.zeCommandListEndGraphCaptureExp));
642+
ZeGraphFuncNameToAddrMap.emplace(
643+
"zeGraphInstantiateExt",
644+
reinterpret_cast<void **>(
645+
&ZeGraphExt.zeCommandListInstantiateGraphExp));
646+
}
647+
602648
ZeGraphExt.Supported = true;
603649
for (auto &[funcName, funcAddr] : ZeGraphFuncNameToAddrMap) {
604650
ZE_CALL_NOCHECK(zeDriverGetExtensionFunctionAddress,
@@ -611,9 +657,10 @@ ur_result_t ur_platform_handle_t_::initialize() {
611657
// code in affected function.
612658
std::unordered_map<std::string, void **> ZeGraphOptionalFuncNameToAddrMap =
613659
{
614-
{"zeCommandListGetGraphExt",
660+
{UseExp ? "zeCommandListGetGraphExp" : "zeCommandListGetGraphExt",
615661
reinterpret_cast<void **>(&ZeGraphExt.zeCommandListGetGraphExp)},
616-
{"zeGraphSetDestructionCallbackExt",
662+
{UseExp ? "zeGraphSetDestructionCallbackExp"
663+
: "zeGraphSetDestructionCallbackExt",
617664
reinterpret_cast<void **>(
618665
&ZeGraphExt.zeGraphSetDestructionCallbackExp)},
619666
};

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ struct ur_platform_handle_t_ : ur::handle_base<ur::level_zero::ddi_getter>,
176176

177177
struct ZeGraphExtension {
178178
bool Supported = false;
179+
// Older Level Zero drivers only expose the experimental
180+
// (ZE_experimental_record_replay_graph) variant of the record & replay
181+
// graph extension instead of the stable one. The two variants differ both
182+
// in the entry-point names (Exp vs Ext suffix) and in the parameter order
183+
// of a few functions. When this flag is set, the *Legacy function pointers
184+
// below are populated and used instead of the stable ones.
185+
bool UsesLegacyExperimentalApi = false;
179186
ze_result_t (*zeGraphCreateExp)(ze_context_handle_t hContext, void *pNext,
180187
ze_graph_handle_t *phGraph);
181188
ze_result_t (*zeCommandListBeginGraphCaptureExp)(
@@ -209,6 +216,44 @@ struct ur_platform_handle_t_ : ur::handle_base<ur::level_zero::ddi_getter>,
209216
ze_result_t (*zeGraphSetDestructionCallbackExp)(
210217
ze_graph_handle_t hGraph, zex_mem_graph_free_callback_fn_t pfnCallback,
211218
void *pUserData, void *pNext);
219+
220+
// Legacy experimental-API entry points for the functions whose parameter
221+
// order differs from the stable API (the output handle and pNext are
222+
// swapped). These are only populated when UsesLegacyExperimentalApi is set.
223+
ze_result_t (*zeGraphCreateExpLegacy)(ze_context_handle_t hContext,
224+
ze_graph_handle_t *phGraph,
225+
void *pNext) = nullptr;
226+
ze_result_t (*zeCommandListEndGraphCaptureExpLegacy)(
227+
ze_command_list_handle_t hCommandList, ze_graph_handle_t *phGraph,
228+
void *pNext) = nullptr;
229+
ze_result_t (*zeCommandListInstantiateGraphExpLegacy)(
230+
ze_graph_handle_t hGraph,
231+
ze_executable_graph_handle_t *phExecutableGraph, void *pNext) = nullptr;
232+
233+
// Dispatch helpers presenting the stable parameter order to all callers,
234+
// routing to either the stable or the legacy experimental entry point.
235+
ze_result_t graphCreate(ze_context_handle_t hContext, void *pNext,
236+
ze_graph_handle_t *phGraph) const {
237+
return UsesLegacyExperimentalApi
238+
? zeGraphCreateExpLegacy(hContext, phGraph, pNext)
239+
: zeGraphCreateExp(hContext, pNext, phGraph);
240+
}
241+
ze_result_t endGraphCapture(ze_command_list_handle_t hCommandList,
242+
void *pNext, ze_graph_handle_t *phGraph) const {
243+
return UsesLegacyExperimentalApi
244+
? zeCommandListEndGraphCaptureExpLegacy(hCommandList, phGraph,
245+
pNext)
246+
: zeCommandListEndGraphCaptureExp(hCommandList, pNext,
247+
phGraph);
248+
}
249+
ze_result_t
250+
instantiateGraph(ze_graph_handle_t hGraph, void *pNext,
251+
ze_executable_graph_handle_t *phExecutableGraph) const {
252+
return UsesLegacyExperimentalApi ? zeCommandListInstantiateGraphExpLegacy(
253+
hGraph, phExecutableGraph, pNext)
254+
: zeCommandListInstantiateGraphExp(
255+
hGraph, pNext, phExecutableGraph);
256+
}
212257
} ZeGraphExt;
213258

214259
struct ZeHostTaskExtension {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,9 +1365,8 @@ ur_command_list_manager::endGraphCapture(ur_exp_graph_handle_t *phGraph) {
13651365
}
13661366

13671367
ze_graph_handle_t zeGraph = nullptr;
1368-
ZE2UR_CALL(
1369-
hContext.get()->getPlatform()->ZeGraphExt.zeCommandListEndGraphCaptureExp,
1370-
(getZeCommandList(), nullptr, &zeGraph));
1368+
ZE2UR_CALL(hContext.get()->getPlatform()->ZeGraphExt.endGraphCapture,
1369+
(getZeCommandList(), nullptr, &zeGraph));
13711370
auto graph = graphCapture.getGraph();
13721371
graphCapture.disableCapture();
13731372

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
ur_exp_graph_handle_t_::ur_exp_graph_handle_t_(ur_context_handle_t hContext)
2020
: hContext(hContext) {
21-
ZE2UR_CALL_THROWS(hContext->getPlatform()->ZeGraphExt.zeGraphCreateExp,
21+
ZE2UR_CALL_THROWS(hContext->getPlatform()->ZeGraphExt.graphCreate,
2222
(hContext->getZeHandle(), nullptr, &zeGraph));
2323
}
2424

@@ -39,9 +39,8 @@ ur_exp_graph_handle_t_::~ur_exp_graph_handle_t_() {
3939
ur_exp_executable_graph_handle_t_::ur_exp_executable_graph_handle_t_(
4040
ur_context_handle_t hContext, ur_exp_graph_handle_t hGraph)
4141
: hContext(hContext) {
42-
ZE2UR_CALL_THROWS(
43-
hContext->getPlatform()->ZeGraphExt.zeCommandListInstantiateGraphExp,
44-
(hGraph->getZeHandle(), nullptr, &zeExGraph));
42+
ZE2UR_CALL_THROWS(hContext->getPlatform()->ZeGraphExt.instantiateGraph,
43+
(hGraph->getZeHandle(), nullptr, &zeExGraph));
4544
}
4645

4746
ur_exp_executable_graph_handle_t_::~ur_exp_executable_graph_handle_t_() {

0 commit comments

Comments
 (0)