sema: hash type keys as words

TypeTable's key (tag, a, b) has padding, so the automatic context hashed
it field by field through Wyhash's streaming interface on every vector,
matrix and array type looked up. KeyContext mixes its words.

Glide's shaders in the hill-climbing set: -1.1% (A/B); output unchanged
on the verify corpus.
This commit is contained in:
peterino2 2026-11-03 10:11:09 -08:00
parent 0634987f5e
commit 4a12f2b89a
1 changed files with 18 additions and 1 deletions

View File

@ -535,7 +535,7 @@ pub const LinAlgMatrix = struct { comp: u32, m: u32, n: u32, use: u32, scope: u3
pub const TypeTable = struct {
gpa: Allocator,
types: std.ArrayList(Type) = .empty,
map: std.AutoHashMapUnmanaged(Key, TypeId) = .empty,
map: std.HashMapUnmanaged(Key, TypeId, KeyContext, std.hash_map.default_max_load_percentage) = .empty,
/// scalar's results.
scalars: [@typeInfo(Scalar).@"enum".field_names.len]TypeId = @splat(std.math.maxInt(TypeId)),
@ -545,6 +545,23 @@ pub const TypeTable = struct {
/// Hashable identity of structural types.
const Key = struct { tag: u8, a: u64, b: u64 };
/// Hashes a Key's words (it has padding, which the automatic context
/// hashes around field by field).
const KeyContext = struct {
fn mix(x: u64) u64 {
var z = x;
z = (z ^ (z >> 30)) *% 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) *% 0x94d049bb133111eb;
return z ^ (z >> 31);
}
pub fn hash(_: KeyContext, k: Key) u64 {
return mix(k.a ^ mix(k.b ^ @as(u64, k.tag) << 56));
}
pub fn eql(_: KeyContext, x: Key, y: Key) bool {
return x.tag == y.tag and x.a == y.a and x.b == y.b;
}
};
pub fn init(gpa: Allocator) !TypeTable {
var t: TypeTable = .{ .gpa = gpa };
try t.types.append(gpa, .@"error");