1313PHP_CONFIG ?= php-config
1414
1515CXX ?= c++
16+ UNAME_S := $(shell uname -s)
17+
18+ # The extension never throws and never walks its own stack (the engine
19+ # unwinds with longjmp), so it needs no unwind tables: -fno-exceptions alone
20+ # still leaves .eh_frame for every function, 0.8 MB of the Linux .so. The
21+ # section flags let the linker drop whatever nothing references.
22+ SIZE_FLAGS := -fno-asynchronous-unwind-tables -fno-unwind-tables -ffunction-sections -fdata-sections
1623# The strict set the CI compile legs build with, defined once here and read
1724# by the workflows (`make -s print-warn-flags`) instead of being spelled out
1825# in each of them. The three -Wno- exemptions cover third-party macro
@@ -47,14 +54,21 @@ STRICT_WARN_FLAGS := -Wall -Wextra -Werror \
4754# warn where the versions CI pins do not, and -Werror would turn that into a
4855# build failure for them.
4956WARN_FLAGS ?= -Wall
57+ # -O2, measured against -Os in the CI image (GCC 11.4, arm64, interleaved
58+ # A/B, user CPU with the extension loaded): -Os with LTO takes the stripped
59+ # .so from 7.5 MB to 5.0 MB but makes the analysis 10.4% slower (6.7% with
60+ # PGO), so the size is not worth it.
5061# ZEND_ENABLE_STATIC_TSRMLS_CACHE: on ZTS builds EG()/CG() go through the
5162# per-thread cache main.cpp defines instead of a ts_resource lookup per
5263# access; a no-op on NTS builds.
5364CXXFLAGS := $(WARN_FLAGS ) -O2 -std=c++17 -fPIC \
5465 -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 \
5566 ` $( PHP_CONFIG) --includes`
5667
57- # Profile-guided optimisation. PGO_FLAGS is empty for a plain build; `make
68+ # Profile-guided optimisation, measured 2026-09-23 with the extension
69+ # loaded (interleaved A/B, user CPU, n=11): 4.0% faster on Linux (GCC 11.4,
70+ # t=-6.3), 3.6% on macOS (clang, t=-14.1), for a stripped .so within 1-2%
71+ # of the same size. PGO_FLAGS is empty for a plain build; `make
5872# pgo` drives the two instrumented/optimised builds below with the flags of
5973# the compiler in use (clang writes .profraw files merged by llvm-profdata,
6074# GCC writes .gcda files next to the objects and reads them back from
@@ -71,6 +85,16 @@ PGO_GEN_FLAGS := -fprofile-generate -fprofile-update=atomic
7185PGO_USE_FLAGS := -fprofile-use -fprofile-correction -Wno-missing-profile
7286endif
7387
88+ # Link-time optimisation, with GCC (every Linux leg). Measured in the CI
89+ # image (GCC 11.4, arm64, `make pgo` on both sides, interleaved A/B, user
90+ # CPU, n=11): 3.9% faster (t=-9.4) and the stripped .so 15% smaller
91+ # (7.4 -> 6.3 MB). Clang goes the other way on size: full LTO on macOS was
92+ # 1.2% faster (t=-3.6) but made the stripped .so 9% larger (7.1 -> 7.7 MB),
93+ # so clang builds stay without it. On the compile and the link line both.
94+ ifneq ($(CXX_IS_CLANG ) ,1)
95+ LTO_FLAGS := -flto=auto
96+ endif
97+
7498# The extension version is the short SHA of the last commit touching
7599# turbo-ext/src/ (the same computation the CI version job enforces against
76100# TurboExtensionEnabler::EXPECTED_EXTENSION_VERSION),
@@ -98,11 +122,13 @@ CXXFLAGS += -DPHPSTANTURBO_VERSION='"$(PHPSTANTURBO_VERSION)"'
98122# GNU ld allows them in shared objects by default, Darwin needs the flag.
99123# On Linux, fold libstdc++/libgcc into the .so statically so a distributed
100124# binary does not depend on the build host's GLIBCXX_*/GCC_* symbol versions.
101- UNAME_S := $(shell uname -s)
102125ifeq ($(UNAME_S ) ,Darwin)
103- LINK_FLAGS := -undefined dynamic_lookup
126+ LINK_FLAGS := -undefined dynamic_lookup -Wl,-dead_strip
104127else
105- LINK_FLAGS := -static-libstdc++ -static-libgcc
128+ LINK_FLAGS := -static-libstdc++ -static-libgcc -Wl,--gc-sections
129+ # main.cpp then supplies the terminate handler, keeping libstdc++'s (and its
130+ # demangler) out of the link
131+ CXXFLAGS += -DPHPSTANTURBO_STATIC_LIBSTDCXX
106132# the loader resolves and write-protects the GOT before handing control over,
107133# and the stack is never executable
108134LINK_FLAGS += -Wl,-z,relro,-z,now -Wl,-z,noexecstack
@@ -127,6 +153,7 @@ endif
127153# (config.m4's phpize path does not carry these, the same way it does not
128154# carry the strict warnings: the Makefile is the primary build.)
129155CXXFLAGS += -fvisibility=hidden -fvisibility-inlines-hidden -fno-exceptions -fno-rtti
156+ CXXFLAGS += $(SIZE_FLAGS ) $(LTO_FLAGS )
130157
131158# Hardening. Every flag is probed against the compiler actually in use rather
132159# than assumed, because the targets disagree: the CI floor is GCC 11.4, which
@@ -183,12 +210,26 @@ SOURCES := $(wildcard src/*.cpp) $(wildcard src/parser/*.cpp)
183210OBJECTS := $(SOURCES:.cpp=.o )
184211
185212phpstan_turbo.so : $(OBJECTS )
186- $(CXX ) ` $( PHP_CONFIG) --ldflags` -shared $(LINK_FLAGS ) $(PGO_FLAGS ) -o $@ $(OBJECTS )
213+ $(CXX ) ` $( PHP_CONFIG) --ldflags` -shared $(LTO_FLAGS ) $( LINK_FLAGS ) $(PGO_FLAGS ) -o $@ $(OBJECTS )
187214 @# a shared object links with undefined symbols allowed (the engine's are
188215 @# resolved at load time), so a helper declared but never defined only
189216 @# surfaces as a jump to NULL at run time — fail the build instead
190217 @if nm -u $@ | grep -E ' pt_[a-z_]+|phpstanturbo' > /dev/null; then echo " undefined extension symbols in $@ :" ; nm -u $@ | grep -E ' pt_[a-z_]+|phpstanturbo' ; rm -f $@ ; exit 1; fi
191218
219+ # The distributed binaries ship without symbol tables: a quarter of the .so
220+ # (2.4 MB on Linux, 3.2 MB on macOS) that the loader never reads, since the
221+ # only symbol PHP looks up is get_module, which stays in the dynamic table.
222+ # Local builds keep them — sample and perf name frames from them. Not a
223+ # dependency of phpstan_turbo.so on purpose: this runs after `make pgo`, and
224+ # must never rebuild what that produced with other flags.
225+ strip :
226+ @test -f phpstan_turbo.so || { echo " phpstan_turbo.so is missing — build it first" ; exit 1; }
227+ ifeq ($(UNAME_S ) ,Darwin)
228+ strip -x phpstan_turbo.so
229+ else
230+ strip --strip-unneeded phpstan_turbo.so
231+ endif
232+
192233# every object depends on every header: the op tables and the trait helper
193234# declarations live in TypeOps.h / TypeTraits.h, and a stale object built
194235# against an older layout jumps into the wrong entry at run time
@@ -302,7 +343,9 @@ lint: compile_commands.json
302343# this build — a bounds violation would abort with no diagnostic, the exact
303344# opposite of what a sanitizer run is for. Probed, because the spelling is
304345# not available on every compiler this builds with.
305- SANITIZE_FLAGS ?= -fsanitize=undefined $(call cxx-supports,-fno-sanitize-trap=all) -fno-omit-frame-pointer
346+ # The unwind tables SIZE_FLAGS drops come back here for the same reason:
347+ # print_stacktrace unwinds through them.
348+ SANITIZE_FLAGS ?= -fsanitize=undefined $(call cxx-supports,-fno-sanitize-trap=all) -fno-omit-frame-pointer -funwind-tables -fasynchronous-unwind-tables
306349SANITIZE_TESTS ?= smoke arena-smoke signature-parity parser-corpus
307350
308351sanitize :
@@ -316,4 +359,4 @@ sanitize:
316359 done
317360 $(MAKE ) clean
318361
319- .PHONY : clean lint pgo pgo-clean print-clang-tidy-version print-warn-flags sanitize FORCE
362+ .PHONY : clean lint pgo pgo-clean print-clang-tidy-version print-warn-flags sanitize strip FORCE
0 commit comments