sema: resolve each intrinsic call signature once per compile
Resolving an intrinsic call matches every overload, comparing class names as strings, and the same calls (mul, dot, saturate, ...) recur with the same argument types. Resolutions are memoized by name, object type, explicit type and the arguments' types and lvalues (not those that reported a diagnostic). The hill-climbing set: sema -5.7%, its allocation calls -25%; output unchanged on the verify corpus.
This commit is contained in:
parent
5019d2b838
commit
e05a217cc1
|
|
@ -112,6 +112,37 @@ pub const Db = struct {
|
||||||
pub const Resolved = struct { ret: TypeId, index: u16 = 0, params: []const TypeId = &.{}, ok: bool = false };
|
pub const Resolved = struct { ret: TypeId, index: u16 = 0, params: []const TypeId = &.{}, ok: bool = false };
|
||||||
|
|
||||||
pub fn resolve(db: *const Db, sema: anytype, loc: source.Loc, ns: []const u8, name: []const u8, object: ?TypeId, args: []const ArgInfo, explicit: ?TypeId) !Resolved {
|
pub fn resolve(db: *const Db, sema: anytype, loc: source.Loc, ns: []const u8, name: []const u8, object: ?TypeId, args: []const ArgInfo, explicit: ?TypeId) !Resolved {
|
||||||
|
// The same call (name, object, explicit type, argument types and
|
||||||
|
// lvalues) resolves the same way: matched once per compile.
|
||||||
|
var key_buf: [256]u8 = undefined;
|
||||||
|
var key: std.ArrayList(u8) = .initBuffer(&key_buf);
|
||||||
|
const keyed = memoKey(&key, ns, name, object, args, explicit);
|
||||||
|
if (keyed) if (sema.intrinsic_memo.get(key.items)) |r| return r;
|
||||||
|
const reported = sema.diags.list.items.len;
|
||||||
|
const r = try db.resolveUncached(sema, loc, ns, name, object, args, explicit);
|
||||||
|
// (Not one that reported something: a repeat reports it again.)
|
||||||
|
if (keyed and r.ok and sema.diags.list.items.len == reported) try sema.intrinsic_memo.put(sema.arena, try sema.arena.dupe(u8, key.items), r);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The memo key of a call; false if it does not fit.
|
||||||
|
fn memoKey(key: *std.ArrayList(u8), ns: []const u8, name: []const u8, object: ?TypeId, args: []const ArgInfo, explicit: ?TypeId) bool {
|
||||||
|
const fixed = ns.len + name.len + 2 + 2 * @sizeOf(TypeId) + args.len * (@sizeOf(TypeId) + 1);
|
||||||
|
if (fixed > key.capacity) return false;
|
||||||
|
key.appendSliceAssumeCapacity(ns);
|
||||||
|
key.appendAssumeCapacity(0);
|
||||||
|
key.appendSliceAssumeCapacity(name);
|
||||||
|
key.appendAssumeCapacity(0);
|
||||||
|
key.appendSliceAssumeCapacity(std.mem.asBytes(&@as(TypeId, object orelse std.math.maxInt(TypeId))));
|
||||||
|
key.appendSliceAssumeCapacity(std.mem.asBytes(&@as(TypeId, explicit orelse std.math.maxInt(TypeId))));
|
||||||
|
for (args) |a| {
|
||||||
|
key.appendSliceAssumeCapacity(std.mem.asBytes(&a.ty));
|
||||||
|
key.appendAssumeCapacity(@intFromBool(a.lvalue));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolveUncached(db: *const Db, sema: anytype, loc: source.Loc, ns: []const u8, name: []const u8, object: ?TypeId, args: []const ArgInfo, explicit: ?TypeId) !Resolved {
|
||||||
const cands = try db.candidates(sema.arena, ns, name);
|
const cands = try db.candidates(sema.arena, ns, name);
|
||||||
if (cands.len == 0) {
|
if (cands.len == 0) {
|
||||||
if (object) |o| {
|
if (object) |o| {
|
||||||
|
|
|
||||||
|
|
@ -219,6 +219,8 @@ pub const Sema = struct {
|
||||||
loop_depth: u32 = 0,
|
loop_depth: u32 = 0,
|
||||||
switch_depth: u32 = 0,
|
switch_depth: u32 = 0,
|
||||||
intrinsic_db: *const intrinsics.Db,
|
intrinsic_db: *const intrinsics.Db,
|
||||||
|
/// Intrinsic calls resolved so far (see Db.resolve).
|
||||||
|
intrinsic_memo: std.StringHashMapUnmanaged(intrinsics.Db.Resolved) = .empty,
|
||||||
enum_scopes: std.AutoHashMapUnmanaged(*types.EnumInfo, *Scope) = .empty,
|
enum_scopes: std.AutoHashMapUnmanaged(*types.EnumInfo, *Scope) = .empty,
|
||||||
/// Where each template was declared (its names resolve from there).
|
/// Where each template was declared (its names resolve from there).
|
||||||
template_scopes: std.AutoHashMapUnmanaged(*const ast.Decl, *Scope) = .empty,
|
template_scopes: std.AutoHashMapUnmanaged(*const ast.Decl, *Scope) = .empty,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue