//! 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 { 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); }