@@ -39,21 +39,30 @@ namespace torch_xla {
39
39
// Unique identifier for the status variable for the current line.
40
40
#define XLA_STATUS_VAR_ XLA_CONCAT_ (status_, __LINE__)
41
41
42
+ // Fake wrapper to `status`.
43
+ //
44
+ // This is used in place of `XLA_ERROR_WITH_LOCATION`, whenever we don't
45
+ // want to append source code location information to the error message,
46
+ // e.g. `XLA_RETURN_IF_ERROR` and `XLA_ASSIGN_OR_RETURN`.
47
+ #define XLA_NO_WRAP_ (status ) status
48
+
42
49
// Provides a flexible way to handle error checking with optional message
43
50
// modification. It evaluates `expr`, checks if it's OK, and either:
44
51
// 1. Returns early with an error status (potentially modified by the provided
45
52
// additional messages)
46
53
// 2. Proceeds with the given `then` block if successful
47
- #define XLA_RETURN_IF_ERROR_IMPL_ (expr, var, then, ...) \
48
- auto var = (expr); \
49
- if (!var.ok()) { \
50
- return ::torch_xla::MaybeWithNewMessage ( \
51
- ::torch_xla::GetStatus (var), __FILE__, __LINE__, ##__VA_ARGS__); \
52
- } \
54
+ #define XLA_RETURN_IF_ERROR_IMPL_ (expr, var, wrapper, then, ...) \
55
+ auto var = (expr); \
56
+ if (!var.ok()) { \
57
+ return wrapper ( ::torch_xla::MaybeWithNewMessage ( \
58
+ ::torch_xla::GetStatus (var), __FILE__, __LINE__, ##__VA_ARGS__)) ; \
59
+ } \
53
60
then
54
61
55
62
// Propagates `rexpr`, in case it's a non-ok status.
56
63
//
64
+ // This macro should be used for propagating status internally.
65
+ //
57
66
// Example:
58
67
//
59
68
// XLA_RETURN_IF_ERROR(
@@ -69,14 +78,37 @@ namespace torch_xla {
69
78
// Previous error message. (at <cpp-source-file>:<line>)
70
79
// ...
71
80
//
72
- #define XLA_RETURN_IF_ERROR (rexpr, ...) \
73
- do { \
74
- XLA_RETURN_IF_ERROR_IMPL_ (rexpr, XLA_STATUS_VAR_, {}, ##__VA_ARGS__) \
81
+ #define XLA_RETURN_IF_ERROR (rexpr, ...) \
82
+ do { \
83
+ XLA_RETURN_IF_ERROR_IMPL_ (rexpr, XLA_STATUS_VAR_, XLA_NO_WRAP_, {}, \
84
+ ##__VA_ARGS__) \
85
+ } while (false )
86
+
87
+ // Propagates `rexpr`, in case it's a non-ok status, appending the source code
88
+ // location to it.
89
+ //
90
+ // Note that while the macro above will append the source code location only if
91
+ // a new message is given, this macro will append the source code location if
92
+ // `XLA_SHOW_CPP_ERROR_CONTEXT` is set.
93
+ //
94
+ // This macro should be used whenever we are propagating some status that came
95
+ // from some external library.
96
+ //
97
+ // Example:
98
+ //
99
+ // XLA_RETURN_IF_ERROR_WITH_LOCATION(FnThatReturnsStatus());
100
+ //
101
+ #define XLA_RETURN_IF_ERROR_WITH_LOCATION (rexpr ) \
102
+ do { \
103
+ XLA_RETURN_IF_ERROR_IMPL_ (rexpr, XLA_STATUS_VAR_, XLA_ERROR_WITH_LOCATION, \
104
+ {}) \
75
105
} while (false )
76
106
77
107
// Propagates `rexpr`, in case it's a non-ok status. Otherwise, assign
78
108
// its result to `lhs`.
79
109
//
110
+ // This macro should be used for propagating status internally.
111
+ //
80
112
// Note 1: `lhs` might be a variable declarate, e.g:
81
113
//
82
114
// Note 2: this macro will be replaced by multiple statements that live on
@@ -100,10 +132,31 @@ namespace torch_xla {
100
132
// ...
101
133
//
102
134
#define XLA_ASSIGN_OR_RETURN (lhs, rexpr, ...) \
103
- XLA_RETURN_IF_ERROR_IMPL_ (rexpr, XLA_STATUS_VAR_, \
135
+ XLA_RETURN_IF_ERROR_IMPL_ (rexpr, XLA_STATUS_VAR_, XLA_NO_WRAP_, \
104
136
lhs = std::move(XLA_STATUS_VAR_).value(), \
105
137
##__VA_ARGS__)
106
138
139
+ // Propagates `rexpr`, in case it's a non-ok status. Otherwise, assign
140
+ // its result to `lhs`.
141
+ //
142
+ // Note that while the macro above will append the source code location only if
143
+ // a new message is given, this macro will append the source code location if
144
+ // `XLA_SHOW_CPP_ERROR_CONTEXT` is set.
145
+ //
146
+ // This macro should be used whenever we are propagating some status that came
147
+ // from some external library.
148
+ //
149
+ // Example:
150
+ //
151
+ // XLA_ASSIGN_OR_RETURN_WITH_LOCATION(
152
+ // int result,
153
+ // FnThatReturnsStatus(),
154
+ // );
155
+ //
156
+ #define XLA_ASSIGN_OR_RETURN_WITH_LOCATION (lhs, rexpr ) \
157
+ XLA_RETURN_IF_ERROR_IMPL_ (rexpr, XLA_STATUS_VAR_, XLA_ERROR_WITH_LOCATION, \
158
+ lhs = std::move(XLA_STATUS_VAR_).value())
159
+
107
160
// Maybe shows location information in the status message.
108
161
//
109
162
// This function assumes that `status` is a non-ok status.
0 commit comments