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_RELAX_VM_BYTECODE_H_
25 #define TVM_RUNTIME_RELAX_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 relax_vm {
36 
40 using ExecWord = int64_t;
41 
43 using RegName = ExecWord;
44 
48 using Index = ExecWord;
49 
56 enum class Opcode {
57  Call = 1U,
58  Ret = 2U,
59  Goto = 3U,
60  If = 4U,
61 };
62 
72 struct Instruction {
74  static constexpr ExecWord kKindBit = 8;
76  static constexpr ExecWord kValueBit = sizeof(ExecWord) * 8 - kKindBit;
78  static constexpr ExecWord kValueMask = (static_cast<ExecWord>(1) << kValueBit) - 1;
80  static constexpr ExecWord kValueMaxLimit = (static_cast<ExecWord>(1) << (kValueBit - 1)) - 1;
82  static constexpr ExecWord kValueMinLimit = -kValueMaxLimit;
84  static constexpr RegName kBeginSpecialReg = static_cast<ExecWord>(1) << 54;
86  static constexpr RegName kVoidRegister = kBeginSpecialReg + 0;
88  static constexpr RegName kVMRegister = kBeginSpecialReg + 1;
92  enum class ArgKind : int { kRegister = 0, kImmediate = 1, kConstIdx = 2, kFuncIdx = 3 };
93 
94  friend std::ostream& operator<<(std::ostream& os, const ArgKind& kind) {
95  switch (kind) {
96  case ArgKind::kRegister:
97  os << "kRegister";
98  break;
100  os << "kImmediate";
101  break;
102  case ArgKind::kConstIdx:
103  os << "kConstIdx";
104  break;
105  case ArgKind::kFuncIdx:
106  os << "kFuncIdx";
107  break;
108  default:
109  LOG(FATAL) << "Internal error: "
110  << "Invalid ArgKind with integer value " << static_cast<int>(kind);
111  }
112  return os;
113  }
114 
118  class Arg {
119  public:
126  static Arg FromData(ExecWord data) { return Arg(data); }
132  static Arg Register(RegName reg) { return Arg(ArgKind::kRegister, reg); }
138  static Arg ConstIdx(Index index) { return Arg(ArgKind::kConstIdx, index); }
144  static Arg Immediate(int64_t imm_value) { return Arg(ArgKind::kImmediate, imm_value); }
150  static Arg FuncIdx(Index index) { return Arg(ArgKind::kFuncIdx, index); }
155  ArgKind kind() const {
156  uint8_t kind = (data_ >> kValueBit) & 0xFF;
157  return Instruction::ArgKind(kind);
158  }
164  ExecWord value() const { return ((data_ & kValueMask) << kKindBit) >> kKindBit; }
169  ExecWord data() const { return data_; }
170 
171  private:
173  explicit Arg(ExecWord data) : data_(data) {}
176  ICHECK_LE(value, kValueMaxLimit);
177  ICHECK_GE(value, kValueMinLimit);
178  data_ = (static_cast<ExecWord>(kind) << kValueBit) | (value & kValueMask);
179  }
181  ExecWord data_;
182  };
185  union {
186  struct /* Call */ {
195  };
196  struct /* Ret */ {
199  };
200  struct /* Goto */ {
203  };
204  struct /* If */ {
209  };
210  };
239 };
240 
241 } // namespace relax_vm
242 } // namespace runtime
243 } // namespace tvm
244 
245 #endif // TVM_RUNTIME_RELAX_VM_BYTECODE_H_
The auxiliary data structure for instruction argument.
Definition: bytecode.h:118
static Arg Register(RegName reg)
construct a register Arg.
Definition: bytecode.h:132
ArgKind kind() const
Get the kind of argument.
Definition: bytecode.h:155
static Arg FuncIdx(Index index)
construct a FuncIdx arg.
Definition: bytecode.h:150
ExecWord data() const
Get the raw data repr of the arg.
Definition: bytecode.h:169
static Arg FromData(ExecWord data)
construct Arg from data.
Definition: bytecode.h:126
static Arg Immediate(int64_t imm_value)
construct a immediate arg.
Definition: bytecode.h:144
ExecWord value() const
Get the value of argument.
Definition: bytecode.h:164
Arg()
Construct a void argument.
Definition: bytecode.h:121
static Arg ConstIdx(Index index)
construct a ConstIdx arg.
Definition: bytecode.h:138
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
Opcode
An enumeration of Relax's opcodes.
Definition: bytecode.h:56
int64_t ExecWord
The storage type for the bytecode in the VM.
Definition: bytecode.h:40
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A single virtual machine instruction.
Definition: bytecode.h:72
RegName cond
The register containing the cond value.
Definition: bytecode.h:206
Opcode op
The instruction opcode.
Definition: bytecode.h:184
static constexpr ExecWord kValueMinLimit
Minimum possible value, remove 1 slot to keep things symmetric.
Definition: bytecode.h:82
static constexpr RegName kVMRegister
Random magic number that represents the VM context.
Definition: bytecode.h:88
static Instruction Goto(RegName pc_offset)
Construct a goto instruction.
static constexpr ExecWord kValueBit
The number of bit for storing value.
Definition: bytecode.h:76
Index false_offset
The program counter offset for the false branch.
Definition: bytecode.h:208
Arg * args
The arguments of the packed function.
Definition: bytecode.h:194
RegName dst
The destination register.
Definition: bytecode.h:188
static constexpr ExecWord kValueMask
The bit mask of the value part.
Definition: bytecode.h:78
friend std::ostream & operator<<(std::ostream &os, const ArgKind &kind)
Definition: bytecode.h:94
static constexpr RegName kVoidRegister
Random magic number that represents void argument, indicate null value.
Definition: bytecode.h:86
static constexpr ExecWord kKindBit
The number of bit for storing value.
Definition: bytecode.h:74
static constexpr ExecWord kValueMaxLimit
Maximum possible value, use 1 bit for sign.
Definition: bytecode.h:80
static constexpr RegName kBeginSpecialReg
Beginning of special register section.
Definition: bytecode.h:84
static Instruction Call(Index func_idx, Index num_args, Arg *args, RegName dst)
Construct a Call instruction.
Index func_idx
The index into the packed function table.
Definition: bytecode.h:190
RegName result
The return result.
Definition: bytecode.h:198
ArgKind
The kind of instruction's argument.
Definition: bytecode.h:92
Index num_args
The number of arguments to the packed function.
Definition: bytecode.h:192
static Instruction Ret(RegName result)
Construct a return instruction.
Index pc_offset
The jump offset.
Definition: bytecode.h:202
static Instruction If(RegName cond, Index false_offset)
Construct an If instruction.