已合并
[feat] 用 HcclDlopen/HcclDlsym/HcclDlclose 弱符号封装 dlopen/dlsym/dlclose #2574
HXDW创建于 8月12日
[feat] 用 HcclDlopen/HcclDlsym/HcclDlclose 弱符号封装 dlopen/dlsym/dlclose #2574
已合并
HXDW创建于 8月12日
11 个文件变更+100-34
@@ -1 +1,2 @@
1*.png filter=lfs diff=lfs merge=lfs -text1*.png filter=lfs diff=lfs merge=lfs -text
2+*.sh text eol=lf
@@ -1,12 +1,12 @@
1/**1/**
2- * Copyright (c) 2026 Huawei Technologies Co., Ltd.2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3- * This program is free software, you can redistribute it and/or modify it under the terms and conditions of3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4- * CANN Open Software License Agreement Version 2.0 (the "License").4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5- * Please refer to the License for details. You may not use this file except in compliance with the License.5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7- * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8- * See LICENSE in the root of the software repository for the full text of the License.8+ * See LICENSE in the root of the software repository for the full text of the License.
9- */9+ */
10 10 
11#ifndef DLSYM_COMMON_H11#ifndef DLSYM_COMMON_H
12#define DLSYM_COMMON_H12#define DLSYM_COMMON_H
@@ -29,6 +29,7 @@
29#include "hccl/hccl_comm.h"29#include "hccl/hccl_comm.h"
30 30 
31#include "hccl_res.h"31#include "hccl_res.h"
32+#include "hccl_dl.h"
32 33 
33#if CANN_VERSION_NUM >= CANN_VERSION(9, 0, 0)34#if CANN_VERSION_NUM >= CANN_VERSION(9, 0, 0)
34#include "hcomm_res_defs.h"35#include "hcomm_res_defs.h"
@@ -164,7 +165,7 @@ extern "C" {
164 165 
165#define INIT_SUPPORT_FLAG(handle, func_name) \166#define INIT_SUPPORT_FLAG(handle, func_name) \
166 do { \167 do { \
167- void* ptr = (void*)dlsym(handle, #func_name); \168+ void* ptr = (void*)HcclDlsym(handle, #func_name); \
168 if (ptr == nullptr) { \169 if (ptr == nullptr) { \
169 g_##func_name##Supported = false; \170 g_##func_name##Supported = false; \
170 HCCL_COMPAT_DEBUG("[HcclWrapper] %s not supported", #func_name); \171 HCCL_COMPAT_DEBUG("[HcclWrapper] %s not supported", #func_name); \
@@ -10,6 +10,7 @@
10 10 
11if(STATIC_MODE)11if(STATIC_MODE)
12 target_sources(hccl PRIVATE12 target_sources(hccl PRIVATE
13+ ${CMAKE_CURRENT_SOURCE_DIR}/hccl_dl.cc
13 ${CMAKE_CURRENT_SOURCE_DIR}/hccl_rank_graph_dl.cc14 ${CMAKE_CURRENT_SOURCE_DIR}/hccl_rank_graph_dl.cc
14 ${CMAKE_CURRENT_SOURCE_DIR}/hccl_res_dl.cc15 ${CMAKE_CURRENT_SOURCE_DIR}/hccl_res_dl.cc
15 ${CMAKE_CURRENT_SOURCE_DIR}/hcomm_primitives_dl.cc16 ${CMAKE_CURRENT_SOURCE_DIR}/hcomm_primitives_dl.cc
@@ -89,6 +90,7 @@ else()
89 )90 )
90 91 
91 target_sources(hccl_compat PRIVATE92 target_sources(hccl_compat PRIVATE
93+ hccl_dl.cc
92 hccl_rank_graph_dl.cc94 hccl_rank_graph_dl.cc
93 hccl_res_dl.cc95 hccl_res_dl.cc
94 hcomm_primitives_dl.cc96 hcomm_primitives_dl.cc
@@ -0,0 +1,33 @@
1+/**
2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+ * See LICENSE in the root of the software repository for the full text of the License.
9+ */
10+ 
11+#include "hccl_dl.h"
12+#include <dlfcn.h>
13+ 
14+#if !defined(weak_alias)
15+#define _weak_alias(name, aliasname) extern __typeof(name) aliasname __attribute__((weak, alias(#name)))
16+#define weak_alias(name, aliasname) _weak_alias(name, aliasname)
17+#endif
18+ 
19+#ifdef __cplusplus
20+extern "C" {
21+#endif
22+int __HcclDlclose(void* handle) { return dlclose(handle); }
23+ 
24+void* __HcclDlsym(void* handle, const char* funcName) { return dlsym(handle, funcName); }
25+ 
26+void* __HcclDlopen(const char* libName, int mode) { return dlopen(libName, mode); }
27+weak_alias(__HcclDlopen, HcclDlopen);
28+weak_alias(__HcclDlclose, HcclDlclose);
29+weak_alias(__HcclDlsym, HcclDlsym);
30+ 
31+#ifdef __cplusplus
32+} // extern "C"
33+#endif
@@ -0,0 +1,25 @@
1+/**
2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+ * See LICENSE in the root of the software repository for the full text of the License.
9+ */
10+ 
11+#ifndef HCCL_DL_H
12+#define HCCL_DL_H
13+#include <dlfcn.h>
14+ 
15+#ifdef __cplusplus
16+extern "C" {
17+#endif
18+void* HcclDlopen(const char* libName, int mode);
19+void* HcclDlsym(void* handle, const char* funcName);
20+int HcclDlclose(void* handle);
21+#ifdef __cplusplus
22+} // extern "C"
23+#endif
24+ 
25+#endif
@@ -9,6 +9,7 @@
9# ----------------------------------------------------------------------------9# ----------------------------------------------------------------------------
10 10 
11add_library(hccl_kernel_compat SHARED11add_library(hccl_kernel_compat SHARED
12+ hccl_dl.cc
12 hcomm_device_dlsym.cc13 hcomm_device_dlsym.cc
13 hcomm_primitives_dl.cc14 hcomm_primitives_dl.cc
14 hcomm_diag_dl.cc15 hcomm_diag_dl.cc
@@ -1,12 +1,12 @@
1/**1/**
2- * Copyright (c) 2026 Huawei Technologies Co., Ltd.2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3- * This program is free software, you can redistribute it and/or modify it under the terms and conditions of3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4- * CANN Open Software License Agreement Version 2.0 (the "License").4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5- * Please refer to the License for details. You may not use this file except in compliance with the License.5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7- * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8- * See LICENSE in the root of the software repository for the full text of the License.8+ * See LICENSE in the root of the software repository for the full text of the License.
9- */9+ */
10 10 
11#include "hcomm_device_dlsym.h"11#include "hcomm_device_dlsym.h"
12#include "hccl_res_dl.h"12#include "hccl_res_dl.h"
@@ -28,7 +28,7 @@ void HcommDeviceDlInit(void)
28 if (gLibHandle != nullptr)28 if (gLibHandle != nullptr)
29 return;29 return;
30 30 
31- gLibHandle = dlopen("libccl_kernel.so", RTLD_NOW);31+ gLibHandle = HcclDlopen("libccl_kernel.so", RTLD_NOW);
32 if (!gLibHandle) {32 if (!gLibHandle) {
33 fprintf(stderr, "[HcclWrapper] Failed to open libccl_kernel.so: %s\n", dlerror());33 fprintf(stderr, "[HcclWrapper] Failed to open libccl_kernel.so: %s\n", dlerror());
34 return;34 return;
@@ -1,12 +1,12 @@
1/**1/**
2- * Copyright (c) 2026 Huawei Technologies Co., Ltd.2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3- * This program is free software, you can redistribute it and/or modify it under the terms and conditions of3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4- * CANN Open Software License Agreement Version 2.0 (the "License").4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5- * Please refer to the License for details. You may not use this file except in compliance with the License.5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7- * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8- * See LICENSE in the root of the software repository for the full text of the License.8+ * See LICENSE in the root of the software repository for the full text of the License.
9- */9+ */
10 10 
11#include "hcomm_dlsym.h"11#include "hcomm_dlsym.h"
12#include "hccl_res_dl.h"12#include "hccl_res_dl.h"
@@ -65,7 +65,7 @@ void HcommDlInit(void)
65 if (gLibHandle != nullptr)65 if (gLibHandle != nullptr)
66 return;66 return;
67 67 
68- gLibHandle = dlopen("libhcomm.so", RTLD_NOW);68+ gLibHandle = HcclDlopen("libhcomm.so", RTLD_NOW);
69 if (!gLibHandle) {69 if (!gLibHandle) {
70 fprintf(stderr, "[HcclWrapper] Failed to open libhcomm: %s\n", dlerror());70 fprintf(stderr, "[HcclWrapper] Failed to open libhcomm: %s\n", dlerror());
71 return;71 return;
@@ -138,7 +138,7 @@ void HcommPrimitivesDlInit(void* libHcommHandle)
138 INIT_SUPPORT_FLAG(libHcommHandle, HcommAicpuTsTaskCacheExecute);138 INIT_SUPPORT_FLAG(libHcommHandle, HcommAicpuTsTaskCacheExecute);
139 INIT_SUPPORT_FLAG(libHcommHandle, HcommAicpuTsTaskCacheClear);139 INIT_SUPPORT_FLAG(libHcommHandle, HcommAicpuTsTaskCacheClear);
140 g_HcommBatchTransferOnThread140 g_HcommBatchTransferOnThread
141- = reinterpret_cast<HcclHcommBatchTransferOnThreadFunc>(dlsym(libHcommHandle, "HcommBatchTransferOnThread"));141+ = reinterpret_cast<HcclHcommBatchTransferOnThreadFunc>(HcclDlsym(libHcommHandle, "HcommBatchTransferOnThread"));
142 if (g_HcommBatchTransferOnThread == nullptr) {142 if (g_HcommBatchTransferOnThread == nullptr) {
143 g_HcommBatchTransferOnThreadSupported = false;143 g_HcommBatchTransferOnThreadSupported = false;
144 HCCL_COMPAT_DEBUG("[HcclWrapper] %s not supported", "HcommBatchTransferOnThread");144 HCCL_COMPAT_DEBUG("[HcclWrapper] %s not supported", "HcommBatchTransferOnThread");
@@ -15,6 +15,7 @@
15 15 
16#include "barrier_op.h"16#include "barrier_op.h"
17#include "op_common_ops.h"17#include "op_common_ops.h"
18+#include "hccl_dl.h"
18#include <string>19#include <string>
19 20 
20using namespace std;21using namespace std;
@@ -30,7 +31,7 @@ namespace {
30HcclResult BarrierFallbackToOldFlow(HcclComm comm, aclrtStream stream)31HcclResult BarrierFallbackToOldFlow(HcclComm comm, aclrtStream stream)
31{32{
32 using BarrierFn = HcclResult (*)(HcclComm, aclrtStream);33 using BarrierFn = HcclResult (*)(HcclComm, aclrtStream);
33- static BarrierFn oldBarrier = reinterpret_cast<BarrierFn>(dlsym(RTLD_NEXT, "HcclBarrier"));34+ static BarrierFn oldBarrier = reinterpret_cast<BarrierFn>(HcclDlsym(RTLD_NEXT, "HcclBarrier"));
34 if (oldBarrier != nullptr && oldBarrier != &HcclBarrier) { // 防自递归35 if (oldBarrier != nullptr && oldBarrier != &HcclBarrier) { // 防自递归
35 return oldBarrier(comm, stream);36 return oldBarrier(comm, stream);
36 }37 }
@@ -9,6 +9,7 @@
9 */9 */
10 10 
11#include "dlhcomm_function.h"11#include "dlhcomm_function.h"
12+#include "hccl_dl.h"
12#include "log.h"13#include "log.h"
13 14 
14namespace ops_hccl {15namespace ops_hccl {
@@ -28,18 +29,19 @@ DlHcommFunction::~DlHcommFunction()
28 handle_ = nullptr;29 handle_ = nullptr;
29 }30 }
30 if (h != nullptr) {31 if (h != nullptr) {
31- (void)dlclose(h);32+ (void)HcclDlclose(h);
32 }33 }
33}34}
34 35 
35HcclResult DlHcommFunction::DlHcommFunctionInterInit()36HcclResult DlHcommFunction::DlHcommFunctionInterInit()
36{37{
37 dlHcclThreadResGetInfo38 dlHcclThreadResGetInfo
38- = (HcclResult(*)(HcclComm, ThreadHandle, void*, uint32_t, void**))dlsym(handle_, "HcclThreadResGetInfo");39+ = (HcclResult(*)(HcclComm, ThreadHandle, void*, uint32_t, void**))HcclDlsym(handle_, "HcclThreadResGetInfo");
39- dlHcclConfigGetInfo = (HcclResult(*)(HcclComm, HcclConfigType, uint32_t, void*))dlsym(handle_, "HcclConfigGetInfo");40+ dlHcclConfigGetInfo
41+ = (HcclResult(*)(HcclComm, HcclConfigType, uint32_t, void*))HcclDlsym(handle_, "HcclConfigGetInfo");
40 42 
41 dlHcommThreadResGetInfo43 dlHcommThreadResGetInfo
42- = (HcclResult(*)(ThreadHandle, void*, uint32_t, void**))dlsym(handle_, "HcommThreadResGetInfo");44+ = (HcclResult(*)(ThreadHandle, void*, uint32_t, void**))HcclDlsym(handle_, "HcommThreadResGetInfo");
43 return HCCL_SUCCESS;45 return HCCL_SUCCESS;
44}46}
45 47 
@@ -50,7 +52,7 @@ HcclResult DlHcommFunction::DlHcommFunctionInit()
50 return HCCL_SUCCESS;52 return HCCL_SUCCESS;
51 }53 }
52 // dlopen54 // dlopen
53- void* h = dlopen("libhcomm.so", RTLD_NOW);55+ void* h = HcclDlopen("libhcomm.so", RTLD_NOW);
54 CHK_PRT_RET(h == nullptr, HCCL_WARNING("dlopen libhcomm.so failed, error: %s", dlerror()), HCCL_E_PTR);56 CHK_PRT_RET(h == nullptr, HCCL_WARNING("dlopen libhcomm.so failed, error: %s", dlerror()), HCCL_E_PTR);
55 handle_ = h;57 handle_ = h;
56 CHK_RET(DlHcommFunctionInterInit());58 CHK_RET(DlHcommFunctionInterInit());