Skip to content

Commit 7669e65

Browse files
committed
[CUDA][HIP][L0][NATIVECPU][OpenCL] Remove usage of die/terminate throughout adapters.
1 parent 41228d3 commit 7669e65

Some content is hidden

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

49 files changed

+498
-519
lines changed

source/adapters/cuda/common.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,6 @@ std::string getCudaVersionString() {
100100
return stream.str();
101101
}
102102

103-
void detail::ur::die(const char *Message) {
104-
std::cerr << "ur_die: " << Message << std::endl;
105-
std::terminate();
106-
}
107-
108-
void detail::ur::assertion(bool Condition, const char *Message) {
109-
if (!Condition)
110-
die(Message);
111-
}
112-
113103
void detail::ur::cuPrint(const char *Message) {
114104
std::cerr << "ur_print: " << Message << std::endl;
115105
}

source/adapters/cuda/common.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
#include <cuda.h>
1313
#include <ur/ur.hpp>
1414

15+
/**
16+
* Call an UR API and, if the result is not UR_RESULT_SUCCESS, automatically
17+
* return from the current function.
18+
*/
19+
#define UR_RETURN_ON_FAILURE(urCall) \
20+
if (const ur_result_t ur_result_macro = urCall; \
21+
ur_result_macro != UR_RESULT_SUCCESS) { \
22+
return ur_result_macro; \
23+
}
24+
1525
ur_result_t mapErrorUR(CUresult Result);
1626

1727
/// Converts CUDA error into UR error codes, and outputs error information
@@ -46,16 +56,8 @@ void setPluginSpecificMessage(CUresult cu_res);
4656
namespace detail {
4757
namespace ur {
4858

49-
// Report error and no return (keeps compiler from printing warnings).
50-
// TODO: Probably change that to throw a catchable exception,
51-
// but for now it is useful to see every failure.
52-
//
53-
[[noreturn]] void die(const char *Message);
54-
5559
// Reports error messages
5660
void cuPrint(const char *Message);
5761

58-
void assertion(bool Condition, const char *Message = nullptr);
59-
6062
} // namespace ur
6163
} // namespace detail

source/adapters/cuda/device.cpp

Lines changed: 102 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
5959
UR_CHECK_ERROR(cuDeviceGetAttribute(
6060
&ComputeUnits, CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT,
6161
hDevice->get()));
62-
detail::ur::assertion(ComputeUnits >= 0);
62+
if (ComputeUnits < 0) {
63+
return UR_RESULT_ERROR_INVALID_SIZE;
64+
}
6365
return ReturnValue(static_cast<uint32_t>(ComputeUnits));
6466
}
6567
case UR_DEVICE_INFO_MAX_WORK_ITEM_DIMENSIONS: {
@@ -73,15 +75,21 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
7375
int MaxX = 0, MaxY = 0, MaxZ = 0;
7476
UR_CHECK_ERROR(cuDeviceGetAttribute(
7577
&MaxX, CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X, hDevice->get()));
76-
detail::ur::assertion(MaxX >= 0);
78+
if (MaxX < 0) {
79+
return UR_RESULT_ERROR_INVALID_SIZE;
80+
}
7781

7882
UR_CHECK_ERROR(cuDeviceGetAttribute(
7983
&MaxY, CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Y, hDevice->get()));
80-
detail::ur::assertion(MaxY >= 0);
84+
if (MaxY < 0) {
85+
return UR_RESULT_ERROR_INVALID_SIZE;
86+
}
8187

8288
UR_CHECK_ERROR(cuDeviceGetAttribute(
8389
&MaxZ, CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Z, hDevice->get()));
84-
detail::ur::assertion(MaxZ >= 0);
90+
if (MaxZ < 0) {
91+
return UR_RESULT_ERROR_INVALID_SIZE;
92+
}
8593

8694
ReturnSizes.Sizes[0] = size_t(MaxX);
8795
ReturnSizes.Sizes[1] = size_t(MaxY);
@@ -96,15 +104,21 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
96104
int MaxX = 0, MaxY = 0, MaxZ = 0;
97105
UR_CHECK_ERROR(cuDeviceGetAttribute(
98106
&MaxX, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_X, hDevice->get()));
99-
detail::ur::assertion(MaxX >= 0);
107+
if (MaxX < 0) {
108+
return UR_RESULT_ERROR_INVALID_SIZE;
109+
}
100110

101111
UR_CHECK_ERROR(cuDeviceGetAttribute(
102112
&MaxY, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_Y, hDevice->get()));
103-
detail::ur::assertion(MaxY >= 0);
113+
if (MaxY < 0) {
114+
return UR_RESULT_ERROR_INVALID_SIZE;
115+
}
104116

105117
UR_CHECK_ERROR(cuDeviceGetAttribute(
106118
&MaxZ, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_Z, hDevice->get()));
107-
detail::ur::assertion(MaxZ >= 0);
119+
if (MaxZ < 0) {
120+
return UR_RESULT_ERROR_INVALID_SIZE;
121+
}
108122

109123
ReturnSizes.Sizes[0] = size_t(MaxX);
110124
ReturnSizes.Sizes[1] = size_t(MaxY);
@@ -118,7 +132,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
118132
&MaxWorkGroupSize, CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_BLOCK,
119133
hDevice->get()));
120134

