authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-06-02 04:24:25-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:59-07:00
logda24ea7f36d056cb49e8e91064f06cb724e46f67
tree0b3920d68166cf664d4c72731d1260ea34a82d72
parent04e66e6b4deb67aef9a4064decd82a678cb7ec82

Sema: rewrite `monomorphed_funcs` usage

In an effort to delete `Value.hashUncoerced`, generic instantiation has been redesigned. Instead of just storing instantiations in `monomorphed_funcs`, partially instantiated generic argument types are also cached. This isn't quite the single `getOrPut` that it used to be, but one `get` per generic argument plus one get for the instantiation, with an equal number of `put`s per unique instantiation isn't bad.

3 files changed, 126 insertions(+), 224 deletions(-)

src/Module.zig+27-13
......@@ -99,6 +99,7 @@ tmp_hack_arena: std.heap.ArenaAllocator,
9999/// This is currently only used for string literals.
100100memoized_decls: std.AutoHashMapUnmanaged(InternPool.Index, Decl.Index) = .{},
101101
102monomorphed_func_keys: std.ArrayListUnmanaged(InternPool.Index) = .{},
102103/// The set of all the generic function instantiations. This is used so that when a generic
103104/// function is called twice with the same comptime parameter arguments, both calls dispatch
104105/// to the same function.
......@@ -202,24 +203,40 @@ pub const CImportError = struct {
202203 }
203204};
204205
205const MonomorphedFuncsSet = std.HashMapUnmanaged(
206 Fn.Index,
207 void,
206pub const MonomorphedFuncKey = struct { func: Fn.Index, args_index: u32, args_len: u32 };
207
208pub const MonomorphedFuncAdaptedKey = struct { func: Fn.Index, args: []const InternPool.Index };
209
210pub const MonomorphedFuncsSet = std.HashMapUnmanaged(
211 MonomorphedFuncKey,
212 InternPool.Index,
208213 MonomorphedFuncsContext,
209214 std.hash_map.default_max_load_percentage,
210215);
211216
212const MonomorphedFuncsContext = struct {
217pub const MonomorphedFuncsContext = struct {
218 mod: *Module,
219
220 pub fn eql(_: @This(), a: MonomorphedFuncKey, b: MonomorphedFuncKey) bool {
221 return std.meta.eql(a, b);
222 }
223
224 pub fn hash(ctx: @This(), key: MonomorphedFuncKey) u64 {
225 const key_args = ctx.mod.monomorphed_func_keys.items[key.args_index..][0..key.args_len];
226 return std.hash.Wyhash.hash(@enumToInt(key.func), std.mem.sliceAsBytes(key_args));
227 }
228};
229
230pub const MonomorphedFuncsAdaptedContext = struct {
213231 mod: *Module,
214232
215 pub fn eql(ctx: @This(), a: Fn.Index, b: Fn.Index) bool {
216 _ = ctx;
217 return a == b;
233 pub fn eql(ctx: @This(), adapted_key: MonomorphedFuncAdaptedKey, other_key: MonomorphedFuncKey) bool {
234 const other_key_args = ctx.mod.monomorphed_func_keys.items[other_key.args_index..][0..other_key.args_len];
235 return adapted_key.func == other_key.func and std.mem.eql(InternPool.Index, adapted_key.args, other_key_args);
218236 }
219237
220 /// Must match `Sema.GenericCallAdapter.hash`.
221 pub fn hash(ctx: @This(), key: Fn.Index) u64 {
222 return ctx.mod.funcPtr(key).hash;
238 pub fn hash(_: @This(), adapted_key: MonomorphedFuncAdaptedKey) u64 {
239 return std.hash.Wyhash.hash(@enumToInt(adapted_key.func), std.mem.sliceAsBytes(adapted_key.args));
223240 }
224241};
225242
......@@ -571,9 +588,6 @@ pub const Decl = struct {
571588 pub fn clearValues(decl: *Decl, mod: *Module) void {
572589 if (decl.getOwnedFunctionIndex(mod).unwrap()) |func| {
573590 _ = mod.align_stack_fns.remove(func);
574 if (mod.funcPtr(func).comptime_args != null) {
575 _ = mod.monomorphed_funcs.removeContext(func, .{ .mod = mod });
576 }
577591 mod.destroyFunc(func);
578592 }
579593 }
src/Sema.zig+99-140
......@@ -6679,78 +6679,6 @@ fn callBuiltin(
66796679 _ = try sema.analyzeCall(block, builtin_fn, func_ty, sema.src, sema.src, modifier, false, args, null, null);
66806680}
66816681
6682const GenericCallAdapter = struct {
6683 generic_fn: *Module.Fn,
6684 precomputed_hash: u64,
6685 func_ty_info: InternPool.Key.FuncType,
6686 args: []const Arg,
6687 module: *Module,
6688
6689 const Arg = struct {
6690 ty: Type,
6691 val: Value,
6692 is_anytype: bool,
6693 };
6694
6695 pub fn eql(ctx: @This(), adapted_key: void, other_key: Module.Fn.Index) bool {
6696 _ = adapted_key;
6697 const other_func = ctx.module.funcPtr(other_key);
6698
6699 // Checking for equality may happen on an item that has been inserted
6700 // into the map but is not yet fully initialized. In such case, the
6701 // two initialized fields are `hash` and `generic_owner_decl`.
6702 if (ctx.generic_fn.owner_decl != other_func.generic_owner_decl.unwrap().?) return false;
6703
6704 const other_comptime_args = other_func.comptime_args.?;
6705 for (other_comptime_args[0..ctx.func_ty_info.param_types.len], 0..) |other_arg, i| {
6706 const this_arg = ctx.args[i];
6707 const this_is_comptime = !this_arg.val.isGenericPoison();
6708 const other_is_comptime = !other_arg.val.isGenericPoison();
6709 const this_is_anytype = this_arg.is_anytype;
6710 const other_is_anytype = other_func.isAnytypeParam(ctx.module, @intCast(u32, i));
6711
6712 if (other_is_anytype != this_is_anytype) return false;
6713 if (other_is_comptime != this_is_comptime) return false;
6714
6715 if (this_is_anytype) {
6716 // Both are anytype parameters.
6717 if (!this_arg.ty.eql(other_arg.ty, ctx.module)) {
6718 return false;
6719 }
6720 if (this_is_comptime) {
6721 // Both are comptime and anytype parameters with matching types.
6722 if (!this_arg.val.eql(other_arg.val, other_arg.ty, ctx.module)) {
6723 return false;
6724 }
6725 }
6726 } else if (this_is_comptime) {
6727 // Both are comptime parameters but not anytype parameters.
6728 // We assert no error is possible here because any lazy values must be resolved
6729 // before inserting into the generic function hash map.
6730 const is_eql = Value.eqlAdvanced(
6731 this_arg.val,
6732 this_arg.ty,
6733 other_arg.val,
6734 other_arg.ty,
6735 ctx.module,
6736 null,
6737 ) catch unreachable;
6738 if (!is_eql) {
6739 return false;
6740 }
6741 }
6742 }
6743 return true;
6744 }
6745
6746 /// The implementation of the hash is in semantic analysis of function calls, so
6747 /// that any errors when computing the hash can be properly reported.
6748 pub fn hash(ctx: @This(), adapted_key: void) u64 {
6749 _ = adapted_key;
6750 return ctx.precomputed_hash;
6751 }
6752};
6753
67546682fn analyzeCall(
67556683 sema: *Sema,
67566684 block: *Block,
......@@ -7480,11 +7408,12 @@ fn instantiateGenericCall(
74807408 const ip = &mod.intern_pool;
74817409
74827410 const func_val = try sema.resolveConstValue(block, func_src, func, "generic function being called must be comptime-known");
7483 const module_fn = mod.funcPtr(switch (ip.indexToKey(func_val.toIntern())) {
7411 const module_fn_index = switch (ip.indexToKey(func_val.toIntern())) {
74847412 .func => |function| function.index,
74857413 .ptr => |ptr| mod.declPtr(ptr.addr.decl).val.getFunctionIndex(mod).unwrap().?,
74867414 else => unreachable,
7487 });
7415 };
7416 const module_fn = mod.funcPtr(module_fn_index);
74887417 // Check the Module's generic function map with an adapted context, so that we
74897418 // can match against `uncasted_args` rather than doing the work below to create a
74907419 // generic Scope only to junk it if it matches an existing instantiation.
......@@ -7495,32 +7424,24 @@ fn instantiateGenericCall(
74957424 const fn_info = fn_zir.getFnInfo(module_fn.zir_body_inst);
74967425 const zir_tags = fn_zir.instructions.items(.tag);
74977426
7498 // This hash must match `Module.MonomorphedFuncsContext.hash`.
7499 // For parameters explicitly marked comptime and simple parameter type expressions,
7500 // we know whether a parameter is elided from a monomorphed function, and can
7501 // use it in the hash here. However, for parameter type expressions that are not
7502 // explicitly marked comptime and rely on previous parameter comptime values, we
7503 // don't find out until after generating a monomorphed function whether the parameter
7504 // type ended up being a "must-be-comptime-known" type.
7505 var hasher = std.hash.Wyhash.init(0);
7506 std.hash.autoHash(&hasher, module_fn.owner_decl);
7507
7508 const generic_args = try sema.arena.alloc(GenericCallAdapter.Arg, func_ty_info.param_types.len);
7509 {
7510 var i: usize = 0;
7427 const generic_args = try sema.arena.alloc(InternPool.Index, func_ty_info.param_types.len);
7428 const callee_index = callee: {
7429 var arg_i: usize = 0;
7430 var generic_arg_i: u32 = 0;
7431 var known_unique = false;
75117432 for (fn_info.param_body) |inst| {
75127433 var is_comptime = false;
75137434 var is_anytype = false;
75147435 switch (zir_tags[inst]) {
75157436 .param => {
7516 is_comptime = func_ty_info.paramIsComptime(@intCast(u5, i));
7437 is_comptime = func_ty_info.paramIsComptime(@intCast(u5, arg_i));
75177438 },
75187439 .param_comptime => {
75197440 is_comptime = true;
75207441 },
75217442 .param_anytype => {
75227443 is_anytype = true;
7523 is_comptime = func_ty_info.paramIsComptime(@intCast(u5, i));
7444 is_comptime = func_ty_info.paramIsComptime(@intCast(u5, arg_i));
75247445 },
75257446 .param_anytype_comptime => {
75267447 is_anytype = true;
......@@ -7529,7 +7450,15 @@ fn instantiateGenericCall(
75297450 else => continue,
75307451 }
75317452
7532 const arg_ty = sema.typeOf(uncasted_args[i]);
7453 defer arg_i += 1;
7454 if (known_unique) {
7455 if (is_comptime or is_anytype) {
7456 generic_arg_i += 1;
7457 }
7458 continue;
7459 }
7460
7461 const arg_ty = sema.typeOf(uncasted_args[arg_i]);
75337462 if (is_comptime or is_anytype) {
75347463 // Tuple default values are a part of the type and need to be
75357464 // resolved to hash the type.
......@@ -7537,69 +7466,72 @@ fn instantiateGenericCall(
75377466 }
75387467
75397468 if (is_comptime) {
7540 const arg_val = sema.analyzeGenericCallArgVal(block, .unneeded, uncasted_args[i]) catch |err| switch (err) {
7469 const arg_val = sema.analyzeGenericCallArgVal(block, .unneeded, uncasted_args[arg_i]) catch |err| switch (err) {
75417470 error.NeededSourceLocation => {
75427471 const decl = sema.mod.declPtr(block.src_decl);
7543 const arg_src = mod.argSrc(call_src.node_offset.x, decl, i, bound_arg_src);
7544 _ = try sema.analyzeGenericCallArgVal(block, arg_src, uncasted_args[i]);
7472 const arg_src = mod.argSrc(call_src.node_offset.x, decl, arg_i, bound_arg_src);
7473 _ = try sema.analyzeGenericCallArgVal(block, arg_src, uncasted_args[arg_i]);
75457474 unreachable;
75467475 },
75477476 else => |e| return e,
75487477 };
7549 arg_val.hashUncoerced(arg_ty, &hasher, mod);
7478
75507479 if (is_anytype) {
7551 std.hash.autoHash(&hasher, arg_ty.toIntern());
7552 generic_args[i] = .{
7553 .ty = arg_ty,
7554 .val = arg_val,
7555 .is_anytype = true,
7556 };
7480 generic_args[generic_arg_i] = arg_val.toIntern();
75577481 } else {
7558 generic_args[i] = .{
7559 .ty = arg_ty,
7560 .val = arg_val,
7561 .is_anytype = false,
7482 const final_arg_ty = mod.monomorphed_funcs.getAdapted(
7483 Module.MonomorphedFuncAdaptedKey{
7484 .func = module_fn_index,
7485 .args = generic_args[0..generic_arg_i],
7486 },
7487 Module.MonomorphedFuncsAdaptedContext{ .mod = mod },
7488 ) orelse {
7489 known_unique = true;
7490 generic_arg_i += 1;
7491 continue;
7492 };
7493 const casted_arg = sema.coerce(block, final_arg_ty.toType(), uncasted_args[arg_i], .unneeded) catch |err| switch (err) {
7494 error.NeededSourceLocation => {
7495 const decl = sema.mod.declPtr(block.src_decl);
7496 const arg_src = mod.argSrc(call_src.node_offset.x, decl, arg_i, bound_arg_src);
7497 _ = try sema.coerce(block, final_arg_ty.toType(), uncasted_args[arg_i], arg_src);
7498 unreachable;
7499 },
7500 else => |e| return e,
75627501 };
7502 const casted_arg_val = sema.analyzeGenericCallArgVal(block, .unneeded, casted_arg) catch |err| switch (err) {
7503 error.NeededSourceLocation => {
7504 const decl = sema.mod.declPtr(block.src_decl);
7505 const arg_src = mod.argSrc(call_src.node_offset.x, decl, arg_i, bound_arg_src);
7506 _ = try sema.analyzeGenericCallArgVal(block, arg_src, casted_arg);
7507 unreachable;
7508 },
7509 else => |e| return e,
7510 };
7511 generic_args[generic_arg_i] = casted_arg_val.toIntern();
75637512 }
7513 generic_arg_i += 1;
75647514 } else if (is_anytype) {
7565 std.hash.autoHash(&hasher, arg_ty.toIntern());
7566 generic_args[i] = .{
7567 .ty = arg_ty,
7568 .val = Value.generic_poison,
7569 .is_anytype = true,
7570 };
7571 } else {
7572 generic_args[i] = .{
7573 .ty = arg_ty,
7574 .val = Value.generic_poison,
7575 .is_anytype = false,
7576 };
7515 generic_args[generic_arg_i] = arg_ty.toIntern();
7516 generic_arg_i += 1;
75777517 }
7578
7579 i += 1;
75807518 }
7581 }
75827519
7583 const precomputed_hash = hasher.final();
7520 if (!known_unique) {
7521 if (mod.monomorphed_funcs.getAdapted(
7522 Module.MonomorphedFuncAdaptedKey{
7523 .func = module_fn_index,
7524 .args = generic_args[0..generic_arg_i],
7525 },
7526 Module.MonomorphedFuncsAdaptedContext{ .mod = mod },
7527 )) |callee_func| break :callee mod.intern_pool.indexToKey(callee_func).func.index;
7528 }
75847529
7585 const adapter: GenericCallAdapter = .{
7586 .generic_fn = module_fn,
7587 .precomputed_hash = precomputed_hash,
7588 .func_ty_info = func_ty_info,
7589 .args = generic_args,
7590 .module = mod,
7591 };
7592 const gop = try mod.monomorphed_funcs.getOrPutContextAdapted(gpa, {}, adapter, .{ .mod = mod });
7593 const callee_index = if (!gop.found_existing) callee: {
75947530 const new_module_func_index = try mod.createFunc(undefined);
75957531 const new_module_func = mod.funcPtr(new_module_func_index);
75967532
7597 // This ensures that we can operate on the hash map before the Module.Fn
7598 // struct is fully initialized.
7599 new_module_func.hash = precomputed_hash;
76007533 new_module_func.generic_owner_decl = module_fn.owner_decl.toOptional();
76017534 new_module_func.comptime_args = null;
7602 gop.key_ptr.* = new_module_func_index;
76037535
76047536 try namespace.anon_decls.ensureUnusedCapacity(gpa, 1);
76057537
......@@ -7641,7 +7573,8 @@ fn instantiateGenericCall(
76417573 new_decl,
76427574 new_decl_index,
76437575 uncasted_args,
7644 module_fn,
7576 generic_arg_i,
7577 module_fn_index,
76457578 new_module_func_index,
76467579 namespace_index,
76477580 func_ty_info,
......@@ -7657,12 +7590,10 @@ fn instantiateGenericCall(
76577590 }
76587591 assert(namespace.anon_decls.orderedRemove(new_decl_index));
76597592 mod.destroyDecl(new_decl_index);
7660 assert(mod.monomorphed_funcs.removeContext(new_module_func_index, .{ .mod = mod }));
76617593 mod.destroyFunc(new_module_func_index);
76627594 return err;
76637595 },
76647596 else => {
7665 assert(mod.monomorphed_funcs.removeContext(new_module_func_index, .{ .mod = mod }));
76667597 // TODO look up the compile error that happened here and attach a note to it
76677598 // pointing here, at the generic instantiation callsite.
76687599 if (sema.owner_func) |owner_func| {
......@@ -7675,9 +7606,8 @@ fn instantiateGenericCall(
76757606 };
76767607
76777608 break :callee new_func;
7678 } else gop.key_ptr.*;
7609 };
76797610 const callee = mod.funcPtr(callee_index);
7680
76817611 callee.branch_quota = @max(callee.branch_quota, sema.branch_quota);
76827612
76837613 const callee_inst = try sema.analyzeDeclVal(block, func_src, callee.owner_decl);
......@@ -7752,7 +7682,7 @@ fn instantiateGenericCall(
77527682 if (call_tag == .call_always_tail) {
77537683 return sema.handleTailCall(block, call_src, func_ty, result);
77547684 }
7755 if (new_fn_info.return_type == .noreturn_type) {
7685 if (func_ty.fnReturnType(mod).isNoReturn(mod)) {
77567686 _ = try block.addNoOp(.unreach);
77577687 return Air.Inst.Ref.unreachable_value;
77587688 }
......@@ -7766,7 +7696,8 @@ fn resolveGenericInstantiationType(
77667696 new_decl: *Decl,
77677697 new_decl_index: Decl.Index,
77687698 uncasted_args: []const Air.Inst.Ref,
7769 module_fn: *Module.Fn,
7699 generic_args_len: u32,
7700 module_fn_index: Module.Fn.Index,
77707701 new_module_func: Module.Fn.Index,
77717702 namespace: Namespace.Index,
77727703 func_ty_info: InternPool.Key.FuncType,
......@@ -7777,6 +7708,7 @@ fn resolveGenericInstantiationType(
77777708 const gpa = sema.gpa;
77787709
77797710 const zir_tags = fn_zir.instructions.items(.tag);
7711 const module_fn = mod.funcPtr(module_fn_index);
77807712 const fn_info = fn_zir.getFnInfo(module_fn.zir_body_inst);
77817713
77827714 // Re-run the block that creates the function, with the comptime parameters
......@@ -7893,9 +7825,15 @@ fn resolveGenericInstantiationType(
78937825 const new_func = new_func_val.getFunctionIndex(mod).unwrap().?;
78947826 assert(new_func == new_module_func);
78957827
7828 const generic_args_index = @intCast(u32, mod.monomorphed_func_keys.items.len);
7829 const generic_args = try mod.monomorphed_func_keys.addManyAsSlice(gpa, generic_args_len);
7830 var generic_arg_i: u32 = 0;
7831 try mod.monomorphed_funcs.ensureUnusedCapacityContext(gpa, generic_args_len + 1, .{ .mod = mod });
7832
78967833 arg_i = 0;
78977834 for (fn_info.param_body) |inst| {
78987835 var is_comptime = false;
7836 var is_anytype = false;
78997837 switch (zir_tags[inst]) {
79007838 .param => {
79017839 is_comptime = func_ty_info.paramIsComptime(@intCast(u5, arg_i));
......@@ -7904,9 +7842,11 @@ fn resolveGenericInstantiationType(
79047842 is_comptime = true;
79057843 },
79067844 .param_anytype => {
7845 is_anytype = true;
79077846 is_comptime = func_ty_info.paramIsComptime(@intCast(u5, arg_i));
79087847 },
79097848 .param_anytype_comptime => {
7849 is_anytype = true;
79107850 is_comptime = true;
79117851 },
79127852 else => continue,
......@@ -7924,11 +7864,24 @@ fn resolveGenericInstantiationType(
79247864
79257865 if (is_comptime) {
79267866 const arg_val = (child_sema.resolveMaybeUndefValAllowVariables(arg) catch unreachable).?;
7867 if (!is_anytype) {
7868 if (mod.monomorphed_funcs.fetchPutAssumeCapacityContext(.{
7869 .func = module_fn_index,
7870 .args_index = generic_args_index,
7871 .args_len = generic_arg_i,
7872 }, arg_ty.toIntern(), .{ .mod = mod })) |kv| assert(kv.value == arg_ty.toIntern());
7873 }
7874 generic_args[generic_arg_i] = arg_val.toIntern();
7875 generic_arg_i += 1;
79277876 child_sema.comptime_args[arg_i] = .{
79287877 .ty = arg_ty,
79297878 .val = (try arg_val.intern(arg_ty, mod)).toValue(),
79307879 };
79317880 } else {
7881 if (is_anytype) {
7882 generic_args[generic_arg_i] = arg_ty.toIntern();
7883 generic_arg_i += 1;
7884 }
79327885 child_sema.comptime_args[arg_i] = .{
79337886 .ty = arg_ty,
79347887 .val = Value.generic_poison,
......@@ -7963,6 +7916,12 @@ fn resolveGenericInstantiationType(
79637916 new_decl.owns_tv = true;
79647917 new_decl.analysis = .complete;
79657918
7919 mod.monomorphed_funcs.putAssumeCapacityNoClobberContext(.{
7920 .func = module_fn_index,
7921 .args_index = generic_args_index,
7922 .args_len = generic_arg_i,
7923 }, new_decl.val.toIntern(), .{ .mod = mod });
7924
79667925 // Queue up a `codegen_func` work item for the new Fn. The `comptime_args` field
79677926 // will be populated, ensuring it will have `analyzeBody` called with the ZIR
79687927 // parameters mapped appropriately.
src/value.zig-71
......@@ -1691,77 +1691,6 @@ pub const Value = struct {
16911691 return (try orderAdvanced(a, b, mod, opt_sema)).compare(.eq);
16921692 }
16931693
1694 /// This is a more conservative hash function that produces equal hashes for values
1695 /// that can coerce into each other.
1696 /// This function is used by hash maps and so treats floating-point NaNs as equal
1697 /// to each other, and not equal to other floating-point values.
1698 pub fn hashUncoerced(val: Value, ty: Type, hasher: *std.hash.Wyhash, mod: *Module) void {
1699 if (val.isUndef(mod)) return;
1700 // The value is runtime-known and shouldn't affect the hash.
1701 if (val.isRuntimeValue(mod)) return;
1702
1703 if (val.ip_index != .none) {
1704 // The InternPool data structure hashes based on Key to make interned objects
1705 // unique. An Index can be treated simply as u32 value for the
1706 // purpose of Type/Value hashing and equality.
1707 std.hash.autoHash(hasher, val.toIntern());
1708 return;
1709 }
1710
1711 switch (ty.zigTypeTag(mod)) {
1712 .Opaque => unreachable, // Cannot hash opaque types
1713 .Void,
1714 .NoReturn,
1715 .Undefined,
1716 .Null,
1717 .Struct, // It sure would be nice to do something clever with structs.
1718 => |zig_type_tag| std.hash.autoHash(hasher, zig_type_tag),
1719 .Pointer => {
1720 assert(ty.isSlice(mod));
1721 const slice = val.castTag(.slice).?.data;
1722 const ptr_ty = ty.slicePtrFieldType(mod);
1723 slice.ptr.hashUncoerced(ptr_ty, hasher, mod);
1724 },
1725 .Type,
1726 .Float,
1727 .ComptimeFloat,
1728 .Bool,
1729 .Int,
1730 .ComptimeInt,
1731 .Fn,
1732 .Optional,
1733 .ErrorSet,
1734 .ErrorUnion,
1735 .Enum,
1736 .EnumLiteral,
1737 => unreachable, // handled above with the ip_index check
1738 .Array, .Vector => {
1739 const len = ty.arrayLen(mod);
1740 const elem_ty = ty.childType(mod);
1741 var index: usize = 0;
1742 while (index < len) : (index += 1) {
1743 const elem_val = val.elemValue(mod, index) catch |err| switch (err) {
1744 // Will be solved when arrays and vectors get migrated to the intern pool.
1745 error.OutOfMemory => @panic("OOM"),
1746 };
1747 elem_val.hashUncoerced(elem_ty, hasher, mod);
1748 }
1749 },
1750 .Union => {
1751 hasher.update(val.tagName(mod));
1752 switch (mod.intern_pool.indexToKey(val.toIntern())) {
1753 .un => |un| {
1754 const active_field_ty = ty.unionFieldType(un.tag.toValue(), mod);
1755 un.val.toValue().hashUncoerced(active_field_ty, hasher, mod);
1756 },
1757 else => std.hash.autoHash(hasher, std.builtin.TypeId.Void),
1758 }
1759 },
1760 .Frame => @panic("TODO implement hashing frame values"),
1761 .AnyFrame => @panic("TODO implement hashing anyframe values"),
1762 }
1763 }
1764
17651694 pub fn isComptimeMutablePtr(val: Value, mod: *Module) bool {
17661695 return switch (mod.intern_pool.indexToKey(val.toIntern())) {
17671696 .ptr => |ptr| switch (ptr.addr) {