Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sycl/cmake/modules/BuildUnifiedRuntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ set(UR_BUILD_EXAMPLES "${SYCL_UR_BUILD_TESTS}" CACHE BOOL "" FORCE)
option(SYCL_UR_FORMAT_CPP_STYLE "Format code style of UR C++ sources" OFF)
set(UR_FORMAT_CPP_STYLE "${SYCL_UR_FORMAT_CPP_STYLE}" CACHE BOOL "" FORCE)

option(SYCL_UR_ENABLE_ASSERTIONS "Enable assertions for all UR build types" OFF)
set(UR_ENABLE_ASSERTIONS "${SYCL_UR_ENABLE_ASSERTIONS}" CACHE BOOL "" FORCE)

# Here we override the defaults to unified-runtime
set(UR_BUILD_XPTI_LIBS OFF CACHE BOOL "")
set(UR_ENABLE_SYMBOLIZER ON CACHE BOOL "Enable symbolizer for sanitizer layer.")
Expand Down
1 change: 1 addition & 0 deletions unified-runtime/cmake/Assertions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if(UR_ENABLE_ASSERTIONS)
# MSVC doesn't like _DEBUG on release builds
if( NOT MSVC )
add_compile_definitions(_DEBUG)
add_compile_definitions(UR_DASSERT_ENABLED)
endif()
# On non-Debug builds cmake automatically defines NDEBUG, so we
# explicitly undefine it:
Expand Down
1 change: 1 addition & 0 deletions unified-runtime/source/adapters/level_zero/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
if (err == UR_RESULT_SUCCESS) {
Platforms = std::move(platforms);
} else {
UR_LOG(ERR, "Failed to initialize Platforms");
throw err;
}
}
Expand Down
5 changes: 4 additions & 1 deletion unified-runtime/source/adapters/level_zero/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,11 @@ void zeParseError(ze_result_t ZeError, const char *&ErrorString);
#define ZE2UR_CALL_THROWS(ZeName, ZeArgs) \
{ \
ze_result_t ZeResult = ZeName ZeArgs; \
if (auto Result = ZeCall().doCall(ZeResult, #ZeName, #ZeArgs, true)) \
if (auto Result = ZeCall().doCall(ZeResult, #ZeName, #ZeArgs, true)) { \
UR_DFAILURE("failed ZE call " #ZeName " with " #ZeArgs ", with result:" \
<< Result); \
throw ze2urResult(Result); \
} \
}

// Perform traced call to L0 without checking for errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ command_list_cache_t::createCommandList(const command_list_descriptor_t &desc) {
if (!ZeMutableCmdListExtentionSupported && IsMutable) {
UR_LOG(INFO, "Mutable command lists were requested but are not supported "
"by the driver.");
UR_DFAILURE("Mutable command lists unsupported");
throw UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ZeStruct<ze_command_list_desc_t> CmdListDesc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ static void *getGlobalPointerFromModule(ze_module_handle_t hModule,
ZE2UR_CALL_THROWS(zeModuleGetGlobalPointer,
(hModule, name, &globalVarSize, &globalVarPtr));
if (globalVarSize < offset + count) {
UR_DFAILURE("Write device global variable is out of range");
setErrorMessage("Write device global variable is out of range.",
UR_RESULT_ERROR_INVALID_VALUE,
static_cast<int32_t>(ZE_RESULT_ERROR_INVALID_ARGUMENT));
Expand Down Expand Up @@ -820,8 +821,7 @@ ur_result_t ur_command_list_manager::appendUSMAllocHelper(
commandType = UR_COMMAND_ENQUEUE_USM_SHARED_ALLOC_EXP;
break;
default:
UR_LOG(ERR, "enqueueUSMAllocHelper: unsupported USM type");
throw UR_RESULT_ERROR_INVALID_ARGUMENT;
UR_FFAILURE("enqueueUSMAllocHelper: unsupported USM type:" << type);
}

auto zeSignalEvent = getSignalEvent(phEvent, commandType);
Expand Down
4 changes: 3 additions & 1 deletion unified-runtime/source/adapters/level_zero/v2/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ struct ze_handle_wrapper {
ZE_CALL_NOCHECK_NAME(destroy, (handle), destroyName);
// Gracefully handle the case that L0 was already unloaded.
if (zeResult && (zeResult != ZE_RESULT_ERROR_UNINITIALIZED &&
zeResult != ZE_RESULT_ERROR_UNKNOWN))
zeResult != ZE_RESULT_ERROR_UNKNOWN)) {
UR_DFAILURE("destroy failed in L0 with" << zeResult);
throw ze2urResult(zeResult);
}
if (zeResult == ZE_RESULT_ERROR_UNKNOWN) {
zeResult = ZE_RESULT_ERROR_UNINITIALIZED;
}
Expand Down
4 changes: 1 addition & 3 deletions unified-runtime/source/adapters/level_zero/v2/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ struct ur_context_handle_t_ : ur_object {
return eventPoolCacheImmediate;
case PoolCacheType::Regular:
return eventPoolCacheRegular;
default:
assert(false && "Requested invalid event pool cache type");
throw UR_RESULT_ERROR_INVALID_VALUE;
}
UR_FFAILURE("Requested invalid event pool cache type");
}
// Checks if Device is covered by this context.
// For that the Device or its root devices need to be in the context.
Expand Down
3 changes: 3 additions & 0 deletions unified-runtime/source/adapters/level_zero/v2/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ ur_kernel_handle_t_::ur_kernel_handle_t_(
ze_kernel_handle_t zeKernel = ur_cast<ze_kernel_handle_t>(hNativeKernel);

if (!zeKernel) {
UR_DFAILURE("could not create kernel");
throw UR_RESULT_ERROR_INVALID_KERNEL;
}

Expand Down Expand Up @@ -136,6 +137,7 @@ void ur_kernel_handle_t_::completeInitialization() {

size_t ur_kernel_handle_t_::deviceIndex(ur_device_handle_t hDevice) const {
if (!hDevice) {
UR_DFAILURE("invalid handle:" << hDevice);
throw UR_RESULT_ERROR_INVALID_DEVICE;
}

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

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

Expand Down
10 changes: 8 additions & 2 deletions unified-runtime/source/adapters/level_zero/v2/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ static void migrateMemory(ze_command_list_handle_t cmdList, void *src,
void *dst, size_t size,
wait_list_view &waitListView) {
if (!cmdList) {
UR_DFAILURE("invalid handle in migrateMemory");
throw UR_RESULT_ERROR_INVALID_NULL_HANDLE;
}
ZE2UR_CALL_THROWS(zeCommandListAppendMemoryCopy,
Expand Down Expand Up @@ -356,6 +357,7 @@ void ur_discrete_buffer_handle_t::unmapHostPtr(void *pMappedPtr,
});

if (hostAlloc == hostAllocations.end()) {
UR_DFAILURE("could not find pMappedPtr:" << pMappedPtr);
throw UR_RESULT_ERROR_INVALID_ARGUMENT;
}

Expand Down Expand Up @@ -507,11 +509,15 @@ static void verifyImageRegion([[maybe_unused]] ze_image_desc_t &zeImageDesc,
(zeImageDesc.format.layout == ZE_IMAGE_FORMAT_LAYOUT_16_16_16_16 &&
rowPitch == 4 * 2 * zeRegion.width) ||
(zeImageDesc.format.layout == ZE_IMAGE_FORMAT_LAYOUT_8_8_8_8 &&
rowPitch == 4 * zeRegion.width)))
rowPitch == 4 * zeRegion.width))) {
UR_DFAILURE("image size is invalid");
throw UR_RESULT_ERROR_INVALID_IMAGE_SIZE;
}
#endif
if (!(slicePitch == 0 || slicePitch == rowPitch * zeRegion.height))
if (!(slicePitch == 0 || slicePitch == rowPitch * zeRegion.height)) {
UR_DFAILURE("image size is invalid");
throw UR_RESULT_ERROR_INVALID_IMAGE_SIZE;
}
}

std::pair<ze_image_handle_t, ze_image_region_t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ ur_result_t ur_queue_immediate_out_of_order_t::queueGetInfo(
} else if (status == ZE_RESULT_NOT_READY) {
return false;
} else {
UR_DFAILURE("getting queue info failed with: " << status);
throw ze2urResult(status);
}
};
Expand Down
16 changes: 10 additions & 6 deletions unified-runtime/source/adapters/level_zero/v2/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

static inline void UMF_CALL_THROWS(umf_result_t res) {
if (res != UMF_RESULT_SUCCESS) {
UR_DFAILURE("some umf call in v2 L0 adapter returned "
<< res << " instead of success");
throw res;
}
}
Expand Down Expand Up @@ -80,9 +82,10 @@ inline umf_usm_memory_type_t urToUmfMemoryType(ur_usm_type_t type) {
return UMF_MEMORY_TYPE_SHARED;
case UR_USM_TYPE_HOST:
return UMF_MEMORY_TYPE_HOST;
default:
throw UR_RESULT_ERROR_INVALID_ARGUMENT;
case UR_USM_TYPE_UNKNOWN:
case UR_USM_TYPE_FORCE_UINT32:; // silence warning, fail below
}
UR_FFAILURE("invalid memory type: " << type);
}

static usm::DisjointPoolMemType
Expand All @@ -93,14 +96,14 @@ descToDisjoinPoolMemType(const usm::pool_descriptor &desc) {
case UR_USM_TYPE_SHARED: {
if (desc.deviceReadOnly)
return usm::DisjointPoolMemType::SharedReadOnly;
else
return usm::DisjointPoolMemType::Shared;
return usm::DisjointPoolMemType::Shared;
}
case UR_USM_TYPE_HOST:
return usm::DisjointPoolMemType::Host;
default:
throw UR_RESULT_ERROR_INVALID_ARGUMENT;
case UR_USM_TYPE_UNKNOWN:
case UR_USM_TYPE_FORCE_UINT32:; // silence warning, fail below
}
UR_FFAILURE("invalid memory type: " << desc.type);
}

