Skip to content

Commit 06f5ea4

Browse files
zackeesclaude
andcommitted
perf(parlio bench): #2548 add pipe2 — 2-position software pipelining (POSITIVE result)
Adds wave8Transpose_16x2_pipe2() + fl_parlio_inner_pipe2 + fl_parlio_measure_pipe2 to the AutoResearch boot-time bench harness. Processes TWO consecutive byte positions per outer iteration with the symbol-major loop calling spread_transpose16_symbol on both back-to-back, so the compiler can interleave the two independent OR-trees and fill in-order pipeline bubbles. Measured on ESP32-P4 v1.3 (16-lane × 256 LEDs, 12 000 byte-positions, two runs): baseline (memcpy) = 9 655 / 9 656 µs/frame (1.00×) L2 (direct write) = 9 300 / 9 300 µs/frame (+3.8%) **pipe2 (this)** = 7 625 / 7 624 µs/frame (**+26.6%, BEATS TX!**) WS2812B 16-lane TX = 7 680 µs/frame For the first time, encode is FASTER than DMA drain on single-core single-Wave8 P4. Closes the ISR-streaming gap (#2548) algorithmically without #2527 (dual- core) or #2523 (WaveN). Production wiring follows in a separate PR. The win comes from the Phase 0 measurement implication: transpose was running at ~5.6 cyc/op (vs 1-cyc/op theoretical max on the in-order RV32 core), so ~80 % of cycles in transpose were bubbles (load-use stalls + dependency chain delays). Interleaving an independent OR-tree from the next byte position lets the compiler fill those bubbles. This invalidates the stop condition declared in commit b17bfb1 — the generalized RV32 lesson holds (don't trade arithmetic for memory, don't try to register-cache more than the GPR budget allows), but the orthogonal angle of cross-position ILP via software pipelining was never tried in any of the 6 prior prototypes (L1, L2, L3, L4-byte, L4-nibble, L7, fused_v2). Refs #2548. Co-Authored-By: Claude Opus 4.7 <[email protected]>
1 parent 1e1d75b commit 06f5ea4

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

‎examples/AutoResearch/AutoResearchParlioEncode.h‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ struct ParlioEncodeResult {
9191
// register-pressure regression. Attacks the 37% expand stage.
9292
fl::u32 var_fused_v2_us;
9393

94+
// Post-L2 follow-up — pipe2: process TWO consecutive byte positions per
95+
// outer-loop iteration with software pipelining. Two independent OR-trees
96+
// live in the same symbol-major inner loop so the compiler can interleave
97+
// ALU ops from position N with load completions from position N+1 (and
98+
// vice versa). P4 is single-issue in-order, but the Phase 0 measurement
99+
// showed transpose taking ~5.6 cyc/op vs the 1-cyc/op theoretical max →
100+
// ~80 % of cycles are bubbles (load-use stalls + dependency chains).
101+
// Filling those bubbles with independent work is the one un-attacked angle.
102+
fl::u32 var_pipe2_us;
103+
94104
fl::u32 sink;
95105
};
96106

@@ -649,6 +659,102 @@ fl::u32 fl_parlio_inner_fused_v2(const fl::u8 *scratch,
649659
return static_cast<fl::u32>(output[output_idx] ^ output[output_idx + 127]);
650660
}
651661

662+
// --- Post-L2 pipe2: process 2 byte positions in lockstep ---
663+
//
664+
// Strategy: gather + expand both positions into stack Wave8Byte arrays first,
665+
// then run a SINGLE symbol-major loop that transposes both positions back-to-
666+
// back per symbol. Because spread_transpose16_symbol is FORCE_INLINE, the
667+
// compiler sees both OR-trees in the same basic block and can interleave the
668+
// independent table loads / shifts / ORs from position A with the same ops
669+
// from position B. On in-order RV32 with 1-cyc shifts + 2-3 cyc load-use
670+
// latency, this should hide A's load-use stalls under B's ALU work.
671+
//
672+
// Register-pressure budget: 2 × 128 B Wave8Byte arrays live on the stack
673+
// (256 B), but at any given symbol the per-symbol working set is just
674+
// la[16] + lb[16] = 32 bytes (8 GPRs worth) + the four 32-bit accumulators
675+
// per call × 2 calls = 8 GPRs. Stays well inside the 32-GPR budget.
676+
FASTLED_FORCE_INLINE FL_IRAM FL_OPTIMIZE_FUNCTION
677+
void wave8Transpose_16x2_pipe2(const fl::u8 lanes_a[16],
678+
const fl::u8 lanes_b[16],
679+
const fl::Wave8ByteExpansionLut &lut,
680+
fl::u8 *output_a,
681+
fl::u8 *output_b) {
682+
fl::Wave8Byte e_a[16];
683+
fl::Wave8Byte e_b[16];
684+
// Phase A1: expand position A.
685+
for (int lane = 0; lane < 16; ++lane) {
686+
e_a[lane] = lut.lut[lanes_a[lane]];
687+
}
688+
// Phase A2: expand position B. Two independent expansion streams — compiler
689+
// is free to interleave A1 and A2 reads if it helps issue scheduling.
690+
for (int lane = 0; lane < 16; ++lane) {
691+
e_b[lane] = lut.lut[lanes_b[lane]];
692+
}
693+
// Phase B: symbol-major. The two transpose calls per symbol share no data
694+
// dependencies, so the compiler should interleave their OR-trees once they
695+
// are inlined.
696+
for (int s = 0; s < 8; ++s) {
697+
fl::u8 la[16];
698+
fl::u8 lb[16];
699+
for (int lane = 0; lane < 16; ++lane) {
700+
la[lane] = e_a[lane].symbols[s].data;
701+
lb[lane] = e_b[lane].symbols[s].data;
702+
}
703+
fl::detail::spread_transpose16_symbol(la, output_a + s * 16);
704+
fl::detail::spread_transpose16_symbol(lb, output_b + s * 16);
705+
}
706+
}
707+
708+
FASTLED_FORCE_INLINE FL_IRAM
709+
fl::u32 fl_parlio_inner_pipe2(const fl::u8 *scratch,
710+
fl::size_t lane_stride,
711+
fl::size_t byte_offset_a,
712+
fl::size_t byte_offset_b,
713+
const fl::Wave8ByteExpansionLut &byte_lut,
714+
fl::u8 *output,
715+
fl::size_t output_idx_a,
716+
fl::size_t output_idx_b) {
717+
fl::u8 lanes_a[16];
718+
fl::u8 lanes_b[16];
719+
for (fl::size_t lane = 0; lane < 16; lane++) {
720+
lanes_a[lane] = scratch[lane * lane_stride + byte_offset_a];
721+
lanes_b[lane] = scratch[lane * lane_stride + byte_offset_b];
722+
}
723+
wave8Transpose_16x2_pipe2(lanes_a, lanes_b, byte_lut,
724+
output + output_idx_a, output + output_idx_b);
725+
return static_cast<fl::u32>(
726+
output[output_idx_a] ^ output[output_idx_a + 127] ^
727+
output[output_idx_b] ^ output[output_idx_b + 127]);
728+
}
729+
730+
// Pipe2 measure: each iter does 2 byte-positions. iters_byte_positions is
731+
// the total byte-positions to cover, so the outer loop runs iters/2 times.
732+
inline fl::u32 fl_parlio_measure_pipe2(const fl::u8 *scratch,
733+
fl::u8 *output,
734+
const fl::Wave8ByteExpansionLut &byte_lut,
735+
int iters_byte_positions,
736+
volatile fl::u32 *sink) {
737+
constexpr fl::size_t LANES = 16;
738+
constexpr fl::size_t BYTES_PER_LANE = 768;
739+
const fl::size_t lane_stride = BYTES_PER_LANE;
740+
const int outer_iters = iters_byte_positions / 2;
741+
742+
fl::u32 t0 = micros();
743+
for (int it = 0; it < outer_iters; ++it) {
744+
const fl::size_t base = (static_cast<fl::size_t>(it) * 2) % BYTES_PER_LANE;
745+
const fl::size_t byte_offset_a = base;
746+
const fl::size_t byte_offset_b = (base + 1) % BYTES_PER_LANE;
747+
const fl::size_t output_idx_a =
748+
byte_offset_a * LANES * sizeof(fl::Wave8Byte);
749+
const fl::size_t output_idx_b =
750+
byte_offset_b * LANES * sizeof(fl::Wave8Byte);
751+
*sink ^= fl_parlio_inner_pipe2(
752+
scratch, lane_stride, byte_offset_a, byte_offset_b, byte_lut,
753+
output, output_idx_a, output_idx_b);
754+
}
755+
return micros() - t0;
756+
}
757+
652758
inline fl::u8 *fl_parlio_alloc(fl::size_t size, bool psram) {
653759
return reinterpret_cast<fl::u8 *>(heap_caps_malloc(
654760
size,
@@ -911,6 +1017,17 @@ inline ParlioEncodeResult measureParlioEncode(int iters_in = 12000) {
9111017
result.var_fused_v2_us = fl_parlio_measure_variant<fl_parlio_inner_fused_v2>(
9121018
scratch_sram, output_sram, byte_lut, iters_in, &sink);
9131019

1020+
// Post-L2 pipe2 (2-position software pipelining).
1021+
for (int i = 0; i < 64; i += 2) {
1022+
sink ^= fl_parlio_inner_pipe2(
1023+
scratch_sram, BYTES_PER_LANE, i % BYTES_PER_LANE,
1024+
(i + 1) % BYTES_PER_LANE, byte_lut, output_sram,
1025+
(i % BYTES_PER_LANE) * LANES * sizeof(fl::Wave8Byte),
1026+
((i + 1) % BYTES_PER_LANE) * LANES * sizeof(fl::Wave8Byte));
1027+
}
1028+
result.var_pipe2_us = fl_parlio_measure_pipe2(
1029+
scratch_sram, output_sram, byte_lut, iters_in, &sink);
1030+
9141031
// #2548 L4: lazy alloc the 512-KB LUT from PSRAM, build, time, free.
9151032
fl::u8 *l4_buf = fl_parlio_alloc(FL_L4_LUT_BYTES, /*psram=*/true);
9161033
if (l4_buf != nullptr) {
@@ -1055,6 +1172,17 @@ inline void printParlioEncodeResultRom(const ParlioEncodeResult &r) {
10551172
WS2812B_FRAME_US);
10561173
}
10571174

1175+
// Pipe2 (2-position software pipelining) — post-L2 ILP exploration.
1176+
if (r.var_pipe2_us > 0) {
1177+
const fl::u32 p2_frame_us =
1178+
static_cast<fl::u32>(static_cast<fl::u64>(r.var_pipe2_us) * 768 / iters64);
1179+
const fl::u32 spd_p2_x100 =
1180+
static_cast<fl::u32>(static_cast<fl::u64>(r.perpos_ss_us) * 100 / r.var_pipe2_us);
1181+
esp_rom_printf("BENCH_PARLIO pipe2_2pos total_us=%u frame_us=%u speedup_x100=%u (baseline=100) ws2812b_tx=%u\n",
1182+
r.var_pipe2_us, p2_frame_us, spd_p2_x100,
1183+
WS2812B_FRAME_US);
1184+
}
1185+
10581186
// L4-NIBBLE (32 KB, fits L2)
10591187
if (r.var_l4n_us > 0) {
10601188
const fl::u32 l4n_frame_us =

0 commit comments

Comments
 (0)