1111#include " micro_session.h"
1212#include " low_level_device.h"
1313#include " micro_common.h"
14+ #include " ../pack_args.h"
1415
1516namespace tvm {
1617namespace runtime {
@@ -32,14 +33,24 @@ class MicroModuleNode final : public ModuleNode {
3233 * \brief initializes module by establishing device connection and loads binary
3334 * \param binary name of the binary to be loaded
3435 */
35- void InitMicroModule (const std::string binary);
36+ void InitMicroModule (const std::string binary) {
37+ // TODO: if first MicroModule, then load init section in MicroSession
38+ session_ = MicroSession::Global ();
39+ // TODO: ensure low_level_device_ is initialized in MicroSession
40+ lldevice_ = session_->low_level_device ();
41+ binary_ = binary;
42+ LoadBinary ();
43+ }
3644
3745 /* !
3846 * \brief runs selected function on the micro device
3947 * \param func name of the function to be run
48+ * \param func_addr address of the function to be run
4049 * \param args type-erased arguments passed to the function
4150 */
42- void RunFunction (std::string func, TVMArgs args);
51+ void RunFunction (std::string func, void * func_addr, TVMArgs args) {
52+ session_->PushToExecQueue (func_addr, args);
53+ }
4354
4455 private:
4556 /* ! \brief loaded module text start address */
@@ -49,7 +60,7 @@ class MicroModuleNode final : public ModuleNode {
4960 /* ! \brief loaded module bss start address */
5061 void * bss_start_;
5162 /* ! \brief size of module text section */
52- size_t code_size_ ;
63+ size_t text_size_ ;
5364 /* ! \brief size of module data section */
5465 size_t data_size_;
5566 /* ! \brief size of module bss section */
@@ -61,7 +72,27 @@ class MicroModuleNode final : public ModuleNode {
6172 /* ! \brief low-level device pointer */
6273 std::shared_ptr<LowLevelDevice> lldevice_;
6374 /* ! \brief symbol map to addresses */
64- std::unordered_map<std::string, void *> symbol_map;
75+ std::unordered_map<std::string, void *> symbol_map_;
76+
77+ void LoadBinary () {
78+ text_size_ = GetSectionSize (binary_, kText );
79+ data_size_ = GetSectionSize (binary_, kData );
80+ bss_size_ = GetSectionSize (binary_, kBss );
81+ text_start_ = session_->AllocateInSection (kText , text_size_);
82+ data_start_ = session_->AllocateInSection (kData , data_size_);
83+ bss_start_ = session_->AllocateInSection (kBss , bss_size_);
84+ CHECK (text_start_ != nullptr && data_start_ != nullptr && bss_start_ != nullptr )
85+ << " Not enough space to load module on device" ;
86+ std::string relocated_bin = RelocateBinarySections (binary_, text_start_,
87+ data_start_, bss_start_);
88+ std::string text_contents = ReadSection (relocated_bin, kText );
89+ std::string data_contents = ReadSection (relocated_bin, kData );
90+ std::string bss_contents = ReadSection (relocated_bin, kBss );
91+ lldevice_->Write (text_start_, &text_contents[0 ], text_size_);
92+ lldevice_->Write (data_start_, &data_contents[0 ], data_size_);
93+ lldevice_->Write (bss_start_, &bss_contents[0 ], bss_size_);
94+ symbol_map_ = GetSymbolMap (relocated_bin);
95+ }
6596};
6697
6798class MicroWrappedFunc {
@@ -74,7 +105,9 @@ class MicroWrappedFunc {
74105 func_addr_ = func_addr;
75106 }
76107
77- void operator ()(TVMArgs args, TVMRetValue* rv) const {
108+ void operator ()(TVMArgs args, TVMRetValue* rv, void ** void_args) const {
109+ // no return value yet, but may implement in the future
110+ m_->RunFunction (func_name_, func_addr_, args);
78111 }
79112
80113 private:
@@ -86,10 +119,20 @@ class MicroWrappedFunc {
86119 void * func_addr_;
87120};
88121
89- // TODO: register module load function
122+ PackedFunc MicroModuleNode::GetFunction (
123+ const std::string& name,
124+ const std::shared_ptr<ModuleNode>& sptr_to_self) {
125+ void * func_addr = GetSymbol (symbol_map_, name, lldevice_->base_addr ());
126+ MicroWrappedFunc f (this , name, func_addr);
127+ return PackFuncVoidAddr (f, std::vector<TVMType>());
128+ }
129+
90130// register loadfile function to load module from Python frontend
91131TVM_REGISTER_GLOBAL (" module.loadfile_micro_dev" )
92132.set_body([](TVMArgs args, TVMRetValue* rv) {
133+ std::shared_ptr<MicroModuleNode> n = std::make_shared<MicroModuleNode>();
134+ n->InitMicroModule (args[0 ]);
135+ *rv = runtime::Module (n);
93136 });
94137} // namespace runtime
95138} // namespace tvm
0 commit comments