Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion 3rdparty/cutlass_fpA_intB_gemm
4 changes: 2 additions & 2 deletions apps/cpp_rpc/rpc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ void RPCServerCreate(std::string host, int port, int port_end, std::string track
rpc.Start();
}

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def("rpc.ServerCreate", RPCServerCreate);
});
}
} // namespace runtime
} // namespace tvm
8 changes: 4 additions & 4 deletions apps/ios_rpc/tvmrpc/TVMRuntime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void LogMessageImpl(const std::string& file, int lineno, int level, const std::s

} // namespace detail

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef()
.def_packed("tvm.rpc.server.workpath",
Expand Down Expand Up @@ -85,7 +85,7 @@ void LogMessageImpl(const std::string& file, int lineno, int level, const std::s
*rv = Module::LoadFromFile(name, fmt);
LOG(INFO) << "Load module from " << name << " ...";
});
});
}

#if defined(USE_CUSTOM_DSO_LOADER) && USE_CUSTOM_DSO_LOADER == 1

Expand All @@ -112,15 +112,15 @@ void Init(const std::string& name) {
};

// Add UnsignedDSOLoader plugin in global registry
TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def_packed("ffi.Module.load_from_file.dylib_custom",
[](ffi::PackedArgs args, ffi::Any* rv) {
auto n = ffi::make_object<UnsignedDSOLoader>();
n->Init(args[0]);
*rv = tvm::ffi::CreateLibraryModule(n);
});
});
}

#endif

Expand Down
8 changes: 4 additions & 4 deletions docs/arch/device_target_interactions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ then be registered with the following steps.

#. Register the function to the tvm registry::

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def("device_api.foo", FooDeviceAPI::Global);
});
}

.. _base.h: https://github.com/apache/tvm/blob/main/include/tvm/runtime/base.h

Expand Down Expand Up @@ -228,10 +228,10 @@ the same name as was used in the ``TVM_REGISTER_TARGET_KIND``
definition above. ::

tvm::runtime::Module GeneratorFooCode(IRModule mod, Target target);
TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def("target.build.foo", GeneratorFooCode);
});
}

The code generator takes two arguments. The first is the ``IRModule``
to compile, and the second is the ``Target`` that describes the device
Expand Down
4 changes: 2 additions & 2 deletions docs/arch/pass_infra.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,10 @@ Python when needed.
return CreateFunctionPass(pass_func, 0, "FoldConstant", {});
}

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def("relax.transform.FoldConstant", FoldConstant);
});
}

} // namespace transform

Expand Down
10 changes: 5 additions & 5 deletions docs/arch/runtime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ The following example registers PackedFunc in C++ and calls from python.
.. code:: c

// register a global packed function in c++
TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def_packed("myadd", MyAdd);
});
}

.. code:: python

Expand Down Expand Up @@ -112,13 +112,13 @@ we can pass functions from python (as PackedFunc) to C++.

.. code:: c

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def_packed("callhello", [](ffi::PackedArgs args, ffi::Any* rv) {
ffi::Function f = args[0].cast<ffi::Function>();
f("hello world");
});
});
}

.. code:: python

Expand Down Expand Up @@ -230,7 +230,7 @@ Each ``Object`` subclass will override this to register its members. Here is an
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.IntImm", IntImmNode, PrimExprNode);
};
// in cc file
TVM_FFI_STATIC_INIT_BLOCK({ IntImmNode::RegisterReflection(); });
TVM_FFI_STATIC_INIT_BLOCK() { IntImmNode::RegisterReflection(); }

The RegisterReflection gives us a reflection API to register each member of the object.
We can use this function to visit the node and serialize any language object recursively.
Expand Down
4 changes: 2 additions & 2 deletions ffi/docs/guides/packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ void RaiseError(ffi::String msg) {
TVM_FFI_THROW(RuntimeError) << msg;
}

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef()
.def("my_ffi_extension.raise_error", RaiseError);
});
}
```

Make sure to have a unique name across all registered functions when registering a global function.
Expand Down
4 changes: 2 additions & 2 deletions ffi/docs/guides/python_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public:
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TestIntPair, tvm::ffi::ObjectRef, TestIntPairObj);
};

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
// register the object into the system
// register field accessors and a global static function `__create__` as ffi::Function
Expand All @@ -213,7 +213,7 @@ TVM_FFI_STATIC_INIT_BLOCK({
.def_static("__create__", [](int64_t a, int64_t b) -> TestIntPair {
return TestIntPair(a, b);
});
});
}
```

