[Bug] libgemmini / gemmini_params.h parity bugs: dead sys_shift on accumulator output, ROUND_NEAR_EVEN float->long long UB, and misplaced transpose assert when set_only_strides=1

Abierto
#9 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
c, cpp
Área
devtools

Línea de trabajo

Start with the accumulator branch of the store path in gemmini.cc around lines 638-647, where shifted is computed but value is written to the accumulator, and compare it with the scratchpad branch near lines 655-659. Then check the assertions at gemmini.cc lines 420 and 434 and the macros in include/gemmini_params.h (ROUND_NEAR_EVEN, ACC_SCALE). Done means the accumulator output matches the RTL behavior described in the issue, and the three divergences are checked with Spike runs against the reference RTL.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Verified Upstream HEADs:

  • ucb-bar/libgemmini master @ 6652649b26aa0beede6bc8f049b52431f2768c28 (2026-10-15)
  • ucb-bar/gemmini-rocc-tests dev @ 7ac28e7e7f447d4131db03292de405e5bd978410 (2026-11-10, include/gemmini_params.h)

(Note: All items below are software/reference-simulator divergences where cycle-accurate Gemmini RTL behaves correctly.)

1. OS sys_shift Computed into Dead Variable and Silently Ignored Whenever Output Targets Accumulator SRAM (gemmini.cc:638-647)

  • RTL Behavior (Correct): PE.scala:91, 104, 111 (live ucb-bar/gemmini @ b82d49cc) applies ROUNDING_RIGHT_SHIFT (shift_offset = sys_shift) inside the PE before io.out_c leaves the systolic array, regardless of whether destination C is in Scratchpad or Accumulator SRAM.
  • Root Cause (gemmini.cc:638-647):
    if (acc) {
      output_t shifted = gemmini_state.mode == gemmini_state_t::OS ?
              sys_shift(value, gemmini_state.sys_shift) :
              sys_shift(value, 0);
    
      if (acc_accum) {
        gemmini_state.accumulator.at(base_sp_addr + gemmini_state.c_stride * i).at(j) += value; // BUG: uses `value`, not shifted!
      } else { // Overwrite
        gemmini_state.accumulator.at(base_sp_addr + gemmini_state.c_stride * i).at(j) = value;  // BUG: uses `value`, not shifted!
      }
    
    shifted is computed and immediately discarded (whereas the else scratchpad branch at L655-659 properly passes shifted to apply_activation_sys(shifted)).
  • Observed Divergence (sys_shift = 2, rc = 0 on both):
    • GemminiRocketConfig RTL (36,679 cyc): ACC_I8 = [2, 5, -2, -5, 1, -1, 25, -25], ACC_I32 = [2, 5, -2, -5, 1, -1, 25, -25], SPAD_I8 = [2, 5, -2, -5, 1, -1, 25, -25]
    • Spike (libgemmini): ACC_I8 = [10, 20, -10, -20, 3, -3, 100, -100], ACC_I32 = [10, 20, -10, -20, 3, -3, 100, -100] (unshifted!) vs. SPAD_I8 = [2, 5, -2, -5, 1, -1, 25, -25].
  • Suggested Fix (gemmini.cc:638-647): Note that sys_shift() (gemmini.cc:1863-1874) returns elem_t (clamping to [-128, 127]), so do not write sys_shift() directly into the 32-bit accumulator. Instead, compute a wide output_t (acc_t) rounding right shift in OS mode (e.g., output_t shifted = (gemmini_state.mode == gemmini_state_t::OS) ? ROUNDING_RIGHT_SHIFT(value, gemmini_state.sys_shift) : value;, optionally clipped to spatialArrayOutputType width to match RTL) and add/assign shifted at L644, 646.

2. ROUND_NEAR_EVEN Performs float -> long long Cast Before Clamping (C++ UB Sign-Inverting Large Positive Scales to -128)

  • Locations: ucb-bar/gemmini-rocc-tests include/gemmini_params.h:52-60, 68-72 (invoked by libgemmini gemmini.cc:1822 [MVIN_SCALE] and L1851 [ACC_SCALE]).
  • RTL Behavior (Correct): HardFloat RecFNToIN saturates positive floating-point overflow (e.g., +1 * 1.58e29f or +Inf) to +127 (INT8_MAX) on both MVIN_SCALE and ACC_SCALE.
  • Root Cause (include/gemmini_params.h:52-72):
    #define ROUND_NEAR_EVEN(x) \
        ({ const SAME_TYPE(x) x_ = (x); \
             const long long i = x_; /* UB when |x_| >= 2^63 or Inf/NaN -> LLONG_MIN on x86-64! */ \
             ... })
    
    #define ACC_SCALE(x, scale) \
        ({float y = ROUND_NEAR_EVEN((x) * (scale)); y > INT8_MAX ? INT8_MAX : (y < INT8_MIN ? INT8_MIN : (acc_t)y);})
    
    On x86-64 hosts (cvttss2si), any finite positive product >= 2^63 (or +Inf) yields LLONG_MIN (-2^63), so y < INT8_MIN silently sign-inverts every positive element to -128 (INT8_MIN) instead of +127.
  • Observed Divergence (scale = 1.58e29f, rc = 0 on both, verified on both ACC_SCALE and MVIN_SCALE):
    • RTL (32,930 cyc): [0, 127, -128, 127, -128, 127, -128, 127]
    • Spike: [0, -128, -128, -128, -128, -128, -128, -128]
  • Suggested Fix (gemmini_params.h & Configs.scala): Clamp the floating-point product (isnan(p) / p >= INT8_MAX / p <= INT8_MIN) before calling ROUND_NEAR_EVEN(p).

3. Control & Assertion Parity Gaps (gemmini.cc:420, 434, 138-141)

  1. Over-strict new_sys_shift assertion (gemmini.cc:420): assert(new_sys_shift >= 0 && new_sys_shift < sizeof(output_t)*8) aborts with SIGABRT on shift >= 32, whereas RTL ExecuteController.scala:116 stores a 5-bit register (shift & 0x1f, returning rc = 0).
  2. Misplaced OS/WS transpose assertion when set_only_strides = 1 (gemmini.cc:434): assert(!(new_mode == OS && !new_a_transpose && new_b_transpose) && !(new_mode == WS && new_a_transpose && new_b_transpose)) is placed outside if (!set_only_strides) (L422-429). When software issues config_ex with set_only_strides = 1 (rs1[7] = 1), RTL ExecuteController.scala:550 (when (!set_only_strides)) ignores the dataflow/transpose bits (rc = 0, 16,105 cyc), whereas libgemmini still asserts on the ignored local new_mode/new_a_transpose/new_b_transpose variables and aborts (rc = 255). Moving L434 inside if (!set_only_strides) makes libgemmini match RTL cleanly.
  3. Missing RTL-assert parity (optional strict mode): libgemmini silently returns rc = 0 on zero-column/zero-byte mvin (gemmini.cc:138-141, which trips LoadController.scala:189 in RTL) and on unfenced preload + config_ex/config_ld queue hazards (which trip ReservationStation.scala in RTL). Adding optional strict-mode assertions would help catch RTL-failing programs during Spike simulation.

Happy to open a small PR for items 1 and 3 (gemmini.cc) if helpful!

Lenguaje dominante
C++
Estrellas
5
Forks
18
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de ucb-bar/libgemmini

Todos los issues de ucb-bar/libgemmini

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.