Skip to content

Build the Linux releases with PGO and ThinLTO - #8649

Merged
danlapid merged 6 commits into
mainfrom
dlapid/workerd-pgo-build
Nov 22, 2026
Merged

danlapid merged 6 commits into
mainfrom
dlapid/workerd-pgo-build

Conversation

@danlapid

@danlapid danlapid commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

The Linux release builds, x64 and arm64, now use ThinLTO and PGO, end to end. The profile is trained by a workflow and kept as a GitHub release asset.

Note

The first profile is uploaded: workerd-2026-11-20-d4e0b3cc39.profdata.zst on the pgo-profiles prerelease, the weight-10 profile measured below.

What changes

  • release_linux = ThinLTO + PGO + Rust bitcode LTO.
    • The profile covers the C++ code, V8 included, and the Rust code. rustc compiles to bitcode (-Clinker-plugin-lto), so the ThinLTO link inlines across the cxx bridge.
    • The release workflow, the types jobs (bazelflare-release) and anyone building --config=release_linux get it.
    • release_linux-without-pgo is the old release build. The fork-PR fallback that builds types now uses it, so that job doesn't pay for a ThinLTO link.
    • macOS and Windows are unchanged; they're follow-ups.
  • Where the profile lives.
    • build/pgo/profile.json holds the URL and hash of an asset on the single pgo-profiles prerelease. A module extension downloads it as @workerd_pgo_profile.
    • One release holds all of them, so the profiles don't crowd the release list or shift which release the daily release notes start from.
    • Release assets don't expire.
    • CI's repository cache keys downloads by hash, so a runner that has the file keeps building even if the asset goes away.
    • Every target in build/pgo is manual, so bazel build //... and bazel test //... never fetch the profile: a missing asset breaks only the release builds, which release.yml runs on every push to main.
  • Rust gets the profile on remote executors too. rules_rust can't add an input to rustc actions, so the old configs ran rustc in a local sandbox with an absolute path.
    • A small rules_rust patch (0004) adds an extra_rustc_inputs label flag, and pgo-use puts the profile there.
    • The same patch replaces ${extra_rustc_inputs} in an extra rustc flag with the file's path, so -Cprofile-use=${extra_rustc_inputs} needn't name where Bazel puts the profile.
  • Two test fixes that the release build needs. See Testing.
  • Training (build/pgo/train.sh):
    • Builds --config=pgo-instrument and runs all 1,693 tests that run the workerd binary, in every variant but @gc-stress.
    • The tests run under a --run_under wrapper that sets KJ_CLEAN_SHUTDOWN=1. Without it, workerd exits through _exit() and writes no profile.
    • The wrapper merges each test's raw profiles in place, sparse except for one test, so the downloads stay small. The whole run downloads about 7 GB.
    • The tests only start Workers and hand them a few events, so a new serving workload covers the request path: src/workerd/server/tests/pgo-workload.
      • It runs, through the existing WorkerdServerHarness, workerd serve of a sync and an async hello world, plus a Worker that uses a mix of APIs (JSON, headers, streams, TransformStream, subrequests, crypto.subtle, URL).
      • It checks every response.
      • Normally it sends 50 requests per path; under training, 10,000–40,000.
    • Its profile is weighted ×10 against the tests'. See the measurement below.
  • Workflows:
    • pgo-train.yml runs only when started by hand. It trains on bazelflare, uploads with build/pgo/upload.sh, and opens a PR that updates profile.json, like the deps updater.
    • release.yml already runs the release builds on every push to main, and publishes only the first build of each release version. So a missing profile, or a change that breaks the PGO build, fails the merge right after it.
    • Its Linux builds now also run the serving workload on the PGO binary, next to the WPT suite.

The arm64 build uses the x64 profile: Clang matches it to the code in its architecture-independent form. arm64-only code gets no profile data.

Measurement

The setup:

  • An AMD EPYC Linux VM, at the binary's newest compatibility date.
  • The server is pinned to one core; oha sends 200k requests per round, and the rounds of every binary are interleaved.
  • rewriter (HTMLRewriter) and render (JSON to an HTML table) were left out of training.
  • The profile was trained by build/pgo/train.sh on this branch.

Requests per second (median of three rounds) and instructions per request, against today's release build (release_linux-without-pgo). "ThinLTO" adds --config=thin-lto --config=rust-lto to that, without the profile.

