xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/lite/metrics/error_collector_inst.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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 #ifndef TENSORFLOW_COMPILER_MLIR_LITE_METRICS_ERROR_COLLECTOR_INST_H_
16 #define TENSORFLOW_COMPILER_MLIR_LITE_METRICS_ERROR_COLLECTOR_INST_H_
17 
18 #include <string>
19 #include <utility>
20 
21 #include "mlir/IR/Location.h"  // from @llvm-project
22 #include "mlir/IR/Operation.h"  // from @llvm-project
23 #include "mlir/Pass/PassInstrumentation.h"  // from @llvm-project
24 #include "tensorflow/compiler/mlir/lite/metrics/error_collector.h"
25 #include "tensorflow/compiler/mlir/lite/metrics/types_util.h"
26 #include "tensorflow/lite/python/metrics/converter_error_data.pb.h"
27 
28 namespace mlir {
29 namespace TFL {
30 
31 // Collects errors when running the pass manager.
32 class ErrorCollectorInstrumentation : public PassInstrumentation {
33   using ConverterErrorData = tflite::metrics::ConverterErrorData;
34   using ErrorCode = ConverterErrorData::ErrorCode;
35 
36  public:
37   explicit ErrorCollectorInstrumentation(MLIRContext *context);
38 
39  private:
40   // Instrumentation hooks. These hooks don't need to be thread-safe. The pass
41   // manager runs each pass for the entire module, then it walks through
42   // each op in the module and runs the pass on them, may be in async mode.
43   void runBeforePass(Pass *pass, Operation *module) override;
44   void runAfterPass(Pass *pass, Operation *module) override;
45   void runAfterPassFailed(Pass *pass, Operation *module) override;
46 
47   // The handler to capture error messages.
48   std::unique_ptr<ScopedDiagnosticHandler> handler_;
49   // A map from location to op name.
50   std::unordered_map<Location, std::string, LocationHash> loc_to_name_;
51   // Stores the error message for errors without op name and error code.
52   std::string common_error_message_;
53   // Name of the running pass.
54   std::string pass_name_;
55   // Pointer to the global ErrorCollector instance.
56   ErrorCollector *error_collector_;
57 };
58 
59 // Prefix when adding error code as a note in Diagnostic.
60 constexpr char kErrorCodePrefix[] = "Error code: ";
61 
62 // Adds error code to a newly created InFlightDiagnostic.
AttachErrorCode(InFlightDiagnostic && diag,int error_code)63 inline InFlightDiagnostic AttachErrorCode(InFlightDiagnostic &&diag,
64                                           int error_code) {
65   using tflite::metrics::ConverterErrorData;
66   diag.attachNote() << kErrorCodePrefix
67                     << ConverterErrorData::ErrorCode_Name(error_code);
68   return std::move(diag);
69 }
70 
71 }  // namespace TFL
72 }  // namespace mlir
73 
74 #endif  // TENSORFLOW_COMPILER_MLIR_LITE_METRICS_ERROR_COLLECTOR_INST_H_
75