summaryrefslogtreecommitdiff
path: root/llvm/bench.sh
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 11:32:21 -0300
committerGabriel Schneider <[email protected]>2026-11-10 12:08:27 -0300
commitb426a8a99bcad81ecbb41d1f2a2e83738c29d8e1 (patch)
tree06e303f1f33ce53cfc2d8a2043e24f032877f9ad /llvm/bench.sh
parent7f363779cb99b0820cce69c433bcfab87bb3d709 (diff)
downloadcomputeranim-b426a8a99bcad81ecbb41d1f2a2e83738c29d8e1.tar.gz
computeranim-b426a8a99bcad81ecbb41d1f2a2e83738c29d8e1.zip
llvm: a pass that instruments for animation, its runtime, ztrace cc
llvm/pass.cpp is an LLVM pass plugin: lines, stores, loads, calls and variables, each call into the runtime carrying names and type descriptions, selected by globs (-ztrace-only, -ztrace-mem), before the optimizer by default. src/instr/rt.zig is the runtime (libztrace-rt.a), which also answers clang's own coverage and -finstrument-functions hooks; src/instr/types.zig the grammar of the type descriptions and its parser. ztrace cc adds all of it to a compiler command. test-llvm compiles fib, two sorts, a linked list and a BFS through ztrace cc and asserts on their recordings; bench-llvm measures what each selection costs. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'llvm/bench.sh')
-rw-r--r--llvm/bench.sh57
1 files changed, 57 insertions, 0 deletions
diff --git a/llvm/bench.sh b/llvm/bench.sh
new file mode 100644
index 0000000..00edc88
--- /dev/null
+++ b/llvm/bench.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+# What compiler instrumentation costs: zig build bench-llvm -Doptimize=ReleaseFast.
+# Arguments: ztrace, the bench tool (src/instr/bench.zig), the plugin, the
+# runtime, the targets directory.
+set -eu
+ztrace=$1 bench=$2 plugin=$3 rt=$4 targets=$5
+tmp=$(mktemp -d)
+trap 'rm -rf "$tmp"' EXIT
+
+# target : argument when traced : argument when not : how many times more
+# work the second is. With no tracer a target runs too briefly to time at
+# the first.
+workloads="fib:24:35:199.006 sort:2000:400000:200 list:5000:1000000:200 bfs:3000:600000:200"
+selections="lines stores loads calls vars lines,calls,vars lines,calls,vars,stores edges cmps cyg"
+
+# The best of three runs: milliseconds, then whatever else the tool printed.
+best() {
+ for _ in 1 2 3; do "$bench" "$@"; done | sort -n | head -1
+}
+
+cat <<'EOF2'
+Per selection of `ztrace cc --instr`:
+ events emitted at the traced workload
+ idle time against the uninstrumented program, with no tracer
+ ring the same into a ring drained and thrown away
+ ns nanoseconds an event, into that ring
+ B/event bytes of recording an event (`ztrace run`, a moment a line)
+ B/line bytes of recording a source line executed
+ rec/s events a second into the recording
+EOF2
+for opt in -O0 -O2; do
+ for w in $workloads; do
+ IFS=: read -r name arg long scale <<EOF2
+$w
+EOF2
+ clang $opt "$targets/instr_$name.c" -o "$tmp/plain"
+ start=$(best --untraced "$tmp/plain" 0 | cut -d' ' -f1)
+ alone=$(best --untraced "$tmp/plain" "$long" | awk -v start="$start" '{ print $1 - start }')
+ printf '\n%s %s: uninstrumented, %.1f ms at %s; traced at %s\n' "$name" "$opt" "$alone" "$long" "$arg"
+ printf ' %-24s %9s %7s %9s %6s %8s %7s %9s\n' selection events idle ring ns B/event B/line rec/s
+ for s in $selections; do
+ "$ztrace" cc --instr "$s" --plugin "$plugin" --rt "$rt" -- clang $opt "$targets/instr_$name.c" -o "$tmp/instr"
+ idle=$(best --untraced "$tmp/instr" "$long" | cut -d' ' -f1)
+ traced=$(best "$tmp/instr" "$arg")
+ # The lines the workload executes, whatever is selected: counted once.
+ [ "$s" = lines ] && lines=$(echo "$traced" | sed 's/.* line=\([0-9]*\).*/\1/')
+ recorded=$("$ztrace" run -o "$tmp/r.ztrace" -- "$tmp/instr" "$arg" 2>&1 | sed 's/.* \([0-9]*\) bytes.* \([0-9]*\) events\/s.*/\1 \2/')
+ echo "$traced $recorded" | awk -v s="$s" -v start="$start" -v alone="$alone" -v scale="$scale" -v idle="$idle" -v lines="$lines" \
+ '{ printf " %-24s %9d %6.1fx %8.1fx %6.1f %8.1f %7.1f %9d\n", s, $2, (idle - start) / alone, ($1 - start) * scale / alone, ($1 - start - alone / scale) * 1e6 / $2, $(NF - 1) / $2, $(NF - 1) / lines, $NF }'
+ done
+ done
+done
+
+"$ztrace" cc --plugin "$plugin" --rt "$rt" -- clang -O0 "$targets/instr_sort.c" -o "$tmp/sort"
+echo
+echo "sort 2000 -O0, held at each line by the stop mask and resumed at once:"
+best --step "$tmp/sort" 2000 | awk '{ ms = $1; sub(/.* line=/, ""); printf " %d stops in %.0f ms: %.0f a second\n", $1, ms, $1 / ms * 1000 }'