authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-11-28 16:17:02+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-12 17:07:50+02:00
loge63e3f7a7cabb10e86e94682835fe99669a404bf
tree93b5c203d5c19fcfd67ccc72a49b6e23454fef04
parentfe5dbc247430cc0b7bce4fe7d01ef6c425db0bf2
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

test: Add test-llvm-ir step and harness for testing generated LLVM IR.


4 files changed, 164 insertions(+), 0 deletions(-)

build.zig+5
...@@ -550,6 +550,11 @@ pub fn build(b: *std.Build) !void {...@@ -550,6 +550,11 @@ pub fn build(b: *std.Build) !void {
550 .skip_non_native = skip_non_native,550 .skip_non_native = skip_non_native,
551 .skip_libc = skip_libc,551 .skip_libc = skip_libc,
552 })) |test_debugger_step| test_step.dependOn(test_debugger_step);552 })) |test_debugger_step| test_step.dependOn(test_debugger_step);
553 if (tests.addLlvmIrTests(b, .{
554 .enable_llvm = enable_llvm,
555 .test_filters = test_filters,
556 .test_target_filters = test_target_filters,
557 })) |test_llvm_ir_step| test_step.dependOn(test_llvm_ir_step);
553558
554 try addWasiUpdateStep(b, version);559 try addWasiUpdateStep(b, version);
555560
test/llvm_ir.zig created+6
...@@ -0,0 +1,6 @@
1pub fn addCases(cases: *tests.LlvmIrContext) void {
2 _ = cases;
3}
4
5const std = @import("std");
6const tests = @import("tests.zig");
test/src/LlvmIr.zig created+132
...@@ -0,0 +1,132 @@
1b: *std.Build,
2options: Options,
3root_step: *std.Build.Step,
4
5pub const Options = struct {
6 enable_llvm: bool,
7 test_filters: []const []const u8,
8 test_target_filters: []const []const u8,
9};
10
11const TestCase = struct {
12 name: []const u8,
13 source: []const u8,
14 check: union(enum) {
15 matches: []const []const u8,
16 exact: []const u8,
17 },
18 params: Params,
19
20 pub const Params = struct {
21 code_model: std.builtin.CodeModel = .default,
22 dll_export_fns: ?bool = null,
23 dwarf_format: ?std.dwarf.Format = null,
24 error_tracing: ?bool = null,
25 no_builtin: ?bool = null,
26 omit_frame_pointer: ?bool = null,
27 // For most cases, we want to test the LLVM IR that we output; we don't want to be in the
28 // business of testing LLVM's optimization passes. `Debug` gets us the closest to that as it
29 // disables the vast majority of passes in LLVM.
30 optimize: std.builtin.OptimizeMode = .Debug,
31 pic: ?bool = null,
32 pie: ?bool = null,
33 red_zone: ?bool = null,
34 sanitize_thread: ?bool = null,
35 single_threaded: ?bool = null,
36 stack_check: ?bool = null,
37 stack_protector: ?bool = null,
38 strip: ?bool = null,
39 target: std.Target.Query = .{},
40 unwind_tables: ?std.builtin.UnwindTables = null,
41 valgrind: ?bool = null,
42 };
43};
44
45pub fn addMatches(
46 self: *LlvmIr,
47 name: []const u8,
48 source: []const u8,
49 matches: []const []const u8,
50 params: TestCase.Params,
51) void {
52 self.addCase(.{
53 .name = name,
54 .source = source,
55 .check = .{ .matches = matches },
56 .params = params,
57 });
58}
59
60pub fn addExact(
61 self: *LlvmIr,
62 name: []const u8,
63 source: []const u8,
64 expected: []const []const u8,
65 params: TestCase.Params,
66) void {
67 self.addCase(.{
68 .name = name,
69 .source = source,
70 .check = .{ .exact = expected },
71 .params = params,
72 });
73}
74
75pub fn addCase(self: *LlvmIr, case: TestCase) void {
76 const target = self.b.resolveTargetQuery(case.params.target);
77 if (self.options.test_target_filters.len > 0) {
78 const triple_txt = target.result.zigTriple(self.b.allocator) catch @panic("OOM");
79 for (self.options.test_target_filters) |filter| {
80 if (std.mem.indexOf(u8, triple_txt, filter) != null) break;
81 } else return;
82 }
83
84 const name = std.fmt.allocPrint(self.b.allocator, "check llvm-ir {s}", .{case.name}) catch @panic("OOM");
85 if (self.options.test_filters.len > 0) {
86 for (self.options.test_filters) |filter| {
87 if (std.mem.indexOf(u8, name, filter) != null) break;
88 } else return;
89 }
90
91 const obj = self.b.addObject(.{
92 .name = "test",
93 .root_source_file = self.b.addWriteFiles().add("test.zig", case.source),
94 .use_llvm = true,
95
96 .code_model = case.params.code_model,
97 .error_tracing = case.params.error_tracing,
98 .omit_frame_pointer = case.params.omit_frame_pointer,
99 .optimize = case.params.optimize,
100 .pic = case.params.pic,
101 .sanitize_thread = case.params.sanitize_thread,
102 .single_threaded = case.params.single_threaded,
103 .strip = case.params.strip,
104 .target = target,
105 .unwind_tables = case.params.unwind_tables,
106 });
107
108 obj.dll_export_fns = case.params.dll_export_fns;
109 obj.pie = case.params.pie;
110 obj.no_builtin = case.params.no_builtin;
111
112 obj.root_module.dwarf_format = case.params.dwarf_format;
113 obj.root_module.red_zone = case.params.red_zone;
114 obj.root_module.stack_check = case.params.stack_check;
115 obj.root_module.stack_protector = case.params.stack_protector;
116 obj.root_module.valgrind = case.params.valgrind;
117
118 // This is not very sophisticated at the moment. Eventually, we should move towards something
119 // like LLVM's `FileCheck` utility (https://llvm.org/docs/CommandGuide/FileCheck.html), though
120 // likely a more simplified version as we probably don't want a full-blown regex engine in the
121 // standard library...
122 const check = self.b.addCheckFile(obj.getEmittedLlvmIr(), switch (case.check) {
123 .matches => |m| .{ .expected_matches = m },
124 .exact => |e| .{ .expected_exact = e },
125 });
126 check.setName(name);
127
128 self.root_step.dependOn(&check.step);
129}
130
131const LlvmIr = @This();
132const std = @import("std");
test/tests.zig+21
...@@ -11,6 +11,7 @@ const stack_traces = @import("stack_traces.zig");...@@ -11,6 +11,7 @@ const stack_traces = @import("stack_traces.zig");
11const assemble_and_link = @import("assemble_and_link.zig");11const assemble_and_link = @import("assemble_and_link.zig");
12const translate_c = @import("translate_c.zig");12const translate_c = @import("translate_c.zig");
13const run_translated_c = @import("run_translated_c.zig");13const run_translated_c = @import("run_translated_c.zig");
14const llvm_ir = @import("llvm_ir.zig");
1415
15// Implementations16// Implementations
16pub const TranslateCContext = @import("src/TranslateC.zig");17pub const TranslateCContext = @import("src/TranslateC.zig");
...@@ -18,6 +19,7 @@ pub const RunTranslatedCContext = @import("src/RunTranslatedC.zig");...@@ -18,6 +19,7 @@ pub const RunTranslatedCContext = @import("src/RunTranslatedC.zig");
18pub const CompareOutputContext = @import("src/CompareOutput.zig");19pub const CompareOutputContext = @import("src/CompareOutput.zig");
19pub const StackTracesContext = @import("src/StackTrace.zig");20pub const StackTracesContext = @import("src/StackTrace.zig");
20pub const DebuggerContext = @import("src/Debugger.zig");21pub const DebuggerContext = @import("src/Debugger.zig");
22pub const LlvmIrContext = @import("src/LlvmIr.zig");
2123
22const TestTarget = struct {24const TestTarget = struct {
23 linkage: ?std.builtin.LinkMode = null,25 linkage: ?std.builtin.LinkMode = null,
...@@ -2125,3 +2127,22 @@ pub fn addIncrementalTests(b: *std.Build, test_step: *Step) !void {...@@ -2125,3 +2127,22 @@ pub fn addIncrementalTests(b: *std.Build, test_step: *Step) !void {
2125 test_step.dependOn(&run.step);2127 test_step.dependOn(&run.step);
2126 }2128 }
2127}2129}
2130
2131pub fn addLlvmIrTests(b: *std.Build, options: LlvmIrContext.Options) ?*Step {
2132 const step = b.step("test-llvm-ir", "Run the LLVM IR tests");
2133
2134 if (!options.enable_llvm) {
2135 step.dependOn(&b.addFail("test-llvm-ir requires -Denable-llvm").step);
2136 return null;
2137 }
2138
2139 var context: LlvmIrContext = .{
2140 .b = b,
2141 .options = options,
2142 .root_step = step,
2143 };
2144
2145 llvm_ir.addCases(&context);
2146
2147 return step;
2148}