From e323cf1264f390911dcc2efea71d46be1d631d92 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 27 Aug 2022 18:48:01 +0300 Subject: stage2: change how defers are stored in Zir Storing defers this way has the benefits that the defer doesn't get analyzed multiple times in AstGen, it takes up less space, and it makes Sema aware of defers allowing for 'unreachable else prong' error on error sets in generic code. The disadvantage is that it is a bit more complex and errdefers with payloads now emit a placeholder instruction (but those are rare). Sema.zig before: Total ZIR bytes: 3.7794370651245117MiB Instructions: 238996 (2.051319122314453MiB) String Table Bytes: 89.2802734375KiB Extra Data Items: 430144 (1.640869140625MiB) Sema.zig after: Total ZIR bytes: 3.3344192504882812MiB Instructions: 211829 (1.8181428909301758MiB) String Table Bytes: 89.2802734375KiB Extra Data Items: 374611 (1.4290275573730469MiB) --- src/Zir.zig | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/Zir.zig') diff --git a/src/Zir.zig b/src/Zir.zig index 3ce086e10b..9621aefcf6 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -995,6 +995,13 @@ pub const Inst = struct { /// closure_capture instruction ref. closure_get, + /// A defer statement. + /// Uses the `defer` union field. + @"defer", + /// An errdefer statement with a code. + /// Uses the `err_defer_code` union field. + defer_err_code, + /// The ZIR instruction tag is one of the `Extended` ones. /// Uses the `extended` union field. extended, @@ -1244,6 +1251,8 @@ pub const Inst = struct { .try_ptr, //.try_inline, //.try_ptr_inline, + .@"defer", + .defer_err_code, => false, .@"break", @@ -1311,6 +1320,8 @@ pub const Inst = struct { .memcpy, .memset, .check_comptime_control_flow, + .@"defer", + .defer_err_code, => true, .param, @@ -1819,6 +1830,9 @@ pub const Inst = struct { .closure_capture = .un_tok, .closure_get = .inst_node, + .@"defer" = .@"defer", + .defer_err_code = .defer_err_code, + .extended = .extended, }); }; @@ -2575,6 +2589,14 @@ pub const Inst = struct { return zir.nullTerminatedString(self.str); } }, + @"defer": struct { + index: u32, + len: u32, + }, + defer_err_code: struct { + err_code: Ref, + payload_index: u32, + }, // Make sure we don't accidentally add a field to make this union // bigger than expected. Note that in Debug builds, Zig is allowed @@ -2611,6 +2633,8 @@ pub const Inst = struct { dbg_stmt, inst_node, str_op, + @"defer", + defer_err_code, }; }; @@ -3550,6 +3574,12 @@ pub const Inst = struct { line: u32, column: u32, }; + + pub const DeferErrCode = struct { + remapped_err_code: Index, + index: u32, + len: u32, + }; }; pub const SpecialProng = enum { none, @"else", under }; -- cgit v1.2.3