diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 11:32:21 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 12:08:27 -0300 |
| commit | b426a8a99bcad81ecbb41d1f2a2e83738c29d8e1 (patch) | |
| tree | 06e303f1f33ce53cfc2d8a2043e24f032877f9ad /targets | |
| parent | 7f363779cb99b0820cce69c433bcfab87bb3d709 (diff) | |
| download | computeranim-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 'targets')
| -rw-r--r-- | targets/instr_bfs.c | 39 | ||||
| -rw-r--r-- | targets/instr_big.c | 11 | ||||
| -rw-r--r-- | targets/instr_fib.c | 12 | ||||
| -rw-r--r-- | targets/instr_list.c | 46 | ||||
| -rw-r--r-- | targets/instr_own.c | 27 | ||||
| -rw-r--r-- | targets/instr_sort.c | 44 |
6 files changed, 179 insertions, 0 deletions
diff --git a/targets/instr_bfs.c b/targets/instr_bfs.c new file mode 100644 index 0000000..a7b176e --- /dev/null +++ b/targets/instr_bfs.c @@ -0,0 +1,39 @@ +// Breadth-first search from vertex 0 of a seven-vertex graph, one vertex +// out of reach, argv[1] times (by default once). Exits with the number of +// vertices reached. +#include <stdlib.h> + +#define N 7 +static const int edges[][2] = {{0, 1}, {0, 2}, {1, 3}, {2, 3}, {3, 4}, {4, 5}}; +static int adjacent[N][N]; +static int distance[N]; +static int order[N]; + +static int bfs(int from) { + int queue[N], head = 0, tail = 0; + for (int v = 0; v < N; v++) distance[v] = -1; + distance[from] = 0; + queue[tail++] = from; + while (head < tail) { + int v = queue[head]; + order[head++] = v; + for (int w = 0; w < N; w++) { + if (adjacent[v][w] && distance[w] < 0) { + distance[w] = distance[v] + 1; + queue[tail++] = w; + } + } + } + return tail; +} + +int main(int argc, char **argv) { + int rounds = argc > 1 ? atoi(argv[1]) : 1; + int reached = 0; + for (unsigned i = 0; i < sizeof edges / sizeof *edges; i++) { + adjacent[edges[i][0]][edges[i][1]] = 1; + adjacent[edges[i][1]][edges[i][0]] = 1; + } + while (rounds-- > 0) reached = bfs(0); + return reached; +} diff --git a/targets/instr_big.c b/targets/instr_big.c new file mode 100644 index 0000000..ce89947 --- /dev/null +++ b/targets/instr_big.c @@ -0,0 +1,11 @@ +// A megabyte set, then copied: a recording keeps the first 64 KiB of each +// write. Exits with the last byte copied. +#include <string.h> + +static char from[1 << 20], to[1 << 20]; + +int main(void) { + memset(from, 7, sizeof from); + memcpy(to, from, sizeof to); + return to[sizeof to - 1]; +} diff --git a/targets/instr_fib.c b/targets/instr_fib.c new file mode 100644 index 0000000..9da9306 --- /dev/null +++ b/targets/instr_fib.c @@ -0,0 +1,12 @@ +// fib(n) the slow way: a call tree to draw. Exits with fib(argv[1]), by +// default fib(6). +#include <stdlib.h> + +static unsigned fib(unsigned n) { + if (n < 2) return n; + return fib(n - 1) + fib(n - 2); +} + +int main(int argc, char **argv) { + return (int)fib(argc > 1 ? (unsigned)atoi(argv[1]) : 6); +} diff --git a/targets/instr_list.c b/targets/instr_list.c new file mode 100644 index 0000000..aaa6c8a --- /dev/null +++ b/targets/instr_list.c @@ -0,0 +1,46 @@ +// A linked list of five squares, built at its head and then reversed in +// place, argv[1] times (by default once). The last list is left standing. +// Exits with the sum of its values. +#include <stdlib.h> + +struct node { + int value; + struct node *next; +}; + +static struct node *list; + +static struct node *push(struct node *head, int value) { + struct node *n = malloc(sizeof *n); + n->value = value; + n->next = head; + return n; +} + +static struct node *reverse(struct node *head) { + struct node *prev = NULL; + while (head) { + struct node *next = head->next; + head->next = prev; + prev = head; + head = next; + } + return prev; +} + +int main(int argc, char **argv) { + int rounds = argc > 1 ? atoi(argv[1]) : 1; + int sum = 0; + while (rounds-- > 0) { + while (list) { + struct node *next = list->next; + free(list); + list = next; + } + for (int i = 1; i <= 5; i++) list = push(list, i * i); + list = reverse(list); + sum = 0; + for (struct node *n = list; n; n = n->next) sum += n->value; + } + return sum; +} diff --git a/targets/instr_own.c b/targets/instr_own.c new file mode 100644 index 0000000..5c30141 --- /dev/null +++ b/targets/instr_own.c @@ -0,0 +1,27 @@ +// A program with its own memcpy and strlen, as one linked with a libc +// compiled by the pass has: the runtime calls them too, and must not trace +// itself. Exits with the length of the string it copied. +#include <stddef.h> + +void *memcpy(void *to, const void *from, size_t n) { + char *d = to; + const char *s = from; + for (size_t i = 0; i < n; i++) d[i] = s[i]; + return to; +} + +size_t strlen(const char *s) { + size_t n = 0; + while (s[n]) n++; + return n; +} + +// Through pointers: the compiler would do a known memcpy inline. +void *(*volatile copy)(void *, const void *, size_t) = memcpy; +size_t (*volatile length)(const char *) = strlen; + +int main(void) { + char to[8], from[8] = "abcdefg"; + copy(to, from, sizeof from); + return (int)length(to); +} 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 <stdlib.h> +#include <string.h> + +#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]; +} |
