Skip to content

Commit aa4c818

Browse files
authored
[FFI][ABI] ABI Updates to for future metadata and complex ordering (#18254)
This PR updates the ABI to enable potential future need for getting metadata from a dynamically loaded module. Orders the current static object into simple objects that have C ABI and more complex one that may need c++. These items changes ABI to be future compact before we freeze.
1 parent 601da7b commit aa4c818

File tree

8 files changed

+62
-24
lines changed

8 files changed

+62
-24
lines changed

ffi/include/tvm/ffi/c_api.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,22 @@ typedef enum {
126126
kTVMFFIError = 67,
127127
/*! \brief Function object. */
128128
kTVMFFIFunction = 68,
129-
/*! \brief Array object. */
130-
kTVMFFIArray = 69,
131-
/*! \brief Map object. */
132-
kTVMFFIMap = 70,
133129
/*!
134130
* \brief Shape object, layout = { TVMFFIObject, { const int64_t*, size_t }, ... }
135131
*/
136-
kTVMFFIShape = 71,
132+
kTVMFFIShape = 69,
137133
/*!
138134
* \brief NDArray object, layout = { TVMFFIObject, DLTensor, ... }
139135
*/
140-
kTVMFFINDArray = 72,
141-
/*! \brief Runtime module object. */
136+
kTVMFFINDArray = 70,
137+
/*! \brief Array object. */
138+
kTVMFFIArray = 71,
139+
//----------------------------------------------------------------
140+
// more complex objects
141+
//----------------------------------------------------------------
142+
/*! \brief Map object. */
143+
kTVMFFIMap = 72,
144+
/*! \brief Runtime dynamic loaded module object. */
142145
kTVMFFIModule = 73,
143146
kTVMFFIStaticObjectEnd,
144147
// [Section] Dynamic Boxed: [kTVMFFIDynObjectBegin, +oo)
@@ -763,11 +766,11 @@ typedef struct TVMFFITypeInfo {
763766
*
764767
* \param name The name of the function.
765768
* \param f The function to be registered.
766-
* \param override Whether allow override already registered function.
769+
* \param allow_override Whether allow override already registered function.
767770
* \return 0 when success, nonzero when failure happens
768771
*/
769772
TVM_FFI_DLL int TVMFFIFunctionSetGlobal(const TVMFFIByteArray* name, TVMFFIObjectHandle f,
770-
int override);
773+
int allow_override);
771774

772775
/*!
773776
* \brief Register the function to runtime's global table with method info.
@@ -780,7 +783,7 @@ TVM_FFI_DLL int TVMFFIFunctionSetGlobal(const TVMFFIByteArray* name, TVMFFIObjec
780783
* \return 0 when success, nonzero when failure happens
781784
*/
782785
TVM_FFI_DLL int TVMFFIFunctionSetGlobalFromMethodInfo(const TVMFFIMethodInfo* method_info,
783-
int override);
786+
int allow_override);
784787

785788
/*!
786789
* \brief Register type field information for runtime reflection.

ffi/include/tvm/ffi/extra/module.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class TVM_FFI_EXTRA_CXX_API ModuleObj : public Object {
6868
* \return True if the module implements the function, false otherwise.
6969
*/
7070
virtual bool ImplementsFunction(const String& name) { return GetFunction(name).defined(); }
71+
/*!
72+
* \brief Get the metadata of the function, if available.
73+
* \param name The name of the function.
74+
* \return The metadata stored in json string format.
75+
*/
76+
virtual Optional<String> GetFunctionMetadata(const String& name) { return std::nullopt; }
7177
/*!
7278
* \brief Write the current module to file with given format (for further compilation).
7379
*
@@ -121,6 +127,12 @@ class TVM_FFI_EXTRA_CXX_API ModuleObj : public Object {
121127
* \return True if the module implements the function, false otherwise.
122128
*/
123129
bool ImplementsFunction(const String& name, bool query_imports);
130+
/*!
131+
* \brief Get the function metadata of the function if available.
132+
* \param name The name of the function.
133+
* \return The function metadata of the function in json format.
134+
*/
135+
Optional<String> GetFunctionMetadata(const String& name, bool query_imports);
124136
/*!
125137
* \brief Get the imports of the module.
126138
* \return The imports of the module.
@@ -215,6 +227,8 @@ namespace symbol {
215227
constexpr const char* tvm_ffi_library_ctx = "__tvm_ffi_library_ctx";
216228
/*! \brief Global variable to store binary data alongside a library module. */
217229
constexpr const char* tvm_ffi_library_bin = "__tvm_ffi_library_bin";
230+
/*! \brief Optional metadata prefix of a symbol. */
231+
constexpr const char* tvm_ffi_metadata_prefix = "__tvm_ffi_metadata_";
218232
/*! \brief Default entry function of a library module. */
219233
constexpr const char* tvm_ffi_main = "__tvm_ffi_main__";
220234
} // namespace symbol

