diff --git a/source/adapters/hip/adapter.cpp b/source/adapters/hip/adapter.cpp index 4691d78913..6c8a5c78a9 100644 --- a/source/adapters/hip/adapter.cpp +++ b/source/adapters/hip/adapter.cpp @@ -10,14 +10,44 @@ #include "adapter.hpp" #include "common.hpp" +#include "logger/ur_logger.hpp" #include #include struct ur_adapter_handle_t_ { std::atomic RefCount = 0; + logger::Logger &logger; + ur_adapter_handle_t_(); }; +class ur_legacy_sink : public logger::Sink { +public: + ur_legacy_sink(std::string logger_name = "", bool skip_prefix = true) + : Sink(std::move(logger_name), skip_prefix) { + this->ostream = &std::cerr; + } + + virtual void print([[maybe_unused]] logger::Level level, + const std::string &msg) override { + std::cerr << msg << std::endl; + } + + ~ur_legacy_sink() = default; +}; + +ur_adapter_handle_t_::ur_adapter_handle_t_() + : logger(logger::get_logger("hip")) { + + if (std::getenv("UR_LOG_HIP") != nullptr) + return; + + if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") != nullptr || + std::getenv("UR_SUPPRESS_ERROR_MESSAGE") != nullptr) { + logger.setLegacySink(std::make_unique()); + } +} + ur_adapter_handle_t_ adapter{}; UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet( diff --git a/source/adapters/hip/common.cpp b/source/adapters/hip/common.cpp index f1f8ec4fbb..da8cc2c765 100644 --- a/source/adapters/hip/common.cpp +++ b/source/adapters/hip/common.cpp @@ -8,6 +8,7 @@ // //===----------------------------------------------------------------------===// #include "common.hpp" +#include "logger/ur_logger.hpp" #include @@ -54,35 +55,34 @@ void checkErrorUR(amd_comgr_status_t Result, const char *Function, int Line, return; } - if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr || - std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) { - const char *ErrorString = nullptr; - const char *ErrorName = nullptr; - switch (Result) { - case AMD_COMGR_STATUS_ERROR: - ErrorName = "AMD_COMGR_STATUS_ERROR"; - ErrorString = "Generic error"; - break; - case AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT: - ErrorName = "AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT"; - ErrorString = - "One of the actual arguments does not meet a precondition stated in " - "the documentation of the corresponding formal argument."; - break; - case AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES: - ErrorName = "AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES"; - ErrorString = "Failed to allocate the necessary resources"; - break; - default: - break; - } - std::cerr << "\nUR HIP ERROR:" - << "\n\tValue: " << Result - << "\n\tName: " << ErrorName - << "\n\tDescription: " << ErrorString - << "\n\tFunction: " << Function - << "\n\tSource Location: " << File << ":" << Line << "\n\n"; + const char *ErrorString = nullptr; + const char *ErrorName = nullptr; + switch (Result) { + case AMD_COMGR_STATUS_ERROR: + ErrorName = "AMD_COMGR_STATUS_ERROR"; + ErrorString = "Generic error"; + break; + case AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT: + ErrorName = "AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT"; + ErrorString = + "One of the actual arguments does not meet a precondition stated in " + "the documentation of the corresponding formal argument."; + break; + case AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES: + ErrorName = "AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES"; + ErrorString = "Failed to allocate the necessary resources"; + break; + default: + break; } + std::stringstream SS; + SS << "\nUR HIP ERROR:" + << "\n\tValue: " << Result + << "\n\tName: " << ErrorName + << "\n\tDescription: " << ErrorString + << "\n\tFunction: " << Function << "\n\tSource Location: " << File + << ":" << Line << "\n"; + logger::error("{}", SS.str()); if (std::getenv("PI_HIP_ABORT") != nullptr || std::getenv("UR_HIP_ABORT") != nullptr) { @@ -99,19 +99,17 @@ void checkErrorUR(hipError_t Result, const char *Function, int Line, return; } - if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr || - std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) { - const char *ErrorString = nullptr; - const char *ErrorName = nullptr; - ErrorName = hipGetErrorName(Result); - ErrorString = hipGetErrorString(Result); - std::cerr << "\nUR HIP ERROR:" - << "\n\tValue: " << Result - << "\n\tName: " << ErrorName - << "\n\tDescription: " << ErrorString - << "\n\tFunction: " << Function - << "\n\tSource Location: " << File << ":" << Line << "\n\n"; - } + const char *ErrorString = hipGetErrorString(Result); + const char *ErrorName = hipGetErrorName(Result); + + std::stringstream SS; + SS << "\nUR HIP ERROR:" + << "\n\tValue: " << Result + << "\n\tName: " << ErrorName + << "\n\tDescription: " << ErrorString + << "\n\tFunction: " << Function << "\n\tSource Location: " << File + << ":" << Line << "\n"; + logger::error("{}", SS.str()); if (std::getenv("PI_HIP_ABORT") != nullptr || std::getenv("UR_HIP_ABORT") != nullptr) { @@ -127,13 +125,11 @@ void checkErrorUR(ur_result_t Result, const char *Function, int Line, return; } - if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr || - std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) { - std::cerr << "\nUR HIP ERROR:" - << "\n\tValue: " << Result - << "\n\tFunction: " << Function - << "\n\tSource Location: " << File << ":" << Line << "\n\n"; - } + std::stringstream SS; + SS << "\nUR HIP ERROR:" + << "\n\tValue: " << Result << "\n\tFunction: " << Function + << "\n\tSource Location: " << File << ":" << Line << "\n"; + logger::error("{}", SS.str()); if (std::getenv("PI_HIP_ABORT") != nullptr || std::getenv("UR_HIP_ABORT") != nullptr) { @@ -157,7 +153,7 @@ hipError_t getHipVersionString(std::string &Version) { } void detail::ur::die(const char *pMessage) { - std::cerr << "ur_die: " << pMessage << '\n'; + logger::always("ur_die: {}", pMessage); std::terminate(); } @@ -166,10 +162,6 @@ void detail::ur::assertion(bool Condition, const char *pMessage) { die(pMessage); } -void detail::ur::hipPrint(const char *pMessage) { - std::cerr << "ur_print: " << pMessage << '\n'; -} - // Global variables for UR_RESULT_ADAPTER_SPECIFIC_ERROR thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS; thread_local char ErrorMessage[MaxMessageSize];