Skip to content

Commit d404d8a

Browse files
authored
Merge pull request #20593 from jacobly0/more-races
InternPool: fix more races
2 parents 464537d + ad55fb7 commit d404d8a

31 files changed

+998
-516
lines changed

src/Air/types_resolved.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@ fn checkType(ty: Type, zcu: *Zcu) bool {
501501
.struct_type => {
502502
const struct_obj = zcu.typeToStruct(ty).?;
503503
return switch (struct_obj.layout) {
504-
.@"packed" => struct_obj.backingIntType(ip).* != .none,
505-
.auto, .@"extern" => struct_obj.flagsPtr(ip).fully_resolved,
504+
.@"packed" => struct_obj.backingIntTypeUnordered(ip) != .none,
505+
.auto, .@"extern" => struct_obj.flagsUnordered(ip).fully_resolved,
506506
};
507507
},
508508
.anon_struct_type => |tuple| {
@@ -516,6 +516,6 @@ fn checkType(ty: Type, zcu: *Zcu) bool {
516516
},
517517
else => unreachable,
518518
},
519-
.Union => return zcu.typeToUnion(ty).?.flagsPtr(ip).status == .fully_resolved,
519+
.Union => return zcu.typeToUnion(ty).?.flagsUnordered(ip).status == .fully_resolved,
520520
};
521521
}

src/Compilation.zig

Lines changed: 165 additions & 89 deletions
Large diffs are not rendered by default.

src/InternPool.zig

Lines changed: 584 additions & 153 deletions
Large diffs are not rendered by default.

src/Sema.zig

Lines changed: 98 additions & 123 deletions
Large diffs are not rendered by default.

src/Type.zig

Lines changed: 48 additions & 47 deletions
Large diffs are not rendered by default.

