Skip to content

Loop IDs with a sketch that finds a moved or edited loop - #9638

Draft
tautschnig wants to merge 2 commits into
diffblue:developfrom
tautschnig:loop-ids-sketch
Draft

tautschnig wants to merge 2 commits into
diffblue:developfrom
tautschnig:loop-ids-sketch

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

Depends-on: #9637

A loop ID of the form F.L-N still changes when a real loop is inserted
earlier in function F, and then silently refers to a different loop.
--show-loops now prints F.L-N-S, where S is a sketch of the loop's
instructions as 16 hexadecimal digits, and --unwindset uses the sketch
to find the loop again.

The sketch is a b-bit MinHash signature (16 components of 4 bits) of
the multiset of the fingerprints of the loop's instructions.
Fingerprints do not include the names of local variables, types, the
values of constants, branch targets, or comments such as source
locations, so that the sketch does not depend on configuration
parameters such as array sizes. Functions and variables with static
lifetime are fingerprinted by their base names. ASSERT, ASSUME, SKIP and
LOCATION instructions and the backwards gotos of pseudo-loops are
ignored, so checks such as --bounds-check do not change the sketch. The
similarity of two sketches estimates the Jaccard index of the two
multisets, corrected for components that agree by chance.

--unwindset resolves F.L-N-S to the loop of F whose sketch is most
similar to S, if the similarity is at least 0.5 and exceeds that of
every other loop of F by at least 0.1. It warns, and prints the current
ID of the loop, if that loop does not have index N ("moved") or if the
similarity is below 0.8 ("changed"). If no loop clearly matches, the
loop with index N is used, with a warning. F.L-N without a sketch keeps
its meaning. The thresholds are provisional: they were chosen from the
replay below, and should be revisited as data from more projects
becomes available.

