tvm
bytecode.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_RUNTIME_VM_BYTECODE_H_
25 #define TVM_RUNTIME_VM_BYTECODE_H_
26 
27 #include <tvm/runtime/data_type.h>
28 #include <tvm/runtime/logging.h>
29 
30 #include <iostream>
31 #include <vector>
32 
33 namespace tvm {
34 namespace runtime {
35 namespace vm {
36 
38 using RegName = int64_t;
39 
43 using Index = int64_t;
44 
50 enum class Opcode {
51  Move = 0U,
52  Ret = 1U,
53  Invoke = 2U,
54  InvokeClosure = 3U,
55  InvokePacked = 4U,
56  AllocTensor = 5U,
57  AllocTensorReg = 6U,
58  AllocADT = 7U,
59  AllocClosure = 8U,
60  GetField = 9U,
61  If = 10U,
62  LoadConst = 11U,
63  Goto = 12U,
64  GetTag = 13U,
65  LoadConsti = 14U,
66  Fatal = 15U,
67  AllocStorage = 16U,
68  ShapeOf = 17U,
69  ReshapeTensor = 18U,
70  DeviceCopy = 19U,
71  KillRegister = 20U,
72 };
73 
83 struct Instruction {
86 
89 
90  union {
91  struct /* AllocTensor Operands */ {
97  uint32_t ndim;
99  int64_t* shape;
101  DLDataType dtype;
103  struct /* AllocTensorReg Operands */ {
107  Index offset;
111  DLDataType dtype;
113  struct /* InvokeClosure Operands */ {
120  };
121  struct /* Return Operands */ {
124  };
125  struct /* Move Operands */ {
128  };
129  struct /* InvokePacked Operands */ {
138  };
139  struct /* If Operands */ {
148  } if_op;
149  struct /* Invoke Operands */ {
156  };
157  struct /* LoadConst Operands */ {
158  /* \brief The index into the constant pool. */
162  };
163  struct /* LoadConsti Operands */ {
164  /* \brief The index into the constant pool. */
167  struct /* Jump Operands */ {
170  };
171  struct /* Proj Operands */ {
176  };
177  struct /* GetTag Operands */ {
179  RegName object;
181  struct /* AllocADT Operands */ {
182  // TODO(mbs): Needs a DeviceAndScope.
189  };
190  struct /* AllocClosure Operands */ {
191  // TODO(mbs): Needs a DeviceAndScope.
198  };
199  struct /* AllocStorage Operands */ {
203  DLDataType dtype_hint;
205  uint32_t ndim;
206  union {
208  int64_t* shape;
211  };
215  struct /* ShapeOf Operands */ {
218  struct /* ReshapeTensor Operands */ {
219  RegName tensor;
222  struct /* DeviceCopy Operands */ {
229  };
230 
236  static Instruction Ret(RegName return_reg);
241  static Instruction Fatal();
251  const std::vector<RegName>& args);
261  static Instruction AllocTensor(RegName storage, Index offset, const std::vector<int64_t>& shape,
262  DLDataType dtype, RegName dst);
273  DLDataType dtype, RegName dst);
282  static Instruction AllocADT(Index tag, Index num_fields, const std::vector<RegName>& fields,
283  RegName dst);
293  const std::vector<RegName>& free_vars, RegName dst);
308  static Instruction GetTag(RegName object_reg, RegName dst);
317  static Instruction If(RegName test, RegName target, Index true_branch, Index false_branch);
331  static Instruction Invoke(Index func_index, const std::vector<RegName>& args, RegName dst);
339  static Instruction InvokeClosure(RegName closure, const std::vector<RegName>& args, RegName dst);
373  Index device_index, const std::vector<int64_t>& shape,
374  RegName dst);
399  RegName dst);
400 
402 
404  Instruction(const Instruction& instr);
407 
408  friend std::ostream& operator<<(std::ostream& os, const Instruction&);
409 };
410 
411 } // namespace vm
412 } // namespace runtime
413 } // namespace tvm
414 
415 #endif // TVM_RUNTIME_VM_BYTECODE_H_
Definition: builtin.h:29
int64_t Index
An alias for the integer type used ubiquitously in the VM.
Definition: bytecode.h:43
int64_t RegName
A register name.
Definition: bytecode.h:38
Opcode
An enumeration of Relay's opcodes.
Definition: bytecode.h:50
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A single virtual machine instruction.
Definition: bytecode.h:83
Index offset
The offset into the storage to allocate from.
Definition: bytecode.h:95
RegName shape_register
The register to read the shape out of.
Definition: bytecode.h:109
RegName object
The register to project from.
Definition: bytecode.h:173
DLDataType dtype_hint
The hint of the dtype.
Definition: bytecode.h:203
static Instruction Fatal()
Construct a fatal instruction.
struct tvm::runtime::vm::Instruction::@11::@23 if_op
int64_t * shape
The shape of tensor.
Definition: bytecode.h:99
static Instruction Move(RegName src, RegName dst)
Construct a move instruction.
uint32_t ndim
The number of dimensions.
Definition: bytecode.h:97
RegName dst
The destination register.
Definition: bytecode.h:88
struct tvm::runtime::vm::Instruction::@11::@33 get_tag
Index false_offset
The program counter offset for the false branch.
Definition: bytecode.h:147
Instruction(const Instruction &instr)
RegName tensor
Definition: bytecode.h:216
RegName * datatype_fields
The fields as an array.
Definition: bytecode.h:188
static Instruction Ret(RegName return_reg)
Construct a return instruction.
struct tvm::runtime::vm::Instruction::@11::@14 alloc_tensor_reg
Index dst_device_index
The index of the destination deviceto copy to.
Definition: bytecode.h:227
RegName storage
The storage to allocate from.
Definition: bytecode.h:93
Index arity
The arity of the packed function.
Definition: bytecode.h:133
RegName closure
The register containing the closure.
Definition: bytecode.h:115
static Instruction Goto(Index pc_offset)
Construct a goto instruction.
Index num_fields
The number of fields to store in the datatype.
Definition: bytecode.h:186
RegName target
The register containing the target value.
Definition: bytecode.h:143
RegName allocation_size
The size of the allocation.
Definition: bytecode.h:210
RegName from
The source register for a move operation.
Definition: bytecode.h:127
static Instruction GetField(RegName object_reg, Index field_index, RegName dst)
Construct a get field instruction.
RegName newshape
Definition: bytecode.h:220
Index true_offset
The program counter offset for the true branch.
Definition: bytecode.h:145
RegName * packed_args
The arguments to pass to the packed function.
Definition: bytecode.h:137
Index num_args
The number of arguments to the function.
Definition: bytecode.h:153
static Instruction ShapeOf(RegName tensor, RegName dst)
Get the shape of an input tensor.
Index field_index
The field to read out.
Definition: bytecode.h:175
Index src_device_index
The index of the source device to copy from.
Definition: bytecode.h:225
Instruction & operator=(const Instruction &instr)
Index output_size
The number of outputs produced by the packed function.
Definition: bytecode.h:135
RegName * invoke_args_registers
The registers containing the arguments.
Definition: bytecode.h:155
RegName src
Definition: bytecode.h:223
RegName * free_vars
The free variables as an array.
Definition: bytecode.h:197
Index pc_offset
The jump offset.
Definition: bytecode.h:169
Index constructor_tag
The datatype's constructor tag.
Definition: bytecode.h:184
static Instruction ReshapeTensor(RegName tensor, RegName newshape, RegName dst)
Reshape the tensor given the new shape.
friend std::ostream & operator<<(std::ostream &os, const Instruction &)
static Instruction LoadConsti(Index val, RegName dst)
Construct a load_constanti instruction.
static Instruction InvokePacked(Index packed_index, Index arity, Index output_size, const std::vector< RegName > &args)
Construct a invoke packed instruction.
static Instruction KillRegister(RegName dst)
static Instruction GetTag(RegName object_reg, RegName dst)
Construct a get_tag instruction.
Index alignment
The alignment of the allocation.
Definition: bytecode.h:201
DLDataType dtype
The datatype of tensor to be allocated.
Definition: bytecode.h:101
Index const_index
Definition: bytecode.h:159
static Instruction AllocStorage(RegName size, Index alignment, DLDataType dtype_hint, Index device_index, const std::vector< int64_t > &shape, RegName dst)
Allocate a storage block.
struct tvm::runtime::vm::Instruction::@11::@38 alloc_storage
Opcode op
The instruction opcode.
Definition: bytecode.h:85
static Instruction AllocADT(Index tag, Index num_fields, const std::vector< RegName > &fields, RegName dst)
Construct an allocate datatype instruction.
Index func_index
The function to call.
Definition: bytecode.h:151
static Instruction LoadConst(Index const_index, Index device_index, RegName dst)
Construct a load constant instruction.
Index clo_index
The index into the function table.
Definition: bytecode.h:193
struct tvm::runtime::vm::Instruction::@11::@28 load_consti
struct tvm::runtime::vm::Instruction::@11::@41 device_copy
Index val
Definition: bytecode.h:165
Index packed_index
The index into the packed function table.
Definition: bytecode.h:131
static Instruction Invoke(Index func_index, const std::vector< RegName > &args, RegName dst)
Construct an invoke instruction.
struct tvm::runtime::vm::Instruction::@11::@40 reshape_tensor
static Instruction DeviceCopy(RegName src, Index src_device_index, Index dst_device_index, RegName dst)
Copy tensor cross different devices.
Index num_closure_args
The number of arguments to the closure.
Definition: bytecode.h:117
Index num_freevar
The number of free variables to capture.
Definition: bytecode.h:195
RegName test
The register containing the test value.
Definition: bytecode.h:141
RegName result
The register to return.
Definition: bytecode.h:123
struct tvm::runtime::vm::Instruction::@11::@39 shape_of
static Instruction If(RegName test, RegName target, Index true_branch, Index false_branch)
Construct an if instruction.
RegName * closure_args
The closure arguments as an array.
Definition: bytecode.h:119
static Instruction AllocTensor(RegName storage, Index offset, const std::vector< int64_t > &shape, DLDataType dtype, RegName dst)
Construct an allocate tensor instruction with constant shape.
static Instruction AllocTensorReg(RegName storage, Index offset, RegName shape_register, DLDataType dtype, RegName dst)
Construct an allocate tensor instruction with register.
static Instruction AllocClosure(Index func_index, Index num_freevar, const std::vector< RegName > &free_vars, RegName dst)
Construct an allocate closure instruction.
Index device_index
The index of the device on which the load will be made.
Definition: bytecode.h:161
struct tvm::runtime::vm::Instruction::@11::@13 alloc_tensor
static Instruction InvokeClosure(RegName closure, const std::vector< RegName > &args, RegName dst)
Construct an invoke closure instruction.