1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_COMPILER_XLA_STATUS_MACROS_H_ 17 #define TENSORFLOW_COMPILER_XLA_STATUS_MACROS_H_ 18 19 #include <memory> 20 #include <ostream> // NOLINT 21 #include <string> 22 #include <vector> 23 24 #include "tensorflow/compiler/xla/statusor.h" 25 #include "tensorflow/compiler/xla/types.h" 26 #include "tensorflow/core/lib/core/status.h" 27 28 namespace xla { 29 namespace status_macros { 30 31 // This is a useful error message when encountering XLA Compiler errors that 32 // could be handled with the non-strict AutoJit mode. 33 extern const char kPossibleAutoJitAlternative[]; 34 35 // Stream object used to collect error messages in MAKE_ERROR macros 36 // or append error messages with APPEND_ERROR. It accepts any 37 // arguments with operator<< to build an error string, and then has an 38 // implicit cast operator to Status, which converts the 39 // logged string to a Status object and returns it, after logging the 40 // error. At least one call to operator<< is required; a compile time 41 // error will be generated if none are given. Errors will only be 42 // logged by default for certain status codes, as defined in 43 // IsLoggedByDefault. This class will give ERROR errors if you don't 44 // retrieve a Status exactly once before destruction. 45 // 46 // The class converts into an intermediate wrapper object 47 // MakeErrorStreamWithOutput to check that the error stream gets at least one 48 // item of input. 49 class MakeErrorStream { 50 public: 51 // Wrapper around MakeErrorStream that only allows for output. This 52 // is created as output of the first operator<< call on 53 // MakeErrorStream. The bare MakeErrorStream does not have a 54 // Status operator. The net effect of that is that you 55 // have to call operator<< at least once or else you'll get a 56 // compile time error. 57 class MakeErrorStreamWithOutput { 58 public: MakeErrorStreamWithOutput(MakeErrorStream * error_stream)59 explicit MakeErrorStreamWithOutput(MakeErrorStream* error_stream) 60 : wrapped_error_stream_(error_stream) {} 61 62 template <typename T> 63 MakeErrorStreamWithOutput& operator<<(const T& value) { 64 *wrapped_error_stream_ << value; 65 return *this; 66 } 67 68 // Implicit cast operators to Status and StatusOr. 69 // Exactly one of these must be called exactly once before destruction. Status()70 operator Status() { return wrapped_error_stream_->GetStatus(); } 71 template <typename T> 72 operator xla::StatusOr<T>() { 73 return wrapped_error_stream_->GetStatus(); 74 } 75 76 private: 77 MakeErrorStream* wrapped_error_stream_; 78 79 MakeErrorStreamWithOutput(const MakeErrorStreamWithOutput&) = delete; 80 MakeErrorStreamWithOutput& operator=(const MakeErrorStreamWithOutput&) = 81 delete; 82 }; 83 84 // When starting from an existing error status, this determines whether we'll 85 // append or prepend to that status's error message. 86 enum PriorMessageHandling { kAppendToPriorMessage, kPrependToPriorMessage }; 87 88 // Make an error with the given code. 89 template <typename ERROR_CODE_TYPE> MakeErrorStream(const char * file,int line,ERROR_CODE_TYPE code)90 MakeErrorStream(const char* file, int line, ERROR_CODE_TYPE code) 91 : impl_(new Impl(file, line, code, this, true)) {} 92 93 template <typename T> 94 MakeErrorStreamWithOutput& operator<<(const T& value) { 95 CheckNotDone(); 96 impl_->stream_ << value; 97 return impl_->make_error_stream_with_output_wrapper_; 98 } 99 100 // When this message is logged (see with_logging()), include the stack trace. with_log_stack_trace()101 MakeErrorStream& with_log_stack_trace() { 102 impl_->should_log_stack_trace_ = true; 103 return *this; 104 } 105 106 // Adds RET_CHECK failure text to error message. add_ret_check_failure(const char * condition)107 MakeErrorStreamWithOutput& add_ret_check_failure(const char* condition) { 108 return *this << "RET_CHECK failure (" << impl_->file_ << ":" << impl_->line_ 109 << ") " << condition << " "; 110 } 111 112 private: 113 class Impl { 114 public: 115 Impl(const char* file, int line, tensorflow::error::Code code, 116 MakeErrorStream* error_stream, bool is_logged_by_default = true); 117 Impl(const Status& status, PriorMessageHandling prior_message_handling, 118 const char* file, int line, MakeErrorStream* error_stream); 119 120 ~Impl(); 121 122 // This must be called exactly once before destruction. 123 Status GetStatus(); 124 125 void CheckNotDone() const; 126 127 private: 128 const char* file_; 129 int line_; 130 tensorflow::error::Code code_; 131 132 PriorMessageHandling prior_message_handling_ = kAppendToPriorMessage; 133 std::string prior_message_; 134 bool is_done_; // true after Status object has been returned 135 std::ostringstream stream_; 136 bool should_log_; 137 int log_severity_; 138 bool should_log_stack_trace_; 139 140 // Wrapper around the MakeErrorStream object that has a 141 // Status conversion. The first << operator called on 142 // MakeErrorStream will return this object, and only this object 143 // can implicitly convert to Status. The net effect of 144 // this is that you'll get a compile time error if you call 145 // MAKE_ERROR etc. without adding any output. 146 MakeErrorStreamWithOutput make_error_stream_with_output_wrapper_; 147 148 friend class MakeErrorStream; 149 Impl(const Impl&) = delete; 150 Impl& operator=(const Impl&) = delete; 151 }; 152 153 void CheckNotDone() const; 154 155 // Returns the status. Used by MakeErrorStreamWithOutput. GetStatus()156 Status GetStatus() const { return impl_->GetStatus(); } 157 158 // Store the actual data on the heap to reduce stack frame sizes. 159 std::unique_ptr<Impl> impl_; 160 161 MakeErrorStream(const MakeErrorStream&) = delete; 162 MakeErrorStream& operator=(const MakeErrorStream&) = delete; 163 }; 164 165 // Provides a conversion to bool so that it can be used inside an if statement 166 // that declares a variable. 167 class StatusAdaptorForMacros { 168 public: StatusAdaptorForMacros(Status status)169 explicit StatusAdaptorForMacros(Status status) : status_(std::move(status)) {} 170 171 StatusAdaptorForMacros(const StatusAdaptorForMacros&) = delete; 172 StatusAdaptorForMacros& operator=(const StatusAdaptorForMacros&) = delete; 173 174 explicit operator bool() const { return ABSL_PREDICT_TRUE(status_.ok()); } 175 Consume()176 Status&& Consume() { return std::move(status_); } 177 178 private: 179 Status status_; 180 }; 181 182 } // namespace status_macros 183 } // namespace xla 184 185 #define TF_RET_CHECK(condition) \ 186 while (ABSL_PREDICT_FALSE(!(condition))) \ 187 return xla::status_macros::MakeErrorStream(__FILE__, __LINE__, \ 188 ::tensorflow::error::INTERNAL) \ 189 .with_log_stack_trace() \ 190 .add_ret_check_failure(#condition) 191 192 #endif // TENSORFLOW_COMPILER_XLA_STATUS_MACROS_H_ 193