From d5094aeae3722f4b98fcfb03e7158077f336bdd6 Mon Sep 17 00:00:00 2001 From: Dan F-M Date: Sat, 24 Oct 2026 15:18:10 -0400 Subject: [PATCH] Vendor the XLA FFI headers instead of requiring jax to build The JAX extension only needs the header-only XLA FFI API. Pinning jax==0.8.0 in build-system requires already fixed the headers at that version, but made the sdist unbuildable wherever jaxlib publishes no wheel (e.g. macOS x86_64) and pulled jaxlib into every isolated build. Copy xla/ffi/api/{api,c_api,ffi}.h verbatim from jaxlib 0.8.0 into c++/vendor/xla (Apache 2.0) and drop jax from the build requirements. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 20 +- MANIFEST.in | 1 + c++/vendor/xla/LICENSE | 201 +++ c++/vendor/xla/README.md | 10 + c++/vendor/xla/xla/ffi/api/api.h | 2023 ++++++++++++++++++++++++++++ c++/vendor/xla/xla/ffi/api/c_api.h | 770 +++++++++++ c++/vendor/xla/xla/ffi/api/ffi.h | 1654 +++++++++++++++++++++++ pyproject.toml | 2 +- 8 files changed, 4665 insertions(+), 16 deletions(-) create mode 100644 c++/vendor/xla/LICENSE create mode 100644 c++/vendor/xla/README.md create mode 100644 c++/vendor/xla/xla/ffi/api/api.h create mode 100644 c++/vendor/xla/xla/ffi/api/c_api.h create mode 100644 c++/vendor/xla/xla/ffi/api/ffi.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dedab486..1e210552 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,20 +21,10 @@ pybind11_add_module(backprop "python/celerite2/backprop.cpp") target_compile_features(backprop PUBLIC cxx_std_14) install(TARGETS backprop LIBRARY DESTINATION ${SKBUILD_PROJECT_NAME}) -option(BUILD_JAX "Build JAX extension (requires jaxlib headers)" ON) +option(BUILD_JAX "Build JAX extension" ON) if(BUILD_JAX) - execute_process( - COMMAND "${Python_EXECUTABLE}" "-c" "from jax import ffi; print(ffi.include_dir())" - OUTPUT_STRIP_TRAILING_WHITESPACE - OUTPUT_VARIABLE XLA_DIR - RESULT_VARIABLE JAXLIB_RES) - if(JAXLIB_RES EQUAL 0 AND NOT "${XLA_DIR}" STREQUAL "") - message(STATUS "Building JAX extension with XLA include: ${XLA_DIR}") - pybind11_add_module(xla_ops "python/celerite2/jax/xla_ops.cpp") - target_compile_features(xla_ops PUBLIC cxx_std_17) - target_include_directories(xla_ops PUBLIC "${XLA_DIR}") - install(TARGETS xla_ops LIBRARY DESTINATION "${SKBUILD_PROJECT_NAME}/jax") - else() - message(STATUS "Skipping JAX extension (jax.ffi include_dir not found)") - endif() + pybind11_add_module(xla_ops "python/celerite2/jax/xla_ops.cpp") + target_compile_features(xla_ops PUBLIC cxx_std_17) + target_include_directories(xla_ops PUBLIC "c++/vendor/xla") + install(TARGETS xla_ops LIBRARY DESTINATION "${SKBUILD_PROJECT_NAME}/jax") endif() diff --git a/MANIFEST.in b/MANIFEST.in index c068b015..fa15ffe8 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -9,6 +9,7 @@ recursive-exclude * .gitignore graft c++/include graft c++/vendor/eigen/Eigen +graft c++/vendor/xla prune c++/test prune .github diff --git a/c++/vendor/xla/LICENSE b/c++/vendor/xla/LICENSE new file mode 100644 index 00000000..261eeb9e --- /dev/null +++ b/c++/vendor/xla/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/c++/vendor/xla/README.md b/c++/vendor/xla/README.md new file mode 100644 index 00000000..90d255f5 --- /dev/null +++ b/c++/vendor/xla/README.md @@ -0,0 +1,10 @@ +# XLA FFI headers + +Header-only XLA FFI API (`xla/ffi/api/{api,c_api,ffi}.h`) used to build the +JAX extension (`python/celerite2/jax/xla_ops.cpp`), copied verbatim from +`jaxlib==0.8.0` (`jaxlib/include`). Licensed under Apache 2.0; see `LICENSE`. + +Vendoring these means building celerite2 does not require installing jax. The +FFI ABI is versioned and newer jaxlib releases accept handlers compiled against +older headers, so these only need updating to use newer FFI features. To +update, copy the files from `jax.ffi.include_dir()` of the desired version. diff --git a/c++/vendor/xla/xla/ffi/api/api.h b/c++/vendor/xla/xla/ffi/api/api.h new file mode 100644 index 00000000..81e0166d --- /dev/null +++ b/c++/vendor/xla/xla/ffi/api/api.h @@ -0,0 +1,2023 @@ +/* Copyright 2023 The OpenXLA Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef XLA_FFI_API_API_H_ +#define XLA_FFI_API_API_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// This is a header-only base C++ library that defines templates for decoding +// XLA FFI call frames and invoking corresponding C++ functions. This must have +// no dependencies outside of the C++ standard library. +// +// There are two extensions to this base library: +// +// (1) xla/ffi/api/ffi.h for defining "external" FFI handlers loaded from +// dynamic libraries potentially built with different toolchains and/or +// a different XLA commit. It is a header-only library without any +// dependencies. +// +// (2) xla/ffi/ffi.h for defining "internal" FFI handlers that must be +// statically linked into the binary and must be built from the same +// commit using the same toolchain, as it provides access to XLA +// implementation details (e.g. ServiceExecutableOptions) and C++ ABI +// across different libraries is hard. +// +// Extensions define template specializations for argument-decoding hooks +// defined in this file. + +#include "xla/ffi/api/c_api.h" + +#ifdef __has_builtin +#define XLA_FFI_HAS_BUILTIN(x) __has_builtin(x) +#else +#define XLA_FFI_HAS_BUILTIN(x) 0 +#endif + +#if __has_attribute(always_inline) +#define XLA_FFI_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline)) +#elif defined(_MSC_VER) +#define XLA_FFI_ATTRIBUTE_ALWAYS_INLINE __forceinline +#else +#define XLA_FFI_ATTRIBUTE_ALWAYS_INLINE +#endif + +#if __has_attribute(noinline) +#define XLA_FFI_ATTRIBUTE_NEVER_INLINE __attribute__((noinline)) +#elif defined(_MSC_VER) +#define XLA_FFI_ATTRIBUTE_NEVER_INLINE __declspec(noinline) +#else +#define XLA_FFI_ATTRIBUTE_NEVER_INLINE +#endif + +#if XLA_FFI_HAS_BUILTIN(__builtin_expect) +#define XLA_FFI_PREDICT_FALSE(x) (__builtin_expect(false || (x), false)) +#define XLA_FFI_PREDICT_TRUE(x) (__builtin_expect(false || (x), true)) +#else +#define XLA_FFI_PREDICT_FALSE(x) (x) +#define XLA_FFI_PREDICT_TRUE(x) (x) +#endif + +//===----------------------------------------------------------------------===// +// Builtin enum pretty printing +//===----------------------------------------------------------------------===// + +inline std::ostream& operator<<(std::ostream& os, + const XLA_FFI_DataType dtype) { + switch (dtype) { + case XLA_FFI_DataType_INVALID: + return os << "INVALID"; + case XLA_FFI_DataType_PRED: + return os << "PRED"; + case XLA_FFI_DataType_S1: + return os << "S1"; + case XLA_FFI_DataType_S2: + return os << "S2"; + case XLA_FFI_DataType_S4: + return os << "S4"; + case XLA_FFI_DataType_S8: + return os << "S8"; + case XLA_FFI_DataType_S16: + return os << "S16"; + case XLA_FFI_DataType_S32: + return os << "S32"; + case XLA_FFI_DataType_S64: + return os << "S64"; + case XLA_FFI_DataType_U1: + return os << "U1"; + case XLA_FFI_DataType_U2: + return os << "U2"; + case XLA_FFI_DataType_U4: + return os << "U4"; + case XLA_FFI_DataType_U8: + return os << "U8"; + case XLA_FFI_DataType_U16: + return os << "U16"; + case XLA_FFI_DataType_U32: + return os << "U32"; + case XLA_FFI_DataType_U64: + return os << "U64"; + case XLA_FFI_DataType_F16: + return os << "F16"; + case XLA_FFI_DataType_F32: + return os << "F32"; + case XLA_FFI_DataType_F64: + return os << "F64"; + case XLA_FFI_DataType_BF16: + return os << "BF16"; + case XLA_FFI_DataType_C64: + return os << "C64"; + case XLA_FFI_DataType_C128: + return os << "C128"; + case XLA_FFI_DataType_TOKEN: + return os << "TOKEN"; + case XLA_FFI_DataType_F4E2M1FN: + return os << "F4E2M1FN"; + case XLA_FFI_DataType_F8E5M2: + return os << "F8E5M2"; + case XLA_FFI_DataType_F8E3M4: + return os << "F8E3M4"; + case XLA_FFI_DataType_F8E4M3: + return os << "F8E4M3"; + case XLA_FFI_DataType_F8E4M3FN: + return os << "F8E4M3FN"; + case XLA_FFI_DataType_F8E4M3B11FNUZ: + return os << "F8E4M3B11FNUZ"; + case XLA_FFI_DataType_F8E5M2FNUZ: + return os << "F8E5M2FNUZ"; + case XLA_FFI_DataType_F8E4M3FNUZ: + return os << "F8E4M3FNUZ"; + case XLA_FFI_DataType_F8E8M0FNU: + return os << "F8E8M0FNU"; + } +} + +inline std::ostream& operator<<(std::ostream& os, const XLA_FFI_AttrType type) { + switch (type) { + case XLA_FFI_AttrType_ARRAY: + return os << "array"; + case XLA_FFI_AttrType_DICTIONARY: + return os << "dictionary"; + case XLA_FFI_AttrType_SCALAR: + return os << "scalar"; + case XLA_FFI_AttrType_STRING: + return os << "string"; + } +} + +inline std::ostream& operator<<(std::ostream& os, + const XLA_FFI_ExecutionStage stage) { + switch (stage) { + case XLA_FFI_ExecutionStage_INSTANTIATE: + return os << "instantiate"; + case XLA_FFI_ExecutionStage_PREPARE: + return os << "prepare"; + case XLA_FFI_ExecutionStage_INITIALIZE: + return os << "initialize"; + case XLA_FFI_ExecutionStage_EXECUTE: + return os << "execute"; + } +} + +namespace xla::ffi { + +enum class ExecutionStage : uint8_t { + kInstantiate = XLA_FFI_ExecutionStage_INSTANTIATE, + kPrepare = XLA_FFI_ExecutionStage_PREPARE, + kInitialize = XLA_FFI_ExecutionStage_INITIALIZE, + kExecute = XLA_FFI_ExecutionStage_EXECUTE, +}; + +enum class Traits : uint32_t { + // Indicates that the handler is compatible with command buffers. In the + // XLA:GPU CUDA backend, we rely on graph capture to trace the execution of a + // FFI handler and record it as a command buffer (CUDA graph). For a FFI + // handler to be compatible with CUDA graphs, it has to satisfy certain + // constraints as documented in + // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#prohibited-and-unhandled-operations. + // + // Broadly speaking, a handler that satisfies the following conditions should + // be compatible with command buffers: + // 1. it only launches kernels that must be captured in command buffers on + // the device (e.g. it does *not* do autotuning). This is because + // everything it launches will be captured in the command buffer; + // 2. the FFI handler only uses device allocations passed in as buffer + // arguments (e.g. it does *not* do any runtime device memory + // allocations); + // 3. the FFI handler may not query the execution status of the stream + // (e.g. calling `cudaGetLastError` on the stream is invalid). + kCmdBufferCompatible = XLA_FFI_HANDLER_TRAITS_COMMAND_BUFFER_COMPATIBLE, +}; + +// Forward declare template defined below. +template +class Binding; + +// Forward declare template defined below. +template +class Handler; + +//===----------------------------------------------------------------------===// +// XLA FFI virtual base for implementing FFI handlers +//===----------------------------------------------------------------------===// + +class Ffi { + public: + // Creates an empty binding specification which allows to define FFI handler + // signature separately from implementation and rely on compile time type + // checking to verify that signature matches the provided implementation. + template + static Binding Bind(); + + // Creates an empty binding for the instantiate stage. + static Binding BindInstantiate(); + + // Automatic FFI binding that does binding specification inference from the + // `fn` type signature and binds `fn` to it. This enables a more concise FFI + // handler registration with fully automatic type inference at the cost of + // less readable error messages, template metaprogramming "magic" and a risk + // to accidentally change handler type without noticing it. + template + static auto BindTo(Fn fn, std::initializer_list traits = {}); + + virtual ~Ffi() = default; + virtual XLA_FFI_Error* Call(XLA_FFI_CallFrame* call_frame) const = 0; + + // Registers FFI handler bundle with an XLA runtime under the given name on a + // given platform. + static XLA_FFI_Error* RegisterStaticHandler( + const XLA_FFI_Api* api, std::string_view name, std::string_view platform, + XLA_FFI_Handler_Bundle bundle, XLA_FFI_Handler_Traits traits = 0); + + // Registers FFI execute handler with an XLA runtime under the given name on a + // given platform. + static XLA_FFI_Error* RegisterStaticHandler( + const XLA_FFI_Api* api, std::string_view name, std::string_view platform, + XLA_FFI_Handler* execute, XLA_FFI_Handler_Traits traits = 0) { + return RegisterStaticHandler( + api, name, platform, + XLA_FFI_Handler_Bundle{nullptr, nullptr, nullptr, execute}, traits); + } + + // Registers a custom type so that it can be used with State and UserData + // arguments to external FFI handlers. The `name` argument must be a unique + // identifier for the type, and duplicate registrations with the same name + // are not allowed. When successful, a unique ID will be returned by updating + // `type_id`. + static XLA_FFI_Error* RegisterTypeId(const XLA_FFI_Api* api, + std::string_view name, + XLA_FFI_TypeId* type_id, + XLA_FFI_TypeInfo type_info = {nullptr}); + + // This is a helper template that allows to convert function pointers from + // the run time values to compile time values (template arguments) with + // automatic template arguments deduction. + // + // Example: + // + // static Error Foo(int32_t arg) {... } + // + // template + // void call(Callable callable) { callable(42); } + // + // call(Foo); // `Foo` passed as a runtime value + // call(Ffi::Wrapper()) // `Foo` passed as a template argument + // + // In the first case compiler will not be able to inline `Foo` into the `call` + // body. However in the second case it can do that, because function pointer + // is a statically known value (template non-type argument). + template + struct Wrapper; + + template + struct Wrapper { + XLA_FFI_ATTRIBUTE_ALWAYS_INLINE Ret operator()(Args... args) const { + return fn(std::forward(args)...); + } + }; + + protected: + template + static std::string StrCat(Args... args); + + static XLA_FFI_Error* Sucess(); + + static XLA_FFI_Error* MakeError(const XLA_FFI_Api* api, + XLA_FFI_Error_Code errc, std::string message); + + static XLA_FFI_Error* InvalidArgument(const XLA_FFI_Api* api, + std::string message); + + static XLA_FFI_Error* CheckStructSize(const XLA_FFI_Api* api, + std::string_view struct_name, + size_t expected, size_t actual); + + static XLA_FFI_Error* StructSizeIsGreaterOrEqual(const XLA_FFI_Api* api, + std::string_view struct_name, + size_t expected, + size_t actual); +}; + +inline XLA_FFI_Error* Ffi::RegisterStaticHandler( + const XLA_FFI_Api* api, std::string_view name, std::string_view platform, + XLA_FFI_Handler_Bundle bundle, XLA_FFI_Handler_Traits traits) { + XLA_FFI_Handler_Register_Args args; + args.struct_size = XLA_FFI_Handler_Register_Args_STRUCT_SIZE; + args.extension_start = nullptr; + args.name = XLA_FFI_ByteSpan{name.data(), name.size()}; + args.platform = XLA_FFI_ByteSpan{platform.data(), platform.size()}; + args.bundle = bundle; + args.traits = traits; + return api->XLA_FFI_Handler_Register(&args); +} + +inline XLA_FFI_Error* Ffi::RegisterTypeId(const XLA_FFI_Api* api, + std::string_view name, + XLA_FFI_TypeId* type_id, + XLA_FFI_TypeInfo type_info) { + XLA_FFI_TypeId_Register_Args args; + args.struct_size = XLA_FFI_TypeId_Register_Args_STRUCT_SIZE; + args.extension_start = nullptr; + args.name = XLA_FFI_ByteSpan{name.data(), name.size()}; + args.type_id = type_id; + return api->XLA_FFI_TypeId_Register(&args); +} + +template +std::string Ffi::StrCat(Args... args) { + std::stringstream ss; + (ss << ... << args); + return ss.str(); +} + +inline XLA_FFI_Error* Ffi::Sucess() { return nullptr; } + +inline XLA_FFI_Error* Ffi::MakeError(const XLA_FFI_Api* api, + XLA_FFI_Error_Code errc, + std::string message) { + XLA_FFI_Error_Create_Args args; + args.struct_size = XLA_FFI_Error_Create_Args_STRUCT_SIZE; + args.extension_start = nullptr; + args.errc = errc; + args.message = message.c_str(); + return api->XLA_FFI_Error_Create(&args); +} + +inline XLA_FFI_Error* Ffi::InvalidArgument(const XLA_FFI_Api* api, + std::string message) { + return MakeError(api, XLA_FFI_Error_Code_INVALID_ARGUMENT, + std::move(message)); +} + +inline XLA_FFI_Error* Ffi::CheckStructSize(const XLA_FFI_Api* api, + std::string_view struct_name, + size_t expected, size_t actual) { + if (XLA_FFI_PREDICT_FALSE(expected != actual)) { + return InvalidArgument( + api, StrCat("Unexpected ", struct_name, " size: expected ", expected, + " got ", actual, ". Check installed software versions.")); + } + return nullptr; +} + +inline XLA_FFI_Error* Ffi::StructSizeIsGreaterOrEqual( + const XLA_FFI_Api* api, std::string_view struct_name, size_t expected, + size_t actual) { + if (XLA_FFI_PREDICT_FALSE(actual < expected)) { + return InvalidArgument( + api, StrCat("Unexpected ", struct_name, " size: expected at least ", + expected, " got ", actual, + ". Check installed software versions.")); + } + return nullptr; +} + +//===----------------------------------------------------------------------===// +// Type tags for distinguishing handler argument types +//===----------------------------------------------------------------------===// + +// Forward declare. +class Dictionary; + +namespace internal { + +// WARNING: A lot of template metaprogramming on top of C++ variadic templates +// parameter packs. We need this to be able to pattern match FFI handler +// signature at compile time. + +// A type tag for decoding optional argument. +template +struct OptionalArgTag {}; + +// A type tag to forward all remaining args as `RemainingArgs`. +struct RemainingArgsTag {}; + +// A type tag to distinguish parameters tied to results in the `Binding` +// variadic template. In XLA FFI we use destination passing style APIs and don't +// return anything from the handler, but instead pass a destination where the +// handler should write the result. +template +struct RetTag {}; + +// A type tag for decoding optional result. +template +struct OptionalRetTag {}; + +// A type tag to forward all remaining results as `RemainingRets`. +struct RemainingRetsTag {}; + +// A type tag to distinguish parameters tied to the attributes in the +// `Binding` variadic template. +template +struct AttrTag {}; + +// A type tag to forward all attributes as `Dictionary` (and optionally decode +// it into a custom struct). +template +struct AttrsTag {}; + +// A type tag to distinguish parameter extracted from an execution context. +template +struct CtxTag {}; + +//----------------------------------------------------------------------------// +// A template for counting tagged arguments in the Ts pack (i.e. attributes). +//----------------------------------------------------------------------------// + +template