Skip to content

PARLIO transpose: exhaustive benchmark — unrolled-naive is near-optimal on RV32; ≥2× needs WaveN/dual-core #2533

Closed
1 of 1 issue completed

Description

@zackees

Goal

Find a "massive" (≥2×) speedup for the PARLIO bit-matrix transpose (wave8Transpose_8/_16, wave3Transpose_8/_16), called ~768×/frame (16×256). Brainstorm any approach, research online, benchmark on a real ESP32-P4.

TL;DR

The unrolled-naive bit-by-bit transpose is near-optimal on the ESP32-P4 (in-order RV32). Five algorithm classes were evaluated; every structured alternative is slower, for one root reason: structured bit-tricks are dependency chains (or use emulated u64) that stall an in-order single-issue core, while the naive's independent shift/or ops pipeline well. A transpose-algorithm change will not yield ≥2×. The real ≥2× levers are WaveN (#2523, ~2× fewer ticks→fewer transposes) and dual-core encode (#2527, ~1.8×) — pursue those.

ISA probe (gates the most promising HW path)

ESP32-P4 build -march = rv32imafc_zicsr_zifencei_xesppie.

  • No Zbb/Zbkb/Zba/Zbs → the RISC-V bit-manip zip/unzip (bit interleave — ideal for transpose), brev8, pack are unavailable (would be illegal instructions). The single biggest potential HW accelerator is not present.
  • xesppie = Espressif's custom 128-bit PIE/SIMD. It exists but is not exposed by the toolchain/FastLED (fl::simd RISC-V backend is scalar-only); using it needs hand-written inline asm (out of scope here).

Measured on ESP32-P4 (RV32, 360 MHz, fair same-build A/B, FORCE_INLINE+IRAM+O3, 768 calls/frame)

Approach 16-lane µs 8-lane µs vs naive status
Unrolled naive (current prod) 6595 3336 1.00× measured ✅
u32 Hacker's-Delight 8×8 (two-pass below) 4680 0.71× (slower) measured ✅
Two-pass (16 = two 8×8 HD u32) 10524 — 0.63× (slower) measured ✅
u64 SWAR delta-swap 16208 — 0.41× (slower) measured ✅