121-
detail::ur::assertion(MaxWorkGroupSize >= 0);
135+
if (MaxWorkGroupSize < 0) {
136+
return UR_RESULT_ERROR_INVALID_SIZE;
137+
}
122138

123139
return ReturnValue(size_t(MaxWorkGroupSize));
124140
}
@@ -261,7 +277,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
261277
int ClockFreq = 0;
262278
UR_CHECK_ERROR(cuDeviceGetAttribute(
263279
&ClockFreq, CU_DEVICE_ATTRIBUTE_CLOCK_RATE, hDevice->get()));
264-
detail::ur::assertion(ClockFreq >= 0);
280+
if (ClockFreq < 0) {
281+
return UR_RESULT_ERROR_INVALID_SIZE;
282+
}
265283
return ReturnValue(static_cast<uint32_t>(ClockFreq) / 1000u);
266284
}
267285
case UR_DEVICE_INFO_ADDRESS_BITS: {
@@ -305,12 +323,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
305323
UR_CHECK_ERROR(cuDeviceGetAttribute(
306324
&TexHeight, CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_HEIGHT,
307325
hDevice->get()));
308-
detail::ur::assertion(TexHeight >= 0);
326+
if (TexHeight < 0) {
327+
return UR_RESULT_ERROR_INVALID_SIZE;
328+
}
309329
int SurfHeight = 0;
310330
UR_CHECK_ERROR(cuDeviceGetAttribute(
311331
&SurfHeight, CU_DEVICE_ATTRIBUTE_MAXIMUM_SURFACE2D_HEIGHT,
312332
hDevice->get()));
313-
detail::ur::assertion(SurfHeight >= 0);
333+
if (SurfHeight < 0) {
334+
return UR_RESULT_ERROR_INVALID_SIZE;
335+
}
314336

315337
int Min = std::min(TexHeight, SurfHeight);
316338

@@ -322,12 +344,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
322344
UR_CHECK_ERROR(cuDeviceGetAttribute(
323345
&TexWidth, CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_WIDTH,
324346
hDevice->get()));
325-
detail::ur::assertion(TexWidth >= 0);
347+
if (TexWidth < 0) {
348+
return UR_RESULT_ERROR_INVALID_SIZE;
349+
}
326350
int SurfWidth = 0;
327351
UR_CHECK_ERROR(cuDeviceGetAttribute(
328352
&SurfWidth, CU_DEVICE_ATTRIBUTE_MAXIMUM_SURFACE2D_WIDTH,
329353
hDevice->get()));
330-
detail::ur::assertion(SurfWidth >= 0);
354+
if (SurfWidth < 0) {
355+
return UR_RESULT_ERROR_INVALID_SIZE;
356+
}
331357

332358
int Min = std::min(TexWidth, SurfWidth);
333359

@@ -339,12 +365,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
339365
UR_CHECK_ERROR(cuDeviceGetAttribute(
340366
&TexHeight, CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_HEIGHT,
341367
hDevice->get()));
342-
detail::ur::assertion(TexHeight >= 0);
368+
if (TexHeight < 0) {
369+
return UR_RESULT_ERROR_INVALID_SIZE;
370+
}
343371
int SurfHeight = 0;
344372
UR_CHECK_ERROR(cuDeviceGetAttribute(
345373
&SurfHeight, CU_DEVICE_ATTRIBUTE_MAXIMUM_SURFACE3D_HEIGHT,
346374
hDevice->get()));
347-
detail::ur::assertion(SurfHeight >= 0);
375+
if (SurfHeight < 0) {
376+
return UR_RESULT_ERROR_INVALID_SIZE;
377+
}
348378

349379
int Min = std::min(TexHeight, SurfHeight);
350380

@@ -356,12 +386,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
356386
UR_CHECK_ERROR(cuDeviceGetAttribute(
357387
&TexWidth, CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_WIDTH,
358388
hDevice->get()));
359-
detail::ur::assertion(TexWidth >= 0);
389+
if (TexWidth < 0) {
390+
return UR_RESULT_ERROR_INVALID_SIZE;
391+
}
360392
int SurfWidth = 0;
361393
UR_CHECK_ERROR(cuDeviceGetAttribute(
362394
&SurfWidth, CU_DEVICE_ATTRIBUTE_MAXIMUM_SURFACE3D_WIDTH,
363395
hDevice->get()));
364-
detail::ur::assertion(SurfWidth >= 0);
396+
if (SurfWidth < 0) {
397+
return UR_RESULT_ERROR_INVALID_SIZE;
398+
}
365399

366400
int Min = std::min(TexWidth, SurfWidth);
367401

@@ -373,12 +407,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
373407
UR_CHECK_ERROR(cuDeviceGetAttribute(
374408
&TexDepth, CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_DEPTH,
375409
hDevice->get()));
376-
detail::ur::assertion(TexDepth >= 0);
410+
if (TexDepth < 0) {
411+
return UR_RESULT_ERROR_INVALID_SIZE;
412+
}
377413
int SurfDepth = 0;
378414
UR_CHECK_ERROR(cuDeviceGetAttribute(
379415
&SurfDepth, CU_DEVICE_ATTRIBUTE_MAXIMUM_SURFACE3D_DEPTH,
380416
hDevice->get()));
381-
detail::ur::assertion(SurfDepth >= 0);
417+
if (SurfDepth < 0) {
418+
return UR_RESULT_ERROR_INVALID_SIZE;
419+
}
382420

383421
int Min = std::min(TexDepth, SurfDepth);
384422

@@ -390,12 +428,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
390428
UR_CHECK_ERROR(cuDeviceGetAttribute(
391429
&TexWidth, CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_WIDTH,
392430
hDevice->get()));
393-
detail::ur::assertion(TexWidth >= 0);
431+
if (TexWidth < 0) {
432+
return UR_RESULT_ERROR_INVALID_SIZE;
433+
}
394434
int SurfWidth = 0;
395435
UR_CHECK_ERROR(cuDeviceGetAttribute(
396436
&SurfWidth, CU_DEVICE_ATTRIBUTE_MAXIMUM_SURFACE1D_WIDTH,
397437
hDevice->get()));
398-
detail::ur::assertion(SurfWidth >= 0);
438+
if (SurfWidth < 0) {
439+
return UR_RESULT_ERROR_INVALID_SIZE;
440+
}
399441

400442
int Min = std::min(TexWidth, SurfWidth);
401443

@@ -464,23 +506,28 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
464506
int CacheSize = 0;
465507
UR_CHECK_ERROR(cuDeviceGetAttribute(
466508
&CacheSize, CU_DEVICE_ATTRIBUTE_L2_CACHE_SIZE, hDevice->get()));
467-
detail::ur::assertion(CacheSize >= 0);
509+
if (CacheSize < 0) {
510+
return UR_RESULT_ERROR_INVALID_SIZE;
511+
}
468512
// The L2 cache is global to the GPU.
469513
return ReturnValue(static_cast<uint64_t>(CacheSize));
470514
}
471515
case UR_DEVICE_INFO_GLOBAL_MEM_SIZE: {
472516
size_t Bytes = 0;
473517
// Runtime API has easy access to this value, driver API info is scarse.
474-
detail::ur::assertion(cuDeviceTotalMem(&Bytes, hDevice->get()) ==
475-
CUDA_SUCCESS);
518+
if (cuDeviceTotalMem(&Bytes, hDevice->get()) != CUDA_SUCCESS) {
519+
return UR_RESULT_ERROR_INVALID_OPERATION;
520+
}
476521
return ReturnValue(uint64_t{Bytes});
477522
}
478523
case UR_DEVICE_INFO_MAX_CONSTANT_BUFFER_SIZE: {
479524
int ConstantMemory = 0;
480525
UR_CHECK_ERROR(cuDeviceGetAttribute(
481526
&ConstantMemory, CU_DEVICE_ATTRIBUTE_TOTAL_CONSTANT_MEMORY,
482527
hDevice->get()));
483-
detail::ur::assertion(ConstantMemory >= 0);
528+
if (ConstantMemory < 0) {
529+
return UR_RESULT_ERROR_INVALID_SIZE;
530+
}
484531

485532
return ReturnValue(static_cast<uint64_t>(ConstantMemory));
486533
}
@@ -510,7 +557,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
510557
UR_CHECK_ERROR(cuDeviceGetAttribute(
511558
&ECCEnabled, CU_DEVICE_ATTRIBUTE_ECC_ENABLED, hDevice->get()));
512559

