-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
dso_library.cc
157 lines (135 loc) · 4.33 KB
/
dso_library.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
/*!
* \file dso_libary.cc
* \brief Create library module to load from dynamic shared library.
*/
#include <tvm/runtime/memory.h>
#include <tvm/runtime/module.h>
#include <tvm/runtime/packed_func.h>
#include <tvm/runtime/registry.h>
#include "library_module.h"
#if defined(_WIN32)
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#if defined(__hexagon__)
extern "C" {
#include <HAP_farf.h>
}
#endif
namespace tvm {
namespace runtime {
/*!
* \brief Dynamic shared library object used to load
* and retrieve symbols by name. This is the default
* module TVM uses for host-side AOT compilation.
*/
class DSOLibrary final : public Library {
public:
~DSOLibrary();
/*!
* \brief Initialize by loading and storing
* a handle to the underlying shared library.
* \param name The string name/path to the
* shared library over which to initialize.
*/
void Init(const std::string& name);
/*!
* \brief Returns the symbol address within
* the shared library for a given symbol name.
* \param name The name of the symbol.
* \return The symbol.
*/
void* GetSymbol(const char* name) final;
private:
/*! \brief Private implementation of symbol lookup.
* Implementation is operating system dependent.
* \param The name of the symbol.
* \return The symbol.
*/
void* GetSymbol_(const char* name);
/*! \brief Implementation of shared library load.
* Implementation is operating system dependent.
* \param The name/path of the shared library.
*/
void Load(const std::string& name);
/*! \brief Implementation of shared library unload.
* Implementation is operating system dependent.
*/
void Unload();
#if defined(_WIN32)
//! \brief Windows library handle
HMODULE lib_handle_{nullptr};
#else
// \brief Linux library handle
void* lib_handle_{nullptr};
#endif
};
DSOLibrary::~DSOLibrary() {
if (lib_handle_) Unload();
}
void DSOLibrary::Init(const std::string& name) { Load(name); }
void* DSOLibrary::GetSymbol(const char* name) { return GetSymbol_(name); }
#if defined(_WIN32)
void* DSOLibrary::GetSymbol_(const char* name) {
return reinterpret_cast<void*>(GetProcAddress(lib_handle_, (LPCSTR)name)); // NOLINT(*)
}
void DSOLibrary::Load(const std::string& name) {
// use wstring version that is needed by LLVM.
std::wstring wname(name.begin(), name.end());
lib_handle_ = LoadLibraryW(wname.c_str());
ICHECK(lib_handle_ != nullptr) << "Failed to load dynamic shared library " << name;
}
void DSOLibrary::Unload() {
FreeLibrary(lib_handle_);
lib_handle_ = nullptr;
}
#else
void DSOLibrary::Load(const std::string& name) {
lib_handle_ = dlopen(name.c_str(), RTLD_LAZY | RTLD_LOCAL);
ICHECK(lib_handle_ != nullptr) << "Failed to load dynamic shared library " << name << " "
<< dlerror();
#if defined(__hexagon__)
int p;
int rc = dlinfo(lib_handle_, RTLD_DI_LOAD_ADDR, &p);
if (rc)
FARF(ERROR, "error getting model .so start address : %u", rc);
else
FARF(ALWAYS, "Model .so Start Address : %x", p);
#endif
}
void* DSOLibrary::GetSymbol_(const char* name) { return dlsym(lib_handle_, name); }
void DSOLibrary::Unload() {
dlclose(lib_handle_);
lib_handle_ = nullptr;
}
#endif
ObjectPtr<Library> CreateDSOLibraryObject(std::string library_path) {
auto n = make_object<DSOLibrary>();
n->Init(library_path);
return n;
}
TVM_REGISTER_GLOBAL("runtime.module.loadfile_so").set_body([](TVMArgs args, TVMRetValue* rv) {
ObjectPtr<Library> n = CreateDSOLibraryObject(args[0]);
*rv = CreateModuleFromLibrary(n);
});
} // namespace runtime
} // namespace tvm