tvm
Loading...
Searching...
No Matches
module.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
24#ifndef TVM_IR_MODULE_H_
25#define TVM_IR_MODULE_H_
26
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>
35#include <tvm/ir/cow.h>
36#include <tvm/ir/expr.h>
37#include <tvm/ir/function.h>
38#include <tvm/ir/global_info.h>
39#include <tvm/ir/location.h>
40#include <tvm/ir/type.h>
41
42#include <string>
43#include <unordered_map>
44#include <unordered_set>
45#include <utility>
46#include <vector>
47
48namespace tvm {
49
50class IRModule;
51
62class IRModuleObj : public ffi::Object {
63 public:
65 ffi::Map<GlobalVar, BaseFunc> functions;
66 /* \brief Additional attributes storing meta-data about the module. */
69 ffi::Map<ffi::String, ffi::Array<GlobalInfo>> global_infos;
74 ffi::Map<ffi::String, GlobalVar> global_var_map_;
75
95 template <typename TObjectRef>
96 ffi::Optional<TObjectRef> GetAttr(
97 const std::string& attr_key,
98 ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt)) const {
99 return attrs.GetAttr(attr_key, default_value);
100 }
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 {
111 return attrs.GetAttr(key, std::move(default_value));
112 }
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)));
118 }
119
120 // variant that uses TObjectRef to enable implicit conversion to 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));
124 }
125
130 DictAttrs GetAttrs() const { return attrs; }
131
151 bool HasNonzeroAttr(const std::string& attr_key) const { return attrs.HasNonzeroAttr(attr_key); }
152
153 IRModuleObj() = default;
154
155 static void RegisterReflection() {
156 namespace refl = tvm::ffi::reflection;
157 refl::ObjectDef<IRModuleObj>()
158 .def_ro("functions", &IRModuleObj::functions)
159 .def_ro("global_var_map_", &IRModuleObj::global_var_map_)
160 .def_ro("attrs", &IRModuleObj::attrs)
161 .def_ro("global_infos", &IRModuleObj::global_infos);
162 // register custom structural equal and hash.
163 refl::TypeAttrDef<IRModuleObj>()
164 .def(tvm::ffi::type_attr::kSEqual.name, &IRModuleObj::SEqual)
165 .def(tvm::ffi::type_attr::kSHash.name, &IRModuleObj::SHash);
166 }
167
168 TVM_TIRX_DLL bool SEqual(const IRModuleObj* other,
169 ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const;
170 TVM_TIRX_DLL int64_t SHash(int64_t init_hash,
171 ffi::TypedFunction<int64_t(AnyView, int64_t, bool)> hash) const;
172
180 void Add(const GlobalVar& var, const BaseFunc& func, bool update = false) {
181 BaseFunc checked_func = func;
182 AddUnchecked(var, checked_func);
183 }
184
192 TVM_TIRX_DLL void AddUnchecked(const GlobalVar& var, const BaseFunc& func);
193
199 void Update(const GlobalVar& var, const BaseFunc& func) { this->Add(var, func, true); }
200
206 void UpdateGlobalInfo(const ffi::String& name, const ffi::Array<GlobalInfo>& info) {
207 this->global_infos.Set(name, info);
208 }
209
214 void Remove(const GlobalVar& var) {
215 auto functions_node = this->functions.CopyOnWrite();
216 functions_node->erase(var);
217 auto gvar_node = global_var_map_.CopyOnWrite();
218 gvar_node->erase(var->name_hint);
219 }
220
226 bool ContainGlobalVar(const ffi::String& name) const {
227 return global_var_map_.find(name) != global_var_map_.end();
228 }
229
235 TVM_TIRX_DLL GlobalVar GetGlobalVar(const ffi::String& str) const;
236
242 TVM_TIRX_DLL ffi::Array<GlobalVar> GetGlobalVars() const;
243
249 BaseFunc Lookup(const GlobalVar& var) const {
250 auto it = functions.find(var);
251 TVM_FFI_ICHECK(it != functions.end()) << "There is no definition of " << var;
252 return (*it).second;
253 }
254
260 BaseFunc Lookup(const ffi::String& name) const {
261 GlobalVar id = this->GetGlobalVar(name);
262 return this->Lookup(id);
263 }
264
270 void Update(const IRModule& other);
271
277
278 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
279
280 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.IRModule", IRModuleObj, ffi::Object);
281 // Shared instances are mutated in place, so the class is mutable.
282 static constexpr bool _type_mutable = true;
283
284 private:
285 friend class IRModule;
286};
287
292class IRModule : public ffi::ObjectRef {
293 public:
294 static constexpr bool _type_is_nullable = false;
301 TVM_TIRX_DLL explicit IRModule(ffi::Map<GlobalVar, BaseFunc> functions,
302 DictAttrs attrs = DictAttrs(),
303 ffi::Map<ffi::String, ffi::Array<GlobalInfo>> global_infos = {});
304
306 IRModule() : IRModule(ffi::Map<GlobalVar, BaseFunc>({})) {}
308 explicit IRModule(ffi::ObjectPtr<IRModuleObj> n) : ffi::ObjectRef(std::move(n)) {
309 TVM_FFI_ICHECK(data_ != nullptr);
310 }
314 explicit IRModule(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
317 auto* ptr = get_mutable();
318 TVM_FFI_ICHECK(ptr != nullptr);
319 return static_cast<IRModuleObj*>(ptr);
320 }
321
326 TVM_TIRX_DLL static IRModule FromExpr(const Expr& expr,
327 const ffi::Map<GlobalVar, BaseFunc>& global_funcs = {});
328
331
332 // allow copy on write.
334};
335
336inline void IRModuleObj::Update(const IRModule& mod) {
337 for (auto pair : mod->functions) {
338 this->AddUnchecked(pair.first, pair.second);
339 }
340}
341
343 return IRModule(this->functions, this->attrs, this->global_infos);
344}
345
346namespace attr {
347
348// Following are attributes for IRModule only.
349
356inline constexpr ffi::reflection::AttrKey<ffi::Array<ffi::Module>> kExternalMods{"external_mods"};
357
385inline constexpr ffi::reflection::AttrKey<ffi::String> kSystemLibPrefix{"system_lib_prefix"};
386
387} // namespace attr
388} // namespace tvm
389#endif // TVM_IR_MODULE_H_
Base expr nodes in TVM.
Function nodes.
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
IRModuleObj()=default
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