ffi/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
[project]
1919
name = "apache-tvm-ffi"
20-
version = "0.1.0a2"
20+
version = "0.1.0a3"
2121
description = "tvm ffi"
2222

2323
authors = [{ name = "TVM FFI team" }]

ffi/python/tvm_ffi/cython/base.pxi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ cdef extern from "tvm/ffi/c_api.h":
4848
kTVMFFIBytes = 66
4949
kTVMFFIError = 67
5050
kTVMFFIFunction = 68
51-
kTVMFFIArray = 69
52-
kTVMFFIMap = 70
53-
kTVMFFIShape = 71
54-
kTVMFFINDArray = 72
51+
kTVMFFIShape = 69
52+
kTVMFFINDArray = 70
53+
kTVMFFIArray = 71
54+
kTVMFFIMap = 72
5555
kTVMFFIModule = 73
5656

57+
5758
ctypedef void* TVMFFIObjectHandle
5859

5960
ctypedef struct DLDataType:

ffi/scripts/run_tests.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
# under the License.
1818
set -euxo pipefail
1919

20-
BUILD_TYPE=Release
20+
BUILD_TYPE=RelWithDebugInfo
21+
22+
rm -rf build/CMakeCache.txt
2123

2224
cmake -G Ninja -S . -B build -DTVM_FFI_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
2325
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_CXX_COMPILER_LAUNCHER=ccache

ffi/src/ffi/extra/module.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ Optional<Function> ModuleObj::GetFunction(const String& name, bool query_imports
4444
return std::nullopt;
4545
}
4646

47+
Optional<String> ModuleObj::GetFunctionMetadata(const String& name, bool query_imports) {
48+
if (auto opt_metadata = this->GetFunctionMetadata(name)) {
49+
return opt_metadata;
50+
}
51+
if (query_imports) {
52+
for (const Any& import : imports_) {
53+
if (auto opt_metadata = import.cast<Module>()->GetFunctionMetadata(name, query_imports)) {
54+
return *opt_metadata;
55+
}
56+
}
57+
}
58+
return std::nullopt;
59+
}
60+
4761
void ModuleObj::ImportModule(const Module& other) {
4862
std::unordered_set<const ModuleObj*> visited{other.operator->()};
4963
std::vector<const ModuleObj*> stack{other.operator->()};
@@ -115,6 +129,10 @@ TVM_FFI_STATIC_INIT_BLOCK({
115129
[](Module mod, String name, bool query_imports) {
116130
return mod->ImplementsFunction(name, query_imports);
117131
})
132+
.def_method("ffi.ModuleGetFunctionMetadata",
133+
[](Module mod, String name, bool query_imports) {
134+
return mod->GetFunctionMetadata(name, query_imports);
135+
})
118136
.def_method("ffi.ModuleGetFunction",
119137
[](Module mod, String name, bool query_imports) {
120138
return mod->GetFunction(name, query_imports);

jvm/core/src/main/java/org/apache/tvm/TypeIndex.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public class TypeIndex {
3636
public static final int kTVMFFIBytes = 66;
3737
public static final int kTVMFFIError = 67;
3838
public static final int kTVMFFIFunction = 68;
39-
public static final int kTVMFFIArray = 69;
40-
public static final int kTVMFFIMap = 70;
41-
public static final int kTVMFFIShape = 71;
42-
public static final int kTVMFFINDArray = 72;
39+
public static final int kTVMFFIShape = 70;
40+
public static final int kTVMFFINDArray = 71;
41+
public static final int kTVMFFIArray = 72;
42+
public static final int kTVMFFIMap = 73;
4343
public static final int kTVMFFIModule = 73;
4444
}

web/src/ctypes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ export const enum TypeIndex {
9797
kTVMFFIFunction = 68,
9898
/*! \brief Array object. */
9999
kTVMFFIArray = 69,
100-
/*! \brief Map object. */
101-
kTVMFFIMap = 70,
102100
/*!
103101
* \brief Shape object, layout = { TVMFFIObject, { const int64_t*, size_t }, ... }
104102
*/
105-
kTVMFFIShape = 71,
103+
kTVMFFIShape = 70,
106104
/*!
107105
* \brief NDArray object, layout = { TVMFFIObject, DLTensor, ... }
108106
*/
109-
kTVMFFINDArray = 72,
107+
kTVMFFINDArray = 71,
108+
/*! \brief Map object. */
109+
kTVMFFIMap = 72,
110110
/*! \brief Runtime module object. */
111111
kTVMFFIModule = 73,
112112
}

0 commit comments

Comments
 (0)