static umf::provider_unique_handle_t
Expand Down Expand Up @@ -143,6 +146,7 @@ makeProvider(usm::pool_descriptor poolDescriptor) {
auto [ret, provider] =
umf::providerMakeUniqueFromOps(umfLevelZeroMemoryProviderOps(), hParams);
if (ret != UMF_RESULT_SUCCESS) {
UR_DFAILURE("umf::providerMakeUniqueFromOps failed with " << ret);
throw umf::umf2urResult(ret);
}

Expand Down
25 changes: 25 additions & 0 deletions unified-runtime/source/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,38 @@ endif()
add_ur_library(ur_common STATIC
ur_util.cpp
ur_util.hpp
logger/ur_logger.cpp
logger/ur_logger.hpp
latency_tracker.hpp
offload_bundle_parser.cpp
offload_bundle_parser.hpp
$<$<PLATFORM_ID:Windows>:windows/ur_lib_loader.cpp>
$<$<PLATFORM_ID:Linux,Darwin>:linux/ur_lib_loader.cpp>
)

# link validation backtrace dependencies
if(UNIX)
find_package(Libbacktrace)
endif()
if (VAL_USE_LIBBACKTRACE_BACKTRACE AND LIBBACKTRACE_FOUND)
message(STATUS "Using libbacktrace backtrace")

target_sources(ur_common PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/backtrace_libbacktrace.cpp)
target_link_libraries(ur_common PRIVATE Libbacktrace)
else()
message(STATUS "Using default backtrace")

if(WIN32)
target_sources(ur_common PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/backtrace_win.cpp)
target_link_libraries(ur_common PRIVATE dbghelp)
else()
target_sources(ur_common PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/backtrace_lin.cpp)
endif()
endif()

add_library(${PROJECT_NAME}::common ALIAS ur_common)

target_include_directories(ur_common PUBLIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#ifndef UR_BACKTRACE_H
#define UR_BACKTRACE_H 1

#include "ur_validation_layer.hpp"

#define MAX_BACKTRACE_FRAMES 64
#include <string>
#include <vector>

namespace ur_validation_layer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
*
*/
#include "backtrace.hpp"

#include <execinfo.h>
#include <vector>

namespace ur_validation_layer {

#define MAX_BACKTRACE_FRAMES 64

std::vector<BacktraceLine> getCurrentBacktrace() {
void *backtraceFrames[MAX_BACKTRACE_FRAMES];
int frameCount = backtrace(backtraceFrames, MAX_BACKTRACE_FRAMES);
char **backtraceStr = backtrace_symbols(backtraceFrames, frameCount);

int frameCount = ::backtrace(backtraceFrames, MAX_BACKTRACE_FRAMES);
char **backtraceStr = ::backtrace_symbols(backtraceFrames, frameCount);
// TODO: implement getting demangled symbols using abi::__cxa_demangle
if (backtraceStr == nullptr) {
return std::vector<BacktraceLine>(1, "Failed to acquire a backtrace");
}
Expand Down
115 changes: 115 additions & 0 deletions unified-runtime/source/common/logger/ur_logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
*
* Copyright (C) 2022-2025 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
*
*/

#include <algorithm>

#include "../backtrace.hpp"
#include "ur_logger.hpp"

namespace logger {

void print_backtrace() {
for (auto btLine : ur_validation_layer::getCurrentBacktrace()) {
std::cerr << btLine << std::endl;
}
}

static bool str_to_bool(const std::string &str) {
if (!str.empty()) {
std::string lower_value = str;
std::transform(lower_value.begin(), lower_value.end(), lower_value.begin(),
[](unsigned char c) { return std::tolower(c); });
const std::initializer_list<std::string> true_str = {"y", "yes", "t",
"true", "1"};
return std::find(true_str.begin(), true_str.end(), lower_value) !=
true_str.end();
}
return false;
}

Logger create_logger(std::string logger_name, bool skip_prefix,
bool skip_linebreak, ur_logger_level_t default_log_level) {
std::transform(logger_name.begin(), logger_name.end(), logger_name.begin(),
::toupper);

const std::string env_var_name = "UR_LOG_" + logger_name;
const auto default_flush_level = UR_LOGGER_LEVEL_ERROR;
const std::string default_output = "stderr";
const bool default_fileline = false;
auto flush_level = default_flush_level;
ur_logger_level_t level = default_log_level;
bool fileline = default_fileline;
std::unique_ptr<Sink> sink;

try {
auto map = getenv_to_map(env_var_name.c_str());
if (!map.has_value()) {
return Logger(default_log_level,
std::make_unique<logger::StderrSink>(
std::move(logger_name), skip_prefix, skip_linebreak));
}

auto kv = map->find("level");
if (kv != map->end()) {
auto value = kv->second.front();
level = str_to_level(std::move(value));
map->erase(kv);
}

kv = map->find("flush");
if (kv != map->end()) {
auto value = kv->second.front();
flush_level = str_to_level(std::move(value));
map->erase(kv);
}

kv = map->find("fileline");
if (kv != map->end()) {
auto value = kv->second.front();
fileline = str_to_bool(std::move(value));
map->erase(kv);
}

std::vector<std::string> values = {default_output};
kv = map->find("output");
if (kv != map->end()) {
values = kv->second;
map->erase(kv);
}

if (!map->empty()) {
std::cerr << "Wrong logger environment variable parameter: '"
<< map->begin()->first << "'. Default logger options are set.";
return Logger(default_log_level,
std::make_unique<logger::StderrSink>(
std::move(logger_name), skip_prefix, skip_linebreak));
}

sink = values.size() == 2 ? sink_from_str(logger_name, values[0], values[1],
skip_prefix, skip_linebreak)
: sink_from_str(logger_name, values[0], "",
skip_prefix, skip_linebreak);
} catch (const std::invalid_argument &e) {
std::cerr << "Error when creating a logger instance from the '"
<< env_var_name << "' environment variable:\n"
<< e.what() << std::endl;
return Logger(default_log_level,
std::make_unique<logger::StderrSink>(
std::move(logger_name), skip_prefix, skip_linebreak));
}

sink->setFlushLevel(flush_level);
sink->setFileLine(fileline);

return Logger(level, std::move(sink));
}

} // namespace logger
Loading
Loading