Skip to content

Commit c61e49c

Browse files
dfmGoogle-ML-Automation
authored andcommitted
Simplify logic in jaxlib FFI_ASSIGN_OR_RETURN macro, and fix gcc build.
In jax-ml#23687, it was reported that recent jaxlib changes introduced issues when building from source using gcc, instead of the clang build that we test. I'm not 100% sure why the previous macro didn't work, but in investigating I found a version that seems to work on both clang and gcc with simpler logic. PiperOrigin-RevId: 675641259
1 parent e4081b5 commit c61e49c

File tree

1 file changed

+9
-29
lines changed

1 file changed

+9
-29
lines changed

jaxlib/ffi_helpers.h

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -62,35 +62,15 @@ namespace jax {
6262
FFI_ASSIGN_OR_RETURN_CONCAT_INNER_(x, y)
6363

6464
// All the macros below here are to handle the case in FFI_ASSIGN_OR_RETURN
65-
// where the LHS is wrapped in parentheses.
66-
#define FFI_ASSIGN_OR_RETURN_EAT(...)
67-
#define FFI_ASSIGN_OR_RETURN_REM(...) __VA_ARGS__
68-
#define FFI_ASSIGN_OR_RETURN_EMPTY()
69-
70-
#define FFI_ASSIGN_OR_RETURN_IS_EMPTY_INNER(...) \
71-
FFI_ASSIGN_OR_RETURN_IS_EMPTY_INNER_HELPER((__VA_ARGS__, 0, 1))
72-
#define FFI_ASSIGN_OR_RETURN_IS_EMPTY_INNER_HELPER(args) \
73-
FFI_ASSIGN_OR_RETURN_IS_EMPTY_INNER_I args
74-
#define FFI_ASSIGN_OR_RETURN_IS_EMPTY_INNER_I(e0, e1, is_empty, ...) is_empty
75-
76-
#define FFI_ASSIGN_OR_RETURN_IS_EMPTY(...) \
77-
FFI_ASSIGN_OR_RETURN_IS_EMPTY_I(__VA_ARGS__)
78-
#define FFI_ASSIGN_OR_RETURN_IS_EMPTY_I(...) \
79-
FFI_ASSIGN_OR_RETURN_IS_EMPTY_INNER(_, ##__VA_ARGS__)
80-
81-
#define FFI_ASSIGN_OR_RETURN_IF_1(_Then, _Else) _Then
82-
#define FFI_ASSIGN_OR_RETURN_IF_0(_Then, _Else) _Else
83-
#define FFI_ASSIGN_OR_RETURN_IF(_Cond, _Then, _Else) \
84-
FFI_ASSIGN_OR_RETURN_CONCAT_(FFI_ASSIGN_OR_RETURN_IF_, _Cond)(_Then, _Else)
85-
86-
#define FFI_ASSIGN_OR_RETURN_IS_PARENTHESIZED(...) \
87-
FFI_ASSIGN_OR_RETURN_IS_EMPTY(FFI_ASSIGN_OR_RETURN_EAT __VA_ARGS__)
88-
89-
#define FFI_ASSIGN_OR_RETURN_UNPARENTHESIZE_IF_PARENTHESIZED(...) \
90-
FFI_ASSIGN_OR_RETURN_IF(FFI_ASSIGN_OR_RETURN_IS_PARENTHESIZED(__VA_ARGS__), \
91-
FFI_ASSIGN_OR_RETURN_REM, \
92-
FFI_ASSIGN_OR_RETURN_EMPTY()) \
93-
__VA_ARGS__
65+
// where the LHS is wrapped in parentheses. See a more detailed discussion at
66+
// https://stackoverflow.com/a/62984543
67+
#define FFI_ASSIGN_OR_RETURN_UNPARENTHESIZE_IF_PARENTHESIZED(X) \
68+
FFI_ASSIGN_OR_RETURN_ESCAPE(FFI_ASSIGN_OR_RETURN_EMPTY X)
69+
#define FFI_ASSIGN_OR_RETURN_EMPTY(...) FFI_ASSIGN_OR_RETURN_EMPTY __VA_ARGS__
70+
#define FFI_ASSIGN_OR_RETURN_ESCAPE(...) \
71+
FFI_ASSIGN_OR_RETURN_ESCAPE_(__VA_ARGS__)
72+
#define FFI_ASSIGN_OR_RETURN_ESCAPE_(...) FFI_ASSIGN_OR_RETURN_##__VA_ARGS__
73+
#define FFI_ASSIGN_OR_RETURN_FFI_ASSIGN_OR_RETURN_EMPTY
9474

9575
template <typename T>
9676
inline absl::StatusOr<T> MaybeCastNoOverflow(

0 commit comments

Comments
 (0)