24#ifndef TVM_IR_MODULE_H_
25#define TVM_IR_MODULE_H_
27#include <tvm/ffi/container/array.h>
28#include <tvm/ffi/container/map.h>
29#include <tvm/ffi/container/tensor.h>
30#include <tvm/ffi/extra/module.h>
31#include <tvm/ffi/extra/structural_equal.h>
32#include <tvm/ffi/extra/structural_hash.h>
33#include <tvm/ffi/reflection/registry.h>
34#include <tvm/ffi/string.h>
43#include <unordered_map>
44#include <unordered_set>
95 template <
typename TObjectRef>
97 const std::string& attr_key,
98 ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt))
const {
107 template <
typename T>
108 ffi::Optional<T>
GetAttr(ffi::reflection::AttrKey<T> key,
109 ffi::Optional<
typename ffi::reflection::AttrKey<T>::ValueType>
110 default_value = std::nullopt)
const {
114 template <
typename T>
115 ffi::Optional<T>
GetAttr(ffi::reflection::AttrKey<T> key,
116 typename ffi::reflection::AttrKey<T>::ValueType default_value)
const {
117 return GetAttr(key, ffi::Optional<T>(std::move(default_value)));
121 template <
typename TObjectRef>
122 ffi::Optional<TObjectRef>
GetAttr(
const std::string& attr_key, TObjectRef default_value)
const {
123 return GetAttr<TObjectRef>(attr_key, ffi::Optional<TObjectRef>(default_value));
156 namespace refl = tvm::ffi::reflection;
157 refl::ObjectDef<IRModuleObj>()
163 refl::TypeAttrDef<IRModuleObj>()
169 ffi::TypedFunction<
bool(AnyView, AnyView,
bool, AnyView)>
equal)
const;
171 ffi::TypedFunction<int64_t(AnyView, int64_t,
bool)> hash)
const;
207 this->global_infos.Set(name, info);
215 auto functions_node = this->functions.CopyOnWrite();
216 functions_node->erase(var);
218 gvar_node->erase(var->name_hint);
251 TVM_FFI_ICHECK(it !=
functions.end()) <<
"There is no definition of " << var;
303 ffi::Map<ffi::String, ffi::Array<GlobalInfo>> global_infos = {});
308 explicit IRModule(ffi::ObjectPtr<IRModuleObj> n) : ffi::ObjectRef(std::move(n)) {
309 TVM_FFI_ICHECK(data_ !=
nullptr);
314 explicit IRModule(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
317 auto* ptr = get_mutable();
318 TVM_FFI_ICHECK(ptr !=
nullptr);
327 const ffi::Map<GlobalVar, BaseFunc>& global_funcs = {});
337 for (
auto pair : mod->functions) {
343 return IRModule(this->functions, this->attrs, this->global_infos);
356inline constexpr ffi::reflection::AttrKey<ffi::Array<ffi::Module>>
kExternalMods{
"external_mods"};
385inline constexpr ffi::reflection::AttrKey<ffi::String>
kSystemLibPrefix{
"system_lib_prefix"};
Base for globally static objects referred to by the IR.
IR/AST nodes for TVM types shared across IR variants.
#define TVM_TIRX_DLL
Definition base.h:55
Managed reference to BaseFuncObj.
Definition function.h:202
Managed reference to DictAttrsObj.
Definition attrs.h:108
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, ffi::Optional< TObjectRef > default_value=ffi::Optional< TObjectRef >(std::nullopt)) const
Get a function attribute.
Definition attrs.h:171
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the function has an non-zero integer attr.
Definition attrs.h:225
Managed reference to ExprObj.
Definition expr.h:101
Managed reference to GlobalVarObj.
Definition expr.h:709
IRModule that holds functions and type definitions.
Definition module.h:62
TVM_TIRX_DLL int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
BaseFunc Lookup(const ffi::String &name) const
Look up a global function by its string name.
Definition module.h:260
ffi::Map< ffi::String, GlobalVar > global_var_map_
A map from string names to global variables that ensures global uniqueness.
Definition module.h:74
ffi::Optional< T > GetAttr(ffi::reflection::AttrKey< T > key, typename ffi::reflection::AttrKey< T >::ValueType default_value) const
Accept a default convertible to the key's value type.
Definition module.h:115
DictAttrs GetAttrs() const
Get the metadata attributes.
Definition module.h:130
friend class IRModule
The IRModule declaration statement.
Definition module.h:285
ffi::Map< GlobalVar, BaseFunc > functions
A map from ids to all global functions.
Definition module.h:65
void UpdateGlobalInfo(const ffi::String &name, const ffi::Array< GlobalInfo > &info)
Update an array of global infos in the global environment.
Definition module.h:206
void Add(const GlobalVar &var, const BaseFunc &func, bool update=false)
Add a function to the global environment.
Definition module.h:180
DictAttrs attrs
Definition module.h:67
TVM_TIRX_DLL GlobalVar GetGlobalVar(const ffi::String &str) const
Lookup a global function by its variable.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.IRModule", IRModuleObj, ffi::Object)
ffi::Map< ffi::String, ffi::Array< GlobalInfo > > global_infos
Globally static object that are referred by the IR itself.
Definition module.h:69
bool ContainGlobalVar(const ffi::String &name) const
Check if the global_var_map_ contains a global variable.
Definition module.h:226
void Update(const GlobalVar &var, const BaseFunc &func)
Update a function in the global environment.
Definition module.h:199
static void RegisterReflection()
Definition module.h:155
static constexpr bool _type_mutable
Definition module.h:282
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the module has an non-zero integer attr.
Definition module.h:151
TVM_TIRX_DLL bool SEqual(const IRModuleObj *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
ffi::Optional< T > GetAttr(ffi::reflection::AttrKey< T > key, ffi::Optional< typename ffi::reflection::AttrKey< T >::ValueType > default_value=std::nullopt) const
Get an attribute with its value type deduced only from the key.
Definition module.h:108
TVM_TIRX_DLL void AddUnchecked(const GlobalVar &var, const BaseFunc &func)
Add a function to the global environment.
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Definition module.h:122
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, ffi::Optional< TObjectRef > default_value=ffi::Optional< TObjectRef >(std::nullopt)) const
Get a module attribute.
Definition module.h:96
BaseFunc Lookup(const GlobalVar &var) const
Look up a global function by its variable.
Definition module.h:249
IRModule ShallowCopy()
Create a shallow copy of this IRModule.
Definition module.h:342
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition module.h:278
TVM_TIRX_DLL ffi::Array< GlobalVar > GetGlobalVars() const
Collect all global vars defined in this module, ordered by the global variable name.
void Remove(const GlobalVar &var)
Remove a function from the global environment.
Definition module.h:214
Managed reference class to IRModuleObj.
Definition module.h:292
static constexpr bool _type_is_nullable
Definition module.h:294
TVM_DEFINE_OBJECT_REF_COW_METHOD(IRModuleObj)
IRModule(ffi::UnsafeInit tag)
constructor with UnsafeInit
Definition module.h:314
static TVM_TIRX_DLL IRModule FromExpr(const Expr &expr, const ffi::Map< GlobalVar, BaseFunc > &global_funcs={})
As for FromExprInContext, but assuming expr is bound to 'main' and no imports.
IRModule(ffi::ObjectPtr< IRModuleObj > n)
Checked pointer construction required by ffi::RValueRef.
Definition module.h:308
IRModule()
default constructor
Definition module.h:306
IRModuleObj * operator->() const
Definition module.h:316
TVM_TIRX_DLL IRModule(ffi::Map< GlobalVar, BaseFunc > functions, DictAttrs attrs=DictAttrs(), ffi::Map< ffi::String, ffi::Array< GlobalInfo > > global_infos={})
constructor
Copy-on-write helper macro for IR ffi::ObjectRef types.
Source locations and source-text lookup.
constexpr ffi::reflection::AttrKey< ffi::Array< ffi::Module > > kExternalMods
All the runtime::Modules accumulated during compilation by external codegen. These modules must be ei...
Definition module.h:356
constexpr ffi::reflection::AttrKey< ffi::String > kSystemLibPrefix
A prefix for generating C symbols system lib creation.
Definition module.h:385
TVM_TIRX_DLL IRModuleFrame IRModule()
The IRModule declaration statement.
Definition module.h:285
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
TVM_TIRX_DLL PrimExpr equal(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
equal