From b426a8a99bcad81ecbb41d1f2a2e83738c29d8e1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:32:21 -0300 Subject: 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 --- llvm/bench.sh | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 llvm/bench.sh (limited to 'llvm/bench.sh') 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 <&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 }' -- cgit v1.3