authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-04 13:40:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-04 13:40:51-07:00
logef2fa67ef018a4a49124a40e60941149efd34553
tree5c5c538cf0b1d79e6626637ea5b3192e7cdec868
parentaa0906e9aaaf36bc928b5502bdb34e7a0409b2c0
parent7e64dc42215c93a2d1d6b7fa4f5e07b885788a7d

Merge branch 'g-w1-stage2-evalbranch'

closes #7682

5 files changed, 97 insertions(+), 5 deletions(-)

src/Module.zig+23-4
......@@ -23,6 +23,8 @@ const trace = @import("tracy.zig").trace;
2323const astgen = @import("astgen.zig");
2424const zir_sema = @import("zir_sema.zig");
2525
26const default_eval_branch_quota = 1000;
27
2628/// General-purpose allocator. Used for both temporary and long-term storage.
2729gpa: *Allocator,
2830comp: *Compilation,
......@@ -765,6 +767,8 @@ pub const Scope = struct {
765767 label: ?Label = null,
766768 inlining: ?*Inlining,
767769 is_comptime: bool,
770 /// Shared to sub-blocks.
771 branch_quota: *u32,
768772
769773 pub const InstTable = std.AutoHashMap(*zir.Inst, *Inst);
770774
......@@ -792,8 +796,7 @@ pub const Scope = struct {
792796
793797 pub const Shared = struct {
794798 caller: ?*Fn,
795 branch_count: u64,
796 branch_quota: u64,
799 branch_count: u32,
797800 };
798801 };
799802
......@@ -1104,6 +1107,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
11041107 var inst_table = Scope.Block.InstTable.init(self.gpa);
11051108 defer inst_table.deinit();
11061109
1110 var branch_quota: u32 = default_eval_branch_quota;
1111
11071112 var block_scope: Scope.Block = .{
11081113 .parent = null,
11091114 .inst_table = &inst_table,
......@@ -1113,6 +1118,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
11131118 .arena = &decl_arena.allocator,
11141119 .inlining = null,
11151120 .is_comptime = false,
1121 .branch_quota = &branch_quota,
11161122 };
11171123 defer block_scope.instructions.deinit(self.gpa);
11181124
......@@ -1297,6 +1303,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
12971303 var decl_inst_table = Scope.Block.InstTable.init(self.gpa);
12981304 defer decl_inst_table.deinit();
12991305
1306 var branch_quota: u32 = default_eval_branch_quota;
1307
13001308 var block_scope: Scope.Block = .{
13011309 .parent = null,
13021310 .inst_table = &decl_inst_table,
......@@ -1306,6 +1314,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
13061314 .arena = &decl_arena.allocator,
13071315 .inlining = null,
13081316 .is_comptime = true,
1317 .branch_quota = &branch_quota,
13091318 };
13101319 defer block_scope.instructions.deinit(self.gpa);
13111320
......@@ -1367,6 +1376,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
13671376 var var_inst_table = Scope.Block.InstTable.init(self.gpa);
13681377 defer var_inst_table.deinit();
13691378
1379 var branch_quota_vi: u32 = default_eval_branch_quota;
13701380 var inner_block: Scope.Block = .{
13711381 .parent = null,
13721382 .inst_table = &var_inst_table,
......@@ -1376,6 +1386,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
13761386 .arena = &gen_scope_arena.allocator,
13771387 .inlining = null,
13781388 .is_comptime = true,
1389 .branch_quota = &branch_quota_vi,
13791390 };
13801391 defer inner_block.instructions.deinit(self.gpa);
13811392 try zir_sema.analyzeBody(self, &inner_block, .{
......@@ -1494,6 +1505,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
14941505 var inst_table = Scope.Block.InstTable.init(self.gpa);
14951506 defer inst_table.deinit();
14961507
1508 var branch_quota: u32 = default_eval_branch_quota;
1509
14971510 var block_scope: Scope.Block = .{
14981511 .parent = null,
14991512 .inst_table = &inst_table,
......@@ -1503,6 +1516,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
15031516 .arena = &analysis_arena.allocator,
15041517 .inlining = null,
15051518 .is_comptime = true,
1519 .branch_quota = &branch_quota,
15061520 };
15071521 defer block_scope.instructions.deinit(self.gpa);
15081522
......@@ -1875,6 +1889,8 @@ pub fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {
18751889 defer decl.typed_value.most_recent.arena.?.* = arena.state;
18761890 var inst_table = Scope.Block.InstTable.init(self.gpa);
18771891 defer inst_table.deinit();
1892 var branch_quota: u32 = default_eval_branch_quota;
1893
18781894 var inner_block: Scope.Block = .{
18791895 .parent = null,
18801896 .inst_table = &inst_table,
......@@ -1884,6 +1900,7 @@ pub fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {
18841900 .arena = &arena.allocator,
18851901 .inlining = null,
18861902 .is_comptime = false,
1903 .branch_quota = &branch_quota,
18871904 };
18881905 defer inner_block.instructions.deinit(self.gpa);
18891906
......@@ -3466,7 +3483,9 @@ pub fn addSafetyCheck(mod: *Module, parent_block: *Scope.Block, ok: *Inst, panic
34663483 .arena = parent_block.arena,
34673484 .inlining = parent_block.inlining,
34683485 .is_comptime = parent_block.is_comptime,
3486 .branch_quota = parent_block.branch_quota,
34693487 };
3488
34703489 defer fail_block.instructions.deinit(mod.gpa);
34713490
34723491 _ = try mod.safetyPanic(&fail_block, ok.src, panic_id);
......@@ -3532,10 +3551,10 @@ pub fn identifierTokenString(mod: *Module, scope: *Scope, token: ast.TokenIndex)
35323551pub fn emitBackwardBranch(mod: *Module, block: *Scope.Block, src: usize) !void {
35333552 const shared = block.inlining.?.shared;
35343553 shared.branch_count += 1;
3535 if (shared.branch_count > shared.branch_quota) {
3554 if (shared.branch_count > block.branch_quota.*) {
35363555 // TODO show the "called from here" stack
35373556 return mod.fail(&block.base, src, "evaluation exceeded {d} backwards branches", .{
3538 shared.branch_quota,
3557 block.branch_quota.*,
35393558 });
35403559 }
35413560}
src/astgen.zig+15
......@@ -2317,6 +2317,19 @@ fn compileError(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerE
23172317 return addZIRUnOp(mod, scope, src, .compileerror, target);
23182318}
23192319
2320fn setEvalBranchQuota(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
2321 try ensureBuiltinParamCount(mod, scope, call, 1);
2322 const tree = scope.tree();
2323 const src = tree.token_locs[call.builtin_token].start;
2324 const params = call.params();
2325 const u32_type = try addZIRInstConst(mod, scope, src, .{
2326 .ty = Type.initTag(.type),
2327 .val = Value.initTag(.u32_type),
2328 });
2329 const quota = try expr(mod, scope, .{ .ty = u32_type }, params[0]);
2330 return addZIRUnOp(mod, scope, src, .set_eval_branch_quota, quota);
2331}
2332
23202333fn typeOf(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
23212334 const tree = scope.tree();
23222335 const arena = scope.arena();
......@@ -2362,6 +2375,8 @@ fn builtinCall(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.Built
23622375 return rlWrap(mod, scope, rl, try import(mod, scope, call));
23632376 } else if (mem.eql(u8, builtin_name, "@compileError")) {
23642377 return compileError(mod, scope, call);
2378 } else if (mem.eql(u8, builtin_name, "@setEvalBranchQuota")) {
2379 return setEvalBranchQuota(mod, scope, call);
23652380 } else {
23662381 return mod.failTok(scope, call.builtin_token, "invalid builtin function: '{s}'", .{builtin_name});
23672382 }
src/zir.zig+5
......@@ -127,6 +127,9 @@ pub const Inst = struct {
127127 coerce_to_ptr_elem,
128128 /// Emit an error message and fail compilation.
129129 compileerror,
130 /// Changes the maximum number of backwards branches that compile-time
131 /// code execution can use before giving up and making a compile error.
132 set_eval_branch_quota,
130133 /// Conditional branch. Splits control flow based on a boolean condition value.
131134 condbr,
132135 /// Special case, has no textual representation.
......@@ -347,6 +350,7 @@ pub const Inst = struct {
347350 .anyframe_type,
348351 .bitnot,
349352 .import,
353 .set_eval_branch_quota,
350354 => UnOp,
351355
352356 .add,
......@@ -535,6 +539,7 @@ pub const Inst = struct {
535539 .switch_range,
536540 .typeof_peer,
537541 .resolve_inferred_alloc,
542 .set_eval_branch_quota,
538543 => false,
539544
540545 .@"break",
src/zir_sema.zig+39-1
......@@ -81,6 +81,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
8181 .mut_slice_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.mut_slice_type).?, true, .Slice),
8282 .ptr_type => return analyzeInstPtrType(mod, scope, old_inst.castTag(.ptr_type).?),
8383 .store => return analyzeInstStore(mod, scope, old_inst.castTag(.store).?),
84 .set_eval_branch_quota => return analyzeInstSetEvalBranchQuota(mod, scope, old_inst.castTag(.set_eval_branch_quota).?),
8485 .str => return analyzeInstStr(mod, scope, old_inst.castTag(.str).?),
8586 .int => {
8687 const big_int = old_inst.castTag(.int).?.positionals.int;
......@@ -280,6 +281,24 @@ fn resolveType(mod: *Module, scope: *Scope, old_inst: *zir.Inst) !Type {
280281 return val.toType(scope.arena());
281282}
282283
284/// Appropriate to call when the coercion has already been done by result
285/// location semantics. Asserts the value fits in the provided `Int` type.
286/// Only supports `Int` types 64 bits or less.
287fn resolveAlreadyCoercedInt(
288 mod: *Module,
289 scope: *Scope,
290 old_inst: *zir.Inst,
291 comptime Int: type,
292) !Int {
293 comptime assert(@typeInfo(Int).Int.bits <= 64);
294 const new_inst = try resolveInst(mod, scope, old_inst);
295 const val = try mod.resolveConstValue(scope, new_inst);
296 switch (@typeInfo(Int).Int.signedness) {
297 .signed => return @intCast(Int, val.toSignedInt()),
298 .unsigned => return @intCast(Int, val.toUnsignedInt()),
299 }
300}
301
283302fn resolveInt(mod: *Module, scope: *Scope, old_inst: *zir.Inst, dest_type: Type) !u64 {
284303 const new_inst = try resolveInst(mod, scope, old_inst);
285304 const coerced = try mod.coerce(scope, dest_type, new_inst);
......@@ -486,6 +505,18 @@ fn analyzeInstStoreToInferredPtr(
486505 return mod.storePtr(scope, inst.base.src, bitcasted_ptr, value);
487506}
488507
508fn analyzeInstSetEvalBranchQuota(
509 mod: *Module,
510 scope: *Scope,
511 inst: *zir.Inst.UnOp,
512) InnerError!*Inst {
513 const b = try mod.requireFunctionBlock(scope, inst.base.src);
514 const quota = try resolveAlreadyCoercedInt(mod, scope, inst.positionals.operand, u32);
515 if (b.branch_quota.* < quota)
516 b.branch_quota.* = quota;
517 return mod.constVoid(scope, inst.base.src);
518}
519
489520fn analyzeInstStore(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
490521 const ptr = try resolveInst(mod, scope, inst.positionals.lhs);
491522 const value = try resolveInst(mod, scope, inst.positionals.rhs);
......@@ -594,6 +625,7 @@ fn analyzeInstLoop(mod: *Module, scope: *Scope, inst: *zir.Inst.Loop) InnerError
594625 .arena = parent_block.arena,
595626 .inlining = parent_block.inlining,
596627 .is_comptime = parent_block.is_comptime,
628 .branch_quota = parent_block.branch_quota,
597629 };
598630 defer child_block.instructions.deinit(mod.gpa);
599631
......@@ -619,6 +651,7 @@ fn analyzeInstBlockFlat(mod: *Module, scope: *Scope, inst: *zir.Inst.Block, is_c
619651 .label = null,
620652 .inlining = parent_block.inlining,
621653 .is_comptime = parent_block.is_comptime or is_comptime,
654 .branch_quota = parent_block.branch_quota,
622655 };
623656 defer child_block.instructions.deinit(mod.gpa);
624657
......@@ -666,6 +699,7 @@ fn analyzeInstBlock(mod: *Module, scope: *Scope, inst: *zir.Inst.Block, is_compt
666699 }),
667700 .inlining = parent_block.inlining,
668701 .is_comptime = is_comptime or parent_block.is_comptime,
702 .branch_quota = parent_block.branch_quota,
669703 };
670704 const merges = &child_block.label.?.merges;
671705
......@@ -867,7 +901,6 @@ fn analyzeInstCall(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError
867901 // Otherwise we pass on the shared data from the parent scope.
868902 var shared_inlining = Scope.Block.Inlining.Shared{
869903 .branch_count = 0,
870 .branch_quota = 1000,
871904 .caller = b.func,
872905 };
873906 // This one is shared among sub-blocks within the same callee, but not
......@@ -896,7 +929,9 @@ fn analyzeInstCall(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError
896929 .label = null,
897930 .inlining = &inlining,
898931 .is_comptime = is_comptime_call,
932 .branch_quota = b.branch_quota,
899933 };
934
900935 const merges = &child_block.inlining.?.merges;
901936
902937 defer child_block.instructions.deinit(mod.gpa);
......@@ -1417,6 +1452,7 @@ fn analyzeInstSwitchBr(mod: *Module, scope: *Scope, inst: *zir.Inst.SwitchBr) In
14171452 .arena = parent_block.arena,
14181453 .inlining = parent_block.inlining,
14191454 .is_comptime = parent_block.is_comptime,
1455 .branch_quota = parent_block.branch_quota,
14201456 };
14211457 defer case_block.instructions.deinit(mod.gpa);
14221458
......@@ -1960,6 +1996,7 @@ fn analyzeInstCondBr(mod: *Module, scope: *Scope, inst: *zir.Inst.CondBr) InnerE
19601996 .arena = parent_block.arena,
19611997 .inlining = parent_block.inlining,
19621998 .is_comptime = parent_block.is_comptime,
1999 .branch_quota = parent_block.branch_quota,
19632000 };
19642001 defer true_block.instructions.deinit(mod.gpa);
19652002 try analyzeBody(mod, &true_block, inst.positionals.then_body);
......@@ -1973,6 +2010,7 @@ fn analyzeInstCondBr(mod: *Module, scope: *Scope, inst: *zir.Inst.CondBr) InnerE
19732010 .arena = parent_block.arena,
19742011 .inlining = parent_block.inlining,
19752012 .is_comptime = parent_block.is_comptime,
2013 .branch_quota = parent_block.branch_quota,
19762014 };
19772015 defer false_block.instructions.deinit(mod.gpa);
19782016 try analyzeBody(mod, &false_block, inst.positionals.else_body);
test/stage2/cbe.zig+15
......@@ -67,7 +67,22 @@ pub fn addCases(ctx: *TestContext) !void {
6767 \\}
6868 , "");
6969 }
70 {
71 var case = ctx.exeFromCompiledC("@setEvalBranchQuota", .{});
7072
73 case.addCompareOutput(
74 \\export fn main() i32 {
75 \\ @setEvalBranchQuota(1001);
76 \\ const y = rec(1001);
77 \\ return y - 1;
78 \\}
79 \\
80 \\inline fn rec(n: usize) usize {
81 \\ if (n <= 1) return n;
82 \\ return rec(n - 1);
83 \\}
84 , "");
85 }
7186 ctx.c("empty start function", linux_x64,
7287 \\export fn _start() noreturn {
7388 \\ unreachable;