已合并
[feat] 用 HcclDlopen/HcclDlsym/HcclDlclose 弱符号封装 dlopen/dlsym/dlclose #2574
HXDW创建于 8月12日
[feat] 用 HcclDlopen/HcclDlsym/HcclDlclose 弱符号封装 dlopen/dlsym/dlclose #2574
已合并
共 11 个文件变更+100-34
| @@ -1 +1,2 @@ | |||
| 1 | *.png filter=lfs diff=lfs merge=lfs -text | 1 | *.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 of | 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"). | 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 | 11 | ||
| 12 | 12 | ||
| @@ -29,6 +29,7 @@ | |||
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | + | ||
| 32 | 33 | ||
| 33 | 34 | ||
| 34 | 35 | ||
| @@ -164,7 +165,7 @@ extern "C" { | |||
| 164 | 165 | ||
| 165 | 166 | ||
| 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 | ||
| 11 | if(STATIC_MODE) | 11 | if(STATIC_MODE) |
| 12 | target_sources(hccl PRIVATE | 12 | target_sources(hccl PRIVATE |
| 13 | + ${CMAKE_CURRENT_SOURCE_DIR}/hccl_dl.cc | ||
| 13 | ${CMAKE_CURRENT_SOURCE_DIR}/hccl_rank_graph_dl.cc | 14 | ${CMAKE_CURRENT_SOURCE_DIR}/hccl_rank_graph_dl.cc |
| 14 | ${CMAKE_CURRENT_SOURCE_DIR}/hccl_res_dl.cc | 15 | ${CMAKE_CURRENT_SOURCE_DIR}/hccl_res_dl.cc |
| 15 | ${CMAKE_CURRENT_SOURCE_DIR}/hcomm_primitives_dl.cc | 16 | ${CMAKE_CURRENT_SOURCE_DIR}/hcomm_primitives_dl.cc |
| @@ -89,6 +90,7 @@ else() | |||
| 89 | ) | 90 | ) |
| 90 | 91 | ||
| 91 | target_sources(hccl_compat PRIVATE | 92 | target_sources(hccl_compat PRIVATE |
| 93 | + hccl_dl.cc | ||
| 92 | hccl_rank_graph_dl.cc | 94 | hccl_rank_graph_dl.cc |
| 93 | hccl_res_dl.cc | 95 | hccl_res_dl.cc |
| 94 | hcomm_primitives_dl.cc | 96 | 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 | + | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + | ||
| 20 | +extern "C" { | ||
| 21 | + | ||
| 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 | + | ||
| 32 | +} // extern "C" | ||
| 33 | + | ||
| @@ -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 | + | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + | ||
| 16 | +extern "C" { | ||
| 17 | + | ||
| 18 | +void* HcclDlopen(const char* libName, int mode); | ||
| 19 | +void* HcclDlsym(void* handle, const char* funcName); | ||
| 20 | +int HcclDlclose(void* handle); | ||
| 21 | + | ||
| 22 | +} // extern "C" | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + | ||
| @@ -9,6 +9,7 @@ | |||
| 9 | # ---------------------------------------------------------------------------- | 9 | # ---------------------------------------------------------------------------- |
| 10 | 10 | ||
| 11 | add_library(hccl_kernel_compat SHARED | 11 | add_library(hccl_kernel_compat SHARED |
| 12 | + hccl_dl.cc | ||
| 12 | hcomm_device_dlsym.cc | 13 | hcomm_device_dlsym.cc |
| 13 | hcomm_primitives_dl.cc | 14 | hcomm_primitives_dl.cc |
| 14 | hcomm_diag_dl.cc | 15 | 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 of | 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"). | 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 | 11 | ||
| 12 | 12 | ||
| @@ -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 of | 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"). | 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 | 11 | ||
| 12 | 12 | ||
| @@ -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_HcommBatchTransferOnThread | 140 | 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 | 16 | ||
| 17 | 17 | ||
| 18 | + | ||
| 18 | 19 | ||
| 19 | 20 | ||
| 20 | using namespace std; | 21 | using namespace std; |
| @@ -30,7 +31,7 @@ namespace { | |||
| 30 | HcclResult BarrierFallbackToOldFlow(HcclComm comm, aclrtStream stream) | 31 | HcclResult 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 | 11 | ||
| 12 | + | ||
| 12 | 13 | ||
| 13 | 14 | ||
| 14 | namespace ops_hccl { | 15 | namespace 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 | ||
| 35 | HcclResult DlHcommFunction::DlHcommFunctionInterInit() | 36 | HcclResult DlHcommFunction::DlHcommFunctionInterInit() |
| 36 | { | 37 | { |
| 37 | dlHcclThreadResGetInfo | 38 | 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 | dlHcommThreadResGetInfo | 43 | 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 | // dlopen | 54 | // 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()); |