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:
parent
0634987f5e
commit
4a12f2b89a
|
|
@ -535,7 +535,7 @@ pub const LinAlgMatrix = struct { comp: u32, m: u32, n: u32, use: u32, scope: u3
|
||||||
pub const TypeTable = struct {
|
pub const TypeTable = struct {
|
||||||
gpa: Allocator,
|
gpa: Allocator,
|
||||||
types: std.ArrayList(Type) = .empty,
|
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.
|
/// scalar's results.
|
||||||
scalars: [@typeInfo(Scalar).@"enum".field_names.len]TypeId = @splat(std.math.maxInt(TypeId)),
|
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.
|
/// Hashable identity of structural types.
|
||||||
const Key = struct { tag: u8, a: u64, b: u64 };
|
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 {
|
pub fn init(gpa: Allocator) !TypeTable {
|
||||||
var t: TypeTable = .{ .gpa = gpa };
|
var t: TypeTable = .{ .gpa = gpa };
|
||||||
try t.types.append(gpa, .@"error");
|
try t.types.append(gpa, .@"error");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue