Commit 9f59189c95

Veikka Tuominen <git@vexu.eu>
2022-02-27 15:40:59
stage2: do not memoize calls that can mutate comptime state
1 parent 7a92b89
Changed files (4)
lib/std/special/test_runner.zig
@@ -58,13 +58,7 @@ pub fn main() void {
         test_node.activate();
         progress.refresh();
         if (!have_tty) {
-            if (builtin.zig_backend == .stage2_llvm) {
-                std.debug.print("{d}/", .{i + 1});
-                std.debug.print("{d} ", .{test_fn_list.len});
-                std.debug.print("{s}... ", .{test_fn.name});
-            } else {
-                std.debug.print("{d}/{d} {s}... ", .{ i + 1, test_fn_list.len, test_fn.name });
-            }
+            std.debug.print("{d}/{d} {s}... ", .{ i + 1, test_fn_list.len, test_fn.name });
         }
         const result = if (test_fn.async_frame_size) |size| switch (io_mode) {
             .evented => blk: {
@@ -109,13 +103,7 @@ pub fn main() void {
     if (ok_count == test_fn_list.len) {
         std.debug.print("All {d} tests passed.\n", .{ok_count});
     } else {
-        if (builtin.zig_backend == .stage2_llvm) {
-            std.debug.print("{d} passed; ", .{ok_count});
-            std.debug.print("{d} skipped; ", .{skip_count});
-            std.debug.print("{d} failed.\n", .{fail_count});
-        } else {
-            std.debug.print("{d} passed; {d} skipped; {d} failed.\n", .{ ok_count, skip_count, fail_count });
-        }
+        std.debug.print("{d} passed; {d} skipped; {d} failed.\n", .{ ok_count, skip_count, fail_count });
     }
     if (log_err_count != 0) {
         std.debug.print("{d} errors were logged.\n", .{log_err_count});
lib/std/Progress.zig
@@ -269,12 +269,7 @@ fn refreshWithHeldLock(self: *Progress) void {
                 }
                 if (eti > 0) {
                     if (need_ellipse) self.bufWrite(&end, " ", .{});
-                    if (builtin.zig_backend == .stage2_llvm) {
-                        self.bufWrite(&end, "[{d}/", .{current_item});
-                        self.bufWrite(&end, "{d}] ", .{eti});
-                    } else {
-                        self.bufWrite(&end, "[{d}/{d}] ", .{ current_item, eti });
-                    }
+                    self.bufWrite(&end, "[{d}/{d}] ", .{ current_item, eti });
                     need_ellipse = false;
                 } else if (completed_items != 0) {
                     if (need_ellipse) self.bufWrite(&end, " ", .{});
src/Sema.zig
@@ -4494,6 +4494,10 @@ fn analyzeCall(
 
         try sema.emitBackwardBranch(&child_block, call_src);
 
+        // Whether this call should be memoized, set to false if the call can mutate
+        // comptime state.
+        var should_memoize = true;
+
         // This will have return instructions analyzed as break instructions to
         // the block_inst above. Here we are performing "comptime/inline semantic analysis"
         // for a function body, which means we must map the parameter ZIR instructions to
@@ -4527,6 +4531,7 @@ fn analyzeCall(
                         },
                         else => {},
                     }
+                    should_memoize = should_memoize and !arg_val.isComptimeMutablePtr();
                     memoized_call_key.args[arg_i] = .{
                         .ty = param_ty,
                         .val = arg_val,
@@ -4552,6 +4557,7 @@ fn analyzeCall(
                         },
                         else => {},
                     }
+                    should_memoize = should_memoize and !arg_val.isComptimeMutablePtr();
                     memoized_call_key.args[arg_i] = .{
                         .ty = sema.typeOf(uncasted_arg),
                         .val = arg_val,
@@ -4597,7 +4603,7 @@ fn analyzeCall(
         // This `res2` is here instead of directly breaking from `res` due to a stage1
         // bug generating invalid LLVM IR.
         const res2: Air.Inst.Ref = res2: {
-            if (is_comptime_call) {
+            if (should_memoize and is_comptime_call) {
                 if (mod.memoized_calls.get(memoized_call_key)) |result| {
                     const ty_inst = try sema.addType(fn_ret_ty);
                     try sema.air_values.append(gpa, result.val);
@@ -4621,7 +4627,7 @@ fn analyzeCall(
                 break :result try sema.analyzeBlockBody(block, call_src, &child_block, merges);
             };
 
-            if (is_comptime_call) {
+            if (should_memoize and is_comptime_call) {
                 const result_val = try sema.resolveConstMaybeUndefVal(block, call_src, result);
 
                 // TODO: check whether any external comptime memory was mutated by the
test/behavior.zig
@@ -125,6 +125,7 @@ test {
             _ = @import("behavior/widening.zig");
             _ = @import("behavior/bugs/421.zig");
             _ = @import("behavior/bugs/726.zig");
+            _ = @import("behavior/bugs/828.zig");
             _ = @import("behavior/bugs/1421.zig");
             _ = @import("behavior/bugs/1442.zig");
             _ = @import("behavior/bugs/1607.zig");
@@ -132,6 +133,7 @@ test {
             _ = @import("behavior/bugs/3384.zig");
             _ = @import("behavior/bugs/3742.zig");
             _ = @import("behavior/bugs/5398.zig");
+            _ = @import("behavior/bugs/5413.zig");
             _ = @import("behavior/bugs/5487.zig");
             _ = @import("behavior/struct_contains_null_ptr_itself.zig");
             _ = @import("behavior/switch_prong_err_enum.zig");
@@ -148,12 +150,10 @@ test {
                 _ = @import("behavior/await_struct.zig");
                 _ = @import("behavior/bugs/529.zig");
                 _ = @import("behavior/bugs/718.zig");
-                _ = @import("behavior/bugs/828.zig");
                 _ = @import("behavior/bugs/920.zig");
                 _ = @import("behavior/bugs/1120.zig");
                 _ = @import("behavior/bugs/1851.zig");
                 _ = @import("behavior/bugs/3779.zig");
-                _ = @import("behavior/bugs/5413.zig");
                 _ = @import("behavior/bugs/6456.zig");
                 _ = @import("behavior/bugs/6781.zig");
                 _ = @import("behavior/bugs/7003.zig");