Skip to content

Commit b5068d0

Browse files
lslusarczykbb-ur
authored andcommitted
better assertions (#20169)
- added printing backtrace to assertions - added debug and production assertions that break code immediately instead of throwing exception - asserting code immediately instead of throwing exception in debug mode for faster development - uninlining create_logger function - moved backtrace code from validation layer to common code
1 parent ed76466 commit b5068d0

21 files changed

+258
-182
lines changed

cmake/Assertions.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ if(UR_ENABLE_ASSERTIONS)
88
# MSVC doesn't like _DEBUG on release builds
99
if( NOT MSVC )
1010
add_compile_definitions(_DEBUG)
11+
add_compile_definitions(UR_DASSERT_ENABLED)
1112
endif()
1213
# On non-Debug builds cmake automatically defines NDEBUG, so we
1314
# explicitly undefine it:

source/adapters/level_zero/adapter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
569569
if (err == UR_RESULT_SUCCESS) {
570570
Platforms = std::move(platforms);
571571
} else {
572+
UR_LOG(ERR, "Failed to initialize Platforms");
572573
throw err;
573574
}
574575
}

source/adapters/level_zero/common.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,11 @@ void zeParseError(ze_result_t ZeError, const char *&ErrorString);
218218
#define ZE2UR_CALL_THROWS(ZeName, ZeArgs) \
219219
{ \
220220
ze_result_t ZeResult = ZeName ZeArgs; \
221-
if (auto Result = ZeCall().doCall(ZeResult, #ZeName, #ZeArgs, true)) \
221+
if (auto Result = ZeCall().doCall(ZeResult, #ZeName, #ZeArgs, true)) { \
222+
UR_DFAILURE("failed ZE call " #ZeName " with " #ZeArgs ", with result:" \
223+
<< Result); \
222224
throw ze2urResult(Result); \
225+
} \
223226
}
224227

225228
// Perform traced call to L0 without checking for errors

source/adapters/level_zero/v2/command_list_cache.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ command_list_cache_t::createCommandList(const command_list_descriptor_t &desc) {
109109
if (!ZeMutableCmdListExtentionSupported && IsMutable) {
110110
UR_LOG(INFO, "Mutable command lists were requested but are not supported "
111111
"by the driver.");
112+
UR_DFAILURE("Mutable command lists unsupported");
112113
throw UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
113114
}
114115
ZeStruct<ze_command_list_desc_t> CmdListDesc;

source/adapters/level_zero/v2/command_list_manager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ static void *getGlobalPointerFromModule(ze_module_handle_t hModule,
715715
ZE2UR_CALL_THROWS(zeModuleGetGlobalPointer,
716716
(hModule, name, &globalVarSize, &globalVarPtr));
717717
if (globalVarSize < offset + count) {
718+
UR_DFAILURE("Write device global variable is out of range");
718719
setErrorMessage("Write device global variable is out of range.",
719720
UR_RESULT_ERROR_INVALID_VALUE,
720721
static_cast<int32_t>(ZE_RESULT_ERROR_INVALID_ARGUMENT));
@@ -820,8 +821,7 @@ ur_result_t ur_command_list_manager::appendUSMAllocHelper(
820821
commandType = UR_COMMAND_ENQUEUE_USM_SHARED_ALLOC_EXP;
821822
break;
822823
default:
823-
UR_LOG(ERR, "enqueueUSMAllocHelper: unsupported USM type");
824-
throw UR_RESULT_ERROR_INVALID_ARGUMENT;
824+
UR_FFAILURE("enqueueUSMAllocHelper: unsupported USM type:" << type);
825825
}
826826

827827
auto zeSignalEvent = getSignalEvent(phEvent, commandType);

source/adapters/level_zero/v2/common.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ struct ze_handle_wrapper {
6969
ZE_CALL_NOCHECK_NAME(destroy, (handle), destroyName);
7070
// Gracefully handle the case that L0 was already unloaded.
7171
if (zeResult && (zeResult != ZE_RESULT_ERROR_UNINITIALIZED &&
72-
zeResult != ZE_RESULT_ERROR_UNKNOWN))
72+
zeResult != ZE_RESULT_ERROR_UNKNOWN)) {
73+
UR_DFAILURE("destroy failed in L0 with" << zeResult);
7374
throw ze2urResult(zeResult);
75+
}
7476
if (zeResult == ZE_RESULT_ERROR_UNKNOWN) {
7577
zeResult = ZE_RESULT_ERROR_UNINITIALIZED;
7678
}

source/adapters/level_zero/v2/context.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ struct ur_context_handle_t_ : ur_object {
5656
return eventPoolCacheImmediate;
5757
case PoolCacheType::Regular:
5858
return eventPoolCacheRegular;
59-
default:
60-
assert(false && "Requested invalid event pool cache type");
61-
throw UR_RESULT_ERROR_INVALID_VALUE;
6259
}
60+
UR_FFAILURE("Requested invalid event pool cache type");
6361
}
6462
// Checks if Device is covered by this context.
6563
// For that the Device or its root devices need to be in the context.

source/adapters/level_zero/v2/kernel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ ur_kernel_handle_t_::ur_kernel_handle_t_(
8484
ze_kernel_handle_t zeKernel = ur_cast<ze_kernel_handle_t>(hNativeKernel);
8585

8686
if (!zeKernel) {
87+
UR_DFAILURE("could not create kernel");
8788
throw UR_RESULT_ERROR_INVALID_KERNEL;
8889
}
8990

@@ -136,6 +137,7 @@ void ur_kernel_handle_t_::completeInitialization() {
136137

137138
size_t ur_kernel_handle_t_::deviceIndex(ur_device_handle_t hDevice) const {
138139
if (!hDevice) {
140+
UR_DFAILURE("invalid handle:" << hDevice);
139141
throw UR_RESULT_ERROR_INVALID_DEVICE;
140142
}
141143

@@ -145,6 +147,7 @@ size_t ur_kernel_handle_t_::deviceIndex(ur_device_handle_t hDevice) const {
145147
}
146148

147149
if (!deviceKernels[hDevice->Id.value()].has_value()) {
150+
UR_DFAILURE("invalid device:" << hDevice << ", not found in deviceKernels");
148151
throw UR_RESULT_ERROR_INVALID_DEVICE;
149152
}
150153

source/adapters/level_zero/v2/memory.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ static void migrateMemory(ze_command_list_handle_t cmdList, void *src,
302302
void *dst, size_t size,
303303
wait_list_view &waitListView) {
304304
if (!cmdList) {
305+
UR_DFAILURE("invalid handle in migrateMemory");
305306
throw UR_RESULT_ERROR_INVALID_NULL_HANDLE;
306307
}
307308
ZE2UR_CALL_THROWS(zeCommandListAppendMemoryCopy,
@@ -356,6 +357,7 @@ void ur_discrete_buffer_handle_t::unmapHostPtr(void *pMappedPtr,
356357
});
357358

358359
if (hostAlloc == hostAllocations.end()) {
360+
UR_DFAILURE("could not find pMappedPtr:" << pMappedPtr);
359361
throw UR_RESULT_ERROR_INVALID_ARGUMENT;
360362
}
361363

@@ -507,11 +509,15 @@ static void verifyImageRegion([[maybe_unused]] ze_image_desc_t &zeImageDesc,
507509
(zeImageDesc.format.layout == ZE_IMAGE_FORMAT_LAYOUT_16_16_16_16 &&
508510
rowPitch == 4 * 2 * zeRegion.width) ||
509511
(zeImageDesc.format.layout == ZE_IMAGE_FORMAT_LAYOUT_8_8_8_8 &&
510-
rowPitch == 4 * zeRegion.width)))
512+
rowPitch == 4 * zeRegion.width))) {
513+
UR_DFAILURE("image size is invalid");
511514
throw UR_RESULT_ERROR_INVALID_IMAGE_SIZE;
515+
}
512516
#endif
513-
if (!(slicePitch == 0 || slicePitch == rowPitch * zeRegion.height))
517+
if (!(slicePitch == 0 || slicePitch == rowPitch * zeRegion.height)) {
518+
UR_DFAILURE("image size is invalid");
514519
throw UR_RESULT_ERROR_INVALID_IMAGE_SIZE;
520+
}
515521
}
516522

517523
std::pair<ze_image_handle_t, ze_image_region_t>

source/adapters/level_zero/v2/queue_immediate_out_of_order.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ ur_result_t ur_queue_immediate_out_of_order_t::queueGetInfo(
6868
} else if (status == ZE_RESULT_NOT_READY) {
6969
return false;
7070
} else {
71+
UR_DFAILURE("getting queue info failed with: " << status);
7172
throw ze2urResult(status);
7273
}
7374
};

0 commit comments

Comments
 (0)