dso_library.cc 2.81 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

20
/*!
21 22
 * \file dso_libary.cc
 * \brief Create library module to load from dynamic shared library.
23 24
 */
#include <tvm/runtime/module.h>
25
#include <tvm/runtime/memory.h>
26 27
#include <tvm/runtime/registry.h>
#include <tvm/runtime/packed_func.h>
28
#include "library_module.h"
29 30 31 32 33 34 35 36 37 38

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

namespace tvm {
namespace runtime {

39
// Dynamic shared libary.
40
// This is the default module TVM used for host-side AOT
41
class DSOLibrary final : public Library {
42
 public:
43
  ~DSOLibrary() {
44 45 46 47
    if (lib_handle_) Unload();
  }
  void Init(const std::string& name) {
    Load(name);
48 49 50 51
  }

  void* GetSymbol(const char* name) final {
    return GetSymbol_(name);
52 53 54 55 56 57 58
  }

 private:
  // Platform dependent handling.
#if defined(_WIN32)
  // library handle
  HMODULE lib_handle_{nullptr};
59 60 61 62 63 64

  void* GetSymbol_(const char* name) {
    return reinterpret_cast<void*>(
        GetProcAddress(lib_handle_, (LPCSTR)name)); // NOLINT(*)
  }

65 66
  // Load the library
  void Load(const std::string& name) {
67 68 69
    // use wstring version that is needed by LLVM.
    std::wstring wname(name.begin(), name.end());
    lib_handle_ = LoadLibraryW(wname.c_str());
70 71
    CHECK(lib_handle_ != nullptr)
        << "Failed to load dynamic shared library " << name;
72
  }
73

74 75
  void Unload() {
    FreeLibrary(lib_handle_);
76
    lib_handle_ = nullptr;
77 78 79 80 81 82 83
  }
#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);
84
    CHECK(lib_handle_ != nullptr)
Tianqi Chen committed
85 86
        << "Failed to load dynamic shared library " << name
        << " " << dlerror();
87
  }
88 89

  void* GetSymbol_(const char* name) {
90
    return dlsym(lib_handle_, name);
91
  }
92

93 94
  void Unload() {
    dlclose(lib_handle_);
95
    lib_handle_ = nullptr;
96 97 98 99
  }
#endif
};

100
TVM_REGISTER_GLOBAL("module.loadfile_so")
101
.set_body([](TVMArgs args, TVMRetValue* rv) {
102
    auto n = make_object<DSOLibrary>();
103
    n->Init(args[0]);
104
    *rv = CreateModuleFromLibrary(n);
105 106 107
  });
}  // namespace runtime
}  // namespace tvm