Skip to content

Commit 40aafcd

Browse files
committed
compiler: remove Cau
The `Cau` abstraction originated from noting that one of the two primary roles of the legacy `Decl` type was to be the subject of comptime semantic analysis. However, the data stored in `Cau` has always had some level of redundancy. While preparing for #131, I went to remove that redundany, and realised that `Cau` now had exactly one field: `owner`. This led me to conclude that `Cau` is, in fact, an unnecessary level of abstraction over what are in reality *fundamentally different* kinds of analysis unit (`AnalUnit`). Types, `Nav` vals, and `comptime` declarations are all analyzed in different ways, and trying to treat them as the same thing is counterproductive! So, these 3 cases are now different alternatives in `AnalUnit`. To avoid stealing bits from `InternPool`-based IDs, which are already a little starved for bits due to the sharding datastructures, `AnalUnit` is expanded to 64 bits (30 of which are currently unused). This doesn't impact memory usage too much by default, because we don't store `AnalUnit`s all too often; however, we do store them a lot under `-fincremental`, so a non-trivial bump to peak RSS can be observed there. This will be improved in the future when I made `InternPool.DepEntry` less memory-inefficient. `Zcu.PerThread.ensureCauAnalyzed` is split into 3 functions, for each of the 3 new types of `AnalUnit`. The new logic is much easier to understand, because it avoids conflating the logic of these fundamentally different cases.
1 parent 18362eb commit 40aafcd

File tree

7 files changed

+1321
-1524
lines changed

7 files changed

+1321
-1524
lines changed

src/Compilation.zig

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,15 @@ const Job = union(enum) {
348348
/// Corresponds to the task in `link.Task`.
349349
/// Only needed for backends that haven't yet been updated to not race against Sema.
350350
codegen_type: InternPool.Index,
351-
/// The `Cau` must be semantically analyzed (and possibly export itself).
351+
/// The `AnalUnit`, which is *not* a `func`, must be semantically analyzed.
352+
/// This may be its first time being analyzed, or it may be outdated.
353+
/// If the unit is a function, a `codegen_func` job will then be queued.
354+
analyze_comptime_unit: InternPool.AnalUnit,
355+
/// This function must be semantically analyzed.
352356
/// This may be its first time being analyzed, or it may be outdated.
353-
analyze_cau: InternPool.Cau.Index,
354-
/// Analyze the body of a runtime function.
355357
/// After analysis, a `codegen_func` job will be queued.
356358
/// These must be separate jobs to ensure any needed type resolution occurs *before* codegen.
359+
/// This job is separate from `analyze_comptime_unit` because it has a different priority.
357360
analyze_func: InternPool.Index,
358361
/// The main source file for the module needs to be analyzed.
359362
analyze_mod: *Package.Module,
@@ -3141,8 +3144,10 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
31413144
}
31423145

31433146
const file_index = switch (anal_unit.unwrap()) {
3144-
.cau => |cau| zcu.namespacePtr(ip.getCau(cau).namespace).file_scope,
3145-
.func => |ip_index| (zcu.funcInfo(ip_index).zir_body_inst.resolveFull(ip) orelse continue).file,
3147+
.@"comptime" => |cu| ip.getComptimeUnit(cu).zir_index.resolveFile(ip),
3148+
.nav_val => |nav| ip.getNav(nav).analysis.?.zir_index.resolveFile(ip),
3149+
.type => |ty| Type.fromInterned(ty).typeDeclInst(zcu).?.resolveFile(ip),
3150+
.func => |ip_index| zcu.funcInfo(ip_index).zir_body_inst.resolveFile(ip),
31463151
};
31473152

