From 638f93ebdceb860974aae54b6f8c2c9f52157305 Mon Sep 17 00:00:00 2001 From: g-w1 Date: Sun, 3 Jan 2021 15:45:22 -0500 Subject: stage2: implementation of `@setEvalBranchQuota`: `@setEvalBranchQuota` can be called before the comptime/inline call stack is created. For example: ```zig @setEvalBranchQuota(100); comptime { while (true) {} } ``` Here we need to set the branch_quota before the comptime block creates a scope for the branch_count. --- src/Module.zig | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 24ea48043b..ce4fd51bb9 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -765,6 +765,8 @@ pub const Scope = struct { label: ?Label = null, inlining: ?*Inlining, is_comptime: bool, + /// Shared to sub-blocks. + branch_quota: *u32, pub const InstTable = std.AutoHashMap(*zir.Inst, *Inst); @@ -792,8 +794,7 @@ pub const Scope = struct { pub const Shared = struct { caller: ?*Fn, - branch_count: u64, - branch_quota: u64, + branch_count: u32, }; }; @@ -1104,6 +1105,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { var inst_table = Scope.Block.InstTable.init(self.gpa); defer inst_table.deinit(); + var branch_quota: u32 = 1000; + var block_scope: Scope.Block = .{ .parent = null, .inst_table = &inst_table, @@ -1113,6 +1116,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { .arena = &decl_arena.allocator, .inlining = null, .is_comptime = false, + .branch_quota = &branch_quota, }; defer block_scope.instructions.deinit(self.gpa); @@ -1297,6 +1301,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { var decl_inst_table = Scope.Block.InstTable.init(self.gpa); defer decl_inst_table.deinit(); + var branch_quota: u32 = 1000; + var block_scope: Scope.Block = .{ .parent = null, .inst_table = &decl_inst_table, @@ -1306,6 +1312,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { .arena = &decl_arena.allocator, .inlining = null, .is_comptime = true, + .branch_quota = &branch_quota, }; defer block_scope.instructions.deinit(self.gpa); @@ -1367,6 +1374,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { var var_inst_table = Scope.Block.InstTable.init(self.gpa); defer var_inst_table.deinit(); + var branch_quota_vi: u32 = 1000; var inner_block: Scope.Block = .{ .parent = null, .inst_table = &var_inst_table, @@ -1376,6 +1384,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { .arena = &gen_scope_arena.allocator, .inlining = null, .is_comptime = true, + .branch_quota = &branch_quota_vi, }; defer inner_block.instructions.deinit(self.gpa); try zir_sema.analyzeBody(self, &inner_block, .{ @@ -1494,6 +1503,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { var inst_table = Scope.Block.InstTable.init(self.gpa); defer inst_table.deinit(); + var branch_quota: u32 = 1000; + var block_scope: Scope.Block = .{ .parent = null, .inst_table = &inst_table, @@ -1503,6 +1514,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { .arena = &analysis_arena.allocator, .inlining = null, .is_comptime = true, + .branch_quota = &branch_quota, }; defer block_scope.instructions.deinit(self.gpa); @@ -1875,6 +1887,8 @@ pub fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void { defer decl.typed_value.most_recent.arena.?.* = arena.state; var inst_table = Scope.Block.InstTable.init(self.gpa); defer inst_table.deinit(); + var branch_quota: u32 = 1000; + var inner_block: Scope.Block = .{ .parent = null, .inst_table = &inst_table, @@ -1884,6 +1898,7 @@ pub fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void { .arena = &arena.allocator, .inlining = null, .is_comptime = false, + .branch_quota = &branch_quota, }; defer inner_block.instructions.deinit(self.gpa); @@ -3466,7 +3481,9 @@ pub fn addSafetyCheck(mod: *Module, parent_block: *Scope.Block, ok: *Inst, panic .arena = parent_block.arena, .inlining = parent_block.inlining, .is_comptime = parent_block.is_comptime, + .branch_quota = parent_block.branch_quota, }; + defer fail_block.instructions.deinit(mod.gpa); _ = try mod.safetyPanic(&fail_block, ok.src, panic_id); @@ -3532,10 +3549,10 @@ pub fn identifierTokenString(mod: *Module, scope: *Scope, token: ast.TokenIndex) pub fn emitBackwardBranch(mod: *Module, block: *Scope.Block, src: usize) !void { const shared = block.inlining.?.shared; shared.branch_count += 1; - if (shared.branch_count > shared.branch_quota) { + if (shared.branch_count > block.branch_quota.*) { // TODO show the "called from here" stack return mod.fail(&block.base, src, "evaluation exceeded {d} backwards branches", .{ - shared.branch_quota, + block.branch_quota.*, }); } } -- cgit v1.2.3 From 7e64dc42215c93a2d1d6b7fa4f5e07b885788a7d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 4 Jan 2021 13:40:01 -0700 Subject: stage2: improvements to `@setEvalBranchQuota` * extract magic number into a constant * properly use result location casting for the operand * naming convention for ZIR instructions --- src/Module.zig | 12 +++++++----- src/astgen.zig | 4 ++-- src/zir.zig | 9 +++++---- src/zir_sema.zig | 22 ++++++++++++++++++++-- 4 files changed, 34 insertions(+), 13 deletions(-) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index ce4fd51bb9..6a4575394a 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -23,6 +23,8 @@ const trace = @import("tracy.zig").trace; const astgen = @import("astgen.zig"); const zir_sema = @import("zir_sema.zig"); +const default_eval_branch_quota = 1000; + /// General-purpose allocator. Used for both temporary and long-term storage. gpa: *Allocator, comp: *Compilation, @@ -1105,7 +1107,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { var inst_table = Scope.Block.InstTable.init(self.gpa); defer inst_table.deinit(); - var branch_quota: u32 = 1000; + var branch_quota: u32 = default_eval_branch_quota; var block_scope: Scope.Block = .{ .parent = null, @@ -1301,7 +1303,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { var decl_inst_table = Scope.Block.InstTable.init(self.gpa); defer decl_inst_table.deinit(); - var branch_quota: u32 = 1000; + var branch_quota: u32 = default_eval_branch_quota; var block_scope: Scope.Block = .{ .parent = null, @@ -1374,7 +1376,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { var var_inst_table = Scope.Block.InstTable.init(self.gpa); defer var_inst_table.deinit(); - var branch_quota_vi: u32 = 1000; + var branch_quota_vi: u32 = default_eval_branch_quota; var inner_block: Scope.Block = .{ .parent = null, .inst_table = &var_inst_table, @@ -1503,7 +1505,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool { var inst_table = Scope.Block.InstTable.init(self.gpa); defer inst_table.deinit(); - var branch_quota: u32 = 1000; + var branch_quota: u32 = default_eval_branch_quota; var block_scope: Scope.Block = .{ .parent = null, @@ -1887,7 +1889,7 @@ pub fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void { defer decl.typed_value.most_recent.arena.?.* = arena.state; var inst_table = Scope.Block.InstTable.init(self.gpa); defer inst_table.deinit(); - var branch_quota: u32 = 1000; + var branch_quota: u32 = default_eval_branch_quota; var inner_block: Scope.Block = .{ .parent = null, diff --git a/src/astgen.zig b/src/astgen.zig index a24470c304..8275a05d77 100644 --- a/src/astgen.zig +++ b/src/astgen.zig @@ -2322,12 +2322,12 @@ fn setEvalBranchQuota(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) const tree = scope.tree(); const src = tree.token_locs[call.builtin_token].start; const params = call.params(); - const target = try expr(mod, scope, .none, params[0]); const u32_type = try addZIRInstConst(mod, scope, src, .{ .ty = Type.initTag(.type), .val = Value.initTag(.u32_type), }); - return addZIRUnOp(mod, scope, src, .setevalbranchquota, try rlWrap(mod, scope, .{ .ty = u32_type }, target)); + const quota = try expr(mod, scope, .{ .ty = u32_type }, params[0]); + return addZIRUnOp(mod, scope, src, .set_eval_branch_quota, quota); } fn typeOf(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst { diff --git a/src/zir.zig b/src/zir.zig index 25984b665f..8019d0e030 100644 --- a/src/zir.zig +++ b/src/zir.zig @@ -127,8 +127,9 @@ pub const Inst = struct { coerce_to_ptr_elem, /// Emit an error message and fail compilation. compileerror, - /// Changes the maximum number of backwards branches that compile-time code execution can use before giving up and making a compile error. - setevalbranchquota, + /// Changes the maximum number of backwards branches that compile-time + /// code execution can use before giving up and making a compile error. + set_eval_branch_quota, /// Conditional branch. Splits control flow based on a boolean condition value. condbr, /// Special case, has no textual representation. @@ -349,7 +350,7 @@ pub const Inst = struct { .anyframe_type, .bitnot, .import, - .setevalbranchquota, + .set_eval_branch_quota, => UnOp, .add, @@ -538,7 +539,7 @@ pub const Inst = struct { .switch_range, .typeof_peer, .resolve_inferred_alloc, - .setevalbranchquota, + .set_eval_branch_quota, => false, .@"break", diff --git a/src/zir_sema.zig b/src/zir_sema.zig index 605dc7dcf4..ef15a4bd45 100644 --- a/src/zir_sema.zig +++ b/src/zir_sema.zig @@ -81,7 +81,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError! .mut_slice_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.mut_slice_type).?, true, .Slice), .ptr_type => return analyzeInstPtrType(mod, scope, old_inst.castTag(.ptr_type).?), .store => return analyzeInstStore(mod, scope, old_inst.castTag(.store).?), - .setevalbranchquota => return analyzeInstSetEvalBranchQuota(mod, scope, old_inst.castTag(.setevalbranchquota).?), + .set_eval_branch_quota => return analyzeInstSetEvalBranchQuota(mod, scope, old_inst.castTag(.set_eval_branch_quota).?), .str => return analyzeInstStr(mod, scope, old_inst.castTag(.str).?), .int => { const big_int = old_inst.castTag(.int).?.positionals.int; @@ -281,6 +281,24 @@ fn resolveType(mod: *Module, scope: *Scope, old_inst: *zir.Inst) !Type { return val.toType(scope.arena()); } +/// Appropriate to call when the coercion has already been done by result +/// location semantics. Asserts the value fits in the provided `Int` type. +/// Only supports `Int` types 64 bits or less. +fn resolveAlreadyCoercedInt( + mod: *Module, + scope: *Scope, + old_inst: *zir.Inst, + comptime Int: type, +) !Int { + comptime assert(@typeInfo(Int).Int.bits <= 64); + const new_inst = try resolveInst(mod, scope, old_inst); + const val = try mod.resolveConstValue(scope, new_inst); + switch (@typeInfo(Int).Int.signedness) { + .signed => return @intCast(Int, val.toSignedInt()), + .unsigned => return @intCast(Int, val.toUnsignedInt()), + } +} + fn resolveInt(mod: *Module, scope: *Scope, old_inst: *zir.Inst, dest_type: Type) !u64 { const new_inst = try resolveInst(mod, scope, old_inst); const coerced = try mod.coerce(scope, dest_type, new_inst); @@ -493,7 +511,7 @@ fn analyzeInstSetEvalBranchQuota( inst: *zir.Inst.UnOp, ) InnerError!*Inst { const b = try mod.requireFunctionBlock(scope, inst.base.src); - const quota = @truncate(u32, try resolveInt(mod, scope, inst.positionals.operand, Type.initTag(.u32))); + const quota = try resolveAlreadyCoercedInt(mod, scope, inst.positionals.operand, u32); if (b.branch_quota.* < quota) b.branch_quota.* = quota; return mod.constVoid(scope, inst.base.src); -- cgit v1.2.3