1 /* Copyright 2019 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 #include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h"
17 
18 #include "tensorflow/core/platform/errors.h"
19 
20 namespace mlir {
21 
StatusScopedDiagnosticHandler(MLIRContext * context,bool propagate)22 StatusScopedDiagnosticHandler::StatusScopedDiagnosticHandler(
23     MLIRContext* context, bool propagate)
24     : SourceMgrDiagnosticHandler(source_mgr_, context, diag_stream_),
25       diag_stream_(diag_str_),
26       propagate_(propagate) {
27   setHandler([this](Diagnostic& diag) { return this->handler(&diag); });
28 }
29 
~StatusScopedDiagnosticHandler()30 StatusScopedDiagnosticHandler::~StatusScopedDiagnosticHandler() {
31   // Verify errors were consumed and re-register old handler.
32   bool all_errors_produced_were_consumed = ok();
33   DCHECK(all_errors_produced_were_consumed) << "Error status not consumed:\n"
34                                             << diag_str_;
35 }
36 
ok() const37 bool StatusScopedDiagnosticHandler::ok() const { return diag_str_.empty(); }
38 
ConsumeStatus()39 Status StatusScopedDiagnosticHandler::ConsumeStatus() {
40   if (ok()) return Status::OK();
41 
42   // TODO(jpienaar) This should be combining status with one previously built
43   // up.
44   Status s = tensorflow::errors::Unknown(diag_str_);
45   diag_str_.clear();
46   return s;
47 }
48 
Combine(Status status)49 Status StatusScopedDiagnosticHandler::Combine(Status status) {
50   if (status.ok()) return ConsumeStatus();
51 
52   // status is not-OK here, so if there was no diagnostics reported
53   // additionally then return this error.
54   if (ok()) return status;
55 
56   // Append the diagnostics reported to the status. This repeats the behavior of
57   // TensorFlow's AppendToMessage without the additional formatting inserted
58   // there.
59   status = ::tensorflow::Status(
60       status.code(), absl::StrCat(status.error_message(), diag_str_));
61   diag_str_.clear();
62   return status;
63 }
64 
handler(Diagnostic * diag)65 LogicalResult StatusScopedDiagnosticHandler::handler(Diagnostic* diag) {
66   // Non-error diagnostic are ignored when VLOG isn't enabled.
67   if (diag->getSeverity() != DiagnosticSeverity::Error && VLOG_IS_ON(1))
68     return success();
69 
70   size_t current_diag_str_size_ = diag_str_.size();
71 
72   // Emit the diagnostic and flush the stream.
73   emitDiagnostic(*diag);
74   diag_stream_.flush();
75 
76   // Emit non-errors to VLOG instead of the internal status.
77   if (diag->getSeverity() != DiagnosticSeverity::Error) {
78     VLOG(1) << diag_str_.substr(current_diag_str_size_);
79     diag_str_.resize(current_diag_str_size_);
80   }
81 
82   // Return failure to signal propagation if necessary.
83   return failure(propagate_);
84 }
85 
86 }  // namespace mlir
87