authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-12 11:02:18+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-08-17 18:50:10-04:00
log6faa4cc7e60c2ecd26759878a6f9e277d69a4968
treefb54ba56449d8e78535a9459a4de0ec0e8d01d7c
parentb65865b027f5531408654eae82cec05468b2c082

Zcu: construct full reference graph

This commit updates `Zcu.resolveReferences` to traverse the graph of `AnalUnit` references (starting from the 1-3 roots of analysis) in order to determine which `AnalUnit`s are referenced in an update. Errors for unreferenced entities are omitted from the error bundle. However, note that unreferenced `Nav`s are not removed from the binary.

3 files changed, 330 insertions(+), 50 deletions(-)

src/Compilation.zig+30-14
......@@ -2264,13 +2264,19 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void {
22642264 }
22652265 }
22662266
2267 zcu.analysis_roots.resize(0) catch unreachable;
2268
22672269 try comp.queueJob(.{ .analyze_mod = std_mod });
2270 zcu.analysis_roots.appendAssumeCapacity(std_mod);
2271
22682272 if (comp.config.is_test) {
22692273 try comp.queueJob(.{ .analyze_mod = zcu.main_mod });
2274 zcu.analysis_roots.appendAssumeCapacity(zcu.main_mod);
22702275 }
22712276
22722277 if (zcu.root_mod.deps.get("compiler_rt")) |compiler_rt_mod| {
22732278 try comp.queueJob(.{ .analyze_mod = compiler_rt_mod });
2279 zcu.analysis_roots.appendAssumeCapacity(compiler_rt_mod);
22742280 }
22752281 }
22762282
......@@ -3059,6 +3065,9 @@ pub fn totalErrorCount(comp: *Compilation) u32 {
30593065 if (comp.module) |zcu| {
30603066 const ip = &zcu.intern_pool;
30613067
3068 var all_references = try zcu.resolveReferences();
3069 defer all_references.deinit(zcu.gpa);
3070
30623071 total += zcu.failed_exports.count();
30633072 total += zcu.failed_embed_files.count();
30643073
......@@ -3079,6 +3088,7 @@ pub fn totalErrorCount(comp: *Compilation) u32 {
30793088 // the previous parse success, including compile errors, but we cannot
30803089 // emit them until the file succeeds parsing.
30813090 for (zcu.failed_analysis.keys()) |anal_unit| {
3091 if (!all_references.contains(anal_unit)) continue;
30823092 const file_index = switch (anal_unit.unwrap()) {
30833093 .cau => |cau| zcu.namespacePtr(ip.getCau(cau).namespace).file_scope,
30843094 .func => |ip_index| (zcu.funcInfo(ip_index).zir_body_inst.resolveFull(ip) orelse continue).file,
......@@ -3116,12 +3126,6 @@ pub fn totalErrorCount(comp: *Compilation) u32 {
31163126 }
31173127 }
31183128
3119 if (comp.module) |zcu| {
3120 if (total == 0 and zcu.transitive_failed_analysis.count() > 0) {
3121 @panic("Transitive analysis errors, but none actually emitted");
3122 }
3123 }
3124
31253129 return @intCast(total);
31263130}
31273131
......@@ -3167,12 +3171,13 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
31673171 .msg = try bundle.addString("memory allocation failure"),
31683172 });
31693173 }
3174
3175 var all_references = if (comp.module) |zcu| try zcu.resolveReferences() else undefined;
3176 defer if (comp.module != null) all_references.deinit(gpa);
3177
31703178 if (comp.module) |zcu| {
31713179 const ip = &zcu.intern_pool;
31723180
3173 var all_references = try zcu.resolveReferences();
3174 defer all_references.deinit(gpa);
3175
31763181 for (zcu.failed_files.keys(), zcu.failed_files.values()) |file, error_msg| {
31773182 if (error_msg) |msg| {
31783183 try addModuleErrorMsg(zcu, &bundle, msg.*, &all_references);
......@@ -3220,6 +3225,8 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
32203225 if (err) |e| return e;
32213226 }
32223227 for (zcu.failed_analysis.keys(), zcu.failed_analysis.values()) |anal_unit, error_msg| {
3228 if (!all_references.contains(anal_unit)) continue;
3229
32233230 const file_index = switch (anal_unit.unwrap()) {
32243231 .cau => |cau| zcu.namespacePtr(ip.getCau(cau).namespace).file_scope,
32253232 .func => |ip_index| (zcu.funcInfo(ip_index).zir_body_inst.resolveFull(ip) orelse continue).file,
......@@ -3313,9 +3320,6 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
33133320
33143321 if (comp.module) |zcu| {
33153322 if (bundle.root_list.items.len == 0 and zcu.compile_log_sources.count() != 0) {
3316 var all_references = try zcu.resolveReferences();
3317 defer all_references.deinit(gpa);
3318
33193323 const values = zcu.compile_log_sources.values();
33203324 // First one will be the error; subsequent ones will be notes.
33213325 const src_loc = values[0].src();
......@@ -3339,6 +3343,17 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
33393343
33403344 assert(comp.totalErrorCount() == bundle.root_list.items.len);
33413345
3346 if (comp.module) |zcu| {
3347 if (bundle.root_list.items.len == 0) {
3348 const should_have_error = for (zcu.transitive_failed_analysis.keys()) |failed_unit| {
3349 if (all_references.contains(failed_unit)) break true;
3350 } else false;
3351 if (should_have_error) {
3352 @panic("referenced transitive analysis errors, but none actually emitted");
3353 }
3354 }
3355 }
3356
33423357 const compile_log_text = if (comp.module) |m| m.compile_log_text.items else "";
33433358 return bundle.toOwnedBundle(compile_log_text);
33443359}
......@@ -3393,7 +3408,7 @@ pub fn addModuleErrorMsg(
33933408 mod: *Zcu,
33943409 eb: *ErrorBundle.Wip,
33953410 module_err_msg: Zcu.ErrorMsg,
3396 all_references: *const std.AutoHashMapUnmanaged(InternPool.AnalUnit, Zcu.ResolvedReference),
3411 all_references: *const std.AutoHashMapUnmanaged(InternPool.AnalUnit, ?Zcu.ResolvedReference),
33973412) !void {
33983413 const gpa = eb.gpa;
33993414 const ip = &mod.intern_pool;
......@@ -3423,7 +3438,8 @@ pub fn addModuleErrorMsg(
34233438 const max_references = mod.comp.reference_trace orelse Sema.default_reference_trace_len;
34243439
34253440 var referenced_by = rt_root;
3426 while (all_references.get(referenced_by)) |ref| {
3441 while (all_references.get(referenced_by)) |maybe_ref| {
3442 const ref = maybe_ref orelse break;
34273443 const gop = try seen.getOrPut(gpa, ref.referencer);
34283444 if (gop.found_existing) break;
34293445 if (ref_traces.items.len < max_references) {
src/Sema.zig+63-13
......@@ -110,6 +110,7 @@ exports: std.ArrayListUnmanaged(Zcu.Export) = .{},
110110/// of data stored in `Zcu.all_references`. It exists to avoid adding references to
111111/// a given `AnalUnit` multiple times.
112112references: std.AutoArrayHashMapUnmanaged(AnalUnit, void) = .{},
113type_references: std.AutoArrayHashMapUnmanaged(InternPool.Index, void) = .{},
113114
114115const MaybeComptimeAlloc = struct {
115116 /// The runtime index of the `alloc` instruction.
......@@ -877,6 +878,7 @@ pub fn deinit(sema: *Sema) void {
877878 sema.comptime_allocs.deinit(gpa);
878879 sema.exports.deinit(gpa);
879880 sema.references.deinit(gpa);
881 sema.type_references.deinit(gpa);
880882 sema.* = undefined;
881883}
882884
......@@ -2809,7 +2811,11 @@ fn zirStructDecl(
28092811 };
28102812 const wip_ty = sema.wrapWipTy(switch (try ip.getStructType(gpa, pt.tid, struct_init)) {
28112813 .existing => |ty| wip: {
2812 if (!try sema.maybeRemoveOutdatedType(ty)) return Air.internedToRef(ty);
2814 if (!try sema.maybeRemoveOutdatedType(ty)) {
2815 try sema.declareDependency(.{ .interned = ty });
2816 try sema.addTypeReferenceEntry(src, ty);
2817 return Air.internedToRef(ty);
2818 }
28132819 break :wip (try ip.getStructType(gpa, pt.tid, struct_init)).wip;
28142820 },
28152821 .wip => |wip| wip,
......@@ -2850,8 +2856,8 @@ fn zirStructDecl(
28502856 if (block.ownerModule().strip) break :codegen_type;
28512857 try mod.comp.queueJob(.{ .codegen_type = wip_ty.index });
28522858 }
2853 try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .cau = new_cau_index }));
28542859 try sema.declareDependency(.{ .interned = wip_ty.index });
2860 try sema.addTypeReferenceEntry(src, wip_ty.index);
28552861 return Air.internedToRef(wip_ty.finish(ip, new_cau_index.toOptional(), new_namespace_index));
28562862}
28572863
......@@ -3031,7 +3037,11 @@ fn zirEnumDecl(
30313037 };
30323038 const wip_ty = sema.wrapWipTy(switch (try ip.getEnumType(gpa, pt.tid, enum_init)) {
30333039 .existing => |ty| wip: {
3034 if (!try sema.maybeRemoveOutdatedType(ty)) return Air.internedToRef(ty);
3040 if (!try sema.maybeRemoveOutdatedType(ty)) {
3041 try sema.declareDependency(.{ .interned = ty });
3042 try sema.addTypeReferenceEntry(src, ty);
3043 return Air.internedToRef(ty);
3044 }
30353045 break :wip (try ip.getEnumType(gpa, pt.tid, enum_init)).wip;
30363046 },
30373047 .wip => |wip| wip,
......@@ -3071,8 +3081,8 @@ fn zirEnumDecl(
30713081
30723082 try pt.scanNamespace(new_namespace_index, decls);
30733083
3074 try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .cau = new_cau_index }));
30753084 try sema.declareDependency(.{ .interned = wip_ty.index });
3085 try sema.addTypeReferenceEntry(src, wip_ty.index);
30763086
30773087 // We've finished the initial construction of this type, and are about to perform analysis.
30783088 // Set the Cau and namespace appropriately, and don't destroy anything on failure.
......@@ -3297,7 +3307,11 @@ fn zirUnionDecl(
32973307 };
32983308 const wip_ty = sema.wrapWipTy(switch (try ip.getUnionType(gpa, pt.tid, union_init)) {
32993309 .existing => |ty| wip: {
3300 if (!try sema.maybeRemoveOutdatedType(ty)) return Air.internedToRef(ty);
3310 if (!try sema.maybeRemoveOutdatedType(ty)) {
3311 try sema.declareDependency(.{ .interned = ty });
3312 try sema.addTypeReferenceEntry(src, ty);
3313 return Air.internedToRef(ty);
3314 }
33013315 break :wip (try ip.getUnionType(gpa, pt.tid, union_init)).wip;
33023316 },
33033317 .wip => |wip| wip,
......@@ -3338,8 +3352,8 @@ fn zirUnionDecl(
33383352 if (block.ownerModule().strip) break :codegen_type;
33393353 try mod.comp.queueJob(.{ .codegen_type = wip_ty.index });
33403354 }
3341 try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .cau = new_cau_index }));
33423355 try sema.declareDependency(.{ .interned = wip_ty.index });
3356 try sema.addTypeReferenceEntry(src, wip_ty.index);
33433357 return Air.internedToRef(wip_ty.finish(ip, new_cau_index.toOptional(), new_namespace_index));
33443358}
33453359
......@@ -3388,7 +3402,10 @@ fn zirOpaqueDecl(
33883402 // No `wrapWipTy` needed as no std.builtin types are opaque.
33893403 const wip_ty = switch (try ip.getOpaqueType(gpa, pt.tid, opaque_init)) {
33903404 // No `maybeRemoveOutdatedType` as opaque types are never outdated.
3391 .existing => |ty| return Air.internedToRef(ty),
3405 .existing => |ty| {
3406 try sema.addTypeReferenceEntry(src, ty);
3407 return Air.internedToRef(ty);
3408 },
33923409 .wip => |wip| wip,
33933410 };
33943411 errdefer wip_ty.cancel(ip, pt.tid);
......@@ -3416,6 +3433,7 @@ fn zirOpaqueDecl(
34163433 if (block.ownerModule().strip) break :codegen_type;
34173434 try mod.comp.queueJob(.{ .codegen_type = wip_ty.index });
34183435 }
3436 try sema.addTypeReferenceEntry(src, wip_ty.index);
34193437 return Air.internedToRef(wip_ty.finish(ip, .none, new_namespace_index));
34203438}
34213439
......@@ -21820,7 +21838,10 @@ fn zirReify(
2182021838 .zir_index = try block.trackZir(inst),
2182121839 } },
2182221840 })) {
21823 .existing => |ty| return Air.internedToRef(ty),
21841 .existing => |ty| {
21842 try sema.addTypeReferenceEntry(src, ty);
21843 return Air.internedToRef(ty);
21844 },
2182421845 .wip => |wip| wip,
2182521846 };
2182621847 errdefer wip_ty.cancel(ip, pt.tid);
......@@ -21839,6 +21860,7 @@ fn zirReify(
2183921860 .file_scope = block.getFileScopeIndex(mod),
2184021861 });
2184121862
21863 try sema.addTypeReferenceEntry(src, wip_ty.index);
2184221864 return Air.internedToRef(wip_ty.finish(ip, .none, new_namespace_index));
2184321865 },
2184421866 .Union => {
......@@ -22020,7 +22042,11 @@ fn reifyEnum(
2202022042 } },
2202122043 })) {
2202222044 .wip => |wip| wip,
22023 .existing => |ty| return Air.internedToRef(ty),
22045 .existing => |ty| {
22046 try sema.declareDependency(.{ .interned = ty });
22047 try sema.addTypeReferenceEntry(src, ty);
22048 return Air.internedToRef(ty);
22049 },
2202422050 };
2202522051 errdefer wip_ty.cancel(ip, pt.tid);
2202622052
......@@ -22044,6 +22070,8 @@ fn reifyEnum(
2204422070
2204522071 const new_cau_index = try ip.createTypeCau(gpa, pt.tid, tracked_inst, new_namespace_index, wip_ty.index);
2204622072
22073 try sema.declareDependency(.{ .interned = wip_ty.index });
22074 try sema.addTypeReferenceEntry(src, wip_ty.index);
2204722075 wip_ty.prepare(ip, new_cau_index, new_namespace_index);
2204822076 wip_ty.setTagTy(ip, tag_ty.toIntern());
2204922077
......@@ -22182,7 +22210,11 @@ fn reifyUnion(
2218222210 } },
2218322211 })) {
2218422212 .wip => |wip| wip,
22185 .existing => |ty| return Air.internedToRef(ty),
22213 .existing => |ty| {
22214 try sema.declareDependency(.{ .interned = ty });
22215 try sema.addTypeReferenceEntry(src, ty);
22216 return Air.internedToRef(ty);
22217 },
2218622218 };
2218722219 errdefer wip_ty.cancel(ip, pt.tid);
2218822220
......@@ -22347,7 +22379,8 @@ fn reifyUnion(
2234722379 if (block.ownerModule().strip) break :codegen_type;
2234822380 try mod.comp.queueJob(.{ .codegen_type = wip_ty.index });
2234922381 }
22350 try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .cau = new_cau_index }));
22382 try sema.declareDependency(.{ .interned = wip_ty.index });
22383 try sema.addTypeReferenceEntry(src, wip_ty.index);
2235122384 return Air.internedToRef(wip_ty.finish(ip, new_cau_index.toOptional(), new_namespace_index));
2235222385}
2235322386
......@@ -22447,7 +22480,11 @@ fn reifyStruct(
2244722480 } },
2244822481 })) {
2244922482 .wip => |wip| wip,
22450 .existing => |ty| return Air.internedToRef(ty),
22483 .existing => |ty| {
22484 try sema.declareDependency(.{ .interned = ty });
22485 try sema.addTypeReferenceEntry(src, ty);
22486 return Air.internedToRef(ty);
22487 },
2245122488 };
2245222489 errdefer wip_ty.cancel(ip, pt.tid);
2245322490
......@@ -22625,7 +22662,8 @@ fn reifyStruct(
2262522662 if (block.ownerModule().strip) break :codegen_type;
2262622663 try mod.comp.queueJob(.{ .codegen_type = wip_ty.index });
2262722664 }
22628 try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .cau = new_cau_index }));
22665 try sema.declareDependency(.{ .interned = wip_ty.index });
22666 try sema.addTypeReferenceEntry(src, wip_ty.index);
2262922667 return Air.internedToRef(wip_ty.finish(ip, new_cau_index.toOptional(), new_namespace_index));
2263022668}
2263122669
......@@ -32231,6 +32269,18 @@ fn addReferenceEntry(
3223132269 try zcu.addUnitReference(sema.owner, referenced_unit, src);
3223232270}
3223332271
32272fn addTypeReferenceEntry(
32273 sema: *Sema,
32274 src: LazySrcLoc,
32275 referenced_type: InternPool.Index,
32276) !void {
32277 const zcu = sema.pt.zcu;
32278 if (zcu.comp.reference_trace == 0) return;
32279 const gop = try sema.type_references.getOrPut(sema.gpa, referenced_type);
32280 if (gop.found_existing) return;
32281 try zcu.addTypeReference(sema.owner, referenced_type, src);
32282}
32283
3223432284pub fn ensureNavResolved(sema: *Sema, src: LazySrcLoc, nav_index: InternPool.Nav.Index) CompileError!void {
3223532285 const pt = sema.pt;
3223632286 const zcu = pt.zcu;
src/Zcu.zig+237-23
......@@ -168,6 +168,10 @@ outdated_ready: std.AutoArrayHashMapUnmanaged(AnalUnit, void) = .{},
168168/// it as outdated.
169169retryable_failures: std.ArrayListUnmanaged(AnalUnit) = .{},
170170
171/// These are the modules which we initially queue for analysis in `Compilation.update`.
172/// `resolveReferences` will use these as the root of its reachability traversal.
173analysis_roots: std.BoundedArray(*Package.Module, 3) = .{},
174
171175stage1_flags: packed struct {
172176 have_winmain: bool = false,
173177 have_wwinmain: bool = false,
......@@ -186,7 +190,7 @@ global_assembly: std.AutoArrayHashMapUnmanaged(InternPool.Cau.Index, []u8) = .{}
186190
187191/// Key is the `AnalUnit` *performing* the reference. This representation allows
188192/// incremental updates to quickly delete references caused by a specific `AnalUnit`.
189/// Value is index into `all_reference` of the first reference triggered by the unit.
193/// Value is index into `all_references` of the first reference triggered by the unit.
190194/// The `next` field on the `Reference` forms a linked list of all references
191195/// triggered by the key `AnalUnit`.
192196reference_table: std.AutoArrayHashMapUnmanaged(AnalUnit, u32) = .{},
......@@ -194,6 +198,16 @@ all_references: std.ArrayListUnmanaged(Reference) = .{},
194198/// Freelist of indices in `all_references`.
195199free_references: std.ArrayListUnmanaged(u32) = .{},
196200
201/// Key is the `AnalUnit` *performing* the reference. This representation allows
202/// incremental updates to quickly delete references caused by a specific `AnalUnit`.
203/// Value is index into `all_type_reference` of the first reference triggered by the unit.
204/// The `next` field on the `TypeReference` forms a linked list of all type references
205/// triggered by the key `AnalUnit`.
206type_reference_table: std.AutoArrayHashMapUnmanaged(AnalUnit, u32) = .{},
207all_type_references: std.ArrayListUnmanaged(TypeReference) = .{},
208/// Freelist of indices in `all_type_references`.
209free_type_references: std.ArrayListUnmanaged(u32) = .{},
210
197211panic_messages: [PanicId.len]InternPool.Nav.Index.Optional = .{.none} ** PanicId.len,
198212/// The panic function body.
199213panic_func_index: InternPool.Index = .none,
......@@ -302,6 +316,16 @@ pub const Reference = struct {
302316 src: LazySrcLoc,
303317};
304318
319pub const TypeReference = struct {
320 /// The container type which was referenced.
321 referenced: InternPool.Index,
322 /// Index into `all_type_references` of the next `TypeReference` triggered by the same `AnalUnit`.
323 /// `std.math.maxInt(u32)` is the sentinel.
324 next: u32,
325 /// The source location of the reference.
326 src: LazySrcLoc,
327};
328
305329/// The container that structs, enums, unions, and opaques have.
306330pub const Namespace = struct {
307331 parent: OptionalIndex,
......@@ -2155,6 +2179,10 @@ pub fn deinit(zcu: *Zcu) void {
21552179 zcu.all_references.deinit(gpa);
21562180 zcu.free_references.deinit(gpa);
21572181
2182 zcu.type_reference_table.deinit(gpa);
2183 zcu.all_type_references.deinit(gpa);
2184 zcu.free_type_references.deinit(gpa);
2185
21582186 zcu.intern_pool.deinit(gpa);
21592187}
21602188
......@@ -2660,16 +2688,32 @@ pub fn deleteUnitExports(zcu: *Zcu, anal_unit: AnalUnit) void {
26602688pub fn deleteUnitReferences(zcu: *Zcu, anal_unit: AnalUnit) void {
26612689 const gpa = zcu.gpa;
26622690
2663 const kv = zcu.reference_table.fetchSwapRemove(anal_unit) orelse return;
2664 var idx = kv.value;
2691 unit_refs: {
2692 const kv = zcu.reference_table.fetchSwapRemove(anal_unit) orelse return;
2693 var idx = kv.value;
26652694
2666 while (idx != std.math.maxInt(u32)) {
2667 zcu.free_references.append(gpa, idx) catch {
2668 // This space will be reused eventually, so we need not propagate this error.
2669 // Just leak it for now, and let GC reclaim it later on.
2670 return;
2671 };
2672 idx = zcu.all_references.items[idx].next;
2695 while (idx != std.math.maxInt(u32)) {
2696 zcu.free_references.append(gpa, idx) catch {
2697 // This space will be reused eventually, so we need not propagate this error.
2698 // Just leak it for now, and let GC reclaim it later on.
2699 break :unit_refs;
2700 };
2701 idx = zcu.all_references.items[idx].next;
2702 }
2703 }
2704
2705 type_refs: {
2706 const kv = zcu.type_reference_table.fetchSwapRemove(anal_unit) orelse return;
2707 var idx = kv.value;
2708
2709 while (idx != std.math.maxInt(u32)) {
2710 zcu.free_type_references.append(gpa, idx) catch {
2711 // This space will be reused eventually, so we need not propagate this error.
2712 // Just leak it for now, and let GC reclaim it later on.
2713 break :type_refs;
2714 };
2715 idx = zcu.all_type_references.items[idx].next;
2716 }
26732717 }
26742718}
26752719
......@@ -2696,6 +2740,29 @@ pub fn addUnitReference(zcu: *Zcu, src_unit: AnalUnit, referenced_unit: AnalUnit
26962740 gop.value_ptr.* = @intCast(ref_idx);
26972741}
26982742
2743pub fn addTypeReference(zcu: *Zcu, src_unit: AnalUnit, referenced_type: InternPool.Index, ref_src: LazySrcLoc) Allocator.Error!void {
2744 const gpa = zcu.gpa;
2745
2746 try zcu.type_reference_table.ensureUnusedCapacity(gpa, 1);
2747
2748 const ref_idx = zcu.free_type_references.popOrNull() orelse idx: {
2749 _ = try zcu.all_type_references.addOne(gpa);
2750 break :idx zcu.all_type_references.items.len - 1;
2751 };
2752
2753 errdefer comptime unreachable;
2754
2755 const gop = zcu.type_reference_table.getOrPutAssumeCapacity(src_unit);
2756
2757 zcu.all_type_references.items[ref_idx] = .{
2758 .referenced = referenced_type,
2759 .next = if (gop.found_existing) gop.value_ptr.* else std.math.maxInt(u32),
2760 .src = ref_src,
2761 };
2762
2763 gop.value_ptr.* = @intCast(ref_idx);
2764}
2765
26992766pub fn errorSetBits(mod: *Zcu) u16 {
27002767 if (mod.error_limit == 0) return 0;
27012768 return @as(u16, std.math.log2_int(ErrorInt, mod.error_limit)) + 1;
......@@ -2990,28 +3057,175 @@ pub const ResolvedReference = struct {
29903057};
29913058
29923059/// Returns a mapping from an `AnalUnit` to where it is referenced.
2993/// TODO: in future, this must be adapted to traverse from roots of analysis. That way, we can
2994/// use the returned map to determine which units have become unreferenced in an incremental update.
2995pub fn resolveReferences(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ResolvedReference) {
3060/// If the value is `null`, the `AnalUnit` is a root of analysis.
3061/// If an `AnalUnit` is not in the returned map, it is unreferenced.
3062pub fn resolveReferences(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?ResolvedReference) {
29963063 const gpa = zcu.gpa;
3064 const comp = zcu.comp;
3065 const ip = &zcu.intern_pool;
29973066
2998 var result: std.AutoHashMapUnmanaged(AnalUnit, ResolvedReference) = .{};
3067 var result: std.AutoHashMapUnmanaged(AnalUnit, ?ResolvedReference) = .{};
29993068 errdefer result.deinit(gpa);
30003069
3070 var checked_types: std.AutoArrayHashMapUnmanaged(InternPool.Index, void) = .{};
3071 var type_queue: std.AutoArrayHashMapUnmanaged(InternPool.Index, ?ResolvedReference) = .{};
3072 var unit_queue: std.AutoArrayHashMapUnmanaged(AnalUnit, ?ResolvedReference) = .{};
3073 defer {
3074 checked_types.deinit(gpa);
3075 type_queue.deinit(gpa);
3076 unit_queue.deinit(gpa);
3077 }
3078
30013079 // This is not a sufficient size, but a lower bound.
30023080 try result.ensureTotalCapacity(gpa, @intCast(zcu.reference_table.count()));
30033081
3004 for (zcu.reference_table.keys(), zcu.reference_table.values()) |referencer, first_ref_idx| {
3005 assert(first_ref_idx != std.math.maxInt(u32));
3006 var ref_idx = first_ref_idx;
3007 while (ref_idx != std.math.maxInt(u32)) {
3008 const ref = zcu.all_references.items[ref_idx];
3009 const gop = try result.getOrPut(gpa, ref.referenced);
3010 if (!gop.found_existing) {
3011 gop.value_ptr.* = .{ .referencer = referencer, .src = ref.src };
3082 try type_queue.ensureTotalCapacity(gpa, zcu.analysis_roots.len);
3083 for (zcu.analysis_roots.slice()) |mod| {
3084 // Logic ripped from `Zcu.PerThread.importPkg`.
3085 // TODO: this is silly, `Module` should just store a reference to its root `File`.
3086 const resolved_path = try std.fs.path.resolve(gpa, &.{
3087 mod.root.root_dir.path orelse ".",
3088 mod.root.sub_path,
3089 mod.root_src_path,
3090 });
3091 defer gpa.free(resolved_path);
3092 const file = zcu.import_table.get(resolved_path).?;
3093 if (zcu.fileByIndex(file).status != .success_zir) continue;
3094 const root_ty = zcu.fileRootType(file);
3095 if (root_ty == .none) continue;
3096 type_queue.putAssumeCapacityNoClobber(root_ty, null);
3097 }
3098
3099 while (true) {
3100 if (type_queue.popOrNull()) |kv| {
3101 const ty = kv.key;
3102 const referencer = kv.value;
3103 try checked_types.putNoClobber(gpa, ty, {});
3104
3105 // If this type has a `Cau` for resolution, it's automatically referenced.
3106 const resolution_cau: InternPool.Cau.Index.Optional = switch (ip.indexToKey(ty)) {
3107 .struct_type => ip.loadStructType(ty).cau,
3108 .union_type => ip.loadUnionType(ty).cau.toOptional(),
3109 .enum_type => ip.loadEnumType(ty).cau,
3110 .opaque_type => .none,
3111 else => unreachable,
3112 };
3113 if (resolution_cau.unwrap()) |cau| {
3114 // this should only be referenced by the type
3115 const unit = AnalUnit.wrap(.{ .cau = cau });
3116 assert(!result.contains(unit));
3117 try unit_queue.putNoClobber(gpa, unit, referencer);
3118 }
3119
3120 // If this is a union with a generated tag, its tag type is automatically referenced.
3121 // We don't add this reference for non-generated tags, as those will already be referenced via the union's `Cau`, with a better source location.
3122 if (zcu.typeToUnion(Type.fromInterned(ty))) |union_obj| {
3123 const tag_ty = union_obj.enum_tag_ty;
3124 if (tag_ty != .none) {
3125 if (ip.indexToKey(tag_ty).enum_type == .generated_tag) {
3126 if (!checked_types.contains(tag_ty)) {
3127 try type_queue.put(gpa, tag_ty, referencer);
3128 }
3129 }
3130 }
3131 }
3132
3133 // Queue any decls within this type which would be automatically analyzed.
3134 // Keep in sync with analysis queueing logic in `Zcu.PerThread.ScanDeclIter.scanDecl`.
3135 const ns = Type.fromInterned(ty).getNamespace(zcu).unwrap() orelse continue;
3136 for (zcu.namespacePtr(ns).other_decls.items) |cau| {
3137 // These are `comptime` and `test` declarations.
3138 // `comptime` decls are always analyzed; `test` declarations are analyzed depending on the test filter.
3139 const inst_info = ip.getCau(cau).zir_index.resolveFull(ip) orelse continue;
3140 const file = zcu.fileByIndex(inst_info.file);
3141 const zir = file.zir;
3142 const declaration = zir.getDeclaration(inst_info.inst)[0];
3143 const want_analysis = switch (declaration.name) {
3144 .@"usingnamespace" => unreachable,
3145 .@"comptime" => true,
3146 else => a: {
3147 if (!comp.config.is_test) break :a false;
3148 if (file.mod != zcu.main_mod) break :a false;
3149 if (declaration.name.isNamedTest(zir) or declaration.name == .decltest) {
3150 const nav = ip.getCau(cau).owner.unwrap().nav;
3151 const fqn_slice = ip.getNav(nav).fqn.toSlice(ip);
3152 for (comp.test_filters) |test_filter| {
3153 if (std.mem.indexOf(u8, fqn_slice, test_filter) != null) break;
3154 } else break :a false;
3155 }
3156 break :a true;
3157 },
3158 };
3159 if (want_analysis) {
3160 const unit = AnalUnit.wrap(.{ .cau = cau });
3161 if (!result.contains(unit)) try unit_queue.put(gpa, unit, referencer);
3162 }
3163 }
3164 for (zcu.namespacePtr(ns).pub_decls.keys()) |nav| {
3165 // These are named declarations. They are analyzed only if marked `export`.
3166 const cau = ip.getNav(nav).analysis_owner.unwrap().?;
3167 const inst_info = ip.getCau(cau).zir_index.resolveFull(ip) orelse continue;
3168 const declaration = zcu.fileByIndex(inst_info.file).zir.getDeclaration(inst_info.inst)[0];
3169 if (declaration.flags.is_export) {
3170 const unit = AnalUnit.wrap(.{ .cau = cau });
3171 if (!result.contains(unit)) try unit_queue.put(gpa, unit, referencer);
3172 }
3173 }
3174 for (zcu.namespacePtr(ns).priv_decls.keys()) |nav| {
3175 // These are named declarations. They are analyzed only if marked `export`.
3176 const cau = ip.getNav(nav).analysis_owner.unwrap().?;
3177 const inst_info = ip.getCau(cau).zir_index.resolveFull(ip) orelse continue;
3178 const declaration = zcu.fileByIndex(inst_info.file).zir.getDeclaration(inst_info.inst)[0];
3179 if (declaration.flags.is_export) {
3180 const unit = AnalUnit.wrap(.{ .cau = cau });
3181 if (!result.contains(unit)) try unit_queue.put(gpa, unit, referencer);
3182 }
3183 }
3184 // Incremental compilation does not support `usingnamespace`.
3185 // These are only included to keep good reference traces in non-incremental updates.
3186 for (zcu.namespacePtr(ns).pub_usingnamespace.items) |nav| {
3187 const cau = ip.getNav(nav).analysis_owner.unwrap().?;
3188 const unit = AnalUnit.wrap(.{ .cau = cau });
3189 if (!result.contains(unit)) try unit_queue.put(gpa, unit, referencer);
30123190 }
3013 ref_idx = ref.next;
3191 for (zcu.namespacePtr(ns).priv_usingnamespace.items) |nav| {
3192 const cau = ip.getNav(nav).analysis_owner.unwrap().?;
3193 const unit = AnalUnit.wrap(.{ .cau = cau });
3194 if (!result.contains(unit)) try unit_queue.put(gpa, unit, referencer);
3195 }
3196 continue;
3197 }
3198 if (unit_queue.popOrNull()) |kv| {
3199 const unit = kv.key;
3200 try result.putNoClobber(gpa, unit, kv.value);
3201
3202 if (zcu.reference_table.get(unit)) |first_ref_idx| {
3203 assert(first_ref_idx != std.math.maxInt(u32));
3204 var ref_idx = first_ref_idx;
3205 while (ref_idx != std.math.maxInt(u32)) {
3206 const ref = zcu.all_references.items[ref_idx];
3207 if (!result.contains(ref.referenced)) try unit_queue.put(gpa, ref.referenced, .{
3208 .referencer = unit,
3209 .src = ref.src,
3210 });
3211 ref_idx = ref.next;
3212 }
3213 }
3214 if (zcu.type_reference_table.get(unit)) |first_ref_idx| {
3215 assert(first_ref_idx != std.math.maxInt(u32));
3216 var ref_idx = first_ref_idx;
3217 while (ref_idx != std.math.maxInt(u32)) {
3218 const ref = zcu.all_type_references.items[ref_idx];
3219 if (!checked_types.contains(ref.referenced)) try type_queue.put(gpa, ref.referenced, .{
3220 .referencer = unit,
3221 .src = ref.src,
3222 });
3223 ref_idx = ref.next;
3224 }
3225 }
3226 continue;
30143227 }
3228 break;
30153229 }
30163230
30173231 return result;