src/Value.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ pub fn writeToPackedMemory(
558558
},
559559
.Union => {
560560
const union_obj = mod.typeToUnion(ty).?;
561-
switch (union_obj.getLayout(ip)) {
561+
switch (union_obj.flagsUnordered(ip).layout) {
562562
.auto, .@"extern" => unreachable, // Handled in non-packed writeToMemory
563563
.@"packed" => {
564564
if (val.unionTag(mod)) |union_tag| {

src/Zcu.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,7 +2968,7 @@ pub fn ensureFuncBodyAnalysisQueued(mod: *Module, func_index: InternPool.Index)
29682968
const is_outdated = mod.outdated.contains(func_as_depender) or
29692969
mod.potentially_outdated.contains(func_as_depender);
29702970

2971-
switch (func.analysis(ip).state) {
2971+
switch (func.analysisUnordered(ip).state) {
29722972
.none => {},
29732973
.queued => return,
29742974
// As above, we don't need to forward errors here.
@@ -2983,13 +2983,13 @@ pub fn ensureFuncBodyAnalysisQueued(mod: *Module, func_index: InternPool.Index)
29832983

29842984
// Decl itself is safely analyzed, and body analysis is not yet queued
29852985

2986-
try mod.comp.work_queue.writeItem(.{ .analyze_func = func_index });
2986+
try mod.comp.queueJob(.{ .analyze_func = func_index });
29872987
if (mod.emit_h != null) {
29882988
// TODO: we ideally only want to do this if the function's type changed
29892989
// since the last update
2990-
try mod.comp.work_queue.writeItem(.{ .emit_h_decl = decl_index });
2990+
try mod.comp.queueJob(.{ .emit_h_decl = decl_index });
29912991
}
2992-
func.analysis(ip).state = .queued;
2992+
func.setAnalysisState(ip, .queued);
29932993
}
29942994

29952995
pub const SemaDeclResult = packed struct {

src/Zcu/PerThread.zig

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@ pub fn ensureFuncBodyAnalyzed(pt: Zcu.PerThread, maybe_coerced_func_index: Inter
641641

642642
// We'll want to remember what the IES used to be before the update for
643643
// dependency invalidation purposes.
644-
const old_resolved_ies = if (func.analysis(ip).inferred_error_set)
645-
func.resolvedErrorSet(ip).*
644+
const old_resolved_ies = if (func.analysisUnordered(ip).inferred_error_set)
645+
func.resolvedErrorSetUnordered(ip)
646646
else
647647
.none;
648648

@@ -671,7 +671,7 @@ pub fn ensureFuncBodyAnalyzed(pt: Zcu.PerThread, maybe_coerced_func_index: Inter
671671
zcu.deleteUnitReferences(func_as_depender);
672672
}
673673

674-
switch (func.analysis(ip).state) {
674+
switch (func.analysisUnordered(ip).state) {
675675
.success => if (!was_outdated) return,
676676
.sema_failure,
677677
.dependency_failure,
@@ -693,11 +693,11 @@ pub fn ensureFuncBodyAnalyzed(pt: Zcu.PerThread, maybe_coerced_func_index: Inter
693693

694694
var air = pt.analyzeFnBody(func_index, sema_arena) catch |err| switch (err) {
695695
error.AnalysisFail => {
696-
if (func.analysis(ip).state == .in_progress) {
696+
if (func.analysisUnordered(ip).state == .in_progress) {
697697
// If this decl caused the compile error, the analysis field would
698698
// be changed to indicate it was this Decl's fault. Because this
699699
// did not happen, we infer here that it was a dependency failure.
700-
func.analysis(ip).state = .dependency_failure;
700+
func.setAnalysisState(ip, .dependency_failure);
701701
}
702702
return error.AnalysisFail;
703703
},
@@ -707,8 +707,8 @@ pub fn ensureFuncBodyAnalyzed(pt: Zcu.PerThread, maybe_coerced_func_index: Inter
707707

708708
const invalidate_ies_deps = i: {
709709
if (!was_outdated) break :i false;
710-
if (!func.analysis(ip).inferred_error_set) break :i true;
711-
const new_resolved_ies = func.resolvedErrorSet(ip).*;
710+
if (!func.analysisUnordered(ip).inferred_error_set) break :i true;
711+
const new_resolved_ies = func.resolvedErrorSetUnordered(ip);
712712
break :i new_resolved_ies != old_resolved_ies;
713713
};
714714
if (invalidate_ies_deps) {
@@ -729,7 +729,7 @@ pub fn ensureFuncBodyAnalyzed(pt: Zcu.PerThread, maybe_coerced_func_index: Inter
729729
return;
730730
}
731731

732-
try comp.work_queue.writeItem(.{ .codegen_func = .{
732+
try comp.queueJob(.{ .codegen_func = .{
733733
.func = func_index,
734734
.air = air,
735735
} });
@@ -783,7 +783,7 @@ pub fn linkerUpdateFunc(pt: Zcu.PerThread, func_index: InternPool.Index, air: Ai
783783
.{@errorName(err)},
784784
),
785785
);
786-
func.analysis(ip).state = .codegen_failure;
786+
func.setAnalysisState(ip, .codegen_failure);
787787
return;
788788
},
789789
};
@@ -797,12 +797,12 @@ pub fn linkerUpdateFunc(pt: Zcu.PerThread, func_index: InternPool.Index, air: Ai
797797
// Correcting this failure will involve changing a type this function
798798
// depends on, hence triggering re-analysis of this function, so this
799799
// interacts correctly with incremental compilation.
800-
func.analysis(ip).state = .codegen_failure;
800+
func.setAnalysisState(ip, .codegen_failure);
801801
} else if (comp.bin_file) |lf| {
802802
lf.updateFunc(pt, func_index, air, liveness) catch |err| switch (err) {
803803
error.OutOfMemory => return error.OutOfMemory,
804804
error.AnalysisFail => {
805-
func.analysis(ip).state = .codegen_failure;
805+
func.setAnalysisState(ip, .codegen_failure);
806806
},
807807
else => {
808808
try zcu.failed_analysis.ensureUnusedCapacity(gpa, 1);
@@ -812,7 +812,7 @@ pub fn linkerUpdateFunc(pt: Zcu.PerThread, func_index: InternPool.Index, air: Ai
812812
"unable to codegen: {s}",
813813
.{@errorName(err)},
814814
));
815-
func.analysis(ip).state = .codegen_failure;
815+
func.setAnalysisState(ip, .codegen_failure);
816816
try zcu.retryable_failures.append(zcu.gpa, InternPool.AnalUnit.wrap(.{ .func = func_index }));
817817
},
818818
};
@@ -903,7 +903,7 @@ fn getFileRootStruct(
903903
decl.analysis = .complete;
904904

905905
try pt.scanNamespace(namespace_index, decls, decl);
906-
try zcu.comp.work_queue.writeItem(.{ .resolve_type_fully = wip_ty.index });
906+
try zcu.comp.queueJob(.{ .resolve_type_fully = wip_ty.index });
907907
return wip_ty.finish(ip, decl_index, namespace_index.toOptional());
908908
}
909909

@@ -1080,7 +1080,7 @@ fn semaDecl(pt: Zcu.PerThread, decl_index: Zcu.Decl.Index) !Zcu.SemaDeclResult {
10801080
const old_linksection = decl.@"linksection";
10811081
const old_addrspace = decl.@"addrspace";
10821082
const old_is_inline = if (decl.getOwnedFunction(zcu)) |prev_func|
1083-
prev_func.analysis(ip).state == .inline_only
1083+
prev_func.analysisUnordered(ip).state == .inline_only
10841084
else
10851085
false;
10861086

@@ -1311,10 +1311,10 @@ fn semaDecl(pt: Zcu.PerThread, decl_index: Zcu.Decl.Index) !Zcu.SemaDeclResult {
13111311
// codegen backend wants full access to the Decl Type.
13121312
try decl_ty.resolveFully(pt);
13131313

1314-
try zcu.comp.work_queue.writeItem(.{ .codegen_decl = decl_index });
1314+
try zcu.comp.queueJob(.{ .codegen_decl = decl_index });
13151315

13161316
if (result.invalidate_decl_ref and zcu.emit_h != null) {
1317-
try zcu.comp.work_queue.writeItem(.{ .emit_h_decl = decl_index });
1317+
try zcu.comp.queueJob(.{ .emit_h_decl = decl_index });
13181318
}
13191319
}
13201320

@@ -1740,8 +1740,6 @@ pub fn scanNamespace(
17401740
var seen_decls: std.AutoHashMapUnmanaged(InternPool.NullTerminatedString, void) = .{};
17411741
defer seen_decls.deinit(gpa);
17421742

1743-
try zcu.comp.work_queue.ensureUnusedCapacity(decls.len);
1744-
17451743
namespace.decls.clearRetainingCapacity();
17461744
try namespace.decls.ensureTotalCapacity(gpa, decls.len);
17471745

@@ -1967,7 +1965,7 @@ const ScanDeclIter = struct {
19671965
log.debug("scanDecl queue analyze_decl file='{s}' decl_name='{}' decl_index={d}", .{
19681966
namespace.fileScope(zcu).sub_file_path, decl_name.fmt(ip), decl_index,
19691967
});
1970-
comp.work_queue.writeItemAssumeCapacity(.{ .analyze_decl = decl_index });
1968+
try comp.queueJob(.{ .analyze_decl = decl_index });
19711969
}
19721970
}
19731971

@@ -1976,7 +1974,7 @@ const ScanDeclIter = struct {
19761974
// updated line numbers. Look into this!
19771975
// TODO Look into detecting when this would be unnecessary by storing enough state
19781976
// in `Decl` to notice that the line number did not change.
1979-
comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index });
1977+
try comp.queueJob(.{ .update_line_number = decl_index });
19801978
}
19811979
}
19821980
};
@@ -1991,7 +1989,7 @@ pub fn abortAnonDecl(pt: Zcu.PerThread, decl_index: Zcu.Decl.Index) void {
19911989
/// Finalize the creation of an anon decl.
19921990
pub fn finalizeAnonDecl(pt: Zcu.PerThread, decl_index: Zcu.Decl.Index) Allocator.Error!void {
19931991
if (pt.zcu.declPtr(decl_index).typeOf(pt.zcu).isFnOrHasRuntimeBits(pt)) {
1994-
try pt.zcu.comp.work_queue.writeItem(.{ .codegen_decl = decl_index });
1992+
try pt.zcu.comp.queueJob(.{ .codegen_decl = decl_index });
19951993
}
19961994
}
19971995

@@ -2037,7 +2035,7 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All
20372035
.fn_ret_ty = Type.fromInterned(fn_ty_info.return_type),
20382036
.fn_ret_ty_ies = null,
20392037
.owner_func_index = func_index,
2040-
.branch_quota = @max(func.branchQuota(ip).*, Sema.default_branch_quota),
2038+
.branch_quota = @max(func.branchQuotaUnordered(ip), Sema.default_branch_quota),
20412039
.comptime_err_ret_trace = &comptime_err_ret_trace,
20422040
};
20432041
defer sema.deinit();
@@ -2047,14 +2045,14 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All
20472045
try sema.declareDependency(.{ .src_hash = decl.zir_decl_index.unwrap().? });
20482046
try sema.declareDependency(.{ .decl_val = decl_index });
20492047

2050-
if (func.analysis(ip).inferred_error_set) {
2048+
if (func.analysisUnordered(ip).inferred_error_set) {
20512049
const ies = try arena.create(Sema.InferredErrorSet);
20522050
ies.* = .{ .func = func_index };
20532051
sema.fn_ret_ty_ies = ies;
20542052
}
20552053

20562054
// reset in case calls to errorable functions are removed.
2057-
func.analysis(ip).calls_or_awaits_errorable_fn = false;
2055+
func.setCallsOrAwaitsErrorableFn(ip, false);
20582056

20592057
// First few indexes of extra are reserved and set at the end.
20602058
const reserved_count = @typeInfo(Air.ExtraIndex).Enum.fields.len;
@@ -2080,7 +2078,7 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All
20802078
};
20812079
defer inner_block.instructions.deinit(gpa);
20822080

2083-
const fn_info = sema.code.getFnInfo(func.zirBodyInst(ip).resolve(ip));
2081+
const fn_info = sema.code.getFnInfo(func.zirBodyInstUnordered(ip).resolve(ip));
20842082

20852083
// Here we are performing "runtime semantic analysis" for a function body, which means
20862084
// we must map the parameter ZIR instructions to `arg` AIR instructions.
@@ -2149,7 +2147,7 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All
21492147
});
21502148
}
21512149

2152-
func.analysis(ip).state = .in_progress;
2150+
func.setAnalysisState(ip, .in_progress);
21532151

21542152
const last_arg_index = inner_block.instructions.items.len;
21552153

@@ -2176,7 +2174,7 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All
21762174
}
21772175

21782176
// If we don't get an error return trace from a caller, create our own.
2179-
if (func.analysis(ip).calls_or_awaits_errorable_fn and
2177+
if (func.analysisUnordered(ip).calls_or_awaits_errorable_fn and
21802178
mod.comp.config.any_error_tracing and
21812179
!sema.fn_ret_ty.isError(mod))
21822180
{
@@ -2218,10 +2216,10 @@ pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: All
22182216
else => |e| return e,
22192217
};
22202218
assert(ies.resolved != .none);
2221-
ip.funcIesResolved(func_index).* = ies.resolved;
2219+
ip.funcSetIesResolved(func_index, ies.resolved);
22222220
}
22232221

2224-
func.analysis(ip).state = .success;
2222+
func.setAnalysisState(ip, .success);
22252223

22262224
// Finally we must resolve the return type and parameter types so that backends
22272225
// have full access to type information.
@@ -2415,6 +2413,7 @@ fn processExportsInner(
24152413
) error{OutOfMemory}!void {
24162414
const zcu = pt.zcu;
24172415
const gpa = zcu.gpa;
2416+
const ip = &zcu.intern_pool;
24182417

24192418
for (export_indices) |export_idx| {
24202419
const new_export = &zcu.all_exports.items[export_idx];
@@ -2423,7 +2422,7 @@ fn processExportsInner(
24232422
new_export.status = .failed_retryable;
24242423
try zcu.failed_exports.ensureUnusedCapacity(gpa, 1);
24252424
const msg = try Zcu.ErrorMsg.create(gpa, new_export.src, "exported symbol collision: {}", .{
2426-
new_export.opts.name.fmt(&zcu.intern_pool),
2425+
new_export.opts.name.fmt(ip),
24272426
});
24282427
errdefer msg.destroy(gpa);
24292428
const other_export = zcu.all_exports.items[gop.value_ptr.*];
@@ -2443,8 +2442,7 @@ fn processExportsInner(
24432442
if (!decl.owns_tv) break :failed false;
24442443
if (decl.typeOf(zcu).zigTypeTag(zcu) != .Fn) break :failed false;
24452444
// Check if owned function failed
2446-
const a = zcu.funcInfo(decl.val.toIntern()).analysis(&zcu.intern_pool);
2447-
break :failed a.state != .success;
2445+
break :failed zcu.funcInfo(decl.val.toIntern()).analysisUnordered(ip).state != .success;
24482446
}) {
24492447
// This `Decl` is failed, so was never sent to codegen.
24502448
// TODO: we should probably tell the backend to delete any old exports of this `Decl`?
@@ -3072,7 +3070,7 @@ pub fn getUnionLayout(pt: Zcu.PerThread, loaded_union: InternPool.LoadedUnionTyp
30723070
most_aligned_field_size = field_size;
30733071
}
30743072
}
3075-
const have_tag = loaded_union.flagsPtr(ip).runtime_tag.hasTag();
3073+
const have_tag = loaded_union.flagsUnordered(ip).runtime_tag.hasTag();
30763074
if (!have_tag or !Type.fromInterned(loaded_union.enum_tag_ty).hasRuntimeBits(pt)) {
30773075
return .{
30783076
.abi_size = payload_align.forward(payload_size),
@@ -3091,7 +3089,7 @@ pub fn getUnionLayout(pt: Zcu.PerThread, loaded_union: InternPool.LoadedUnionTyp
30913089
const tag_size = Type.fromInterned(loaded_union.enum_tag_ty).abiSize(pt);
30923090
const tag_align = Type.fromInterned(loaded_union.enum_tag_ty).abiAlignment(pt).max(.@"1");
30933091
return .{
3094-
.abi_size = loaded_union.size(ip).*,
3092+
.abi_size = loaded_union.sizeUnordered(ip),
30953093
.abi_align = tag_align.max(payload_align),
30963094
.most_aligned_field = most_aligned_field,
30973095
.most_aligned_field_size = most_aligned_field_size,
@@ -3100,7 +3098,7 @@ pub fn getUnionLayout(pt: Zcu.PerThread, loaded_union: InternPool.LoadedUnionTyp
31003098
.payload_align = payload_align,
31013099
.tag_align = tag_align,
31023100
.tag_size = tag_size,
3103-
.padding = loaded_union.padding(ip).*,
3101+
.padding = loaded_union.paddingUnordered(ip),
31043102
};
31053103
}
31063104

@@ -3142,7 +3140,7 @@ pub fn unionFieldNormalAlignmentAdvanced(
31423140
strat: Type.ResolveStrat,
31433141
) Zcu.SemaError!InternPool.Alignment {
31443142
const ip = &pt.zcu.intern_pool;
3145-
assert(loaded_union.flagsPtr(ip).layout != .@"packed");
3143+
assert(loaded_union.flagsUnordered(ip).layout != .@"packed");
31463144
const field_align = loaded_union.fieldAlign(ip, field_index);
31473145
if (field_align != .none) return field_align;
31483146
const field_ty = Type.fromInterned(loaded_union.field_types.get(ip)[field_index]);

src/arch/arm/abi.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn classifyType(ty: Type, pt: Zcu.PerThread, ctx: Context) Class {
5656
.Union => {
5757
const bit_size = ty.bitSize(pt);
5858
const union_obj = pt.zcu.typeToUnion(ty).?;
59-
if (union_obj.getLayout(ip) == .@"packed") {
59+
if (union_obj.flagsUnordered(ip).layout == .@"packed") {
6060
if (bit_size > 64) return .memory;
6161
return .byval;
6262
}

src/arch/riscv64/CodeGen.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ pub fn generate(
768768
@intFromEnum(FrameIndex.stack_frame),
769769
FrameAlloc.init(.{
770770
.size = 0,
771-
.alignment = func.analysis(ip).stack_alignment.max(.@"1"),
771+
.alignment = func.analysisUnordered(ip).stack_alignment.max(.@"1"),
772772
}),
773773
);
774774
function.frame_allocs.set(

0 commit comments

Comments
 (0)