From 2275c74695e4e490f4561fc68fa59bee23764564 Mon Sep 17 00:00:00 2001 From: Konstantin Zhuravlyov Date: Wed, 21 Aug 2019 13:29:15 -0400 Subject: [PATCH] Loader: add basic logging abilities - Enabled with env var LOADER_ENABLE_LOGGING=1 Change-Id: Ibdbb1b55ffddb7dc9c63e52fc9db3013409376a4 --- runtime/hsa-runtime/loader/executable.cpp | 49 ++++++++++++++++++----- runtime/hsa-runtime/loader/executable.hpp | 34 ++++++++++++++++ 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/runtime/hsa-runtime/loader/executable.cpp b/runtime/hsa-runtime/loader/executable.cpp index e8dde0409f..7fb9a3f412 100644 --- a/runtime/hsa-runtime/loader/executable.cpp +++ b/runtime/hsa-runtime/loader/executable.cpp @@ -1036,6 +1036,7 @@ hsa_status_t ExecutableImpl::LoadCodeObject( { WriterLockGuard writer_lock(rw_lock_); if (HSA_EXECUTABLE_STATE_FROZEN == state_) { + logger_ << "LoaderError: executable is already frozen\n"; return HSA_STATUS_ERROR_FROZEN_EXECUTABLE; } @@ -1110,15 +1111,25 @@ hsa_status_t ExecutableImpl::LoadCodeObject( } std::string codeIsa; - if (!code->GetIsa(codeIsa)) { return HSA_STATUS_ERROR_INVALID_CODE_OBJECT; } - - uint32_t majorVersion, minorVersion; - if (!code->GetCodeObjectVersion(&majorVersion, &minorVersion)) { + if (!code->GetIsa(codeIsa)) { + logger_ << "LoaderError: failed to determine code object's ISA\n"; return HSA_STATUS_ERROR_INVALID_CODE_OBJECT; } - if (majorVersion != 1 && majorVersion != 2 && majorVersion != 3) { return HSA_STATUS_ERROR_INVALID_CODE_OBJECT; } - if (agent.handle == 0 && majorVersion == 1) { return HSA_STATUS_ERROR_INVALID_AGENT; } + uint32_t majorVersion, minorVersion; + if (!code->GetCodeObjectVersion(&majorVersion, &minorVersion)) { + logger_ << "LoaderError: failed to determine code object's version\n"; + return HSA_STATUS_ERROR_INVALID_CODE_OBJECT; + } + + if (majorVersion != 1 && majorVersion != 2 && majorVersion != 3) { + logger_ << "LoaderError: unsupported code object version: " << majorVersion << "\n"; + return HSA_STATUS_ERROR_INVALID_CODE_OBJECT; + } + if (agent.handle == 0 && majorVersion == 1) { + logger_ << "LoaderError: code object v1 requires non-null agent\n"; + return HSA_STATUS_ERROR_INVALID_AGENT; + } uint32_t codeHsailMajor; uint32_t codeHsailMinor; @@ -1129,13 +1140,18 @@ hsa_status_t ExecutableImpl::LoadCodeObject( codeProfile = profile_; } if (profile_ != codeProfile) { + logger_ << "LoaderError: mismatched profiles\n"; return HSA_STATUS_ERROR_INCOMPATIBLE_ARGUMENTS; } hsa_isa_t objectsIsa = context_->IsaFromName(codeIsa.c_str()); - if (!objectsIsa.handle) { return HSA_STATUS_ERROR_INVALID_ISA_NAME; } + if (!objectsIsa.handle) { + logger_ << "LoaderError: code object's ISA (" << codeIsa.c_str() << ") is invalid\n"; + return HSA_STATUS_ERROR_INVALID_ISA_NAME; + } if (agent.handle != 0 && !context_->IsaSupportedByAgent(agent, objectsIsa)) { + logger_ << "LoaderError: code object's ISA (" << codeIsa.c_str() << ") is not supported by the agent\n"; return HSA_STATUS_ERROR_INCOMPATIBLE_ARGUMENTS; } @@ -1411,6 +1427,8 @@ hsa_status_t ExecutableImpl::LoadDeclarationSymbol(hsa_agent_t agent, if (program_symbol == program_symbols_.end()) { auto agent_symbol = agent_symbols_.find(std::make_pair(sym->Name(), agent)); if (agent_symbol == agent_symbols_.end()) { + logger_ << "LoaderError: symbol \"" << sym->Name() << "\" is undefined\n"; + // TODO(spec): this is not spec compliant. return HSA_STATUS_ERROR_VARIABLE_UNDEFINED; } @@ -1523,7 +1541,10 @@ hsa_status_t ExecutableImpl::ApplyStaticRelocation(hsa_agent_t agent, amd::hsa:: sagent = nullptr; } SymbolImpl* esym = (SymbolImpl*) GetSymbolInternal(sym->name().c_str(), sagent); - if (!esym) { return HSA_STATUS_ERROR_VARIABLE_UNDEFINED; } + if (!esym) { + logger_ << "LoaderError: symbol \"" << sym->name() << "\" is undefined\n"; + return HSA_STATUS_ERROR_VARIABLE_UNDEFINED; + } addr = esym->address; break; } @@ -1697,8 +1718,10 @@ hsa_status_t ExecutableImpl::ApplyDynamicRelocation(hsa_agent_t agent, amd::hsa: switch (rel->type()) { case R_AMDGPU_32_HIGH: { - if (!symAddr) + if (!symAddr) { + logger_ << "LoaderError: symbol \"" << rel->symbol()->name() << "\" is undefined\n"; return HSA_STATUS_ERROR_VARIABLE_UNDEFINED; + } uint32_t symAddr32 = uint32_t((symAddr >> 32) & 0xFFFFFFFF); relSeg->Copy(rel->offset(), &symAddr32, sizeof(symAddr32)); @@ -1707,8 +1730,10 @@ hsa_status_t ExecutableImpl::ApplyDynamicRelocation(hsa_agent_t agent, amd::hsa: case R_AMDGPU_32_LOW: { - if (!symAddr) + if (!symAddr) { + logger_ << "LoaderError: symbol \"" << rel->symbol()->name() << "\" is undefined\n"; return HSA_STATUS_ERROR_VARIABLE_UNDEFINED; + } uint32_t symAddr32 = uint32_t(symAddr & 0xFFFFFFFF); relSeg->Copy(rel->offset(), &symAddr32, sizeof(symAddr32)); @@ -1717,8 +1742,10 @@ hsa_status_t ExecutableImpl::ApplyDynamicRelocation(hsa_agent_t agent, amd::hsa: case R_AMDGPU_64: { - if (!symAddr) + if (!symAddr) { + logger_ << "LoaderError: symbol \"" << rel->symbol()->name() << "\" is undefined\n"; return HSA_STATUS_ERROR_VARIABLE_UNDEFINED; + } relSeg->Copy(rel->offset(), &symAddr, sizeof(symAddr)); break; diff --git a/runtime/hsa-runtime/loader/executable.hpp b/runtime/hsa-runtime/loader/executable.hpp index b37e33a0ab..d8b9c37264 100644 --- a/runtime/hsa-runtime/loader/executable.hpp +++ b/runtime/hsa-runtime/loader/executable.hpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -224,6 +225,38 @@ private: VariableSymbol& operator=(const VariableSymbol &vs); }; +//===----------------------------------------------------------------------===// +// Logger. // +//===----------------------------------------------------------------------===// + +class Logger final { +public: + Logger(std::ostream &Stream = std::cerr) : OutStream(Stream) {} + + template + Logger &operator<<(const T &Data) { + if (!IsLoggingEnabled()) + return *this; + OutStream << Data; + return *this; + } + +private: + Logger(const Logger &L); + Logger& operator=(const Logger &L); + + bool IsLoggingEnabled() const { + const char *enable_logging = getenv("LOADER_ENABLE_LOGGING"); + if (!enable_logging) + return false; + if (std::string(enable_logging) == "0") + return false; + return true; + } + + std::ostream &OutStream; +}; + //===----------------------------------------------------------------------===// // Executable. // //===----------------------------------------------------------------------===// @@ -501,6 +534,7 @@ private: amd::hsa::common::ReaderWriterLock rw_lock_; hsa_profile_t profile_; Context *context_; + Logger logger_; const size_t id_; hsa_default_float_rounding_mode_t default_float_rounding_mode_; hsa_executable_state_t state_;