tvm
Loading...
Searching...
No Matches
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/ffi/dtype.h>
28#include <tvm/ffi/error.h>
29
30#include <iostream>
31#include <vector>
32
33namespace tvm {
34namespace runtime {
35namespace vm {
36
41
44
49
56enum class Opcode {
57 Call = 1U,
58 Ret = 2U,
59 Goto = 3U,
60 If = 4U,
61};
62
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;
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) {
97 os << "kRegister";
98 break;
100 os << "kImmediate";
101 break;
103 os << "kConstIdx";
104 break;
106 os << "kFuncIdx";
107 break;
108 default:
110 << "Internal error: "
111 << "Invalid ArgKind with integer value " << static_cast<int>(kind);
112 }
113 return os;
114 }
115
119 class Arg {
120 public:
127 static Arg FromData(ExecWord data) { return Arg(data); }
139 static Arg ConstIdx(Index index) { return Arg(ArgKind::kConstIdx, index); }
151 static Arg FuncIdx(Index index) { return Arg(ArgKind::kFuncIdx, index); }
156 ArgKind kind() const {
157 uint8_t kind = (data_ >> kValueBit) & 0xFF;
159 }
165 ExecWord value() const { return ((data_ & kValueMask) << kKindBit) >> kKindBit; }
170 ExecWord data() const { return data_; }
171
172 private:
174 explicit Arg(ExecWord data) : data_(data) {}
179 data_ = (static_cast<ExecWord>(kind) << kValueBit) | (value & kValueMask);
180 }
182 ExecWord data_;
183 };
186 union {
187 struct /* Call */ {
196 };
197 struct /* Ret */ {
200 };
201 struct /* Goto */ {
204 };
205 struct /* If */ {
210 };
211 };
240};
241
242} // namespace vm
243} // namespace runtime
244} // namespace tvm
245
246#endif // TVM_RUNTIME_VM_BYTECODE_H_
Managed reference to CallNode.
Definition expr.h:474
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
The auxiliary data structure for instruction argument.
Definition bytecode.h:119
static Arg ConstIdx(Index index)
construct a ConstIdx arg.
Definition bytecode.h:139
static Arg FuncIdx(Index index)
construct a FuncIdx arg.
Definition bytecode.h:151
static Arg Immediate(int64_t imm_value)
construct a immediate arg.
Definition bytecode.h:145
ArgKind kind() const
Get the kind of argument.
Definition bytecode.h:156
ExecWord value() const
Get the value of argument.
Definition bytecode.h:165
ExecWord data() const
Get the raw data repr of the arg.
Definition bytecode.h:170
Arg()
Construct a void argument.
Definition bytecode.h:122
static Arg Register(RegName reg)
construct a register Arg.
Definition bytecode.h:133
static Arg FromData(ExecWord data)
construct Arg from data.
Definition bytecode.h:127
Definition builtin.h:29
int64_t ExecWord
The storage type for the bytecode in the VM.
Definition bytecode.h:40
Opcode
An enumeration of Relax's opcodes.
Definition bytecode.h:56
ExecWord RegName
A register name.
Definition bytecode.h:43
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
A single virtual machine instruction.
Definition bytecode.h:72
static constexpr RegName kVMRegister
Random magic number that represents the VM context.
Definition bytecode.h:88
RegName cond
The register containing the cond value.
Definition bytecode.h:207
static constexpr ExecWord kValueMask
The bit mask of the value part.
Definition bytecode.h:78
static constexpr ExecWord kValueBit
The number of bit for storing value.
Definition bytecode.h:76
RegName dst
The destination register.
Definition bytecode.h:189
static constexpr RegName kVoidRegister
Random magic number that represents void argument, indicate null value.
Definition bytecode.h:86
Index false_offset
The program counter offset for the false branch.
Definition bytecode.h:209
static Instruction If(RegName cond, Index false_offset)
Construct an If instruction.
static constexpr ExecWord kValueMinLimit
Minimum possible value, remove 1 slot to keep things symmetric.
Definition bytecode.h:82
Index num_args
The number of arguments to the packed function.
Definition bytecode.h:193
Index func_idx
The index into the packed function table.
Definition bytecode.h:191
static Instruction Call(Index func_idx, Index num_args, Arg *args, RegName dst)
Construct a Call instruction.
Index pc_offset
The jump offset.
Definition bytecode.h:203
static constexpr RegName kBeginSpecialReg
Beginning of special register section.
Definition bytecode.h:84
static constexpr ExecWord kKindBit
The number of bit for storing value.
Definition bytecode.h:74
friend std::ostream & operator<<(std::ostream &os, const ArgKind &kind)
Definition bytecode.h:94
Opcode op
The instruction opcode.
Definition bytecode.h:185
ArgKind
The kind of instruction's argument.
Definition bytecode.h:92
static Instruction Ret(RegName result)
Construct a return instruction.
RegName result
The return result.
Definition bytecode.h:199
Arg * args
The arguments of the packed function.
Definition bytecode.h:195
static constexpr ExecWord kValueMaxLimit
Maximum possible value, use 1 bit for sign.
Definition bytecode.h:80
static Instruction Goto(RegName pc_offset)
Construct a goto instruction.