Skip to content

Commit 691cb90

Browse files
BensuoEwan Crawford
andauthored
[SYCL][Graph] Add unit test/mocking for command handle behaviour (#15806)
- Add a unit test and mocking functions that check the command handle behaviour changes from #15522 --------- Co-authored-by: Ewan Crawford <[email protected]>
1 parent 79b620b commit 691cb90

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

sycl/unittests/Extensions/CommandGraph/Update.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,38 @@ TEST_F(WholeGraphUpdateTest, EmptyNode) {
399399
auto GraphExec = Graph.finalize(experimental::property::graph::updatable{});
400400
GraphExec.update(UpdateGraph);
401401
}
402+
403+
// Vars and callbacks for tracking how many times mocked functions are called
404+
static int GetInfoCount = 0;
405+
static int AppendKernelLaunchCount = 0;
406+
static ur_result_t redefinedCommandBufferGetInfoExpAfter(void *pParams) {
407+
GetInfoCount++;
408+
return UR_RESULT_SUCCESS;
409+
}
410+
static ur_result_t
411+
redefinedCommandBufferAppendKernelLaunchExpAfter(void *pParams) {
412+
AppendKernelLaunchCount++;
413+
return UR_RESULT_SUCCESS;
414+
}
415+
416+
TEST_F(CommandGraphTest, CheckFinalizeBehavior) {
417+
// Check that both finalize with and without updatable property work as
418+
// expected
419+
auto Node = Graph.add(
420+
[&](sycl::handler &cgh) { cgh.single_task<TestKernel<>>([]() {}); });
421+
mock::getCallbacks().set_after_callback(
422+
"urCommandBufferGetInfoExp", &redefinedCommandBufferGetInfoExpAfter);
423+
mock::getCallbacks().set_after_callback(
424+
"urCommandBufferAppendKernelLaunchExp",
425+
&redefinedCommandBufferAppendKernelLaunchExpAfter);
426+
427+
ASSERT_NO_THROW(Graph.finalize(experimental::property::graph::updatable{}));
428+
// GetInfo and AppendKernelLaunch should be called once each time a node is
429+
// added to a command buffer during finalization
430+
ASSERT_EQ(GetInfoCount, 1);
431+
ASSERT_EQ(AppendKernelLaunchCount, 1);
432+
433+
ASSERT_NO_THROW(Graph.finalize());
434+
ASSERT_EQ(GetInfoCount, 2);
435+
ASSERT_EQ(AppendKernelLaunchCount, 2);
436+
}

sycl/unittests/helpers/UrMock.hpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,67 @@ inline ur_result_t mock_urVirtualMemReserve(void *pParams) {
439439
return UR_RESULT_SUCCESS;
440440
}
441441

442+
// Create dummy command buffer handle and store the provided descriptor as the
443+
// data
444+
inline ur_result_t mock_urCommandBufferCreateExp(void *pParams) {
445+
auto params =
446+
reinterpret_cast<ur_command_buffer_create_exp_params_t *>(pParams);
447+
const ur_exp_command_buffer_desc_t *descPtr = *(params->ppCommandBufferDesc);
448+
ur_exp_command_buffer_handle_t *retCmdBuffer = *params->pphCommandBuffer;
449+
*retCmdBuffer = mock::createDummyHandle<ur_exp_command_buffer_handle_t>(
450+
static_cast<size_t>(sizeof(ur_exp_command_buffer_desc_t)));
451+
if (descPtr) {
452+
reinterpret_cast<mock::dummy_handle_t>(*retCmdBuffer)
453+
->setDataAs<ur_exp_command_buffer_desc_t>(*descPtr);
454+
}
455+
456+
return UR_RESULT_SUCCESS;
457+
}
458+
459+
inline ur_result_t mock_urCommandBufferGetInfoExp(void *pParams) {
460+
auto params =
461+
reinterpret_cast<ur_command_buffer_get_info_exp_params_t *>(pParams);
462+
463+
auto cmdBufferDummyHandle =
464+
reinterpret_cast<mock::dummy_handle_t>(*params->phCommandBuffer);
465+
switch (*params->ppropName) {
466+
case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: {
467+
if (*params->ppPropValue) {
468+
ur_exp_command_buffer_desc_t *propValue =
469+
reinterpret_cast<ur_exp_command_buffer_desc_t *>(
470+
*params->ppPropValue);
471+
*propValue =
472+
cmdBufferDummyHandle->getDataAs<ur_exp_command_buffer_desc_t>();
473+
}
474+
if (*params->ppPropSizeRet)
475+
**params->ppPropSizeRet = sizeof(ur_exp_command_buffer_desc_t);
476+
}
477+
return UR_RESULT_SUCCESS;
478+
default:
479+
return UR_RESULT_SUCCESS;
480+
}
481+
return UR_RESULT_SUCCESS;
482+
}
483+
484+
// Checking command handle behaviour only
485+
inline ur_result_t mock_urCommandBufferAppendKernelLaunchExp(void *pParams) {
486+
auto params =
487+
reinterpret_cast<ur_command_buffer_append_kernel_launch_exp_params_t *>(
488+
pParams);
489+
490+
auto cmdBufferDummyHandle =
491+
reinterpret_cast<mock::dummy_handle_t>(*params->phCommandBuffer);
492+
// Requesting a command handle when the command buffer is not updatable is an
493+
// error
494+
if (*(params->pphCommand) &&
495+
cmdBufferDummyHandle->getDataAs<ur_exp_command_buffer_desc_t>()
496+
.isUpdatable == false) {
497+
return UR_RESULT_ERROR_INVALID_OPERATION;
498+
}
499+
500+
return UR_RESULT_SUCCESS;
501+
}
502+
442503
} // namespace MockAdapter
443504

444505
/// The UrMock<> class sets up UR for adapter mocking with the set of default
@@ -484,6 +545,12 @@ template <sycl::backend Backend = backend::opencl> class UrMock {
484545
ADD_DEFAULT_OVERRIDE(urUsmP2PPeerAccessGetInfoExp,
485546
mock_urUsmP2PPeerAccessGetInfoExp)
486547
ADD_DEFAULT_OVERRIDE(urVirtualMemReserve, mock_urVirtualMemReserve)
548+
ADD_DEFAULT_OVERRIDE(urCommandBufferCreateExp,
549+
mock_urCommandBufferCreateExp);
550+
ADD_DEFAULT_OVERRIDE(urCommandBufferAppendKernelLaunchExp,
551+
mock_urCommandBufferAppendKernelLaunchExp);
552+
ADD_DEFAULT_OVERRIDE(urCommandBufferGetInfoExp,
553+
mock_urCommandBufferGetInfoExp);
487554
#undef ADD_DEFAULT_OVERRIDE
488555

489556
ur_loader_config_handle_t UrLoaderConfig = nullptr;

0 commit comments

Comments
 (0)