summaryrefslogtreecommitdiff
path: root/build/llvm.zig
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 /build/llvm.zig
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 'build/llvm.zig')
-rw-r--r--build/llvm.zig95
1 files changed, 94 insertions, 1 deletions
diff --git a/build/llvm.zig b/build/llvm.zig
index d3c7eb3..a3e93cc 100644
--- a/build/llvm.zig
+++ b/build/llvm.zig
@@ -1,5 +1,98 @@
+//! Compiler instrumentation: the pass plugin (C++, built with the system
+//! clang++ against the LLVM that will load it), the runtime instrumented
+//! programs link, and tests on targets compiled through `ztrace cc`.
+const std = @import("std");
const Ctx = @import("../build.zig").Ctx;
pub fn add(ctx: Ctx) void {
- _ = ctx;
+ const b = ctx.b;
+
+ // The runtime runs inside the target at every event: optimized whatever
+ // mode the rest is built in.
+ const fast = b.createModule(.{ .root_source_file = b.path("src/trace/root.zig"), .target = ctx.target, .optimize = .ReleaseFast, .pic = true });
+ const rt = b.addLibrary(.{ .name = "ztrace-rt", .root_module = b.createModule(.{
+ .root_source_file = b.path("src/instr/rt.zig"),
+ .target = ctx.target,
+ .optimize = .ReleaseFast,
+ .pic = true,
+ .imports = &.{.{ .name = "ztrace", .module = fast }},
+ }) });
+ b.installArtifact(rt);
+
+ const cxx = b.addSystemCommand(&.{ "sh", "-c", "exec clang++ -shared -fPIC -O2 -fno-rtti $(llvm-config --cxxflags) \"$0\" -o \"$1\"" });
+ cxx.addFileArg(b.path("llvm/pass.cpp"));
+ const plugin = cxx.addOutputFileArg("ztrace-pass.so");
+ b.getInstallStep().dependOn(&b.addInstallLibFile(plugin, "ztrace-pass.so").step);
+
+ // For whoever draws values: the parser of type descriptions.
+ const types = b.addModule("ztrace-types", .{ .root_source_file = b.path("src/instr/types.zig"), .target = ctx.target, .optimize = ctx.optimize });
+
+ // The tests read recordings of the targets, each compiled by `ztrace cc`
+ // and run by `ztrace run`.
+ const options = b.addOptions();
+ // A recording by name: its source, how clang optimizes it (-O2 is the
+ // optimizer going over what the pass put in), what `ztrace cc` is told.
+ const Target = struct { []const u8, []const u8, []const u8, []const []const u8 };
+ for ([_]Target{
+ .{ "fib", "fib", "-O0", &.{} },
+ .{ "sort", "sort", "-O0", &.{} },
+ .{ "sort_O2", "sort", "-O2", &.{} },
+ .{ "sort_only", "sort", "-O0", &.{ "--only", "bub*" } },
+ .{ "sort_mem", "sort", "-O0", &.{ "--only", "instr_sort.c", "--mem", "insertion" } },
+ .{ "list", "list", "-O0", &.{} },
+ .{ "bfs", "bfs", "-O0", &.{} },
+ .{ "own", "own", "-O0", &.{} },
+ .{ "big", "big", "-O0", &.{} },
+ }) |target| {
+ const name, const source, const opt, const selection = target;
+ const cc = b.addRunArtifact(ctx.exe);
+ cc.addArg("cc");
+ cc.addArgs(selection);
+ cc.addArg("--plugin");
+ cc.addFileArg(plugin);
+ cc.addArg("--rt");
+ cc.addArtifactArg(rt);
+ cc.addArgs(&.{ "--", "clang", opt });
+ cc.addFileArg(b.path(b.fmt("targets/instr_{s}.c", .{source})));
+ cc.addArg("-o");
+ const program = cc.addOutputFileArg(b.fmt("instr_{s}", .{name}));
+ options.addOptionPath(b.fmt("{s}_program", .{name}), program);
+
+ const run = b.addRunArtifact(ctx.exe);
+ run.addArgs(&.{ "run", "-o" });
+ options.addOptionPath(name, run.addOutputFileArg(b.fmt("{s}.ztrace", .{name})));
+ run.addArg("--");
+ run.addFileArg(program);
+ run.expectStdErrMatch("(0 dropped)");
+ }
+
+ const step = ctx.areaTest("test-llvm", "Compiler instrumentation: the pass, the runtime, ztrace cc");
+ const tests = b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("src/instr/test.zig"),
+ .target = ctx.target,
+ .optimize = ctx.optimize,
+ .imports = &.{
+ .{ .name = "ztrace", .module = ctx.ztrace },
+ .{ .name = "types", .module = types },
+ .{ .name = "recordings", .module = options.createModule() },
+ },
+ }) });
+ step.dependOn(&b.addRunArtifact(tests).step);
+ step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = types })).step);
+
+ // zig build bench-llvm -Doptimize=ReleaseFast: what each kind of
+ // instrumentation costs each target (llvm/bench.sh).
+ const bench = b.addSystemCommand(&.{"sh"});
+ bench.addFileArg(b.path("llvm/bench.sh"));
+ bench.addArtifactArg(ctx.exe);
+ bench.addArtifactArg(b.addExecutable(.{ .name = "bench", .root_module = b.createModule(.{
+ .root_source_file = b.path("src/instr/bench.zig"),
+ .target = ctx.target,
+ .optimize = .ReleaseFast,
+ .imports = &.{.{ .name = "ztrace", .module = fast }},
+ }) }));
+ bench.addFileArg(plugin);
+ bench.addArtifactArg(rt);
+ bench.addDirectoryArg(b.path("targets"));
+ b.step("bench-llvm", "Measure the cost of compiler instrumentation").dependOn(&bench.step);
}