xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/stream_executor/tpu/status_helper.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 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_STREAM_EXECUTOR_TPU_STATUS_HELPER_H_
17 #define TENSORFLOW_COMPILER_XLA_STREAM_EXECUTOR_TPU_STATUS_HELPER_H_
18 
19 #include "tensorflow/compiler/xla/stream_executor/tpu/tpu_executor_c_api.h"
20 #include "tensorflow/core/platform/status.h"
21 #include "tensorflow/core/tpu/tpu_api.h"
22 
23 class StatusHelper {
24  public:
StatusHelper()25   StatusHelper()
26       : c_status(tensorflow::tpu::ExecutorApiFn()->TpuStatus_NewFn()) {}
27 
~StatusHelper()28   ~StatusHelper() {
29     tensorflow::tpu::ExecutorApiFn()->TpuStatus_FreeFn(c_status);
30   }
31 
FromC(TF_Status * const c_status)32   static tensorflow::Status FromC(  // TENSORFLOW_STATUS_OK
33       TF_Status* const c_status) {
34     if (tensorflow::tpu::ExecutorApiFn()->TpuStatus_OkFn(c_status)) {
35       return ::tensorflow::OkStatus();
36     } else {
37       return tensorflow::Status(  // TENSORFLOW_STATUS_OK
38           tensorflow::error::Code(
39               tensorflow::tpu::ExecutorApiFn()->TpuStatus_CodeFn(c_status)),
40           tensorflow::tpu::ExecutorApiFn()->TpuStatus_MessageFn(c_status));
41     }
42   }
43 
ok()44   bool ok() const {
45     return tensorflow::tpu::ExecutorApiFn()->TpuStatus_OkFn(c_status);
46   }
47 
status()48   tensorflow::Status status() const {  // TENSORFLOW_STATUS_OK
49     return FromC(c_status);
50   }
51 
52   TF_Status* const c_status;  // NOLINT
53 };
54 
55 #endif  // TENSORFLOW_COMPILER_XLA_STREAM_EXECUTOR_TPU_STATUS_HELPER_H_
56