14
14
15
15
namespace torch_xla {
16
16
17
- // Creates a new Status instance, appending the current location (e.g. file and
18
- // line information) to the status message.
17
+ // If `XLA_SHOW_CPP_ERROR_CONTEXT` is set, creates a new Status instance,
18
+ // appending the current location (e.g. file and line information) to the
19
+ // status message.
20
+ //
21
+ // This should be used whenever we are returning new error status.
22
+ //
23
+ // Example:
24
+ //
25
+ // XLA_ERROR_WITH_LOCATION(
26
+ // absl::InvalidArgumentError("Error message.")
27
+ // );
28
+ //
29
+ // If `XLA_SHOW_CPP_ERROR_CONTEXT` is set, the error shown will be:
30
+ //
31
+ // Error message. (at <cpp-source-file>:<line>)
19
32
//
20
- // This should be used whenever we are returning new error status, instead of
21
- // propagating. Then, if `XLA_SHOW_CPP_ERROR_CONTEXT` environment variable is
22
- // set, the location information will be shown.
23
33
#define XLA_ERROR_WITH_LOCATION (status ) \
24
34
::torch_xla::MaybeWithLocation (status, __FILE__, __LINE__)
25
35
26
- #define XLA_CONCAT (a, b ) XLA_CONCAT_IMPL (a, b)
27
- #define XLA_CONCAT_IMPL (a, b ) a##b
36
+ #define XLA_CONCAT_ (a, b ) XLA_CONCAT_IMPL_ (a, b)
37
+ #define XLA_CONCAT_IMPL_ (a, b ) a##b
28
38
29
39
// Unique identifier for the status variable for the current line.
30
- #define XLA_STATUS_VAR XLA_CONCAT (status__ , __LINE__)
40
+ #define XLA_STATUS_VAR_ XLA_CONCAT_ (status_ , __LINE__)
31
41
32
42
// Provides a flexible way to handle error checking with optional message
33
43
// modification. It evaluates `expr`, checks if it's OK, and either:
34
44
// 1. Returns early with an error status (potentially modified by the provided
35
45
// additional messages)
36
46
// 2. Proceeds with the given `then` block if successful
37
- #define XLA_RETURN_IF_ERROR_IMPL (expr, var, then, ...) \
47
+ #define XLA_RETURN_IF_ERROR_IMPL_ (expr, var, then, ...) \
38
48
auto var = (expr); \
39
49
if (!var.ok()) { \
40
50
return ::torch_xla::MaybeWithNewMessage ( \
41
51
::torch_xla::GetStatus (var), __FILE__, __LINE__, ##__VA_ARGS__); \
42
52
} \
43
- then;
53
+ then
44
54
45
55
// Propagates `rexpr`, in case it's a non-ok status.
46
- #define XLA_RETURN_IF_ERROR (rexpr, ...) \
47
- do { \
48
- XLA_RETURN_IF_ERROR_IMPL (rexpr, XLA_STATUS_VAR, {}, ##__VA_ARGS__) \
56
+ //
57
+ // Example:
58
+ //
59
+ // XLA_RETURN_IF_ERROR(
60
+ // FnThatReturnsStatus(),
61
+ // "New error message."
62
+ // );
63
+ //
64
+ // If the function call results in an ok status, execution continues. Otherwise,
65
+ // we early return a non-ok status. Then, if `XLA_SHOW_CPP_ERROR_CONTEXT` is
66
+ // set, the error shown will be:
67
+ //
68
+ // New error message. (at <cpp-source-file>:<line>)
69
+ // Previous error message. (at <cpp-source-file>:<line>)
70
+ // ...
71
+ //
72
+ #define XLA_RETURN_IF_ERROR (rexpr, ...) \
73
+ do { \
74
+ XLA_RETURN_IF_ERROR_IMPL_ (rexpr, XLA_STATUS_VAR, {}, ##__VA_ARGS__) \
49
75
} while (false )
50
76
51
77
// Propagates `rexpr`, in case it's a non-ok status. Otherwise, assign
52
78
// its result to `lhs`.
53
79
//
54
80
// Note 1: `lhs` might be a variable declarate, e.g:
55
81
//
56
- // XLA_ASSIGN_OR_RETURN(int value, FnThatReturnsStatus(), ...);
57
- //
58
82
// Note 2: this macro will be replaced by multiple statements that live on
59
83
// the scope it was called (see XLA_RETURN_IF_ERROR_IMPL).
60
84
//
61
- #define XLA_ASSIGN_OR_RETURN (lhs, rexpr, ...) \
62
- XLA_RETURN_IF_ERROR_IMPL (rexpr, XLA_STATUS_VAR, \
63
- lhs = std::move(XLA_STATUS_VAR).value(); \
64
- , ##__VA_ARGS__)
85
+ // Example:
86
+ //
87
+ // XLA_ASSIGN_OR_RETURN(
88
+ // int result,
89
+ // FnThatReturnsStatus(),
90
+ // "New error message."
91
+ // );
92
+ //
93
+ // If the function call results in an ok status, execution continues with
94
+ // `result` set to `ret.value()`, where `ret` is the returned value of the
95
+ // function. Otherwise, we early return a non-ok status. Then, if
96
+ // `XLA_SHOW_CPP_ERROR_CONTEXT` is set, the error shown will be:
97
+ //
98
+ // New error message. (at <cpp-source-file>:<line>)
99
+ // Previous error message. (at <cpp-source-file>:<line>)
100
+ // ...
101
+ //
102
+ #define XLA_ASSIGN_OR_RETURN (lhs, rexpr, ...) \
103
+ XLA_RETURN_IF_ERROR_IMPL_ (rexpr, XLA_STATUS_VAR, \
104
+ lhs = std::move(XLA_STATUS_VAR).value(), \
105
+ ##__VA_ARGS__)
65
106
66
107
// Maybe shows location information in the status message.
67
108
//
@@ -73,8 +114,13 @@ namespace torch_xla {
73
114
absl::Status MaybeWithLocation (const absl::Status& status, const char * file,
74
115
int32_t line);
75
116
76
- const absl::Status& GetStatus (const absl::Status& status);
117
+ // Returns an `absl::Status` from an `absl::Status`.
118
+ // In this case, this function is a no-op. It simply returns the argument.
119
+ inline const absl::Status& GetStatus (const absl::Status& status) {
120
+ return status;
121
+ }
77
122
123
+ // Returns an `absl::Status` from an `absl::StatusOr<T>`.
78
124
template <class T >
79
125
const absl::Status& GetStatus (const absl::StatusOr<T>& status) {
80
126
return status.status ();
@@ -97,27 +143,32 @@ absl::Status MaybeWithNewMessage(const absl::Status& status, const char* file,
97
143
int32_t line,
98
144
std::string_view new_message = " " );
99
145
100
- // Consumes the `status` and maybe throws an exception if `status` has
101
- // a non-ok code.
146
+ // Maybe throws an exception if `status` has a non-ok code.
102
147
//
103
148
// Ideally, this function should be used only used in the project's
104
149
// boundary, e.g. when we need to throw an exception for the user to see.
105
- void ConsumeAndMaybeThrow (const absl::Status& status);
150
+ void MaybeThrow (const absl::Status& status);
106
151
107
- // Consumes the `status`, either returning the value it holds (for
108
- // ok status), or throwing an exception .
152
+ // Either returns the value `status` holds, if it's an ok-status, or throw an
153
+ // exception from its error status .
109
154
template <class T >
110
- T ConsumeAndMaybeThrow (absl::StatusOr<T>& & status) {
111
- ConsumeAndMaybeThrow (status.status ());
112
- return std::move ( status) .value ();
155
+ T& GetValueOrThrow (absl::StatusOr<T>& status) {
156
+ MaybeThrow (status.status ());
157
+ return status.value ();
113
158
}
114
159
115
160
template <class T >
116
- T ConsumeAndMaybeThrow (const absl::StatusOr<T>& status) {
117
- ConsumeAndMaybeThrow (status.status ());
161
+ const T& GetValueOrThrow (const absl::StatusOr<T>& status) {
162
+ MaybeThrow (status.status ());
118
163
return status.value ();
119
164
}
120
165
166
+ template <class T >
167
+ T GetValueOrThrow (absl::StatusOr<T>&& status) {
168
+ MaybeThrow (status.status ());
169
+ return std::move (status).value ();
170
+ }
171
+
121
172
} // namespace torch_xla
122
173
123
174
#endif // XLA_TORCH_XLA_CSRC_STATUS_H_
0 commit comments