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/reflection/registry.h>
30#include <tvm/ffi/string.h>
31#include <tvm/ir/cow.h>
32#include <tvm/ir/expr.h>
33#include <tvm/ir/function.h>
34#include <tvm/ir/global_info.h>
35#include <tvm/ir/source_map.h>
36#include <tvm/ir/type.h>
37
38#include <string>
39#include <unordered_map>
40#include <unordered_set>
41#include <utility>
42#include <vector>
43
44namespace tvm {
45
46class IRModule;
47
58class IRModuleNode : public ffi::Object {
59 public:
61 ffi::Map<GlobalVar, BaseFunc> functions;
64 /* \brief Additional attributes storing meta-data about the module. */
67 ffi::Map<ffi::String, ffi::Array<GlobalInfo>> global_infos;
72 ffi::Map<ffi::String, GlobalVar> global_var_map_;
73
93 template <typename TObjectRef>
94 ffi::Optional<TObjectRef> GetAttr(
95 const std::string& attr_key,
96 ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt)) const {
97 return attrs.GetAttr(attr_key, default_value);
98 }
99 // variant that uses TObjectRef to enable implicit conversion to default value.
100 template <typename TObjectRef>
101 ffi::Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
102 return GetAttr<TObjectRef>(attr_key, ffi::Optional<TObjectRef>(default_value));
103 }
104
109 DictAttrs GetAttrs() const { return attrs; }
110
130 bool HasNonzeroAttr(const std::string& attr_key) const { return attrs.HasNonzeroAttr(attr_key); }
131
133
134 static void RegisterReflection() {
135 namespace refl = tvm::ffi::reflection;
136 refl::ObjectDef<IRModuleNode>()
137 .def_ro("functions", &IRModuleNode::functions)
138 .def_ro("global_var_map_", &IRModuleNode::global_var_map_)
139 .def_ro("source_map", &IRModuleNode::source_map)
140 .def_ro("attrs", &IRModuleNode::attrs)
141 .def_ro("global_infos", &IRModuleNode::global_infos);
142 // register custom structural equal and hash.
143 refl::TypeAttrDef<IRModuleNode>()
144 .def("__s_equal__", &IRModuleNode::SEqual)
145 .def("__s_hash__", &IRModuleNode::SHash);
146 }
147
149 ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const;
151 ffi::TypedFunction<int64_t(AnyView, int64_t, bool)> hash) const;
152
160 TVM_DLL void Add(const GlobalVar& var, const BaseFunc& func, bool update = false);
161
169 TVM_DLL void AddUnchecked(const GlobalVar& var, const BaseFunc& func);
170
176 TVM_DLL void Update(const GlobalVar& var, const BaseFunc& func);
177
183 TVM_DLL void UpdateGlobalInfo(const ffi::String& name, const ffi::Array<GlobalInfo>& info);
184
189 TVM_DLL void Remove(const GlobalVar& var);
190
196 TVM_DLL bool ContainGlobalVar(const ffi::String& name) const;
197
203 TVM_DLL GlobalVar GetGlobalVar(const ffi::String& str) const;
204
210 TVM_DLL ffi::Array<GlobalVar> GetGlobalVars() const;
211
217 TVM_DLL BaseFunc Lookup(const GlobalVar& var) const;
218
224 TVM_DLL BaseFunc Lookup(const ffi::String& name) const;
225
232
241 TVM_DLL std::unordered_set<ffi::String> Imports() const;
242
244
246
247 private:
248 friend class IRModule;
249};
250
255class IRModule : public ffi::ObjectRef {
256 public:
264 TVM_DLL explicit IRModule(ffi::Map<GlobalVar, BaseFunc> functions, SourceMap map = {},
265 DictAttrs attrs = DictAttrs(),
266 ffi::Map<ffi::String, ffi::Array<GlobalInfo>> global_infos = {});
267
274 explicit IRModule(ffi::ObjectPtr<IRModuleNode> n) : ffi::ObjectRef(n) {}
278 explicit IRModule(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
281 auto* ptr = get_mutable();
282 TVM_FFI_ICHECK(ptr != nullptr);
283 return static_cast<IRModuleNode*>(ptr);
284 }
285
290 TVM_DLL static IRModule FromExpr(const Expr& expr,
291 const ffi::Map<GlobalVar, BaseFunc>& global_funcs = {});
292
299
302
303 // allow copy on write.
305};
306
307namespace attr {
308
309// Following are attributes for IRModule only.
310
316constexpr const char* kModuleName = "mod_name";
317
324constexpr const char* kExternalMods = "external_mods";
325
353constexpr const char* kSystemLibPrefix = "system_lib_prefix";
354
362constexpr const char* kConstNameToConstant = "const_name_to_constant";
363
364} // namespace attr
365} // namespace tvm
366#endif // TVM_IR_MODULE_H_
Managed reference to BaseFuncNode.
Definition function.h:250
Managed reference to DictAttrsNode.
Definition attrs.h:102
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:165
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the function has an non-zero integer attr.
Definition attrs.h:200
Managed reference to ExprNode.
Definition base_expr.h:335
Managed reference to GlobalVarNode.
Definition expr.h:429
IRModule that holds functions and type definitions.
Definition module.h:58
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the module has an non-zero integer attr.
Definition module.h:130
static void RegisterReflection()
Definition module.h:134
IRModuleNode()
Definition module.h:132
void Remove(const GlobalVar &var)
Remove a function from the global environment.
bool ContainGlobalVar(const ffi::String &name) const
Check if the global_var_map_ contains a global variable.
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Definition module.h:101
ffi::Map< ffi::String, GlobalVar > global_var_map_
A map from string names to global variables that ensures global uniqueness.
Definition module.h:72
void UpdateGlobalInfo(const ffi::String &name, const ffi::Array< GlobalInfo > &info)
Update an array of global infos in the global environment.
std::unordered_set< ffi::String > Imports() const
The set of imported files.
void AddUnchecked(const GlobalVar &var, const BaseFunc &func)
Add a function to the global environment.
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition module.h:243
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.IRModule", IRModuleNode, ffi::Object)
ffi::Map< ffi::String, ffi::Array< GlobalInfo > > global_infos
Globally static object that are referred by the IR itself.
Definition module.h:67
IRModule ShallowCopy()
Create a shallow copy of this IRModule.
BaseFunc Lookup(const GlobalVar &var) const
Look up a global function by its variable.
ffi::Array< GlobalVar > GetGlobalVars() const
Collect all global vars defined in this module, ordered by the global variable name.
void Update(const IRModule &other)
Update the functions inside this environment by functions in another environment.
BaseFunc Lookup(const ffi::String &name) const
Look up a global function by its string name.
DictAttrs GetAttrs() const
Get the metadata attributes.
Definition module.h:109
ffi::Map< GlobalVar, BaseFunc > functions
A map from ids to all global functions.
Definition module.h:61
void Add(const GlobalVar &var, const BaseFunc &func, bool update=false)
Add a function to the global environment.
SourceMap source_map
The source map for the module.
Definition module.h:63
void Update(const GlobalVar &var, const BaseFunc &func)
Update a function in the global environment.
bool SEqual(const IRModuleNode *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
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:94
GlobalVar GetGlobalVar(const ffi::String &str) const
Lookup a global function by its variable.
DictAttrs attrs
Definition module.h:65
Managed reference class to IRModuleNode.
Definition module.h:255
IRModule(ffi::UnsafeInit tag)
constructor with UnsafeInit
Definition module.h:278
IRModule(ffi::ObjectPtr< IRModuleNode > n)
constructor
Definition module.h:274
IRModuleNode * operator->() const
Definition module.h:280
IRModule(ffi::Map< GlobalVar, BaseFunc > functions, SourceMap map={}, DictAttrs attrs=DictAttrs(), ffi::Map< ffi::String, ffi::Array< GlobalInfo > > global_infos={})
constructor
TVM_DEFINE_OBJECT_REF_COW_METHOD(IRModuleNode)
IRModule()
default constructor
Definition module.h:269
IRModule ShallowCopyIRModule(IRModule mod)
Create a shallow copy of an IRModule.
static 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.
Definition source_map.h:205
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Copy-on-write helper macro for IR ffi::ObjectRef types.
Base expr nodes in TVM.
Function nodes.
GlobalInfo are globally static object that are referred by the IR itself.
IR/AST nodes for TVM types shared across IR variants.
constexpr const char * kConstNameToConstant
All the named runtime::Tensors accumulated during compilation by external codegen....
Definition module.h:362
constexpr const char * kModuleName
Name of the module.
Definition module.h:316
constexpr const char * kExternalMods
All the runtime::Modules accumulated during compilation by external codegen. These modules must be ei...
Definition module.h:324
constexpr const char * kSystemLibPrefix
A prefix for generating C symbols system lib creation.
Definition module.h:353
IRModuleFrame IRModule()
The IRModule declaration statement.
Definition module.h:248
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
A map from source names to source code.