Optimize IE_THROW and IE_ASSERT for size reduction (#18284)

* Optimize IE_THROW and IE_ASSERT for size reduction

* Fix build issues
This commit is contained in:
Pawel Raasz 2023-06-30 09:52:21 +02:00 committed by GitHub
parent 1f7176031d
commit f52ab4f9ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 68 additions and 41 deletions

View File

@ -511,10 +511,8 @@ void InferenceEnginePython::IEExecNetwork::createInferRequests(int num_requests)
if (code != InferenceEngine::StatusCode::OK) {
IE_EXCEPTION_SWITCH(code,
ExceptionType,
InferenceEngine::details::ThrowNow<ExceptionType>{} <<=
std::stringstream{}
<< IE_LOCATION
<< InferenceEngine::details::ExceptionTraits<ExceptionType>::string());
InferenceEngine::details::ThrowNow<ExceptionType>{IE_LOCATION_PARAM} <<=
std::stringstream{});
}
auto end_time = Time::now();

View File

@ -164,8 +164,8 @@ protected:
if (sts != OK) {
IE_EXCEPTION_SWITCH(sts,
ExceptionType,
InferenceEngine::details::ThrowNow<ExceptionType>{} <<=
std::stringstream{} << IE_LOCATION << desc.msg)
InferenceEngine::details::ThrowNow<ExceptionType>{IE_LOCATION_PARAM} <<=
std::stringstream{} << desc.msg)
}
IE_SUPPRESS_DEPRECATED_START
_ptr = std::shared_ptr<T>(object, [](T* ptr) {

View File

@ -328,7 +328,14 @@ IE_SUPPRESS_DEPRECATED_END
namespace details {
template <typename ExceptionType>
struct ExceptionTraits;
}
template <>
struct INFERENCE_ENGINE_1_0_DEPRECATED ExceptionTraits<InferenceEngineException> {
static const char* string() {
return "";
}
};
} // namespace details
#define INFERENCE_ENGINE_DECLARE_EXCEPTION(ExceptionType, statusCode) \
struct INFERENCE_ENGINE_1_0_DEPRECATED INFERENCE_ENGINE_API_CLASS(ExceptionType) final \
@ -400,20 +407,45 @@ namespace details {
/**
* @brief Tag struct used to throw exception
*/
#ifndef NDEBUG
template <typename ExceptionType>
struct INFERENCE_ENGINE_1_0_DEPRECATED ThrowNow final {
[[noreturn]] void operator<<=(const std::ostream& ostream) {
std::ostringstream stream;
stream << ostream.rdbuf();
const char* const file;
const int line;
[[noreturn]] static void create(const std::ostream& ostream, const char* file, int line) {
std::stringstream stream;
stream << '\n' << file << ':' << line << ' ';
stream << ExceptionTraits<ExceptionType>::string() << ' ' << ostream.rdbuf();
throw ExceptionType{stream.str()};
}
[[noreturn]] void operator<<=(const std::ostream& ostream) {
create(ostream, file, line);
}
};
#else
template <typename ExceptionType>
struct INFERENCE_ENGINE_1_0_DEPRECATED ThrowNow final {
[[noreturn]] static void create(const std::ostream& ostream) {
std::stringstream stream;
stream << ExceptionTraits<ExceptionType>::string() << ' ' << ostream.rdbuf();
throw ExceptionType{stream.str()};
}
[[noreturn]] void operator<<=(const std::ostream& ostream) {
create(ostream);
}
};
#endif
/// @cond
#ifndef NDEBUG
# define IE_LOCATION '\n' << __FILE__ << ':' << __LINE__ << ' '
# define IE_LOCATION '\n' << __FILE__ << ':' << __LINE__ << ' '
# define IE_LOCATION_PARAM __FILE__, __LINE__
#else
# define IE_LOCATION ""
# define IE_LOCATION_PARAM
#endif // NDEBUG
// WARNING: DO NOT USE THIS MACRO! Use openvino/util/pp.hpp macro library
@ -430,13 +462,10 @@ struct INFERENCE_ENGINE_1_0_DEPRECATED ThrowNow final {
// ENDWARNING
#define IE_THROW_0() \
InferenceEngine::details::ThrowNow<InferenceEngine::GeneralError>{} <<= std::stringstream{} << IE_LOCATION
(InferenceEngine::details::ThrowNow<InferenceEngine::GeneralError>{IE_LOCATION_PARAM}) <<= std::stringstream {}
#define IE_THROW_1(ExceptionType) \
InferenceEngine::details::ThrowNow<InferenceEngine::ExceptionType>{} <<= \
std::stringstream{} << IE_LOCATION \
<< InferenceEngine::details::ExceptionTraits<InferenceEngine::ExceptionType>::string() \
<< ' '
#define IE_THROW_1(ExceptionType) \
(InferenceEngine::details::ThrowNow<InferenceEngine::ExceptionType>{IE_LOCATION_PARAM}) <<= std::stringstream {}
/// @endcond
/**
@ -452,7 +481,7 @@ struct INFERENCE_ENGINE_1_0_DEPRECATED ThrowNow final {
#ifdef NDEBUG
# define IE_ASSERT(EXPRESSION) \
if (!(EXPRESSION)) \
IE_THROW(GeneralError) << " AssertionFailed: " << #EXPRESSION
IE_THROW(GeneralError) << " AssertionError " #EXPRESSION
#else
/**
* @private
@ -470,9 +499,9 @@ struct NullStream {
#endif // NDEBUG
/// @cond
#define THROW_IE_EXCEPTION \
InferenceEngine::details::ThrowNow<InferenceEngine::details::InferenceEngineException>{} <<= std::stringstream{} \
<< IE_LOCATION
#define THROW_IE_EXCEPTION \
(InferenceEngine::details::ThrowNow<InferenceEngine::details::InferenceEngineException>{IE_LOCATION_PARAM}) <<= \
std::stringstream {}
#define IE_EXCEPTION_CASE(TYPE_ALIAS, STATUS_CODE, EXCEPTION_TYPE, ...) \
case InferenceEngine::STATUS_CODE: { \

View File

@ -59,15 +59,16 @@ namespace InferenceEngine {
}
#define CALL_STATUS_FNC(function, ...) \
if (!actual) \
if (!actual) { \
IE_THROW() << "Wrapper used was not initialized."; \
} \
ResponseDesc resp; \
auto res = actual->function(__VA_ARGS__, &resp); \
if (res != OK) \
IE_EXCEPTION_SWITCH( \
res, \
ExceptionType, \
InferenceEngine::details::ThrowNow<ExceptionType>{} <<= std::stringstream{} << IE_LOCATION << resp.msg)
(InferenceEngine::details::ThrowNow<ExceptionType>{IE_LOCATION_PARAM}) <<= std::stringstream{} << resp.msg)
#define CALL_STATUS_FNC_NO_ARGS(function) \
if (!actual) \
@ -75,8 +76,9 @@ namespace InferenceEngine {
ResponseDesc resp; \
auto res = actual->function(&resp); \
if (res != OK) \
IE_EXCEPTION_SWITCH(res, \
ExceptionType, \
InferenceEngine::details::ThrowNow<ExceptionType>{} <<= std::stringstream{} << IE_LOCATION)
IE_EXCEPTION_SWITCH( \
res, \
ExceptionType, \
(InferenceEngine::details::ThrowNow<ExceptionType>{IE_LOCATION_PARAM}) <<= std::stringstream{})
} // namespace InferenceEngine

View File

@ -31,10 +31,9 @@ std::shared_ptr<T> CreateExtensionFromLibrary(std::shared_ptr<void> _so) {
ResponseDesc desc;
StatusCode sts = reinterpret_cast<CreateF*>(create)(object, &desc);
if (sts != OK) {
IE_EXCEPTION_SWITCH(
sts,
ExceptionType,
details::ThrowNow<ExceptionType>{} <<= std::stringstream{} << IE_LOCATION << desc.msg)
IE_EXCEPTION_SWITCH(sts,
ExceptionType,
details::ThrowNow<ExceptionType>{} <<= std::stringstream{} << desc.msg)
}
IE_SUPPRESS_DEPRECATED_START
_ptr = std::shared_ptr<T>(object, [](T* ptr) {

View File

@ -18,14 +18,14 @@ public:
TO_STATUS(IE_EXCEPTION_SWITCH(
statusCode,
ExceptionType,
InferenceEngine::details::ThrowNow<ExceptionType>{} <<= std::stringstream{} << IE_LOCATION))
InferenceEngine::details::ThrowNow<ExceptionType>{IE_LOCATION_PARAM} <<= std::stringstream{}))
}
static InferenceEngine::StatusCode toStatusWrapperMsg(std::string& msg, InferenceEngine::ResponseDesc* resp) {
TO_STATUS(IE_EXCEPTION_SWITCH(
statusCode,
ExceptionType,
InferenceEngine::details::ThrowNow<ExceptionType>{} <<= std::stringstream{} << IE_LOCATION << msg))
InferenceEngine::details::ThrowNow<ExceptionType>{IE_LOCATION_PARAM} <<= std::stringstream{} << msg))
}
};
@ -72,7 +72,7 @@ TEST_F(ExceptionTests, throwAfterConvertStatusToClassContainMessage) {
std::string refMessage = "Exception message!";
auto actual = std::make_shared<WrapperClass<StatusCode::NOT_ALLOCATED>>();
try {
CALL_STATUS_FNC(toStatusWrapperMsg, refMessage)
CALL_STATUS_FNC(toStatusWrapperMsg, refMessage);
} catch (const NotAllocated& iex) {
std::string actualMessage = iex.what();
ASSERT_TRUE(actualMessage.find(refMessage) != std::string::npos);

View File

@ -36,7 +36,8 @@ TEST(ExceptionTests, ExceptionShowsCorrectMessageDebugVersion) {
lineNum = __LINE__ + 1;
IE_THROW() << message;
} catch (InferenceEngine::Exception& iex) {
std::string ref_message = std::string{"\n"} + __FILE__ + ":" + std::to_string(lineNum) + " " + message;
std::string ref_message =
std::string{"\n"} + __FILE__ + ":" + std::to_string(lineNum) + " [ GENERAL_ERROR ] " + message;
ASSERT_STREQ(iex.what(), ref_message.c_str());
}
}
@ -46,7 +47,7 @@ TEST(ExceptionTests, ExceptionShowsCorrectMessageReleaseVersion) {
try {
IE_THROW() << message;
} catch (InferenceEngine::Exception& iex) {
std::string ref_message = message;
std::string ref_message = "[ GENERAL_ERROR ] " + message;
ASSERT_STREQ(iex.what(), ref_message.c_str());
}
}

View File

@ -62,12 +62,10 @@ void GNAInferRequest::StartAsyncImpl() {
std::exception_ptr exceptionPtr;
if (res != InferenceEngine::StatusCode::OK) {
try {
IE_EXCEPTION_SWITCH(res,
ExceptionType,
InferenceEngine::details::ThrowNow<ExceptionType>{} <<=
std::stringstream{}
<< IE_LOCATION
<< InferenceEngine::details::ExceptionTraits<ExceptionType>::string());
IE_EXCEPTION_SWITCH(
res,
ExceptionType,
InferenceEngine::details::ThrowNow<ExceptionType>{IE_LOCATION_PARAM} <<= std::stringstream{});
} catch (...) {
exceptionPtr = std::current_exception();
}