ubuntips

BOLT, PGO or both for PHP on a Xeon

Draft posted on · 7 min read

On this page
Draft commented on Dec 2, 2026

Nine days ago, turning everything on made PHP slower: GCC with LTO and profile-guided optimisation lost 5-10 % against the distribution package. Since then my setup script has moved to a clang build of PHP with ThinLTO, -march=native and mimalloc, and it runs BOLT on that binary once the site has served real traffic. That build is the one the three-builds post found 5-10 % faster than the package.

PGO deserved a second try on the new build. The three-builds post did have clang PGO arms, but as alternatives to ThinLTO rather than on top of it, and ThinLTO plus BOLT beat PGO plus BOLT there. PGO was never tried on the build the script actually ships, and clang’s PGO is a different implementation from GCC’s. So I compiled the same PHP four ways and measured all four in one run.

Short version: BOLT and PGO each give about +7 % over the plain build, and together +7 to +10 %. Against the BOLT build my script already ships, PGO adds -1.1 to +2.4 %. That isn’t enough to make it a default, so it stays out on CPUs where BOLT runs.

The four builds#

All four are PHP 8.5.13 from the same sources, with the same configure line, extensions and flags as the build the script installs.

BuildWhat it adds
plainThinLTO, -march=native, mimalloc: the script’s build before BOLT runs
+ BOLTwhat the script ships: the binary rewritten from 60 s of perf branch samples (LBR) taken under load
+ PGOLLVM IR PGO: an instrumented build trained on the mixed XenForo load, then rebuilt with -fprofile-use
+ PGO + BOLTthe PGO build, then BOLT from 60 s of the same load

BOLT and PGO both work on code layout: which branches are hot, which functions sit next to each other, what moves out of the way. They get there differently. PGO counts every edge in an instrumented build and feeds the counts back into the compiler. BOLT samples the finished binary with the CPU’s own branch records and rewrites the machine code. I expected a big overlap between them, and that’s what the numbers show.

How I measured#

The test host is a Xeon E-2236 (6 cores, 12 threads) with the clock pinned at 3.6 GHz, so turbo fade can’t favour whichever arm runs first. It serves XenForo 2.3.14 on synthetic, seeded data (20,000 users, 10,000 threads, 100,000 posts), with 12 PHP-FPM workers and the tracing JIT on. Each tier ran for 10 s at 100 connections. There were 5 rounds, with the order of the four builds rotated each round, and the switch between builds was a unit drop-in plus a PHP-FPM restart. The tables show the median of the five rounds, with the slowest and fastest round in brackets.

The numbers#

Requests per second. The percentages compare each build with the plain one.

Tierplain+ BOLT (shipped)+ PGO+ PGO + BOLT
guest home (uncached)380 [368-383]409 (+7.6 %) [407-412]407 (+7.1 %) [405-407]419 (+10.3 %) [403-421]
guest thread (uncached)286 [285-286]305 (+6.6 %) [305-308]306 (+7.0 %) [304-306]312 (+9.1 %) [310-313]
member home324 [310-331]352 (+8.6 %) [340-355]346 (+6.8 %) [335-351]348 (+7.4 %) [344-358]
member thread215 [209-217]232 (+7.9 %) [227-234]229 (+6.5 %) [221-230]236 (+9.8 %) [231-236]

Median p99 latency moved the same way. On the member thread, for example, it went 702 ms plain, 649 ms with BOLT, 644 ms with PGO and 635 ms with both.

The cached guest tiers aren’t in the table. They never reach PHP: nginx serves them from its cache at around 145,000 and 106,000 requests per second. The four builds landed between -2.5 % and +0.9 % there, with overlapping ranges, and that’s nginx noise.

The question that matters for the script is the last column against the second:

Tier+ BOLT (shipped)+ PGO + BOLTChange
guest home (uncached)409419+2.4 % (ranges overlap)
guest thread (uncached)305312+2.3 %
member home352348-1.1 %
member thread232236+1.7 %

The two earlier, partial runs agree with this. The first compared PGO alone with the plain and BOLT builds: two clean rounds, +4.9 to +6.7 % for PGO and +6.5 to +7.6 % for BOLT. I threw away its other three rounds because they overlapped unrelated test runs on the same host. The second run put PGO + BOLT against the shipped BOLT build for 5 rotated rounds and found +0.9 to +2.0 %.

Why PGO isn’t added#

The rule for a default in my script: at least +2 % on some tier, no tier slower, measured on both Intel and AMD. PGO + BOLT misses on two counts. Member home is 1.1 % slower, and the best tier’s range overlaps the shipped build’s. The earlier run topped out at exactly +2.0 %. BOLT has already taken most of what PGO offers, because both of them are fixing the same layout problem.

The cost side is also real. Every PHP refresh would need an instrumented build, a training run under load with a single worker (see below), and a rebuild. That’s a second full compile and minutes of synthetic traffic on a production server, for a gain inside the rule’s margin.

There’s an exception. On CPUs where BOLT can’t run, PGO is the only layout optimisation. Older AMD CPUs have no LBR, so the script records bolt=nolbr and ships the plain build there. That case has its own measurement and its own caveats: PGO where BOLT cannot run.

What tripped me up#

LLVM’s PGO and php-fpm don’t get along out of the box. Four traps, in the order I hit them:

  • The profile runtime is a separate package. -fprofile-generate links clang’s profile runtime library. The LLVM packages from apt.llvm.org ship it in its own libclang-rt-<version>-dev package, and without it the instrumented build fails at link time.
  • Workers inherit the master’s profile file. The profile file name is decided when the process starts, and php-fpm’s workers are forked from the master. All of them then write to the master’s file when they exit, and each one overwrites the others’ counts. The name needs a pattern that differs per process, or a merge pattern.
  • %m merging corrupted the profile with 12 workers. LLVM’s %m pattern makes processes merge into a shared file in place. With twelve busy workers exiting together, the merged profile came out corrupt. Training with pm.max_children=1 produced a clean profile. One worker serves less traffic, but the profile records edge frequencies, not throughput, so that doesn’t matter.
  • Profile names embed the build directory. Static functions are named in the profile by their source file path. The training build and the -fprofile-use rebuild must therefore compile in the same directory, or every static function’s profile silently goes missing. A fixed work directory fixed it.

What changed since the GCC attempt#

The GCC attempt and this one disagree: there PGO cost 5-10 %, here it gains about 7 % over the same build without it. Several things differ, and I didn’t isolate which one decided it:

GCC attempt (November)This run
Compiler and PGOGCC 15, gcov-style -fprofile-generateclang, LLVM IR PGO
PHP VMGCC build, global registers off by a harness bugclang’s tail-call VM
Baselinethe distribution packagethe same build without PGO
Extra flags-falign-functions=32, -fno-semantic-interpositionnone beyond the shipped build
TrainingXenForo and WordPress trafficXenForo only, one worker

The harness bug alone, found in the three-builds post, handicapped every GCC arm in that round. The old post’s main lesson still holds: train with the JIT in the state you ship and measure the same way.

What I didn’t measure#

  • Only XenForo. WordPress and Chevereto weren’t part of the training or the benchmark.
  • One Intel CPU. The Xeon is one generation of one vendor; the AMD side is in its own post.
  • The CLI. These numbers are PHP-FPM with the JIT on; the CLI (opcache off) was not measured on the Xeon.
  • PGO for nginx and Valkey. I didn’t try it. Their share of the CPU time in a XenForo request is a few percent.

The script stays as it is: BOLT where the CPU has branch records, the plain ThinLTO build where it doesn’t. PGO for the CPUs without LBR is the open question.

Back to top ↑