Skip to content

Commit ed0e4c9

Browse files
committed
use weak symbols instead of dynamically loading libcdsprpc.so
1 parent a671273 commit ed0e4c9

File tree

1 file changed

+13
-45
lines changed

1 file changed

+13
-45
lines changed

ggml/src/ggml-hexagon/ggml-hexagon.cpp

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <chrono>
1010
#include <mutex>
1111
#include <string>
12-
#include <dlfcn.h>
1312

1413
#ifdef _WIN32
1514
# include <sal.h>
@@ -52,15 +51,6 @@ static int opt_experimental = 0;
5251
static int opt_opmask = HTP_OPMASK_QUEUE | HTP_OPMASK_QUANTIZE | HTP_OPMASK_COMPUTE;
5352
static int opt_opsync = 0; // synchronous ops
5453

55-
// function signature defined in rpcmem.h
56-
using pfn_rpc_mem_alloc = void *(*)(int, uint32_t, size_t);
57-
58-
// Dynamic pointers to functions in lib[cdsp]rpc.so, used for backward compatibility with older SoCs
59-
// library open happens in the constructor of ggml_hexagon_registry, and closed in the destructor
60-
61-
// dynamic pointer to function rpcmem_alloc or rpcmem_alloc2 at runtime
62-
static pfn_rpc_mem_alloc _pfn_rpc_mem_alloc = nullptr;
63-
6454
#define HEX_VERBOSE(...) \
6555
if (opt_verbose) GGML_LOG_DEBUG(__VA_ARGS__)
6656

@@ -338,6 +328,8 @@ struct ggml_backend_hexagon_buffer_type_context {
338328
std::string name;
339329
};
340330

