junrushao commented on code in PR #191: URL: https://github.com/apache/tvm-ffi/pull/191#discussion_r2463223804
########## docs/get_started/stable_c_abi.rst: ########## @@ -0,0 +1,190 @@ +.. 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 +============ + +Apache TVM FFI is centered around 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. + +Built on top of this stable C ABI idea, TVM FFI provides an extensible, performant and ecosystem-friendly open solution for all. + +This rest of this tutorial includes: + +- Elaborates what the stable C layout looks like; +- Examples C codes that follow the stable C 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: represent the type index, indicating which type the value holds. By definition, it represents at most 2^32 different types. +- Next 32 bits: null padding, or reserved as special flags in rare cases. +- Last 64 bits: represent the value itself, which can be an 64-bit integer, floating pointer number, or pointer to a heap allocated object. Review Comment: Good catch! -- 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]
