Skip to content

Reverse-engineer ESP32-P4 PIE (xesppie) instruction set by experimentation (for a PIE transpose) #2535

Closed

Description

@zackees

Goal

Reverse-engineer the ESP32-P4 PIE (xesppie) SIMD instruction set by experimentation, so we can evaluate a PIE-accelerated bit transpose (and other DSP kernels). The P4 PIE technical-reference chapter is empty (undocumented as of 2026), but the toolchain (-march=...xesppie) and silicon support it: confirmed on this board that esp.vld.128.ip/esp.vst.128.ip inline asm assembles and executes correctly (128-bit load→store smoke test, pie_smoke=1, see #2533).

⚠️ Cross-chip caveat (answered): PIE is NOT guaranteed identical across all ESP32-P4 silicon

Research finding — do not assume "works on this chip" ⇒ "works on all P4s":

  • The P4 PIE was changed across revisions: "in revision v3.x … PIE was updated for saturation and rounding." So PIE semantics differ by silicon revision.
  • The P4 moved from ECO naming (eco0/1/2) to a vM.X revision scheme; field devices show varied revisions, and there is a reported "Illegal instruction" bootloader crash on a v1.3 engineering sample — i.e. instruction availability is revision-sensitive.
  • This board is an early eco2 engineering sample (esp32p4-eco2-20240710). Core load/store (esp.vld/vst.128) is likely stable across revisions, but specific ALU/saturation/shuffle ops may not be.

Implication: any reverse-engineered PIE code must (a) be validated per revision, and (b) be gated at runtime on chip revision (esp_chip_info() / efuse) with a portable scalar fallback. For production, the portable spread-LUT transpose (#2533/#2534, ~2×, bit-exact, runs on every P4) remains the safe default; PIE is research/opt-in only.

Reverse-engineering plan (low-risk → high-risk)

1. Toolchain enumeration (no device, no crash risk) — do first

The assembler already knows the P4 PIE mnemonics (-march has xesppie). Enumerate the accepted set:

  • For each candidate mnemonic, try to assemble a one-instruction .s/inline-asm TU with riscv32-esp-elf-as -march=rv32imafc_zicsr_zifencei_xesppie; the accepted set = the toolchain's known P4 PIE instructions.
  • Candidate list = the ESP32-S3 PIE instructions (P4 PIE "closely matches the S3's", esp. prefix instead of ee.): vld.128/vst.128, vadd/vsub.s8/s16, vmul, vcmp.eq/gt, vzip.8/vunzip.16, andq/orq/notq/xorq, srl/sll/sra, ldxq/stxq, movi, zero.q, vsl/vsr, etc. (from the S3 TRM PIE chapter + community RE).

2. objdump esp-dsp (no device) — ground truth

Espressif's esp-dsp library ships hand-written P4 PIE assembly (FFT/FIR/dotprod). Build it for esp32p4 and objdump -d the .os to get a real list of emitted esp.* instructions + their encodings (and operand forms). This is the most reliable instruction inventory.

3. On-device semantics probe (controlled) — a probePie RPC

Extend the AutoResearch RPC harness: probePie{op, qregs, input[16]} that

  • esp.vld.128 known input into Q registers,
  • executes ONE candidate esp.* op (compile-time-selected),
  • esp.vst.128 the result, returns the 16 output bytes.
    Infer each op's semantics from input→output (e.g. is vzip.8 byte-interleave? what lane width?). Cross-check against the S3 instruction's documented behavior.

4. Survivable illegal-instruction trap

To enumerate safely without reflashing on every bad guess, install a RISC-V illegal-instruction trap handler that records the trapping PC + skips the instruction (sets a "unsupported" flag), so the probe can sweep candidates in one run. (ESP-IDF panic/exception hook.) Without it, each illegal op crashes → reflash; with it, sweep is one pass.

5. Build the transpose primitive (if a usable op exists)

A SIMD bit-transpose needs a bit-movemask (extract bit p of each of 16 bytes → 16-bit value) or a bit interleave. Check whether esp.vzip.8/esp.vunzip.16 + shifts + esp.andq can compose one (byte-interleave → shift → recombine). If so, prototype a PIE wave8_transpose_16, bit-exact-gate vs naive (host + on-device mismatch==0), and benchmark vs the spread-LUT (1.98×) via bash autoresearch --transpose 16.

6. Gate + fallback

If a PIE transpose wins AND is bit-correct: ship it gated on chip revision with the spread-LUT as the universal fallback. Validate on as many P4 revisions as available (this is eco2; ideally also a v1.x and v3.x).

Done criteria

  • A documented table of confirmed P4 PIE instructions (mnemonic, operands, semantics, which revisions tested), from steps 1–4.
  • A decision on whether a PIE bit-transpose beats the spread-LUT, with on-device numbers.
  • If yes: a revision-gated PIE transpose + fallback; if no: documented why (e.g. no usable bit-extract).

Refs #2533 #2534

Activity

  1. zackees commented on May 29, 2026

    @zackees
    MemberAuthor

    Report: PIE (xesppie) is NOT a viable speedup for the bit transpose — recommend keeping the spread-LUT

    Investigated per the goal (detection defines, lowest-common-denominator subset, feasibility). On-device: the PIE foundation works (esp.vld.128/esp.vst.128 assemble + execute, pie_smoke=1). But a PIE bit-transpose is blocked by the ISA itself.

    Detection defines (how to know the ISA exists)

    • __riscv_xesppie — GCC predefined macro (=1000000), set by -march=...xesppie. Primary compile-time gate.
    • SOC_CPU_HAS_PIE / SOC_SIMD_INSTRUCTION_SUPPORTED — ESP-IDF SOC caps (CONFIG_SOC_CPU_HAS_PIE). Note in the header: "PIE coprocessor assembly is only supported with GCC compiler."
    • Recommended gate: #if defined(__riscv_xesppie) && !defined(__clang__) (PIE asm is GCC-only; host/clang must use the scalar fallback).

    Lowest-common-denominator (revision-stable) op inventory — confirmed by assembling each

    OK: vld.128.ip, vst.128.ip, andq, orq, xorq, notq, zero.q, movi.32.q, vcmp.eq.s8, vunzip.8, vsl.32, vsr.u32, slci.2q, srci.2q, src.q.qup, cmul.s16.
    FAIL: movi.32.a (Q→scalar, rejected), zip.8, per-byte shifts (sll.s8/srl.u8), vmsb (movemask — unrecognized).
    LCD = load/store + per-lane bitwise/shift + byte shift-concatenate; the saturation/rounding DSP ops that changed in rev v3.x are NOT needed and are excluded, so an LCD kernel would be revision-portable.

    Why a PIE bit-transpose can't beat the scalar spread-LUT

    The op is: gather bit p of each of 16 byte-lanes → one 16-bit word, ×8 pulses. That needs a movemask (extract one bit per lane → scalar) or a bit-level cross-lane shuffle. PIE (S3-equivalent DSP ISA) has neither:

    • No movemask (vmsb absent; movi.32.a Q→scalar rejected).
    • Shifts are per-lane (vsl.32) or byte-concatenate (slci.2q) — they move bits within a lane or whole bytes across lanes, never single bits across lanes.
      So the fast SIMD recipe (load 16 → 8×[shift + movemask]) is impossible. The only fallback — a 128-bit SWAR delta-swap built from slci.2q+andq+xorq — is a serial dependency chain, the exact shape that already measured slower than the naive on scalar RV32 (u64 SWAR 0.41×, HD 0.71×), and it would further require slci.2q to be bit-granular (undocumented; S3 uses it byte-granular). Net: high effort + crash-prone on-device RE, to land on an approach whose structure already lost.

    Recommendation

    Do not implement PIE for the transpose. Keep the shipped portable spread-LUT (#2534): 1.98×/1.90× (16/8-lane), bit-exact, runs on every P4 revision, no GCC/PIE/revision gating. PIE is well-suited to DSP/MAC kernels (FFT/FIR — what esp-dsp uses it for), not bit-matrix transposes.

    Goal outcome

    Per the goal ("…else report that it's actually slower"): a PIE transpose is not faster — it can't be built to beat the spread-LUT because the required bit-level cross-lane primitive is absent from the ISA. The PIE foundation (load/store) is tested-working; the transpose-specific kernel is infeasible. Reverse-engineering slci.2q's exact semantics to attempt the (already-disadvantaged) delta-swap remains a research follow-up here in #2535, not recommended given the shipped 1.98× portable win.

    Refs #2533 #2534

  2. zackees commented on May 29, 2026

    @zackees
    MemberAuthor

    Done. PIE (xesppie) instruction inventory established by assembler probe + on-device smoke test (vld/vst/andq/orq/xorq/notq execute on the attached v1.3 chip). Conclusion: PIE has no movemask / bit-level cross-lane primitive, so it cannot accelerate a bit-matrix transpose — the transpose stays on the scalar spread-LUT (#2534). The PIE foundation was put to use for fl::simd instead (#2536 → merged #2538). Closing the RE effort; no PIE transpose to pursue.

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