authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-27 16:40:59+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-27 16:43:53+02:00
log9f59189c954298fd2be576b7e3dd26388d07751d
treeff21bd4c7d054562c2823a306c8c9269b7c51289
parent7a92b89a9d19f3e6d258c848f1be13a6e31fec97

stage2: do not memoize calls that can mutate comptime state


4 files changed, 13 insertions(+), 24 deletions(-)

lib/std/Progress.zig+1-6
......@@ -269,12 +269,7 @@ fn refreshWithHeldLock(self: *Progress) void {
269269 }
270270 if (eti > 0) {
271271 if (need_ellipse) self.bufWrite(&end, " ", .{});
272 if (builtin.zig_backend == .stage2_llvm) {
273 self.bufWrite(&end, "[{d}/", .{current_item});
274 self.bufWrite(&end, "{d}] ", .{eti});
275 } else {
276 self.bufWrite(&end, "[{d}/{d}] ", .{ current_item, eti });
277 }
272 self.bufWrite(&end, "[{d}/{d}] ", .{ current_item, eti });
278273 need_ellipse = false;
279274 } else if (completed_items != 0) {
280275 if (need_ellipse) self.bufWrite(&end, " ", .{});
lib/std/special/test_runner.zig+2-14
......@@ -58,13 +58,7 @@ pub fn main() void {
5858 test_node.activate();
5959 progress.refresh();
6060 if (!have_tty) {
61 if (builtin.zig_backend == .stage2_llvm) {
62 std.debug.print("{d}/", .{i + 1});
63 std.debug.print("{d} ", .{test_fn_list.len});
64 std.debug.print("{s}... ", .{test_fn.name});
65 } else {
66 std.debug.print("{d}/{d} {s}... ", .{ i + 1, test_fn_list.len, test_fn.name });
67 }
61 std.debug.print("{d}/{d} {s}... ", .{ i + 1, test_fn_list.len, test_fn.name });
6862 }
6963 const result = if (test_fn.async_frame_size) |size| switch (io_mode) {
7064 .evented => blk: {
......@@ -109,13 +103,7 @@ pub fn main() void {
109103 if (ok_count == test_fn_list.len) {
110104 std.debug.print("All {d} tests passed.\n", .{ok_count});
111105 } else {
112 if (builtin.zig_backend == .stage2_llvm) {
113 std.debug.print("{d} passed; ", .{ok_count});
114 std.debug.print("{d} skipped; ", .{skip_count});
115 std.debug.print("{d} failed.\n", .{fail_count});
116 } else {
117 std.debug.print("{d} passed; {d} skipped; {d} failed.\n", .{ ok_count, skip_count, fail_count });
118 }
106 std.debug.print("{d} passed; {d} skipped; {d} failed.\n", .{ ok_count, skip_count, fail_count });
119107 }
120108 if (log_err_count != 0) {
121109 std.debug.print("{d} errors were logged.\n", .{log_err_count});
src/Sema.zig+8-2
......@@ -4494,6 +4494,10 @@ fn analyzeCall(
44944494
44954495 try sema.emitBackwardBranch(&child_block, call_src);
44964496
4497 // Whether this call should be memoized, set to false if the call can mutate
4498 // comptime state.
4499 var should_memoize = true;
4500
44974501 // This will have return instructions analyzed as break instructions to
44984502 // the block_inst above. Here we are performing "comptime/inline semantic analysis"
44994503 // for a function body, which means we must map the parameter ZIR instructions to
......@@ -4527,6 +4531,7 @@ fn analyzeCall(
45274531 },
45284532 else => {},
45294533 }
4534 should_memoize = should_memoize and !arg_val.isComptimeMutablePtr();
45304535 memoized_call_key.args[arg_i] = .{
45314536 .ty = param_ty,
45324537 .val = arg_val,
......@@ -4552,6 +4557,7 @@ fn analyzeCall(
45524557 },
45534558 else => {},
45544559 }
4560 should_memoize = should_memoize and !arg_val.isComptimeMutablePtr();
45554561 memoized_call_key.args[arg_i] = .{
45564562 .ty = sema.typeOf(uncasted_arg),
45574563 .val = arg_val,
......@@ -4597,7 +4603,7 @@ fn analyzeCall(
45974603 // This `res2` is here instead of directly breaking from `res` due to a stage1
45984604 // bug generating invalid LLVM IR.
45994605 const res2: Air.Inst.Ref = res2: {
4600 if (is_comptime_call) {
4606 if (should_memoize and is_comptime_call) {
46014607 if (mod.memoized_calls.get(memoized_call_key)) |result| {
46024608 const ty_inst = try sema.addType(fn_ret_ty);
46034609 try sema.air_values.append(gpa, result.val);
......@@ -4621,7 +4627,7 @@ fn analyzeCall(
46214627 break :result try sema.analyzeBlockBody(block, call_src, &child_block, merges);
46224628 };
46234629
4624 if (is_comptime_call) {
4630 if (should_memoize and is_comptime_call) {
46254631 const result_val = try sema.resolveConstMaybeUndefVal(block, call_src, result);
46264632
46274633 // TODO: check whether any external comptime memory was mutated by the
test/behavior.zig+2-2
......@@ -125,6 +125,7 @@ test {
125125 _ = @import("behavior/widening.zig");
126126 _ = @import("behavior/bugs/421.zig");
127127 _ = @import("behavior/bugs/726.zig");
128 _ = @import("behavior/bugs/828.zig");
128129 _ = @import("behavior/bugs/1421.zig");
129130 _ = @import("behavior/bugs/1442.zig");
130131 _ = @import("behavior/bugs/1607.zig");
......@@ -132,6 +133,7 @@ test {
132133 _ = @import("behavior/bugs/3384.zig");
133134 _ = @import("behavior/bugs/3742.zig");
134135 _ = @import("behavior/bugs/5398.zig");
136 _ = @import("behavior/bugs/5413.zig");
135137 _ = @import("behavior/bugs/5487.zig");
136138 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
137139 _ = @import("behavior/switch_prong_err_enum.zig");
......@@ -148,12 +150,10 @@ test {
148150 _ = @import("behavior/await_struct.zig");
149151 _ = @import("behavior/bugs/529.zig");
150152 _ = @import("behavior/bugs/718.zig");
151 _ = @import("behavior/bugs/828.zig");
152153 _ = @import("behavior/bugs/920.zig");
153154 _ = @import("behavior/bugs/1120.zig");
154155 _ = @import("behavior/bugs/1851.zig");
155156 _ = @import("behavior/bugs/3779.zig");
156 _ = @import("behavior/bugs/5413.zig");
157157 _ = @import("behavior/bugs/6456.zig");
158158 _ = @import("behavior/bugs/6781.zig");
159159 _ = @import("behavior/bugs/7003.zig");