1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "glow/Support/Error.h"
18
19#include <sstream>
20
21namespace glow {
22bool OneErrOnly::set(Error err) {
23 // Don't do anything in the case of empty Error.
24 if (!err) {
25 return false;
26 }
27
28 std::unique_lock<std::mutex> lock(m_);
29
30 if (!err_) {
31 err_ = std::move(err);
32 return true;
33 } else {
34 // No update happening so don't need the lock any more.
35 lock.unlock();
36 LOG(ERROR) << "OneErrOnly already has an Error, discarding new Error: "
37 << ERR_TO_STRING(std::move(err));
38 return false;
39 }
40}
41
42Error OneErrOnly::get() {
43 std::unique_lock<std::mutex> lock(m_);
44 auto err = std::move(err_);
45 return err;
46}
47
48bool OneErrOnly::containsErr() {
49 std::unique_lock<std::mutex> lock(m_);
50 return static_cast<bool>(err_);
51}
52
53namespace detail {
54std::string GlowErrorValue::logToString(bool warning) const {
55 std::stringstream ss;
56 log(ss, warning);
57 return ss.str();
58}
59
60std::string GlowErrorValue::errorCodeToString(const ErrorCode &ec) {
61 switch (ec) {
62 case ErrorCode::UNKNOWN:
63 return "UNKNOWN";
64 case ErrorCode::MODEL_LOADER_UNSUPPORTED_SHAPE:
65 return "MODEL_LOADER_UNSUPPORTED_SHAPE";
66 case ErrorCode::MODEL_LOADER_UNSUPPORTED_OPERATOR:
67 return "MODEL_LOADER_UNSUPPORTED_OPERATOR";
68 case ErrorCode::MODEL_LOADER_UNSUPPORTED_ATTRIBUTE:
69 return "MODEL_LOADER_UNSUPPORTED_ATTRIBUTE";
70 case ErrorCode::MODEL_LOADER_UNSUPPORTED_DATATYPE:
71 return "MODEL_LOADER_UNSUPPORTED_DATATYPE";
72 case ErrorCode::MODEL_LOADER_UNSUPPORTED_ONNX_VERSION:
73 return "MODEL_LOADER_UNSUPPORTED_ONNX_VERSION";
74 case ErrorCode::MODEL_LOADER_INVALID_PROTOBUF:
75 return "MODEL_LOADER_INVALID_PROTOBUF";
76 case ErrorCode::PARTITIONER_ERROR:
77 return "PARTITIONER_ERROR";
78 case ErrorCode::RUNTIME_ERROR:
79 return "RUNTIME_ERROR";
80 case ErrorCode::RUNTIME_DEFERRED_WEIGHT_ERROR:
81 return "RUNTIME_DEFERRED_WEIGHT_ERROR";
82 case ErrorCode::RUNTIME_OUT_OF_DEVICE_MEMORY:
83 return "RUNTIME_OUT_OF_DEVICE_MEMORY";
84 case ErrorCode::RUNTIME_NET_NOT_FOUND:
85 return "RUNTIME_NET_NOT_FOUND";
86 case ErrorCode::RUNTIME_REQUEST_REFUSED:
87 return "RUNTIME_REQUEST_REFUSED";
88 case ErrorCode::RUNTIME_DEVICE_NOT_FOUND:
89 return "RUNTIME_DEVICE_NOT_FOUND";
90 case ErrorCode::RUNTIME_DEVICE_NONRECOVERABLE:
91 return "RUNTIME_DEVICE_NONRECOVERABLE";
92 case ErrorCode::RUNTIME_NET_BUSY:
93 return "RUNTIME_NET_BUSY";
94 case ErrorCode::DEVICE_FEATURE_NOT_SUPPORTED:
95 return "DEVICE_FEATURE_NOT_SUPPORTED";
96 case ErrorCode::COMPILE_UNSUPPORTED_NODE_AFTER_OPTIMIZE:
97 return "COMPILE_UNSUPPORTED_NODE_AFTER_OPTIMIZE";
98 case ErrorCode::COMPILE_CONTEXT_MALFORMED:
99 return "COMPILE_CONTEXT_MALFORMED";
100 case ErrorCode::MODEL_WRITER_INVALID_FILENAME:
101 return "MODEL_WRITER_INVALID_FILENAME";
102 case ErrorCode::MODEL_WRITER_SERIALIZATION_ERROR:
103 return "MODEL_WRITER_SERIALIZATION_ERROR";
104 case ErrorCode::COMPILE_UNSUPPORTED_IR_AFTER_GENERATE:
105 return "COMPILE_UNSUPPORTED_IR_AFTER_GENERATE";
106 case ErrorCode::COMPILE_UNSUPPORTED_IR_AFTER_OPTIMIZE:
107 return "COMPILE_UNSUPPORTED_IR_AFTER_OPTIMIZE";
108 };
109 LOG(FATAL) << "Unsupported ErrorCode";
110}
111
112std::unique_ptr<GlowErrorValue> takeErrorValue(GlowError error) {
113 return error.takeErrorValue();
114}
115
116void exitOnError(const char *fileName, size_t lineNumber, GlowError error) {
117 if (error) {
118 std::unique_ptr<GlowErrorValue> errorValue =
119 detail::takeErrorValue(std::move(error));
120 assert(errorValue != nullptr &&
121 "Error should have a non-null ErrorValue if bool(error) is true");
122 errorValue->addToStack(fileName, lineNumber);
123 LOG(FATAL) << "exitOnError(Error) got an unexpected ErrorValue: "
124 << (*errorValue);
125 }
126}
127
128bool errorToBool(const char *fileName, size_t lineNumber, GlowError error,
129 bool log, bool warning) {
130 std::unique_ptr<GlowErrorValue> errorValue =
131 detail::takeErrorValue(std::move(error));
132 if (errorValue) {
133 if (log) {
134 errorValue->addToStack(fileName, lineNumber);
135 LOG(ERROR) << "Converting Error to bool: "
136 << errorValue->logToString(warning);
137 }
138 return true;
139 } else {
140 return false;
141 }
142}
143
144std::string errorToString(const char *fileName, size_t lineNumber,
145 GlowError error, bool warning) {
146 std::unique_ptr<GlowErrorValue> errorValue =
147 detail::takeErrorValue(std::move(error));
148 if (errorValue) {
149 errorValue->addToStack(fileName, lineNumber);
150 return errorValue->logToString(warning);
151 } else {
152 return "success";
153 }
154}
155
156void errorToVoid(const char *fileName, size_t lineNumber, GlowError error,
157 bool log, bool warning) {
158 errorToBool(fileName, lineNumber, std::move(error), log, warning);
159}
160
161GlowError::GlowError(GlowErrorEmpty &&other) {
162 setErrorValue(std::move(other.errorValue_), /*skipCheck*/ true);
163 setChecked(true);
164 other.setChecked(true);
165}
166
167} // namespace detail
168} // namespace glow
169