You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

vm.h 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * 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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef MINDSPORE_CCSRC_VM_VM_H_
  19. #define MINDSPORE_CCSRC_VM_VM_H_
  20. #include <map>
  21. #include <memory>
  22. #include <stack>
  23. #include <string>
  24. #include <tuple>
  25. #include <utility>
  26. #include <vector>
  27. #include <deque>
  28. #include <unordered_map>
  29. #include "ir/anf.h"
  30. #include "utils/base_ref.h"
  31. namespace mindspore {
  32. namespace compile {
  33. class Backend;
  34. using BackendPtr = std::shared_ptr<Backend>;
  35. enum Instruction {
  36. kCall = 0,
  37. kTailCall,
  38. kReturn,
  39. kPartial,
  40. kSwitch,
  41. kSwitchReturn,
  42. kTuple,
  43. kInput,
  44. kExternal,
  45. kPush,
  46. kPrim,
  47. kGraph,
  48. kPadStack
  49. };
  50. using InstType = std::pair<Instruction, VectorRef>;
  51. using InstSet = std::vector<InstType>;
  52. using InstFunctionMap = std::map<Instruction, std::function<void(const VectorRef &)>>;
  53. const std::vector<std::string> inst_str{"call", "tail_call", "return", "partial", "switch", "switch_return", "tuple",
  54. "input", "external", "push", "primitive", "graph", "pad_stack"};
  55. class StructPartial : public Base {
  56. public:
  57. // Initialize StructPartial.
  58. StructPartial(int fn, const VectorRef &args, const FuncGraphPtr &fg = nullptr);
  59. virtual ~StructPartial() = default;
  60. MS_DECLARE_PARENT(StructPartial, Base)
  61. int fn_;
  62. VectorRef args_;
  63. FuncGraphPtr fg_;
  64. };
  65. std::ostream &operator<<(std::ostream &os, const StructPartial &other);
  66. bool operator==(const StructPartial &lhs, const StructPartial &rhs);
  67. class StructSimuSwitch : public Base {
  68. public:
  69. StructSimuSwitch(const BaseRef &fn, const BaseRef &value);
  70. virtual ~StructSimuSwitch() = default;
  71. MS_DECLARE_PARENT(StructSimuSwitch, Base)
  72. BaseRef fn_;
  73. BaseRef value_;
  74. };
  75. std::ostream &operator<<(std::ostream &os, const StructSimuSwitch &other);
  76. bool operator==(const StructSimuSwitch &lhs, const StructSimuSwitch &rhs);
  77. class FinalVM {
  78. public:
  79. // Create a VM with the specified instructions and backend.
  80. explicit FinalVM(const InstSet &insts, const BackendPtr &backend);
  81. virtual ~FinalVM() = default;
  82. BaseRef Eval(const VectorRef &args);
  83. void InstCall(const VectorRef &args);
  84. void InstTailCall(const VectorRef &args);
  85. void InstReturn(const VectorRef &args);
  86. void InstPartial(const VectorRef &args);
  87. void InstSimuPartial(const VectorRef &args);
  88. void InstRealPartial(const VectorRef &args);
  89. void InstSwitch(const VectorRef &args);
  90. void InstSimuSwitch(const VectorRef &args);
  91. void InstRealSwitch(const VectorRef &args);
  92. void InstTuple(const VectorRef &args);
  93. void InstPush(const VectorRef &args);
  94. void InstInput(const VectorRef &args);
  95. void InstPadStack(const VectorRef &args);
  96. void InstExternal(const VectorRef &args);
  97. void InstPushPrim(const VectorRef &args);
  98. void InstSwitchReturn(const VectorRef &args);
  99. void set_insts(const InstSet &value) { insts_ = value; }
  100. protected:
  101. BaseRef Ref(int i);
  102. void Push(const BaseRef &v);
  103. void Pop(int n = 1);
  104. void MoveStack(int nitems, int height);
  105. void Pushp();
  106. void Popp();
  107. void Pushsp();
  108. void Popsp();
  109. void DoJmp(const BaseRef &jmp);
  110. void MergeJmpArgs(const BaseRef &jmp, const BaseRef &c);
  111. private:
  112. InstSet insts_;
  113. std::deque<BaseRef> insts_stack_;
  114. std::stack<int> retp_;
  115. std::stack<int> retsp_;
  116. int pc_;
  117. int sp_;
  118. std::unordered_map<BaseRef, BaseRef, BaseRefHash> cond_jmp_;
  119. BackendPtr backend_;
  120. const InstFunctionMap inst_function_map = {
  121. {Instruction::kCall, [this](const VectorRef &args) { InstCall(args); }},
  122. {Instruction::kTailCall, [this](const VectorRef &args) { InstTailCall(args); }},
  123. {Instruction::kReturn, [this](const VectorRef &args) { InstReturn(args); }},
  124. {Instruction::kPartial, [this](const VectorRef &args) { InstPartial(args); }},
  125. {Instruction::kSwitch, [this](const VectorRef &args) { InstSwitch(args); }},
  126. {Instruction::kTuple, [this](const VectorRef &args) { InstTuple(args); }},
  127. {Instruction::kPush, [this](const VectorRef &args) { InstPush(args); }},
  128. {Instruction::kInput, [this](const VectorRef &args) { InstInput(args); }},
  129. {Instruction::kPadStack, [this](const VectorRef &args) { InstPadStack(args); }},
  130. {Instruction::kExternal, [this](const VectorRef &args) { InstExternal(args); }},
  131. {Instruction::kPrim, [this](const VectorRef &args) { InstPushPrim(args); }},
  132. {Instruction::kSwitchReturn, [this](const VectorRef &args) { InstSwitchReturn(args); }},
  133. };
  134. };
  135. using FinalVMPtr = std::shared_ptr<FinalVM>;
  136. } // namespace compile
  137. } // namespace mindspore
  138. #endif // MINDSPORE_CCSRC_VM_VM_H_