Worker Release ThinLTO ThinLTO + PGO
sync hello world 12,787 · 215k 15,283 (+20%) · 195k 22,549 (+76%) · 153k (−29%)
async hello world 13,057 · 215k 15,232 (+17%) · 196k 22,318 (+71%) · 154k (−28%)
render, not trained on 5,925 · 705k 6,656 (+12%) · 664k 9,177 (+55%) · 587k (−17%)
rewriter, not trained on 860 · 2,995k 985 (+15%) · 2,716k 1,175 (+37%) · 2,279k (−24%)

The workload's weight against the tests' (PGO_WORKLOAD_WEIGHT, default 10):

Weight sync async render rewriter
1 +70% +69% +52% +33% · 2,274k
10 +76% +71% +55% +37% · 2,279k
100 +81% +77% +57% +36% · 2,341k
workload only +78% +72% +49% +27% · 2,448k
tests only +50% +47% +44% +31% · 2,332k

100 is 2–4% faster than 10 on the hello worlds, within the spread between rounds, but runs rewriter, which nothing in training resembles, in 3% more instructions. 10 is the safer bet for Workers unlike the workload.

The tests-only profile was measured in a separate run, against weight 10 (+74%, +67%, +57%, +35% · 2,279k in that run). The workload is worth its code even for Workers it wasn't trained on: with it, render is 9.5% faster and rewriter 3% faster than with the tests alone.

Testing

  • The whole suite on the PGO release build (--config=release_linux with this profile, through --override_repository) on bazelflare: 2,951 tests pass, 4 skipped. //build/tools/llvm was left out: it holds only the arm64 coverage tools.
  • It found two tests that the release build had never run:
    • frankenvalue-test failed under ThinLTO alone. Its dynamic_cast to Frankenvalue::CapTableBuilder and CapTableReader found a second copy of their type info, because neither class had a virtual function defined out of line, and a dynamically linked test built with ThinLTO keeps its executable's copy apart from the library's. The shipped binary links statically and never had the problem. Both classes now define their destructors in frankenvalue.c++, and jsg::Serializer::ExternalHandler gets the virtual destructor its Deserializer counterpart has.
    • global-scope-test:structuredCloneError failed under PGO. Its getter builds arrays nested about as deep as the stack allows, and with PGO's inlining the serializer ran out of stack cloning them, throwing a RangeError. The test, which guards against a crash, now accepts that.
    • Both pass five times in every variant on the old release build, ThinLTO alone, and PGO.
  • pgo-workload-test, five times on the PGO build. It and every other test that uses WorkerdServerHarness, which gained options to turn off the inspector and --verbose, five times each on bazelflare.
  • bazel aquery --config=pgo-use shows Rustc actions get -Cprofile-use=external/+pgo_profile+workerd_pgo_profile/workerd.profdata with the profile among their inputs.
  • build/pgo/train.sh on bazelflare: all 1,693 tests it runs pass on the instrumented build, and it wrote the profile measured above.

🤖 Generated with Claude Code

@danlapid
danlapid force-pushed the dlapid/workerd-pgo-build branch from f884c79 to 4913c55 Compare November 20, 2026 00:34
@danlapid danlapid changed the title Add PGO, Rust PGO + LTO and BOLT configs for the Linux release build Build the Linux releases with PGO and ThinLTO Nov 20, 2026
@danlapid
danlapid force-pushed the dlapid/workerd-pgo-build branch from 4913c55 to d4e0b3c Compare November 20, 2026 00:52
@codecov-commenter

codecov-commenter commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 7.80142% with 130 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.74%. Comparing base (ca34b08) to head (c14431d).

Files with missing lines Patch % Lines
src/workerd/server/tests/pgo-workload/mixed.js 0.00% 87 Missing ⚠️
...c/workerd/server/tests/pgo-workload/hello-async.js 0.00% 14 Missing ⚠️
src/workerd/server/tests/pgo-workload/upstream.js 0.00% 14 Missing ⚠️
src/workerd/server/tests/pgo-workload/hello.js 0.00% 10 Missing ⚠️
src/workerd/server/tests/server-harness.mjs 61.53% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8649      +/-   ##
==========================================
- Coverage   67.78%   67.74%   -0.04%     
==========================================
  Files         842      846       +4     
  Lines      214756   214893     +137     
  Branches    32572    32577       +5     
