Skip to content

Commit 76c6bf9

Browse files
authored
Merge pull request #1780 from aarongreig/aaron/mockAdapter
Rework null adapter into mock adapter
2 parents fa43450 + 2bc0e0a commit 76c6bf9

Some content is hidden

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

75 files changed

+6894
-2075
lines changed

examples/collector/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $ mkdir build
1919
$ cd build
2020
$ cmake .. -DUR_ENABLE_TRACING=ON
2121
$ make
22-
$ UR_ADAPTERS_FORCE_LOAD=./lib/libur_adapter_null.so UR_ENABLE_LAYERS=UR_LAYER_TRACING XPTI_TRACE_ENABLE=1 XPTI_FRAMEWORK_DISPATCHER=./lib/libxptifw.so XPTI_SUBSCRIBERS=./lib/libcollector.so ./bin/hello_world
22+
$ UR_ADAPTERS_FORCE_LOAD=./lib/libur_adapter_mock.so UR_ENABLE_LAYERS=UR_LAYER_TRACING XPTI_TRACE_ENABLE=1 XPTI_FRAMEWORK_DISPATCHER=./lib/libxptifw.so XPTI_SUBSCRIBERS=./lib/libcollector.so ./bin/hello_world
2323
```
2424

2525
See [XPTI framework documentation](https://github.com/intel/llvm/blob/sycl/xptifw/doc/XPTI_Framework.md) for more information.

include/ur_api.h

+47-14
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 = 229, ///< 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,29 @@ urLoaderConfigSetCodeLocationCallback(
742743
void *pUserData ///< [in][out][optional] pointer to data to be passed to callback.
743744
);
744745

746+
///////////////////////////////////////////////////////////////////////////////
747+
/// @brief The only adapter reported with mock enabled will be the mock adapter.
748+
///
749+
/// @details
750+
/// - The mock adapter will default to returning ::UR_RESULT_SUCCESS for all
751+
/// entry points. It will also create and correctly reference count dummy
752+
/// handles where appropriate. Its behaviour can be modified by linking
753+
/// the mock library and using the object accessed via
754+
/// mock::getCallbacks().
755+
///
756+
/// @returns
757+
/// - ::UR_RESULT_SUCCESS
758+
/// - ::UR_RESULT_ERROR_UNINITIALIZED
759+
/// - ::UR_RESULT_ERROR_DEVICE_LOST
760+
/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC
761+
/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE
762+
/// + `NULL == hLoaderConfig`
763+
UR_APIEXPORT ur_result_t UR_APICALL
764+
urLoaderConfigSetMockingEnabled(
765+
ur_loader_config_handle_t hLoaderConfig, ///< [in] Handle to config object mocking will be enabled for.
766+
ur_bool_t enable ///< [in] Handle to config object the layer will be enabled for.
767+
);
768+
745769
///////////////////////////////////////////////////////////////////////////////
746770
/// @brief Initialize the 'oneAPI' loader
747771
///
@@ -863,7 +887,7 @@ urAdapterRelease(
863887
/// + `NULL == hAdapter`
864888
UR_APIEXPORT ur_result_t UR_APICALL
865889
urAdapterRetain(
866-
ur_adapter_handle_t hAdapter ///< [in] Adapter handle to retain
890+
ur_adapter_handle_t hAdapter ///< [in][retain] Adapter handle to retain
867891
);
868892

869893
///////////////////////////////////////////////////////////////////////////////
@@ -1736,7 +1760,7 @@ urDeviceGetInfo(
17361760
/// + `NULL == hDevice`
17371761
UR_APIEXPORT ur_result_t UR_APICALL
17381762
urDeviceRetain(
1739-
ur_device_handle_t hDevice ///< [in] handle of the device to get a reference of.
1763+
ur_device_handle_t hDevice ///< [in][retain] handle of the device to get a reference of.
17401764
);
17411765

17421766
///////////////////////////////////////////////////////////////////////////////
@@ -2217,7 +2241,7 @@ urContextCreate(
22172241
/// + `NULL == hContext`
22182242
UR_APIEXPORT ur_result_t UR_APICALL
22192243
urContextRetain(
2220-
ur_context_handle_t hContext ///< [in] handle of the context to get a reference of.
2244+
ur_context_handle_t hContext ///< [in][retain] handle of the context to get a reference of.
22212245
);
22222246

22232247
///////////////////////////////////////////////////////////////////////////////
@@ -2739,7 +2763,7 @@ urMemBufferCreate(
27392763
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
27402764
UR_APIEXPORT ur_result_t UR_APICALL
27412765
urMemRetain(
2742-
ur_mem_handle_t hMem ///< [in] handle of the memory object to get access
2766+
ur_mem_handle_t hMem ///< [in][retain] handle of the memory object to get access
27432767
);
27442768

27452769
///////////////////////////////////////////////////////////////////////////////
@@ -3130,7 +3154,7 @@ urSamplerCreate(
31303154
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
31313155
UR_APIEXPORT ur_result_t UR_APICALL
31323156
urSamplerRetain(
3133-
ur_sampler_handle_t hSampler ///< [in] handle of the sampler object to get access
3157+
ur_sampler_handle_t hSampler ///< [in][retain] handle of the sampler object to get access
31343158
);
31353159

31363160
///////////////////////////////////////////////////////////////////////////////
@@ -3690,7 +3714,7 @@ urUSMPoolCreate(
36903714
/// + `NULL == pPool`
36913715
UR_APIEXPORT ur_result_t UR_APICALL
36923716
urUSMPoolRetain(
3693-
ur_usm_pool_handle_t pPool ///< [in] pointer to USM memory pool
3717+
ur_usm_pool_handle_t pPool ///< [in][retain] pointer to USM memory pool
36943718
);
36953719

36963720
///////////////////////////////////////////////////////////////////////////////
@@ -4046,7 +4070,7 @@ urPhysicalMemCreate(
40464070
/// + `NULL == hPhysicalMem`
40474071
UR_APIEXPORT ur_result_t UR_APICALL
40484072
urPhysicalMemRetain(
4049-
ur_physical_mem_handle_t hPhysicalMem ///< [in] handle of the physical memory object to retain.
4073+
ur_physical_mem_handle_t hPhysicalMem ///< [in][retain] handle of the physical memory object to retain.
40504074
);
40514075

40524076
///////////////////////////////////////////////////////////////////////////////
@@ -4334,7 +4358,7 @@ urProgramLink(
43344358
/// + `NULL == hProgram`
43354359
UR_APIEXPORT ur_result_t UR_APICALL
43364360
urProgramRetain(
4337-
ur_program_handle_t hProgram ///< [in] handle for the Program to retain
4361+
ur_program_handle_t hProgram ///< [in][retain] handle for the Program to retain
43384362
);
43394363

43404364
///////////////////////////////////////////////////////////////////////////////
@@ -4985,7 +5009,7 @@ urKernelGetSubGroupInfo(
49855009
/// + `NULL == hKernel`
49865010
UR_APIEXPORT ur_result_t UR_APICALL
49875011
urKernelRetain(
4988-
ur_kernel_handle_t hKernel ///< [in] handle for the Kernel to retain
5012+
ur_kernel_handle_t hKernel ///< [in][retain] handle for the Kernel to retain
49895013
);
49905014

49915015
///////////////////////////////////////////////////////////////////////////////
@@ -5492,7 +5516,7 @@ urQueueCreate(
54925516
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
54935517
UR_APIEXPORT ur_result_t UR_APICALL
54945518
urQueueRetain(
5495-
ur_queue_handle_t hQueue ///< [in] handle of the queue object to get access
5519+
ur_queue_handle_t hQueue ///< [in][retain] handle of the queue object to get access
54965520
);
54975521

54985522
///////////////////////////////////////////////////////////////////////////////
@@ -5887,7 +5911,7 @@ urEventWait(
58875911
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
58885912
UR_APIEXPORT ur_result_t UR_APICALL
58895913
urEventRetain(
5890-
ur_event_handle_t hEvent ///< [in] handle of the event object
5914+
ur_event_handle_t hEvent ///< [in][retain] handle of the event object
58915915
);
58925916

58935917
///////////////////////////////////////////////////////////////////////////////
@@ -8252,7 +8276,7 @@ urCommandBufferCreateExp(
82528276
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
82538277
UR_APIEXPORT ur_result_t UR_APICALL
82548278
urCommandBufferRetainExp(
8255-
ur_exp_command_buffer_handle_t hCommandBuffer ///< [in] Handle of the command-buffer object.
8279+
ur_exp_command_buffer_handle_t hCommandBuffer ///< [in][retain] Handle of the command-buffer object.
82568280
);
82578281

82588282
///////////////////////////////////////////////////////////////////////////////
@@ -8799,7 +8823,7 @@ urCommandBufferEnqueueExp(
87998823
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
88008824
UR_APIEXPORT ur_result_t UR_APICALL
88018825
urCommandBufferRetainCommandExp(
8802-
ur_exp_command_buffer_command_handle_t hCommand ///< [in] Handle of the command-buffer command.
8826+
ur_exp_command_buffer_command_handle_t hCommand ///< [in][retain] Handle of the command-buffer command.
88038827
);
88048828

88058829
///////////////////////////////////////////////////////////////////////////////
@@ -9657,6 +9681,15 @@ typedef struct ur_loader_config_set_code_location_callback_params_t {
96579681
void **ppUserData;
96589682
} ur_loader_config_set_code_location_callback_params_t;
96599683

9684+
///////////////////////////////////////////////////////////////////////////////
9685+
/// @brief Function parameters for urLoaderConfigSetMockingEnabled
9686+
/// @details Each entry is a pointer to the parameter passed to the function;
9687+
/// allowing the callback the ability to modify the parameter's value
9688+
typedef struct ur_loader_config_set_mocking_enabled_params_t {
9689+
ur_loader_config_handle_t *phLoaderConfig;
9690+
ur_bool_t *penable;
9691+
} ur_loader_config_set_mocking_enabled_params_t;
9692+
96609693
///////////////////////////////////////////////////////////////////////////////
96619694
/// @brief Function parameters for urPlatformGet
96629695
/// @details Each entry is a pointer to the parameter passed to the function;

include/ur_print.h

+8
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

+25
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

+2-1
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

+27
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,33 @@ 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 it via
262+
${x}LoaderConfigSetMockingEnabled.
263+
264+
The default fallback behavior for entry points in the mock adapter is to simply
265+
return ``UR_RESULT_SUCCESS``. For entry points concerning handles, i.e. those
266+
that create a new handle or modify the reference count of an existing one, a
267+
dummy handle mechanism is used. This means the adapter will return generic
268+
handles that track a reference count, and ``Retain``/``Release`` entry points will
269+
function as expected when used with these handles.
270+
271+
The behavior of the mock adapter can be customized by linking the
272+
``unified-runtime::mock`` library and making use of the object accessed via the
273+
``mock::getCallbacks()`` helper. Callbacks can be passed into this object to
274+
run either before or after a given entry point, or they can be set to entirely
275+
replace the default behavior. Only one callback of each type (before, replace,
276+
after) can be set per entry point, with subsequent callbacks set in the same
277+
"slot" overwriting any set previously.
278+
279+
The callback signature defined by ``ur_mock_callback_t`` takes a single
280+
``void *`` parameter. When calling a user callback the adapter will pack the
281+
entry point's parameters into the appropriate ``_params_t`` struct (e.g.
282+
``ur_adapter_get_params_t``) and pass a pointer to that struct into the
283+
callback. This allows parameters to be accessed and modified. The definitions
284+
for these parameter structs can be found in the main API header.
285+
259286
Layers
260287
---------------------
261288
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`_).

