xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/runtime/errors.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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 XLA_RUNTIME_ERRORS_H_
17 #define XLA_RUNTIME_ERRORS_H_
18 
19 #include <string>
20 
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 namespace xla {
25 namespace runtime {
26 
27 // TODO(ezhulenev): Replace all uses of llvm errors inside the runtime with ABSL
28 // types: Error -> Status, Expected -> StatusOr.
29 
30 namespace internal {
31 
32 template <typename StreamT>
ToStreamHelper(StreamT & os)33 inline void ToStreamHelper(StreamT& os) {}
34 
35 template <typename StreamT, typename T, typename... Args>
ToStreamHelper(StreamT & os,T && v,Args &&...args)36 void ToStreamHelper(StreamT& os, T&& v, Args&&... args) {
37   os << std::forward<T>(v);
38   ToStreamHelper(os, std::forward<Args>(args)...);
39 }
40 
41 }  // namespace internal
42 
43 template <typename... Args>
StrCat(Args &&...args)44 std::string StrCat(Args&&... args) {
45   std::string str;
46   llvm::raw_string_ostream sstr(str);
47   internal::ToStreamHelper(sstr, std::forward<Args>(args)...);
48   sstr.flush();
49   return str;
50 }
51 
52 template <typename... Args>
MakeStringError(Args &&...args)53 llvm::Error MakeStringError(Args&&... args) {
54   return llvm::createStringError(llvm::inconvertibleErrorCode(),
55                                  StrCat(std::forward<Args>(args)...));
56 }
57 
58 }  // namespace runtime
59 }  // namespace xla
60 
61 #endif  // XLA_RUNTIME_ERRORS_H_
62