==========================================
+ Hits       145569   145580      +11     
- Misses      49947    50075     +128     
+ Partials    19240    19238       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@danlapid
danlapid force-pushed the dlapid/workerd-pgo-build branch 4 times, most recently from a28fb7d to 8f337a9 Compare November 21, 2026 21:15
extra_rustc_flag can pass rustc a flag that names a file, such as
-Cprofile-use=<profile>, but nothing adds that file to the Rustc
action's inputs, so rustc can't read it in a sandbox or on a remote
executor. The extra_rustc_inputs label flag adds its files to every
target-configuration Rustc action, as extra_rustc_flag adds its
flags, and ${extra_rustc_inputs} in an extra flag stands for the
path of its file, so the flag needn't hardcode where Bazel puts it.
The PGO build uses it to give rustc the profile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danlapid
danlapid force-pushed the dlapid/workerd-pgo-build branch 3 times, most recently from b8e227e to 8a12947 Compare November 21, 2026 21:58
@danlapid
danlapid added this pull request to stack #8796 November 21, 2026 22:54
danlapid and others added 3 commits November 21, 2026 17:54
The tests only do what `workerd test` does: start a Worker and hand it
a few events. A profile trained on them alone wouldn't see the HTTP
server, or a Worker handling one request after another. This test runs
`workerd serve` of a few Workers, a sync and an async hello world and
one that uses a mix of the APIs real Workers use, and sends each many
requests from concurrent clients, checking every response so that a
broken Worker fails the test rather than training the profile on error
paths. It starts workerd through WorkerdServerHarness, which gains
options to leave out the inspector and --verbose, as production
does.

Normally it sends 50 requests per path. Under the PGO training
wrapper, which sets WORKERD_PGO_TRAINING, it sends 10,000 to 40,000.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Callers find CapTableReader and CapTableBuilder with dynamic_cast,
and neither had a virtual function defined out of line, so every
library that used them emitted its own copy of their type info. A
dynamically linked test built with ThinLTO keeps its executable's
copy apart from the library's, and frankenvalue-test's dynamic_cast
then failed. Their destructors, now defined in frankenvalue.c++, make
the type info a single definition there. CapTableBuilder's base,
jsg::Serializer::ExternalHandler, gets the virtual destructor that
jsg::Deserializer::ExternalHandler already has, so that
CapTableBuilder's destructor is virtual too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The test's getter recurses until the stack runs out, catching the
overflow at each level, so it returns arrays nested about as deep as
the stack allows. Whether structuredClone() can then serialize them
depends on how much stack each level of the serializer takes, which
the PGO build changes: there it throws a RangeError. That isn't the
crash the test guards against, so the test accepts it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danlapid
danlapid force-pushed the dlapid/workerd-pgo-build branch from 8a12947 to 8a695db Compare November 21, 2026 22:54
danlapid added a commit that referenced this pull request Nov 21, 2026
#8649's branch was rebased onto a newer main. Its own versions of
the PGO workload, server harness and README win; the README keeps
this branch's Windows section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danlapid
danlapid marked this pull request as ready for review November 22, 2026 01:29
@danlapid
danlapid requested review from a team as code owners November 22, 2026 01:29

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of a0e2211f.

This review, of 8a695db6

💬 Approved with comments — No blocking or warning-level issues. One INFO note: the x64 release CI job in cfsetup.yaml now builds with ThinLTO and PGO and needs the profile from GitHub.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 info

No correctness bugs. One note about another CI job that the new release_linux config now affects.

Recommendation: 💬 Optional Nits

  • [INFO] cfsetup.yaml:26 (ci-bazel-x64-release) uses --config=release_linux, so it now gets ThinLTO, PGO and a profile download from GitHub. The pull request doesn't mention this job. (.bazelrc:509)
View full review

release_linux now expands to thin-lto, rust-lto and pgo-use (.bazelrc:509-510). The ci-bazel-x64-release job in cfsetup.yaml:26 runs bazel test --config=release_linux //.... That job now does a ThinLTO link for every test and has to download @workerd_pgo_profile from a GitHub release asset. It also installs clang-22/lld-22 from apt, while the rust-lto comment says rustc and lld must share an LLVM major version. The pull request doesn't mention this job. Either confirm it works with the new config, or switch it to release_linux-without-pgo the way the types fallback in test.yml was switched.

Other checks found no problems:

  • The out-of-line destructors in frankenvalue and the virtual destructor added to ExternalHandler.
  • The option plumbing in server-harness.mjs.
  • The test_target expression in release.yml.
  • Argument handling in train.sh.

