ubuntips

PGO where BOLT cannot run: PHP 8.6 on an AMD EPYC (Zen 1)

Draft posted on · 6 min read

On this page
Draft commented on Dec 2, 2026

My setup script compiles PHP with clang and LTO, then runs BOLT over the finished binary. BOLT reorders the machine code by how it ran under live traffic, so the hot paths sit together. On Intel that step is worth about 7-9 % to an uncached XenForo page.

BOLT needs a profile with branch history, and it gets that from the CPU’s last branch records (LBR). Intel has had them for years. AMD added full branch records with Zen 4; Zen 3 has only a more limited branch-sampling feature, and Zen 1 and 2 have none. Where the script finds no branch samples, it records bolt=nolbr and skips the step. The PHP it installs there has no profile-based layout optimisation at all.

That left an obvious question: profile-guided optimisation (PGO) gets its profile from an instrumented build, not from CPU counters, so it runs on any CPU. Does it give back what BOLT can’t?

On an AMD EPYC 7401P (Zen 1) it does: +6.2 to +9.1 % requests per second, p99 down 6-9 %, and it won every tier in every round. There’s one catch, and it matters for cron jobs.

Not the first PGO result on this blog#

PGO has a mixed record here. Turning everything on with GCC made PHP 8.5 5-10 % slower. That was GCC’s PGO, measured against a distribution package. The three-builds post had clang PGO arms that did gain, but ThinLTO plus BOLT beat them. This run uses clang’s IR-level PGO on top of the script’s real production build, with the same flags and extensions. On the Xeon E-2236 test host the same method was worth about the same as BOLT (+5 to +7 %), but BOLT already takes most of that there, so PGO on top added only -1.1 to +2.4 % and didn’t become a default. The interesting machine is the one where BOLT can’t run.

How it was measured#

The EPYC is a shared development server that other people use. I didn’t install anything on it system-wide. Everything ran inside one temporary directory with its own private copy of the stack:

  • MariaDB, Valkey, php-fpm and nginx, all on loopback, plain HTTP
  • a XenForo forum with synthetic, seeded data
  • PHP 8.6.0 with the same flags and extensions as the PHP the script builds for that CPU
  • 12 static PHP-FPM workers, wrk with 48 connections
  • the benchmark pinned to CPUs away from the ones the machine’s other users run on

Two arms: A is the plain production-style build, I is the same build plus -fprofile-use from a profile trained on XenForo traffic on that machine. Five rounds, arm order rotated each round, median with min-max. I couldn’t pin the CPU clock on a machine I don’t own, so the per-round numbers move more than on the Xeon. To check the neighbours weren’t skewing it, I logged their CPUs during every tier: median 0.05 % busy, maximum 1.8 %. No request returned anything but 2xx.

When it finished I deleted the directory and ran the same audit as before the test. The machine was unchanged.

The numbers#

Uncached XenForo pages, the ones that run PHP:

TierA req/sI (PGO) req/sChangep99 A -> I
guest home334 (315-341)355 (342-363)+6.2 %171 -> 159 ms (-7.4 %)
guest thread437 (411-439)475 (459-477)+8.7 %127 -> 118 ms (-7.4 %)
member home307 (301-309)335 (325-343)+9.1 %175 -> 164 ms (-5.8 %)
member thread347 (341-349)377 (369-384)+8.7 %161 -> 146 ms (-9.0 %)

The ranges don’t overlap on any tier, though on guest home they miss by less than 1 req/s. Round by round, PGO won all 20 comparisons. The smallest was +0.2 % (guest home, round 1) and the largest +15.7 %, which is what an unpinned clock does to single rounds. The medians are the number to read.

The profile itself shows where a XenForo page spends its time. Of 17,027 functions in the binary, 4,025 ran at all during training. The hottest were strtr with an array, PHP’s allocator (_emalloc, _efree), hash lookups, stripos, lower-casing, JSON parsing and HTML escaping. That’s mostly template rendering and string work, plus a few JIT and VM helpers, not the interpreter loop. With the tracing JIT on, PHP’s own code runs as machine code the JIT generated. What C compiler flags can still reach are these runtime helpers, and that’s exactly what PGO rearranged.

The catch: PHP without the JIT got slower#

The training ran with the JIT on, like production. That left the interpreter’s VM handlers cold in the profile, and the optimiser treated them as code that hardly runs. PHP’s own CPU benchmarks, run with php -n (no php.ini, so no OPcache and no JIT), on one CPU, three runs each:

ScriptAI (PGO)Change
Zend/bench.php0.438 s0.483 s+10.3 % time (slower)
Zend/micro_bench.php2.209 s2.290 s+3.7 % time (slower)

php-fpm doesn’t care. The PHP CLI does: it runs with OPcache off, so XenForo’s cron jobs (xf:run-jobs), imports and rebuilds would get slower with this profile. That’s a regression for the CLI, and my rule for a new default is at least 2 % faster somewhere and slower nowhere. So this isn’t shipped as it stands.

What I didn’t measure#

  • Training and measuring overlapped. About half the training requests hit the same pages the benchmark measured. The Intel run kept them apart. Some of the gain may be the profile fitting this exact test, so +6-9 % may be a little optimistic.
  • One Zen 1 CPU, one application. I didn’t test Zen 2 or Zen 3, WordPress or Chevereto. Zen 2 has no branch records either and takes the same path in the script; I haven’t checked what the script records on Zen 3. I have no numbers for any of them.
  • No clock pinning. The rotation and five rounds even out drift, but single rounds are noisy.
  • No sample-based BOLT. BOLT can also work from ordinary sampled profiles without LBR, at lower quality. I haven’t tried that yet.

What’s next#

A follow-up run is already underway on the same machine and the same way. It fixes both problems: training uses its own pages, kept apart from the measured ones, and the CLI is trained too, so the interpreter’s handlers aren’t cold. It also adds sample-based BOLT as a third arm, to see whether BOLT without LBR adds anything to PGO. That gets its own post.

What it means#

On CPUs without LBR (such as AMD Zen 1 and 2), BOLT can’t run, so PGO is the only layout optimisation available, and it’s worth roughly +6-9 % to XenForo. That clears the 2 % bar for a default by a wide margin. Once a profile no longer slows the CLI down, PGO becomes a candidate default for those CPUs. On Intel and Zen 4 or newer, BOLT already does most of this job, and the script stays as it is.

The numbers, with the Intel PGO runs beside them, are in tests/stress-site/RESULTS.md in ubuntu-setup-script.

Back to top ↑