From 8513eb80f4ee73f19bc90075cadc65ab9e00d9c2 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 4 Dec 2026 14:37:57 +0000 Subject: [PATCH] a64/jit: run SCVTF/UCVTF and FCVTZS/FCVTZU with the integer in a V register inline The scalar AdvSIMD forms (scvtf d0, d0; fcvtzs d0, d0) took the generic interpreter call from compiled code: 1.17M calls per run of a Perl kernel. They now share the W/X forms' fast paths (perl-k6 about 6% faster, 2026-12-04). --- .agents/a64/jit.md | 3 ++- src/a64/jit/emit.zig | 11 +++++++++++ src/a64/jit/generic.zig | 34 +++++++++++++++++++++++++--------- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/.agents/a64/jit.md b/.agents/a64/jit.md index 00e65b9..56bd699 100644 --- a/.agents/a64/jit.md +++ b/.agents/a64/jit.md @@ -274,7 +274,8 @@ memset are the main users. **Scalar FP fast paths** (`fpFast`, `fpToInt`): FADD, FSUB, FMUL, FDIV, FSQRT, FMAX, FMIN, FMAXNM, FMINNM, FMADD, FMSUB, FNMADD, FNMSUB (S, D), SCVTF, UCVTF -(W/X to S/D), FCVTZS, FCVTZU (S/D to W/X), FCVTAS/FCVTAU (S to W), FCVT (S to D, D to S) inline, +(W/X, or S/D as the scalar AdvSIMD form, to S/D), FCVTZS, FCVTZU (S/D to +W/X, or to S/D), FCVTAS/FCVTAU (S to W), FCVT (S to D, D to S) inline, bit-exact with fparith.zig, with the interpreter call on the rest; FMOV, FABS, FNEG (register), FMOV (immediate) and FCSEL need no slow path. The arithmetic fast path needs FPCR.RMode = nearest. Contiguous scalar FCVT diff --git a/src/a64/jit/emit.zig b/src/a64/jit/emit.zig index 6580ed7..57a9aa5 100644 --- a/src/a64/jit/emit.zig +++ b/src/a64/jit/emit.zig @@ -677,6 +677,17 @@ fn genFp(r: std.Random, out: []u32, pc: u64) usize { return 2; }, 15 => out[0] = 0x1e22_0000 | (r.uintLessThan(u32, 2) << 31) | ftype | (r.uintLessThan(u32, 2) << 16) | ((if (r.boolean()) x9 else kt.anyReg(r)) << 5) | vd, // scvtf, ucvtf + // The scalar AdvSIMD forms (the integer in a V register). + 16 => { + // half the time an integer from x9 (case 3's edge values) first + if (r.boolean()) { + out[0] = (if (ftype != 0) @as(u32, 0x9e67_0000) else 0x1e27_0000) | (x9 << 5) | vn; // fmov dN, x9 / sN, w9 + out[1] = 0x5e21_d800 | (r.uintLessThan(u32, 2) << 29) | ftype | (vn << 5) | vd; // scvtf, ucvtf + return 2; + } + out[0] = 0x5e21_d800 | (r.uintLessThan(u32, 2) << 29) | ftype | (vn << 5) | vd; + }, + 17 => out[0] = 0x5ea1_b800 | (r.uintLessThan(u32, 2) << 29) | ftype | (vn << 5) | vd, // fcvtzs, fcvtzu (scalar AdvSIMD) 18 => out[0] = 0x1e20_0400 | ftype | (vm << 16) | (r.uintLessThan(u32, 16) << 12) | (vn << 5) | r.uintLessThan(u32, 32), // fccmp/e 19 => out[0] = 0x1e24_0000 | (r.uintLessThan(u32, 2) << 16) | (vn << 5) | kt.anyReg(r), // fcvtas/u Wd, Sn else => out[0] = 0x1e38_0000 | (r.uintLessThan(u32, 2) << 31) | ftype | (r.uintLessThan(u32, 2) << 16) | (vn << 5) | (if (r.uintLessThan(u32, 8) == 0) 31 else kt.dst(r)), // fcvtzs, fcvtzu diff --git a/src/a64/jit/generic.zig b/src/a64/jit/generic.zig index 7bd35a5..025351a 100644 --- a/src/a64/jit/generic.zig +++ b/src/a64/jit/generic.zig @@ -73,10 +73,12 @@ pub const Kind = enum(u8) { fminmax, /// Sd/Dd = ±Sa/Da ± Sn/Dn * Sm/Dm (FMADD, FMSUB, FNMADD, FNMSUB: op = (o1<<1)|o0; h: double) fmadd, - /// Sd/Dd = Wn/Xn (SCVTF, UCVTF (integer): op 1 unsigned; q: Xn) + /// Sd/Dd = Wn/Xn (SCVTF, UCVTF (integer): op 1 unsigned; q: Xn); with + /// `vr` the integer is Sn/Dn (the scalar AdvSIMD form, q = h) cvtf, /// Wd/Xd = Sn/Dn rounded towards zero (FCVTZS, FCVTZU (integer): op 1 - /// unsigned; q: Xd) + /// unsigned; q: Xd); with `vr` the result goes to Sd/Dd (the scalar + /// AdvSIMD form, q = h) cvtz, /// FCVTAS/FCVTAU Wd, Sn (nearest with ties away). cvta, @@ -98,6 +100,8 @@ pub const Fast = struct { q: bool = false, /// fcmp: compare with +0.0 zero: bool = false, + /// cvtf, cvtz: the integer side is a V register (the scalar AdvSIMD form) + vr: bool = false, /// logic: 0 AND, 1 BIC, 2 ORR, 3 ORN, 4 EOR op: u8 = 0, mask: u64 = 0, @@ -175,6 +179,18 @@ pub fn decode(raw: u32) ?Fast { else => return null, } } + // SCVTF/UCVTF (scalar, integer) and FCVTZS/FCVTZU (scalar, integer): + // 01 U 11110 0 sz 10000 11101 10 / 01 U 11110 1 sz 10000 11011 10, the + // integer as wide as the float, in Vn / Vd. + { + const u: u8 = @intCast(bits(raw, 29, 29)); + const sz: u8 = @intCast(bits(raw, 22, 22)); + switch (raw & 0xdfbf_fc00) { + 0x5e21_d800 => return .{ .kind = .cvtf, .d = d, .n = n, .h = sz, .q = sz == 1, .op = u, .vr = true, .raw = raw }, + 0x5ea1_b800 => return .{ .kind = .cvtz, .d = d, .n = n, .h = sz, .q = sz == 1, .op = u, .vr = true, .raw = raw }, + else => {}, + } + } // FCMP, FCMPE (single, double; register or #0.0) if (raw & 0xffa0_fc07 == 0x1e20_2000) { return .{ .kind = .fcmp, .n = n, .m = m, .h = @intCast(bits(raw, 22, 22)), .zero = raw & 8 != 0 }; @@ -268,8 +284,8 @@ pub fn usage(f: Fast) Use { .fsqrt, .fmov1, .fcvt => cm.rd(vlo(f.n)).with(cm.wr(vlo(f.d))), .fimm => cm.wr(vlo(f.d)), .fcsel => cm.rd(vlo(f.n)).with(cm.rd(vlo(f.m))).with(cm.wr(vlo(f.d))).with(.{ .flags_read = true }), - .cvtf => cm.rd(xs(f.n)).with(cm.wr(vlo(f.d))), - .cvtz, .cvta => cm.rd(vlo(f.n)).with(cm.wr(xs(f.d))), + .cvtf => cm.rd(if (f.vr) vlo(f.n) else xs(f.n)).with(cm.wr(vlo(f.d))), + .cvtz, .cvta => cm.rd(vlo(f.n)).with(cm.wr(if (f.vr) vlo(f.d) else xs(f.d))), }; } @@ -797,7 +813,7 @@ fn fpSlow(e: *Emit, f: Fast, at: At) void { return; } const reads: [3]u8 = switch (f.kind) { - .cvtf => .{ xs(f.n), ops.zr, ops.zr }, + .cvtf => .{ if (f.vr) vlo(f.n) else xs(f.n), ops.zr, ops.zr }, .farith, .fminmax => .{ vlo(f.n), vlo(f.m), ops.zr }, .fmadd => .{ vlo(f.n), vlo(f.m), vlo(f.a) }, else => .{ vlo(f.n), ops.zr, ops.zr }, @@ -815,7 +831,7 @@ fn fpSlow(e: *Emit, f: Fast, at: At) void { e.k64(at.pc); e.callTable(@intFromPtr(&opexec.jitGeneric)); e.op(W.drop); - if (f.kind != .cvtz and f.kind != .cvta) return cm.loadV(e, f.d); + if ((f.kind != .cvtz and f.kind != .cvta) or f.vr) return cm.loadV(e, f.d); if (xs(f.d) == ops.zr) return; e.get(jc.l_x); e.addr(); @@ -1130,8 +1146,8 @@ fn fpFast(e: *Emit, f: Fast, at: At) void { inRange(e, t0, dbl); }, .cvtf => { - // The integer, sign- or zero-extended from W. - cm.get(e, xs(f.n)); + // The integer, sign- or zero-extended from W (or from Sn). + cm.get(e, if (f.vr) vlo(f.n) else xs(f.n)); if (!f.q) { e.k64(32); e.op(W.i64_shl); @@ -1523,7 +1539,7 @@ fn fpToInt(e: *Emit, f: Fast, at: At) void { e.set(t1); e.close(); e.get(t0); - cm.set(e, xs(f.d)); + if (f.vr) cm.setScalar(e, vlo(f.d)) else cm.set(e, xs(f.d)); orFpsr(e, t1); e.op(W.@"else"); fpSlow(e, f, at);