Convert MKL symbols from global to local (#122284)

PyTorch is statically linked to MKL but MKL symbols are visible to global, which may cause symbol conflicts.
Such error has been observed when a different version of MKL is dynamically linked to the other components: `libtorch_cpu.so` was invoked incorrectly when MKL descriptor was freed.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/122284
Approved by: https://github.com/EikanWang, https://github.com/cyyever, https://github.com/ezyang
This commit is contained in:
Cui, Yifeng 2024-04-12 15:36:47 +00:00 committed by PyTorch MergeBot
parent 616446cc0a
commit b024c0c2ef

View File

@ -21,3 +21,20 @@ endforeach()
set_property(
TARGET caffe2::mkl PROPERTY INTERFACE_LINK_DIRECTORIES
${MKL_ROOT}/lib ${MKL_ROOT}/lib/intel64 ${MKL_ROOT}/lib/intel64_win ${MKL_ROOT}/lib/win-x64)
if(UNIX)
if(USE_STATIC_MKL)
foreach(MKL_LIB_PATH IN LISTS MKL_LIBRARIES)
if(NOT EXISTS "${MKL_LIB_PATH}")
continue()
endif()
get_filename_component(MKL_LIB_NAME "${MKL_LIB_PATH}" NAME)
# Match archive libraries starting with "libmkl_"
if(MKL_LIB_NAME MATCHES "^libmkl_" AND MKL_LIB_NAME MATCHES ".a$")
target_link_options(caffe2::mkl INTERFACE "-Wl,--exclude-libs,${MKL_LIB_NAME}")
endif()
endforeach()
endif()
endif()