dso_module.cc 3.08 KB
Newer Older
1 2
/*!
 *  Copyright (c) 2017 by Contributors
3
 * \file dso_dll_module.cc
4 5 6 7 8
 * \brief Module to load from dynamic shared library.
 */
#include <tvm/runtime/module.h>
#include <tvm/runtime/registry.h>
#include <tvm/runtime/packed_func.h>
9
#include "./module_util.h"
10 11 12 13 14 15 16 17 18 19 20

#if defined(_WIN32)
#include <windows.h>
#else
#include <dlfcn.h>
#endif

namespace tvm {
namespace runtime {

// Module to load from dynamic shared libary.
21
// This is the default module TVM used for host-side AOT
22
class DSOModuleNode final : public ModuleNode {
23 24 25 26 27
 public:
  ~DSOModuleNode() {
    if (lib_handle_) Unload();
  }

28
  const char* type_key() const final {
29 30 31 32 33 34
    return "dso";
  }

  PackedFunc GetFunction(
      const std::string& name,
      const std::shared_ptr<ModuleNode>& sptr_to_self) final {
35 36 37 38 39 40 41 42 43 44
    BackendPackedCFunc faddr;
    if (name == runtime::symbol::tvm_module_main) {
      const char* entry_name = reinterpret_cast<const char*>(
          GetSymbol(runtime::symbol::tvm_module_main));
      CHECK(entry_name!= nullptr)
          << "Symbol " << runtime::symbol::tvm_module_main << " is not presented";
      faddr = reinterpret_cast<BackendPackedCFunc>(GetSymbol(entry_name));
    } else {
      faddr = reinterpret_cast<BackendPackedCFunc>(GetSymbol(name));
    }
45
    if (faddr == nullptr) return PackedFunc();
46
    return WrapPackedFunc(faddr, sptr_to_self);
47 48 49 50 51 52
  }

  void Init(const std::string& name) {
    Load(name);
    void** ctx_addr =
        reinterpret_cast<void**>(
53
            GetSymbol(runtime::symbol::tvm_module_ctx));
54 55 56
    if (ctx_addr != nullptr) {
      *ctx_addr = this;
    }
57 58 59
    // Load the imported modules
    const char* dev_mblob =
        reinterpret_cast<const char*>(
60
            GetSymbol(runtime::symbol::tvm_dev_mblob));
61
    if (dev_mblob != nullptr) {
62
      ImportModuleBlob(dev_mblob, &imports_);
63
    }
64 65 66 67 68 69 70 71 72
  }

 private:
  // Platform dependent handling.
#if defined(_WIN32)
  // library handle
  HMODULE lib_handle_{nullptr};
  // Load the library
  void Load(const std::string& name) {
73 74 75
    // use wstring version that is needed by LLVM.
    std::wstring wname(name.begin(), name.end());
    lib_handle_ = LoadLibraryW(wname.c_str());
76 77
    CHECK(lib_handle_ != nullptr)
        << "Failed to load dynamic shared library " << name;
78
  }
79
  void* GetSymbol(const std::string& name) {
80
    return reinterpret_cast<void*>(
81
        GetProcAddress(lib_handle_, (LPCSTR)name.c_str())); // NOLINT(*)
82 83 84 85 86 87 88 89 90 91
  }
  void Unload() {
    FreeLibrary(lib_handle_);
  }
#else
  // Library handle
  void* lib_handle_{nullptr};
  // load the library
  void Load(const std::string& name) {
    lib_handle_ = dlopen(name.c_str(), RTLD_LAZY | RTLD_LOCAL);
92 93
    CHECK(lib_handle_ != nullptr)
        << "Failed to load dynamic shared library " << name;
94
  }
95
  void* GetSymbol(const std::string& name) {
96 97 98 99 100 101 102 103
    return dlsym(lib_handle_, name.c_str());
  }
  void Unload() {
    dlclose(lib_handle_);
  }
#endif
};

104
TVM_REGISTER_GLOBAL("module.loadfile_so")
105 106 107 108 109 110 111
.set_body([](TVMArgs args, TVMRetValue* rv) {
    std::shared_ptr<DSOModuleNode> n = std::make_shared<DSOModuleNode>();
    n->Init(args[0]);
    *rv = runtime::Module(n);
  });
}  // namespace runtime
}  // namespace tvm