Skip to content

Commit fd6cf3a

Browse files
aarongreigkbenzie
authored andcommitted
Rework null adapter into mock adapter
This also comes with a new accompanying helper header which allows tests to override entry points and create their own reference counted dummy handles.
1 parent 642e343 commit fd6cf3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+6722
-1787
lines changed

include/ur_api.h

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ typedef enum ur_function_t {
226226
UR_FUNCTION_BINDLESS_IMAGES_IMPORT_EXTERNAL_MEMORY_EXP = 226, ///< Enumerator for ::urBindlessImagesImportExternalMemoryExp
227227
UR_FUNCTION_BINDLESS_IMAGES_IMPORT_EXTERNAL_SEMAPHORE_EXP = 227, ///< Enumerator for ::urBindlessImagesImportExternalSemaphoreExp
228228
UR_FUNCTION_ENQUEUE_NATIVE_COMMAND_EXP = 228, ///< Enumerator for ::urEnqueueNativeCommandExp
229+
UR_FUNCTION_LOADER_CONFIG_SET_MOCKING_ENABLED = 231, ///< Enumerator for ::urLoaderConfigSetMockingEnabled
229230
/// @cond
230231
UR_FUNCTION_FORCE_UINT32 = 0x7fffffff
231232
/// @endcond
@@ -601,7 +602,7 @@ urLoaderConfigCreate(
601602
/// + `NULL == hLoaderConfig`
602603
UR_APIEXPORT ur_result_t UR_APICALL
603604
urLoaderConfigRetain(
604-
ur_loader_config_handle_t hLoaderConfig ///< [in] loader config handle to retain
605+
ur_loader_config_handle_t hLoaderConfig ///< [in][retain] loader config handle to retain
605606
);
606607

607608
///////////////////////////////////////////////////////////////////////////////
@@ -742,6 +743,35 @@ urLoaderConfigSetCodeLocationCallback(
742743
void *pUserData ///< [in][out][optional] pointer to data to be passed to callback.
743744
);
744745

746+
///////////////////////////////////////////////////////////////////////////////
747+
/// @brief Callback to replace or instrument generic mock functionality in the
748+
/// mock adapter.
749+
typedef ur_result_t (*ur_mock_callback_t)(
750+
void *pParams ///< [in][out] Pointer to the appropriate param struct for the function
751+
);
752+
753+
///////////////////////////////////////////////////////////////////////////////
754+
/// @brief The only adapter reported with mock enabled will be the mock adapter.
755+
///
756+
/// @details
757+
/// - The mock adapter will default to returning ::UR_RESULT_SUCCESS for all
758+
/// entry points. It will also create and correctly reference count dummy
759+
/// handles where appropriate. Its behaviour can be modified by linking
760+
/// the ::ur_mock_headers library and using the callbacks object.
761+
///
762+
/// @returns
763+
/// - ::UR_RESULT_SUCCESS
764+
/// - ::UR_RESULT_ERROR_UNINITIALIZED
765+
/// - ::UR_RESULT_ERROR_DEVICE_LOST
766+
/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC
767+
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
768+
/// + `NULL == hLoaderConfig`
769+
UR_APIEXPORT ur_result_t UR_APICALL
770+
urLoaderConfigSetMockingEnabled(
771+
ur_loader_config_handle_t hLoaderConfig, ///< [in] Handle to config object mocking will be enabled for.
772+
ur_bool_t enable ///< [in] Handle to config object the layer will be enabled for.
773+
);
774+
745775
///////////////////////////////////////////////////////////////////////////////
746776
/// @brief Initialize the 'oneAPI' loader
747777
///
@@ -863,7 +893,7 @@ urAdapterRelease(
863893
/// + `NULL == hAdapter`
864894
UR_APIEXPORT ur_result_t UR_APICALL
865895
urAdapterRetain(
866-
ur_adapter_handle_t hAdapter ///< [in] Adapter handle to retain
896+
ur_adapter_handle_t hAdapter ///< [in][retain] Adapter handle to retain
867897
);
868898

869899
///////////////////////////////////////////////////////////////////////////////
@@ -1736,7 +1766,7 @@ urDeviceGetInfo(
17361766
/// + `NULL == hDevice`
17371767
UR_APIEXPORT ur_result_t UR_APICALL
17381768
urDeviceRetain(
1739-
ur_device_handle_t hDevice ///< [in] handle of the device to get a reference of.
1769+
ur_device_handle_t hDevice ///< [in][retain] handle of the device to get a reference of.
17401770
);
17411771

17421772
///////////////////////////////////////////////////////////////////////////////
@@ -2217,7 +2247,7 @@ urContextCreate(
22172247
/// + `NULL == hContext`
22182248
UR_APIEXPORT ur_result_t UR_APICALL
22192249
urContextRetain(
2220-
ur_context_handle_t hContext ///< [in] handle of the context to get a reference of.
2250+
ur_context_handle_t hContext ///< [in][retain] handle of the context to get a reference of.
22212251
);
22222252

22232253
///////////////////////////////////////////////////////////////////////////////
@@ -2739,7 +2769,7 @@ urMemBufferCreate(
27392769
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
27402770
UR_APIEXPORT ur_result_t UR_APICALL
27412771
urMemRetain(
2742-
ur_mem_handle_t hMem ///< [in] handle of the memory object to get access
2772+
ur_mem_handle_t hMem ///< [in][retain] handle of the memory object to get access
27432773
);
27442774

27452775
///////////////////////////////////////////////////////////////////////////////
@@ -3130,7 +3160,7 @@ urSamplerCreate(
31303160
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
31313161
UR_APIEXPORT ur_result_t UR_APICALL
31323162
urSamplerRetain(
3133-
ur_sampler_handle_t hSampler ///< [in] handle of the sampler object to get access
3163+
ur_sampler_handle_t hSampler ///< [in][retain] handle of the sampler object to get access
31343164
);
31353165

31363166
///////////////////////////////////////////////////////////////////////////////
@@ -3690,7 +3720,7 @@ urUSMPoolCreate(
36903720
/// + `NULL == pPool`
36913721
UR_APIEXPORT ur_result_t UR_APICALL
36923722
urUSMPoolRetain(
3693-
ur_usm_pool_handle_t pPool ///< [in] pointer to USM memory pool
3723+
ur_usm_pool_handle_t pPool ///< [in][retain] pointer to USM memory pool
36943724
);
36953725

36963726
///////////////////////////////////////////////////////////////////////////////
@@ -4046,7 +4076,7 @@ urPhysicalMemCreate(
40464076
/// + `NULL == hPhysicalMem`
40474077
UR_APIEXPORT ur_result_t UR_APICALL
40484078
urPhysicalMemRetain(
4049-
ur_physical_mem_handle_t hPhysicalMem ///< [in] handle of the physical memory object to retain.
4079+
ur_physical_mem_handle_t hPhysicalMem ///< [in][retain] handle of the physical memory object to retain.
40504080
);
40514081

40524082
///////////////////////////////////////////////////////////////////////////////
@@ -4334,7 +4364,7 @@ urProgramLink(
43344364
/// + `NULL == hProgram`
43354365
UR_APIEXPORT ur_result_t UR_APICALL
43364366
urProgramRetain(
4337-
ur_program_handle_t hProgram ///< [in] handle for the Program to retain
4367+
ur_program_handle_t hProgram ///< [in][retain] handle for the Program to retain
43384368
);
43394369

43404370
///////////////////////////////////////////////////////////////////////////////
@@ -4985,7 +5015,7 @@ urKernelGetSubGroupInfo(
49855015
/// + `NULL == hKernel`
49865016
UR_APIEXPORT ur_result_t UR_APICALL
49875017
urKernelRetain(
4988-
ur_kernel_handle_t hKernel ///< [in] handle for the Kernel to retain
5018+
ur_kernel_handle_t hKernel ///< [in][retain] handle for the Kernel to retain
49895019
);
49905020

49915021
///////////////////////////////////////////////////////////////////////////////
@@ -5492,7 +5522,7 @@ urQueueCreate(
54925522
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
54935523
UR_APIEXPORT ur_result_t UR_APICALL
54945524
urQueueRetain(
5495-
ur_queue_handle_t hQueue ///< [in] handle of the queue object to get access
5525+
ur_queue_handle_t hQueue ///< [in][retain] handle of the queue object to get access
54965526
);
54975527

54985528
///////////////////////////////////////////////////////////////////////////////
@@ -5887,7 +5917,7 @@ urEventWait(
58875917
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
58885918
UR_APIEXPORT ur_result_t UR_APICALL
58895919
urEventRetain(
5890-
ur_event_handle_t hEvent ///< [in] handle of the event object
5920+
ur_event_handle_t hEvent ///< [in][retain] handle of the event object
58915921
);
58925922

58935923
///////////////////////////////////////////////////////////////////////////////
@@ -8252,7 +8282,7 @@ urCommandBufferCreateExp(
82528282
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
82538283
UR_APIEXPORT ur_result_t UR_APICALL
82548284
urCommandBufferRetainExp(
8255-
ur_exp_command_buffer_handle_t hCommandBuffer ///< [in] Handle of the command-buffer object.
8285+
ur_exp_command_buffer_handle_t hCommandBuffer ///< [in][retain] Handle of the command-buffer object.
82568286
);
82578287

82588288
///////////////////////////////////////////////////////////////////////////////
@@ -9657,6 +9687,15 @@ typedef struct ur_loader_config_set_code_location_callback_params_t {
96579687
void **ppUserData;
96589688
} ur_loader_config_set_code_location_callback_params_t;
96599689

9690+
///////////////////////////////////////////////////////////////////////////////
9691+
/// @brief Function parameters for urLoaderConfigSetMockingEnabled
9692+
/// @details Each entry is a pointer to the parameter passed to the function;
9693+
/// allowing the callback the ability to modify the parameter's value
9694+
typedef struct ur_loader_config_set_mocking_enabled_params_t {
9695+
ur_loader_config_handle_t *phLoaderConfig;
9696+
ur_bool_t *penable;
9697+
} ur_loader_config_set_mocking_enabled_params_t;
9698+
96609699
///////////////////////////////////////////////////////////////////////////////
96619700
/// @brief Function parameters for urPlatformGet
96629701
/// @details Each entry is a pointer to the parameter passed to the function;

include/ur_print.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urPrintLoaderConfigEnableLayerParams(const s
11061106
/// - `buff_size < out_size`
11071107
UR_APIEXPORT ur_result_t UR_APICALL urPrintLoaderConfigSetCodeLocationCallbackParams(const struct ur_loader_config_set_code_location_callback_params_t *params, char *buffer, const size_t buff_size, size_t *out_size);
11081108

1109+
///////////////////////////////////////////////////////////////////////////////
1110+
/// @brief Print ur_loader_config_set_mocking_enabled_params_t struct
1111+
/// @returns
1112+
/// - ::UR_RESULT_SUCCESS
1113+
/// - ::UR_RESULT_ERROR_INVALID_SIZE
1114+
/// - `buff_size < out_size`
1115+
UR_APIEXPORT ur_result_t UR_APICALL urPrintLoaderConfigSetMockingEnabledParams(const struct ur_loader_config_set_mocking_enabled_params_t *params, char *buffer, const size_t buff_size, size_t *out_size);
1116+
11091117
///////////////////////////////////////////////////////////////////////////////
11101118
/// @brief Print ur_platform_get_params_t struct
11111119
/// @returns

include/ur_print.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,9 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_function_t value) {
938938
case UR_FUNCTION_ENQUEUE_NATIVE_COMMAND_EXP:
939939
os << "UR_FUNCTION_ENQUEUE_NATIVE_COMMAND_EXP";
940940
break;
941+
case UR_FUNCTION_LOADER_CONFIG_SET_MOCKING_ENABLED:
942+
os << "UR_FUNCTION_LOADER_CONFIG_SET_MOCKING_ENABLED";
943+
break;
941944
default:
942945
os << "unknown enumerator";
943946
break;
@@ -10277,6 +10280,25 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1027710280
return os;
1027810281
}
1027910282

10283+
///////////////////////////////////////////////////////////////////////////////
10284+
/// @brief Print operator for the ur_loader_config_set_mocking_enabled_params_t type
10285+
/// @returns
10286+
/// std::ostream &
10287+
inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct ur_loader_config_set_mocking_enabled_params_t *params) {
10288+
10289+
os << ".hLoaderConfig = ";
10290+
10291+
ur::details::printPtr(os,
10292+
*(params->phLoaderConfig));
10293+
10294+
os << ", ";
10295+
os << ".enable = ";
10296+
10297+
os << *(params->penable);
10298+
10299+
return os;
10300+
}
10301+
1028010302
///////////////////////////////////////////////////////////////////////////////
1028110303
/// @brief Print operator for the ur_platform_get_params_t type
1028210304
/// @returns
@@ -17332,6 +17354,9 @@ inline ur_result_t UR_APICALL printFunctionParams(std::ostream &os, ur_function_
1733217354
case UR_FUNCTION_LOADER_CONFIG_SET_CODE_LOCATION_CALLBACK: {
1733317355
os << (const struct ur_loader_config_set_code_location_callback_params_t *)params;
1733417356
} break;
17357+
case UR_FUNCTION_LOADER_CONFIG_SET_MOCKING_ENABLED: {
17358+
os << (const struct ur_loader_config_set_mocking_enabled_params_t *)params;
17359+
} break;
1733517360
case UR_FUNCTION_PLATFORM_GET: {
1733617361
os << (const struct ur_platform_get_params_t *)params;
1733717362
} break;

scripts/YaML.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,12 @@ class ur_name_t(Structure):
620620
- `out` is used for params that are write-only; if the param is a pointer, then the memory being pointed to is also write-only
621621
- `in,out` is used for params that are both read and write; typically this is used for pointers to other data structures that contain both read and write params
622622
- `nocheck` is used to specify that no additional validation checks will be generated.
623-
+ `desc` may include one the following annotations: {`"[optional]"`, `"[range(start,end)]"`, `"[release]"`, `"[typename(typeVarName)]"`, `"[bounds(offset,size)]"`}
623+
+ `desc` may include one the following annotations: {`"[optional]"`, `"[range(start,end)]"`, `"[retain]"`, `"[release]"`, `"[typename(typeVarName)]"`, `"[bounds(offset,size)]"`}
624624
- `optional` is used for params that are handles or pointers where it is legal for the value to be `nullptr`
625625
- `range` is used for params that are array pointers to specify the valid range that the is valid to read
626626
+ `start` and `end` must be an ISO-C standard identifier or literal
627627
+ `start` is inclusive and `end` is exclusive
628+
- `retain` is used for params that are handles or pointers to handles where the function will increment the reference counter associated with the handle(s).
628629
- `release` is used for params that are handles or pointers to handles where the function will decrement the handle's reference count, potentially leaving it in an invalid state if the reference count reaches zero.
629630
- `typename` is used to denote the type enum for params that are opaque pointers to values of tagged data types.
630631
- `bounds` is used for params that are memory objects or USM allocations. It specifies the range within the memory allocation represented by the param that will be accessed by the operation.

scripts/core/INTRO.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,37 @@ Currently, UR looks for these adapter libraries:
256256

257257
For more information about the usage of mentioned environment variables see `Environment Variables`_ section.
258258

259+
Mocking
260+
---------------------
261+
A mock UR adapter can be accessed for test purposes by enabling the ``MOCK``
262+
layer as described below. When the mock layer is enabled, calls to the API will
263+
still be intercepted by other layers (e.g. validation, tracing), but they will
264+
stop short of the loader - the call chain will end in either a generic fallback
265+
behavior defined by the mock layer itself, or a user defined replacement
266+
callback.
267+
268+
The default fallback behavior for entry points in the mock layer is to simply
269+
return ``UR_RESULT_SUCCESS``. For entry points concerning handles, i.e. those
270+
that create a new handle or modify the reference count of an existing one, a
271+
dummy handle mechanism is used. This means the layer will return generic
272+
handles that track a reference count, and ``Retain``/``Release`` entry points will
273+
function as expected when used with these handles.
274+
275+
During global setup the behavior of the mock layer can be customized by setting
276+
chain of structs, with each registering a callback with a given entry point in
277+
the API. Callbacks can be registered to be called ``BEFORE`` or ``AFTER`` the
278+
generic implementation, or they can be registered to entirely ``REPLACE`` it. A
279+
given entry point can only have one of each kind of callback associated with
280+
it, multiple structs with the same function/mode combination will override
281+
eachother.
282+
283+
The callback signature defined by ``${x}_mock_callback_t`` takes a single
284+
``void *`` parameter. When calling a user callback the layer will pack the
285+
entry point's parameters into the appropriate ``_params_t`` struct (e.g.
286+
``ur_adapter_get_params_t``) and pass a pointer to that struct into the
287+
callback. This allows parameters to be accessed and modified. The definitions
288+
for these parameter structs can be found in the main API header.
289+
259290
Layers
260291
---------------------
261292
UR comes with a mechanism that allows various API intercept layers to be enabled, either through the API or with an environment variable (see `Environment Variables`_).
@@ -278,6 +309,8 @@ Layers currently included with the runtime are as follows:
278309
- Enables the XPTI tracing layer, see Tracing_ for more detail.
279310
* - UR_LAYER_ASAN \| UR_LAYER_MSAN \| UR_LAYER_TSAN
280311
- Enables the device-side sanitizer layer, see Sanitizers_ for more detail.
312+
* - UR_LAYER_MOCK
313+
- Enables adapter mocking for test purposes. Similar behavior to the null adapter except entry points can be overridden or instrumented with callbacks. See Mocking_ for more detail.
281314

282315
Environment Variables
283316
---------------------

scripts/core/adapter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ params:
7070
- type: "$x_adapter_handle_t"
7171
name: hAdapter
7272
desc: |
73-
[in] Adapter handle to retain
73+
[in][retain] Adapter handle to retain
7474
--- #--------------------------------------------------------------------------
7575
type: function
7676
desc: "Get the last adapter specific error."

scripts/core/context.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ params:
8080
- type: "$x_context_handle_t"
8181
name: hContext
8282
desc: |
83-
[in] handle of the context to get a reference of.
83+
[in][retain] handle of the context to get a reference of.
8484
--- #--------------------------------------------------------------------------
8585
type: enum
8686
desc: "Supported context info"

scripts/core/device.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ params:
505505
- type: "$x_device_handle_t"
506506
name: hDevice
507507
desc: |
508-
[in] handle of the device to get a reference of.
508+
[in][retain] handle of the device to get a reference of.
509509
--- #--------------------------------------------------------------------------
510510
type: function
511511
desc: "Releases the device handle reference indicating end of its usage"

scripts/core/event.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ analogue:
230230
params:
231231
- type: $x_event_handle_t
232232
name: hEvent
233-
desc: "[in] handle of the event object"
233+
desc: "[in][retain] handle of the event object"
234234
returns:
235235
- $X_RESULT_ERROR_INVALID_EVENT
236236
- $X_RESULT_ERROR_OUT_OF_RESOURCES

scripts/core/exp-command-buffer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ name: RetainExp
252252
params:
253253
- type: $x_exp_command_buffer_handle_t
254254
name: hCommandBuffer
255-
desc: "[in] Handle of the command-buffer object."
255+
desc: "[in][retain] Handle of the command-buffer object."
256256
returns:
257257
- $X_RESULT_ERROR_INVALID_COMMAND_BUFFER_EXP
258258
- $X_RESULT_ERROR_OUT_OF_RESOURCES

0 commit comments

Comments
 (0)