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 };
72 
82 struct Instruction {
85 
88 
89  union {
90  struct /* AllocTensor Operands */ {
96  uint32_t ndim;
98  int64_t* shape;
100  DLDataType dtype;
101  } alloc_tensor;
102  struct /* AllocTensorReg Operands */ {
104  RegName storage;
106  Index offset;
110  DLDataType dtype;
111  } alloc_tensor_reg;
112  struct /* InvokeClosure Operands */ {
119  };
120  struct /* Return Operands */ {
123  };
124  struct /* Move Operands */ {
127  };
128  struct /* InvokePacked Operands */ {
137  };
138  struct /* If Operands */ {
147  } if_op;
148  struct /* Invoke Operands */ {
155  };
156  struct /* LoadConst Operands */ {
157  /* \brief The index into the constant pool. */
159  };
160  struct /* LoadConsti Operands */ {
161  /* \brief The index into the constant pool. */
163  } load_consti;
164  struct /* Jump Operands */ {
167  };
168  struct /* Proj Operands */ {
173  };
174  struct /* GetTag Operands */ {
176  RegName object;
177  } get_tag;
178  struct /* AllocADT Operands */ {
185  };
186  struct /* AllocClosure Operands */ {
193  };
194  struct /* AllocStorage Operands */ {
200  DLDataType dtype_hint;
203  } alloc_storage;
204  struct /* ShapeOf Operands */ {
206  } shape_of;
207  struct /* ReshapeTensor Operands */ {
208  RegName tensor;
210  } reshape_tensor;
211  struct /* DeviceCopy Operands */ {
217  };
218  };
219 
225  static Instruction Ret(RegName return_reg);
230  static Instruction Fatal();
239  static Instruction InvokePacked(Index packed_index, Index arity, Index output_size,
240  const std::vector<RegName>& args);
250  static Instruction AllocTensor(RegName storage, Index offset, const std::vector<int64_t>& shape,
251  DLDataType dtype, RegName dst);
261  static Instruction AllocTensorReg(RegName storage, Index offset, RegName shape_register,
262  DLDataType dtype, RegName dst);
271  static Instruction AllocADT(Index tag, Index num_fields, const std::vector<RegName>& fields,
272  RegName dst);
281  static Instruction AllocClosure(Index func_index, Index num_freevar,
282  const std::vector<RegName>& free_vars, RegName dst);
290  static Instruction GetField(RegName object_reg, Index field_index, RegName dst);
297  static Instruction GetTag(RegName object_reg, RegName dst);
306  static Instruction If(RegName test, RegName target, Index true_branch, Index false_branch);
312  static Instruction Goto(Index pc_offset);
320  static Instruction Invoke(Index func_index, const std::vector<RegName>& args, RegName dst);
328  static Instruction InvokeClosure(RegName closure, const std::vector<RegName>& args, RegName dst);
335  static Instruction LoadConst(Index const_index, RegName dst);
342  static Instruction LoadConsti(Index val, RegName dst);
349  static Instruction Move(RegName src, RegName dst);
359  static Instruction AllocStorage(RegName size, Index alignment, DLDataType dtype_hint,
360  Index device_type, RegName dst);
367  static Instruction ShapeOf(RegName tensor, RegName dst);
375  static Instruction ReshapeTensor(RegName tensor, RegName newshape, RegName dst);
384  static Instruction DeviceCopy(RegName src, Index src_device_type, Index dst_device_type,
385  RegName dst);
386 
387  Instruction();
388  Instruction(const Instruction& instr);
389  Instruction& operator=(const Instruction& instr);
390  ~Instruction();
391 
392  friend std::ostream& operator<<(std::ostream& os, const Instruction&);
393 };
394 
395 } // namespace vm
396 } // namespace runtime
397 } // namespace tvm
398 
399 #endif // TVM_RUNTIME_VM_BYTECODE_H_
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:69
Index num_freevar
The number of free variables to capture.
Definition: bytecode.h:190
Index clo_index
The index into the function table.
Definition: bytecode.h:188
Index packed_index
The index into the packed function table.
Definition: bytecode.h:130
constexpr const char * device_type
The device type.
Definition: stmt.h:1261
RegName result
The register to return.
Definition: bytecode.h:122
Index false_offset
The program counter offset for the false branch.
Definition: bytecode.h:146
int64_t * shape
The shape of tensor.
Definition: bytecode.h:98
Opcode op
The instruction opcode.
Definition: bytecode.h:84
Index pc_offset
The jump offset.
Definition: bytecode.h:166
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
Index output_size
The number of outputs produced by the packed function.
Definition: bytecode.h:134
RegName tensor
Definition: bytecode.h:205
Index val
Definition: bytecode.h:162
DLDataType dtype
The datatype of tensor to be allocated.
Definition: bytecode.h:100
A single virtual machine instruction.
Definition: bytecode.h:82
Index const_index
Definition: bytecode.h:158
RegName * closure_args
The closure arguments as an array.
Definition: bytecode.h:118
DLDataType dtype_hint
The hint of the dtype.
Definition: bytecode.h:200
RegName dst
The destination register.
Definition: bytecode.h:87
Index func_index
The function to call.
Definition: bytecode.h:150
RegName newshape
Definition: bytecode.h:209
Index offset
The offset into the storage to allocate from.
Definition: bytecode.h:94
RegName object
The register to project from.
Definition: bytecode.h:170
Index field_index
The field to read out.
Definition: bytecode.h:172
RegName src
Definition: bytecode.h:212
uint32_t ndim
The number of dimensions.
Definition: bytecode.h:96
int64_t RegName
A register name.
Definition: bytecode.h:38
RegName target
The register containing the target value.
Definition: bytecode.h:142
Index arity
The arity of the packed function.
Definition: bytecode.h:132
RegName closure
The register containing the closure.
Definition: bytecode.h:114
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:1608
RegName storage
The storage to allocate from.
Definition: bytecode.h:92
int64_t Index
An alias for the integer type used ubiquitously in the VM.
Definition: bytecode.h:43
Index device_type
The device type of the allocation.
Definition: bytecode.h:202
Index num_args
The number of arguments to the function.
Definition: bytecode.h:152
Index alignment
The alignment of the allocation.
Definition: bytecode.h:198
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:144
RegName * datatype_fields
The fields as an array.
Definition: bytecode.h:184
RegName allocation_size
The size of the allocation.
Definition: bytecode.h:196
Index num_closure_args
The number of arguments to the closure.
Definition: bytecode.h:116
RegName * free_vars
The free variables as an array.
Definition: bytecode.h:192
RegName * invoke_args_registers
The registers containing the arguments.
Definition: bytecode.h:154
Index src_device_type
The source device type.
Definition: bytecode.h:214
Index constructor_tag
The datatype&#39;s constructor tag.
Definition: bytecode.h:180
RegName shape_register
The register to read the shape out of.
Definition: bytecode.h:108
Index dst_device_type
The destination device type.
Definition: bytecode.h:216
RegName from
The source register for a move operation.
Definition: bytecode.h:126
RegName test
The register containing the test value.
Definition: bytecode.h:140
Index num_fields
The number of fields to store in the datatype.
Definition: bytecode.h:182
RegName * packed_args
The arguments to pass to the packed function.
Definition: bytecode.h:136