forge
Explore
Log in

Don't you dare go Hollow

Deblock -- masked transform-alignment gate, inlined edge_params head, running-sum filter14

Browse files at this commit

2a2adbabb7cf5c876fc67adeb705e5b6f8efdd0dAhmet Kaan GümüşParent: 46b0c49
  • perf
    • deblock -- masked transform-alignment gate, inlined edge_params head, running-sum filter14
Show full message
perf(av1): deblock -- masked transform-alignment gate, inlined edge_params head, running-sum filter14

Three measured steps on the loop filter, all byte-exact (the four gate
streams hash identically at 1, 4 and 16 threads):

1. `edge_params`' transform-alignment test was `coord % cur_tx`, a hardware
   `divl` whose result test carried 10.7% of the function's samples;
   `cur_tx` is a power of two, so it is a mask. -0.29% instructions.
2. That gate rejects most 4-pixel edge groups, so it is now inlined into the
   deblock loops and only survivors pay the call and the register-spilling
   prologue of `edge_params_body`. -1.83%.
3. The AVX2 kernel's wide-filter rows took their weights as slices, leaving a
   branch and a weight load per tap (`testl`/`je`/`movzwl` were 34% of the
   kernel's samples): `filter6`/`filter8` rows are unrolled with const
   weights, and `filter14`'s twelve rows become a running sum over the
   constant row-to-row deltas (168 tap operations -> 44). -1.50%.

seg4k at one thread: 553.30 -> 533.44 G instructions:u (-3.59%); the deblock
share of the 4K profile falls from ~7.3% to ~6.0% and the kernel itself from
4.00% to 2.52%. `deblock_simd_kernel_matches_the_scalar_filter` already pins
the kernel against the scalar `filter_edge` at 8/10/12-bit over every length,
level and sharpness.
crates/ec-av1/src/decode.rsmodified+110 -19
@@ -17561,6 +17561,7 @@ fn lf_level(
17561 /// edge" suppression (which only ever fires at a non-PU-edge TU boundary)17561 /// edge" suppression (which only ever fires at a non-PU-edge TU boundary)
17562 /// never applies here, letting this skip the `skip`/`skip_txfm` lookup17562 /// never applies here, letting this skip the `skip`/`skip_txfm` lookup
17563 /// entirely. Returns `None` when this 4-pixel edge group is not filtered.17563 /// entirely. Returns `None` when this 4-pixel edge group is not filtered.
17564+#[inline(always)]
17564 fn edge_params(17565 fn edge_params(
17565 lf: &LoopFilterParams,17566 lf: &LoopFilterParams,
17566 n: &Neighbours,17567 n: &Neighbours,
@@ -17581,9 +17582,37 @@ fn edge_params(
17581 } else {17582 } else {
17582 tx_h_px_at(n, chroma, mi_r, mi_c)17583 tx_h_px_at(n, chroma, mi_r, mi_c)
17583 };17584 };
17584- if cur_tx == 0 || (if dir == 0 { x0 } else { y0 }) % cur_tx as usize != 0 {17585+ // lane-deblock: `cur_tx` is a power-of-two pixel size, so the alignment
17586+ // test is a mask, not the hardware `divl` this used to emit (10.7% of
17587+ // `edge_params`' own samples sat on that divide's result test).
17588+ //
17589+ // Most 4-pixel edge groups die right here (a 4-aligned coordinate that
17590+ // the covering transform does not start at), so this head is inlined
17591+ // into the deblock loops and only survivors pay the call and the
17592+ // register-spilling prologue of the body below.
17593+ if cur_tx == 0 || (if dir == 0 { x0 } else { y0 }) & (cur_tx as usize - 1) != 0 {
17585 return None;17594 return None;
17586 }17595 }
17596+ edge_params_body(lf, n, plane_idx, dir, chroma, x0, y0, mi_r, mi_c, cur_tx, seg_lf_active, fctx)
17597+}
17598+
17599+/// [`edge_params`] past its transform-alignment gate.
17600+#[allow(clippy::too_many_arguments)]
17601+#[inline(never)]
17602+fn edge_params_body(
17603+ lf: &LoopFilterParams,
17604+ n: &Neighbours,
17605+ plane_idx: usize,
17606+ dir: usize,
17607+ chroma: bool,
17608+ x0: usize,
17609+ y0: usize,
17610+ mi_r: usize,
17611+ mi_c: usize,
17612+ cur_tx: u32,
17613+ seg_lf_active: bool,
17614+ fctx: &crate::decode::FrameCtx,
17615+) -> Option<(u8, i32)> {
17587 let cur_ref = n.ref_grid[mi_r * n.skip_grid_cols_mi + mi_c];17616 let cur_ref = n.ref_grid[mi_r * n.skip_grid_cols_mi + mi_c];
17588 // `mode_step` (`set_lpf_parameters`): one mi for luma, `1 << ss` for17617 // `mode_step` (`set_lpf_parameters`): one mi for luma, `1 << ss` for
17589 // chroma -- the previous cell must keep the odd parity `plane_to_mi`17618 // chroma -- the previous cell must keep the odd parity `plane_to_mi`
@@ -18062,6 +18091,7 @@ mod deblock_simd {
18062 [0, 0, 1, 1, 1, 2, 1, 2],18091 [0, 0, 1, 1, 1, 2, 1, 2],
18063 [0, 0, 0, 1, 1, 1, 2, 3],18092 [0, 0, 0, 1, 1, 1, 2, 3],
18064 ];18093 ];
18094+ #[allow(dead_code)] // superseded by the running-sum rows; kept as the weights' statement
18065 const W14: [[i16; 14]; 12] = [18095 const W14: [[i16; 14]; 12] = [
18066 [7, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],18096 [7, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
18067 [5, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],18097 [5, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
@@ -18083,26 +18113,44 @@ mod deblock_simd {
18083 ///18113 ///
18084 /// # Safety18114 /// # Safety
18085 /// Requires AVX2.18115 /// Requires AVX2.
18116+ ///
18117+ /// lane-deblock: the tap count, the weights and the shift are all const
18118+ /// here (they were slice parameters, which left the weight `match` as a
18119+ /// runtime branch and a `movzwl` per tap -- `testl`/`je`/`movzwl` were
18120+ /// 34% of this kernel's samples, and the spilled tap slices another 17%).
18086 #[target_feature(enable = "avx2")]18121 #[target_feature(enable = "avx2")]
18087- #[inline]18122+ #[inline] // rust#145574: `inline(always)` is rejected on a `target_feature` fn
18088- unsafe fn wsum(taps: &[__m256i], w: &[i16], shift: i32) -> __m256i {18123+ unsafe fn wsum<const N: usize, const SHIFT: i32>(
18124+ taps: &[__m256i; N],
18125+ w: &[i16; N],
18126+ ) -> __m256i {
18089 let mut s = _mm256_setzero_si256();18127 let mut s = _mm256_setzero_si256();
18090- for (t, &k) in taps.iter().zip(w) {18128+ for i in 0..N {
18091- match k {18129+ match w[i] {
18092 0 => {}18130 0 => {}
18093- 1 => s = _mm256_add_epi16(s, *t),18131+ 1 => s = _mm256_add_epi16(s, taps[i]),
18094- 2 => s = _mm256_add_epi16(s, _mm256_add_epi16(*t, *t)),18132+ 2 => s = _mm256_add_epi16(s, _mm256_add_epi16(taps[i], taps[i])),
18095- _ => s = _mm256_add_epi16(s, _mm256_mullo_epi16(*t, _mm256_set1_epi16(k))),18133+ k => s = _mm256_add_epi16(s, _mm256_mullo_epi16(taps[i], _mm256_set1_epi16(k))),
18096 }18134 }
18097 }18135 }
18098- let s = _mm256_add_epi16(s, _mm256_set1_epi16(1 << (shift - 1)));18136+ let s = _mm256_add_epi16(s, _mm256_set1_epi16(1 << (SHIFT - 1)));
18099- if shift == 3 {18137+ if SHIFT == 3 {
18100 _mm256_srli_epi16(s, 3)18138 _mm256_srli_epi16(s, 3)
18101 } else {18139 } else {
18102 _mm256_srli_epi16(s, 4)18140 _mm256_srli_epi16(s, 4)
18103 }18141 }
18104 }18142 }
18105 18143
18144+ /// One wide-kernel row set, unrolled so each `wsum` sees its weight row
18145+ /// as a constant.
18146+ macro_rules! wrows {
18147+ ($r:expr, $off:expr, $tbl:ident, $taps:expr, $shift:literal, $flat:expr, $($j:literal),*) => {
18148+ $(
18149+ $r[$off + $j] = _mm256_blendv_epi8($r[$off + $j], wsum::<_, $shift>(&$taps, &$tbl[$j]), $flat);
18150+ )*
18151+ };
18152+ }
18153+
18106 /// [`super::filter_edge`]'s body for 16 lines held as `t[0..16]`, `q0` in18154 /// [`super::filter_edge`]'s body for 16 lines held as `t[0..16]`, `q0` in
18107 /// `t[8]`; the filtered taps are written back in place.18155 /// `t[8]`; the filtered taps are written back in place.
18108 ///18156 ///
@@ -18200,14 +18248,10 @@ mod deblock_simd {
18200 let flat = _mm256_and_si256(le(fl, fthr), mask);18248 let flat = _mm256_and_si256(le(fl, fthr), mask);
18201 if len == 6 {18249 if len == 6 {
18202 let taps = [p2, p1, p0, q0, q1, q2];18250 let taps = [p2, p1, p0, q0, q1, q2];
18203- for (j, w) in W6.iter().enumerate() {18251+ wrows!(r, 4, W6, taps, 3, flat, 0, 1, 2, 3);
18204- r[4 + j] = _mm256_blendv_epi8(r[4 + j], wsum(&taps, w, 3), flat);
18205- }
18206 } else {18252 } else {
18207 let taps = [p3, p2, p1, p0, q0, q1, q2, q3];18253 let taps = [p3, p2, p1, p0, q0, q1, q2, q3];
18208- for (j, w) in W8.iter().enumerate() {18254+ wrows!(r, 3, W8, taps, 3, flat, 0, 1, 2, 3, 4, 5);
18209- r[3 + j] = _mm256_blendv_epi8(r[3 + j], wsum(&taps, w, 3), flat);
18210- }
18211 if len > 8 {18255 if len > 8 {
18212 let fl2 = _mm256_max_epi16(18256 let fl2 = _mm256_max_epi16(
18213 _mm256_max_epi16(18257 _mm256_max_epi16(
@@ -18217,10 +18261,57 @@ mod deblock_simd {
18217 _mm256_max_epi16(ad(p6, p0), ad(q6, q0)),18261 _mm256_max_epi16(ad(p6, p0), ad(q6, q0)),
18218 );18262 );
18219 let flat2 = _mm256_and_si256(le(fl2, fthr), flat);18263 let flat2 = _mm256_and_si256(le(fl2, fthr), flat);
18220- let taps = [p6, p5, p4, p3, p2, p1, p0, q0, q1, q2, q3, q4, q5, q6];18264+ // lane-deblock: `filter14`'s twelve rows as a RUNNING
18221- for (j, w) in W14.iter().enumerate() {18265+ // sum. Consecutive `W14` rows differ by three or four
18222- r[j] = _mm256_blendv_epi8(r[j], wsum(&taps, w, 4), flat2);18266+ // taps (row j+1 - row j is a constant vector with two
18267+ // negative and two positive entries), so each output
18268+ // costs one add, one sub and the rounding shift instead
18269+ // of a fresh 14-tap weighted sum -- 168 tap operations
18270+ // become 44. Exact: every partial sum is taken mod
18271+ // 2^16 by `add/sub_epi16` and every ROW sum is a real
18272+ // `W14` row, at most `16 * 4095 + 8 < 2^16`, so the
18273+ // unsigned rounding shift sees the true value.
18274+ // `deblock_simd_kernel_matches_the_scalar_filter` pins
18275+ // it against `filter_edge` at 8/10/12-bit.
18276+ let mut s = _mm256_add_epi16(
18277+ _mm256_add_epi16(
18278+ _mm256_mullo_epi16(p6, _mm256_set1_epi16(7)),
18279+ _mm256_slli_epi16(_mm256_add_epi16(p5, p4), 1),
18280+ ),
18281+ _mm256_add_epi16(
18282+ _mm256_add_epi16(_mm256_add_epi16(p3, p2), _mm256_add_epi16(p1, p0)),
18283+ q0,
18284+ ),
18285+ );
18286+ macro_rules! row14 {
18287+ ($idx:literal) => {
18288+ r[$idx] = _mm256_blendv_epi8(
18289+ r[$idx],
18290+ _mm256_srli_epi16(
18291+ _mm256_add_epi16(s, _mm256_set1_epi16(8)),
18292+ 4,
18293+ ),
18294+ flat2,
18295+ );
18296+ };
18297+ ($idx:literal, $plus:expr, $minus:expr) => {
18298+ s = _mm256_sub_epi16(_mm256_add_epi16(s, $plus), $minus);
18299+ row14!($idx);
18300+ };
18223 }18301 }
18302+ let add2 = |a, b| _mm256_add_epi16(a, b);
18303+ row14!(0);
18304+ row14!(1, add2(p3, q1), _mm256_slli_epi16(p6, 1));
18305+ row14!(2, add2(p2, q2), add2(p6, p5));
18306+ row14!(3, add2(p1, q3), add2(p6, p4));
18307+ row14!(4, add2(p0, q4), add2(p6, p3));
18308+ row14!(5, add2(q0, q5), add2(p6, p2));
18309+ row14!(6, add2(q1, q6), add2(p6, p1));
18310+ row14!(7, add2(q2, q6), add2(p5, p0));
18311+ row14!(8, add2(q3, q6), add2(p4, q0));
18312+ row14!(9, add2(q4, q6), add2(p3, q1));
18313+ row14!(10, add2(q5, q6), add2(p2, q2));
18314+ row14!(11, _mm256_slli_epi16(q6, 1), add2(p1, q3));
18224 }18315 }
18225 }18316 }
18226 }18317 }