forge
Explore
Log in

Don't you dare go Hollow

The square coefficient loop's trace sites move out of line

Browse files at this commit

70a2869fda23bc260a8da9c75e685a75025d88a5Ahmet Kaan GümüşParent: de3e402
  • perf
    • the square coefficient loop's trace sites move out of line
Show full message
perf(av1): the square coefficient loop's trace sites move out of line

`eprintln!` builds its `fmt::Arguments` on the stack at the call site, so the
six trace sites inside `read_coeffs`'s base and sign loops kept their values
pinned in the hot loop's frame -- the annotated profile's top line was stack
spill traffic. Each is a `#[cold] #[inline(never)]` call taking the values it
prints; the output is character-for-character what it was.

instructions:u at EC_AV1_THREADS=1: 4K segment -0.25%, 1080p segment -0.32%.

The same treatment on `read_coeffs_rect`'s four sites plus the two remaining
`TRACE` sites of the sign pass measured WORSE (4K +0.04%, 1080p +0.05%,
reproduced in two interleaved pairs on both segments) and was reverted by
inverse edit -- those loops are short enough that the extra call sequences
cost more than the frame they free.
crates/ec-av1/src/decode.rsmodified+68 -25
@@ -5948,6 +5948,65 @@ fn is_smooth_mode(mode: usize) -> bool {
5948 (crate::intra::SMOOTH_PRED as usize..=crate::intra::SMOOTH_H_PRED as usize).contains(&mode)5948 (crate::intra::SMOOTH_PRED as usize..=crate::intra::SMOOTH_H_PRED as usize).contains(&mode)
5949 }5949 }
5950 5950
5951+/// lane-coefread: the per-coefficient trace sites, one `#[cold]` call each.
5952+/// `eprintln!` builds its `fmt::Arguments` on the stack AT THE CALL SITE, so
5953+/// nine of them inside the base/sign loops pinned nine sets of values in the
5954+/// hot loop's frame (the annotated profile was full of `mov ...(%rsp)` spill
5955+/// traffic). Out of line, the loop keeps its values in registers and the
5956+/// trace output is character-for-character what it was.
5957+#[cold]
5958+#[inline(never)]
5959+fn trace_base_eob(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, v: i32) {
5960+ eprintln!("TRACE base_eob scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}");
5961+}
5962+
5963+#[cold]
5964+#[inline(never)]
5965+fn trace_base(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, v: i32) {
5966+ eprintln!("TRACE base scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}");
5967+}
5968+
5969+#[cold]
5970+#[inline(never)]
5971+fn trace_br(scan_idx: usize, pos: usize, row: usize, col: usize, ctx: usize, k: i32) {
5972+ eprintln!("TRACE br scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={k}");
5973+}
5974+
5975+#[cold]
5976+#[inline(never)]
5977+fn coeff_step_base(dec: &SymbolDecoder, scan_idx: usize, apos: usize, ctx: usize, v: i32) {
5978+ let (rng, _) = dec.debug_state();
5979+ eprintln!("EC_COEFF_STEP tag=base c={scan_idx} pos={apos} ctx={ctx} level={v} rng={rng}");
5980+}
5981+
5982+#[cold]
5983+#[inline(never)]
5984+fn coeff_step_br(dec: &SymbolDecoder, scan_idx: usize, apos: usize, ctx: usize, k: i32) {
5985+ let (rng, _) = dec.debug_state();
5986+ eprintln!("EC_COEFF_STEP tag=br c={scan_idx} pos={apos} ctx={ctx} k={k} rng={rng}");
5987+}
5988+
5989+#[cold]
5990+#[inline(never)]
5991+fn coeff_step_sign(dec: &SymbolDecoder, c: usize, negative: bool) {
5992+ let (rng, _) = dec.debug_state();
5993+ eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32);
5994+}
5995+
5996+#[cold]
5997+#[inline(never)]
5998+fn coeff_step_post_golomb(dec: &SymbolDecoder, c: usize, level: i32) {
5999+ let (rng, _) = dec.debug_state();
6000+ eprintln!("EC_COEFF_STEP tag=post_golomb c={c} level={level} rng={rng}");
6001+}
6002+
6003+#[cold]
6004+#[inline(never)]
6005+fn coeff_step_base_eob_level(dec: &SymbolDecoder, level: i32) {
6006+ let (rng, _) = dec.debug_state();
6007+ eprintln!("EC_COEFF_STEP tag=base_eob level={level} rng={rng}");
6008+}
6009+
5951 fn read_coeffs(6010 fn read_coeffs(
5952 dec: &mut SymbolDecoder,6011 dec: &mut SymbolDecoder,
5953 coding: &mut TxbTables,6012 coding: &mut TxbTables,
@@ -6093,9 +6152,7 @@ fn read_coeffs(
6093 let ctx = eob_coeff_ctx(scan_idx, side * side);6152 let ctx = eob_coeff_ctx(scan_idx, side * side);
6094 let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1;6153 let v = dec.symbol(&mut coding.base_eob[ctx]) as i32 + 1;
6095 if trace {6154 if trace {
6096- eprintln!(6155+ trace_base_eob(scan_idx, pos, row, col, ctx, v);
6097- "TRACE base_eob scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}"
6098- );
6099 }6156 }
6100 v6157 v
6101 } else {6158 } else {
@@ -6111,18 +6168,12 @@ fn read_coeffs(
6111 };6168 };
6112 let v = dec.symbol(&mut coding.base[ctx]) as i32;6169 let v = dec.symbol(&mut coding.base[ctx]) as i32;
6113 if ec_trace_coeff {6170 if ec_trace_coeff {
6114- let (rng, _) = dec.debug_state();
6115 // `pos` in libaom's column-major `coeff_idx` convention, so6171 // `pos` in libaom's column-major `coeff_idx` convention, so
6116 // the ladder lines up with instrumented `aomdec`.6172 // the ladder lines up with instrumented `aomdec`.
6117- let apos = col * side + row;6173+ coeff_step_base(dec, scan_idx, col * side + row, ctx, v);
6118- eprintln!(
6119- "EC_COEFF_STEP tag=base c={scan_idx} pos={apos} ctx={ctx} level={v} rng={rng}"
6120- );
6121 }6174 }
6122 if trace {6175 if trace {
6123- eprintln!(6176+ trace_base(scan_idx, pos, row, col, ctx, v);
6124- "TRACE base scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}"
6125- );
6126 }6177 }
6127 v6178 v
6128 };6179 };
@@ -6149,16 +6200,10 @@ fn read_coeffs(
6149 // path's trace, so the cross-decoder range ladder was6200 // path's trace, so the cross-decoder range ladder was
6150 // blind to every base-range symbol and mis-reported the6201 // blind to every base-range symbol and mis-reported the
6151 // first divergence (class [[gate-blind-to-feature]]).6202 // first divergence (class [[gate-blind-to-feature]]).
6152- let (rng, _) = dec.debug_state();6203+ coeff_step_br(dec, scan_idx, col * side + row, ctx, k);
6153- let apos = col * side + row;
6154- eprintln!(
6155- "EC_COEFF_STEP tag=br c={scan_idx} pos={apos} ctx={ctx} k={k} rng={rng}"
6156- );
6157 }6204 }
6158 if trace {6205 if trace {
6159- eprintln!(6206+ trace_br(scan_idx, pos, row, col, ctx, k);
6160- "TRACE br scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={k}"
6161- );
6162 }6207 }
6163 level += k;6208 level += k;
6164 sent += BR_STEP;6209 sent += BR_STEP;
@@ -6171,8 +6216,7 @@ fn read_coeffs(
6171 level6216 level
6172 };6217 };
6173 if ec_trace_coeff && scan_idx == eob - 1 {6218 if ec_trace_coeff && scan_idx == eob - 1 {
6174- let (rng, _) = dec.debug_state();6219+ coeff_step_base_eob_level(dec, level);
6175- eprintln!("EC_COEFF_STEP tag=base_eob level={level} rng={rng}");
6176 }6220 }
6177 // Every coded level here is a magnitude in `0..=15` (base <= 3 plus6221 // Every coded level here is a magnitude in `0..=15` (base <= 3 plus
6178 // at most `COEFF_BASE_RANGE` from the `br` loop; the Golomb tail is6222 // at most `COEFF_BASE_RANGE` from the `br` loop; the Golomb tail is
@@ -6205,8 +6249,7 @@ fn read_coeffs(
6205 dec.literal(1) == 16249 dec.literal(1) == 1
6206 };6250 };
6207 if ec_trace_coeff {6251 if ec_trace_coeff {
6208- let (rng, _) = dec.debug_state();6252+ coeff_step_sign(dec, c, negative);
6209- eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32);
6210 }6253 }
6211 let level = if level > MAX_BR_LEVEL {6254 let level = if level > MAX_BR_LEVEL {
6212 let g = read_golomb(dec)?;6255 let g = read_golomb(dec)?;
@@ -6218,8 +6261,7 @@ fn read_coeffs(
6218 level6261 level
6219 };6262 };
6220 if ec_trace_coeff {6263 if ec_trace_coeff {
6221- let (rng, _) = dec.debug_state();6264+ coeff_step_post_golomb(dec, c, level);
6222- eprintln!("EC_COEFF_STEP tag=post_golomb c={c} level={level} rng={rng}");
6223 }6265 }
6224 grid[pos] = if negative { -level } else { level };6266 grid[pos] = if negative { -level } else { level };
6225 }6267 }
@@ -6234,6 +6276,7 @@ fn read_coeffs(
6234 /// rather than guess-decode if a non-2D class ever shows up here, since6276 /// rather than guess-decode if a non-2D class ever shows up here, since
6235 /// neither `base_ctx_rect`/`br_ctx_rect` nor [`class_scan_table`] have a rect6277 /// neither `base_ctx_rect`/`br_ctx_rect` nor [`class_scan_table`] have a rect
6236 /// form for those.6278 /// form for those.
6279+
6237 fn read_coeffs_rect(6280 fn read_coeffs_rect(
6238 dec: &mut SymbolDecoder,6281 dec: &mut SymbolDecoder,
6239 coding: &mut TxbTables,6282 coding: &mut TxbTables,