tvm
exec_builder.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 
23 #ifndef TVM_RELAX_EXEC_BUILDER_H_
24 #define TVM_RELAX_EXEC_BUILDER_H_
25 
26 #include <tvm/ir/expr.h>
27 #include <tvm/node/reflection.h>
28 #include <tvm/node/repr_printer.h>
29 #include <tvm/runtime/object.h>
30 #include <tvm/runtime/registry.h>
33 
34 #include <string>
35 #include <unordered_map>
36 #include <vector>
37 
38 namespace tvm {
39 namespace relax {
40 
41 namespace vm = tvm::runtime::relax_vm;
42 
43 class ExecBuilder;
44 
48 class ExecBuilderNode : public Object {
49  public:
55  void DeclareFunction(const std::string& func, vm::VMFuncInfo::FuncKind kind);
64  void EmitFunction(const std::string& func, int64_t num_inputs,
65  Optional<Array<String>> param_names,
66  vm::VMFuncInfo::FuncKind kind = vm::VMFuncInfo::FuncKind::kVMFunc,
67  int64_t init_register_size = 0);
72  void EndFunction(const std::string& func);
79  void EmitCall(const std::string& func, std::vector<vm::Instruction::Arg> args, vm::RegName ret);
86  void EmitCall(vm::Instruction::Arg func, std::vector<vm::Instruction::Arg> args, vm::RegName ret);
97  void EmitGoto(vm::Index pc_offset);
104  void EmitIf(vm::Instruction::Arg cond, vm::Index false_offset);
110  vm::Instruction::Arg GetFunction(const std::string& name);
119  template <typename T>
121  TVMRetValue rv;
122  rv = value;
123  return ConvertConstant_(rv);
124  }
138  TVM_DLL static ExecBuilder Create();
139 
141 
142  static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
143  static constexpr const char* _type_key = "relax.ExecBuilder";
145 
146  private:
155  vm::Instruction::Arg ConvertConstant_(TVMRetValue obj);
156 
161  void CheckExecutable();
165  void Formalize();
166 
168  ObjectPtr<vm::Executable> exec_; // mutable
170  std::unordered_map<ObjectRef, vm::Index, StructuralHash, StructuralEqual> const_dedup_map_;
171 };
172 
173 class ExecBuilder : public ObjectRef {
174  public:
176 };
177 
178 } // namespace relax
179 } // namespace tvm
180 
181 #endif // TVM_RELAX_EXEC_BUILDER_H_
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
A builder provides api to build VM executable with instructions.
Definition: exec_builder.h:48
void EndFunction(const std::string &func)
Annotate the end of a vm function.
ObjectPtr< vm::Executable > Get()
Finalize the build, run formalize and get the final result.
TVM_DECLARE_FINAL_OBJECT_INFO(ExecBuilderNode, Object)
vm::Executable * exec() const
Raw access to underlying executable build in progress.
void EmitGoto(vm::Index pc_offset)
Emit a goto instruction.
void EmitCall(vm::Instruction::Arg func, std::vector< vm::Instruction::Arg > args, vm::RegName ret)
Emit a call instruction with func as argument.
static constexpr const char * _type_key
Definition: exec_builder.h:143
static ExecBuilder Create()
Create an ExecBuilder.
vm::Instruction::Arg GetFunction(const std::string &name)
Get function index by its name.
void EmitCall(const std::string &func, std::vector< vm::Instruction::Arg > args, vm::RegName ret)
Emit a call instruction for a packed function.
void EmitFunction(const std::string &func, int64_t num_inputs, Optional< Array< String >> param_names, vm::VMFuncInfo::FuncKind kind=vm::VMFuncInfo::FuncKind::kVMFunc, int64_t init_register_size=0)
To annotate the start of a vm function.
vm::Instruction::Arg ConvertConstant(T value)
Convert a constant value something that exec builder can understand.
Definition: exec_builder.h:120
void DeclareFunction(const std::string &func, vm::VMFuncInfo::FuncKind kind)
Declare a function, it is OK to have multiple declarations.
void EmitIf(vm::Instruction::Arg cond, vm::Index false_offset)
Emit an If instruction.
void VisitAttrs(AttrVisitor *v)
Definition: exec_builder.h:140
static constexpr const uint32_t _type_index
Definition: exec_builder.h:142
void EmitRet(vm::Instruction::Arg result)
Emit a ret instruction.
Definition: exec_builder.h:173
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ExecBuilder, ObjectRef, ExecBuilderNode)
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
A custom smart pointer for Object.
Definition: object.h:362
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:946
The executable emitted by the VM compiler.
Definition: executable.h:89
Base expr nodes in TVM.
Definition: builtin.h:29
ExecWord RegName
A register name.
Definition: bytecode.h:43
ExecWord Index
An alias for the integer type used ubiquitously in the VM.
Definition: bytecode.h:48
tvm::relax::Var Arg(const String &name, const tvm::relax::StructInfo &struct_info)
Add a parameter to the last function frame.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
A managed object in the TVM runtime.
Reflection and serialization of compiler IR/AST nodes.
This file defines the TVM global function registry.
The bytecode for the virtual machine.
Printer class to print repr string of each AST/IR nodes.
@ kDynamic
Type index is allocated during runtime.
Definition: object.h:84
FuncKind
kind of the function.
Definition: executable.h:55