Skip to content

Commit 0445d64

Browse files
Optimize Linux x86-64 ty releases with LLVM BOLT (#4833)
## Summary This PR enables BOLT for x86-64 releases. The net effect is: 4.23% less wall-time, at the cost of a 9.9% larger wheel (and another 53 seconds in the release pipeline). | Project | ty 0.0.92 | This PR | Wall-time reduction (95% interval) | Peak RSS, 0.0.92 → PR | | --- | ---: | ---: | ---: | ---: | | discord.py | 773 ms | 727 ms | 5.9% (3.5–8.3%) | 199.3 → 198.8 MiB | | Jinja | 186 ms | 175 ms | 6.0% (4.0–7.9%) | 83.8 → 82.3 MiB | | Black | 271 ms | 259 ms | 4.3% (0.8–7.3%) | 110.9 → 109.8 MiB | | isort | 336 ms | 309 ms | 8.1% (4.8–11.2%) | 74.9 → 74.0 MiB | | pandas-stubs | 280 ms | 258 ms | 7.8% (3.9–12.7%) | 142.7 → 140.1 MiB | | Prefect | 460 ms | 428 ms | 6.9% (3.0–10.9%) | 194.7 → 194.1 MiB | | pandas | 2.55 s | 2.35 s | 7.7% (5.6–9.7%) | 539.7 → 539.0 MiB | | PyTorch | 8.71 s | 8.24 s | 5.4% (1.9–9.8%) | 1,568.4 → 1,563.3 MiB | | Home Assistant | 15.69 s | 15.25 s | 2.8% (−3.2% to 8.1%) | 3,422.4 → 3,423.6 MiB |
1 parent 3933e69 commit 0445d64

3 files changed

Lines changed: 226 additions & 30 deletions

File tree

‎.github/workflows/build-binaries.yml‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ on:
2020
# And when we change this workflow itself...
2121
- .github/workflows/build-binaries.yml
2222
- scripts/build_ty_pgo.py
23+
- scripts/build_ty_bolt.py
2324

2425
concurrency:
2526
group: ${{ github.workflow }}-${{ github.ref }}
@@ -361,7 +362,10 @@ jobs:
361362
--train-only
362363
363364
PROFILE_HOT_COUNT="$(cat "${{ github.workspace }}/ruff/target/ty-pgo/ty.profile-hot-count")"
364-
echo "RUSTFLAGS=${RUSTFLAGS:+${RUSTFLAGS} }-Cprofile-use=${{ github.workspace }}/ruff/target/ty-pgo/ty.profdata -Cllvm-args=--profile-summary-hot-count=$PROFILE_HOT_COUNT" >> "$GITHUB_ENV"
365+
echo "RUSTFLAGS=${RUSTFLAGS:+${RUSTFLAGS} }-Cprofile-use=${{ github.workspace }}/ruff/target/ty-pgo/ty.profdata -Cllvm-args=--profile-summary-hot-count=$PROFILE_HOT_COUNT -Clink-arg=-Wl,--emit-relocs" >> "$GITHUB_ENV"
366+
echo "MATURIN_STRIP=false" >> "$GITHUB_ENV"
367+
# maturin-action restores ownership of this directory after the container exits.
368+
echo "CARGO_TARGET_DIR=$GITHUB_WORKSPACE/ruff/target" >> "$GITHUB_ENV"
365369
- name: "Prep README.md"
366370
run: python scripts/transform_readme.py
367371
- name: "Build wheels"
@@ -373,6 +377,30 @@ jobs:
373377
rust-toolchain: ${{ steps.pgo-toolchain.outputs.toolchain }}
374378
manylinux: 2_17
375379
args: --release --locked --out dist --compatibility pypi
380+
- name: "Optimize the Linux x86-64 executable and wheel with BOLT"
381+
if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }}
382+
run: |
383+
llvm_dir="$RUNNER_TEMP/ty-bolt"
384+
mkdir -p "$llvm_dir"
385+
(
386+
cd "$llvm_dir"
387+
version="22.1.8~++20260714014902+ca7933e47d3a-1~exp1~20260714135019.80"
388+
for package in bolt-22 libbolt-22-dev; do
389+
curl --fail --location --retry 3 -o "$package.deb" \
390+
"https://apt.llvm.org/noble/pool/main/l/llvm-toolchain-22/${package}_${version}_amd64.deb"
391+
done
392+
sha256sum --check <<'EOF'
393+
97a9e4e8d2b2219757c38abef93e913793ebc64580aa51207bd23941c44ec070 bolt-22.deb
394+
48639dafeb45892eec58f5d8a2d0d612ce791a2f713f06c794b71d6a72fa582d libbolt-22-dev.deb
395+
EOF
396+
dpkg-deb --extract bolt-22.deb .
397+
dpkg-deb --extract libbolt-22-dev.deb .
398+
)
399+
python scripts/build_ty_bolt.py \
400+
--llvm-dir "$llvm_dir/usr/lib/llvm-22" \
401+
--pgo-dir ruff/target/ty-pgo \
402+
--binary "ruff/target/${{ matrix.target }}/release/ty" \
403+
--wheel dist/ty-*.whl
376404
- name: "Test wheel"
377405
if: ${{ startsWith(matrix.target, 'x86_64') }}
378406
uses: astral-sh/github-actions/release-smoke-test@3994daa59cb9d97f991f8196a17218a2641fce4b