✅ All other checks passed

🚀 Release: ✅ LGTM

Overall risk: Medium. The Linux release binaries are now built with ThinLTO and PGO, which changes how the shipped artifacts are optimized. The pull request description says the whole suite and the serving workload pass on the PGO build. macOS and Windows release builds are unchanged.


🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 8a695db6

Comment thread .bazelrc Resolved
release_linux, which the x64 and arm64 release builds use, now adds
ThinLTO and Clang's instrumentation-based PGO, for the C++ code, V8
included, and the Rust code, which it compiles to bitcode so that the
ThinLTO link optimizes both together. release_linux-without-pgo is the
old release build, for comparisons.

The profile isn't checked in. build/pgo/profile.json names it, an
asset of the pgo-profiles GitHub release, with its hash, and Bazel
downloads it. rustc gets it through rules_rust's extra_rustc_inputs,
which also names its path in -Cprofile-use.

build/pgo/train.sh trains a profile: it builds pgo-instrument, runs
every test that runs the workerd binary under a wrapper that sets
KJ_CLEAN_SHUTDOWN, so that workerd writes its profile, and merges each
test's profiles, then merges them all, weighting the serving
workload's. build/pgo/upload.sh uploads it and updates profile.json.
The pgo-train workflow does both and opens a pull request; it runs
only when someone starts it. release.yml already builds the release
configuration on every push to main, publishing only the first build
of each release version, so a deleted profile or a change that breaks
the PGO build fails the merge that follows it. Its Linux builds now
also run the serving workload on the PGO binary.

The arm64 build uses the profile trained on x64: Clang matches it to
the code in its architecture-independent form, and the arm64-specific
code gets no profile data. macOS and Windows keep their release
builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danlapid
danlapid force-pushed the dlapid/workerd-pgo-build branch from 8a695db to a0e2211 Compare November 22, 2026 01:34

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of c14431d8.

This review, of a0e2211f

✅ Approved — The earlier note is fixed: the x64 release CI job now builds with release_linux-without-pgo. There are no other findings.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🚀 Release: ✅ LGTM

Overall risk: Medium. The Linux release binaries are now built with ThinLTO and PGO, which changes how the shipped artifacts are optimized. The pull request reports that the full test suite and the serving workload pass on the PGO build. macOS and Windows release builds are unchanged, and no version or changelog files change.


🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

ci-bazel-x64-release (cfsetup.yaml:26) now uses --config=release_linux-without-pgo. Per .bazelrc:507-508, that is the old release build, so the job builds as it did before this pull request, with no ThinLTO links and no profile download.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: a0e2211f

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No findings. The last review's one note (the x64 release CI job) stays fixed, and the merge from main brought no new issues.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🚀 Release: ✅ LGTM

Overall risk: Medium. The Linux release binaries (x64 and arm64) are now built with ThinLTO and PGO, which changes how the shipped binaries are optimized. According to the pull request, the full test suite (2,951 passed, 4 skipped) and the serving workload pass on the PGO build. macOS and Windows release builds are unchanged. No version or changelog files change.


🔒 Security: ✅ LGTM

No security issues in the changed code.

  • Profile download: build/pgo/profile.bzl requires an integrity hash when it downloads the profile. upload.sh computes that hash and writes it into profile.json, which can only change on main through a pull request.
  • pgo-train.yml: it runs only when started by hand (workflow_dispatch). It doesn't insert untrusted input into commands, and its token permissions are limited.
  • Secrets: no hardcoded secrets.

📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues in the changed files.

  • Release workflow test targets: in release.yml, the Linux test_target passes two space-separated targets, and _bazel.yml expands them unquoted into bazel test, so both run.
  • Training scripts: train.sh and profile-env.sh merge the weighted profiles consistently. They accept Bazel exit status 3 (some tests failed) and fail the run if the dense profile is missing.
  • Destructors: the out-of-line virtual destructors in frankenvalue.h and ser.h are noexcept(false), which matches the existing Deserializer convention.
  • structuredCloneError test: it now also accepts a RangeError when cloning runs out of stack.

⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: c14431d8

@danlapid
danlapid merged commit e9a7b9c into main Nov 22, 2026
23 checks passed
@danlapid
danlapid deleted the dlapid/workerd-pgo-build branch November 22, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants