Skip to content

fix(callgrind): model REP copies with calibrated bulk costs - #60

Merged
not-matthias merged 2 commits into
masterfrom
rep-bulk-cost-model
Nov 10, 2026
Merged

not-matthias merged 2 commits into
masterfrom
rep-bulk-cost-model

Conversation

@not-matthias

@not-matthias not-matthias commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Price amd64 REP MOVS/STOS through 48 measured curves covering widths, directions, and alignment classes instead of per-element instruction costs.
  • Carry the full hot-copy cost in existing Ct/Cl columns. Suppress only REP data L1-hit references; preserve instruction counters, cache simulation, and every data miss.
  • Enable with cache simulation and cycle estimation. --rep-bulk-cost=no restores legacy accounting. No new output events or format changes.
  • The table is generated out of tree from Zen 5 measurements. Each curve includes the fixed REP startup cost, measured against a no-REP baseline.

Curve cost for aligned forward rep movsb, in cycles:

Bytes Cycles
1–127 11.25
128 37.14
1 KiB 45.21
10 KiB 190.52
64 KiB (extrapolated) 1,222.45

Verification

  • rep_bulk and rep_bulk_legacy pass: zero-length, one-byte, hot and cold 10 KiB, backward overlap, stores, and self-copy.
  • The hot-copy regression warms its buffers after all setup code, so lazy symbol binding in the dynamic linker cannot evict buffer lines before the measured copy.
  • Full callgrind suite: 29 of 31 pass. runtime_obj_skip_c and runtime_obj_skip_underflow fail with and without this change.

Modeling limits requiring review

This is a draft, not a claim of calibrated end-to-end speedups. Existing scalar/vector pricing is unchanged, so REP copies can score far cheaper than equivalent vector loops even where native timing does not agree.

The table was measured on Zen 5; the existing static instruction LUT is Zen 4. Calibration targets disjoint L1-resident buffers up to 16 KiB MOVS / 32 KiB STOS. Larger sizes extrapolate the final slope and keep existing miss penalties. Overlap performance is outside the calibration domain, although copy semantics are preserved. Mixed REP/non-REP Ct/Cl aggregation keeps the existing geometric-mean cross terms.

Dr/Dw become modeled miss-only references for supported REP instructions, not raw access counts.

@codspeed

codspeed Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 84 untouched benchmarks
⏩ 60 skipped benchmarks1


Comparing rep-bulk-cost-model (7e853af) with master (346abce)

Open in CodSpeed

Footnotes

  1. 60 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@not-matthias
not-matthias marked this pull request as ready for review November 3, 2026 09:31
@greptile-apps

greptile-apps Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; both previous findings are fixed, and no new actionable issues were found.

Summary

Adds calibrated amd64 REP MOVS/STOS costs to the existing Ct and Cl events.

  • REP copies now use measured curves for cycle costs.
  • REP data counts now include only cache misses.

Reviews (2) · Last reviewed commit: "fixup! fix(callgrind): model REP copies ..." · Reviewed by Greptile

Comment thread callgrind/sim.c Resolved
Comment thread callgrind/rep_bulk_table.inc Resolved

@GuillaumeLagrange GuillaumeLagrange left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, how do we plan to expose this? Experimental flag that will become on by default on next major if we're happy?

Replace per-element REP MOVS/STOS cycle estimates with measured hot-copy curves on amd64. Charge the calibrated marginal through the existing Ct/Cl counters and report only data L1 misses as Dr/Dw, retaining cache simulation, instruction counters, and all miss penalties.

Enable the model with cache simulation and cycle estimation; --rep-bulk-cost=no restores the per-element accounting. Keep the output format unchanged and document mixed-counter, overlap, cache-residency, and vector-comparison limitations.

The curves come from a Zen 5 calibration generated out of tree. Each curve includes the fixed REP startup cost measured against a no-REP baseline. Points that do not change the interpolated cost at any element multiple are dropped, so the table stores 2,567 points across 48 curves.

Add default and legacy runtime regressions for zero-length copies, hot/cold counters, backward overlap, stores, and self-copy.
@not-matthias

Copy link
Copy Markdown
Member Author

lgtm, how do we plan to expose this? Experimental flag that will become on by default on next major if we're happy?

yep, that's the plan

The bulk curve covers L1-resident cost. Charging every L1 miss of a REP
copy on top priced lines streamed from LL like isolated misses, which
overstated large copies by an order of magnitude. Only accesses that miss
LL are now reported for modeled REP instructions.
@not-matthias
not-matthias merged commit 7e853af into master Nov 10, 2026
10 checks passed
@not-matthias
not-matthias deleted the rep-bulk-cost-model branch November 10, 2026 15:15
adriencaccia added a commit to CodSpeedHQ/codspeed that referenced this pull request Nov 14, 2026
`--experimental-rep-bulk-cost` passes `--rep-bulk-cost=yes` to
Callgrind, which 0codspeed13 rejects as an unknown option, so the run
fails. 0codspeed14 adds the option (CodSpeedHQ/valgrind-codspeed#60),
off by default: results without the flag do not change.

Closes COD-4234

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants