CDEF band body as a real function, so its plane writes are noalias
Browse files at this commit065f6373cc7062a7c2c3826d19fa5123da77eabeParent: 1a45793- perf
- CDEF band body as a real function, so its plane writes are noalias
1 changed file +145 -101
- crates/ec-av1/src/decode.rs +145 -101
crates/ec-av1/src/decode.rsmodified+145 -101
| @@ -17549,117 +17549,39 @@ fn scratch_return(v: Vec<u16>) { | |||
| 17549 | PLANE_SCRATCH.with(|p| p.borrow_mut().push(v)); | 17549 | PLANE_SCRATCH.with(|p| p.borrow_mut().push(v)); |
| 17550 | } | 17550 | } |
| 17551 | 17551 | ||
| 17552 | -fn apply_cdef( | 17552 | +/// One CDEF mi-row band, as a real function rather than the body of the |
| 17553 | +/// `run_bands` closure: inside the closure the destination planes are | ||
| 17554 | +/// `&mut` references derived from a `par::Shared` raw pointer in the same | ||
| 17555 | +/// scope as the `src_*` snapshot reads, so LLVM cannot tell the two apart and | ||
| 17556 | +/// re-loads through them on every tap. Reference parameters restore `noalias` | ||
| 17557 | +/// (lane-perf11 (a): -x% instructions at one filter thread). | ||
| 17558 | +#[allow(clippy::too_many_arguments)] | ||
| 17559 | +fn cdef_band( | ||
| 17560 | + r_lo: usize, | ||
| 17561 | + r_hi: usize, | ||
| 17553 | y: &mut PlaneBuf<'static>, | 17562 | y: &mut PlaneBuf<'static>, |
| 17554 | u: &mut PlaneBuf<'static>, | 17563 | u: &mut PlaneBuf<'static>, |
| 17555 | v: &mut PlaneBuf<'static>, | 17564 | v: &mut PlaneBuf<'static>, |
| 17565 | + src_y: &[u16], | ||
| 17566 | + src_u: &[u16], | ||
| 17567 | + src_v: &[u16], | ||
| 17556 | cdef: &CdefParams, | 17568 | cdef: &CdefParams, |
| 17557 | skip_grid: &Neighbours, | 17569 | skip_grid: &Neighbours, |
| 17558 | - // This frame header's own CROPPED luma size, for the straddling-unit | 17570 | + idx_grid: &[u8], |
| 17559 | - // counter only -- CDEF itself walks the mi-rounded plane. | 17571 | + sb_cols: usize, |
| 17560 | - (frame_w, frame_h): (usize, usize), fctx: &crate::decode::FrameCtx, | 17572 | + coeff_shift: i32, |
| 17573 | + damping: i32, | ||
| 17574 | + damping_uv: i32, | ||
| 17575 | + (frame_w, frame_h): (usize, usize), | ||
| 17576 | + fctx: &crate::decode::FrameCtx, | ||
| 17561 | ) { | 17577 | ) { |
| 17562 | - // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling. | ||
| 17563 | - if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") { | ||
| 17564 | - return; | ||
| 17565 | - } | ||
| 17566 | - // lane-cdefstrip r1 instrument, BEFORE either early return so it is valid | ||
| 17567 | - // for every frame: an 8x8 CDEF unit none of whose four mi cells a coded | ||
| 17568 | - // block wrote this frame. `Neighbours` seeds `skip_grid` all-false, so a | ||
| 17569 | - // decode arm that forgets `fill_skip_grid_rect` reads "not skip" and CDEF | ||
| 17570 | - // filters a unit libaom's `is_8x8_block_skip` (`cdef.c:29-38`) may exclude. | ||
| 17571 | - // Always 0 on every stream measured; a gate asserts it stays 0. | ||
| 17572 | - { | ||
| 17573 | - let mut r = 0usize; | ||
| 17574 | - while r < skip_grid.mi_rows { | ||
| 17575 | - let mut c = 0usize; | ||
| 17576 | - while c < skip_grid.mi_cols { | ||
| 17577 | - if (0..2).any(|rr| { | ||
| 17578 | - (0..2).any(|cc| { | ||
| 17579 | - !skip_grid | ||
| 17580 | - .skip_written | ||
| 17581 | - .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc)) | ||
| 17582 | - .copied() | ||
| 17583 | - .unwrap_or(true) | ||
| 17584 | - }) | ||
| 17585 | - }) { | ||
| 17586 | - CDEF_UNWRITTEN_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1)); | ||
| 17587 | - if crate::envflags::env_flag!("EC_AV1_DEBUG_CDEF_BAND") { | ||
| 17588 | - eprintln!("cdef band unwritten at mi ({r},{c}) px ({},{})", r * 4, c * 4); | ||
| 17589 | - } | ||
| 17590 | - } | ||
| 17591 | - let cell = |rr: usize, cc: usize| { | ||
| 17592 | - skip_grid | ||
| 17593 | - .skip_grid | ||
| 17594 | - .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc)) | ||
| 17595 | - .copied() | ||
| 17596 | - .unwrap_or(false) | ||
| 17597 | - }; | ||
| 17598 | - let first = cell(0, 0); | ||
| 17599 | - if (0..2).any(|rr| (0..2).any(|cc| cell(rr, cc) != first)) { | ||
| 17600 | - CDEF_MIXED_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1)); | ||
| 17601 | - } | ||
| 17602 | - c += 2; | ||
| 17603 | - } | ||
| 17604 | - r += 2; | ||
| 17605 | - } | ||
| 17606 | - } | ||
| 17607 | - // `cdef.bits == 0` still means "no filtering at all" whenever the single | ||
| 17608 | - // (index 0) strength pair is all-zero -- the fast path every existing | ||
| 17609 | - // gate stream takes. A `bits > 0` frame can still be all-zero-strength | ||
| 17610 | - // at every index, but that is rare enough not to special-case; the | ||
| 17611 | - // per-superblock lookup below degrades to the same index-0 read anyway | ||
| 17612 | - // when [`CDEF_IDX_GRID`] was never populated (`bits == 0`). | ||
| 17613 | - if cdef.bits == 0 | ||
| 17614 | - && cdef.y_pri_strength[0] == 0 | ||
| 17615 | - && cdef.y_sec_strength[0] == 0 | ||
| 17616 | - && cdef.uv_pri_strength[0] == 0 | ||
| 17617 | - && cdef.uv_sec_strength[0] == 0 | ||
| 17618 | - { | ||
| 17619 | - return; | ||
| 17620 | - } | ||
| 17621 | - let sb_cols = fctx.cdef_sb_cols.with(|c| c.get()); | ||
| 17622 | - // One entry per superblock (a few hundred at 4K), so this clone is not | ||
| 17623 | - // one of the per-frame copies lane-perf6 pooled below. | ||
| 17624 | - let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone()); | ||
| 17625 | let strength_idx = |mi_r: usize, mi_c: usize| -> usize { | 17578 | let strength_idx = |mi_r: usize, mi_c: usize| -> usize { |
| 17626 | if sb_cols == 0 { | 17579 | if sb_cols == 0 { |
| 17627 | return 0; | 17580 | return 0; |
| 17628 | } | 17581 | } |
| 17629 | let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); | 17582 | let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); |
| 17630 | - idx_grid | 17583 | + idx_grid.get(sb_r * sb_cols + sb_c).copied().unwrap_or(0) as usize |
| 17631 | - .get(sb_r * sb_cols + sb_c) | ||
| 17632 | - .copied() | ||
| 17633 | - .unwrap_or(0) as usize | ||
| 17634 | }; | 17584 | }; |
| 17635 | - let src_y = scratch_from(&y.data); | ||
| 17636 | - let src_u = scratch_from(&u.data); | ||
| 17637 | - let src_v = scratch_from(&v.data); | ||
| 17638 | - // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`, | ||
| 17639 | - // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`. | ||
| 17640 | - let coeff_shift = i32::from(bit_depth(fctx)) - 8; | ||
| 17641 | - let damping = i32::from(cdef.damping) + coeff_shift; | ||
| 17642 | - let damping_uv = (damping - 1).max(0); | ||
| 17643 | - // lane-filt1: every CDEF unit reads ONLY the pre-CDEF snapshots | ||
| 17644 | - // (`src_y`/`src_u`/`src_v`, immutable here) and writes its own 8x8 luma / | ||
| 17645 | - // 4x4 chroma square, so units are independent. Bands are mi ROW bands (2 | ||
| 17646 | - // mi = 8 luma rows, 4 chroma rows), which makes each band's writes a | ||
| 17647 | - // disjoint row range of all three planes. | ||
| 17648 | - let (sy, su, sv) = ( | ||
| 17649 | - crate::par::Shared::new(y), | ||
| 17650 | - crate::par::Shared::new(u), | ||
| 17651 | - crate::par::Shared::new(v), | ||
| 17652 | - ); | ||
| 17653 | - let mi_rows = skip_grid.mi_rows; | ||
| 17654 | - let cbands: Vec<(usize, usize)> = crate::par::bands(mi_rows.div_ceil(2), crate::par::filter_threads()) | ||
| 17655 | - .into_iter() | ||
| 17656 | - .map(|(a, b)| (a * 2, (b * 2).min(mi_rows))) | ||
| 17657 | - .collect(); | ||
| 17658 | - crate::par::run_bands(&cbands, fctx, |r_lo, r_hi, fctx| { | ||
| 17659 | - // SAFETY: this band writes only the plane rows its own mi rows cover, | ||
| 17660 | - // disjoint from every other band's (see `Shared`). | ||
| 17661 | - #[allow(unsafe_code)] | ||
| 17662 | - let (y, u, v) = unsafe { (sy.get(), su.get(), sv.get()) }; | ||
| 17663 | // One scratch window per plane kind, reused by every unit (8x8 luma and | 17585 | // One scratch window per plane kind, reused by every unit (8x8 luma and |
| 17664 | // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring). | 17586 | // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring). |
| 17665 | let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)]; | 17587 | let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)]; |
| @@ -17686,7 +17608,7 @@ fn apply_cdef( | |||
| 17686 | continue; | 17608 | continue; |
| 17687 | } | 17609 | } |
| 17688 | let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height); | 17610 | let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height); |
| 17689 | - cdef_gather(&src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y); | 17611 | + cdef_gather(src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y); |
| 17690 | let sample_y = |r: i32, c: i32| -> i32 { | 17612 | let sample_y = |r: i32, c: i32| -> i32 { |
| 17691 | win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER) | 17613 | win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER) |
| 17692 | + (c + CDEF_BORDER as i32) as usize] | 17614 | + (c + CDEF_BORDER as i32) as usize] |
| @@ -17751,7 +17673,7 @@ fn apply_cdef( | |||
| 17751 | let enable_secondary_uv = uv_sec_strength != 0; | 17673 | let enable_secondary_uv = uv_sec_strength != 0; |
| 17752 | if enable_primary_uv || enable_secondary_uv { | 17674 | if enable_primary_uv || enable_secondary_uv { |
| 17753 | let (cox, coy) = (ox / 2, oy / 2); | 17675 | let (cox, coy) = (ox / 2, oy / 2); |
| 17754 | - for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] { | 17676 | + for (plane, src_p) in [(&mut *u, src_u), (&mut *v, src_v)] { |
| 17755 | let (tw, th) = (plane.true_width, plane.true_height); | 17677 | let (tw, th) = (plane.true_width, plane.true_height); |
| 17756 | let stride = plane.width; | 17678 | let stride = plane.width; |
| 17757 | cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv); | 17679 | cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv); |
| @@ -17778,6 +17700,128 @@ fn apply_cdef( | |||
| 17778 | } | 17700 | } |
| 17779 | mi_r += 2; | 17701 | mi_r += 2; |
| 17780 | } | 17702 | } |
| 17703 | +} | ||
| 17704 | + | ||
| 17705 | +fn apply_cdef( | ||
| 17706 | + y: &mut PlaneBuf<'static>, | ||
| 17707 | + u: &mut PlaneBuf<'static>, | ||
| 17708 | + v: &mut PlaneBuf<'static>, | ||
| 17709 | + cdef: &CdefParams, | ||
| 17710 | + skip_grid: &Neighbours, | ||
| 17711 | + // This frame header's own CROPPED luma size, for the straddling-unit | ||
| 17712 | + // counter only -- CDEF itself walks the mi-rounded plane. | ||
| 17713 | + (frame_w, frame_h): (usize, usize), fctx: &crate::decode::FrameCtx, | ||
| 17714 | +) { | ||
| 17715 | + // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling. | ||
| 17716 | + if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") { | ||
| 17717 | + return; | ||
| 17718 | + } | ||
| 17719 | + // lane-cdefstrip r1 instrument, BEFORE either early return so it is valid | ||
| 17720 | + // for every frame: an 8x8 CDEF unit none of whose four mi cells a coded | ||
| 17721 | + // block wrote this frame. `Neighbours` seeds `skip_grid` all-false, so a | ||
| 17722 | + // decode arm that forgets `fill_skip_grid_rect` reads "not skip" and CDEF | ||
| 17723 | + // filters a unit libaom's `is_8x8_block_skip` (`cdef.c:29-38`) may exclude. | ||
| 17724 | + // Always 0 on every stream measured; a gate asserts it stays 0. | ||
| 17725 | + { | ||
| 17726 | + let mut r = 0usize; | ||
| 17727 | + while r < skip_grid.mi_rows { | ||
| 17728 | + let mut c = 0usize; | ||
| 17729 | + while c < skip_grid.mi_cols { | ||
| 17730 | + if (0..2).any(|rr| { | ||
| 17731 | + (0..2).any(|cc| { | ||
| 17732 | + !skip_grid | ||
| 17733 | + .skip_written | ||
| 17734 | + .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc)) | ||
| 17735 | + .copied() | ||
| 17736 | + .unwrap_or(true) | ||
| 17737 | + }) | ||
| 17738 | + }) { | ||
| 17739 | + CDEF_UNWRITTEN_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1)); | ||
| 17740 | + if crate::envflags::env_flag!("EC_AV1_DEBUG_CDEF_BAND") { | ||
| 17741 | + eprintln!("cdef band unwritten at mi ({r},{c}) px ({},{})", r * 4, c * 4); | ||
| 17742 | + } | ||
| 17743 | + } | ||
| 17744 | + let cell = |rr: usize, cc: usize| { | ||
| 17745 | + skip_grid | ||
| 17746 | + .skip_grid | ||
| 17747 | + .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc)) | ||
| 17748 | + .copied() | ||
| 17749 | + .unwrap_or(false) | ||
| 17750 | + }; | ||
| 17751 | + let first = cell(0, 0); | ||
| 17752 | + if (0..2).any(|rr| (0..2).any(|cc| cell(rr, cc) != first)) { | ||
| 17753 | + CDEF_MIXED_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1)); | ||
| 17754 | + } | ||
| 17755 | + c += 2; | ||
| 17756 | + } | ||
| 17757 | + r += 2; | ||
| 17758 | + } | ||
| 17759 | + } | ||
| 17760 | + // `cdef.bits == 0` still means "no filtering at all" whenever the single | ||
| 17761 | + // (index 0) strength pair is all-zero -- the fast path every existing | ||
| 17762 | + // gate stream takes. A `bits > 0` frame can still be all-zero-strength | ||
| 17763 | + // at every index, but that is rare enough not to special-case; the | ||
| 17764 | + // per-superblock lookup below degrades to the same index-0 read anyway | ||
| 17765 | + // when [`CDEF_IDX_GRID`] was never populated (`bits == 0`). | ||
| 17766 | + if cdef.bits == 0 | ||
| 17767 | + && cdef.y_pri_strength[0] == 0 | ||
| 17768 | + && cdef.y_sec_strength[0] == 0 | ||
| 17769 | + && cdef.uv_pri_strength[0] == 0 | ||
| 17770 | + && cdef.uv_sec_strength[0] == 0 | ||
| 17771 | + { | ||
| 17772 | + return; | ||
| 17773 | + } | ||
| 17774 | + let sb_cols = fctx.cdef_sb_cols.with(|c| c.get()); | ||
| 17775 | + // One entry per superblock (a few hundred at 4K), so this clone is not | ||
| 17776 | + // one of the per-frame copies lane-perf6 pooled below. | ||
| 17777 | + let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone()); | ||
| 17778 | + let src_y = scratch_from(&y.data); | ||
| 17779 | + let src_u = scratch_from(&u.data); | ||
| 17780 | + let src_v = scratch_from(&v.data); | ||
| 17781 | + // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`, | ||
| 17782 | + // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`. | ||
| 17783 | + let coeff_shift = i32::from(bit_depth(fctx)) - 8; | ||
| 17784 | + let damping = i32::from(cdef.damping) + coeff_shift; | ||
| 17785 | + let damping_uv = (damping - 1).max(0); | ||
| 17786 | + // lane-filt1: every CDEF unit reads ONLY the pre-CDEF snapshots | ||
| 17787 | + // (`src_y`/`src_u`/`src_v`, immutable here) and writes its own 8x8 luma / | ||
| 17788 | + // 4x4 chroma square, so units are independent. Bands are mi ROW bands (2 | ||
| 17789 | + // mi = 8 luma rows, 4 chroma rows), which makes each band's writes a | ||
| 17790 | + // disjoint row range of all three planes. | ||
| 17791 | + let (sy, su, sv) = ( | ||
| 17792 | + crate::par::Shared::new(y), | ||
| 17793 | + crate::par::Shared::new(u), | ||
| 17794 | + crate::par::Shared::new(v), | ||
| 17795 | + ); | ||
| 17796 | + let mi_rows = skip_grid.mi_rows; | ||
| 17797 | + let cbands: Vec<(usize, usize)> = crate::par::bands(mi_rows.div_ceil(2), crate::par::filter_threads()) | ||
| 17798 | + .into_iter() | ||
| 17799 | + .map(|(a, b)| (a * 2, (b * 2).min(mi_rows))) | ||
| 17800 | + .collect(); | ||
| 17801 | + crate::par::run_bands(&cbands, fctx, |r_lo, r_hi, fctx| { | ||
| 17802 | + // SAFETY: this band writes only the plane rows its own mi rows cover, | ||
| 17803 | + // disjoint from every other band's (see `Shared`). | ||
| 17804 | + #[allow(unsafe_code)] | ||
| 17805 | + let (y, u, v) = unsafe { (sy.get(), su.get(), sv.get()) }; | ||
| 17806 | + cdef_band( | ||
| 17807 | + r_lo, | ||
| 17808 | + r_hi, | ||
| 17809 | + y, | ||
| 17810 | + u, | ||
| 17811 | + v, | ||
| 17812 | + &src_y, | ||
| 17813 | + &src_u, | ||
| 17814 | + &src_v, | ||
| 17815 | + cdef, | ||
| 17816 | + skip_grid, | ||
| 17817 | + &idx_grid, | ||
| 17818 | + sb_cols, | ||
| 17819 | + coeff_shift, | ||
| 17820 | + damping, | ||
| 17821 | + damping_uv, | ||
| 17822 | + (frame_w, frame_h), | ||
| 17823 | + fctx, | ||
| 17824 | + ); | ||
| 17781 | }); | 17825 | }); |
| 17782 | scratch_return(src_y); | 17826 | scratch_return(src_y); |
| 17783 | scratch_return(src_u); | 17827 | scratch_return(src_u); |