authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-17 08:30:56+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-08-17 18:50:10-04:00
log90116d92b08ff882715ca94ecc79934bc0a73762
tree9802071e8f83134558d15f967b67f760112fc469
parent89f02d1c107a159dc6433863c7d9167872b4c0c8

Compilation: don't call `resolveReferences` unnecessarily

This function is slow and should only be called in compile error cases.

1 files changed, 26 insertions(+), 9 deletions(-)

src/Compilation.zig+26-9
...@@ -3065,8 +3065,8 @@ pub fn totalErrorCount(comp: *Compilation) Allocator.Error!u32 {...@@ -3065,8 +3065,8 @@ pub fn totalErrorCount(comp: *Compilation) Allocator.Error!u32 {
3065 if (comp.module) |zcu| {3065 if (comp.module) |zcu| {
3066 const ip = &zcu.intern_pool;3066 const ip = &zcu.intern_pool;
30673067
3068 var all_references = try zcu.resolveReferences();3068 var all_references: ?std.AutoHashMapUnmanaged(InternPool.AnalUnit, ?Zcu.ResolvedReference) = null;
3069 defer all_references.deinit(zcu.gpa);3069 defer if (all_references) |*a| a.deinit(zcu.gpa);
30703070
3071 total += zcu.failed_exports.count();3071 total += zcu.failed_exports.count();
3072 total += zcu.failed_embed_files.count();3072 total += zcu.failed_embed_files.count();
...@@ -3088,7 +3088,12 @@ pub fn totalErrorCount(comp: *Compilation) Allocator.Error!u32 {...@@ -3088,7 +3088,12 @@ pub fn totalErrorCount(comp: *Compilation) Allocator.Error!u32 {
3088 // the previous parse success, including compile errors, but we cannot3088 // the previous parse success, including compile errors, but we cannot
3089 // emit them until the file succeeds parsing.3089 // emit them until the file succeeds parsing.
3090 for (zcu.failed_analysis.keys()) |anal_unit| {3090 for (zcu.failed_analysis.keys()) |anal_unit| {
3091 if (comp.incremental and !all_references.contains(anal_unit)) continue;3091 if (comp.incremental) {
3092 if (all_references == null) {
3093 all_references = try zcu.resolveReferences();
3094 }
3095 if (!all_references.?.contains(anal_unit)) continue;
3096 }
3092 const file_index = switch (anal_unit.unwrap()) {3097 const file_index = switch (anal_unit.unwrap()) {
3093 .cau => |cau| zcu.namespacePtr(ip.getCau(cau).namespace).file_scope,3098 .cau => |cau| zcu.namespacePtr(ip.getCau(cau).namespace).file_scope,
3094 .func => |ip_index| (zcu.funcInfo(ip_index).zir_body_inst.resolveFull(ip) orelse continue).file,3099 .func => |ip_index| (zcu.funcInfo(ip_index).zir_body_inst.resolveFull(ip) orelse continue).file,
...@@ -3172,8 +3177,8 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {...@@ -3172,8 +3177,8 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
3172 });3177 });
3173 }3178 }
31743179
3175 var all_references = if (comp.module) |zcu| try zcu.resolveReferences() else undefined;3180 var all_references: ?std.AutoHashMapUnmanaged(InternPool.AnalUnit, ?Zcu.ResolvedReference) = null;
3176 defer if (comp.module != null) all_references.deinit(gpa);3181 defer if (all_references) |*a| a.deinit(gpa);
31773182
3178 if (comp.module) |zcu| {3183 if (comp.module) |zcu| {
3179 const ip = &zcu.intern_pool;3184 const ip = &zcu.intern_pool;
...@@ -3231,7 +3236,12 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {...@@ -3231,7 +3236,12 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
3231 if (err) |e| return e;3236 if (err) |e| return e;
3232 }3237 }
3233 for (zcu.failed_analysis.keys(), zcu.failed_analysis.values()) |anal_unit, error_msg| {3238 for (zcu.failed_analysis.keys(), zcu.failed_analysis.values()) |anal_unit, error_msg| {
3234 if (comp.incremental and !all_references.contains(anal_unit)) continue;3239 if (comp.incremental) {
3240 if (all_references == null) {
3241 all_references = try zcu.resolveReferences();
3242 }
3243 if (!all_references.?.contains(anal_unit)) continue;
3244 }
32353245
3236 const file_index = switch (anal_unit.unwrap()) {3246 const file_index = switch (anal_unit.unwrap()) {
3237 .cau => |cau| zcu.namespacePtr(ip.getCau(cau).namespace).file_scope,3247 .cau => |cau| zcu.namespacePtr(ip.getCau(cau).namespace).file_scope,
...@@ -3352,7 +3362,10 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {...@@ -3352,7 +3362,10 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
3352 if (comp.module) |zcu| {3362 if (comp.module) |zcu| {
3353 if (comp.incremental and bundle.root_list.items.len == 0) {3363 if (comp.incremental and bundle.root_list.items.len == 0) {
3354 const should_have_error = for (zcu.transitive_failed_analysis.keys()) |failed_unit| {3364 const should_have_error = for (zcu.transitive_failed_analysis.keys()) |failed_unit| {
3355 if (all_references.contains(failed_unit)) break true;3365 if (all_references == null) {
3366 all_references = try zcu.resolveReferences();
3367 }
3368 if (all_references.?.contains(failed_unit)) break true;
3356 } else false;3369 } else false;
3357 if (should_have_error) {3370 if (should_have_error) {
3358 @panic("referenced transitive analysis errors, but none actually emitted");3371 @panic("referenced transitive analysis errors, but none actually emitted");
...@@ -3414,7 +3427,7 @@ pub fn addModuleErrorMsg(...@@ -3414,7 +3427,7 @@ pub fn addModuleErrorMsg(
3414 mod: *Zcu,3427 mod: *Zcu,
3415 eb: *ErrorBundle.Wip,3428 eb: *ErrorBundle.Wip,
3416 module_err_msg: Zcu.ErrorMsg,3429 module_err_msg: Zcu.ErrorMsg,
3417 all_references: *const std.AutoHashMapUnmanaged(InternPool.AnalUnit, ?Zcu.ResolvedReference),3430 all_references: *?std.AutoHashMapUnmanaged(InternPool.AnalUnit, ?Zcu.ResolvedReference),
3418) !void {3431) !void {
3419 const gpa = eb.gpa;3432 const gpa = eb.gpa;
3420 const ip = &mod.intern_pool;3433 const ip = &mod.intern_pool;
...@@ -3438,13 +3451,17 @@ pub fn addModuleErrorMsg(...@@ -3438,13 +3451,17 @@ pub fn addModuleErrorMsg(
3438 defer ref_traces.deinit(gpa);3451 defer ref_traces.deinit(gpa);
34393452
3440 if (module_err_msg.reference_trace_root.unwrap()) |rt_root| {3453 if (module_err_msg.reference_trace_root.unwrap()) |rt_root| {
3454 if (all_references.* == null) {
3455 all_references.* = try mod.resolveReferences();
3456 }
3457
3441 var seen: std.AutoHashMapUnmanaged(InternPool.AnalUnit, void) = .{};3458 var seen: std.AutoHashMapUnmanaged(InternPool.AnalUnit, void) = .{};
3442 defer seen.deinit(gpa);3459 defer seen.deinit(gpa);
34433460
3444 const max_references = mod.comp.reference_trace orelse Sema.default_reference_trace_len;3461 const max_references = mod.comp.reference_trace orelse Sema.default_reference_trace_len;
34453462
3446 var referenced_by = rt_root;3463 var referenced_by = rt_root;
3447 while (all_references.get(referenced_by)) |maybe_ref| {3464 while (all_references.*.?.get(referenced_by)) |maybe_ref| {
3448 const ref = maybe_ref orelse break;3465 const ref = maybe_ref orelse break;
3449 const gop = try seen.getOrPut(gpa, ref.referencer);3466 const gop = try seen.getOrPut(gpa, ref.referencer);
3450 if (gop.found_existing) break;3467 if (gop.found_existing) break;