From 5d014c18f57dab32217802f94b5c2043ce4642c4 Mon Sep 17 00:00:00 2001 From: jel221 Date: Thu, 3 Dec 2026 12:03:20 -0800 Subject: [PATCH 1/2] tfu: shorten the stage 1 and stage 5 paths of the PE The PE's two deepest paths were not the polynomial: stage 1 was the ex2 and sin/cos range reduction (a subtract, compares and two barrel shifts, then a full-width negate or add), and stage 5 was the lg2 sum followed by a 41-bit add and a 40-bit negate in series. Beacon put the tfu DUT at 24 logic levels and a core with the TFU at 23, against 15 for the rest of the core. - ex2 and sin/cos share one shift of x_sig by x_e - 96, which over the exponents that matter is {x_e[7], x_e[4:0]}: no subtract, no compare. - The ex2 negation becomes ~mag; its +1 is registered as two carries and applied in stage 2 (6-bit index, 18-bit fraction, exponent). - The sin/cos exact +-1 test uses the registered ~idx/~xl in stage 2; only u[5:0] is tested in stage 1. The cos phase is two XORs. - lg2 sums k * 2^30 + p and its negation in parallel instead of negating after the sum. Results stay bit-identical to sw/common/tfu.cpp and the pipeline stays 7 stages, so SimX, cycles and perf baselines do not move. Beacon: tfu 24 -> 13 levels; 8-thread 16-warp core with TFU 23 -> 15. ASIC: Beacon 913 -> 1239 MHz, Yosys/STA 1055 -> 1252 MHz. --- CHANGELOG.md | 5 +++ hw/rtl/tfu/VX_tfu_pe.sv | 88 +++++++++++++++++++++++++++++------------ 2 files changed, 67 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a32da39540..6d30fc01c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,11 @@ follows the version pins recorded in [VERSION](VERSION) (`VORTEX_VERSION`, ### Changed +- **Shallower TFU datapath, same results and latency.** The ex2 and sin/cos range reduction share one shift whose amount is + bits of the exponent, the ex2 negation's +1 and the sin/cos ±1 test move to the stage after, and the lg2 result and its + negation are summed in parallel instead of a negate after the sum. Results stay bit-identical to `sw/common/tfu.cpp` and the + pipeline stays 7 stages. Beacon: the `tfu` DUT drops from 24 to 13 logic levels, and a 16-warp, 8-thread core with the TFU + from 23 to 15 (the TFU no longer sets its depth); the ASIC estimate of `tfu` rises from 913 to 1239 MHz with 7% fewer cells. - **Machine-mode trap CSRs.** `mtvec` is one per core, held by the core scheduler and shared by every warp; `mcause` holds the 4-bit exception codes the core raises; `mtval` reads as zero. `mstatus` (its `MIE`, `MPIE` and `FS` fields), `mepc` and `mcause` stay per warp. Per-warp trap state drops from 160 to at most 39 bits at XLEN 32. diff --git a/hw/rtl/tfu/VX_tfu_pe.sv b/hw/rtl/tfu/VX_tfu_pe.sv index 1e38062724..00c0a013d0 100644 --- a/hw/rtl/tfu/VX_tfu_pe.sv +++ b/hw/rtl/tfu/VX_tfu_pe.sv @@ -33,23 +33,28 @@ module VX_tfu_pe import VX_gpu_pkg::*; ( wire signed [9:0] x_k = 10'($signed({2'b00, x_e})) - 10'sd127; - wire [3:0] ex2_lsh = 4'(x_e - 8'd120); - wire [7:0] ex2_rsh = 8'd120 - x_e; - wire [37:0] ex2_mag = (x_e >= 8'd120) ? (38'(x_sig) << ex2_lsh) - : ((ex2_rsh < 8'd24) ? 38'(x_sig >> ex2_rsh[4:0]) : 38'd0); - wire [38:0] ex2_x = x_s ? -{1'b0, ex2_mag} : {1'b0, ex2_mag}; - - wire [7:0] trig_lsh = x_e - 8'd119; - wire [7:0] trig_rsh = 8'd119 - x_e; + // ex2 and sin/cos both take x_sig * 2^(x_e - 96) over x_e in [96, 159], + // where the shift amount is just {x_e[7], x_e[4:0]}: no subtract, no compare. + wire x_e_mid = (x_e[7:5] == 3'b011) || (x_e[7:5] == 3'b100); + wire [61:0] x_sig_sh = x_e_mid ? (62'(x_sig) << {x_e[7], x_e[4:0]}) : 62'd0; + + wire [37:0] ex2_mag = x_sig_sh[61:24]; + // A negative ex2 argument is negated as ~mag + 1. The +1 is deferred to + // stage 2 as carries: c0 into the fraction, cz past it into the table index. + wire [38:0] ex2_x = x_s ? ~{1'b0, ex2_mag} : {1'b0, ex2_mag}; + wire ex2_c0 = x_s && (ex2_mag[5:0] == 6'd0); + wire ex2_cz = x_s && (ex2_mag[23:0] == 24'd0); + wire is_sin = (fn == 3'(INST_TFU_SINPI)); - wire [31:0] trig_u0 = (x_e >= 8'd119) ? ((trig_lsh < 8'd32) ? (32'(x_sig) << trig_lsh[4:0]) : 32'd0) - : ((trig_rsh < 8'd24) ? 32'(x_sig >> trig_rsh[4:0]) : 32'd0); - wire [31:0] trig_u = is_sin ? trig_u0 : (trig_u0 + 32'h40000000); + wire [31:0] trig_u0 = x_sig_sh[54:23]; + wire [31:0] trig_u = {trig_u0[31] ^ (~is_sin && trig_u0[30]), trig_u0[30] ^ ~is_sin, trig_u0[29:0]}; wire trig_odd = trig_u[30]; wire [29:0] trig_t = trig_odd ? ~trig_u[29:0] : trig_u[29:0]; wire trig_neg = trig_u[31] ^ (is_sin && x_s); - wire trig_one = trig_odd && (trig_u[29:0] == 30'd0); - `UNUSED_VAR ({ex2_x[5:0], trig_t[5:0]}) + // sin/cos of an odd multiple of 1/2 is exactly +-1: u[29:0] == 0. Bits + // [29:6] are the registered ~idx and ~xl, so only u[5:0] is tested here. + wire trig_one_lo = trig_odd && (trig_u[5:0] == 6'd0); + `UNUSED_VAR ({x_sig_sh[22:0], ex2_x[5:0], trig_t[5:0]}) reg r_special; reg [31:0] r_sval; @@ -60,6 +65,9 @@ module VX_tfu_pe import VX_gpu_pkg::*; ( reg r_neg; reg r_zsign; reg r_lg2; + reg r_c0; + reg r_cz; + reg r_one; always @(*) begin r_special = 1'b0; @@ -71,6 +79,9 @@ module VX_tfu_pe import VX_gpu_pkg::*; ( r_neg = 1'b0; r_zsign = 1'b0; r_lg2 = 1'b0; + r_c0 = 1'b0; + r_cz = 1'b0; + r_one = 1'b0; case (fn) 3'(INST_TFU_RCP): begin r_neg = x_s; @@ -104,6 +115,8 @@ module VX_tfu_pe import VX_gpu_pkg::*; ( r_idx = {1'b0, ex2_x[29:24]}; r_xl = ex2_x[23:6]; r_exp = 10'($signed(ex2_x[38:30])); + r_c0 = ex2_c0; + r_cz = ex2_cz; if (x_inf) begin r_special = 1'b1; r_sval = x_s ? 32'd0 : F32_POS_INF; @@ -138,15 +151,13 @@ module VX_tfu_pe import VX_gpu_pkg::*; ( r_xl = trig_t[23:6]; r_neg = trig_neg; r_zsign = is_sin && x_s; + r_one = trig_one_lo; if (x_inf) begin r_special = 1'b1; r_sval = F32_QNAN; end else if (x_zero) begin r_special = 1'b1; r_sval = is_sin ? {x_s, 31'd0} : F32_ONE; - end else if (trig_one) begin - r_special = 1'b1; - r_sval = {trig_neg, F32_ONE[30:0]}; end end 3'(INST_TFU_TANH): begin @@ -179,21 +190,38 @@ module VX_tfu_pe import VX_gpu_pkg::*; ( localparam MISC_W = 1 + 32 + 10 + 1 + 1 + 1 + 10; - wire [MISC_W-1:0] s1_misc, s2_misc, s3_misc, s4_misc; + wire [MISC_W-1:0] s1_misc_r, s1_misc, s2_misc, s3_misc, s4_misc; wire [2:0] s1_tbl; wire [6:0] s1_idx; wire [17:0] s1_xl, s2_xl, s3_xl; + wire s1_c0, s1_cz, s1_one; VX_pipe_register #( - .DATAW (MISC_W + 3 + 7 + 18) + .DATAW (MISC_W + 3 + 3 + 7 + 18) ) s1_reg ( .clk (clk), .reset (reset), .enable (enable), - .data_in ({r_special, r_sval, r_exp, r_neg, r_zsign, r_lg2, x_k, r_tbl, r_idx, r_xl}), - .data_out ({s1_misc, s1_tbl, s1_idx, s1_xl}) + .data_in ({r_special, r_sval, r_exp, r_neg, r_zsign, r_lg2, x_k, r_c0, r_cz, r_one, r_tbl, r_idx, r_xl}), + .data_out ({s1_misc_r, s1_c0, s1_cz, s1_one, s1_tbl, s1_idx, s1_xl}) ); + wire s1_special; + wire [31:0] s1_sval; + wire signed [9:0] s1_exp; + wire s1_neg; + wire [11:0] s1_rest; + assign {s1_special, s1_sval, s1_exp, s1_neg, s1_rest} = s1_misc_r; + + // What stage 1 deferred: the ex2 negation's carries and the sin/cos +-1 case. + + wire [6:0] s1_idx_c = {s1_idx[6], s1_idx[5:0] + 6'(s1_cz)}; + wire [17:0] s1_xl_c = s1_xl + 18'(s1_c0); + wire signed [9:0] s1_exp_c = s1_exp + 10'(s1_cz && (s1_idx[5:0] == 6'h3f)); + wire s1_trig_one = s1_one && ~s1_special && (s1_idx[5:0] == 6'h3f) && (s1_xl == 18'h3ffff); + wire [31:0] s1_sval_c = s1_trig_one ? {s1_neg, F32_ONE[30:0]} : s1_sval; + assign s1_misc = {s1_special || s1_trig_one, s1_sval_c, s1_exp_c, s1_neg, s1_rest}; + wire [31:0] rom_a0; wire signed [26:0] rom_a1; wire signed [19:0] rom_a2; @@ -202,7 +230,7 @@ module VX_tfu_pe import VX_gpu_pkg::*; ( .clk (clk), .enable (enable), .tbl (s1_tbl), - .idx (s1_idx), + .idx (s1_idx_c), .a0 (rom_a0), .a1 (rom_a1), .a2 (rom_a2) @@ -214,7 +242,7 @@ module VX_tfu_pe import VX_gpu_pkg::*; ( .clk (clk), .reset (reset), .enable (enable), - .data_in ({s1_misc, s1_xl}), + .data_in ({s1_misc, s1_xl_c}), .data_out ({s2_misc, s2_xl}) ); @@ -284,10 +312,18 @@ module VX_tfu_pe import VX_gpu_pkg::*; ( wire signed [27:0] t1 = 28'(s4_p1 >>> 18); wire signed [20:0] t2 = 21'(s4_p2 >>> 18); wire signed [34:0] p_sum = $signed({3'b000, s4_a0}) + 35'(t1) + 35'(t2); - wire [33:0] p_pos = p_sum[34] ? 34'd0 : p_sum[33:0]; - wire signed [40:0] lg2_sum = 41'($signed({s4_k, 30'd0})) + $signed({7'd0, p_pos}); - wire lg2_neg = lg2_sum[40]; - wire [39:0] lg2_mag = 40'(lg2_neg ? -lg2_sum : lg2_sum); + wire p_neg = p_sum[34]; + wire [33:0] p_pos = p_neg ? 34'd0 : p_sum[33:0]; + + // lg2 = k * 2^30 + p_pos. Its value and its negation are each one sum of + // the polynomial terms, in parallel, so no negate follows the sum. + wire signed [40:0] lg2_k = 41'($signed({s4_k, 30'd0})); + wire signed [40:0] lg2_a = 41'($signed({3'b000, s4_a0})); + wire signed [40:0] lg2_p = lg2_k + lg2_a + 41'(t1) + 41'(t2); + wire [39:0] lg2_n = 40'(~lg2_k + ~lg2_a + ~41'(t1) + ~41'(t2) + 41'sd4); + wire [9:0] lg2_kabs = s4_k[9] ? 10'(-s4_k) : 10'(s4_k); + wire lg2_neg = p_neg ? s4_k[9] : lg2_p[40]; + wire [39:0] lg2_mag = p_neg ? {lg2_kabs, 30'd0} : (lg2_p[40] ? lg2_n : lg2_p[39:0]); wire [39:0] mag = s4_lg2 ? lg2_mag : 40'(p_pos); wire neg = s4_lg2 ? lg2_neg : s4_neg; From f46fddc643b058f7a60fd5f67f7dcdf5a8273032 Mon Sep 17 00:00:00 2001 From: jel221 Date: Thu, 3 Dec 2026 12:04:45 -0800 Subject: [PATCH 2/2] ci: re-record the beacon_asic tfu baseline gate_asic judges tfu IMPROVED after the PE path rework (cells 15150 -> 14074, -7.1%, past the 5% band), so the win is recorded to lock it in. Fmax 913.2 -> 1239.2 MHz, 36 -> 24 stages, flip-flops 1813 -> 1816. Command: ci/logic_gate.py --tool beacon_asic -b tfu --update-baseline (beacon 134d03b9e, CIRCT 90c5fd313, TOOLCHAIN_REV v3.0.4). Re-recorded by: agent, authorized by jel221 2026-12-03. --- ci/baselines/synthesis/beacon_asic/tfu.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ci/baselines/synthesis/beacon_asic/tfu.json b/ci/baselines/synthesis/beacon_asic/tfu.json index c42abfa82d..a518233f6a 100644 --- a/ci/baselines/synthesis/beacon_asic/tfu.json +++ b/ci/baselines/synthesis/beacon_asic/tfu.json @@ -6,20 +6,20 @@ "dut": "tfu", "result": { "build_time_s": 10, - "cells": 15150, - "endpoints": 2340, + "cells": 14074, + "endpoints": 2343, "env": { "beacon": "beacon 134d03b9e", "circt": "CIRCT 90c5fd313", "pdk": "asap7", "xlen": 32 }, - "ff": 1813, - "fmax_mhz": 913.2, + "ff": 1816, + "fmax_mhz": 1239.2, "macros": 0, - "max_stages": 36, + "max_stages": 24, "overhead_ps": 231, - "period_ps": 1095, + "period_ps": 807, "stage_ps": 24, "tied_bits": 0, "tied_ports": []