Repository navigation
Conversation
`canonicalize_unsigned_compare_binding_casts` restores the `(uint)` a
p-code `IntLess` needs when its operand is a signed binding. It visited
`if`, `for` and `switch` conditions but walked straight into the body of
`while` and `do`/`while`, so a compare that loop-condition inlining had
moved into `cond` printed as a signed C `<`.
The defect is older than this branch. It became visible here because a
flag that used to stay behind as `tmpcy = (uint)i >= 10` in the loop
body is now substituted into the condition:
while (i < 10 || i == 10) // i is int: signed in C
while ((uint)i < 10 || i == 10) // what IntLess means
Two tests, each failing with the new arm disabled.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
… first that changed `canonicalize_unsigned_compare_binding_casts` walked a statement list, and a call's arguments, with `.iter_mut().map(rewrite).any(|changed| changed)`. `any` stops at the first `true`, so once one statement had its cast restored, every statement after it in the same list was never visited and kept an unsigned `IntLess` printed as a signed C `<`. One function with four unsigned compares on `short` locals printed one cast and three signed compares. `substitute_var_in_expr` in IV recovery had the same shape on call arguments: `f(old, old)` became `f(new, old)`. Both now fold over every child. Found by reading the functions whose text changed when loop conditions started being visited: in 62 of 235 a cast appeared in the loop condition and disappeared from a later compare, because the walk now stopped earlier. Mechanical correctness change. Structure distance cannot see a cast. Co-Authored-By: Claude Fable 5.2 <[email protected]>
`(outer)(inner)x` lost the inner cast whenever both casts were unsigned, the outer was at least as wide, and `x` was unsigned. The width of `x` was never asked, so `(uint)(uchar)x` for a 32-bit `x` printed as `x`: the low byte test `(uchar)v >= 5` became `v >= 5`. The rule existed twice, in presentation cleanup and in the printer, and both now also require that `x` is no wider than the inner cast, which is what makes that cast width sugar. Surfaced by the previous commit: an unsigned compare that now gets its `(uint)` cast put that cast directly over a forwarded `(uchar)`. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Contributor
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three pre-existing correctness defects in how unsigned casts are restored and peeled, found while reading the changed output of #900. This is a correctness fix, not a quality claim: no score moved on any measured row (structure distance cannot see a cast).
f0d1b7dd2while/do-whilecondition never got its(uint)cast back, so it printed as a signed compare.fission-midend-normalizecleanup/casts.rs5ba13f322.iter_mut().map(mutate).any(..).anyshort-circuits, so every statement (and every call argument) after the first one that changed was skipped. Same shape iniv_recovery::substitute_var_in_expr, wheref(old, old)becamef(new, old).cleanup/casts.rs,recovery/iv_recovery.rs7be6e77b2(outer)(inner)xwas peeled to(outer)xwhenever both casts were unsigned andouter >= inner, without checking thatxfits ininner.(uint)(uchar)xwith a 32-bitxlost its truncation. The rule existed twice.render/presentation/cleanup.rs,render/printer.rsThe third defect was hidden until the second was fixed: once the pass visited every statement, two functions (
USBD_StdDevReqin two binaries) turned(uchar)uVar13 >= 5intouVar13 >= 5, a byte compare into a 32-bit compare.Why the new output is better (without the metric)
r0 >= -4096on anintbinding compiled as a signed compare; the binary does an unsigned one. It now prints(uint)r0 >= 4294963200.substring(local_30, local_40, local_38)passed 64-bit locals; the binary doesmovq -0x38(%rbp), %rax; movl %eax, %edx. It now prints(unsigned long long)(uint)local_38. The neighbouring line in the same function already printed that form for a signed 64-bit local — only unsigned ones lost the truncation.Measurement
Full DecBench sweeps, caches disabled, same rows,
main(1c10eda) vs this branch (7be6e77).mainis goneSample-set gate: 67/233 perfect, total distance 7,603 — identical to
mainon all 233 rows.Every changed line was classified by cast-token multiset. All are cast-only additions except one line (
e2fsck_add_dx_dir, O2), whererax += rax << 1becamerax = (unsigned long long)(uint)rax + ((unsigned long long)(uint)rax << 1)because its operands gained casts and it is no longer a compound assignment.By commit:
f(x)→f((unsigned long long)(uint)x), where a 64-bit unsigned variable is passed in a 32-bit register (476 functions unoptimized, 902 O2).iv_recoveryfix changed no corpus output; it is covered by its test only.Cost, stated plainly
The third commit makes ~1,600 functions noisier. Where the variable really is 64-bit the cast is a truncation the old output hid. Where type recovery declared a 32-bit value as 64-bit, the cast is correct for the declared type but would not be needed with the right declaration; that is a type-recovery defect this PR does not address.
Tests
Seven new tests, each with a positive control (the fix reverted, test confirmed failing):
restores_unsigned_cast_in_a_while_condition,restores_unsigned_cast_in_a_do_while_conditionrestores_unsigned_cast_in_every_statement_not_only_the_first,restores_unsigned_cast_in_every_call_argument_not_only_the_firstsubstitution_reaches_every_call_argumenthir_presentation_keeps_a_truncation_under_a_widening_cast,hir_presentation_peels_a_widening_cast_over_a_narrower_unsigned_value— the two rule sites (presentation, printer) were reverted one at a time; either revert fails the first test. The second test originally passed with the rule disabled (a bare variable is peeled by a different rule) and was rewritten to usenarrow + 1.cargo nextest run -p fission-midend-normalize -p fission-pcode: 1,798 passed (debug), 1,797 passed (release).cargo fmt --all --checkand CI'scargo clippy --workspace --lockedline are clean.Not fixed
a < b || a == bis not fused to<=.🤖 Generated with Claude Code