#!/usr/bin/env bash # PGO for the fast runtime archive (issue #193, step 2): builds # liblouise_rt instrumented, trains it on the benchmarks, rebuilds with # the profile, and leaves a directory with the result as # liblouise_rt-fast.a. Link it with: # # LOUISE_LIB_DIR=target/pgo/use LOUISE_RUNTIME=fast louise build --release … # # The recipe's traps, found the hard way: # - the *link* needs the driver's `-fprofile-generate` flag, not just the # archive: without it the profile writer is never registered and no # .profraw is written (any linker, with or without --gc-sections); # - the instrumented archive must also be on the link line (it is, as the # fast archive); # - PGO applies to the `fast` (machine-code) archive; with ThinLTO the # runtime is re-laid-out at the program's link, where this profile does # not reach. # # Measured 2026-10-15 (best of 5-8, pinned, interleaved; instructions in # parentheses): fanin -25% (-22%), selective -17% (-18%), records -8% # (-11%), html ~+2% (careful re-measure; a first pass said +14%, which # was run-order noise), the compute benchmarks ~0%. Trained on # tests/bench. set -euo pipefail cd "$(dirname "$0")/.." out=target/pgo raw="$PWD/$out/profraw" rm -rf "$out" mkdir -p "$raw" "$out/bin" "$out/use" echo "== instrumented runtime" >&2 RUSTFLAGS="-Cprofile-generate=$raw" \ cargo build --quiet --profile runtime -p louise_rt --no-default-features --features mimalloc,tls,crypto \ --target-dir "$out/gen" cp "$out/gen/runtime/liblouise_rt.a" "$out/use/liblouise_rt-fast.a" echo "== instrumented benchmarks" >&2 # The link needs the driver's flag (see the header); the training builds' # own C is instrumented too, harmlessly. for program in tests/bench/*.lse; do name="$(basename "$program" .lse)" LOUISE_LIB_DIR="$PWD/$out/use" LOUISE_RUNTIME=fast \ LOUISE_CFLAGS="-fprofile-generate=$raw" \ ./target/dist/louise build --release "$program" -o "$out/bin/$name" >/dev/null done echo "== training run" >&2 for bin in "$out"/bin/*; do [ -f "$bin" ] && [ -x "$bin" ] || continue "$bin" >/dev/null done [ -z "$(ls -A "$raw" 2>/dev/null)" ] && { echo "pgo-runtime: no profiles written (see the header's traps)" >&2; exit 1; } llvm-profdata merge -o "$out/rt.profdata" "$raw"/*.profraw echo "== runtime with the profile" >&2 RUSTFLAGS="-Cprofile-use=$PWD/$out/rt.profdata" \ cargo build --quiet --profile runtime -p louise_rt --no-default-features --features mimalloc,tls,crypto \ --target-dir "$out/final" cp "$out/final/runtime/liblouise_rt.a" "$out/use/liblouise_rt-fast.a" echo "pgo-runtime: $out/use/liblouise_rt-fast.a" >&2