tvm
Loading...
Searching...
No Matches
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/ffi/extra/dataclass.h>
27#include <tvm/ffi/extra/structural_equal.h>
28#include <tvm/ffi/extra/structural_hash.h>
29#include <tvm/ffi/function.h>
30#include <tvm/ffi/reflection/registry.h>
31#include <tvm/ir/expr.h>
34
35#include <string>
36#include <unordered_map>
37#include <vector>
38
39namespace tvm {
40namespace relax {
41
42namespace vm = tvm::runtime::vm;
43
44class ExecBuilder;
45
49class ExecBuilderObj : public ffi::Object {
50 public:
56 void DeclareFunction(const std::string& func, vm::VMFuncInfo::FuncKind kind);
65 void EmitFunction(const std::string& func, int64_t num_inputs,
66 ffi::Optional<ffi::Array<ffi::String>> param_names,
67 vm::VMFuncInfo::FuncKind kind = vm::VMFuncInfo::FuncKind::kVMFunc,
68 int64_t init_register_size = 0);
73 void EndFunction(const std::string& func);
80 void EmitCall(const std::string& func, std::vector<vm::Instruction::Arg> args, vm::RegName ret);
87 void EmitCall(vm::Instruction::Arg func, std::vector<vm::Instruction::Arg> args, vm::RegName ret);
93 void EmitRet(vm::Instruction::Arg result);
98 void EmitGoto(vm::Index pc_offset);
105 void EmitIf(vm::Instruction::Arg cond, vm::Index false_offset);
111 vm::Instruction::Arg GetFunction(const std::string& name);
120 template <typename T>
121 vm::Instruction::Arg ConvertConstant(T value) {
122 ffi::Any rv;
123 rv = value;
124 return ConvertConstant_(rv);
125 }
134 void SaveMemoryScope(vm::Instruction::Arg idx, ffi::String scope);
143 ffi::ObjectPtr<vm::VMExecutable> Get();
148 TVM_DLL static ExecBuilder Create();
149
150 static void RegisterReflection() {
151 namespace refl = tvm::ffi::reflection;
152 refl::ObjectDef<ExecBuilderObj>();
153 }
154
155 static constexpr const bool _type_mutable = true;
156 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.ExecBuilder", ExecBuilderObj, ffi::Object);
157
158 private:
167 vm::Instruction::Arg ConvertConstant_(ffi::Any obj);
168
173 void CheckExecutable();
177 void Formalize();
178
180 ffi::ObjectPtr<vm::VMExecutable> exec_; // mutable
182 std::unordered_map<ffi::Any, vm::Index, ffi::StructuralHash, ffi::StructuralEqual>
183 const_dedup_map_;
184};
185
186class ExecBuilder : public ffi::ObjectRef {
187 public:
188 explicit ExecBuilder(ffi::UnsafeInit tag, ffi::ObjectPtr<ExecBuilderObj> n)
189 : ffi::ObjectRef(tag) {
190 data_ = std::move(n);
191 }
193};
194
195} // namespace relax
196} // namespace tvm
197
198#endif // TVM_RELAX_EXEC_BUILDER_H_
The bytecode for the virtual machine.
A builder provides api to build VM executable with instructions.
Definition exec_builder.h:49
static void RegisterReflection()
Definition exec_builder.h:150
void EndFunction(const std::string &func)
Annotate the end of a vm function.
vm::Instruction::Arg ConvertConstant(T value)
Convert a constant value something that exec builder can understand.
Definition exec_builder.h:121
vm::Instruction::Arg GetFunction(const std::string &name)
Get function index by its name.
void SaveMemoryScope(vm::Instruction::Arg idx, ffi::String scope)
update memory scopes.
void EmitGoto(vm::Index pc_offset)
Emit a goto instruction.
void DeclareFunction(const std::string &func, vm::VMFuncInfo::FuncKind kind)
Declare a function, it is OK to have multiple declarations.
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 bool _type_mutable
Definition exec_builder.h:155
vm::VMExecutable * exec() const
Raw access to underlying executable build in progress.
static ExecBuilder Create()
Create an ExecBuilder.
void EmitFunction(const std::string &func, int64_t num_inputs, ffi::Optional< ffi::Array< ffi::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.
void EmitRet(vm::Instruction::Arg result)
Emit a ret instruction.
void EmitIf(vm::Instruction::Arg cond, vm::Index false_offset)
Emit an If instruction.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.ExecBuilder", ExecBuilderObj, ffi::Object)
ffi::ObjectPtr< vm::VMExecutable > Get()
Finalize the build, run formalize and get the final result.
void EmitCall(const std::string &func, std::vector< vm::Instruction::Arg > args, vm::RegName ret)
Emit a call instruction for a packed function.
Definition exec_builder.h:186
ExecBuilder(ffi::UnsafeInit tag, ffi::ObjectPtr< ExecBuilderObj > n)
Definition exec_builder.h:188
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ExecBuilder, ffi::ObjectRef, ExecBuilderObj)
The virtual machine executable emitted by the VM compiler.
Definition executable.h:87
Base expr nodes in TVM.
Definition builtin.h:29
ExecWord Index
An alias for the integer type used ubiquitously in the VM.
Definition bytecode.h:48
ExecWord RegName
A register name.
Definition bytecode.h:43
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23