forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-perf11' (2f7fcb64): CDEF band body as a real function with reference parameters (alias facts restored); byte-exact at 1 and 4 filter threads, 0.9% fewer instructions at 4K

Browse files at this commit

f97a50b4c59e4f5041b5e447de49cbab997aa4caAhmet Kaan GümüşParent: 34a3d01065f637
crates/ec-av1/src/decode.rsmodified+145 -101
@@ -18215,117 +18215,39 @@ fn scratch_return(v: Vec<u16>) {
1821518215 PLANE_SCRATCH.with(|p| p.borrow_mut().push(v));
1821618216 }
1821718217
18218-fn apply_cdef(
18218+/// One CDEF mi-row band, as a real function rather than the body of the
18219+/// `run_bands` closure: inside the closure the destination planes are
18220+/// `&mut` references derived from a `par::Shared` raw pointer in the same
18221+/// scope as the `src_*` snapshot reads, so LLVM cannot tell the two apart and
18222+/// re-loads through them on every tap. Reference parameters restore `noalias`
18223+/// (lane-perf11 (a): -x% instructions at one filter thread).
18224+#[allow(clippy::too_many_arguments)]
18225+fn cdef_band(
18226+ r_lo: usize,
18227+ r_hi: usize,
1821918228 y: &mut PlaneBuf<'static>,
1822018229 u: &mut PlaneBuf<'static>,
1822118230 v: &mut PlaneBuf<'static>,
18231+ src_y: &[u16],
18232+ src_u: &[u16],
18233+ src_v: &[u16],
1822218234 cdef: &CdefParams,
1822318235 skip_grid: &Neighbours,
18224- // This frame header's own CROPPED luma size, for the straddling-unit
18225- // counter only -- CDEF itself walks the mi-rounded plane.
18226- (frame_w, frame_h): (usize, usize), fctx: &crate::decode::FrameCtx,
18236+ idx_grid: &[u8],
18237+ sb_cols: usize,
18238+ coeff_shift: i32,
18239+ damping: i32,
18240+ damping_uv: i32,
18241+ (frame_w, frame_h): (usize, usize),
18242+ fctx: &crate::decode::FrameCtx,
1822718243 ) {
18228- // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling.
18229- if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") {
18230- return;
18231- }
18232- // lane-cdefstrip r1 instrument, BEFORE either early return so it is valid
18233- // for every frame: an 8x8 CDEF unit none of whose four mi cells a coded
18234- // block wrote this frame. `Neighbours` seeds `skip_grid` all-false, so a
18235- // decode arm that forgets `fill_skip_grid_rect` reads "not skip" and CDEF
18236- // filters a unit libaom's `is_8x8_block_skip` (`cdef.c:29-38`) may exclude.
18237- // Always 0 on every stream measured; a gate asserts it stays 0.
18238- {
18239- let mut r = 0usize;
18240- while r < skip_grid.mi_rows {
18241- let mut c = 0usize;
18242- while c < skip_grid.mi_cols {
18243- if (0..2).any(|rr| {
18244- (0..2).any(|cc| {
18245- !skip_grid
18246- .skip_written
18247- .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc))
18248- .copied()
18249- .unwrap_or(true)
18250- })
18251- }) {
18252- CDEF_UNWRITTEN_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1));
18253- if crate::envflags::env_flag!("EC_AV1_DEBUG_CDEF_BAND") {
18254- eprintln!("cdef band unwritten at mi ({r},{c}) px ({},{})", r * 4, c * 4);
18255- }
18256- }
18257- let cell = |rr: usize, cc: usize| {
18258- skip_grid
18259- .skip_grid
18260- .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc))
18261- .copied()
18262- .unwrap_or(false)
18263- };
18264- let first = cell(0, 0);
18265- if (0..2).any(|rr| (0..2).any(|cc| cell(rr, cc) != first)) {
18266- CDEF_MIXED_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1));
18267- }
18268- c += 2;
18269- }
18270- r += 2;
18271- }
18272- }
18273- // `cdef.bits == 0` still means "no filtering at all" whenever the single
18274- // (index 0) strength pair is all-zero -- the fast path every existing
18275- // gate stream takes. A `bits > 0` frame can still be all-zero-strength
18276- // at every index, but that is rare enough not to special-case; the
18277- // per-superblock lookup below degrades to the same index-0 read anyway
18278- // when [`CDEF_IDX_GRID`] was never populated (`bits == 0`).
18279- if cdef.bits == 0
18280- && cdef.y_pri_strength[0] == 0
18281- && cdef.y_sec_strength[0] == 0
18282- && cdef.uv_pri_strength[0] == 0
18283- && cdef.uv_sec_strength[0] == 0
18284- {
18285- return;
18286- }
18287- let sb_cols = fctx.cdef_sb_cols.with(|c| c.get());
18288- // One entry per superblock (a few hundred at 4K), so this clone is not
18289- // one of the per-frame copies lane-perf6 pooled below.
18290- let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone());
1829118244 let strength_idx = |mi_r: usize, mi_c: usize| -> usize {
1829218245 if sb_cols == 0 {
1829318246 return 0;
1829418247 }
1829518248 let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize);
18296- idx_grid
18297- .get(sb_r * sb_cols + sb_c)
18298- .copied()
18299- .unwrap_or(0) as usize
18249+ idx_grid.get(sb_r * sb_cols + sb_c).copied().unwrap_or(0) as usize
1830018250 };
18301- let src_y = scratch_from(&y.data);
18302- let src_u = scratch_from(&u.data);
18303- let src_v = scratch_from(&v.data);
18304- // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`,
18305- // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`.
18306- let coeff_shift = i32::from(bit_depth(fctx)) - 8;
18307- let damping = i32::from(cdef.damping) + coeff_shift;
18308- let damping_uv = (damping - 1).max(0);
18309- // lane-filt1: every CDEF unit reads ONLY the pre-CDEF snapshots
18310- // (`src_y`/`src_u`/`src_v`, immutable here) and writes its own 8x8 luma /
18311- // 4x4 chroma square, so units are independent. Bands are mi ROW bands (2
18312- // mi = 8 luma rows, 4 chroma rows), which makes each band's writes a
18313- // disjoint row range of all three planes.
18314- let (sy, su, sv) = (
18315- crate::par::Shared::new(y),
18316- crate::par::Shared::new(u),
18317- crate::par::Shared::new(v),
18318- );
18319- let mi_rows = skip_grid.mi_rows;
18320- let cbands: Vec<(usize, usize)> = crate::par::bands(mi_rows.div_ceil(2), crate::par::filter_threads())
18321- .into_iter()
18322- .map(|(a, b)| (a * 2, (b * 2).min(mi_rows)))
18323- .collect();
18324- crate::par::run_bands(&cbands, fctx, |r_lo, r_hi, fctx| {
18325- // SAFETY: this band writes only the plane rows its own mi rows cover,
18326- // disjoint from every other band's (see `Shared`).
18327- #[allow(unsafe_code)]
18328- let (y, u, v) = unsafe { (sy.get(), su.get(), sv.get()) };
1832918251 // One scratch window per plane kind, reused by every unit (8x8 luma and
1833018252 // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring).
1833118253 let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)];
@@ -18352,7 +18274,7 @@ fn apply_cdef(
1835218274 continue;
1835318275 }
1835418276 let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height);
18355- cdef_gather(&src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y);
18277+ cdef_gather(src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y);
1835618278 let sample_y = |r: i32, c: i32| -> i32 {
1835718279 win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER)
1835818280 + (c + CDEF_BORDER as i32) as usize]
@@ -18417,7 +18339,7 @@ fn apply_cdef(
1841718339 let enable_secondary_uv = uv_sec_strength != 0;
1841818340 if enable_primary_uv || enable_secondary_uv {
1841918341 let (cox, coy) = (ox / 2, oy / 2);
18420- for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] {
18342+ for (plane, src_p) in [(&mut *u, src_u), (&mut *v, src_v)] {
1842118343 let (tw, th) = (plane.true_width, plane.true_height);
1842218344 let stride = plane.width;
1842318345 cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv);
@@ -18444,6 +18366,128 @@ fn apply_cdef(
1844418366 }
1844518367 mi_r += 2;
1844618368 }
18369+}
18370+
18371+fn apply_cdef(
18372+ y: &mut PlaneBuf<'static>,
18373+ u: &mut PlaneBuf<'static>,
18374+ v: &mut PlaneBuf<'static>,
18375+ cdef: &CdefParams,
18376+ skip_grid: &Neighbours,
18377+ // This frame header's own CROPPED luma size, for the straddling-unit
18378+ // counter only -- CDEF itself walks the mi-rounded plane.
18379+ (frame_w, frame_h): (usize, usize), fctx: &crate::decode::FrameCtx,
18380+) {
18381+ // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling.
18382+ if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") {
18383+ return;
18384+ }
18385+ // lane-cdefstrip r1 instrument, BEFORE either early return so it is valid
18386+ // for every frame: an 8x8 CDEF unit none of whose four mi cells a coded
18387+ // block wrote this frame. `Neighbours` seeds `skip_grid` all-false, so a
18388+ // decode arm that forgets `fill_skip_grid_rect` reads "not skip" and CDEF
18389+ // filters a unit libaom's `is_8x8_block_skip` (`cdef.c:29-38`) may exclude.
18390+ // Always 0 on every stream measured; a gate asserts it stays 0.
18391+ {
18392+ let mut r = 0usize;
18393+ while r < skip_grid.mi_rows {
18394+ let mut c = 0usize;
18395+ while c < skip_grid.mi_cols {
18396+ if (0..2).any(|rr| {
18397+ (0..2).any(|cc| {
18398+ !skip_grid
18399+ .skip_written
18400+ .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc))
18401+ .copied()
18402+ .unwrap_or(true)
18403+ })
18404+ }) {
18405+ CDEF_UNWRITTEN_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1));
18406+ if crate::envflags::env_flag!("EC_AV1_DEBUG_CDEF_BAND") {
18407+ eprintln!("cdef band unwritten at mi ({r},{c}) px ({},{})", r * 4, c * 4);
18408+ }
18409+ }
18410+ let cell = |rr: usize, cc: usize| {
18411+ skip_grid
18412+ .skip_grid
18413+ .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc))
18414+ .copied()
18415+ .unwrap_or(false)
18416+ };
18417+ let first = cell(0, 0);
18418+ if (0..2).any(|rr| (0..2).any(|cc| cell(rr, cc) != first)) {
18419+ CDEF_MIXED_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1));
18420+ }
18421+ c += 2;
18422+ }
18423+ r += 2;
18424+ }
18425+ }
18426+ // `cdef.bits == 0` still means "no filtering at all" whenever the single
18427+ // (index 0) strength pair is all-zero -- the fast path every existing
18428+ // gate stream takes. A `bits > 0` frame can still be all-zero-strength
18429+ // at every index, but that is rare enough not to special-case; the
18430+ // per-superblock lookup below degrades to the same index-0 read anyway
18431+ // when [`CDEF_IDX_GRID`] was never populated (`bits == 0`).
18432+ if cdef.bits == 0
18433+ && cdef.y_pri_strength[0] == 0
18434+ && cdef.y_sec_strength[0] == 0
18435+ && cdef.uv_pri_strength[0] == 0
18436+ && cdef.uv_sec_strength[0] == 0
18437+ {
18438+ return;
18439+ }
18440+ let sb_cols = fctx.cdef_sb_cols.with(|c| c.get());
18441+ // One entry per superblock (a few hundred at 4K), so this clone is not
18442+ // one of the per-frame copies lane-perf6 pooled below.
18443+ let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone());
18444+ let src_y = scratch_from(&y.data);
18445+ let src_u = scratch_from(&u.data);
18446+ let src_v = scratch_from(&v.data);
18447+ // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`,
18448+ // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`.
18449+ let coeff_shift = i32::from(bit_depth(fctx)) - 8;
18450+ let damping = i32::from(cdef.damping) + coeff_shift;
18451+ let damping_uv = (damping - 1).max(0);
18452+ // lane-filt1: every CDEF unit reads ONLY the pre-CDEF snapshots
18453+ // (`src_y`/`src_u`/`src_v`, immutable here) and writes its own 8x8 luma /
18454+ // 4x4 chroma square, so units are independent. Bands are mi ROW bands (2
18455+ // mi = 8 luma rows, 4 chroma rows), which makes each band's writes a
18456+ // disjoint row range of all three planes.
18457+ let (sy, su, sv) = (
18458+ crate::par::Shared::new(y),
18459+ crate::par::Shared::new(u),
18460+ crate::par::Shared::new(v),
18461+ );
18462+ let mi_rows = skip_grid.mi_rows;
18463+ let cbands: Vec<(usize, usize)> = crate::par::bands(mi_rows.div_ceil(2), crate::par::filter_threads())
18464+ .into_iter()
18465+ .map(|(a, b)| (a * 2, (b * 2).min(mi_rows)))
18466+ .collect();
18467+ crate::par::run_bands(&cbands, fctx, |r_lo, r_hi, fctx| {
18468+ // SAFETY: this band writes only the plane rows its own mi rows cover,
18469+ // disjoint from every other band's (see `Shared`).
18470+ #[allow(unsafe_code)]
18471+ let (y, u, v) = unsafe { (sy.get(), su.get(), sv.get()) };
18472+ cdef_band(
18473+ r_lo,
18474+ r_hi,
18475+ y,
18476+ u,
18477+ v,
18478+ &src_y,
18479+ &src_u,
18480+ &src_v,
18481+ cdef,
18482+ skip_grid,
18483+ &idx_grid,
18484+ sb_cols,
18485+ coeff_shift,
18486+ damping,
18487+ damping_uv,
18488+ (frame_w, frame_h),
18489+ fctx,
18490+ );
1844718491 });
1844818492 scratch_return(src_y);
1844918493 scratch_return(src_u);