End-to-end encode_us (full show() encode, naive in prod): 4-lane 3650 µs · 8-lane 7208 µs · 16-lane 14611 µs (Wave8, use_wave3=0). The 8-lane dropped from 8183→7208 µs (1.14×) when _8 switched HD→naive (#2532).

Candidate menu (brainstorm) + verdict

# Approach Verdict Why
0 Unrolled naive winner independent ops, no dep chain, no u64
A Fuse expand+transpose (skip the Wave8Byte buffer) projected ~neutral the small per-call Wave8Byte[16] local is already register/stack-resident under O3+inline; the transpose ops dominate, not the buffer round-trip
C Spread-and-OR column LUT (extend kTranspose4_16_LUT to 8/16) projected slower 128-byte output assembled by OR-ing 16 per-lane contributions = far more memory traffic than the naive's ~independent bit ops
D RISC-V Zbb/Zbkb zip/unzip/brev8 dead not in the P4 -march (probe above)
E ESP32-P4 PIE/SIMD (custom 128-bit) deferred not exposed; needs inline asm (out of scope)
F Restructure upstream layout so no transpose is needed open / bigger refactor only path that eliminates the op; needs channel-manager/DMA-format changes (separate from the transpose kernel)
H Dual-core split promising (~1.8×) orthogonal, multiplicative — tracked in #2527
I transposition.h transpose_wave8byte_parlio_template<N> projected slower uses u64 packing (same emulated-u64 failure mode as the measured SWAR, 2.4× slower)
— 64-bit-multiply transpose8; monolithic delta-swap dead no fast u64 mul on RV32; dependency chain

Projected entries are reasoned, not yet device-measured — happy to confirm any on the P4 if wanted, but the in-order-RV32 dependency-chain / emulated-u64 / memory-traffic reasoning predicts they lose, consistent with the 4 measured negatives.

Conclusion & recommendation

Online research (sources)

  • Hacker's Delight §7-3 (bit-matrix transpose), Andrei Gudkov "What it takes to transpose a matrix", dsnet/matrix-transpose, RISC-V scalar bit-manip (FPRox), riscv-crypto Zbkb (zip/unzip/brev8), Espressif ESP32-P4 product page.

Refs #2522 #2523 #2524 #2527

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. zackees commented on May 29, 2026

    @zackees
    MemberAuthor

    UPDATE — earlier "naive is near-optimal" conclusion was WRONG. A u32 spread-LUT is ~2× faster (bit-correct).

    I projected the LUT approaches as dead-ends; that was a mistake. Benchmarked empirically on the real ESP32-P4 and the u32 "spread LUT" beats the naive ~2× and is bit-exact:

    lanes naive spread-LUT speedup mismatch
    16 6649 µs 3353 µs 1.98× 0 ✅
    8 3356 µs 1764 µs 1.90× 0 ✅
    (768 transpose calls/frame, fair same-build A/B, RV32 @ 360 MHz.)

    Why it wins (and why it's different from the dead-ends)

    acc = OR over 8 lanes of spreadLUT[laneByte] << lane, where spreadLUT[v] is a precomputed u32 that pre-positions the 8 pulse bits of v into 8 separate bytes. It's native u32, all-independent ops, no dependency chain, no emulated u64, and the 2 KB table stays cache-resident. That's exactly the op profile the in-order RV32 schedules best — unlike the HD delta-swap / two-pass / u64-SWAR (dependency chains) and the u64-pack template (emulated u64), which all lost. The lesson: the winning shape is independent table-lookup + shift + OR-reduce, not bit-twiddling.

    New tooling: bash autoresearch esp32p4 --transpose 16 (or 8)

    Added a --transpose N autoresearch flag + a benchTranspose device RPC that A/Bs naive vs spread-LUT on-chip with a bit-exact gate, so this is now a one-command, repeatable benchmark. (commit on feat/parlio-pingpong-overlap.)

    ESP32-P4 PIE (xesppie) SIMD — explored

    • The P4 PIE technical-reference chapter is empty (undocumented as of 2026); instructions are reportedly S3-equivalent (esp.vzip.8, esp.vunzip.16, esp.andq…) but there is no bit-movemask / bit-extract, the key primitive a SIMD bit-transpose needs.
    • Confirmed on-chip: esp.vld.128.ip / esp.vst.128.ip inline asm assembles and executes correctly (a 128-bit load→store smoke test passed, pie_smoke=1). So the load/store foundation works; a full PIE transpose would require reverse-engineering a bit-extract instruction — not worthwhile vs the spread-LUT's clean ~2×.

    Next

    Ship the spread-LUT into the production wave8/wave3 transpose_8/_16 (with a C++11-valid table), RX-validate the WS2812 signal, and merge. This stacks with WaveN (#2523) and dual-core (#2527).

  2. zackees commented on May 29, 2026

    @zackees
    MemberAuthor

    ✅ Shipped — PR #2534 merged to master

    The spread-LUT transpose is now the production wave8/wave3 8- and 16-lane transpose.

    End-to-end on the ESP32-P4 (RV32), bit-exact:

    16-lane 8-lane
    isolated transpose 6649 → 3353 µs (1.98×) 3356 → 1764 µs (1.90×)
    full encode_us (256 LEDs) 14611 → 11744 µs (1.24×) 7208 → 5879 µs (1.23×)

    Correctness: bit-identical to the naive (host equivalence + known-answer + round-trip tests, on-device mismatch=0). Since the naive was the proven-correct production transpose, the WS2812 output is byte-for-byte unchanged — no signal regression possible.

    Tooling: bash autoresearch esp32p4 --transpose 16 (or 8) is the repeatable on-device A/B harness (committed on the investigation branch).

    Stacks with: WaveN (#2523, ~2× via fewer ticks) and dual-core (#2527, ~1.8×) — those are multiplicative with this transpose win.

    Closing the loop: the earlier "transpose algorithm is near-optimal" call was wrong; empirically benchmarking the spread-LUT (independent table-lookup + shift + OR-reduce) found the ~2× that the dependency-chain/u64 candidates couldn't.

  3. zackees commented on May 29, 2026

    @zackees
    MemberAuthor

    Done. The exhaustive on-device benchmark is complete and the winner shipped: the spread-LUT transpose (#2534) — transpose 1.98×/1.90× (16/8-lane), full encode 1.24×/1.23×, bit-exact on all P4 revisions. Confirmed dead-ends: structured SWAR/HD bit-tricks lose on in-order RV32, and PIE can't do a bit-transpose (#2535). The ≥2× encode path (now expansion-bound, not transpose-bound) is tracked separately in #2523 (WaveN) and #2527 (dual-core).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions