From f2b1a015ed648f48e0a55132fdcd9774e04c9340 Mon Sep 17 00:00:00 2001 From: Stella Laurenzo Date: Mon, 28 Oct 2024 19:39:31 -0700 Subject: [PATCH] [shortfin] Make error copyable. (#348) * Just eagerly serializes the exception. Trying to defer this makes the type non copyable and is a dubious optimization given that we never use this type for flow control. * Should fix MSVC warnings and ill-defined behavior there. --- shortfin/src/shortfin/support/iree_helpers.cc | 9 +++++---- shortfin/src/shortfin/support/iree_helpers.h | 17 +++++++---------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/shortfin/src/shortfin/support/iree_helpers.cc b/shortfin/src/shortfin/support/iree_helpers.cc index 417a9f443..17430bb71 100644 --- a/shortfin/src/shortfin/support/iree_helpers.cc +++ b/shortfin/src/shortfin/support/iree_helpers.cc @@ -86,13 +86,14 @@ error::error(std::string message, iree_status_t failing_status) message_(std::move(message)), failing_status_(failing_status) { message_.append(": "); + AppendStatusMessage(); } -error::error(iree_status_t failing_status) : failing_status_(failing_status) {} -void error::AppendStatus() const noexcept { - if (status_appended_) return; - status_appended_ = false; +error::error(iree_status_t failing_status) : failing_status_(failing_status) { + AppendStatusMessage(); +} +void error::AppendStatusMessage() { iree_allocator_t allocator = iree_allocator_system(); char *status_buffer = nullptr; iree_host_size_t length = 0; diff --git a/shortfin/src/shortfin/support/iree_helpers.h b/shortfin/src/shortfin/support/iree_helpers.h index 446f32f41..f8d3f1398 100644 --- a/shortfin/src/shortfin/support/iree_helpers.h +++ b/shortfin/src/shortfin/support/iree_helpers.h @@ -277,24 +277,21 @@ class SHORTFIN_API error : public std::exception { public: error(std::string message, iree_status_t failing_status); error(iree_status_t failing_status); - error(const error &) = delete; + error(const error &other) + : code_(other.code_), + message_(other.message_), + failing_status_(iree_status_clone(other.failing_status_)) {} error &operator=(const error &) = delete; ~error() { iree_status_ignore(failing_status_); } - const char *what() const noexcept override { - if (!status_appended_) { - AppendStatus(); - } - return message_.c_str(); - }; + const char *what() const noexcept override { return message_.c_str(); }; iree_status_code_t code() const { return code_; } private: - void AppendStatus() const noexcept; + void AppendStatusMessage(); iree_status_code_t code_; - mutable std::string message_; + std::string message_; mutable iree_status_t failing_status_; - mutable bool status_appended_ = false; }; #define SHORTFIN_IMPL_HANDLE_IF_API_ERROR(var, ...) \