| author | |
| committer | |
| log | b039a8b6155e1eddd9f447ff261d56771c12e038 |
| tree | 0c6c6d44c4e8d6b89a260d61f4472ebc0c9186f5 |
| parent | 136c5a916ed7e421461ac5839cc0e4c289b80f16 |
Rather than `Zcu.BuiltinDecl.Memoized` being a struct with fields, it
can instead just be an array, indexed by the enum. This allows runtime
indexing, avoiding a few now-unnecessary `inline` switch cases.4 files changed, 39 insertions(+), 31 deletions(-)
src/Sema.zig+13-18| ... | @@ -27612,13 +27612,8 @@ fn explainWhyTypeIsNotPacked( | ... | @@ -27612,13 +27612,8 @@ fn explainWhyTypeIsNotPacked( |
| 27612 | fn preparePanicId(sema: *Sema, src: LazySrcLoc, panic_id: Zcu.PanicId) !InternPool.Index { | 27612 | fn preparePanicId(sema: *Sema, src: LazySrcLoc, panic_id: Zcu.PanicId) !InternPool.Index { |
| 27613 | const zcu = sema.pt.zcu; | 27613 | const zcu = sema.pt.zcu; |
| 27614 | try sema.ensureMemoizedStateResolved(src, .panic); | 27614 | try sema.ensureMemoizedStateResolved(src, .panic); |
| 27615 | try zcu.ensureFuncBodyAnalysisQueued(zcu.builtin_decl_values.@"Panic.call"); | 27615 | try zcu.ensureFuncBodyAnalysisQueued(zcu.builtin_decl_values.get(.@"Panic.call")); |
| 27616 | switch (panic_id) { | 27616 | return zcu.builtin_decl_values.get(panic_id.toBuiltin()); |
| 27617 | inline else => |ct_panic_id| { | ||
| 27618 | const name = "Panic.messages." ++ @tagName(ct_panic_id); | ||
| 27619 | return @field(zcu.builtin_decl_values, name); | ||
| 27620 | }, | ||
| 27621 | } | ||
| 27622 | } | 27617 | } |
| 27623 | 27618 | ||
| 27624 | fn addSafetyCheck( | 27619 | fn addSafetyCheck( |
| ... | @@ -27723,9 +27718,9 @@ fn panicWithMsg(sema: *Sema, block: *Block, src: LazySrcLoc, msg_inst: Air.Inst. | ... | @@ -27723,9 +27718,9 @@ fn panicWithMsg(sema: *Sema, block: *Block, src: LazySrcLoc, msg_inst: Air.Inst. |
| 27723 | } | 27718 | } |
| 27724 | 27719 | ||
| 27725 | try sema.ensureMemoizedStateResolved(src, .panic); | 27720 | try sema.ensureMemoizedStateResolved(src, .panic); |
| 27726 | try zcu.ensureFuncBodyAnalysisQueued(zcu.builtin_decl_values.@"Panic.call"); | 27721 | try zcu.ensureFuncBodyAnalysisQueued(zcu.builtin_decl_values.get(.@"Panic.call")); |
| 27727 | 27722 | ||
| 27728 | const panic_fn = Air.internedToRef(zcu.builtin_decl_values.@"Panic.call"); | 27723 | const panic_fn = Air.internedToRef(zcu.builtin_decl_values.get(.@"Panic.call")); |
| 27729 | const null_stack_trace = Air.internedToRef(zcu.null_stack_trace); | 27724 | const null_stack_trace = Air.internedToRef(zcu.null_stack_trace); |
| 27730 | 27725 | ||
| 27731 | const opt_usize_ty = try pt.optionalType(.usize_type); | 27726 | const opt_usize_ty = try pt.optionalType(.usize_type); |
| ... | @@ -38720,15 +38715,15 @@ const ComptimeLoadResult = @import("Sema/comptime_ptr_access.zig").ComptimeLoadR | ... | @@ -38720,15 +38715,15 @@ const ComptimeLoadResult = @import("Sema/comptime_ptr_access.zig").ComptimeLoadR |
| 38720 | const storeComptimePtr = @import("Sema/comptime_ptr_access.zig").storeComptimePtr; | 38715 | const storeComptimePtr = @import("Sema/comptime_ptr_access.zig").storeComptimePtr; |
| 38721 | const ComptimeStoreResult = @import("Sema/comptime_ptr_access.zig").ComptimeStoreResult; | 38716 | const ComptimeStoreResult = @import("Sema/comptime_ptr_access.zig").ComptimeStoreResult; |
| 38722 | 38717 | ||
| 38723 | pub fn getBuiltinType(sema: *Sema, src: LazySrcLoc, comptime decl: Zcu.BuiltinDecl) SemaError!Type { | 38718 | pub fn getBuiltinType(sema: *Sema, src: LazySrcLoc, decl: Zcu.BuiltinDecl) SemaError!Type { |
| 38724 | comptime assert(decl.kind() == .type); | 38719 | assert(decl.kind() == .type); |
| 38725 | try sema.ensureMemoizedStateResolved(src, decl.stage()); | 38720 | try sema.ensureMemoizedStateResolved(src, decl.stage()); |
| 38726 | return .fromInterned(@field(sema.pt.zcu.builtin_decl_values, @tagName(decl))); | 38721 | return .fromInterned(sema.pt.zcu.builtin_decl_values.get(decl)); |
| 38727 | } | 38722 | } |
| 38728 | pub fn getBuiltin(sema: *Sema, src: LazySrcLoc, comptime decl: Zcu.BuiltinDecl) SemaError!InternPool.Index { | 38723 | pub fn getBuiltin(sema: *Sema, src: LazySrcLoc, decl: Zcu.BuiltinDecl) SemaError!InternPool.Index { |
| 38729 | comptime assert(decl.kind() != .type); | 38724 | assert(decl.kind() != .type); |
| 38730 | try sema.ensureMemoizedStateResolved(src, decl.stage()); | 38725 | try sema.ensureMemoizedStateResolved(src, decl.stage()); |
| 38731 | return @field(sema.pt.zcu.builtin_decl_values, @tagName(decl)); | 38726 | return sema.pt.zcu.builtin_decl_values.get(decl); |
| 38732 | } | 38727 | } |
| 38733 | 38728 | ||
| 38734 | pub const NavPtrModifiers = struct { | 38729 | pub const NavPtrModifiers = struct { |
| ... | @@ -38810,7 +38805,7 @@ pub fn analyzeMemoizedState(sema: *Sema, block: *Block, src: LazySrcLoc, builtin | ... | @@ -38810,7 +38805,7 @@ pub fn analyzeMemoizedState(sema: *Sema, block: *Block, src: LazySrcLoc, builtin |
| 38810 | const parent_ns: Zcu.Namespace.Index, const parent_name: []const u8, const name: []const u8 = switch (comptime builtin_decl.access()) { | 38805 | const parent_ns: Zcu.Namespace.Index, const parent_name: []const u8, const name: []const u8 = switch (comptime builtin_decl.access()) { |
| 38811 | .direct => |name| .{ builtin_namespace, "std.builtin", name }, | 38806 | .direct => |name| .{ builtin_namespace, "std.builtin", name }, |
| 38812 | .nested => |nested| access: { | 38807 | .nested => |nested| access: { |
| 38813 | const parent_ty: Type = .fromInterned(@field(zcu.builtin_decl_values, @tagName(nested[0]))); | 38808 | const parent_ty: Type = .fromInterned(zcu.builtin_decl_values.get(nested[0])); |
| 38814 | const parent_ns = parent_ty.getNamespace(zcu).unwrap() orelse { | 38809 | const parent_ns = parent_ty.getNamespace(zcu).unwrap() orelse { |
| 38815 | return sema.fail(block, src, "std.builtin.{s} is not a container type", .{@tagName(nested[0])}); | 38810 | return sema.fail(block, src, "std.builtin.{s} is not a container type", .{@tagName(nested[0])}); |
| 38816 | }; | 38811 | }; |
| ... | @@ -38845,9 +38840,9 @@ pub fn analyzeMemoizedState(sema: *Sema, block: *Block, src: LazySrcLoc, builtin | ... | @@ -38845,9 +38840,9 @@ pub fn analyzeMemoizedState(sema: *Sema, block: *Block, src: LazySrcLoc, builtin |
| 38845 | }, | 38840 | }, |
| 38846 | } | 38841 | } |
| 38847 | 38842 | ||
| 38848 | const prev = @field(zcu.builtin_decl_values, @tagName(builtin_decl)); | 38843 | const prev = zcu.builtin_decl_values.get(builtin_decl); |
| 38849 | if (val.toIntern() != prev) { | 38844 | if (val.toIntern() != prev) { |
| 38850 | @field(zcu.builtin_decl_values, @tagName(builtin_decl)) = val.toIntern(); | 38845 | zcu.builtin_decl_values.set(builtin_decl, val.toIntern()); |
| 38851 | any_changed = true; | 38846 | any_changed = true; |
| 38852 | } | 38847 | } |
| 38853 | } | 38848 | } |
src/Zcu.zig+18-3| ... | @@ -217,8 +217,8 @@ all_type_references: std.ArrayListUnmanaged(TypeReference) = .empty, | ... | @@ -217,8 +217,8 @@ all_type_references: std.ArrayListUnmanaged(TypeReference) = .empty, |
| 217 | /// Freelist of indices in `all_type_references`. | 217 | /// Freelist of indices in `all_type_references`. |
| 218 | free_type_references: std.ArrayListUnmanaged(u32) = .empty, | 218 | free_type_references: std.ArrayListUnmanaged(u32) = .empty, |
| 219 | 219 | ||
| 220 | /// Populated by analysis of `AnalUnit.wrap(.{ .memoized_state = s })`, where `s` depends on the field. | 220 | /// Populated by analysis of `AnalUnit.wrap(.{ .memoized_state = s })`, where `s` depends on the element. |
| 221 | builtin_decl_values: BuiltinDecl.Memoized = .{}, | 221 | builtin_decl_values: BuiltinDecl.Memoized = .initFill(.none), |
| 222 | /// Populated by analysis of `AnalUnit.wrap(.{ .memoized_state = .panic })`. | 222 | /// Populated by analysis of `AnalUnit.wrap(.{ .memoized_state = .panic })`. |
| 223 | null_stack_trace: InternPool.Index = .none, | 223 | null_stack_trace: InternPool.Index = .none, |
| 224 | 224 | ||
| ... | @@ -420,7 +420,7 @@ pub const BuiltinDecl = enum { | ... | @@ -420,7 +420,7 @@ pub const BuiltinDecl = enum { |
| 420 | }; | 420 | }; |
| 421 | } | 421 | } |
| 422 | 422 | ||
| 423 | const Memoized = std.enums.EnumFieldStruct(BuiltinDecl, InternPool.Index, .none); | 423 | const Memoized = std.enums.EnumArray(BuiltinDecl, InternPool.Index); |
| 424 | }; | 424 | }; |
| 425 | 425 | ||
| 426 | pub const PanicId = enum { | 426 | pub const PanicId = enum { |
| ... | @@ -444,6 +444,21 @@ pub const PanicId = enum { | ... | @@ -444,6 +444,21 @@ pub const PanicId = enum { |
| 444 | memcpy_len_mismatch, | 444 | memcpy_len_mismatch, |
| 445 | memcpy_alias, | 445 | memcpy_alias, |
| 446 | noreturn_returned, | 446 | noreturn_returned, |
| 447 | |||
| 448 | pub fn toBuiltin(id: PanicId) BuiltinDecl { | ||
| 449 | const first_msg: PanicId = @enumFromInt(0); | ||
| 450 | const first_decl = @field(BuiltinDecl, "Panic.messages." ++ @tagName(first_msg)); | ||
| 451 | comptime { | ||
| 452 | // Ensure that the messages are ordered the same in `BuiltinDecl` as they are here. | ||
| 453 | for (@typeInfo(PanicId).@"enum".fields) |panic_field| { | ||
| 454 | const expect_name = "Panic.messages." ++ panic_field.name; | ||
| 455 | const expect_idx = @intFromEnum(first_decl) + panic_field.value; | ||
| 456 | const actual_idx = @intFromEnum(@field(BuiltinDecl, expect_name)); | ||
| 457 | assert(expect_idx == actual_idx); | ||
| 458 | } | ||
| 459 | } | ||
| 460 | return @enumFromInt(@intFromEnum(first_decl) + @intFromEnum(id)); | ||
| 461 | } | ||
| 447 | }; | 462 | }; |
| 448 | 463 | ||
| 449 | pub const GlobalErrorSet = std.AutoArrayHashMapUnmanaged(InternPool.NullTerminatedString, void); | 464 | pub const GlobalErrorSet = std.AutoArrayHashMapUnmanaged(InternPool.NullTerminatedString, void); |
src/Zcu/PerThread.zig+6-6| ... | @@ -590,13 +590,13 @@ pub fn ensureMemoizedStateUpToDate(pt: Zcu.PerThread, stage: InternPool.Memoized | ... | @@ -590,13 +590,13 @@ pub fn ensureMemoizedStateUpToDate(pt: Zcu.PerThread, stage: InternPool.Memoized |
| 590 | _ = zcu.transitive_failed_analysis.swapRemove(unit); | 590 | _ = zcu.transitive_failed_analysis.swapRemove(unit); |
| 591 | } else { | 591 | } else { |
| 592 | if (prev_failed) return error.AnalysisFail; | 592 | if (prev_failed) return error.AnalysisFail; |
| 593 | // We use an arbitrary field to check if the state has been resolved yet. | 593 | // We use an arbitrary element to check if the state has been resolved yet. |
| 594 | const val = switch (stage) { | 594 | const to_check: Zcu.BuiltinDecl = switch (stage) { |
| 595 | .main => zcu.builtin_decl_values.Type, | 595 | .main => .Type, |
| 596 | .panic => zcu.builtin_decl_values.Panic, | 596 | .panic => .Panic, |
| 597 | .va_list => zcu.builtin_decl_values.VaList, | 597 | .va_list => .VaList, |
| 598 | }; | 598 | }; |
| 599 | if (val != .none) return; | 599 | if (zcu.builtin_decl_values.get(to_check) != .none) return; |
| 600 | } | 600 | } |
| 601 | 601 | ||
| 602 | const any_changed: bool, const new_failed: bool = if (pt.analyzeMemoizedState(stage)) |any_changed| | 602 | const any_changed: bool, const new_failed: bool = if (pt.analyzeMemoizedState(stage)) |any_changed| |
src/codegen/llvm.zig+2-4| ... | @@ -5754,9 +5754,7 @@ pub const FuncGen = struct { | ... | @@ -5754,9 +5754,7 @@ pub const FuncGen = struct { |
| 5754 | const o = fg.ng.object; | 5754 | const o = fg.ng.object; |
| 5755 | const zcu = o.pt.zcu; | 5755 | const zcu = o.pt.zcu; |
| 5756 | const ip = &zcu.intern_pool; | 5756 | const ip = &zcu.intern_pool; |
| 5757 | const panic_msg_val: InternPool.Index = switch (panic_id) { | 5757 | const panic_msg_val = zcu.builtin_decl_values.get(panic_id.toBuiltin()); |
| 5758 | inline else => |ct_panic_id| @field(zcu.builtin_decl_values, "Panic.messages." ++ @tagName(ct_panic_id)), | ||
| 5759 | }; | ||
| 5760 | assert(panic_msg_val != .none); | 5758 | assert(panic_msg_val != .none); |
| 5761 | const msg_len = Value.fromInterned(panic_msg_val).typeOf(zcu).childType(zcu).arrayLen(zcu); | 5759 | const msg_len = Value.fromInterned(panic_msg_val).typeOf(zcu).childType(zcu).arrayLen(zcu); |
| 5762 | const msg_ptr = try o.lowerValue(panic_msg_val); | 5760 | const msg_ptr = try o.lowerValue(panic_msg_val); |
| ... | @@ -5770,7 +5768,7 @@ pub const FuncGen = struct { | ... | @@ -5770,7 +5768,7 @@ pub const FuncGen = struct { |
| 5770 | // ptr null, ; stack trace | 5768 | // ptr null, ; stack trace |
| 5771 | // ptr @2, ; addr (null ?usize) | 5769 | // ptr @2, ; addr (null ?usize) |
| 5772 | // ) | 5770 | // ) |
| 5773 | const panic_func = zcu.funcInfo(zcu.builtin_decl_values.@"Panic.call"); | 5771 | const panic_func = zcu.funcInfo(zcu.builtin_decl_values.get(.@"Panic.call")); |
| 5774 | const panic_nav = ip.getNav(panic_func.owner_nav); | 5772 | const panic_nav = ip.getNav(panic_func.owner_nav); |
| 5775 | const fn_info = zcu.typeToFunc(Type.fromInterned(panic_nav.typeOf(ip))).?; | 5773 | const fn_info = zcu.typeToFunc(Type.fromInterned(panic_nav.typeOf(ip))).?; |
| 5776 | const panic_global = try o.resolveLlvmFunction(panic_func.owner_nav); | 5774 | const panic_global = try o.resolveLlvmFunction(panic_func.owner_nav); |