You can then create wrapper classes for objects that are in the library as follows:
Expand Down
4 changes: 2 additions & 2 deletions ffi/examples/packaging/src/extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ TVM_FFI_DLL_EXPORT_TYPED_FUNC(add_one, my_ffi_extension::AddOne);

// The static initialization block is
// called once when the library is loaded.
TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
// In this particular example, we use the reflection mechanisms to
// register the functions directly into the global function table.
Expand All @@ -85,5 +85,5 @@ TVM_FFI_STATIC_INIT_BLOCK({
// tvm::ffi::Module::LoadFromFile, instead, just load the dll or simply bundle into the
// final project
refl::GlobalDef().def("my_ffi_extension.raise_error", RaiseError);
});
}
} // namespace my_ffi_extension
40 changes: 32 additions & 8 deletions ffi/include/tvm/ffi/base_details.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@
#define TVM_FFI_UNREACHABLE() __builtin_unreachable()
#endif

/*! \brief helper macro to suppress unused warning */
#define TVM_FFI_ATTRIBUTE_UNUSED [[maybe_unused]]

#define TVM_FFI_STR_CONCAT_(__x, __y) __x##__y
#define TVM_FFI_STR_CONCAT(__x, __y) TVM_FFI_STR_CONCAT_(__x, __y)

Expand All @@ -86,12 +83,39 @@
#define TVM_FFI_FUNC_SIG __func__
#endif

#define TVM_FFI_STATIC_INIT_BLOCK_VAR_DEF \
TVM_FFI_ATTRIBUTE_UNUSED static inline int __##TVMFFIStaticInitReg
#if defined(__GNUC__)
// gcc and clang and attribute constructor
/// \cond Doxygen_Suppress
#define TVM_FFI_STATIC_INIT_BLOCK_DEF_(FnName) __attribute__((constructor)) static void FnName()
/// \endcond
/*
* \brief Macro that defines a block that will be called during static initialization.
*
* \code
* TVM_FFI_STATIC_INIT_BLOCK() {
* RegisterFunctions();
* }
* \endcode
*/
#define TVM_FFI_STATIC_INIT_BLOCK() \
TVM_FFI_STATIC_INIT_BLOCK_DEF_(TVM_FFI_STR_CONCAT(__TVMFFIStaticInitFunc, __COUNTER__))

/*! \brief helper macro to run code once during initialization */
#define TVM_FFI_STATIC_INIT_BLOCK(Body) \
TVM_FFI_STR_CONCAT(TVM_FFI_STATIC_INIT_BLOCK_VAR_DEF, __COUNTER__) = []() { Body return 0; }()
#else
/// \cond Doxygen_Suppress
// for other compilers, use the variable trick
#define TVM_FFI_STATIC_INIT_BLOCK_DEF_(FnName, RegVar) \
static void FnName(); \
[[maybe_unused]] static inline int RegVar = []() { \
FnName(); \
return 0; \
}(); \
static void FnName()

#define TVM_FFI_STATIC_INIT_BLOCK() \
TVM_FFI_STATIC_INIT_BLOCK_DEF_(TVM_FFI_STR_CONCAT(__TVMFFIStaticInitFunc, __COUNTER__), \
TVM_FFI_STR_CONCAT(__TVMFFIStaticInitReg, __COUNTER__))
/// \endcond
#endif