scripts/core/adapter.yml

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+2-2
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
@@ -879,7 +879,7 @@ name: RetainCommandExp
879879
params:
880880
- type: $x_exp_command_buffer_command_handle_t
881881
name: hCommand
882-
desc: "[in] Handle of the command-buffer command."
882+
desc: "[in][retain] Handle of the command-buffer command."
883883
returns:
884884
- $X_RESULT_ERROR_INVALID_COMMAND_BUFFER_COMMAND_HANDLE_EXP
885885
- $X_RESULT_ERROR_OUT_OF_RESOURCES

scripts/core/kernel.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ details:
292292
params:
293293
- type: $x_kernel_handle_t
294294
name: hKernel
295-
desc: "[in] handle for the Kernel to retain"
295+
desc: "[in][retain] handle for the Kernel to retain"
296296
--- #--------------------------------------------------------------------------
297297
type: function
298298
desc: "Release Kernel."

scripts/core/loader.yml

+17-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ details:
5252
params:
5353
- type: $x_loader_config_handle_t
5454
name: hLoaderConfig
55-
desc: "[in] loader config handle to retain"
55+
desc: "[in][retain] loader config handle to retain"
5656
--- #--------------------------------------------------------------------------
5757
type: function
5858
desc: "Release config handle."
@@ -188,6 +188,22 @@ params:
188188
desc: "[in][out][optional] pointer to data to be passed to callback."
189189
--- #--------------------------------------------------------------------------
190190
type: function
191+
desc: "The only adapter reported with mock enabled will be the mock adapter."
192+
details:
193+
- "The mock adapter will default to returning $X_RESULT_SUCCESS for all entry points. It will also create and correctly reference count dummy handles where appropriate. Its behaviour can be modified by linking the mock library and using the object accessed via mock::getCallbacks()."
194+
class: $xLoaderConfig
195+
loader_only: True
196+
name: "SetMockingEnabled"
197+
decl: static
198+
params:
199+
- type: $x_loader_config_handle_t
200+
name: hLoaderConfig
201+
desc: "[in] Handle to config object mocking will be enabled for."
202+
- type: $x_bool_t
203+
name: enable
204+
desc: "[in] Handle to config object the layer will be enabled for."
205+
--- #--------------------------------------------------------------------------
206+
type: function
191207
desc: "Initialize the $OneApi loader"
192208
class: $xLoader
193209
loader_only: True

0 commit comments

Comments
 (0)