From 0c3764ea3771f68a573948d2cef0251b4810546d Mon Sep 17 00:00:00 2001 From: Neil Dhar Date: Thu, 23 Apr 2026 14:02:43 -0700 Subject: [PATCH] Minimize exported symbols of libtriton When extensions are not enabled, libtriton only needs to export a single symbol `_PyInit_libtriton`. However, currently: 1. On macOS, we export all LLVM symbols since we don't have something like `exclude-libs`. 2. On Linux, despite `exclude-libs` we still export ~100 symbols, mostly from template instantiations. Maintaining an explicit symbol export list ensure the interface is very narrowly scoped, which: 1. Minimizes the chance of symbol conflicts. This is especially a problem on macOS, where we currently export everything, and cpython by default opens libraries with RTLD_GLOBAL (python/cpython#137880). 2. Allows the linker to strip dead code better. 3. Reduces the size of the symbol table. On macOS arm64, this change reduces the size of a stripped libtriton from 133.7 MiB to 113.6 MiB. --- CMakeLists.txt | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f05a53604c0d..47369f3978b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -440,6 +440,23 @@ if(TRITON_BUILD_PYTHON_MODULE) endif() target_link_options(triton PRIVATE ${LLVM_LDFLAGS}) + if(NOT TRITON_EXT_ENABLED) + # When extensions are disabled, only export the Python entrypoint. + set(TRITON_EXPORT_SYMBOLS_FILE "${CMAKE_CURRENT_BINARY_DIR}/triton.exports") + if(APPLE) + set(TRITON_EXPORT_SYMBOLS_FILE_CONTENT "_PyInit_libtriton\n") + target_link_options(triton PRIVATE + "LINKER:-exported_symbols_list,${TRITON_EXPORT_SYMBOLS_FILE}") + elseif(UNIX) + set(TRITON_EXPORT_SYMBOLS_FILE_CONTENT + "{\n global:\n PyInit_libtriton;\n local:\n *;\n};\n") + target_link_options(triton PRIVATE + "LINKER:--version-script,${TRITON_EXPORT_SYMBOLS_FILE}") + endif() + file(GENERATE OUTPUT "${TRITON_EXPORT_SYMBOLS_FILE}" + CONTENT "${TRITON_EXPORT_SYMBOLS_FILE_CONTENT}") + endif() + install(TARGETS triton COMPONENT libraries LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} @@ -460,11 +477,9 @@ if(TRITON_BUILD_PYTHON_MODULE) COPYONLY) # Build plugins when building libtriton since they depend on libtriton. - add_subdirectory(examples/plugins) -endif() - -if (UNIX AND NOT APPLE AND NOT TRITON_EXT_ENABLED) - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--exclude-libs,ALL") + if(TRITON_EXT_ENABLED) + add_subdirectory(examples/plugins) + endif() endif() if(TRITON_BUILD_PYTHON_MODULE AND NOT WIN32)