1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
//! 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;
/// Returns the instrumented list program, for what tests it with other backends.
pub fn add(ctx: Ctx) std.Build.LazyPath {
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 };
var list: std.Build.LazyPath = undefined;
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);
if (std.mem.eql(u8, name, "list")) list = 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);
return list;
}
|