‎scripts/build_ty_bolt.py‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
"""Optimize the Linux x86-64 ty executable and wheel with LLVM BOLT."""
2+
3+
from __future__ import annotations
4+
5+
import argparse
6+
import base64
7+
import csv
8+
import hashlib
9+
import io
10+
import os
11+
import subprocess
12+
import tempfile
13+
import zipfile
14+
from pathlib import Path
15+
16+
from build_ty_pgo import (
17+
CORPUS_PROJECTS,
18+
EXCLUDED_ENVIRONMENT_VARIABLES,
19+
find_llvm_tool,
20+
rustc_host,
21+
training_command,
22+
)
23+
24+
25+
def replace_wheel_executable(wheel: Path, binary: Path) -> None:
26+
with tempfile.TemporaryDirectory(dir=wheel.parent) as directory:
27+
staged = Path(directory) / wheel.name
28+
with zipfile.ZipFile(wheel) as src, zipfile.ZipFile(staged, "w") as dst:
29+
(script,) = (n for n in src.namelist() if n.endswith(".data/scripts/ty"))
30+
(record,) = (n for n in src.namelist() if n.endswith(".dist-info/RECORD"))
31+
payload = binary.read_bytes()
32+
digest = base64.urlsafe_b64encode(hashlib.sha256(payload).digest()).rstrip(
33+
b"="
34+
)
35+
rows = list(csv.reader(io.StringIO(src.read(record).decode())))
36+
(script_row,) = (row for row in rows if row[0] == script)
37+
script_row[1:] = [f"sha256={digest.decode()}", str(len(payload))]
38+
updated = io.StringIO(newline="")
39+
csv.writer(updated, lineterminator="\n").writerows(rows)
40+
replacements = {script: payload, record: updated.getvalue().encode()}
41+
for entry in src.infolist():
42+
data = (
43+
replacements[entry.filename]
44+
if entry.filename in replacements
45+
else src.read(entry)
46+
)
47+
dst.writestr(entry, data)
48+
staged.replace(wheel)
49+
50+
51+
def main() -> None:
52+
parser = argparse.ArgumentParser(description=__doc__)
53+
parser.add_argument("--llvm-dir", required=True, type=Path)
54+
parser.add_argument("--pgo-dir", required=True, type=Path)
55+
parser.add_argument("--binary", required=True, type=Path)
56+
parser.add_argument("--wheel", required=True, type=Path)
57+
args = parser.parse_args()
58+
binary = args.binary.resolve()
59+
bolt = args.llvm_dir / "bin/llvm-bolt"
60+
pgo = args.pgo_dir.resolve()
61+
environment = {
62+
k: v for k, v in os.environ.items() if k not in EXCLUDED_ENVIRONMENT_VARIABLES
63+
}
64+
65+
with tempfile.TemporaryDirectory(dir=binary.parent) as directory:
66+
work = Path(directory)
67+
instrumented = work / "ty-instrumented"
68+
data = work / "ty.fdata"
69+
subprocess.run(
70+
[
71+
bolt,
72+
binary,
73+
"--instrument",
74+
"--thread-count=3",
75+
f"--instrumentation-file={data}",
76+
f"--runtime-instrumentation-lib={args.llvm_dir / 'lib/libbolt_rt_instr.a'}",
77+
"-o",
78+
instrumented,
79+
],
80+
check=True,
81+
)
82+
profiles = []
83+
for project in CORPUS_PROJECTS:
84+
checkout = pgo / "corpus" / project.name
85+
python = pgo / "environments" / project.name / "bin/python"
86+
print(f"Profiling ty on {project.name}", flush=True)
87+
subprocess.run(
88+
training_command(instrumented, project, checkout, python),
89+
cwd=checkout,
90+
env=environment,
91+
check=True,
92+
)
93+
profiles.append(data.rename(work / f"{project.name}.fdata"))
94+
with data.open("wb") as stream:
95+
subprocess.run(
96+
[args.llvm_dir / "bin/merge-fdata", "-q", *profiles],
97+
stdout=stream,
98+
check=True,
99+
)
100+
101+
optimized = work / "ty-optimized"
102+
103+
def rewrite(*split_options: str) -> str:
104+
result = subprocess.run(
105+
[
106+
bolt,
107+
binary,
108+
f"--data={data}",
109+
"--reorder-blocks=ext-tsp",
110+
"--reorder-functions=cdsort",
111+
*split_options,
112+
"--jump-tables=move",
113+
"--icf=all",
114+
"--align-functions=1",
115+
"--align-functions-max-bytes=0",
116+
"--align-blocks=false",
117+
"--use-old-text",
118+
"--no-huge-pages",
119+
"--hugify=false",
120+
"--thread-count=3",
121+
"-o",
122+
optimized,
123+
],
124+
stdout=subprocess.PIPE,
125+
stderr=subprocess.STDOUT,
126+
text=True,
127+
)
128+
print(result.stdout, flush=True)
129+
result.check_returncode()
130+
return result.stdout
131+
132+
log = rewrite(
133+
"--split-functions", "--split-all-cold", "--split-strategy=cdsplit"
134+
)
135+
if "--use-old-text failed. The original .text too small" in log:
136+
print(
137+
"Retrying without function splitting to fit the original .text.",
138+
flush=True,
139+
)
140+
log = rewrite("--split-functions=false")
141+
# BOLT appends a second copy of the code if it outgrows the original text segment.
142+
if "using original .text for new code" not in log:
143+
print(
144+
"Keeping the compiler PGO layout because BOLT did not reuse the original .text.",
145+
flush=True,
146+
)
147+
optimized = binary
148+
149+
stripped = work / "ty"
150+
# GNU strip can move BOLT's unwind sections out of their load segment.
151+
strip = find_llvm_tool("llvm-strip", rustc_host())
152+
subprocess.run([strip, "--strip-all", "-o", stripped, optimized], check=True)
153+
subprocess.run([stripped, "--version"], check=True)
154+
replace_wheel_executable(args.wheel, stripped)
155+
stripped.replace(binary)
156+
157+
158+
if __name__ == "__main__":
159+
main()

