Class Function#

Inheritance Relationships#

Base Type#

Class Documentation#

class Function : public tvm::ffi::ObjectRef#

ffi::Function is a type-erased function. The arguments are passed by “packed format” via AnyView

Public Functions

inline Function(std::nullptr_t)#

Constructor from null.

template<typename TCallable>
inline explicit Function(TCallable packed_call)#

Constructing a packed function from a callable type whose signature is consistent with ffi::Function

Note

legacy purpose, should change to Function::FromPacked for mostfuture use.

Parameters:

packed_call – The packed function signature

inline void CallPacked(const AnyView *args, int32_t num_args, Any *result) const#

Call the function in packed format.

Parameters:
  • args – The arguments

  • num_args – The number of arguments

  • result – The return value.

inline void CallPacked(PackedArgs args, Any *result) const#

Call the function in packed format.

Parameters:
  • args – The arguments

  • result – The return value.

Public Static Functions

template<typename TCallable>
static inline Function FromPacked(TCallable packed_call)#

Constructing a packed function from a callable type whose signature is consistent with ffi::Function

Parameters:

packed_call – The packed function signature

static inline Function FromExternC(void *self, TVMFFISafeCallType safe_call, void (*deleter)(void *self))#

Create ffi::Function from a C style callbacks.

self and deleter can be nullptr if the function do not need closure support and corresponds to a raw function pointer.

Parameters:
  • self – Resource handle to the function

  • safe_call – The safe_call definition in C.

  • deleter – The deleter to release the resource of self.

Returns:

The created function.

static inline std::optional<Function> GetGlobal(std::string_view name)#

Get global function by name.

Note

This function will return std::nullopt if the function is not found.

Parameters:

name – The function name

Returns:

The global function.

static inline std::optional<Function> GetGlobal(const std::string &name)#

Get global function by name.

Note

This function will return std::nullopt if the function is not found.

Parameters:

name – The name of the function

Returns:

The global function

static inline std::optional<Function> GetGlobal(const String &name)#

Get global function by name.

Note

This function will return std::nullopt if the function is not found.

Parameters:

name – The name of the function

Returns:

The global function

static inline std::optional<Function> GetGlobal(const char *name)#

Get global function by name.

Note

This function will return std::nullopt if the function is not found.

Parameters:

name – The name of the function

Returns:

The global function

static inline Function GetGlobalRequired(std::string_view name)#

Get global function by name and throw an error if it is not found.

Note

This function will throw an error if the function is not found.

Parameters:

name – The name of the function

Returns:

The global function

static inline Function GetGlobalRequired(const std::string &name)#

Get global function by name.

Note

This function will throw an error if the function is not found.

Parameters:

name – The name of the function

Returns:

The global function

static inline Function GetGlobalRequired(const String &name)#

Get global function by name.

Note

This function will throw an error if the function is not found.

Parameters:

name – The name of the function

Returns:

The global function

static inline Function GetGlobalRequired(const char *name)#

Get global function by name.

Note

This function will throw an error if the function is not found.

Parameters:

name – The name of the function

Returns:

The global function

static inline void SetGlobal(std::string_view name, Function func, bool override = false)#

Set global function by name.

Parameters:
  • name – The name of the function

  • func – The function

  • override – Whether to override when there is duplication.

static inline std::vector<String> ListGlobalNames()#

List all global names.

Note

This function do not depend on Array so core do not have container dep.

Returns:

A vector of all global names

static inline void RemoveGlobal(const String &name)#

Remove a global function by name.

Parameters:

name – The name of the function

template<typename TCallable>
static inline Function FromTyped(TCallable callable)#

Constructing a packed function from a normal function.

Parameters:

callable – the internal container of packed function.

template<typename TCallable>
static inline Function FromTyped(TCallable callable, std::string name)#

Constructing a packed function from a normal function.

Parameters:
  • callable – the internal container of packed function.

  • name – optional name attacked to the function.

template<typename ...Args>
static inline Any InvokeExternC(void *handle, TVMFFISafeCallType safe_call, Args&&... args)#

Directly invoke an extern “C” function that follows the TVM FFI SafeCall convention.

This function can be useful to turn an existing exported symbol into a typed function.

// An extern "C" function, matching TVMFFISafeCallType
extern "C" int __tvm_ffi_add(
  void* handle, const TVMFFIAny* args, int32_t num_args, TVMFFIAny*result
);

// redirect an existing symbol into a typed function
inline int add(int a, int b) {
  return tvm::ffi::Function::InvokeExternC(nullptr, __tvm_ffi_add, a, b).cast<int>();
}
Template Parameters:

Args – The types of the arguments to the extern function.

Parameters:
  • handle – The handle argument, for exported symbols this is usually nullptr.

  • safe_call – The function pointer to the extern “C” function.

  • args – The arguments to pass to the function.

Returns:

The return value, wrapped in a tvm::ffi::Any.