git: 5c5e2a0a7be8 - stable/15 - Reduce number of external symbols in libllvm, libclang and liblldb

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

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

commit 5c5e2a0a7be849c7a557a026352008190f8bf10b
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:09:11 +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 810eac08d8d5..7c45375b8fbf 100644
--- a/lib/clang/libllvm/Makefile
+++ b/lib/clang/libllvm/Makefile
@@ -48,6 +48,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:
@@ -2050,6 +2052,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 e88b2ff1033e..f6e51129e9fa 100644
--- a/lib/clang/llvm.build.mk
+++ b/lib/clang/llvm.build.mk
@@ -122,6 +122,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