authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-05 18:03:09+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-05 19:58:42+00:00
log8f849684f46ad0835bd9591f420e49e212880cb2
treeebf5d639609a744108f4e9a356d0074c25a640d9
parent7f3211a101d8763ec5f0009b219f6819dba2cd35
signature Commit is signed but in an unrecognized format.

std.zig.Zir: improve instruction tracking

The main change here is to partition tracked instructions found within a declaration. It's very unlikely that, for instance, a `struct { ... }` type declaration was intentionally turned into a reification or an anonymous initialization, so it makes sense to track things in a few different arrays. In particular, this fixes an issue where a `func` instruction could wrongly be mapped to something else if the types of function parameters changed. This would cause huge problems further down the pipeline; we expect that if a `declaration` is tracked, and it previously contained a `func`/`func_inferred`/`func_fancy`, then this instruction is either tracked to another `func`/`func_inferred`/`func_fancy` instruction, or is lost. Also, this commit takes the opportunity to rename the functions actually doing this logic. `Zir.findDecls` was a name that might have made sense at some point, but nowadays, it's definitely not finding declarations, and it's not *exclusively* finding type declarations. Instead, the point is to find instructions which we want to track; hence the new name, `Zir.findTrackable`. Lastly, a nice side effect of partitioning the output of `findTrackable` is that `Zir.declIterator` no longer needs to accept input instructions which aren't type declarations (e.g. `reify`, `func`).

2 files changed, 249 insertions(+), 197 deletions(-)

