Skip to content

Commit fbdb472

Browse files
committed
Fix tests that relied on old null adapter behaviour.
1 parent 9b41a92 commit fbdb472

20 files changed

+178
-240
lines changed

source/adapters/mock/ur_mock.cpp

Lines changed: 64 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -10,230 +10,88 @@
1010
*
1111
*/
1212
#include "ur_mock.hpp"
13+
#include "ur_mock_helpers.hpp"
1314

1415
namespace driver {
1516
//////////////////////////////////////////////////////////////////////////
1617
context_t d_context;
1718

18-
//////////////////////////////////////////////////////////////////////////
19-
context_t::context_t() {
20-
platform = get();
21-
//////////////////////////////////////////////////////////////////////////
22-
urDdiTable.Global.pfnAdapterGet = [](uint32_t NumAdapters,
23-
ur_adapter_handle_t *phAdapters,
24-
uint32_t *pNumAdapters) {
25-
if (phAdapters != nullptr && NumAdapters != 1) {
26-
return UR_RESULT_ERROR_INVALID_SIZE;
27-
}
28-
if (pNumAdapters != nullptr) {
29-
*pNumAdapters = 1;
30-
}
31-
if (nullptr != phAdapters) {
32-
*reinterpret_cast<void **>(phAdapters) = d_context.platform;
33-
}
34-
35-
return UR_RESULT_SUCCESS;
36-
};
37-
//////////////////////////////////////////////////////////////////////////
38-
urDdiTable.Global.pfnAdapterRelease = [](ur_adapter_handle_t) {
39-
return UR_RESULT_SUCCESS;
40-
};
41-
//////////////////////////////////////////////////////////////////////////
42-
urDdiTable.Platform.pfnGet =
43-
[](ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
44-
ur_platform_handle_t *phPlatforms, uint32_t *pNumPlatforms) {
45-
if (phPlatforms != nullptr && NumEntries != 1) {
46-
return UR_RESULT_ERROR_INVALID_SIZE;
47-
}
48-
if (pNumPlatforms != nullptr) {
49-
*pNumPlatforms = 1;
50-
}
51-
if (nullptr != phPlatforms) {
52-
*reinterpret_cast<void **>(phPlatforms) = d_context.platform;
53-
}
54-
return UR_RESULT_SUCCESS;
55-
};
56-
57-
//////////////////////////////////////////////////////////////////////////
58-
urDdiTable.Platform.pfnGetApiVersion = [](ur_platform_handle_t,
59-
ur_api_version_t *version) {
60-
*version = d_context.version;
61-
return UR_RESULT_SUCCESS;
62-
};
19+
ur_result_t mock_urPlatformGetApiVersion(void *pParams) {
20+
auto params = *static_cast<ur_platform_get_api_version_params_t *>(pParams);
21+
**params.ppVersion = d_context.version;
22+
return UR_RESULT_SUCCESS;
23+
}
6324

64-
//////////////////////////////////////////////////////////////////////////
65-
urDdiTable.Platform.pfnGetInfo =
66-
[](ur_platform_handle_t hPlatform, ur_platform_info_t PlatformInfoType,
67-
size_t Size, void *pPlatformInfo, size_t *pSizeRet) {
68-
if (!hPlatform) {
69-
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
70-
}
25+
ur_result_t mock_urPlatformGetInfo(void *pParams) {
26+
auto params = *static_cast<ur_platform_get_info_params_t *>(pParams);
27+
if (!*params.phPlatform) {
28+
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
29+
}
7130

72-
switch (PlatformInfoType) {
73-
case UR_PLATFORM_INFO_NAME: {
74-
const char null_platform_name[] = "UR_PLATFORM_NULL";
75-
if (pSizeRet) {
76-
*pSizeRet = sizeof(null_platform_name);
77-
}
78-
if (pPlatformInfo && Size != sizeof(null_platform_name)) {
79-
return UR_RESULT_ERROR_INVALID_SIZE;
80-
}
81-
if (pPlatformInfo) {
31+
if (*params.ppropName == UR_PLATFORM_INFO_NAME) {
32+
const char mock_platform_name[] = "UR_PLATFORM_MOCK";
33+
if (*params.ppPropSizeRet) {
34+
**params.ppPropSizeRet = sizeof(mock_platform_name);
35+
}
36+
if (*params.ppPropValue) {
8237
#if defined(_WIN32)
83-
strncpy_s(reinterpret_cast<char *>(pPlatformInfo), Size,
84-
null_platform_name, sizeof(null_platform_name));
38+
strncpy_s(reinterpret_cast<char *>(*params.ppPropValue),
39+
*params.ppropSize, null_platform_name,
40+
sizeof(mock_platform_name));
8541
#else
86-
strncpy(reinterpret_cast<char *>(pPlatformInfo),
87-
null_platform_name, Size);
42+
strncpy(reinterpret_cast<char *>(*params.ppPropValue),
43+
mock_platform_name, *params.ppropSize);
8844
#endif
89-
}
90-
} break;
91-
92-
default:
93-
return UR_RESULT_ERROR_INVALID_ENUMERATION;
94-
}
95-
96-
return UR_RESULT_SUCCESS;
97-
};
98-
99-
//////////////////////////////////////////////////////////////////////////
100-
urDdiTable.Device.pfnGet =
101-
[](ur_platform_handle_t hPlatform, ur_device_type_t DevicesType,
102-
uint32_t NumEntries, ur_device_handle_t *phDevices,
103-
uint32_t *pNumDevices) {
104-
(void)DevicesType;
105-
if (hPlatform == nullptr) {
106-
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
107-
}
108-
if (UR_DEVICE_TYPE_VPU < DevicesType) {
109-
return UR_RESULT_ERROR_INVALID_ENUMERATION;
110-
}
111-
if (phDevices != nullptr && NumEntries != 1) {
112-
return UR_RESULT_ERROR_INVALID_SIZE;
113-
}
114-
if (pNumDevices != nullptr) {
115-
*pNumDevices = 1;
116-
}
117-
if (nullptr != phDevices) {
118-
*reinterpret_cast<void **>(phDevices) = d_context.get();
119-
}
120-
return UR_RESULT_SUCCESS;
121-
};
122-
123-
//////////////////////////////////////////////////////////////////////////
124-
urDdiTable.Device.pfnGetInfo = [](ur_device_handle_t,
125-
ur_device_info_t infoType,
126-
size_t propSize, void *pDeviceInfo,
127-
size_t *pPropSizeRet) {
128-
switch (infoType) {
129-
case UR_DEVICE_INFO_TYPE:
130-
if (pDeviceInfo && propSize != sizeof(ur_device_type_t)) {
131-
return UR_RESULT_ERROR_INVALID_SIZE;
132-
}
45+
}
46+
}
47+
return UR_RESULT_SUCCESS;
48+
}
13349

134-
if (pDeviceInfo != nullptr) {
135-
*reinterpret_cast<ur_device_type_t *>(pDeviceInfo) =
136-
UR_DEVICE_TYPE_GPU;
137-
}
138-
if (pPropSizeRet != nullptr) {
139-
*pPropSizeRet = sizeof(ur_device_type_t);
140-
}
141-
break;
50+
//////////////////////////////////////////////////////////////////////////
51+
ur_result_t mock_urDeviceGetInfo(void *pParams) {
52+
auto params = *static_cast<ur_device_get_info_params_t *>(pParams);
53+
switch (*params.ppropName) {
54+
case UR_DEVICE_INFO_TYPE:
55+
if (*params.ppPropValue != nullptr) {
56+
*reinterpret_cast<ur_device_type_t *>(*params.ppPropValue) =
57+
UR_DEVICE_TYPE_GPU;
58+
}
59+
if (*params.ppPropSizeRet != nullptr) {
60+
**params.ppPropSizeRet = sizeof(ur_device_type_t);
61+
}
62+
break;
14263

143-
case UR_DEVICE_INFO_NAME: {
144-
char deviceName[] = "Null Device";
145-
if (pDeviceInfo && propSize < sizeof(deviceName)) {
146-
return UR_RESULT_ERROR_INVALID_SIZE;
147-
}
148-
if (pDeviceInfo != nullptr) {
64+
case UR_DEVICE_INFO_NAME: {
65+
char deviceName[] = "Mock Device";
66+
if (*params.ppPropValue && *params.ppropSize < sizeof(deviceName)) {
67+
return UR_RESULT_ERROR_INVALID_SIZE;
68+
}
69+
if (*params.ppPropValue != nullptr) {
14970
#if defined(_WIN32)
150-
strncpy_s(reinterpret_cast<char *>(pDeviceInfo), propSize,
151-
deviceName, sizeof(deviceName));
71+
strncpy_s(reinterpret_cast<char *>(*params.ppPropValue),
72+
*params.ppropSize, deviceName, sizeof(deviceName));
15273
#else
153-
strncpy(reinterpret_cast<char *>(pDeviceInfo), deviceName,
154-
propSize);
74+
strncpy(reinterpret_cast<char *>(*params.ppPropValue), deviceName,
75+
*params.ppropSize);
15576
#endif
156-
}
157-
if (pPropSizeRet != nullptr) {
158-
*pPropSizeRet = sizeof(deviceName);
159-
}
160-
} break;
161-
case UR_DEVICE_INFO_PLATFORM: {
162-
if (pDeviceInfo && propSize < sizeof(pDeviceInfo)) {
163-
return UR_RESULT_ERROR_INVALID_SIZE;
164-
}
165-
if (pDeviceInfo != nullptr) {
166-
*reinterpret_cast<void **>(pDeviceInfo) = d_context.platform;
167-
}
168-
if (pPropSizeRet != nullptr) {
169-
*pPropSizeRet = sizeof(intptr_t);
170-
}
171-
} break;
172-
default:
173-
return UR_RESULT_ERROR_INVALID_ARGUMENT;
174-
}
175-
return UR_RESULT_SUCCESS;
176-
};
177-
178-
//////////////////////////////////////////////////////////////////////////
179-
urDdiTable.USM.pfnHostAlloc = [](ur_context_handle_t, const ur_usm_desc_t *,
180-
ur_usm_pool_handle_t, size_t size,
181-
void **ppMem) {
182-
if (size == 0) {
183-
*ppMem = nullptr;
184-
return UR_RESULT_ERROR_UNSUPPORTED_SIZE;
18577
}
186-
*ppMem = malloc(size);
187-
if (*ppMem == nullptr) {
188-
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
78+
if (*params.ppPropSizeRet != nullptr) {
79+
**params.ppPropSizeRet = sizeof(deviceName);
18980
}
81+
} break;
82+
default:
19083
return UR_RESULT_SUCCESS;
191-
};
192-
193-
//////////////////////////////////////////////////////////////////////////
194-
urDdiTable.USM.pfnDeviceAlloc =
195-
[](ur_context_handle_t, ur_device_handle_t, const ur_usm_desc_t *,
196-
ur_usm_pool_handle_t, size_t size, void **ppMem) {
197-
if (size == 0) {
198-
*ppMem = nullptr;
199-
return UR_RESULT_ERROR_UNSUPPORTED_SIZE;
200-
}
201-
*ppMem = malloc(size);
202-
if (*ppMem == nullptr) {
203-
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
204-
}
205-
return UR_RESULT_SUCCESS;
206-
};
207-
208-
//////////////////////////////////////////////////////////////////////////
209-
urDdiTable.USM.pfnFree = [](ur_context_handle_t, void *pMem) {
210-
free(pMem);
211-
return UR_RESULT_SUCCESS;
212-
};
84+
}
85+
return UR_RESULT_SUCCESS;
86+
}
21387

214-
//////////////////////////////////////////////////////////////////////////
215-
urDdiTable.USM.pfnGetMemAllocInfo =
216-
[](ur_context_handle_t, const void *pMem, ur_usm_alloc_info_t propName,
217-
size_t, void *pPropValue, size_t *pPropSizeRet) {
218-
switch (propName) {
219-
case UR_USM_ALLOC_INFO_TYPE:
220-
*reinterpret_cast<ur_usm_type_t *>(pPropValue) =
221-
pMem ? UR_USM_TYPE_DEVICE : UR_USM_TYPE_UNKNOWN;
222-
if (pPropSizeRet != nullptr) {
223-
*pPropSizeRet = sizeof(ur_usm_type_t);
224-
}
225-
break;
226-
case UR_USM_ALLOC_INFO_SIZE:
227-
*reinterpret_cast<size_t *>(pPropValue) = pMem ? SIZE_MAX : 0;
228-
if (pPropSizeRet != nullptr) {
229-
*pPropSizeRet = sizeof(size_t);
230-
}
231-
break;
232-
default:
233-
pPropValue = nullptr;
234-
break;
235-
}
236-
return UR_RESULT_SUCCESS;
237-
};
88+
//////////////////////////////////////////////////////////////////////////
89+
context_t::context_t() {
90+
mock::getCallbacks().set_replace_callback("urPlatformGetApiVersion",
91+
&mock_urPlatformGetApiVersion);
92+
mock::getCallbacks().set_replace_callback("urPlatformGetInfo",
93+
&mock_urPlatformGetInfo);
94+
mock::getCallbacks().set_replace_callback("urDeviceGetInfo",
95+
&mock_urDeviceGetInfo);
23896
}
23997
} // namespace driver

source/adapters/mock/ur_mock.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ class __urdlllocal context_t {
2525
ur_dditable_t urDdiTable = {};
2626
context_t();
2727
~context_t() = default;
28-
29-
void *platform;
30-
31-
void *get() {
32-
static uint64_t count = 0x80800000;
33-
return reinterpret_cast<void *>(++count);
34-
}
3528
};
3629

3730
extern context_t d_context;

source/loader/ur_lib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void context_t::tearDownLayers() const {
8181
//////////////////////////////////////////////////////////////////////////
8282
__urdlllocal ur_result_t context_t::Init(
8383
ur_device_init_flags_t, ur_loader_config_handle_t hLoaderConfig) {
84-
if (hLoaderConfig->enableMock) {
84+
if (hLoaderConfig && hLoaderConfig->enableMock) {
8585
// This clears default known adapters and replaces them with the mock
8686
// adapter.
8787
ur_loader::context->adapter_registry.enableMock();

test/layers/tracing/hello_world.out.logged.match

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ API version: {{0\.[0-9]+}}
88
---> urDeviceGet(.hPlatform = {{.*}}, .DeviceType = UR_DEVICE_TYPE_GPU, .NumEntries = 0, .phDevices = {}, .pNumDevices = {{.*}} (1)) -> UR_RESULT_SUCCESS;
99
---> urDeviceGet(.hPlatform = {{.*}}, .DeviceType = UR_DEVICE_TYPE_GPU, .NumEntries = 1, .phDevices = {{.*}}, .pNumDevices = nullptr) -> UR_RESULT_SUCCESS;
1010
---> urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_TYPE, .propSize = 4, .pPropValue = {{.*}} (UR_DEVICE_TYPE_GPU), .pPropSizeRet = nullptr) -> UR_RESULT_SUCCESS;
11-
---> urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_NAME, .propSize = {{.*}}, .pPropValue = {{.*}} (Null Device), .pPropSizeRet = nullptr) -> UR_RESULT_SUCCESS;
12-
Found a Null Device gpu.
11+
---> urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_NAME, .propSize = {{.*}}, .pPropValue = {{.*}} (Mock Device), .pPropSizeRet = nullptr) -> UR_RESULT_SUCCESS;
12+
Found a Mock Device gpu.
1313
---> urAdapterRelease(.hAdapter = {{.*}}) -> UR_RESULT_SUCCESS;

test/layers/tracing/hello_world.out.match

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ function_with_args_begin(8) - urDeviceGetInfo(unimplemented);
1818
function_with_args_end(8) - urDeviceGetInfo(...) -> ur_result_t(0);
1919
function_with_args_begin(9) - urDeviceGetInfo(unimplemented);
2020
function_with_args_end(9) - urDeviceGetInfo(...) -> ur_result_t(0);
21-
Found a Null Device gpu.
21+
Found a Mock Device gpu.
2222
function_with_args_begin(10) - urAdapterRelease(unimplemented);
2323
function_with_args_end(10) - urAdapterRelease(...) -> ur_result_t(0);

test/layers/validation/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function(add_validation_test_executable name)
1414
${PROJECT_NAME}::loader
1515
${PROJECT_NAME}::headers
1616
${PROJECT_NAME}::testing
17+
${PROJECT_NAME}::mock
1718
GTest::gtest_main)
1819
endfunction()
1920

0 commit comments

Comments
 (0)