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 commit1 changed file +145 -101
- crates/ec-av1/src/decode.rs +145 -101
crates/ec-av1/src/decode.rsmodified+145 -101
| @@ -18215,117 +18215,39 @@ fn scratch_return(v: Vec<u16>) { | ||
| 18215 | 18215 | PLANE_SCRATCH.with(|p| p.borrow_mut().push(v)); |
| 18216 | 18216 | } |
| 18217 | 18217 | |
| 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, | |
| 18219 | 18228 | y: &mut PlaneBuf<'static>, |
| 18220 | 18229 | u: &mut PlaneBuf<'static>, |
| 18221 | 18230 | v: &mut PlaneBuf<'static>, |
| 18231 | + src_y: &[u16], | |
| 18232 | + src_u: &[u16], | |
| 18233 | + src_v: &[u16], | |
| 18222 | 18234 | cdef: &CdefParams, |
| 18223 | 18235 | 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, | |
| 18227 | 18243 | ) { |
| 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()); | |
| 18291 | 18244 | let strength_idx = |mi_r: usize, mi_c: usize| -> usize { |
| 18292 | 18245 | if sb_cols == 0 { |
| 18293 | 18246 | return 0; |
| 18294 | 18247 | } |
| 18295 | 18248 | 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 | |
| 18300 | 18250 | }; |
| 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()) }; | |
| 18329 | 18251 | // One scratch window per plane kind, reused by every unit (8x8 luma and |
| 18330 | 18252 | // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring). |
| 18331 | 18253 | let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)]; |
| @@ -18352,7 +18274,7 @@ fn apply_cdef( | ||
| 18352 | 18274 | continue; |
| 18353 | 18275 | } |
| 18354 | 18276 | 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); | |
| 18356 | 18278 | let sample_y = |r: i32, c: i32| -> i32 { |
| 18357 | 18279 | win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER) |
| 18358 | 18280 | + (c + CDEF_BORDER as i32) as usize] |
| @@ -18417,7 +18339,7 @@ fn apply_cdef( | ||
| 18417 | 18339 | let enable_secondary_uv = uv_sec_strength != 0; |
| 18418 | 18340 | if enable_primary_uv || enable_secondary_uv { |
| 18419 | 18341 | 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)] { | |
| 18421 | 18343 | let (tw, th) = (plane.true_width, plane.true_height); |
| 18422 | 18344 | let stride = plane.width; |
| 18423 | 18345 | cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv); |
| @@ -18444,6 +18366,128 @@ fn apply_cdef( | ||
| 18444 | 18366 | } |
| 18445 | 18367 | mi_r += 2; |
| 18446 | 18368 | } |
| 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 | + ); | |
| 18447 | 18491 | }); |
| 18448 | 18492 | scratch_return(src_y); |
| 18449 | 18493 | scratch_return(src_u); |