junrushao commented on code in PR #191: URL: https://github.com/apache/tvm-ffi/pull/191#discussion_r2464096660
########## docs/get_started/stable_c_abi.rst: ########## @@ -0,0 +1,189 @@ +.. Licensed to the Apache Software Foundation (ASF) under one +.. or more contributor license agreements. See the NOTICE file +.. distributed with this work for additional information +.. regarding copyright ownership. The ASF licenses this file +.. to you 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. + +Stable C ABI +============ + +TVM-FFI centers on a single key idea: + + +.. admonition:: Key Idea + :class: important + + Every function call can be represented by a single ABI stable C function: + + .. code-block:: c + + int universal_c_abi( // returns 0 if succeed, error code if failure + void* handle, // library handle + Any* args, // inputs: args[0 ... N - 1] + int N, // number of inputs + Any* result, // output: *result + ); + + where ``Any`` is a tagged union of all supported types, e.g. integers, floats, Tensors, strings, etc., and can be further extended to arbitrary user-defined types. + +Built on top of this stable C ABI, TVM-FFI provides an extensible, performant, and ecosystem-friendly open solution for all. + +The rest of this guide covers: + +- The stable C layout and calling convention of ``universal_c_abi``; +- C examples that implements the stable C ABI, and calls functions that follow this ABI. + +Stable C Layout +--------------- + +The ``universal_c_abi`` function uses a stable layout for all the input and output arguments. + +Layout of ``Any`` +~~~~~~~~~~~~~~~~~ + +TVM-FFI's ``Any`` is a fixed size (128-bit) tagged union that represents all supported types. + +- First 32 bits: type index indicating which value is stored (supports up to 2^32 types). +- Next 32 bits: null padding, or reserved for special flags in rare cases. +- Last 64 bits: payload that is either a 64-bit integer, a 64-bit floating-point number, or a pointer to a heap-allocated object. + +.. figure:: ../_static/tvm-ffi-layout-any.svg + :alt: Layout of the 128-bit Any tagged union + :name: fig:layout-any + + Figure 1. Layout spec for the ``Any`` type. + +The following conventions apply when representing values in ``Any``: + +- Primitive types: the last 64 bits directly store the value, for example: + + * Integers + * Floating-point numbers + +- Heap-allocated objects: the last 64 bits store a pointer to the actual object, for example: + + * `DLPack tensors <https://data-apis.org/array-api/2024.12/design_topics/data_interchange.html#dlpack-an-in-memory-tensor-structure>`_ + * C-style strings + +- Arbitrary objects: the type index identifies the concrete type, and the last 64 bits store a pointer to a reference-counted object in TVM-FFI's object format, for example: + + * :py:class:`tvm_ffi.Function`, representing all functions, such as Python/C++ functions/lambdas, etc.; + * :py:class:`tvm_ffi.Array` and :py:class:`tvm_ffi.Map` as list/dictionary containers of all ``Any`` values; + * Up to 2^32 types can be represented in this type system. + +Function Calling Convention +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Function calls in TVM-FFI share the same calling convention, ``universal_c_abi``, as described above. + +- ``handle: void*``: a library handle that contains the function to be called. It is usually ``nullptr`` for most use cases, but some libraries (such as cuDNN) may use it to store global contexts. Review Comment: changed to this: > ``handle: void*``: a library handle that contains the function to be called. It is usually ``nullptr`` for most use cases such as exported symbols. It can be used to enable closures or module-scoped functions that capture extra context. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