513-
detail::ur::assertion((ECCEnabled == 0) | (ECCEnabled == 1));
560+
if ((ECCEnabled != 0) | (ECCEnabled != 1)) {
561+
return UR_RESULT_ERROR_INVALID_OPERATION;
562+
}
514563
auto Result = static_cast<bool>(ECCEnabled);
515564
return ReturnValue(Result);
516565
}
@@ -519,7 +568,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
519568
UR_CHECK_ERROR(cuDeviceGetAttribute(
520569
&IsIntegrated, CU_DEVICE_ATTRIBUTE_INTEGRATED, hDevice->get()));
521570

522-
detail::ur::assertion((IsIntegrated == 0) | (IsIntegrated == 1));
571+
if ((IsIntegrated != 0) | (IsIntegrated != 1)) {
572+
return UR_RESULT_ERROR_INVALID_OPERATION;
573+
}
523574
auto result = static_cast<bool>(IsIntegrated);
524575
return ReturnValue(result);
525576
}
@@ -800,24 +851,28 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
800851
case UR_DEVICE_INFO_GLOBAL_MEM_FREE: {
801852
size_t FreeMemory = 0;
802853
size_t TotalMemory = 0;
803-
detail::ur::assertion(cuMemGetInfo(&FreeMemory, &TotalMemory) ==
804-
CUDA_SUCCESS,
805-
"failed cuMemGetInfo() API.");
854+
if (cuMemGetInfo(&FreeMemory, &TotalMemory) != CUDA_SUCCESS) {
855+
return UR_RESULT_ERROR_INVALID_OPERATION;
856+
}
806857
return ReturnValue(FreeMemory);
807858
}
808859
case UR_DEVICE_INFO_MEMORY_CLOCK_RATE: {
809860
int Value = 0;
810861
UR_CHECK_ERROR(cuDeviceGetAttribute(
811862
&Value, CU_DEVICE_ATTRIBUTE_MEMORY_CLOCK_RATE, hDevice->get()));
812-
detail::ur::assertion(Value >= 0);
863+
if (Value < 0) {
864+
return UR_RESULT_ERROR_INVALID_SIZE;
865+
}
813866
// Convert kilohertz to megahertz when returning.
814867
return ReturnValue(Value / 1000);
815868
}
816869
case UR_DEVICE_INFO_MEMORY_BUS_WIDTH: {
817870
int Value = 0;
818871
UR_CHECK_ERROR(cuDeviceGetAttribute(
819872
&Value, CU_DEVICE_ATTRIBUTE_GLOBAL_MEMORY_BUS_WIDTH, hDevice->get()));
820-
detail::ur::assertion(Value >= 0);
873+
if (Value < 0) {
874+
return UR_RESULT_ERROR_INVALID_SIZE;
875+
}
821876
return ReturnValue(Value);
822877
}
823878
case UR_DEVICE_INFO_MAX_COMPUTE_QUEUE_INDICES: {
@@ -905,17 +960,21 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
905960
int Value = 0;
906961
UR_CHECK_ERROR(cuDeviceGetAttribute(
907962
&Value, CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID, hDevice->get()));
908-
detail::ur::assertion(Value >= 0);
963+
if (Value < 0) {
964+
return UR_RESULT_ERROR_INVALID_SIZE;
965+
}
909966
return ReturnValue(Value);
910967
}
911968
case UR_DEVICE_INFO_UUID: {
912969
CUuuid UUID;
913970
#if (CUDA_VERSION >= 11040)
914-
detail::ur::assertion(cuDeviceGetUuid_v2(&UUID, hDevice->get()) ==
915-
CUDA_SUCCESS);
971+
if (cuDeviceGetUuid_v2(&UUID, hDevice->get()) != CUDA_SUCCESS) {
972+
return UR_RESULT_ERROR_INVALID_DEVICE;
973+
}
916974
#else
917-
detail::ur::assertion(cuDeviceGetUuid(&UUID, hDevice->get()) ==
918-
CUDA_SUCCESS);
975+
if (cuDeviceGetUuid(&UUID, hDevice->get()) != CUDA_SUCCESS) {
976+
return UR_RESULT_ERROR_INVALID_DEVICE;
977+
}
919978
#endif
920979
std::array<unsigned char, 16> Name;
921980
std::copy(UUID.bytes, UUID.bytes + 16, Name.begin());
@@ -994,7 +1053,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
9941053
&MaxRegisters, CU_DEVICE_ATTRIBUTE_MAX_REGISTERS_PER_BLOCK,
9951054
hDevice->get()));
9961055

997-
detail::ur::assertion(MaxRegisters >= 0);
1056+
if (MaxRegisters < 0) {
1057+
return UR_RESULT_ERROR_INVALID_SIZE;
1058+
}
9981059

9991060
return ReturnValue(static_cast<uint32_t>(MaxRegisters));
10001061
}
@@ -1008,7 +1069,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
10081069
UR_CHECK_ERROR(
10091070
cuDeviceGetPCIBusId(AddressBuffer, AddressBufferSize, hDevice->get()));
10101071
// CUDA API (8.x - 12.1) guarantees 12 bytes + \0 are written
1011-
detail::ur::assertion(strnlen(AddressBuffer, AddressBufferSize) == 12);
1072+
if (strnlen(AddressBuffer, AddressBufferSize) != 12) {
1073+
return UR_RESULT_ERROR_INVALID_OPERATION;
1074+
}
10121075
return ReturnValue(AddressBuffer,
10131076
strnlen(AddressBuffer, AddressBufferSize - 1) + 1);
10141077
}

0 commit comments

Comments
 (0)