331+
#pragma weak rpcmem_alloc2
332+
341333
struct ggml_backend_hexagon_buffer_context {
342334
bool mmap_to(ggml_hexagon_session * s) {
343335
HEX_VERBOSE("ggml-hex: %s mmaping buffer: base %p domain-id %d session-id %d size %zu fd %d repack %d\n",
@@ -377,9 +369,13 @@ struct ggml_backend_hexagon_buffer_context {
377369
ggml_backend_hexagon_buffer_context(ggml_hexagon_session * sess, size_t size, bool repack) {
378370
size += 4 * 1024; // extra page for padding
379371

380-
GGML_ASSERT(_pfn_rpc_mem_alloc != nullptr);
372+
if (rpcmem_alloc2) {
373+
this->base = (uint8_t *) rpcmem_alloc2(RPCMEM_HEAP_ID_SYSTEM, RPCMEM_DEFAULT_FLAGS | RPCMEM_HEAP_NOREG, size);
374+
} else {
375+
GGML_LOG_INFO("ggml-hex: %s rpcmem_alloc2 not found, falling back to rpcmem_alloc\n", sess->name.c_str());
376+
this->base = (uint8_t *) rpcmem_alloc(RPCMEM_HEAP_ID_SYSTEM, RPCMEM_DEFAULT_FLAGS | RPCMEM_HEAP_NOREG, size);
377+
}
381378

382-
this->base = (uint8_t *) _pfn_rpc_mem_alloc(RPCMEM_HEAP_ID_SYSTEM, RPCMEM_DEFAULT_FLAGS | RPCMEM_HEAP_NOREG, size);
383379
if (!this->base) {
384380
GGML_LOG_ERROR("ggml-hex: %s failed to allocate buffer : size %zu\n", sess->name.c_str(), size);
385381
throw std::runtime_error("ggml-hex: rpcmem_alloc failed (see log for details)");
@@ -1691,12 +1687,13 @@ void ggml_hexagon_session::allocate(int dev_id) noexcept(false) {
16911687
}
16921688

16931689
// Get session URI
1694-
char htp_uri[256];
1695-
sprintf(htp_uri, "file:///libggml-htp-v%u.so?htp_iface_skel_handle_invoke&_modver=1.0", opt_arch);
16961690

16971691
char session_uri[256];
16981692
{
1699-
struct remote_rpc_get_uri u;
1693+
char htp_uri[256];
1694+
snprintf(htp_uri, sizeof(htp_uri), "file:///libggml-htp-v%u.so?htp_iface_skel_handle_invoke&_modver=1.0", opt_arch);
1695+
1696+
struct remote_rpc_get_uri u = {};
17001697
u.session_id = this->session_id;
17011698
u.domain_name = const_cast<char *>(CDSP_DOMAIN_NAME);
17021699
u.domain_name_len = strlen(CDSP_DOMAIN_NAME);
@@ -3671,9 +3668,6 @@ struct ggml_hexagon_registry {
36713668
~ggml_hexagon_registry();
36723669

36733670
ggml_backend_device devices[GGML_HEXAGON_MAX_SESSIONS];
3674-
3675-
// dynamic handles
3676-
void * _rpc_lib_handle = nullptr;
36773671
};
36783672

36793673
ggml_hexagon_registry::ggml_hexagon_registry(ggml_backend_reg_t reg) {
@@ -3700,30 +3694,6 @@ ggml_hexagon_registry::ggml_hexagon_registry(ggml_backend_reg_t reg) {
37003694
devices[i].context = nullptr;
37013695
}
37023696
}
3703-
3704-
// obtain handle to dsp library
3705-
_rpc_lib_handle = dlopen("libcdsprpc.so", RTLD_NOW | RTLD_LOCAL);
3706-
if (nullptr == _rpc_lib_handle) {
3707-
GGML_LOG_ERROR("ggml-hex: failed to load rpc lib: %s", dlerror());
3708-
throw std::runtime_error("failed to load rpc lib");
3709-
}
3710-
3711-
// attempt to load rpc_memalloc2 first
3712-
_pfn_rpc_mem_alloc = reinterpret_cast<pfn_rpc_mem_alloc>(dlsym(_rpc_lib_handle,"rpcmem_alloc2"));
3713-
3714-
// fallback to rpc_memalloc
3715-
if(_pfn_rpc_mem_alloc == nullptr) {
3716-
_pfn_rpc_mem_alloc = reinterpret_cast<pfn_rpc_mem_alloc>(dlsym(_rpc_lib_handle,"rpcmem_alloc"));
3717-
3718-
if(_pfn_rpc_mem_alloc == nullptr) {
3719-
GGML_LOG_ERROR("ggml-hex: failed to get rpcmem_alloc function address: %s", dlerror());
3720-
throw std::runtime_error("failed to get rpcmem_alloc function address");
3721-
} else {
3722-
GGML_LOG_INFO("ggml-hex: using rpcmem_alloc\n");
3723-
}
3724-
} else {
3725-
GGML_LOG_INFO("ggml-hex: using rpcmem_alloc2\n");
3726-
}
37273697
}
37283698

37293699
ggml_hexagon_registry::~ggml_hexagon_registry() {
@@ -3734,8 +3704,6 @@ ggml_hexagon_registry::~ggml_hexagon_registry() {
37343704
auto sess = static_cast<ggml_hexagon_session *>(devices[i].context);
37353705
delete sess;
37363706
}
3737-
3738-
if(_rpc_lib_handle) dlclose(_rpc_lib_handle);
37393707
}
37403708

37413709
static const char * ggml_backend_hexagon_reg_get_name(ggml_backend_reg_t reg) {
@@ -3846,4 +3814,4 @@ ggml_backend_reg_t ggml_backend_hexagon_reg(void) {
38463814
return &reg;
38473815
}
38483816

3849-
GGML_BACKEND_DL_IMPL(ggml_backend_hexagon_reg)
3817+
GGML_BACKEND_DL_IMPL(ggml_backend_hexagon_reg)

0 commit comments

Comments
 (0)