/*
* \brief Define the default copy/move constructor and assign operator
Expand Down
4 changes: 2 additions & 2 deletions ffi/src/ffi/container.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MapForwardIterFunctor {
ffi::MapObj::iterator end_;
};

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef()
.def_packed("ffi.Array",
Expand All @@ -83,6 +83,6 @@ TVM_FFI_STATIC_INIT_BLOCK({
.def("ffi.MapForwardIterFunctor", [](const ffi::MapObj* n) -> ffi::Function {
return ffi::Function::FromTyped(MapForwardIterFunctor(n->begin(), n->end()));
});
});
}
} // namespace ffi
} // namespace tvm
4 changes: 2 additions & 2 deletions ffi/src/ffi/extra/json_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,11 @@ json::Value Parse(const String& json_str, String* error_msg) {
return JSONParser::Parse(json_str, error_msg);
}

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def("ffi.json.Parse",
[](const String& json_str) { return json::Parse(json_str); });
});
}

} // namespace json
} // namespace ffi
Expand Down
4 changes: 2 additions & 2 deletions ffi/src/ffi/extra/json_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ String Stringify(const json::Value& value, Optional<int> indent) {
return JSONWriter::Stringify(value, indent);
}

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def("ffi.json.Stringify", Stringify);
});
}

} // namespace json
} // namespace ffi
Expand Down
4 changes: 2 additions & 2 deletions ffi/src/ffi/extra/library_module_dynamic_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ void DSOLibrary::Unload() {
}
#endif

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def("ffi.Module.load_from_file.so", [](String library_path, String) {
return CreateLibraryModule(make_object<DSOLibrary>(library_path));
});
});
}
} // namespace ffi
} // namespace tvm
4 changes: 2 additions & 2 deletions ffi/src/ffi/extra/library_module_system_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class SystemLibModuleRegistry {
Map<String, ffi::Module> lib_map_;
};

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def_packed("ffi.SystemLib", [](ffi::PackedArgs args, ffi::Any* rv) {
String symbol_prefix = "";
Expand All @@ -133,7 +133,7 @@ TVM_FFI_STATIC_INIT_BLOCK({
}
*rv = SystemLibModuleRegistry::Global()->GetOrCreateModule(symbol_prefix);
});
});
}
} // namespace ffi
} // namespace tvm

Expand Down
4 changes: 2 additions & 2 deletions ffi/src/ffi/extra/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Module Module::LoadFromFile(const String& file_name) {
return (*floader)(file_name, format).cast<Module>();
}

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
ModuleObj::InternalUnsafe::RegisterReflection();

Expand All @@ -144,7 +144,7 @@ TVM_FFI_STATIC_INIT_BLOCK({
.def_method("ffi.ModuleWriteToFile", &ModuleObj::WriteToFile)
.def_method("ffi.ModuleImportModule", &ModuleObj::ImportModule)
.def_method("ffi.ModuleClearImports", &ModuleObj::ClearImports);
});
}
} // namespace ffi
} // namespace tvm

Expand Down
4 changes: 2 additions & 2 deletions ffi/src/ffi/extra/reflection_extra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ inline void AccessPathRegisterReflection() {
[](const AccessPath& self, const AccessPath& other) { return self->PathEqual(other); });
}

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
AccessStepRegisterReflection();
AccessPathRegisterReflection();
refl::GlobalDef().def_packed("ffi.MakeObjectFromPackedArgs", MakeObjectFromPackedArgs);
});
}

} // namespace reflection
} // namespace ffi
Expand Down
4 changes: 2 additions & 2 deletions ffi/src/ffi/extra/serialization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ String ToJSONGraphString(const Any& value, const Any& metadata) {
return json::Stringify(ToJSONGraph(value, metadata));
}

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef()
.def("ffi.ToJSONGraph", ToJSONGraph)
Expand All @@ -424,7 +424,7 @@ TVM_FFI_STATIC_INIT_BLOCK({
.def("ffi.FromJSONGraphString", FromJSONGraphString);
refl::EnsureTypeAttrColumn("__data_to_json__");
refl::EnsureTypeAttrColumn("__data_from_json__");
});
}

} // namespace ffi
} // namespace tvm
4 changes: 2 additions & 2 deletions ffi/src/ffi/extra/structural_equal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,12 @@ Optional<reflection::AccessPathPair> StructuralEqual::GetFirstMismatch(const Any
return reflection::AccessPathPair(lhs_path, rhs_path);
}

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def("ffi.GetFirstStructuralMismatch", StructuralEqual::GetFirstMismatch);
// ensure the type attribute column is presented in the system even if it is empty.
refl::EnsureTypeAttrColumn("__s_equal__");
});
}

} // namespace ffi
} // namespace tvm
4 changes: 2 additions & 2 deletions ffi/src/ffi/extra/structural_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ uint64_t StructuralHash::Hash(const Any& value, bool map_free_vars, bool skip_te
return handler.HashAny(value);
}

TVM_FFI_STATIC_INIT_BLOCK({
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def("ffi.StructuralHash", StructuralHash::Hash);
refl::EnsureTypeAttrColumn("__s_hash__");
});
}

} // namespace ffi
} // namespace tvm
Loading
Loading