git: d56ebf78c5a4 - stable/13 - Reduce number of external symbols in libllvm, libclang and liblldb

From: Dimitry Andric <dim_at_FreeBSD.org>
Date: Mon, 29 Dec 2025 02:13:10 UTC
The branch stable/13 has been updated by dim:

URL: https://cgit.FreeBSD.org/src/commit/?id=d56ebf78c5a4a6fdc1fd79666d9bdfa55326bbbf

commit d56ebf78c5a4a6fdc1fd79666d9bdfa55326bbbf
Author:     Dimitry Andric <dim@FreeBSD.org>
AuthorDate: 2025-12-21 21:37:57 +0000
Commit:     Dimitry Andric <dim@FreeBSD.org>
CommitDate: 2025-12-29 02:11:42 +0000

    Reduce number of external symbols in libllvm, libclang and liblldb
    
    In commit 2e47f35be5dc libllvm, libclang and liblldb were converted into
    private shared libraries. This allowed clang, lld, lldb, and other llvm
    tools to be linked against these shared libraries, which makes them
    smaller and avoids duplication.
    
    However, upstream builds the shared libraries using several visibility
    options, which reduces the number of external symbols, and makes the
    libraries a bit smaller.
    
    On my test machine:
    * libprivatellvm.so goes from 75643 to 34706 symbols (~54% reduction)
    * libprivateclang.so goes from 53250 to 33531 symbols (~37% reduction)
    * libprivatelldb.so goes from 27242 to 18798 symbols (~31% reduction)
    
    Note: to get the full benefit, a clean build is required. Incremental
    builds should still work, but I didn't want to force a full rebuild on
    everybody.
    
    MFC after:      1 week
    
    (cherry picked from commit cf1eaaf41cefe7e3aef45f6dab862191a3939ed8)
---
 lib/clang/libllvm/Makefile | 12 ++++++++++++
 lib/clang/llvm.build.mk    |  1 +
 2 files changed, 13 insertions(+)

diff --git a/lib/clang/libllvm/Makefile b/lib/clang/libllvm/Makefile
index 2060f5f52c91..9ea816fd4e10 100644
--- a/lib/clang/libllvm/Makefile
+++ b/lib/clang/libllvm/Makefile
@@ -49,6 +49,8 @@ CFLAGS+=	-DBLAKE3_NO_AVX512 -DBLAKE3_NO_AVX2 -DBLAKE3_NO_SSE41 \
 CFLAGS+=	-DBLAKE3_USE_NEON=0
 .endif
 
+CFLAGS+=	-DLLVM_BUILD_LLVM_DYLIB
+
 SRCDIR=		llvm/lib
 
 # Explanation of different SRCS variants below:
@@ -2051,6 +2053,16 @@ SRCS_ALL+=	${SRCS_COV}
 SRCS+=		${GENSRCS}
 SRCS+=		${SRCS_ALL:O}
 
+# Optimization: for all source files under Target/*/*, use -fvisibility=hidden,
+# which reduces the number of external symbols in the resulting shared library.
+# This is also what upstream does.
+.for src in ${SRCS}
+. if "${src:MTarget/*/*}"
+CFLAGS.${src:T}+=	-fvisibility=hidden
+.info ${src}:	CFLAGS.${src:T}
+. endif
+.endfor
+
 llvm/CodeGen/GenVT.inc: ${LLVM_SRCS}/include/llvm/CodeGen/ValueTypes.td
 	${LLVM_MIN_TBLGEN} -gen-vt \
 	    -I ${LLVM_SRCS}/include -d ${.TARGET}.d -o ${.TARGET} \
diff --git a/lib/clang/llvm.build.mk b/lib/clang/llvm.build.mk
index 2d8d9a2b0fcb..af7e501c007d 100644
--- a/lib/clang/llvm.build.mk
+++ b/lib/clang/llvm.build.mk
@@ -124,6 +124,7 @@ LDFLAGS+=	-Wl,-m,elf64lriscv_fbsd
 CXXSTD?=	c++17
 CXXFLAGS+=	-fno-exceptions
 CXXFLAGS+=	-fno-rtti
+CXXFLAGS+=	-fvisibility-inlines-hidden
 .if ${.MAKE.OS} == "FreeBSD" || !defined(BOOTSTRAPPING)
 CXXFLAGS.clang+= -stdlib=libc++
 .else