authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-07-10 21:39:11-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-07-10 21:39:55-04:00
logc2316c52285b1319d7b44a7f7135d9e79786fd77
tree966c3af5bb0ede2e3f1e7ec69a3bf920c0122914
parent98f3a262a7aec25e0a7f0872dc7fafc9008be1d2

InternPool: make `global_error_set` thread-safe


15 files changed, 252 insertions(+), 96 deletions(-)

src/Compilation.zig+2-2
......@@ -2943,7 +2943,7 @@ pub fn totalErrorCount(comp: *Compilation) u32 {
29432943 }
29442944 }
29452945
2946 if (zcu.global_error_set.entries.len - 1 > zcu.error_limit) {
2946 if (zcu.intern_pool.global_error_set.mutate.list.len > zcu.error_limit) {
29472947 total += 1;
29482948 }
29492949 }
......@@ -3072,7 +3072,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
30723072 try addModuleErrorMsg(zcu, &bundle, value.*, &all_references);
30733073 }
30743074
3075 const actual_error_count = zcu.global_error_set.entries.len - 1;
3075 const actual_error_count = zcu.intern_pool.global_error_set.mutate.list.len;
30763076 if (actual_error_count > zcu.error_limit) {
30773077 try bundle.addRootErrorMessage(.{
30783078 .msg = try bundle.printString("ZCU used more errors than possible: used {d}, max {d}", .{
src/InternPool.zig+160-2
......@@ -6,6 +6,8 @@ locals: []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.
88shards: []Shard = &.{},
9/// Key is the error name, index is the error tag value. Index 0 has a length-0 string.
10global_error_set: GlobalErrorSet = GlobalErrorSet.empty,
911/// Cached number of active bits in a `tid`.
1012tid_width: if (single_threaded) u0 else std.math.Log2Int(u32) = 0,
1113/// Cached shift amount to put a `tid` in the top bits of a 31-bit value.
......@@ -10129,10 +10131,10 @@ pub fn getOrPutTrailingString(
1012910131 defer shard.mutate.string_map.len += 1;
1013010132 const map_header = map.header().*;
1013110133 if (shard.mutate.string_map.len < map_header.capacity * 3 / 5) {
10134 strings.appendAssumeCapacity(.{0});
1013210135 const entry = &map.entries[map_index];
1013310136 entry.hash = hash;
1013410137 entry.release(@enumFromInt(@intFromEnum(value)));
10135 strings.appendAssumeCapacity(.{0});
1013610138 return value;
1013710139 }
1013810140 const arena_state = &ip.getLocal(tid).mutate.arena;
......@@ -10171,12 +10173,12 @@ pub fn getOrPutTrailingString(
1017110173 map_index &= new_map_mask;
1017210174 if (map.entries[map_index].value == .none) break;
1017310175 }
10176 strings.appendAssumeCapacity(.{0});
1017410177 map.entries[map_index] = .{
1017510178 .value = @enumFromInt(@intFromEnum(value)),
1017610179 .hash = hash,
1017710180 };
1017810181 shard.shared.string_map.release(new_map);
10179 strings.appendAssumeCapacity(.{0});
1018010182 return value;
1018110183}
1018210184
......@@ -10942,3 +10944,159 @@ fn ptrsHaveSameAlignment(ip: *InternPool, a_ty: Index, a_info: Key.PtrType, b_ty
1094210944 return a_info.flags.alignment == b_info.flags.alignment and
1094310945 (a_info.child == b_info.child or a_info.flags.alignment != .none);
1094410946}
10947
10948const GlobalErrorSet = struct {
10949 shared: struct {
10950 names: Names,
10951 map: Shard.Map(GlobalErrorSet.Index),
10952 } align(std.atomic.cache_line),
10953 mutate: Local.MutexListMutate align(std.atomic.cache_line),
10954
10955 const Names = Local.List(struct { NullTerminatedString });
10956
10957 const empty: GlobalErrorSet = .{
10958 .shared = .{
10959 .names = Names.empty,
10960 .map = Shard.Map(GlobalErrorSet.Index).empty,
10961 },
10962 .mutate = Local.MutexListMutate.empty,
10963 };
10964
10965 const Index = enum(Zcu.ErrorInt) {
10966 none = 0,
10967 _,
10968 };
10969
10970 /// Not thread-safe, may only be called from the main thread.
10971 pub fn getNamesFromMainThread(ges: *const GlobalErrorSet) []const NullTerminatedString {
10972 return ges.shared.names.view().items(.@"0")[0..ges.mutate.list.len];
10973 }
10974
10975 fn getErrorValue(
10976 ges: *GlobalErrorSet,
10977 gpa: Allocator,
10978 arena_state: *std.heap.ArenaAllocator.State,
10979 name: NullTerminatedString,
10980 ) Allocator.Error!GlobalErrorSet.Index {
10981 if (name == .empty) return .none;
10982 const hash = std.hash.uint32(@intFromEnum(name));
10983 var map = ges.shared.map.acquire();
10984 const Map = @TypeOf(map);
10985 var map_mask = map.header().mask();
10986 const names = ges.shared.names.acquire();
10987 var map_index = hash;
10988 while (true) : (map_index += 1) {
10989 map_index &= map_mask;
10990 const entry = &map.entries[map_index];
10991 const index = entry.acquire();
10992 if (index == .none) break;
10993 if (entry.hash != hash) continue;
10994 if (names.view().items(.@"0")[@intFromEnum(index) - 1] == name) return index;
10995 }
10996 ges.mutate.mutex.lock();
10997 defer ges.mutate.mutex.unlock();
10998 if (map.entries != ges.shared.map.entries) {
10999 map = ges.shared.map;
11000 map_mask = map.header().mask();
11001 map_index = hash;
11002 }
11003 while (true) : (map_index += 1) {
11004 map_index &= map_mask;
11005 const entry = &map.entries[map_index];
11006 const index = entry.value;
11007 if (index == .none) break;
11008 if (entry.hash != hash) continue;
11009 if (names.view().items(.@"0")[@intFromEnum(index) - 1] == name) return index;
11010 }
11011 const mutable_names: Names.Mutable = .{
11012 .gpa = gpa,
11013 .arena = arena_state,
11014 .mutate = &ges.mutate.list,
11015 .list = &ges.shared.names,
11016 };
11017 try mutable_names.ensureUnusedCapacity(1);
11018 const map_header = map.header().*;
11019 if (ges.mutate.list.len < map_header.capacity * 3 / 5) {
11020 mutable_names.appendAssumeCapacity(.{name});
11021 const index: GlobalErrorSet.Index = @enumFromInt(mutable_names.mutate.len);
11022 const entry = &map.entries[map_index];
11023 entry.hash = hash;
11024 entry.release(index);
11025 return index;
11026 }
11027 var arena = arena_state.promote(gpa);
11028 defer arena_state.* = arena.state;
11029 const new_map_capacity = map_header.capacity * 2;
11030 const new_map_buf = try arena.allocator().alignedAlloc(
11031 u8,
11032 Map.alignment,
11033 Map.entries_offset + new_map_capacity * @sizeOf(Map.Entry),
11034 );
11035 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
11036 new_map.header().* = .{ .capacity = new_map_capacity };
11037 @memset(new_map.entries[0..new_map_capacity], .{ .value = .none, .hash = undefined });
11038 const new_map_mask = new_map.header().mask();
11039 map_index = 0;
11040 while (map_index < map_header.capacity) : (map_index += 1) {
11041 const entry = &map.entries[map_index];
11042 const index = entry.value;
11043 if (index == .none) continue;
11044 const item_hash = entry.hash;
11045 var new_map_index = item_hash;
11046 while (true) : (new_map_index += 1) {
11047 new_map_index &= new_map_mask;
11048 const new_entry = &new_map.entries[new_map_index];
11049 if (new_entry.value != .none) continue;
11050 new_entry.* = .{
11051 .value = index,
11052 .hash = item_hash,
11053 };
11054 break;
11055 }
11056 }
11057 map = new_map;
11058 map_index = hash;
11059 while (true) : (map_index += 1) {
11060 map_index &= new_map_mask;
11061 if (map.entries[map_index].value == .none) break;
11062 }
11063 mutable_names.appendAssumeCapacity(.{name});
11064 const index: GlobalErrorSet.Index = @enumFromInt(mutable_names.mutate.len);
11065 map.entries[map_index] = .{ .value = index, .hash = hash };
11066 ges.shared.map.release(new_map);
11067 return index;
11068 }
11069
11070 fn getErrorValueIfExists(
11071 ges: *const GlobalErrorSet,
11072 name: NullTerminatedString,
11073 ) ?GlobalErrorSet.Index {
11074 if (name == .empty) return .none;
11075 const hash = std.hash.uint32(@intFromEnum(name));
11076 const map = ges.shared.map.acquire();
11077 const map_mask = map.header().mask();
11078 const names_items = ges.shared.names.acquire().view().items(.@"0");
11079 var map_index = hash;
11080 while (true) : (map_index += 1) {
11081 map_index &= map_mask;
11082 const entry = &map.entries[map_index];
11083 const index = entry.acquire();
11084 if (index == .none) return null;
11085 if (entry.hash != hash) continue;
11086 if (names_items[@intFromEnum(index) - 1] == name) return index;
11087 }
11088 }
11089};
11090
11091pub fn getErrorValue(
11092 ip: *InternPool,
11093 gpa: Allocator,
11094 tid: Zcu.PerThread.Id,
11095 name: NullTerminatedString,
11096) Allocator.Error!Zcu.ErrorInt {
11097 return @intFromEnum(try ip.global_error_set.getErrorValue(gpa, &ip.getLocal(tid).mutate.arena, name));
11098}
11099
11100pub fn getErrorValueIfExists(ip: *const InternPool, name: NullTerminatedString) ?Zcu.ErrorInt {
11101 return @intFromEnum(ip.global_error_set.getErrorValueIfExists(name) orelse return null);
11102}
src/Sema.zig+17-14
......@@ -3473,7 +3473,7 @@ fn zirErrorSetDecl(
34733473 const name_index: Zir.NullTerminatedString = @enumFromInt(sema.code.extra[extra_index]);
34743474 const name = sema.code.nullTerminatedString(name_index);
34753475 const name_ip = try mod.intern_pool.getOrPutString(gpa, pt.tid, name, .no_embedded_nulls);
3476 _ = try mod.getErrorValue(name_ip);
3476 _ = try pt.getErrorValue(name_ip);
34773477 const result = names.getOrPutAssumeCapacity(name_ip);
34783478 assert(!result.found_existing); // verified in AstGen
34793479 }
......@@ -8705,7 +8705,7 @@ fn zirErrorValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
87058705 inst_data.get(sema.code),
87068706 .no_embedded_nulls,
87078707 );
8708 _ = try pt.zcu.getErrorValue(name);
8708 _ = try pt.getErrorValue(name);
87098709 // Create an error set type with only this error value, and return the value.
87108710 const error_set_type = try pt.singleErrorSetType(name);
87118711 return Air.internedToRef((try pt.intern(.{ .err = .{
......@@ -8735,7 +8735,7 @@ fn zirIntFromError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD
87358735 const err_name = ip.indexToKey(val.toIntern()).err.name;
87368736 return Air.internedToRef((try pt.intValue(
87378737 err_int_ty,
8738 try mod.getErrorValue(err_name),
8738 try pt.getErrorValue(err_name),
87398739 )).toIntern());
87408740 }
87418741
......@@ -8746,10 +8746,7 @@ fn zirIntFromError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD
87468746 const names = ip.indexToKey(err_set_ty_index).error_set_type.names;
87478747 switch (names.len) {
87488748 0 => return Air.internedToRef((try pt.intValue(err_int_ty, 0)).toIntern()),
8749 1 => {
8750 const int: Module.ErrorInt = @intCast(mod.global_error_set.getIndex(names.get(ip)[0]).?);
8751 return pt.intRef(err_int_ty, int);
8752 },
8749 1 => return pt.intRef(err_int_ty, ip.getErrorValueIfExists(names.get(ip)[0]).?),
87538750 else => {},
87548751 }
87558752 },
......@@ -8765,6 +8762,7 @@ fn zirErrorFromInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD
87658762
87668763 const pt = sema.pt;
87678764 const mod = pt.zcu;
8765 const ip = &mod.intern_pool;
87688766 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
87698767 const src = block.nodeOffset(extra.node);
87708768 const operand_src = block.builtinCallArgSrc(extra.node, 0);
......@@ -8774,11 +8772,16 @@ fn zirErrorFromInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD
87748772
87758773 if (try sema.resolveDefinedValue(block, operand_src, operand)) |value| {
87768774 const int = try sema.usizeCast(block, operand_src, try value.toUnsignedIntSema(pt));
8777 if (int > mod.global_error_set.count() or int == 0)
8775 if (int > len: {
8776 const mutate = &ip.global_error_set.mutate;
8777 mutate.mutex.lock();
8778 defer mutate.mutex.unlock();
8779 break :len mutate.list.len;
8780 } or int == 0)
87788781 return sema.fail(block, operand_src, "integer value '{d}' represents no error", .{int});
87798782 return Air.internedToRef((try pt.intern(.{ .err = .{
87808783 .ty = .anyerror_type,
8781 .name = mod.global_error_set.keys()[int],
8784 .name = ip.global_error_set.shared.names.acquire().view().items(.@"0")[int - 1],
87828785 } })));
87838786 }
87848787 try sema.requireRuntimeBlock(block, src, operand_src);
......@@ -14005,7 +14008,7 @@ fn zirRetErrValueCode(sema: *Sema, inst: Zir.Inst.Index) CompileError!Air.Inst.R
1400514008 inst_data.get(sema.code),
1400614009 .no_embedded_nulls,
1400714010 );
14008 _ = try mod.getErrorValue(name);
14011 _ = try pt.getErrorValue(name);
1400914012 const error_set_type = try pt.singleErrorSetType(name);
1401014013 return Air.internedToRef((try pt.intern(.{ .err = .{
1401114014 .ty = error_set_type.toIntern(),
......@@ -19564,7 +19567,7 @@ fn zirRetErrValue(
1956419567 inst_data.get(sema.code),
1956519568 .no_embedded_nulls,
1956619569 );
19567 _ = try mod.getErrorValue(err_name);
19570 _ = try pt.getErrorValue(err_name);
1956819571 // Return the error code from the function.
1956919572 const error_set_type = try pt.singleErrorSetType(err_name);
1957019573 const result_inst = Air.internedToRef((try pt.intern(.{ .err = .{
......@@ -21607,7 +21610,7 @@ fn zirReify(
2160721610 const name = try sema.sliceToIpString(block, src, name_val, .{
2160821611 .needed_comptime_reason = "error set contents must be comptime-known",
2160921612 });
21610 _ = try mod.getErrorValue(name);
21613 _ = try pt.getErrorValue(name);
2161121614 const gop = names.getOrPutAssumeCapacity(name);
2161221615 if (gop.found_existing) {
2161321616 return sema.fail(block, src, "duplicate error '{}'", .{
......@@ -27485,7 +27488,7 @@ fn fieldVal(
2748527488 },
2748627489 .simple_type => |t| {
2748727490 assert(t == .anyerror);
27488 _ = try mod.getErrorValue(field_name);
27491 _ = try pt.getErrorValue(field_name);
2748927492 },
2749027493 else => unreachable,
2749127494 }
......@@ -27725,7 +27728,7 @@ fn fieldPtr(
2772527728 },
2772627729 .simple_type => |t| {
2772727730 assert(t == .anyerror);
27728 _ = try mod.getErrorValue(field_name);
27731 _ = try pt.getErrorValue(field_name);
2772927732 },
2773027733 else => unreachable,
2773127734 }
src/Value.zig+5-5
......@@ -417,7 +417,7 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
417417 var bigint_buffer: BigIntSpace = undefined;
418418 const bigint = BigIntMutable.init(
419419 &bigint_buffer.limbs,
420 mod.global_error_set.getIndex(name).?,
420 ip.getErrorValueIfExists(name).?,
421421 ).toConst();
422422 bigint.writeTwosComplement(buffer[0..byte_count], endian);
423423 },
......@@ -427,7 +427,7 @@ pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) erro
427427 if (val.unionTag(mod)) |union_tag| {
428428 const union_obj = mod.typeToUnion(ty).?;
429429 const field_index = mod.unionTagFieldIndex(union_obj, union_tag).?;
430 const field_type = Type.fromInterned(union_obj.field_types.get(&mod.intern_pool)[field_index]);
430 const field_type = Type.fromInterned(union_obj.field_types.get(ip)[field_index]);
431431 const field_val = try val.fieldValue(pt, field_index);
432432 const byte_count: usize = @intCast(field_type.abiSize(pt));
433433 return writeToMemory(field_val, field_type, pt, buffer[0..byte_count]);
......@@ -1455,9 +1455,9 @@ pub fn getErrorName(val: Value, mod: *const Module) InternPool.OptionalNullTermi
14551455 };
14561456}
14571457
1458pub fn getErrorInt(val: Value, mod: *const Module) Module.ErrorInt {
1459 return if (getErrorName(val, mod).unwrap()) |err_name|
1460 @intCast(mod.global_error_set.getIndex(err_name).?)
1458pub fn getErrorInt(val: Value, zcu: *Zcu) Module.ErrorInt {
1459 return if (getErrorName(val, zcu).unwrap()) |err_name|
1460 zcu.intern_pool.getErrorValueIfExists(err_name).?
14611461 else
14621462 0;
14631463}
src/Zcu.zig-22
......@@ -141,9 +141,6 @@ failed_exports: std.AutoArrayHashMapUnmanaged(u32, *ErrorMsg) = .{},
141141/// are stored here.
142142cimport_errors: std.AutoArrayHashMapUnmanaged(AnalUnit, std.zig.ErrorBundle) = .{},
143143
144/// Key is the error name, index is the error tag value. Index 0 has a length-0 string.
145global_error_set: GlobalErrorSet = .{},
146
147144/// Maximum amount of distinct error values, set by --error-limit
148145error_limit: ErrorInt,
149146
......@@ -2399,7 +2396,6 @@ pub const CompileError = error{
23992396pub fn init(mod: *Module, thread_count: usize) !void {
24002397 const gpa = mod.gpa;
24012398 try mod.intern_pool.init(gpa, thread_count);
2402 try mod.global_error_set.put(gpa, .empty, {});
24032399}
24042400
24052401pub fn deinit(zcu: *Zcu) void {
......@@ -2471,8 +2467,6 @@ pub fn deinit(zcu: *Zcu) void {
24712467 zcu.single_exports.deinit(gpa);
24722468 zcu.multi_exports.deinit(gpa);
24732469
2474 zcu.global_error_set.deinit(gpa);
2475
24762470 zcu.potentially_outdated.deinit(gpa);
24772471 zcu.outdated.deinit(gpa);
24782472 zcu.outdated_ready.deinit(gpa);
......@@ -3108,22 +3102,6 @@ pub fn addUnitReference(zcu: *Zcu, src_unit: AnalUnit, referenced_unit: AnalUnit
31083102 gop.value_ptr.* = @intCast(ref_idx);
31093103}
31103104
3111pub fn getErrorValue(
3112 mod: *Module,
3113 name: InternPool.NullTerminatedString,
3114) Allocator.Error!ErrorInt {
3115 const gop = try mod.global_error_set.getOrPut(mod.gpa, name);
3116 return @as(ErrorInt, @intCast(gop.index));
3117}
3118
3119pub fn getErrorValueFromSlice(
3120 mod: *Module,
3121 name: []const u8,
3122) Allocator.Error!ErrorInt {
3123 const interned_name = try mod.intern_pool.getOrPutString(mod.gpa, name);
3124 return getErrorValue(mod, interned_name);
3125}
3126
31273105pub fn errorSetBits(mod: *Module) u16 {
31283106 if (mod.error_limit == 0) return 0;
31293107 return std.math.log2_int_ceil(ErrorInt, mod.error_limit + 1); // +1 for no error
src/Zcu/PerThread.zig+11
......@@ -2287,6 +2287,17 @@ pub fn allocateNewDecl(pt: Zcu.PerThread, namespace: Zcu.Namespace.Index) !Zcu.D
22872287 return decl_index;
22882288}
22892289
2290pub fn getErrorValue(
2291 pt: Zcu.PerThread,
2292 name: InternPool.NullTerminatedString,
2293) Allocator.Error!Zcu.ErrorInt {
2294 return pt.zcu.intern_pool.getErrorValue(pt.zcu.gpa, pt.tid, name);
2295}
2296
2297pub fn getErrorValueFromSlice(pt: Zcu.PerThread, name: []const u8) Allocator.Error!Zcu.ErrorInt {
2298 return pt.getErrorValue(try pt.zcu.intern_pool.getOrPutString(pt.zcu.gpa, name));
2299}
2300
22902301pub fn initNewAnonDecl(
22912302 pt: Zcu.PerThread,
22922303 new_decl_index: Zcu.Decl.Index,
src/arch/wasm/CodeGen.zig+9-14
......@@ -3304,7 +3304,7 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue {
33043304 }
33053305 },
33063306 .err => |err| {
3307 const int = try mod.getErrorValue(err.name);
3307 const int = try pt.getErrorValue(err.name);
33083308 return WValue{ .imm32 = int };
33093309 },
33103310 .error_union => |error_union| {
......@@ -3452,30 +3452,25 @@ fn emitUndefined(func: *CodeGen, ty: Type) InnerError!WValue {
34523452/// Returns a `Value` as a signed 32 bit value.
34533453/// It's illegal to provide a value with a type that cannot be represented
34543454/// as an integer value.
3455fn valueAsI32(func: *const CodeGen, val: Value, ty: Type) i32 {
3455fn valueAsI32(func: *const CodeGen, val: Value) i32 {
34563456 const pt = func.pt;
34573457 const mod = pt.zcu;
3458 const ip = &mod.intern_pool;
34583459
3459 switch (val.ip_index) {
3460 .none => {},
3460 switch (val.toIntern()) {
34613461 .bool_true => return 1,
34623462 .bool_false => return 0,
3463 else => return switch (mod.intern_pool.indexToKey(val.ip_index)) {
3464 .enum_tag => |enum_tag| intIndexAsI32(&mod.intern_pool, enum_tag.int, pt),
3463 else => return switch (ip.indexToKey(val.ip_index)) {
3464 .enum_tag => |enum_tag| intIndexAsI32(ip, enum_tag.int, pt),
34653465 .int => |int| intStorageAsI32(int.storage, pt),
34663466 .ptr => |ptr| {
34673467 assert(ptr.base_addr == .int);
34683468 return @intCast(ptr.byte_offset);
34693469 },
3470 .err => |err| @as(i32, @bitCast(@as(Zcu.ErrorInt, @intCast(mod.global_error_set.getIndex(err.name).?)))),
3470 .err => |err| @bitCast(ip.getErrorValueIfExists(err.name).?),
34713471 else => unreachable,
34723472 },
34733473 }
3474
3475 return switch (ty.zigTypeTag(mod)) {
3476 .ErrorSet => @as(i32, @bitCast(val.getErrorInt(mod))),
3477 else => unreachable, // Programmer called this function for an illegal type
3478 };
34793474}
34803475
34813476fn intIndexAsI32(ip: *const InternPool, int: InternPool.Index, pt: Zcu.PerThread) i32 {
......@@ -4098,7 +4093,7 @@ fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
40984093
40994094 for (items, 0..) |ref, i| {
41004095 const item_val = (try func.air.value(ref, pt)).?;
4101 const int_val = func.valueAsI32(item_val, target_ty);
4096 const int_val = func.valueAsI32(item_val);
41024097 if (lowest_maybe == null or int_val < lowest_maybe.?) {
41034098 lowest_maybe = int_val;
41044099 }
......@@ -7454,7 +7449,7 @@ fn airErrorSetHasValue(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
74547449 var lowest: ?u32 = null;
74557450 var highest: ?u32 = null;
74567451 for (0..names.len) |name_index| {
7457 const err_int: Zcu.ErrorInt = @intCast(mod.global_error_set.getIndex(names.get(ip)[name_index]).?);
7452 const err_int = ip.getErrorValueIfExists(names.get(ip)[name_index]).?;
74587453 if (lowest) |*l| {
74597454 if (err_int < l.*) {
74607455 l.* = err_int;
src/arch/x86_64/CodeGen.zig+2-2
......@@ -16435,7 +16435,7 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void {
1643516435 .size = .dword,
1643616436 .index = err_reg.to64(),
1643716437 .scale = .@"4",
16438 .disp = 4,
16438 .disp = (1 - 1) * 4,
1643916439 } },
1644016440 },
1644116441 );
......@@ -16448,7 +16448,7 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void {
1644816448 .size = .dword,
1644916449 .index = err_reg.to64(),
1645016450 .scale = .@"4",
16451 .disp = 8,
16451 .disp = (2 - 1) * 4,
1645216452 } },
1645316453 },
1645416454 );
src/codegen.zig+5-5
......@@ -137,10 +137,10 @@ pub fn generateLazySymbol(
137137
138138 if (lazy_sym.ty.isAnyError(pt.zcu)) {
139139 alignment.* = .@"4";
140 const err_names = pt.zcu.global_error_set.keys();
140 const err_names = ip.global_error_set.getNamesFromMainThread();
141141 mem.writeInt(u32, try code.addManyAsArray(4), @intCast(err_names.len), endian);
142142 var offset = code.items.len;
143 try code.resize((1 + err_names.len + 1) * 4);
143 try code.resize((err_names.len + 1) * 4);
144144 for (err_names) |err_name_nts| {
145145 const err_name = err_name_nts.toSlice(ip);
146146 mem.writeInt(u32, code.items[offset..][0..4], @intCast(code.items.len), endian);
......@@ -243,13 +243,13 @@ pub fn generateSymbol(
243243 int_val.writeTwosComplement(try code.addManyAsSlice(abi_size), endian);
244244 },
245245 .err => |err| {
246 const int = try mod.getErrorValue(err.name);
246 const int = try pt.getErrorValue(err.name);
247247 try code.writer().writeInt(u16, @intCast(int), endian);
248248 },
249249 .error_union => |error_union| {
250250 const payload_ty = ty.errorUnionPayload(mod);
251251 const err_val: u16 = switch (error_union.val) {
252 .err_name => |err_name| @intCast(try mod.getErrorValue(err_name)),
252 .err_name => |err_name| @intCast(try pt.getErrorValue(err_name)),
253253 .payload => 0,
254254 };
255255
......@@ -1058,7 +1058,7 @@ pub fn genTypedValue(
10581058 },
10591059 .ErrorSet => {
10601060 const err_name = ip.indexToKey(val.toIntern()).err.name;
1061 const error_index = zcu.global_error_set.getIndex(err_name).?;
1061 const error_index = try pt.getErrorValue(err_name);
10621062 return GenResult.mcv(.{ .immediate = error_index });
10631063 },
10641064 .ErrorUnion => {
src/codegen/c.zig+8-7
......@@ -2622,10 +2622,11 @@ pub fn genErrDecls(o: *Object) !void {
26222622
26232623 var max_name_len: usize = 0;
26242624 // do not generate an invalid empty enum when the global error set is empty
2625 if (zcu.global_error_set.keys().len > 1) {
2625 const names = ip.global_error_set.getNamesFromMainThread();
2626 if (names.len > 0) {
26262627 try writer.writeAll("enum {\n");
26272628 o.indent_writer.pushIndent();
2628 for (zcu.global_error_set.keys()[1..], 1..) |name_nts, value| {
2629 for (names, 1..) |name_nts, value| {
26292630 const name = name_nts.toSlice(ip);
26302631 max_name_len = @max(name.len, max_name_len);
26312632 const err_val = try pt.intern(.{ .err = .{
......@@ -2644,7 +2645,7 @@ pub fn genErrDecls(o: *Object) !void {
26442645 defer o.dg.gpa.free(name_buf);
26452646
26462647 @memcpy(name_buf[0..name_prefix.len], name_prefix);
2647 for (zcu.global_error_set.keys()) |name| {
2648 for (names) |name| {
26482649 const name_slice = name.toSlice(ip);
26492650 @memcpy(name_buf[name_prefix.len..][0..name_slice.len], name_slice);
26502651 const identifier = name_buf[0 .. name_prefix.len + name_slice.len];
......@@ -2674,7 +2675,7 @@ pub fn genErrDecls(o: *Object) !void {
26742675 }
26752676
26762677 const name_array_ty = try pt.arrayType(.{
2677 .len = zcu.global_error_set.count(),
2678 .len = 1 + names.len,
26782679 .child = .slice_const_u8_sentinel_0_type,
26792680 });
26802681
......@@ -2688,9 +2689,9 @@ pub fn genErrDecls(o: *Object) !void {
26882689 .complete,
26892690 );
26902691 try writer.writeAll(" = {");
2691 for (zcu.global_error_set.keys(), 0..) |name_nts, value| {
2692 for (names, 1..) |name_nts, val| {
26922693 const name = name_nts.toSlice(ip);
2693 if (value != 0) try writer.writeByte(',');
2694 if (val > 1) try writer.writeAll(", ");
26942695 try writer.print("{{" ++ name_prefix ++ "{}, {}}}", .{
26952696 fmtIdent(name),
26962697 try o.dg.fmtIntLiteral(try pt.intValue(Type.usize, name.len), .StaticInitializer),
......@@ -6873,7 +6874,7 @@ fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue {
68736874
68746875 try writer.writeAll(" = zig_errorName[");
68756876 try f.writeCValue(writer, operand, .Other);
6876 try writer.writeAll("];\n");
6877 try writer.writeAll(" - 1];\n");
68776878 return local;
68786879}
68796880
src/codegen/llvm.zig+11-10
......@@ -1036,20 +1036,21 @@ pub const Object = struct {
10361036
10371037 const pt = o.pt;
10381038 const mod = pt.zcu;
1039 const ip = &mod.intern_pool;
10391040
1040 const error_name_list = mod.global_error_set.keys();
1041 const llvm_errors = try mod.gpa.alloc(Builder.Constant, error_name_list.len);
1041 const error_name_list = ip.global_error_set.getNamesFromMainThread();
1042 const llvm_errors = try mod.gpa.alloc(Builder.Constant, 1 + error_name_list.len);
10421043 defer mod.gpa.free(llvm_errors);
10431044
10441045 // TODO: Address space
10451046 const slice_ty = Type.slice_const_u8_sentinel_0;
10461047 const llvm_usize_ty = try o.lowerType(Type.usize);
10471048 const llvm_slice_ty = try o.lowerType(slice_ty);
1048 const llvm_table_ty = try o.builder.arrayType(error_name_list.len, llvm_slice_ty);
1049 const llvm_table_ty = try o.builder.arrayType(1 + error_name_list.len, llvm_slice_ty);
10491050
10501051 llvm_errors[0] = try o.builder.undefConst(llvm_slice_ty);
1051 for (llvm_errors[1..], error_name_list[1..]) |*llvm_error, name| {
1052 const name_string = try o.builder.stringNull(name.toSlice(&mod.intern_pool));
1052 for (llvm_errors[1..], error_name_list) |*llvm_error, name| {
1053 const name_string = try o.builder.stringNull(name.toSlice(ip));
10531054 const name_init = try o.builder.stringConst(name_string);
10541055 const name_variable_index =
10551056 try o.builder.addVariable(.empty, name_init.typeOf(&o.builder), .default);
......@@ -1085,7 +1086,7 @@ pub const Object = struct {
10851086 // If there is no such function in the module, it means the source code does not need it.
10861087 const name = o.builder.strtabStringIfExists(lt_errors_fn_name) orelse return;
10871088 const llvm_fn = o.builder.getGlobal(name) orelse return;
1088 const errors_len = o.pt.zcu.global_error_set.count();
1089 const errors_len = o.pt.zcu.intern_pool.global_error_set.mutate.list.len;
10891090
10901091 var wip = try Builder.WipFunction.init(&o.builder, .{
10911092 .function = llvm_fn.ptrConst(&o.builder).kind.function,
......@@ -1096,12 +1097,12 @@ pub const Object = struct {
10961097
10971098 // Example source of the following LLVM IR:
10981099 // fn __zig_lt_errors_len(index: u16) bool {
1099 // return index < total_errors_len;
1100 // return index <= total_errors_len;
11001101 // }
11011102
11021103 const lhs = wip.arg(0);
11031104 const rhs = try o.builder.intValue(try o.errorIntType(), errors_len);
1104 const is_lt = try wip.icmp(.ult, lhs, rhs, "");
1105 const is_lt = try wip.icmp(.ule, lhs, rhs, "");
11051106 _ = try wip.ret(is_lt);
11061107 try wip.finish();
11071108 }
......@@ -3820,7 +3821,7 @@ pub const Object = struct {
38203821 return lowerBigInt(o, ty, bigint);
38213822 },
38223823 .err => |err| {
3823 const int = try mod.getErrorValue(err.name);
3824 const int = try pt.getErrorValue(err.name);
38243825 const llvm_int = try o.builder.intConst(try o.errorIntType(), int);
38253826 return llvm_int;
38263827 },
......@@ -9658,7 +9659,7 @@ pub const FuncGen = struct {
96589659 defer wip_switch.finish(&self.wip);
96599660
96609661 for (0..names.len) |name_index| {
9661 const err_int = mod.global_error_set.getIndex(names.get(ip)[name_index]).?;
9662 const err_int = ip.getErrorValueIfExists(names.get(ip)[name_index]).?;
96629663 const this_tag_int_value = try o.builder.intConst(try o.errorIntType(), err_int);
96639664 try wip_switch.addCase(this_tag_int_value, valid_block, &self.wip);
96649665 }
src/codegen/spirv.zig+1-1
......@@ -963,7 +963,7 @@ const DeclGen = struct {
963963 break :cache result_id;
964964 },
965965 .err => |err| {
966 const value = try mod.getErrorValue(err.name);
966 const value = try pt.getErrorValue(err.name);
967967 break :cache try self.constInt(ty, value, repr);
968968 },
969969 .error_union => |error_union| {
src/link/Dwarf.zig+2-2
......@@ -2698,7 +2698,7 @@ pub fn flushModule(self: *Dwarf, pt: Zcu.PerThread) !void {
26982698 try addDbgInfoErrorSetNames(
26992699 pt,
27002700 Type.anyerror,
2701 pt.zcu.global_error_set.keys(),
2701 pt.zcu.intern_pool.global_error_set.getNamesFromMainThread(),
27022702 target,
27032703 &dbg_info_buffer,
27042704 );
......@@ -2867,7 +2867,7 @@ fn addDbgInfoErrorSetNames(
28672867 mem.writeInt(u64, dbg_info_buffer.addManyAsArrayAssumeCapacity(8), 0, target_endian);
28682868
28692869 for (error_names) |error_name| {
2870 const int = try pt.zcu.getErrorValue(error_name);
2870 const int = try pt.getErrorValue(error_name);
28712871 const error_name_slice = error_name.toSlice(&pt.zcu.intern_pool);
28722872 // DW.AT.enumerator
28732873 try dbg_info_buffer.ensureUnusedCapacity(error_name_slice.len + 2 + @sizeOf(u64));
src/link/SpirV.zig+4-4
......@@ -227,9 +227,9 @@ pub fn flushModule(self: *SpirV, arena: Allocator, tid: Zcu.PerThread.Id, prog_n
227227 var error_info = std.ArrayList(u8).init(self.object.gpa);
228228 defer error_info.deinit();
229229
230 try error_info.appendSlice("zig_errors");
231 const mod = self.base.comp.module.?;
232 for (mod.global_error_set.keys()) |name| {
230 try error_info.appendSlice("zig_errors:");
231 const ip = &self.base.comp.module.?.intern_pool;
232 for (ip.global_error_set.getNamesFromMainThread()) |name| {
233233 // Errors can contain pretty much any character - to encode them in a string we must escape
234234 // them somehow. Easiest here is to use some established scheme, one which also preseves the
235235 // name if it contains no strange characters is nice for debugging. URI encoding fits the bill.
......@@ -238,7 +238,7 @@ pub fn flushModule(self: *SpirV, arena: Allocator, tid: Zcu.PerThread.Id, prog_n
238238 try error_info.append(':');
239239 try std.Uri.Component.percentEncode(
240240 error_info.writer(),
241 name.toSlice(&mod.intern_pool),
241 name.toSlice(ip),
242242 struct {
243243 fn isValidChar(c: u8) bool {
244244 return switch (c) {
src/link/Wasm/ZigObject.zig+15-6
......@@ -652,13 +652,22 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm, tid: Zcu.Per
652652 // Addend for each relocation to the table
653653 var addend: u32 = 0;
654654 const pt: Zcu.PerThread = .{ .zcu = wasm_file.base.comp.module.?, .tid = tid };
655 for (pt.zcu.global_error_set.keys()) |error_name| {
656 const atom = wasm_file.getAtomPtr(atom_index);
655 const slice_ty = Type.slice_const_u8_sentinel_0;
656 const atom = wasm_file.getAtomPtr(atom_index);
657 {
658 // TODO: remove this unreachable entry
659 try atom.code.appendNTimes(gpa, 0, 4);
660 try atom.code.writer(gpa).writeInt(u32, 0, .little);
661 atom.size += @intCast(slice_ty.abiSize(pt));
662 addend += 1;
657663
658 const error_name_slice = error_name.toSlice(&pt.zcu.intern_pool);
664 try names_atom.code.append(gpa, 0);
665 }
666 const ip = &pt.zcu.intern_pool;
667 for (ip.global_error_set.getNamesFromMainThread()) |error_name| {
668 const error_name_slice = error_name.toSlice(ip);
659669 const len: u32 = @intCast(error_name_slice.len + 1); // names are 0-terminated
660670
661 const slice_ty = Type.slice_const_u8_sentinel_0;
662671 const offset = @as(u32, @intCast(atom.code.items.len));
663672 // first we create the data for the slice of the name
664673 try atom.code.appendNTimes(gpa, 0, 4); // ptr to name, will be relocated
......@@ -677,7 +686,7 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm, tid: Zcu.Per
677686 try names_atom.code.ensureUnusedCapacity(gpa, len);
678687 names_atom.code.appendSliceAssumeCapacity(error_name_slice[0..len]);
679688
680 log.debug("Populated error name: '{}'", .{error_name.fmt(&pt.zcu.intern_pool)});
689 log.debug("Populated error name: '{}'", .{error_name.fmt(ip)});
681690 }
682691 names_atom.size = addend;
683692 zig_object.error_names_atom = names_atom_index;
......@@ -1042,7 +1051,7 @@ fn setupErrorsLen(zig_object: *ZigObject, wasm_file: *Wasm) !void {
10421051 const gpa = wasm_file.base.comp.gpa;
10431052 const sym_index = zig_object.findGlobalSymbol("__zig_errors_len") orelse return;
10441053
1045 const errors_len = wasm_file.base.comp.module.?.global_error_set.count();
1054 const errors_len = 1 + wasm_file.base.comp.module.?.intern_pool.global_error_set.mutate.list.len;
10461055 // overwrite existing atom if it already exists (maybe the error set has increased)
10471056 // if not, allcoate a new atom.
10481057 const atom_index = if (wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = sym_index })) |index| blk: {