31483153
// Skip errors for AnalUnits within files that had a parse failure.
@@ -3374,11 +3379,9 @@ pub fn addModuleErrorMsg(
33743379
const rt_file_path = try src.file_scope.fullPath(gpa);
33753380
defer gpa.free(rt_file_path);
33763381
const name = switch (ref.referencer.unwrap()) {
3377-
.cau => |cau| switch (ip.getCau(cau).owner.unwrap()) {
3378-
.nav => |nav| ip.getNav(nav).name.toSlice(ip),
3379-
.type => |ty| Type.fromInterned(ty).containerTypeName(ip).toSlice(ip),
3380-
.none => "comptime",
3381-
},
3382+
.@"comptime" => "comptime",
3383+
.nav_val => |nav| ip.getNav(nav).name.toSlice(ip),
3384+
.type => |ty| Type.fromInterned(ty).containerTypeName(ip).toSlice(ip),
33823385
.func => |f| ip.getNav(zcu.funcInfo(f).owner_nav).name.toSlice(ip),
33833386
};
33843387
try ref_traces.append(gpa, .{
@@ -3641,10 +3644,13 @@ fn performAllTheWorkInner(
36413644
// If there's no work queued, check if there's anything outdated
36423645
// which we need to work on, and queue it if so.
36433646
if (try zcu.findOutdatedToAnalyze()) |outdated| {
3644-
switch (outdated.unwrap()) {
3645-
.cau => |cau| try comp.queueJob(.{ .analyze_cau = cau }),
3646-
.func => |func| try comp.queueJob(.{ .analyze_func = func }),
3647-
}
3647+
try comp.queueJob(switch (outdated.unwrap()) {
3648+
.func => |f| .{ .analyze_func = f },
3649+
.@"comptime",
3650+
.nav_val,
3651+
.type,
3652+
=> .{ .analyze_comptime_unit = outdated },
3653+
});
36483654
continue;
36493655
}
36503656
}
@@ -3667,8 +3673,8 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job, prog_node: std.Progre
36673673
.codegen_nav => |nav_index| {
36683674
const zcu = comp.zcu.?;
36693675
const nav = zcu.intern_pool.getNav(nav_index);
3670-
if (nav.analysis_owner.unwrap()) |cau| {
3671-
const unit = InternPool.AnalUnit.wrap(.{ .cau = cau });
3676+
if (nav.analysis != null) {
3677+
const unit: InternPool.AnalUnit = .wrap(.{ .nav_val = nav_index });
36723678
if (zcu.failed_analysis.contains(unit) or zcu.transitive_failed_analysis.contains(unit)) {
36733679
return;
36743680
}
@@ -3688,36 +3694,47 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job, prog_node: std.Progre
36883694

36893695
const pt: Zcu.PerThread = .activate(comp.zcu.?, @enumFromInt(tid));
36903696
defer pt.deactivate();
3691-
pt.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
3692-
error.OutOfMemory => return error.OutOfMemory,
3697+
3698+
pt.ensureFuncBodyUpToDate(func) catch |err| switch (err) {
3699+
error.OutOfMemory => |e| return e,
36933700
error.AnalysisFail => return,
36943701
};
36953702
},
3696-
.analyze_cau => |cau_index| {
3703+
.analyze_comptime_unit => |unit| {
3704+
const named_frame = tracy.namedFrame("analyze_comptime_unit");
3705+
defer named_frame.end();
3706+
36973707
const pt: Zcu.PerThread = .activate(comp.zcu.?, @enumFromInt(tid));
36983708
defer pt.deactivate();
3699-
pt.ensureCauAnalyzed(cau_index) catch |err| switch (err) {
3700-
error.OutOfMemory => return error.OutOfMemory,
3709+
3710+
const maybe_err: Zcu.SemaError!void = switch (unit.unwrap()) {
3711+
.@"comptime" => |cu| pt.ensureComptimeUnitUpToDate(cu),
3712+
.nav_val => |nav| pt.ensureNavValUpToDate(nav),
3713+
.type => |ty| if (pt.ensureTypeUpToDate(ty)) |_| {} else |err| err,
3714+
.func => unreachable,
3715+
};
3716+
maybe_err catch |err| switch (err) {
3717+
error.OutOfMemory => |e| return e,
37013718
error.AnalysisFail => return,
37023719
};
3720+
37033721
queue_test_analysis: {
37043722
if (!comp.config.is_test) break :queue_test_analysis;
3723+
const nav = switch (unit.unwrap()) {
3724+
.nav_val => |nav| nav,
3725+
else => break :queue_test_analysis,
3726+
};
37053727

37063728
// Check if this is a test function.
37073729
const ip = &pt.zcu.intern_pool;
3708-
const cau = ip.getCau(cau_index);
3709-
const nav_index = switch (cau.owner.unwrap()) {
3710-
.none, .type => break :queue_test_analysis,
3711-
.nav => |nav| nav,
3712-
};
3713-
if (!pt.zcu.test_functions.contains(nav_index)) {
3730+
if (!pt.zcu.test_functions.contains(nav)) {
37143731
break :queue_test_analysis;
37153732
}
37163733

37173734
// Tests are always emitted in test binaries. The decl_refs are created by
37183735
// Zcu.populateTestFunctions, but this will not queue body analysis, so do
37193736
// that now.
3720-
try pt.zcu.ensureFuncBodyAnalysisQueued(ip.getNav(nav_index).status.resolved.val);
3737+
try pt.zcu.ensureFuncBodyAnalysisQueued(ip.getNav(nav).status.resolved.val);
37213738
}
37223739
},
37233740
.resolve_type_fully => |ty| {

0 commit comments

Comments
 (0)