The replay of the histories of mlkem-native and mldsa-native used for
F.L-N (1151 pairs of consecutive mainline commits, with ground truth
from a line diff of each loop's header) was repeated with goto-cc and
goto-instrument --show-loops of this commit. Of the 1544 loops that a
commit changed, F.L-N-S never referred to the wrong loop without a
warning, whereas F.L-N did so 7 times. F.L-N-S referred to the wrong
loop with a warning 7 times, and warned about 233 loops that it
resolved correctly. Of the 24 loops whose index N changed, F.L-N-S found
13, each with a "moved" warning; F.L-N referred to the wrong loop for 7
of them and to no loop for the other 17. Of the 168 loops named in an
UNWINDSET entry, F.L-N-S resolved 167 correctly (37 with a warning) and
one to no loop, with a warning. Across parameter sets (MLKEM_K 2 and 4
compared to 3, and ML-DSA-44 and -87 compared to ML-DSA-65, for every
tenth commit), F.L-N-S resolved all 1072 loops named in an UNWINDSET
entry correctly, 1070 of them without a warning.

Co-authored-by: Kiro kiro-agent@users.noreply.github.com

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

tautschnig and others added 2 commits December 3, 2026 23:51
Loop IDs of the form F.N number all loops of function F, including
pseudo-loops: loops whose condition is constant false, such as the
do { ... } while(0) that C macros commonly expand to. Adding or removing
such a macro before a loop therefore changes the ID of that loop, and
--unwindset settings silently stop applying to the intended loop. In
mlkem-native, a change to debug-assertion macros turned
polyvec_basemul_acc_montgomery_cached.3 into .0, and for 37 mainline
commits an UNWINDSET entry .0 bounded the do-while(0) of a bounds-check
macro instead of the loop it was meant for.

--show-loops now also shows a loop ID of the form F.L-N for each loop
that is not a pseudo-loop, where N only counts such loops, and
--unwindset accepts these IDs. Pseudo-loops have no such ID, so a loop
added later never takes over an ID that belonged to a pseudo-loop. IDs
of the form F.N keep their meaning; `-` cannot occur in labels, so the
new form cannot be mistaken for a label either. As a bound for a
pseudo-loop has no effect, --unwindset now warns when a loop ID refers
to one.

Replaying the histories of mlkem-native and mldsa-native (1151 pairs of
consecutive mainline commits that touch the sources or CBMC proofs, with
ground truth from a line diff of each loop's header) shows that F.L-N
would never have referred to the wrong loop for a loop named in an
UNWINDSET entry. Over all 1544 loops that a commit changed, F.L-N
referred to the wrong loop 7 times, compared to 19 times for F.N. F.L-N
still changes when a real loop is inserted earlier in the same
function.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
A loop ID of the form F.L-N still changes when a real loop is inserted
earlier in function F, and then silently refers to a different loop.
--show-loops now prints F.L-N-S, where S is a sketch of the loop's
instructions as 16 hexadecimal digits, and --unwindset uses the sketch
to find the loop again.

The sketch is a b-bit MinHash signature (16 components of 4 bits) of
the multiset of the fingerprints of the loop's instructions.
Fingerprints do not include the names of local variables, types, the
values of constants, branch targets, or comments such as source
locations, so that the sketch does not depend on configuration
parameters such as array sizes. Functions and variables with static
lifetime are fingerprinted by their base names. ASSERT, ASSUME, SKIP and
LOCATION instructions and the backwards gotos of pseudo-loops are
ignored, so checks such as --bounds-check do not change the sketch. The
similarity of two sketches estimates the Jaccard index of the two
multisets, corrected for components that agree by chance.

--unwindset resolves F.L-N-S to the loop of F whose sketch is most
similar to S, if the similarity is at least 0.5 and exceeds that of
every other loop of F by at least 0.1. It warns, and prints the current
ID of the loop, if that loop does not have index N ("moved") or if the
similarity is below 0.8 ("changed"). If no loop clearly matches, the
loop with index N is used, with a warning. F.L-N without a sketch keeps
its meaning. The thresholds are provisional: they were chosen from the
replay below, and should be revisited as data from more projects
becomes available.

The replay of the histories of mlkem-native and mldsa-native used for
F.L-N (1151 pairs of consecutive mainline commits, with ground truth
from a line diff of each loop's header) was repeated with goto-cc and
goto-instrument --show-loops of this commit. Of the 1544 loops that a
commit changed, F.L-N-S never referred to the wrong loop without a
warning, whereas F.L-N did so 7 times. F.L-N-S referred to the wrong
loop with a warning 7 times, and warned about 233 loops that it
resolved correctly. Of the 24 loops whose index N changed, F.L-N-S found
13, each with a "moved" warning; F.L-N referred to the wrong loop for 7
of them and to no loop for the other 17. Of the 168 loops named in an
UNWINDSET entry, F.L-N-S resolved 167 correctly (37 with a warning) and
one to no loop, with a warning. Across parameter sets (MLKEM_K 2 and 4
compared to 3, and ML-DSA-44 and -87 compared to ML-DSA-65, for every
tenth commit), F.L-N-S resolved all 1072 loops named in an UNWINDSET
entry correctly, 1070 of them without a warning.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@codecov

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.26590% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.39%. Comparing base (cce28f5) to head (8dc7140).
⚠️ Report is 4 commits behind head on develop.

Files with missing lines Patch % Lines
jbmc/src/jbmc/jbmc_parse_options.cpp 0.00% 3 Missing ⚠️
src/goto-programs/loop_sketch.cpp 98.00% 2 Missing ⚠️
src/goto-programs/loop_ids.cpp 99.11% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9638      +/-   ##
===========================================
+ Coverage    81.34%   81.39%   +0.04%     
===========================================
  Files         1727     1730       +3     
  Lines       191715   192087     +372     
  Branches        73       73              
===========================================
+ Hits        155950   156340     +390     
+ Misses       35765    35747      -18     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant