authorgravatar for mail@linusgroh.deLinus Groh <mail@linusgroh.de> 2024-09-11 22:44:02+01:00
committergravatar for mail@linusgroh.deLinus Groh <mail@linusgroh.de> 2024-09-12 16:01:23+01:00
log9271a89c65967ff0fed7011b4195abdd0f9195eb
tree8b7a7559f0c8ff6dbde8f347b7c5870336a82063
parent8588964972acc473c09e21958a7e52247c978603

InternPool: Replace default values with a .empty declaration


2 files changed, 35 insertions(+), 17 deletions(-)

src/InternPool.zig+34-16
......@@ -2,20 +2,20 @@
22//! This data structure is self-contained.
33
44/// One item per thread, indexed by `tid`, which is dense and unique per thread.
5locals: []Local = &.{},
5locals: []Local,
66/// Length must be a power of two and represents the number of simultaneous
77/// writers that can mutate any single sharded data structure.
8shards: []Shard = &.{},
8shards: []Shard,
99/// Key is the error name, index is the error tag value. Index 0 has a length-0 string.
10global_error_set: GlobalErrorSet = GlobalErrorSet.empty,
10global_error_set: GlobalErrorSet,
1111/// Cached number of active bits in a `tid`.
12tid_width: if (single_threaded) u0 else std.math.Log2Int(u32) = 0,
12tid_width: if (single_threaded) u0 else std.math.Log2Int(u32),
1313/// Cached shift amount to put a `tid` in the top bits of a 30-bit value.
14tid_shift_30: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
14tid_shift_30: if (single_threaded) u0 else std.math.Log2Int(u32),
1515/// Cached shift amount to put a `tid` in the top bits of a 31-bit value.
16tid_shift_31: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
16tid_shift_31: if (single_threaded) u0 else std.math.Log2Int(u32),
1717/// Cached shift amount to put a `tid` in the top bits of a 32-bit value.
18tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_threaded) 0 else 31,
18tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32),
1919
2020/// Dependencies on the source code hash associated with a ZIR instruction.
2121/// * For a `declaration`, this is the entire declaration body.
......@@ -23,36 +23,36 @@ tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32) = if (single_th
2323/// * For a `func`, this is the source of the full function signature.
2424/// These are also invalidated if tracking fails for this instruction.
2525/// Value is index into `dep_entries` of the first dependency on this hash.
26src_hash_deps: std.AutoArrayHashMapUnmanaged(TrackedInst.Index, DepEntry.Index) = .empty,
26src_hash_deps: std.AutoArrayHashMapUnmanaged(TrackedInst.Index, DepEntry.Index),
2727/// Dependencies on the value of a Nav.
2828/// Value is index into `dep_entries` of the first dependency on this Nav value.
29nav_val_deps: std.AutoArrayHashMapUnmanaged(Nav.Index, DepEntry.Index) = .empty,
29nav_val_deps: std.AutoArrayHashMapUnmanaged(Nav.Index, DepEntry.Index),
3030/// Dependencies on an interned value, either:
3131/// * a runtime function (invalidated when its IES changes)
3232/// * a container type requiring resolution (invalidated when the type must be recreated at a new index)
3333/// Value is index into `dep_entries` of the first dependency on this interned value.
34interned_deps: std.AutoArrayHashMapUnmanaged(Index, DepEntry.Index) = .empty,
34interned_deps: std.AutoArrayHashMapUnmanaged(Index, DepEntry.Index),
3535/// Dependencies on the full set of names in a ZIR namespace.
3636/// Key refers to a `struct_decl`, `union_decl`, etc.
3737/// Value is index into `dep_entries` of the first dependency on this namespace.
38namespace_deps: std.AutoArrayHashMapUnmanaged(TrackedInst.Index, DepEntry.Index) = .empty,
38namespace_deps: std.AutoArrayHashMapUnmanaged(TrackedInst.Index, DepEntry.Index),
3939/// Dependencies on the (non-)existence of some name in a namespace.
4040/// Value is index into `dep_entries` of the first dependency on this name.
41namespace_name_deps: std.AutoArrayHashMapUnmanaged(NamespaceNameKey, DepEntry.Index) = .empty,
41namespace_name_deps: std.AutoArrayHashMapUnmanaged(NamespaceNameKey, DepEntry.Index),
4242
4343/// Given a `Depender`, points to an entry in `dep_entries` whose `depender`
4444/// matches. The `next_dependee` field can be used to iterate all such entries
4545/// and remove them from the corresponding lists.
46first_dependency: std.AutoArrayHashMapUnmanaged(AnalUnit, DepEntry.Index) = .empty,
46first_dependency: std.AutoArrayHashMapUnmanaged(AnalUnit, DepEntry.Index),
4747
4848/// Stores dependency information. The hashmaps declared above are used to look
4949/// up entries in this list as required. This is not stored in `extra` so that
5050/// we can use `free_dep_entries` to track free indices, since dependencies are
5151/// removed frequently.
52dep_entries: std.ArrayListUnmanaged(DepEntry) = .empty,
52dep_entries: std.ArrayListUnmanaged(DepEntry),
5353/// Stores unused indices in `dep_entries` which can be reused without a full
5454/// garbage collection pass.
55free_dep_entries: std.ArrayListUnmanaged(DepEntry.Index) = .empty,
55free_dep_entries: std.ArrayListUnmanaged(DepEntry.Index),
5656
5757/// Whether a multi-threaded intern pool is useful.
5858/// Currently `false` until the intern pool is actually accessed
......@@ -62,6 +62,24 @@ const want_multi_threaded = true;
6262/// Whether a single-threaded intern pool impl is in use.
6363pub const single_threaded = builtin.single_threaded or !want_multi_threaded;
6464
65pub const empty: InternPool = .{
66 .locals = &.{},
67 .shards = &.{},
68 .global_error_set = .empty,
69 .tid_width = 0,
70 .tid_shift_30 = if (single_threaded) 0 else 31,
71 .tid_shift_31 = if (single_threaded) 0 else 31,
72 .tid_shift_32 = if (single_threaded) 0 else 31,
73 .src_hash_deps = .empty,
74 .nav_val_deps = .empty,
75 .interned_deps = .empty,
76 .namespace_deps = .empty,
77 .namespace_name_deps = .empty,
78 .first_dependency = .empty,
79 .dep_entries = .empty,
80 .free_dep_entries = .empty,
81};
82
6583/// A `TrackedInst.Index` provides a single, unchanging reference to a ZIR instruction across a whole
6684/// compilation. From this index, you can acquire a `TrackedInst`, which containss a reference to both
6785/// the file which the instruction lives in, and the instruction index itself, which is updated on
......@@ -9858,7 +9876,7 @@ fn extraData(extra: Local.Extra, comptime T: type, index: u32) T {
98589876test "basic usage" {
98599877 const gpa = std.testing.allocator;
98609878
9861 var ip: InternPool = .{};
9879 var ip: InternPool = .empty;
98629880 defer ip.deinit(gpa);
98639881
98649882 const i32_type = try ip.get(gpa, .main, .{ .int_type = .{
src/Zcu.zig+1-1
......@@ -116,7 +116,7 @@ embed_table: std.StringArrayHashMapUnmanaged(*EmbedFile) = .empty,
116116/// Stores all Type and Value objects.
117117/// The idea is that this will be periodically garbage-collected, but such logic
118118/// is not yet implemented.
119intern_pool: InternPool = .{},
119intern_pool: InternPool = .empty,
120120
121121analysis_in_progress: std.AutoArrayHashMapUnmanaged(AnalUnit, void) = .empty,
122122/// The ErrorMsg memory is owned by the `AnalUnit`, using Module's general purpose allocator.