-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathur_ldrddi.cpp
10619 lines (8984 loc) · 405 KB
/
ur_ldrddi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (C) 2022-2023 Intel Corporation
*
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
* Exceptions.
* See LICENSE.TXT
*
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
* @file ur_ldrddi.cpp
*
*/
#include "ur_lib_loader.hpp"
#include "ur_loader.hpp"
namespace ur_loader {
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urAdapterGet
__urdlllocal ur_result_t UR_APICALL urAdapterGet(
/// [in] the number of adapters to be added to phAdapters.
/// If phAdapters is not NULL, then NumEntries should be greater than
/// zero, otherwise ::UR_RESULT_ERROR_INVALID_SIZE,
/// will be returned.
uint32_t NumEntries,
/// [out][optional][range(0, NumEntries)][alloc] array of handle of
/// adapters. If NumEntries is less than the number of adapters available,
/// then
/// ::urAdapterGet shall only retrieve that number of adapters.
ur_adapter_handle_t *phAdapters,
/// [out][optional] returns the total number of adapters available.
uint32_t *pNumAdapters) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
size_t adapterIndex = 0;
if (nullptr != phAdapters && NumEntries != 0) {
for (auto &platform : context->platforms) {
if (platform.initStatus != UR_RESULT_SUCCESS)
continue;
platform.dditable.ur.Global.pfnAdapterGet(1, &phAdapters[adapterIndex],
nullptr);
try {
phAdapters[adapterIndex] = reinterpret_cast<ur_adapter_handle_t>(
context->factories.ur_adapter_factory.getInstance(
phAdapters[adapterIndex], &platform.dditable));
} catch (std::bad_alloc &) {
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
break;
}
adapterIndex++;
if (adapterIndex == NumEntries) {
break;
}
}
}
if (pNumAdapters != nullptr) {
*pNumAdapters = static_cast<uint32_t>(context->platforms.size());
}
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urAdapterRelease
__urdlllocal ur_result_t UR_APICALL urAdapterRelease(
/// [in][release] Adapter handle to release
ur_adapter_handle_t hAdapter) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->dditable;
auto pfnAdapterRelease = dditable->ur.Global.pfnAdapterRelease;
if (nullptr == pfnAdapterRelease)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hAdapter = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->handle;
// forward to device-platform
result = pfnAdapterRelease(hAdapter);
// release loader handle
context->factories.ur_adapter_factory.release(hAdapter);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urAdapterRetain
__urdlllocal ur_result_t UR_APICALL urAdapterRetain(
/// [in][retain] Adapter handle to retain
ur_adapter_handle_t hAdapter) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->dditable;
auto pfnAdapterRetain = dditable->ur.Global.pfnAdapterRetain;
if (nullptr == pfnAdapterRetain)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hAdapter = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->handle;
// forward to device-platform
result = pfnAdapterRetain(hAdapter);
// increment refcount of handle
context->factories.ur_adapter_factory.retain(hAdapter);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urAdapterGetLastError
__urdlllocal ur_result_t UR_APICALL urAdapterGetLastError(
/// [in] handle of the adapter instance
ur_adapter_handle_t hAdapter,
/// [out] pointer to a C string where the adapter specific error message
/// will be stored.
const char **ppMessage,
/// [out] pointer to an integer where the adapter specific error code will
/// be stored.
int32_t *pError) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->dditable;
auto pfnAdapterGetLastError = dditable->ur.Global.pfnAdapterGetLastError;
if (nullptr == pfnAdapterGetLastError)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hAdapter = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->handle;
// forward to device-platform
result = pfnAdapterGetLastError(hAdapter, ppMessage, pError);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urAdapterGetInfo
__urdlllocal ur_result_t UR_APICALL urAdapterGetInfo(
/// [in] handle of the adapter
ur_adapter_handle_t hAdapter,
/// [in] type of the info to retrieve
ur_adapter_info_t propName,
/// [in] the number of bytes pointed to by pPropValue.
size_t propSize,
/// [out][optional][typename(propName, propSize)] array of bytes holding
/// the info.
/// If Size is not equal to or greater to the real number of bytes needed
/// to return the info then the ::UR_RESULT_ERROR_INVALID_SIZE error is
/// returned and pPropValue is not used.
void *pPropValue,
/// [out][optional] pointer to the actual number of bytes being queried by
/// pPropValue.
size_t *pPropSizeRet) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->dditable;
auto pfnAdapterGetInfo = dditable->ur.Global.pfnAdapterGetInfo;
if (nullptr == pfnAdapterGetInfo)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hAdapter = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->handle;
// forward to device-platform
result =
pfnAdapterGetInfo(hAdapter, propName, propSize, pPropValue, pPropSizeRet);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urPlatformGet
__urdlllocal ur_result_t UR_APICALL urPlatformGet(
/// [in][range(0, NumAdapters)] array of adapters to query for platforms.
ur_adapter_handle_t *phAdapters,
/// [in] number of adapters pointed to by phAdapters
uint32_t NumAdapters,
/// [in] the number of platforms to be added to phPlatforms.
/// If phPlatforms is not NULL, then NumEntries should be greater than
/// zero, otherwise ::UR_RESULT_ERROR_INVALID_SIZE,
/// will be returned.
uint32_t NumEntries,
/// [out][optional][range(0, NumEntries)] array of handle of platforms.
/// If NumEntries is less than the number of platforms available, then
/// ::urPlatformGet shall only retrieve that number of platforms.
ur_platform_handle_t *phPlatforms,
/// [out][optional] returns the total number of platforms available.
uint32_t *pNumPlatforms) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
uint32_t total_platform_handle_count = 0;
for (uint32_t adapter_index = 0; adapter_index < NumAdapters;
adapter_index++) {
// extract adapter's function pointer table
auto dditable =
reinterpret_cast<ur_platform_object_t *>(phAdapters[adapter_index])
->dditable;
if ((0 < NumEntries) && (NumEntries == total_platform_handle_count))
break;
uint32_t library_platform_handle_count = 0;
result =
dditable->ur.Platform.pfnGet(&phAdapters[adapter_index], 1, 0, nullptr,
&library_platform_handle_count);
if (UR_RESULT_SUCCESS != result)
break;
if (nullptr != phPlatforms && NumEntries != 0) {
if (total_platform_handle_count + library_platform_handle_count >
NumEntries) {
library_platform_handle_count =
NumEntries - total_platform_handle_count;
}
result = dditable->ur.Platform.pfnGet(
&phAdapters[adapter_index], 1, library_platform_handle_count,
&phPlatforms[total_platform_handle_count], nullptr);
if (UR_RESULT_SUCCESS != result)
break;
try {
for (uint32_t i = 0; i < library_platform_handle_count; ++i) {
uint32_t platform_index = total_platform_handle_count + i;
phPlatforms[platform_index] = reinterpret_cast<ur_platform_handle_t>(
context->factories.ur_platform_factory.getInstance(
phPlatforms[platform_index], dditable));
}
} catch (std::bad_alloc &) {
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
}
total_platform_handle_count += library_platform_handle_count;
}
if (UR_RESULT_SUCCESS == result && pNumPlatforms != nullptr)
*pNumPlatforms = total_platform_handle_count;
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urPlatformGetInfo
__urdlllocal ur_result_t UR_APICALL urPlatformGetInfo(
/// [in] handle of the platform
ur_platform_handle_t hPlatform,
/// [in] type of the info to retrieve
ur_platform_info_t propName,
/// [in] the number of bytes pointed to by pPlatformInfo.
size_t propSize,
/// [out][optional][typename(propName, propSize)] array of bytes holding
/// the info.
/// If Size is not equal to or greater to the real number of bytes needed
/// to return the info then the ::UR_RESULT_ERROR_INVALID_SIZE error is
/// returned and pPlatformInfo is not used.
void *pPropValue,
/// [out][optional] pointer to the actual number of bytes being queried by
/// pPlatformInfo.
size_t *pPropSizeRet) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_platform_object_t *>(hPlatform)->dditable;
auto pfnGetInfo = dditable->ur.Platform.pfnGetInfo;
if (nullptr == pfnGetInfo)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hPlatform = reinterpret_cast<ur_platform_object_t *>(hPlatform)->handle;
// this value is needed for converting adapter handles to loader handles
size_t sizeret = 0;
if (pPropSizeRet == NULL)
pPropSizeRet = &sizeret;
// forward to device-platform
result = pfnGetInfo(hPlatform, propName, propSize, pPropValue, pPropSizeRet);
if (UR_RESULT_SUCCESS != result)
return result;
try {
if (pPropValue != nullptr) {
switch (propName) {
case UR_PLATFORM_INFO_ADAPTER: {
ur_adapter_handle_t *handles =
reinterpret_cast<ur_adapter_handle_t *>(pPropValue);
size_t nelements = *pPropSizeRet / sizeof(ur_adapter_handle_t);
for (size_t i = 0; i < nelements; ++i) {
if (handles[i] != nullptr) {
handles[i] = reinterpret_cast<ur_adapter_handle_t>(
context->factories.ur_adapter_factory.getInstance(handles[i],
dditable));
}
}
} break;
default: {
} break;
}
}
} catch (std::bad_alloc &) {
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urPlatformGetApiVersion
__urdlllocal ur_result_t UR_APICALL urPlatformGetApiVersion(
/// [in] handle of the platform
ur_platform_handle_t hPlatform,
/// [out] api version
ur_api_version_t *pVersion) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_platform_object_t *>(hPlatform)->dditable;
auto pfnGetApiVersion = dditable->ur.Platform.pfnGetApiVersion;
if (nullptr == pfnGetApiVersion)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hPlatform = reinterpret_cast<ur_platform_object_t *>(hPlatform)->handle;
// forward to device-platform
result = pfnGetApiVersion(hPlatform, pVersion);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urPlatformGetNativeHandle
__urdlllocal ur_result_t UR_APICALL urPlatformGetNativeHandle(
/// [in] handle of the platform.
ur_platform_handle_t hPlatform,
/// [out] a pointer to the native handle of the platform.
ur_native_handle_t *phNativePlatform) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_platform_object_t *>(hPlatform)->dditable;
auto pfnGetNativeHandle = dditable->ur.Platform.pfnGetNativeHandle;
if (nullptr == pfnGetNativeHandle)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hPlatform = reinterpret_cast<ur_platform_object_t *>(hPlatform)->handle;
// forward to device-platform
result = pfnGetNativeHandle(hPlatform, phNativePlatform);
if (UR_RESULT_SUCCESS != result)
return result;
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urPlatformCreateWithNativeHandle
__urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle(
/// [in][nocheck] the native handle of the platform.
ur_native_handle_t hNativePlatform,
/// [in] handle of the adapter associated with the native backend.
ur_adapter_handle_t hAdapter,
/// [in][optional] pointer to native platform properties struct.
const ur_platform_native_properties_t *pProperties,
/// [out][alloc] pointer to the handle of the platform object created.
ur_platform_handle_t *phPlatform) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->dditable;
auto pfnCreateWithNativeHandle =
dditable->ur.Platform.pfnCreateWithNativeHandle;
if (nullptr == pfnCreateWithNativeHandle)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hAdapter = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->handle;
// forward to device-platform
result = pfnCreateWithNativeHandle(hNativePlatform, hAdapter, pProperties,
phPlatform);
if (UR_RESULT_SUCCESS != result)
return result;
try {
// convert platform handle to loader handle
*phPlatform = reinterpret_cast<ur_platform_handle_t>(
context->factories.ur_platform_factory.getInstance(*phPlatform,
dditable));
} catch (std::bad_alloc &) {
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urPlatformGetBackendOption
__urdlllocal ur_result_t UR_APICALL urPlatformGetBackendOption(
/// [in] handle of the platform instance.
ur_platform_handle_t hPlatform,
/// [in] string containing the frontend option.
const char *pFrontendOption,
/// [out] returns the correct platform specific compiler option based on
/// the frontend option.
const char **ppPlatformOption) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_platform_object_t *>(hPlatform)->dditable;
auto pfnGetBackendOption = dditable->ur.Platform.pfnGetBackendOption;
if (nullptr == pfnGetBackendOption)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hPlatform = reinterpret_cast<ur_platform_object_t *>(hPlatform)->handle;
// forward to device-platform
result = pfnGetBackendOption(hPlatform, pFrontendOption, ppPlatformOption);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urDeviceGet
__urdlllocal ur_result_t UR_APICALL urDeviceGet(
/// [in] handle of the platform instance
ur_platform_handle_t hPlatform,
/// [in] the type of the devices.
ur_device_type_t DeviceType,
/// [in] the number of devices to be added to phDevices.
/// If phDevices is not NULL, then NumEntries should be greater than zero.
/// Otherwise ::UR_RESULT_ERROR_INVALID_SIZE
/// will be returned.
uint32_t NumEntries,
/// [out][optional][range(0, NumEntries)][alloc] array of handle of devices.
/// If NumEntries is less than the number of devices available, then
/// platform shall only retrieve that number of devices.
ur_device_handle_t *phDevices,
/// [out][optional] pointer to the number of devices.
/// pNumDevices will be updated with the total number of devices available.
uint32_t *pNumDevices) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_platform_object_t *>(hPlatform)->dditable;
auto pfnGet = dditable->ur.Device.pfnGet;
if (nullptr == pfnGet)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hPlatform = reinterpret_cast<ur_platform_object_t *>(hPlatform)->handle;
// forward to device-platform
result = pfnGet(hPlatform, DeviceType, NumEntries, phDevices, pNumDevices);
if (UR_RESULT_SUCCESS != result)
return result;
try {
// convert platform handles to loader handles
for (size_t i = 0; (nullptr != phDevices) && (i < NumEntries); ++i)
phDevices[i] = reinterpret_cast<ur_device_handle_t>(
context->factories.ur_device_factory.getInstance(phDevices[i],
dditable));
} catch (std::bad_alloc &) {
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urDeviceGetInfo
__urdlllocal ur_result_t UR_APICALL urDeviceGetInfo(
/// [in] handle of the device instance
ur_device_handle_t hDevice,
/// [in] type of the info to retrieve
ur_device_info_t propName,
/// [in] the number of bytes pointed to by pPropValue.
size_t propSize,
/// [out][optional][typename(propName, propSize)] array of bytes holding
/// the info.
/// If propSize is not equal to or greater than the real number of bytes
/// needed to return the info
/// then the ::UR_RESULT_ERROR_INVALID_SIZE error is returned and
/// pPropValue is not used.
void *pPropValue,
/// [out][optional] pointer to the actual size in bytes of the queried
/// propName.
size_t *pPropSizeRet) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_device_object_t *>(hDevice)->dditable;
auto pfnGetInfo = dditable->ur.Device.pfnGetInfo;
if (nullptr == pfnGetInfo)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hDevice = reinterpret_cast<ur_device_object_t *>(hDevice)->handle;
// this value is needed for converting adapter handles to loader handles
size_t sizeret = 0;
if (pPropSizeRet == NULL)
pPropSizeRet = &sizeret;
// forward to device-platform
result = pfnGetInfo(hDevice, propName, propSize, pPropValue, pPropSizeRet);
if (UR_RESULT_SUCCESS != result)
return result;
try {
if (pPropValue != nullptr) {
switch (propName) {
case UR_DEVICE_INFO_PLATFORM: {
ur_platform_handle_t *handles =
reinterpret_cast<ur_platform_handle_t *>(pPropValue);
size_t nelements = *pPropSizeRet / sizeof(ur_platform_handle_t);
for (size_t i = 0; i < nelements; ++i) {
if (handles[i] != nullptr) {
handles[i] = reinterpret_cast<ur_platform_handle_t>(
context->factories.ur_platform_factory.getInstance(handles[i],
dditable));
}
}
} break;
case UR_DEVICE_INFO_PARENT_DEVICE: {
ur_device_handle_t *handles =
reinterpret_cast<ur_device_handle_t *>(pPropValue);
size_t nelements = *pPropSizeRet / sizeof(ur_device_handle_t);
for (size_t i = 0; i < nelements; ++i) {
if (handles[i] != nullptr) {
handles[i] = reinterpret_cast<ur_device_handle_t>(
context->factories.ur_device_factory.getInstance(handles[i],
dditable));
}
}
} break;
case UR_DEVICE_INFO_COMPONENT_DEVICES: {
ur_device_handle_t *handles =
reinterpret_cast<ur_device_handle_t *>(pPropValue);
size_t nelements = *pPropSizeRet / sizeof(ur_device_handle_t);
for (size_t i = 0; i < nelements; ++i) {
if (handles[i] != nullptr) {
handles[i] = reinterpret_cast<ur_device_handle_t>(
context->factories.ur_device_factory.getInstance(handles[i],
dditable));
}
}
} break;
case UR_DEVICE_INFO_COMPOSITE_DEVICE: {
ur_device_handle_t *handles =
reinterpret_cast<ur_device_handle_t *>(pPropValue);
size_t nelements = *pPropSizeRet / sizeof(ur_device_handle_t);
for (size_t i = 0; i < nelements; ++i) {
if (handles[i] != nullptr) {
handles[i] = reinterpret_cast<ur_device_handle_t>(
context->factories.ur_device_factory.getInstance(handles[i],
dditable));
}
}
} break;
default: {
} break;
}
}
} catch (std::bad_alloc &) {
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urDeviceRetain
__urdlllocal ur_result_t UR_APICALL urDeviceRetain(
/// [in][retain] handle of the device to get a reference of.
ur_device_handle_t hDevice) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_device_object_t *>(hDevice)->dditable;
auto pfnRetain = dditable->ur.Device.pfnRetain;
if (nullptr == pfnRetain)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hDevice = reinterpret_cast<ur_device_object_t *>(hDevice)->handle;
// forward to device-platform
result = pfnRetain(hDevice);
// increment refcount of handle
context->factories.ur_device_factory.retain(hDevice);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urDeviceRelease
__urdlllocal ur_result_t UR_APICALL urDeviceRelease(
/// [in][release] handle of the device to release.
ur_device_handle_t hDevice) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_device_object_t *>(hDevice)->dditable;
auto pfnRelease = dditable->ur.Device.pfnRelease;
if (nullptr == pfnRelease)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hDevice = reinterpret_cast<ur_device_object_t *>(hDevice)->handle;
// forward to device-platform
result = pfnRelease(hDevice);
// release loader handle
context->factories.ur_device_factory.release(hDevice);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urDevicePartition
__urdlllocal ur_result_t UR_APICALL urDevicePartition(
/// [in] handle of the device to partition.
ur_device_handle_t hDevice,
/// [in] Device partition properties.
const ur_device_partition_properties_t *pProperties,
/// [in] the number of sub-devices.
uint32_t NumDevices,
/// [out][optional][range(0, NumDevices)] array of handle of devices.
/// If NumDevices is less than the number of sub-devices available, then
/// the function shall only retrieve that number of sub-devices.
ur_device_handle_t *phSubDevices,
/// [out][optional] pointer to the number of sub-devices the device can be
/// partitioned into according to the partitioning property.
uint32_t *pNumDevicesRet) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_device_object_t *>(hDevice)->dditable;
auto pfnPartition = dditable->ur.Device.pfnPartition;
if (nullptr == pfnPartition)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hDevice = reinterpret_cast<ur_device_object_t *>(hDevice)->handle;
// forward to device-platform
result = pfnPartition(hDevice, pProperties, NumDevices, phSubDevices,
pNumDevicesRet);
if (UR_RESULT_SUCCESS != result)
return result;
try {
// convert platform handles to loader handles
for (size_t i = 0; (nullptr != phSubDevices) && (i < NumDevices); ++i)
phSubDevices[i] = reinterpret_cast<ur_device_handle_t>(
context->factories.ur_device_factory.getInstance(phSubDevices[i],
dditable));
} catch (std::bad_alloc &) {
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urDeviceSelectBinary
__urdlllocal ur_result_t UR_APICALL urDeviceSelectBinary(
/// [in] handle of the device to select binary for.
ur_device_handle_t hDevice,
/// [in] the array of binaries to select from.
const ur_device_binary_t *pBinaries,
/// [in] the number of binaries passed in ppBinaries.
/// Must greater than or equal to zero otherwise
/// ::UR_RESULT_ERROR_INVALID_VALUE is returned.
uint32_t NumBinaries,
/// [out] the index of the selected binary in the input array of binaries.
/// If a suitable binary was not found the function returns
/// ::UR_RESULT_ERROR_INVALID_BINARY.
uint32_t *pSelectedBinary) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_device_object_t *>(hDevice)->dditable;
auto pfnSelectBinary = dditable->ur.Device.pfnSelectBinary;
if (nullptr == pfnSelectBinary)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hDevice = reinterpret_cast<ur_device_object_t *>(hDevice)->handle;
// forward to device-platform
result = pfnSelectBinary(hDevice, pBinaries, NumBinaries, pSelectedBinary);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urDeviceGetNativeHandle
__urdlllocal ur_result_t UR_APICALL urDeviceGetNativeHandle(
/// [in] handle of the device.
ur_device_handle_t hDevice,
/// [out] a pointer to the native handle of the device.
ur_native_handle_t *phNativeDevice) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_device_object_t *>(hDevice)->dditable;
auto pfnGetNativeHandle = dditable->ur.Device.pfnGetNativeHandle;
if (nullptr == pfnGetNativeHandle)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hDevice = reinterpret_cast<ur_device_object_t *>(hDevice)->handle;
// forward to device-platform
result = pfnGetNativeHandle(hDevice, phNativeDevice);
if (UR_RESULT_SUCCESS != result)
return result;
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urDeviceCreateWithNativeHandle
__urdlllocal ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
/// [in][nocheck] the native handle of the device.
ur_native_handle_t hNativeDevice,
/// [in] handle of the adapter to which `hNativeDevice` belongs
ur_adapter_handle_t hAdapter,
/// [in][optional] pointer to native device properties struct.
const ur_device_native_properties_t *pProperties,
/// [out][alloc] pointer to the handle of the device object created.
ur_device_handle_t *phDevice) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->dditable;
auto pfnCreateWithNativeHandle =
dditable->ur.Device.pfnCreateWithNativeHandle;
if (nullptr == pfnCreateWithNativeHandle)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hAdapter = reinterpret_cast<ur_adapter_object_t *>(hAdapter)->handle;
// forward to device-platform
result =
pfnCreateWithNativeHandle(hNativeDevice, hAdapter, pProperties, phDevice);
if (UR_RESULT_SUCCESS != result)
return result;
try {
// convert platform handle to loader handle
*phDevice = reinterpret_cast<ur_device_handle_t>(
context->factories.ur_device_factory.getInstance(*phDevice, dditable));
} catch (std::bad_alloc &) {
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urDeviceGetGlobalTimestamps
__urdlllocal ur_result_t UR_APICALL urDeviceGetGlobalTimestamps(
/// [in] handle of the device instance
ur_device_handle_t hDevice,
/// [out][optional] pointer to the Device's global timestamp that
/// correlates with the Host's global timestamp value
uint64_t *pDeviceTimestamp,
/// [out][optional] pointer to the Host's global timestamp that
/// correlates with the Device's global timestamp value
uint64_t *pHostTimestamp) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_device_object_t *>(hDevice)->dditable;
auto pfnGetGlobalTimestamps = dditable->ur.Device.pfnGetGlobalTimestamps;
if (nullptr == pfnGetGlobalTimestamps)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hDevice = reinterpret_cast<ur_device_object_t *>(hDevice)->handle;
// forward to device-platform
result = pfnGetGlobalTimestamps(hDevice, pDeviceTimestamp, pHostTimestamp);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urContextCreate
__urdlllocal ur_result_t UR_APICALL urContextCreate(
/// [in] the number of devices given in phDevices
uint32_t DeviceCount,
/// [in][range(0, DeviceCount)] array of handle of devices.
const ur_device_handle_t *phDevices,
/// [in][optional] pointer to context creation properties.
const ur_context_properties_t *pProperties,
/// [out][alloc] pointer to handle of context object created
ur_context_handle_t *phContext) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_device_object_t *>(*phDevices)->dditable;
auto pfnCreate = dditable->ur.Context.pfnCreate;
if (nullptr == pfnCreate)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handles to platform handles
auto phDevicesLocal = std::vector<ur_device_handle_t>(DeviceCount);
for (size_t i = 0; i < DeviceCount; ++i)
phDevicesLocal[i] =
reinterpret_cast<ur_device_object_t *>(phDevices[i])->handle;
// forward to device-platform
result =
pfnCreate(DeviceCount, phDevicesLocal.data(), pProperties, phContext);
if (UR_RESULT_SUCCESS != result)
return result;
try {
// convert platform handle to loader handle
*phContext = reinterpret_cast<ur_context_handle_t>(
context->factories.ur_context_factory.getInstance(*phContext,
dditable));
} catch (std::bad_alloc &) {
result = UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urContextRetain
__urdlllocal ur_result_t UR_APICALL urContextRetain(
/// [in][retain] handle of the context to get a reference of.
ur_context_handle_t hContext) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_context_object_t *>(hContext)->dditable;
auto pfnRetain = dditable->ur.Context.pfnRetain;
if (nullptr == pfnRetain)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hContext = reinterpret_cast<ur_context_object_t *>(hContext)->handle;
// forward to device-platform
result = pfnRetain(hContext);
// increment refcount of handle
context->factories.ur_context_factory.retain(hContext);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urContextRelease
__urdlllocal ur_result_t UR_APICALL urContextRelease(
/// [in][release] handle of the context to release.
ur_context_handle_t hContext) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_context_object_t *>(hContext)->dditable;
auto pfnRelease = dditable->ur.Context.pfnRelease;
if (nullptr == pfnRelease)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hContext = reinterpret_cast<ur_context_object_t *>(hContext)->handle;
// forward to device-platform
result = pfnRelease(hContext);
// release loader handle
context->factories.ur_context_factory.release(hContext);
return result;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for urContextGetInfo
__urdlllocal ur_result_t UR_APICALL urContextGetInfo(
/// [in] handle of the context
ur_context_handle_t hContext,
/// [in] type of the info to retrieve
ur_context_info_t propName,
/// [in] the number of bytes of memory pointed to by pPropValue.
size_t propSize,
/// [out][optional][typename(propName, propSize)] array of bytes holding
/// the info.
/// if propSize is not equal to or greater than the real number of bytes
/// needed to return
/// the info then the ::UR_RESULT_ERROR_INVALID_SIZE error is returned and
/// pPropValue is not used.
void *pPropValue,
/// [out][optional] pointer to the actual size in bytes of the queried
/// propName.
size_t *pPropSizeRet) {
ur_result_t result = UR_RESULT_SUCCESS;
[[maybe_unused]] auto context = getContext();
// extract platform's function pointer table
auto dditable = reinterpret_cast<ur_context_object_t *>(hContext)->dditable;
auto pfnGetInfo = dditable->ur.Context.pfnGetInfo;
if (nullptr == pfnGetInfo)
return UR_RESULT_ERROR_UNINITIALIZED;
// convert loader handle to platform handle
hContext = reinterpret_cast<ur_context_object_t *>(hContext)->handle;
// this value is needed for converting adapter handles to loader handles
size_t sizeret = 0;
if (pPropSizeRet == NULL)
pPropSizeRet = &sizeret;
// forward to device-platform
result = pfnGetInfo(hContext, propName, propSize, pPropValue, pPropSizeRet);
if (UR_RESULT_SUCCESS != result)
return result;
try {
if (pPropValue != nullptr) {
switch (propName) {
case UR_CONTEXT_INFO_DEVICES: {