Skip to content

Commit

Permalink
[shortfin] Make error copyable. (#348)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
stellaraccident authored Oct 29, 2024
1 parent 89e26c0 commit f2b1a01
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
9 changes: 5 additions & 4 deletions shortfin/src/shortfin/support/iree_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 7 additions & 10 deletions shortfin/src/shortfin/support/iree_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) \
Expand Down

0 comments on commit f2b1a01

Please sign in to comment.