authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-16 17:39:56+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-09-16 17:39:56+01:00
logf3445f8f6935b4532aab3f339f5d86319d2dca72
treecd564962eefa77c38a89b6958e86ebd3bf7fc784
parent5d7fa5513f92a43a418e3c5c4d27f0b61db313ff
parent8ff2f1057a9e9cc258f335e4a26101866be987a9
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21423 from mlugg/field-init-resolution

compiler: always resolve field inits, remove unncecessary eager resolution

3 files changed, 34 insertions(+), 66 deletions(-)

src/Sema.zig+1
...@@ -36337,6 +36337,7 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void {...@@ -36337,6 +36337,7 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void {
36337/// be resolved.36337/// be resolved.
36338pub fn resolveStructFully(sema: *Sema, ty: Type) SemaError!void {36338pub fn resolveStructFully(sema: *Sema, ty: Type) SemaError!void {
36339 try sema.resolveStructLayout(ty);36339 try sema.resolveStructLayout(ty);
36340 try sema.resolveStructFieldInits(ty);
3634036341
36341 const pt = sema.pt;36342 const pt = sema.pt;
36342 const zcu = pt.zcu;36343 const zcu = pt.zcu;
src/Zcu/PerThread.zig+6-15
...@@ -1240,11 +1240,11 @@ fn semaCau(pt: Zcu.PerThread, cau_index: InternPool.Cau.Index) !SemaCauResult {...@@ -1240,11 +1240,11 @@ fn semaCau(pt: Zcu.PerThread, cau_index: InternPool.Cau.Index) !SemaCauResult {
1240 };1240 };
1241 }1241 }
12421242
1243 const nav_already_populated, const queue_linker_work, const resolve_type = switch (ip.indexToKey(decl_val.toIntern())) {1243 const nav_already_populated, const queue_linker_work = switch (ip.indexToKey(decl_val.toIntern())) {
1244 .func => |f| .{ f.owner_nav == nav_index, true, false },1244 .func => |f| .{ f.owner_nav == nav_index, true },
1245 .variable => |v| .{ false, v.owner_nav == nav_index, true },1245 .variable => |v| .{ false, v.owner_nav == nav_index },
1246 .@"extern" => .{ false, false, false },1246 .@"extern" => .{ false, false },
1247 else => .{ false, true, true },1247 else => .{ false, true },
1248 };1248 };
12491249
1250 if (nav_already_populated) {1250 if (nav_already_populated) {
...@@ -1317,16 +1317,7 @@ fn semaCau(pt: Zcu.PerThread, cau_index: InternPool.Cau.Index) !SemaCauResult {...@@ -1317,16 +1317,7 @@ fn semaCau(pt: Zcu.PerThread, cau_index: InternPool.Cau.Index) !SemaCauResult {
1317 queue_codegen: {1317 queue_codegen: {
1318 if (!queue_linker_work) break :queue_codegen;1318 if (!queue_linker_work) break :queue_codegen;
13191319
1320 if (resolve_type) {1320 if (!try decl_ty.hasRuntimeBitsSema(pt)) {
1321 // Needed for codegen_nav which will call updateDecl and then the
1322 // codegen backend wants full access to the Decl Type.
1323 // We also need this for the `isFnOrHasRuntimeBits` check below.
1324 // TODO: we could make the language more lenient by deferring this work
1325 // to the `codegen_nav` job.
1326 try decl_ty.resolveFully(pt);
1327 }
1328
1329 if (!resolve_type or !decl_ty.hasRuntimeBits(zcu)) {
1330 if (zcu.comp.config.use_llvm) break :queue_codegen;1321 if (zcu.comp.config.use_llvm) break :queue_codegen;
1331 if (file.mod.strip) break :queue_codegen;1322 if (file.mod.strip) break :queue_codegen;
1332 }1323 }
src/link/Dwarf.zig+27-51
...@@ -2643,10 +2643,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool...@@ -2643,10 +2643,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
2643 try uleb128(diw, nav_val.toType().abiAlignment(zcu).toByteUnits().?);2643 try uleb128(diw, nav_val.toType().abiAlignment(zcu).toByteUnits().?);
2644 for (0..loaded_struct.field_types.len) |field_index| {2644 for (0..loaded_struct.field_types.len) |field_index| {
2645 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);2645 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);
2646 const field_init = if (loaded_struct.haveFieldInits(ip))2646 const field_init = loaded_struct.fieldInit(ip, field_index);
2647 loaded_struct.fieldInit(ip, field_index)2647 assert(!(is_comptime and field_init == .none));
2648 else
2649 .none;
2650 try wip_nav.abbrevCode(if (is_comptime)2648 try wip_nav.abbrevCode(if (is_comptime)
2651 .struct_field_comptime2649 .struct_field_comptime
2652 else if (field_init != .none)2650 else if (field_init != .none)
...@@ -2658,20 +2656,14 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool...@@ -2658,20 +2656,14 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
2658 defer dwarf.gpa.free(field_name);2656 defer dwarf.gpa.free(field_name);
2659 try wip_nav.strp(field_name);2657 try wip_nav.strp(field_name);
2660 }2658 }
2661 if (is_comptime and field_init == .none) {2659 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]);
2662 // workaround frontend bug2660 try wip_nav.refType(field_type);
2663 try wip_nav.refType(Type.void);2661 if (!is_comptime) {
2664 try wip_nav.blockValue(nav_src_loc, Value.void);2662 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
2665 } else {2663 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
2666 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]);2664 field_type.abiAlignment(zcu).toByteUnits().?);
2667 try wip_nav.refType(field_type);
2668 if (!is_comptime) {
2669 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
2670 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
2671 field_type.abiAlignment(zcu).toByteUnits().?);
2672 }
2673 if (field_init != .none) try wip_nav.blockValue(nav_src_loc, Value.fromInterned(field_init));
2674 }2665 }
2666 if (field_init != .none) try wip_nav.blockValue(nav_src_loc, Value.fromInterned(field_init));
2675 }2667 }
2676 try uleb128(diw, @intFromEnum(AbbrevCode.null));2668 try uleb128(diw, @intFromEnum(AbbrevCode.null));
2677 }2669 }
...@@ -3511,10 +3503,8 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP...@@ -3511,10 +3503,8 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
3511 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);3503 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3512 for (0..loaded_struct.field_types.len) |field_index| {3504 for (0..loaded_struct.field_types.len) |field_index| {
3513 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);3505 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);
3514 const field_init = if (loaded_struct.haveFieldInits(ip))3506 const field_init = loaded_struct.fieldInit(ip, field_index);
3515 loaded_struct.fieldInit(ip, field_index)3507 assert(!(is_comptime and field_init == .none));
3516 else
3517 .none;
3518 try wip_nav.abbrevCode(if (is_comptime)3508 try wip_nav.abbrevCode(if (is_comptime)
3519 .struct_field_comptime3509 .struct_field_comptime
3520 else if (field_init != .none)3510 else if (field_init != .none)
...@@ -3526,20 +3516,14 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP...@@ -3526,20 +3516,14 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
3526 defer dwarf.gpa.free(field_name);3516 defer dwarf.gpa.free(field_name);
3527 try wip_nav.strp(field_name);3517 try wip_nav.strp(field_name);
3528 }3518 }
3529 if (is_comptime and field_init == .none) {3519 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]);
3530 // workaround frontend bug3520 try wip_nav.refType(field_type);
3531 try wip_nav.refType(Type.void);3521 if (!is_comptime) {
3532 try wip_nav.blockValue(ty_src_loc, Value.void);3522 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
3533 } else {3523 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
3534 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]);3524 field_type.abiAlignment(zcu).toByteUnits().?);
3535 try wip_nav.refType(field_type);
3536 if (!is_comptime) {
3537 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
3538 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
3539 field_type.abiAlignment(zcu).toByteUnits().?);
3540 }
3541 if (field_init != .none) try wip_nav.blockValue(ty_src_loc, Value.fromInterned(field_init));
3542 }3525 }
3526 if (field_init != .none) try wip_nav.blockValue(ty_src_loc, Value.fromInterned(field_init));
3543 }3527 }
3544 try uleb128(diw, @intFromEnum(AbbrevCode.null));3528 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3545 }3529 }
...@@ -3595,10 +3579,8 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP...@@ -3595,10 +3579,8 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
3595 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);3579 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3596 for (0..loaded_struct.field_types.len) |field_index| {3580 for (0..loaded_struct.field_types.len) |field_index| {
3597 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);3581 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);
3598 const field_init = if (loaded_struct.haveFieldInits(ip))3582 const field_init = loaded_struct.fieldInit(ip, field_index);
3599 loaded_struct.fieldInit(ip, field_index)3583 assert(!(is_comptime and field_init == .none));
3600 else
3601 .none;
3602 try wip_nav.abbrevCode(if (is_comptime)3584 try wip_nav.abbrevCode(if (is_comptime)
3603 .struct_field_comptime3585 .struct_field_comptime
3604 else if (field_init != .none)3586 else if (field_init != .none)
...@@ -3610,20 +3592,14 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP...@@ -3610,20 +3592,14 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
3610 defer dwarf.gpa.free(field_name);3592 defer dwarf.gpa.free(field_name);
3611 try wip_nav.strp(field_name);3593 try wip_nav.strp(field_name);
3612 }3594 }
3613 if (is_comptime and field_init == .none) {3595 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]);
3614 // workaround frontend bug3596 try wip_nav.refType(field_type);
3615 try wip_nav.refType(Type.void);3597 if (!is_comptime) {
3616 try wip_nav.blockValue(ty_src_loc, Value.void);3598 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
3617 } else {3599 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
3618 const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]);3600 field_type.abiAlignment(zcu).toByteUnits().?);
3619 try wip_nav.refType(field_type);
3620 if (!is_comptime) {
3621 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
3622 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
3623 field_type.abiAlignment(zcu).toByteUnits().?);
3624 }
3625 if (field_init != .none) try wip_nav.blockValue(ty_src_loc, Value.fromInterned(field_init));
3626 }3601 }
3602 if (field_init != .none) try wip_nav.blockValue(ty_src_loc, Value.fromInterned(field_init));
3627 }3603 }
3628 try uleb128(diw, @intFromEnum(AbbrevCode.null));3604 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3629 }3605 }