|
| 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 | + |
| 20 | +/*! |
| 21 | + * \file tvm/relax/exec_builder.h |
| 22 | + */ |
| 23 | +#ifndef TVM_RELAX_EXEC_BUILDER_H_ |
| 24 | +#define TVM_RELAX_EXEC_BUILDER_H_ |
| 25 | + |
| 26 | +#include <tvm/ir/expr.h> |
| 27 | +#include <tvm/node/reflection.h> |
| 28 | +#include <tvm/node/repr_printer.h> |
| 29 | +#include <tvm/runtime/object.h> |
| 30 | +#include <tvm/runtime/registry.h> |
| 31 | +#include <tvm/runtime/relax_vm/bytecode.h> |
| 32 | +#include <tvm/runtime/relax_vm/executable.h> |
| 33 | + |
| 34 | +#include <string> |
| 35 | +#include <unordered_map> |
| 36 | +#include <vector> |
| 37 | + |
| 38 | +namespace tvm { |
| 39 | +namespace relax { |
| 40 | + |
| 41 | +namespace vm = tvm::runtime::relax_vm; |
| 42 | + |
| 43 | +class ExecBuilder; |
| 44 | + |
| 45 | +/*! |
| 46 | + * \brief A builder provides api to build VM executable with instructions. |
| 47 | + */ |
| 48 | +class ExecBuilderNode : public Object { |
| 49 | + public: |
| 50 | + /*! |
| 51 | + * \brief Declare a function, it is OK to have multiple declarations. |
| 52 | + * \param func The function name. |
| 53 | + * \param kind The kind of the function. |
| 54 | + */ |
| 55 | + void DeclareFunction(const std::string& func, vm::VMFuncInfo::FuncKind kind); |
| 56 | + /*! |
| 57 | + * \brief To annotate the start of a vm function. |
| 58 | + * \param func The function name. |
| 59 | + * \param num_inputs The number of inputs. |
| 60 | + * \param param_names The function parameter names. |
| 61 | + * \param kind The kind of the function. |
| 62 | + * \param init_register_size Initial setting of register file size. |
| 63 | + */ |
| 64 | + void EmitFunction(const std::string& func, int64_t num_inputs, |
| 65 | + Optional<Array<String>> param_names, |
| 66 | + vm::VMFuncInfo::FuncKind kind = vm::VMFuncInfo::FuncKind::kVMFunc, |
| 67 | + int64_t init_register_size = 0); |
| 68 | + /*! |
| 69 | + * \brief Annotate the end of a vm function. |
| 70 | + * \param func The function name. |
| 71 | + */ |
| 72 | + void EndFunction(const std::string& func); |
| 73 | + /*! |
| 74 | + * \brief Emit a call instruction for a packed function. |
| 75 | + * \param func The packed function name. |
| 76 | + * \param args The arguments of the function. |
| 77 | + * \param ret The return register. |
| 78 | + */ |
| 79 | + void EmitCall(const std::string& func, std::vector<vm::Instruction::Arg> args, vm::RegName ret); |
| 80 | + /*! |
| 81 | + * \brief Emit a call instruction with func as argument. |
| 82 | + * \param func The packed function index. |
| 83 | + * \param args The arguments of the function. |
| 84 | + * \param ret The return register. |
| 85 | + */ |
| 86 | + void EmitCall(vm::Instruction::Arg func, std::vector<vm::Instruction::Arg> args, vm::RegName ret); |
| 87 | + /*! |
| 88 | + * \brief Emit a ret instruction. |
| 89 | + * \param result The return result. |
| 90 | + * \note result must be a register. |
| 91 | + */ |
| 92 | + void EmitRet(vm::Instruction::Arg result); |
| 93 | + /*! |
| 94 | + * \brief Emit a goto instruction. |
| 95 | + * \param pc_offset The program counter offset as the jump offset. |
| 96 | + */ |
| 97 | + void EmitGoto(vm::Index pc_offset); |
| 98 | + /*! |
| 99 | + * \brief Emit an If instruction. |
| 100 | + * \param cond The register containing the cond value. |
| 101 | + * \param false_offset The program counter offset for the false branch. |
| 102 | + * \note result must be a register. |
| 103 | + */ |
| 104 | + void EmitIf(vm::Instruction::Arg cond, vm::Index false_offset); |
| 105 | + /*! |
| 106 | + * \brief Get function index by its name. |
| 107 | + * \param name The name of the function. |
| 108 | + * \return The argument corresponding to the function index. |
| 109 | + */ |
| 110 | + vm::Instruction::Arg GetFunction(const std::string& name); |
| 111 | + /*! |
| 112 | + * \brief Convert a constant value something that exec builder can understand. |
| 113 | + * |
| 114 | + * This function may update the constant pool to include the obj value. |
| 115 | + * |
| 116 | + * \param value The input constant value |
| 117 | + * \return An Arg that represents the result of constant argument. |
| 118 | + */ |
| 119 | + template <typename T> |
| 120 | + vm::Instruction::Arg ConvertConstant(T value) { |
| 121 | + TVMRetValue rv; |
| 122 | + rv = value; |
| 123 | + return ConvertConstant_(rv); |
| 124 | + } |
| 125 | + /*! |
| 126 | + * \brief Raw access to underlying executable build in progress. |
| 127 | + */ |
| 128 | + vm::Executable* exec() const; |
| 129 | + /*! |
| 130 | + * \brief Finalize the build, run formalize and get the final result. |
| 131 | + * \note This function should not be called during construction. |
| 132 | + */ |
| 133 | + ObjectPtr<vm::Executable> Get(); |
| 134 | + /*! |
| 135 | + * \brief Create an ExecBuilder. |
| 136 | + * \return The ExecBuilder. |
| 137 | + */ |
| 138 | + TVM_DLL static ExecBuilder Create(); |
| 139 | + |
| 140 | + void VisitAttrs(AttrVisitor* v) {} |
| 141 | + |
| 142 | + static constexpr const uint32_t _type_index = TypeIndex::kDynamic; |
| 143 | + static constexpr const char* _type_key = "relax.ExecBuilder"; |
| 144 | + TVM_DECLARE_FINAL_OBJECT_INFO(ExecBuilderNode, Object); |
| 145 | + |
| 146 | + private: |
| 147 | + /*! |
| 148 | + * \brief Convert a constant value something that exec builder can understand. |
| 149 | + * |
| 150 | + * This function may update the constant pool to include the obj value. |
| 151 | + * |
| 152 | + * \param obj The constant value to be emitted |
| 153 | + * \return An Arg that represents the result of constant argument. |
| 154 | + */ |
| 155 | + vm::Instruction::Arg ConvertConstant_(TVMRetValue obj); |
| 156 | + |
| 157 | + /*! |
| 158 | + * \brief A helper function to check if an executable is legal by checking if registers are used |
| 159 | + * properly |
| 160 | + */ |
| 161 | + void CheckExecutable(); |
| 162 | + /*! |
| 163 | + * \brief Formalize the executable. |
| 164 | + */ |
| 165 | + void Formalize(); |
| 166 | + |
| 167 | + /*! \brief The mutable internal executable. */ |
| 168 | + ObjectPtr<vm::Executable> exec_; // mutable |
| 169 | + /*! \brief internal dedup map when creating index for a new constant */ |
| 170 | + std::unordered_map<ObjectRef, vm::Index, StructuralHash, StructuralEqual> const_dedup_map_; |
| 171 | +}; |
| 172 | + |
| 173 | +class ExecBuilder : public ObjectRef { |
| 174 | + public: |
| 175 | + TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ExecBuilder, ObjectRef, ExecBuilderNode); |
| 176 | +}; |
| 177 | + |
| 178 | +} // namespace relax |
| 179 | +} // namespace tvm |
| 180 | + |
| 181 | +#endif // TVM_RELAX_EXEC_BUILDER_H_ |
0 commit comments