tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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;
102  } alloc_tensor;
103  struct /* AllocTensorReg Operands */ {
105  RegName storage;
107  Index offset;
111  DLDataType dtype;
112  } alloc_tensor_reg;
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. */
160  };
161  struct /* LoadConsti Operands */ {
162  /* \brief The index into the constant pool. */
164  } load_consti;
165  struct /* Jump Operands */ {
168  };
169  struct /* Proj Operands */ {
174  };
175  struct /* GetTag Operands */ {
177  RegName object;
178  } get_tag;
179  struct /* AllocADT Operands */ {
180  // TODO(mbs): Needs a DeviceAndScope.
187  };
188  struct /* AllocClosure Operands */ {
189  // TODO(mbs): Needs a DeviceAndScope.
196  };
197  struct /* AllocStorage Operands */ {
203  DLDataType dtype_hint;
206  } alloc_storage;
207  struct /* ShapeOf Operands */ {
209  } shape_of;
210  struct /* ReshapeTensor Operands */ {
211  RegName tensor;
213  } reshape_tensor;
214  struct /* DeviceCopy Operands */ {
220  } device_copy;
221  };
222 
228  static Instruction Ret(RegName return_reg);
233  static Instruction Fatal();
242  static Instruction InvokePacked(Index packed_index, Index arity, Index output_size,
243  const std::vector<RegName>& args);
253  static Instruction AllocTensor(RegName storage, Index offset, const std::vector<int64_t>& shape,
254  DLDataType dtype, RegName dst);
264  static Instruction AllocTensorReg(RegName storage, Index offset, RegName shape_register,
265  DLDataType dtype, RegName dst);
274  static Instruction AllocADT(Index tag, Index num_fields, const std::vector<RegName>& fields,
275  RegName dst);
284  static Instruction AllocClosure(Index func_index, Index num_freevar,
285  const std::vector<RegName>& free_vars, RegName dst);
293  static Instruction GetField(RegName object_reg, Index field_index, RegName dst);
300  static Instruction GetTag(RegName object_reg, RegName dst);
309  static Instruction If(RegName test, RegName target, Index true_branch, Index false_branch);
315  static Instruction Goto(Index pc_offset);
323  static Instruction Invoke(Index func_index, const std::vector<RegName>& args, RegName dst);
331  static Instruction InvokeClosure(RegName closure, const std::vector<RegName>& args, RegName dst);
338  static Instruction LoadConst(Index const_index, RegName dst);
345  static Instruction LoadConsti(Index val, RegName dst);
352  static Instruction Move(RegName src, RegName dst);
362  static Instruction AllocStorage(RegName size, Index alignment, DLDataType dtype_hint,
363  Index device_index, RegName dst);
370  static Instruction ShapeOf(RegName tensor, RegName dst);
378  static Instruction ReshapeTensor(RegName tensor, RegName newshape, RegName dst);
387  static Instruction DeviceCopy(RegName src, Index src_device_index, Index dst_device_index,
388  RegName dst);
389 
390  static Instruction KillRegister(RegName dst);
391 
392  Instruction();
393  Instruction(const Instruction& instr);
394  Instruction& operator=(const Instruction& instr);
395  ~Instruction();
396 
397  friend std::ostream& operator<<(std::ostream& os, const Instruction&);
398 };
399 
400 } // namespace vm
401 } // namespace runtime
402 } // namespace tvm
403 
404 #endif // TVM_RUNTIME_VM_BYTECODE_H_
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:97
Index num_freevar
The number of free variables to capture.
Definition: bytecode.h:193
Index clo_index
The index into the function table.
Definition: bytecode.h:191
Index packed_index
The index into the packed function table.
Definition: bytecode.h:131
RegName result
The register to return.
Definition: bytecode.h:123
Index false_offset
The program counter offset for the false branch.
Definition: bytecode.h:147
int64_t * shape
The shape of tensor.
Definition: bytecode.h:99
Opcode op
The instruction opcode.
Definition: bytecode.h:85
Index pc_offset
The jump offset.
Definition: bytecode.h:167
Index src_device_index
The index of the source device to copy from.
Definition: bytecode.h:217
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Index output_size
The number of outputs produced by the packed function.
Definition: bytecode.h:135
RegName tensor
Definition: bytecode.h:208
Index val
Definition: bytecode.h:163
DLDataType dtype
The datatype of tensor to be allocated.
Definition: bytecode.h:101
A single virtual machine instruction.
Definition: bytecode.h:83
Index const_index
Definition: bytecode.h:159
RegName * closure_args
The closure arguments as an array.
Definition: bytecode.h:119
DLDataType dtype_hint
The hint of the dtype.
Definition: bytecode.h:203
RegName dst
The destination register.
Definition: bytecode.h:88
Index dst_device_index
The index of the destination deviceto copy to.
Definition: bytecode.h:219
Index func_index
The function to call.
Definition: bytecode.h:151
Index device_index
The index of the device on which the allocation will be made.
Definition: bytecode.h:205
RegName newshape
Definition: bytecode.h:212
Index offset
The offset into the storage to allocate from.
Definition: bytecode.h:95
RegName object
The register to project from.
Definition: bytecode.h:171
Index field_index
The field to read out.
Definition: bytecode.h:173
RegName src
Definition: bytecode.h:215
uint32_t ndim
The number of dimensions.
Definition: bytecode.h:97
int64_t RegName
A register name.
Definition: bytecode.h:38
RegName target
The register containing the target value.
Definition: bytecode.h:143
Index arity
The arity of the packed function.
Definition: bytecode.h:133
RegName closure
The register containing the closure.
Definition: bytecode.h:115
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1768
RegName storage
The storage to allocate from.
Definition: bytecode.h:93
int64_t Index
An alias for the integer type used ubiquitously in the VM.
Definition: bytecode.h:43
Index num_args
The number of arguments to the function.
Definition: bytecode.h:153
Index alignment
The alignment of the allocation.
Definition: bytecode.h:201
Opcode
An enumeration of Relay&#39;s opcodes.
Definition: bytecode.h:50
Index true_offset
The program counter offset for the true branch.
Definition: bytecode.h:145
RegName * datatype_fields
The fields as an array.
Definition: bytecode.h:186
RegName allocation_size
The size of the allocation.
Definition: bytecode.h:199
Index num_closure_args
The number of arguments to the closure.
Definition: bytecode.h:117
RegName * free_vars
The free variables as an array.
Definition: bytecode.h:195
RegName * invoke_args_registers
The registers containing the arguments.
Definition: bytecode.h:155
Index constructor_tag
The datatype&#39;s constructor tag.
Definition: bytecode.h:182
RegName shape_register
The register to read the shape out of.
Definition: bytecode.h:109
RegName from
The source register for a move operation.
Definition: bytecode.h:127
RegName test
The register containing the test value.
Definition: bytecode.h:141
Index num_fields
The number of fields to store in the datatype.
Definition: bytecode.h:184
RegName * packed_args
The arguments to pass to the packed function.
Definition: bytecode.h:137