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 --- targets/instr_sort.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 targets/instr_sort.c (limited to 'targets/instr_sort.c') diff --git a/targets/instr_sort.c b/targets/instr_sort.c new file mode 100644 index 0000000..411b348 --- /dev/null +++ b/targets/instr_sort.c @@ -0,0 +1,44 @@ +// Bubble sort and insertion sort of the same eight numbers, argv[1] times +// (by default once). Exits with the smallest plus the largest. +#include +#include + +#define N 8 +// Not const, not static: -O2 must sort at run time, not when compiling. +int unsorted[N] = {5, 2, 9, 1, 7, 3, 8, 4}; + +static void bubble(int *a, int n) { + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < n - 1 - i; j++) { + if (a[j] > a[j + 1]) { + int t = a[j]; + a[j] = a[j + 1]; + a[j + 1] = t; + } + } + } +} + +static void insertion(int *a, int n) { + for (int i = 1; i < n; i++) { + int key = a[i]; + int j = i - 1; + while (j >= 0 && a[j] > key) { + a[j + 1] = a[j]; + j--; + } + a[j + 1] = key; + } +} + +int main(int argc, char **argv) { + int rounds = argc > 1 ? atoi(argv[1]) : 1; + int bubbled[N], inserted[N]; + while (rounds-- > 0) { + memcpy(bubbled, unsorted, sizeof unsorted); + memcpy(inserted, unsorted, sizeof unsorted); + bubble(bubbled, N); + insertion(inserted, N); + } + return bubbled[0] + inserted[N - 1]; +} -- cgit v1.3