‎scripts/build_ty_pgo.py‎

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def main() -> None:
227227
f"PGO training requires the host-native target {host}, got {target}"
228228
)
229229

230-
profiler = find_llvm_profdata(host, args.llvm_profdata)
230+
profiler = find_llvm_tool("llvm-profdata", host, args.llvm_profdata)
231231
corpus = ecosystem_python_files(target_dir / "corpus", environment=environment)
232232
project_environments = prepare_project_environments(
233233
target_dir / "environments", target_dir / "corpus", environment=environment
@@ -307,7 +307,6 @@ def train_ty(
307307
print(f"Training on {corpus_size} ecosystem Python files", flush=True)
308308
profiles = []
309309
for project in CORPUS_PROJECTS:
310-
checkout = corpus_directory / project.name
311310
training_environment = environment | {
312311
"LLVM_PROFILE_FILE": str(
313312
profile_directory / f"ty-{project.name}-%m-%p.profraw"
@@ -318,25 +317,12 @@ def train_ty(
318317

319318
print(f"Profiling ty on {project.name}", flush=True)
320319
run(
321-
[
322-
str(binary),
323-
"check",
324-
"--python",
325-
str(project_environments[project.name]),
326-
"--project",
327-
str(checkout),
328-
"--python-version",
329-
project.python_version,
330-
*(
331-
argument
332-
for directory in sorted(EXCLUDED_DIRECTORIES)
333-
for argument in ("--exclude", f"{directory}/")
334-
),
335-
"--exit-zero",
336-
"--no-progress",
337-
"-qq",
338-
*(str(checkout / source) for source in project.source_directories),
339-
],
320+
training_command(
321+
binary,
322+
project,
323+
corpus_directory / project.name,
324+
project_environments[project.name],
325+
),
340326
environment=training_environment,
341327
)
342328

@@ -367,6 +353,30 @@ def train_ty(
367353
return profiles
368354

369355

356+
def training_command(
357+
binary: Path, project: EcosystemProject, checkout: Path, python: Path
358+
) -> list[str]:
359+
return [
360+
str(binary),
361+
"check",
362+
"--python",
363+
str(python),
364+
"--project",
365+
str(checkout),
366+
"--python-version",
367+
project.python_version,
368+
*(
369+
argument
370+
for directory in sorted(EXCLUDED_DIRECTORIES)
371+
for argument in ("--exclude", f"{directory}/")
372+
),
373+
"--exit-zero",
374+
"--no-progress",
375+
"-qq",
376+
*(str(checkout / source) for source in project.source_directories),
377+
]
378+
379+
370380
def prepare_project_environments(
371381
environment_directory: Path,
372382
corpus_directory: Path,
@@ -724,9 +734,9 @@ def rustc_host() -> str:
724734
raise RuntimeError("Could not determine the active Rust compiler's host target")
725735

726736

727-
def find_llvm_profdata(host: str, override: Path | None) -> Path:
737+
def find_llvm_tool(name: str, host: str, override: Path | None = None) -> Path:
728738
if override is not None:
729-
profiler = override.resolve()
739+
tool = override.resolve()
730740
else:
731741
sysroot = subprocess.run(
732742
["rustc", "--print", "sysroot"],
@@ -735,15 +745,14 @@ def find_llvm_profdata(host: str, override: Path | None) -> Path:
735745
capture_output=True,
736746
text=True,
737747
).stdout.strip()
738-
binary_name = "llvm-profdata.exe" if "windows" in host else "llvm-profdata"
739-
profiler = Path(sysroot) / "lib" / "rustlib" / host / "bin" / binary_name
748+
binary_name = f"{name}.exe" if "windows" in host else name
749+
tool = Path(sysroot) / "lib" / "rustlib" / host / "bin" / binary_name
740750

741-
if not profiler.is_file() or not os.access(profiler, os.X_OK):
751+
if not tool.is_file() or not os.access(tool, os.X_OK):
742752
raise RuntimeError(
743-
f"Rust toolchain llvm-profdata not found: {profiler}; "
744-
"run `rustup component add llvm-tools-preview`"
753+
f"Could not find {name} at {tool}. Run `rustup component add llvm-tools-preview`."
745754
)
746-
return profiler
755+
return tool
747756

748757

749758
def ecosystem_python_files(

0 commit comments

Comments
 (0)