lib/std/zig/Zir.zig+188-176
......@@ -3615,145 +3615,155 @@ pub const DeclIterator = struct {
36153615};
36163616
36173617pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
3618 const tags = zir.instructions.items(.tag);
3619 const datas = zir.instructions.items(.data);
3620 switch (tags[@intFromEnum(decl_inst)]) {
3621 // Functions are allowed and yield no iterations.
3622 // This is because they are returned by `findDecls`.
3623 .func, .func_inferred, .func_fancy => return .{
3624 .extra_index = undefined,
3625 .decls_remaining = 0,
3626 .zir = zir,
3627 },
3628
3629 .extended => {
3630 const extended = datas[@intFromEnum(decl_inst)].extended;
3631 switch (extended.opcode) {
3632 // Reifications are allowed and yield no iterations.
3633 // This is because they are returned by `findDecls`.
3634 .reify => return .{
3635 .extra_index = undefined,
3636 .decls_remaining = 0,
3637 .zir = zir,
3638 },
3639 .struct_decl => {
3640 const small: Inst.StructDecl.Small = @bitCast(extended.small);
3641 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.StructDecl).@"struct".fields.len);
3642 const captures_len = if (small.has_captures_len) captures_len: {
3643 const captures_len = zir.extra[extra_index];
3644 extra_index += 1;
3645 break :captures_len captures_len;
3646 } else 0;
3647 extra_index += @intFromBool(small.has_fields_len);
3648 const decls_len = if (small.has_decls_len) decls_len: {
3649 const decls_len = zir.extra[extra_index];
3650 extra_index += 1;
3651 break :decls_len decls_len;
3652 } else 0;
3653
3654 extra_index += captures_len;
3655
3656 if (small.has_backing_int) {
3657 const backing_int_body_len = zir.extra[extra_index];
3658 extra_index += 1; // backing_int_body_len
3659 if (backing_int_body_len == 0) {
3660 extra_index += 1; // backing_int_ref
3661 } else {
3662 extra_index += backing_int_body_len; // backing_int_body_inst
3663 }
3664 }
3618 const inst = zir.instructions.get(@intFromEnum(decl_inst));
3619 assert(inst.tag == .extended);
3620 const extended = inst.data.extended;
3621 switch (extended.opcode) {
3622 .struct_decl => {
3623 const small: Inst.StructDecl.Small = @bitCast(extended.small);
3624 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.StructDecl).@"struct".fields.len);
3625 const captures_len = if (small.has_captures_len) captures_len: {
3626 const captures_len = zir.extra[extra_index];
3627 extra_index += 1;
3628 break :captures_len captures_len;
3629 } else 0;
3630 extra_index += @intFromBool(small.has_fields_len);
3631 const decls_len = if (small.has_decls_len) decls_len: {
3632 const decls_len = zir.extra[extra_index];
3633 extra_index += 1;
3634 break :decls_len decls_len;
3635 } else 0;
3636
3637 extra_index += captures_len;
3638
3639 if (small.has_backing_int) {
3640 const backing_int_body_len = zir.extra[extra_index];
3641 extra_index += 1; // backing_int_body_len
3642 if (backing_int_body_len == 0) {
3643 extra_index += 1; // backing_int_ref
3644 } else {
3645 extra_index += backing_int_body_len; // backing_int_body_inst
3646 }
3647 }
36653648
3666 return .{
3667 .extra_index = extra_index,
3668 .decls_remaining = decls_len,
3669 .zir = zir,
3670 };
3671 },
3672 .enum_decl => {
3673 const small: Inst.EnumDecl.Small = @bitCast(extended.small);
3674 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.EnumDecl).@"struct".fields.len);
3675 extra_index += @intFromBool(small.has_tag_type);
3676 const captures_len = if (small.has_captures_len) captures_len: {
3677 const captures_len = zir.extra[extra_index];
3678 extra_index += 1;
3679 break :captures_len captures_len;
3680 } else 0;
3681 extra_index += @intFromBool(small.has_body_len);
3682 extra_index += @intFromBool(small.has_fields_len);
3683 const decls_len = if (small.has_decls_len) decls_len: {
3684 const decls_len = zir.extra[extra_index];
3685 extra_index += 1;
3686 break :decls_len decls_len;
3687 } else 0;
3649 return .{
3650 .extra_index = extra_index,
3651 .decls_remaining = decls_len,
3652 .zir = zir,
3653 };
3654 },
3655 .enum_decl => {
3656 const small: Inst.EnumDecl.Small = @bitCast(extended.small);
3657 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.EnumDecl).@"struct".fields.len);
3658 extra_index += @intFromBool(small.has_tag_type);
3659 const captures_len = if (small.has_captures_len) captures_len: {
3660 const captures_len = zir.extra[extra_index];
3661 extra_index += 1;
3662 break :captures_len captures_len;
3663 } else 0;
3664 extra_index += @intFromBool(small.has_body_len);
3665 extra_index += @intFromBool(small.has_fields_len);
3666 const decls_len = if (small.has_decls_len) decls_len: {
3667 const decls_len = zir.extra[extra_index];
3668 extra_index += 1;
3669 break :decls_len decls_len;
3670 } else 0;
36883671
3689 extra_index += captures_len;
3672 extra_index += captures_len;
36903673
3691 return .{
3692 .extra_index = extra_index,
3693 .decls_remaining = decls_len,
3694 .zir = zir,
3695 };
3696 },
3697 .union_decl => {
3698 const small: Inst.UnionDecl.Small = @bitCast(extended.small);
3699 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.UnionDecl).@"struct".fields.len);
3700 extra_index += @intFromBool(small.has_tag_type);
3701 const captures_len = if (small.has_captures_len) captures_len: {
3702 const captures_len = zir.extra[extra_index];
3703 extra_index += 1;
3704 break :captures_len captures_len;
3705 } else 0;
3706 extra_index += @intFromBool(small.has_body_len);
3707 extra_index += @intFromBool(small.has_fields_len);
3708 const decls_len = if (small.has_decls_len) decls_len: {
3709 const decls_len = zir.extra[extra_index];
3710 extra_index += 1;
3711 break :decls_len decls_len;
3712 } else 0;
3674 return .{
3675 .extra_index = extra_index,
3676 .decls_remaining = decls_len,
3677 .zir = zir,
3678 };
3679 },
3680 .union_decl => {
3681 const small: Inst.UnionDecl.Small = @bitCast(extended.small);
3682 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.UnionDecl).@"struct".fields.len);
3683 extra_index += @intFromBool(small.has_tag_type);
3684 const captures_len = if (small.has_captures_len) captures_len: {
3685 const captures_len = zir.extra[extra_index];
3686 extra_index += 1;
3687 break :captures_len captures_len;
3688 } else 0;
3689 extra_index += @intFromBool(small.has_body_len);
3690 extra_index += @intFromBool(small.has_fields_len);
3691 const decls_len = if (small.has_decls_len) decls_len: {
3692 const decls_len = zir.extra[extra_index];
3693 extra_index += 1;
3694 break :decls_len decls_len;
3695 } else 0;
37133696
3714 extra_index += captures_len;
3697 extra_index += captures_len;
37153698
3716 return .{
3717 .extra_index = extra_index,
3718 .decls_remaining = decls_len,
3719 .zir = zir,
3720 };
3721 },
3722 .opaque_decl => {
3723 const small: Inst.OpaqueDecl.Small = @bitCast(extended.small);
3724 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.OpaqueDecl).@"struct".fields.len);
3725 const decls_len = if (small.has_decls_len) decls_len: {
3726 const decls_len = zir.extra[extra_index];
3727 extra_index += 1;
3728 break :decls_len decls_len;
3729 } else 0;
3730 const captures_len = if (small.has_captures_len) captures_len: {
3731 const captures_len = zir.extra[extra_index];
3732 extra_index += 1;
3733 break :captures_len captures_len;
3734 } else 0;
3699 return .{
3700 .extra_index = extra_index,
3701 .decls_remaining = decls_len,
3702 .zir = zir,
3703 };
3704 },
3705 .opaque_decl => {
3706 const small: Inst.OpaqueDecl.Small = @bitCast(extended.small);
3707 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.OpaqueDecl).@"struct".fields.len);
3708 const decls_len = if (small.has_decls_len) decls_len: {
3709 const decls_len = zir.extra[extra_index];
3710 extra_index += 1;
3711 break :decls_len decls_len;
3712 } else 0;
3713 const captures_len = if (small.has_captures_len) captures_len: {
3714 const captures_len = zir.extra[extra_index];
3715 extra_index += 1;
3716 break :captures_len captures_len;
3717 } else 0;
37353718
3736 extra_index += captures_len;
3719 extra_index += captures_len;
37373720
3738 return .{
3739 .extra_index = extra_index,
3740 .decls_remaining = decls_len,
3741 .zir = zir,
3742 };
3743 },
3744 else => unreachable,
3745 }
3721 return .{
3722 .extra_index = extra_index,
3723 .decls_remaining = decls_len,
3724 .zir = zir,
3725 };
37463726 },
37473727 else => unreachable,
37483728 }
37493729}
37503730
3751/// Find all type declarations, recursively, within a `declaration` instruction. Does not recurse through
3752/// said type declarations' declarations; to find all declarations, call this function on the declarations
3753/// of the discovered types recursively.
3754/// The iterator would have to allocate memory anyway to iterate, so an `ArrayList` is populated as the result.
3755pub fn findDecls(zir: Zir, gpa: Allocator, list: *std.ArrayListUnmanaged(Inst.Index), decl_inst: Zir.Inst.Index) !void {
3756 list.clearRetainingCapacity();
3731/// `DeclContents` contains all "interesting" instructions found within a declaration by `findTrackable`.
3732/// These instructions are partitioned into a few different sets, since this makes ZIR instruction mapping
3733/// more effective.
3734pub const DeclContents = struct {
3735 /// This is a simple optional because ZIR guarantees that a `func`/`func_inferred`/`func_fancy` instruction
3736 /// can only occur once per `declaration`.
3737 func_decl: ?Inst.Index,
3738 explicit_types: std.ArrayListUnmanaged(Inst.Index),
3739 other: std.ArrayListUnmanaged(Inst.Index),
3740
3741 pub const init: DeclContents = .{
3742 .func_decl = null,
3743 .explicit_types = .empty,
3744 .other = .empty,
3745 };
3746
3747 pub fn clear(contents: *DeclContents) void {
3748 contents.func_decl = null;
3749 contents.explicit_types.clearRetainingCapacity();
3750 contents.other.clearRetainingCapacity();
3751 }
3752
3753 pub fn deinit(contents: *DeclContents, gpa: Allocator) void {
3754 contents.explicit_types.deinit(gpa);
3755 contents.other.deinit(gpa);
3756 }
3757};
3758
3759/// Find all tracked ZIR instructions, recursively, within a `declaration` instruction. Does not recurse through
3760/// nested declarations; to find all declarations, call this function recursively on the type declarations discovered
3761/// in `contents.explicit_types`.
3762///
3763/// This populates an `ArrayListUnmanaged` because an iterator would need to allocate memory anyway.
3764pub fn findTrackable(zir: Zir, gpa: Allocator, contents: *DeclContents, decl_inst: Zir.Inst.Index) !void {
3765 contents.clear();
3766
37573767 const declaration, const extra_end = zir.getDeclaration(decl_inst);
37583768 const bodies = declaration.getBodies(extra_end, zir);
37593769
......@@ -3762,27 +3772,27 @@ pub fn findDecls(zir: Zir, gpa: Allocator, list: *std.ArrayListUnmanaged(Inst.In
37623772 var found_defers: std.AutoHashMapUnmanaged(u32, void) = .empty;
37633773 defer found_defers.deinit(gpa);
37643774
3765 try zir.findDeclsBody(gpa, list, &found_defers, bodies.value_body);
3766 if (bodies.align_body) |b| try zir.findDeclsBody(gpa, list, &found_defers, b);
3767 if (bodies.linksection_body) |b| try zir.findDeclsBody(gpa, list, &found_defers, b);
3768 if (bodies.addrspace_body) |b| try zir.findDeclsBody(gpa, list, &found_defers, b);
3775 try zir.findTrackableBody(gpa, contents, &found_defers, bodies.value_body);
3776 if (bodies.align_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
3777 if (bodies.linksection_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
3778 if (bodies.addrspace_body) |b| try zir.findTrackableBody(gpa, contents, &found_defers, b);
37693779}
37703780
3771/// Like `findDecls`, but only considers the `main_struct_inst` instruction. This may return more than
3781/// Like `findTrackable`, but only considers the `main_struct_inst` instruction. This may return more than
37723782/// just that instruction because it will also traverse fields.
3773pub fn findDeclsRoot(zir: Zir, gpa: Allocator, list: *std.ArrayListUnmanaged(Inst.Index)) !void {
3774 list.clearRetainingCapacity();
3783pub fn findTrackableRoot(zir: Zir, gpa: Allocator, contents: *DeclContents) !void {
3784 contents.clear();
37753785
37763786 var found_defers: std.AutoHashMapUnmanaged(u32, void) = .empty;
37773787 defer found_defers.deinit(gpa);
37783788
3779 try zir.findDeclsInner(gpa, list, &found_defers, .main_struct_inst);
3789 try zir.findTrackableInner(gpa, contents, &found_defers, .main_struct_inst);
37803790}
37813791
3782fn findDeclsInner(
3792fn findTrackableInner(
37833793 zir: Zir,
37843794 gpa: Allocator,
3785 list: *std.ArrayListUnmanaged(Inst.Index),
3795 contents: *DeclContents,
37863796 defers: *std.AutoHashMapUnmanaged(u32, void),
37873797 inst: Inst.Index,
37883798) Allocator.Error!void {
......@@ -4026,7 +4036,7 @@ fn findDeclsInner(
40264036 .struct_init,
40274037 .struct_init_ref,
40284038 .struct_init_anon,
4029 => return list.append(gpa, inst),
4039 => return contents.other.append(gpa, inst),
40304040
40314041 .extended => {
40324042 const extended = datas[@intFromEnum(inst)].extended;
......@@ -4093,15 +4103,15 @@ fn findDeclsInner(
40934103 .typeof_peer => {
40944104 const extra = zir.extraData(Zir.Inst.TypeOfPeer, extended.operand);
40954105 const body = zir.bodySlice(extra.data.body_index, extra.data.body_len);
4096 try zir.findDeclsBody(gpa, list, defers, body);
4106 try zir.findTrackableBody(gpa, contents, defers, body);
40974107 },
40984108
40994109 // Reifications and opaque declarations need tracking, but have no body.
4100 .reify, .opaque_decl => return list.append(gpa, inst),
4110 .reify, .opaque_decl => return contents.other.append(gpa, inst),
41014111
41024112 // Struct declarations need tracking and have bodies.
41034113 .struct_decl => {
4104 try list.append(gpa, inst);
4114 try contents.explicit_types.append(gpa, inst);
41054115
41064116 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
41074117 const extra = zir.extraData(Zir.Inst.StructDecl, extended.operand);
......@@ -4130,7 +4140,7 @@ fn findDeclsInner(
41304140 } else {
41314141 const body = zir.bodySlice(extra_index, backing_int_body_len);
41324142 extra_index += backing_int_body_len;
4133 try zir.findDeclsBody(gpa, list, defers, body);
4143 try zir.findTrackableBody(gpa, contents, defers, body);
41344144 }
41354145 }
41364146 extra_index += decls_len;
......@@ -4186,12 +4196,12 @@ fn findDeclsInner(
41864196
41874197 // Now, `fields_extra_index` points to `bodies`. Let's treat this as one big body.
41884198 const merged_bodies = zir.bodySlice(fields_extra_index, total_bodies_len);
4189 try zir.findDeclsBody(gpa, list, defers, merged_bodies);
4199 try zir.findTrackableBody(gpa, contents, defers, merged_bodies);
41904200 },
41914201
41924202 // Union declarations need tracking and have a body.
41934203 .union_decl => {
4194 try list.append(gpa, inst);
4204 try contents.explicit_types.append(gpa, inst);
41954205
41964206 const small: Zir.Inst.UnionDecl.Small = @bitCast(extended.small);
41974207 const extra = zir.extraData(Zir.Inst.UnionDecl, extended.operand);
......@@ -4216,12 +4226,12 @@ fn findDeclsInner(
42164226 extra_index += captures_len;
42174227 extra_index += decls_len;
42184228 const body = zir.bodySlice(extra_index, body_len);
4219 try zir.findDeclsBody(gpa, list, defers, body);
4229 try zir.findTrackableBody(gpa, contents, defers, body);
42204230 },
42214231
42224232 // Enum declarations need tracking and have a body.
42234233 .enum_decl => {
4224 try list.append(gpa, inst);
4234 try contents.explicit_types.append(gpa, inst);
42254235
42264236 const small: Zir.Inst.EnumDecl.Small = @bitCast(extended.small);
42274237 const extra = zir.extraData(Zir.Inst.EnumDecl, extended.operand);
......@@ -4246,7 +4256,7 @@ fn findDeclsInner(
42464256 extra_index += captures_len;
42474257 extra_index += decls_len;
42484258 const body = zir.bodySlice(extra_index, body_len);
4249 try zir.findDeclsBody(gpa, list, defers, body);
4259 try zir.findTrackableBody(gpa, contents, defers, body);
42504260 },
42514261 }
42524262 },
......@@ -4255,7 +4265,8 @@ fn findDeclsInner(
42554265 .func,
42564266 .func_inferred,
42574267 => {
4258 try list.append(gpa, inst);
4268 assert(contents.func_decl == null);
4269 contents.func_decl = inst;
42594270
42604271 const inst_data = datas[@intFromEnum(inst)].pl_node;
42614272 const extra = zir.extraData(Inst.Func, inst_data.payload_index);
......@@ -4266,14 +4277,15 @@ fn findDeclsInner(
42664277 else => {
42674278 const body = zir.bodySlice(extra_index, extra.data.ret_body_len);
42684279 extra_index += body.len;
4269 try zir.findDeclsBody(gpa, list, defers, body);
4280 try zir.findTrackableBody(gpa, contents, defers, body);
42704281 },
42714282 }
42724283 const body = zir.bodySlice(extra_index, extra.data.body_len);
4273 return zir.findDeclsBody(gpa, list, defers, body);
4284 return zir.findTrackableBody(gpa, contents, defers, body);
42744285 },
42754286 .func_fancy => {
4276 try list.append(gpa, inst);
4287 assert(contents.func_decl == null);
4288 contents.func_decl = inst;
42774289
42784290 const inst_data = datas[@intFromEnum(inst)].pl_node;
42794291 const extra = zir.extraData(Inst.FuncFancy, inst_data.payload_index);
......@@ -4284,7 +4296,7 @@ fn findDeclsInner(
42844296 const body_len = zir.extra[extra_index];
42854297 extra_index += 1;
42864298 const body = zir.bodySlice(extra_index, body_len);
4287 try zir.findDeclsBody(gpa, list, defers, body);
4299 try zir.findTrackableBody(gpa, contents, defers, body);
42884300 extra_index += body.len;
42894301 } else if (extra.data.bits.has_align_ref) {
42904302 extra_index += 1;
......@@ -4294,7 +4306,7 @@ fn findDeclsInner(
42944306 const body_len = zir.extra[extra_index];
42954307 extra_index += 1;
42964308 const body = zir.bodySlice(extra_index, body_len);
4297 try zir.findDeclsBody(gpa, list, defers, body);
4309 try zir.findTrackableBody(gpa, contents, defers, body);
42984310 extra_index += body.len;
42994311 } else if (extra.data.bits.has_addrspace_ref) {
43004312 extra_index += 1;
......@@ -4304,7 +4316,7 @@ fn findDeclsInner(
43044316 const body_len = zir.extra[extra_index];
43054317 extra_index += 1;
43064318 const body = zir.bodySlice(extra_index, body_len);
4307 try zir.findDeclsBody(gpa, list, defers, body);
4319 try zir.findTrackableBody(gpa, contents, defers, body);
43084320 extra_index += body.len;
43094321 } else if (extra.data.bits.has_section_ref) {
43104322 extra_index += 1;
......@@ -4314,7 +4326,7 @@ fn findDeclsInner(
43144326 const body_len = zir.extra[extra_index];
43154327 extra_index += 1;
43164328 const body = zir.bodySlice(extra_index, body_len);
4317 try zir.findDeclsBody(gpa, list, defers, body);
4329 try zir.findTrackableBody(gpa, contents, defers, body);
43184330 extra_index += body.len;
43194331 } else if (extra.data.bits.has_cc_ref) {
43204332 extra_index += 1;
......@@ -4324,7 +4336,7 @@ fn findDeclsInner(
43244336 const body_len = zir.extra[extra_index];
43254337 extra_index += 1;
43264338 const body = zir.bodySlice(extra_index, body_len);
4327 try zir.findDeclsBody(gpa, list, defers, body);
4339 try zir.findTrackableBody(gpa, contents, defers, body);
43284340 extra_index += body.len;
43294341 } else if (extra.data.bits.has_ret_ty_ref) {
43304342 extra_index += 1;
......@@ -4333,7 +4345,7 @@ fn findDeclsInner(
43334345 extra_index += @intFromBool(extra.data.bits.has_any_noalias);
43344346
43354347 const body = zir.bodySlice(extra_index, extra.data.body_len);
4336 return zir.findDeclsBody(gpa, list, defers, body);
4348 return zir.findTrackableBody(gpa, contents, defers, body);
43374349 },
43384350
43394351 // Block instructions, recurse over the bodies.
......@@ -4348,24 +4360,24 @@ fn findDeclsInner(
43484360 const inst_data = datas[@intFromEnum(inst)].pl_node;
43494361 const extra = zir.extraData(Inst.Block, inst_data.payload_index);
43504362 const body = zir.bodySlice(extra.end, extra.data.body_len);
4351 return zir.findDeclsBody(gpa, list, defers, body);
4363 return zir.findTrackableBody(gpa, contents, defers, body);
43524364 },
43534365 .condbr, .condbr_inline => {
43544366 const inst_data = datas[@intFromEnum(inst)].pl_node;
43554367 const extra = zir.extraData(Inst.CondBr, inst_data.payload_index);
43564368 const then_body = zir.bodySlice(extra.end, extra.data.then_body_len);
43574369 const else_body = zir.bodySlice(extra.end + then_body.len, extra.data.else_body_len);
4358 try zir.findDeclsBody(gpa, list, defers, then_body);
4359 try zir.findDeclsBody(gpa, list, defers, else_body);
4370 try zir.findTrackableBody(gpa, contents, defers, then_body);
4371 try zir.findTrackableBody(gpa, contents, defers, else_body);
43604372 },
43614373 .@"try", .try_ptr => {
43624374 const inst_data = datas[@intFromEnum(inst)].pl_node;
43634375 const extra = zir.extraData(Inst.Try, inst_data.payload_index);
43644376 const body = zir.bodySlice(extra.end, extra.data.body_len);
4365 try zir.findDeclsBody(gpa, list, defers, body);
4377 try zir.findTrackableBody(gpa, contents, defers, body);
43664378 },
4367 .switch_block, .switch_block_ref => return zir.findDeclsSwitch(gpa, list, defers, inst, .normal),
4368 .switch_block_err_union => return zir.findDeclsSwitch(gpa, list, defers, inst, .err_union),
4379 .switch_block, .switch_block_ref => return zir.findTrackableSwitch(gpa, contents, defers, inst, .normal),
4380 .switch_block_err_union => return zir.findTrackableSwitch(gpa, contents, defers, inst, .err_union),
43694381
43704382 .suspend_block => @panic("TODO iterate suspend block"),
43714383
......@@ -4373,7 +4385,7 @@ fn findDeclsInner(
43734385 const inst_data = datas[@intFromEnum(inst)].pl_tok;
43744386 const extra = zir.extraData(Inst.Param, inst_data.payload_index);
43754387 const body = zir.bodySlice(extra.end, extra.data.body_len);
4376 try zir.findDeclsBody(gpa, list, defers, body);
4388 try zir.findTrackableBody(gpa, contents, defers, body);
43774389 },
43784390
43794391 inline .call, .field_call => |tag| {
......@@ -4389,7 +4401,7 @@ fn findDeclsInner(
43894401 const first_arg_start_off = args_len;
43904402 const final_arg_end_off = zir.extra[extra.end + args_len - 1];
43914403 const args_body = zir.bodySlice(extra.end + first_arg_start_off, final_arg_end_off - first_arg_start_off);
4392 try zir.findDeclsBody(gpa, list, defers, args_body);
4404 try zir.findTrackableBody(gpa, contents, defers, args_body);
43934405 }
43944406 },
43954407 .@"defer" => {
......@@ -4397,7 +4409,7 @@ fn findDeclsInner(
43974409 const gop = try defers.getOrPut(gpa, inst_data.index);
43984410 if (!gop.found_existing) {
43994411 const body = zir.bodySlice(inst_data.index, inst_data.len);
4400 try zir.findDeclsBody(gpa, list, defers, body);
4412 try zir.findTrackableBody(gpa, contents, defers, body);
44014413 }
44024414 },
44034415 .defer_err_code => {
......@@ -4406,16 +4418,16 @@ fn findDeclsInner(
44064418 const gop = try defers.getOrPut(gpa, extra.index);
44074419 if (!gop.found_existing) {
44084420 const body = zir.bodySlice(extra.index, extra.len);
4409 try zir.findDeclsBody(gpa, list, defers, body);
4421 try zir.findTrackableBody(gpa, contents, defers, body);
44104422 }
44114423 },
44124424 }
44134425}
44144426
4415fn findDeclsSwitch(
4427fn findTrackableSwitch(
44164428 zir: Zir,
44174429 gpa: Allocator,
4418 list: *std.ArrayListUnmanaged(Inst.Index),
4430 contents: *DeclContents,
44194431 defers: *std.AutoHashMapUnmanaged(u32, void),
44204432 inst: Inst.Index,
44214433 /// Distinguishes between `switch_block[_ref]` and `switch_block_err_union`.
......@@ -4451,7 +4463,7 @@ fn findDeclsSwitch(
44514463 const body = zir.bodySlice(extra_index, prong_info.body_len);
44524464 extra_index += body.len;
44534465
4454 try zir.findDeclsBody(gpa, list, defers, body);
4466 try zir.findTrackableBody(gpa, contents, defers, body);
44554467
44564468 break :has_special extra.data.bits.has_else;
44574469 },
......@@ -4463,7 +4475,7 @@ fn findDeclsSwitch(
44634475 const body = zir.bodySlice(extra_index, prong_info.body_len);
44644476 extra_index += body.len;
44654477
4466 try zir.findDeclsBody(gpa, list, defers, body);
4478 try zir.findTrackableBody(gpa, contents, defers, body);
44674479 }
44684480
44694481 {
......@@ -4475,7 +4487,7 @@ fn findDeclsSwitch(
44754487 const body = zir.bodySlice(extra_index, prong_info.body_len);
44764488 extra_index += body.len;
44774489
4478 try zir.findDeclsBody(gpa, list, defers, body);
4490 try zir.findTrackableBody(gpa, contents, defers, body);
44794491 }
44804492 }
44814493 {
......@@ -4492,20 +4504,20 @@ fn findDeclsSwitch(
44924504 const body = zir.bodySlice(extra_index, prong_info.body_len);
44934505 extra_index += body.len;
44944506
4495 try zir.findDeclsBody(gpa, list, defers, body);
4507 try zir.findTrackableBody(gpa, contents, defers, body);
44964508 }
44974509 }
44984510}
44994511
4500fn findDeclsBody(
4512fn findTrackableBody(
45014513 zir: Zir,
45024514 gpa: Allocator,
4503 list: *std.ArrayListUnmanaged(Inst.Index),
4515 contents: *DeclContents,
45044516 defers: *std.AutoHashMapUnmanaged(u32, void),
45054517 body: []const Inst.Index,
45064518) Allocator.Error!void {
45074519 for (body) |member| {
4508 try zir.findDeclsInner(gpa, list, defers, member);
4520 try zir.findTrackableInner(gpa, contents, defers, member);
45094521 }
45104522}
45114523
src/Zcu.zig+61-21
......@@ -2593,26 +2593,44 @@ pub fn mapOldZirToNew(
25932593 defer match_stack.deinit(gpa);
25942594
25952595 // Used as temporary buffers for namespace declaration instructions
2596 var old_decls: std.ArrayListUnmanaged(Zir.Inst.Index) = .empty;
2597 defer old_decls.deinit(gpa);
2598 var new_decls: std.ArrayListUnmanaged(Zir.Inst.Index) = .empty;
2599 defer new_decls.deinit(gpa);
2596 var old_contents: Zir.DeclContents = .init;
2597 defer old_contents.deinit(gpa);
2598 var new_contents: Zir.DeclContents = .init;
2599 defer new_contents.deinit(gpa);
26002600
26012601 // Map the main struct inst (and anything in its fields)
26022602 {
2603 try old_zir.findDeclsRoot(gpa, &old_decls);
2604 try new_zir.findDeclsRoot(gpa, &new_decls);
2603 try old_zir.findTrackableRoot(gpa, &old_contents);
2604 try new_zir.findTrackableRoot(gpa, &new_contents);
26052605
2606 assert(old_decls.items[0] == .main_struct_inst);
2607 assert(new_decls.items[0] == .main_struct_inst);
2606 assert(old_contents.explicit_types.items[0] == .main_struct_inst);
2607 assert(new_contents.explicit_types.items[0] == .main_struct_inst);
26082608
2609 // We don't have any smart way of matching up these type declarations, so we always
2610 // correlate them based on source order.
2611 const n = @min(old_decls.items.len, new_decls.items.len);
2612 try match_stack.ensureUnusedCapacity(gpa, n);
2613 for (old_decls.items[0..n], new_decls.items[0..n]) |old_inst, new_inst| {
2609 assert(old_contents.func_decl == null);
2610 assert(new_contents.func_decl == null);
2611
2612 // We don't have any smart way of matching up these instructions, so we correlate them based on source order
2613 // in their respective arrays.
2614
2615 const num_explicit_types = @min(old_contents.explicit_types.items.len, new_contents.explicit_types.items.len);
2616 try match_stack.ensureUnusedCapacity(gpa, @intCast(num_explicit_types));
2617 for (
2618 old_contents.explicit_types.items[0..num_explicit_types],
2619 new_contents.explicit_types.items[0..num_explicit_types],
2620 ) |old_inst, new_inst| {
2621 // Here we use `match_stack`, so that we will recursively consider declarations on these types.
26142622 match_stack.appendAssumeCapacity(.{ .old_inst = old_inst, .new_inst = new_inst });
26152623 }
2624
2625 const num_other = @min(old_contents.other.items.len, new_contents.other.items.len);
2626 try inst_map.ensureUnusedCapacity(gpa, @intCast(num_other));
2627 for (
2628 old_contents.other.items[0..num_other],
2629 new_contents.other.items[0..num_other],
2630 ) |old_inst, new_inst| {
2631 // These instructions don't have declarations, so we just modify `inst_map` directly.
2632 inst_map.putAssumeCapacity(old_inst, new_inst);
2633 }
26162634 }
26172635
26182636 while (match_stack.popOrNull()) |match_item| {
......@@ -2700,17 +2718,39 @@ pub fn mapOldZirToNew(
27002718 // Match the `declaration` instruction
27012719 try inst_map.put(gpa, old_decl_inst, new_decl_inst);
27022720
2703 // Find container type declarations within this declaration
2704 try old_zir.findDecls(gpa, &old_decls, old_decl_inst);
2705 try new_zir.findDecls(gpa, &new_decls, new_decl_inst);
2721 // Find trackable instructions within this declaration
2722 try old_zir.findTrackable(gpa, &old_contents, old_decl_inst);
2723 try new_zir.findTrackable(gpa, &new_contents, new_decl_inst);
2724
2725 // We don't have any smart way of matching up these instructions, so we correlate them based on source order
2726 // in their respective arrays.
27062727
2707 // We don't have any smart way of matching up these type declarations, so we always
2708 // correlate them based on source order.
2709 const n = @min(old_decls.items.len, new_decls.items.len);
2710 try match_stack.ensureUnusedCapacity(gpa, n);
2711 for (old_decls.items[0..n], new_decls.items[0..n]) |old_inst, new_inst| {
2728 const num_explicit_types = @min(old_contents.explicit_types.items.len, new_contents.explicit_types.items.len);
2729 try match_stack.ensureUnusedCapacity(gpa, @intCast(num_explicit_types));
2730 for (
2731 old_contents.explicit_types.items[0..num_explicit_types],
2732 new_contents.explicit_types.items[0..num_explicit_types],
2733 ) |old_inst, new_inst| {
2734 // Here we use `match_stack`, so that we will recursively consider declarations on these types.
27122735 match_stack.appendAssumeCapacity(.{ .old_inst = old_inst, .new_inst = new_inst });
27132736 }
2737
2738 const num_other = @min(old_contents.other.items.len, new_contents.other.items.len);
2739 try inst_map.ensureUnusedCapacity(gpa, @intCast(num_other));
2740 for (
2741 old_contents.other.items[0..num_other],
2742 new_contents.other.items[0..num_other],
2743 ) |old_inst, new_inst| {
2744 // These instructions don't have declarations, so we just modify `inst_map` directly.
2745 inst_map.putAssumeCapacity(old_inst, new_inst);
2746 }
2747
2748 if (old_contents.func_decl) |old_func_inst| {
2749 if (new_contents.func_decl) |new_func_inst| {
2750 // There are no declarations on a function either, so again, we just directly add it to `inst_map`.
2751 try inst_map.put(gpa, old_func_inst, new_func_inst);
2752 }
2753 }
27142754 }
27152755 }
27162756}