Skip to content

fix: unsigned casts restored in every statement and loop condition; truncation survives a widening cast - #901

Open
sjkim1127 wants to merge 3 commits into
mainfrom
fix/unsigned-cast-visits-every-statement
Open

sjkim1127 wants to merge 3 commits into
mainfrom
fix/unsigned-cast-visits-every-statement

Conversation

@sjkim1127

Copy link
Copy Markdown
Contributor

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).

Commit Defect Owner
f0d1b7dd2 An unsigned compare in a while / do-while condition never got its (uint) cast back, so it printed as a signed compare. fission-midend-normalize cleanup/casts.rs
5ba13f322 The cast-restoring traversal used .iter_mut().map(mutate).any(..). any short-circuits, so every statement (and every call argument) after the first one that changed was skipped. Same shape in iv_recovery::substitute_var_in_expr, where f(old, old) became f(new, old). cleanup/casts.rs, recovery/iv_recovery.rs
7be6e77b2 (outer)(inner)x was peeled to (outer)x whenever both casts were unsigned and outer >= inner, without checking that x fits in inner. (uint)(uchar)x with a 32-bit x lost its truncation. The rule existed twice. render/presentation/cleanup.rs, render/printer.rs

The third defect was hidden until the second was fixed: once the pass visited every statement, two functions (USBD_StdDevReq in two binaries) turned (uchar)uVar13 >= 5 into uVar13 >= 5, a byte compare into a 32-bit compare.

Why the new output is better (without the metric)

  • r0 >= -4096 on an int binding 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 does movq -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).

unoptimized (31,987 rows) inlined / O2 (24,186 rows)
Total distance 394,148 → 394,148 621,358 → 621,358
GED perfect 12,976 → 12,976 7,378 → 7,378
Union / type / byte perfect 15,131 / 3,383 / 780, unchanged 7,827 / 624 / 501, unchanged
Rows whose score moved 0 0
Functions whose text changed 1,775 2,333
Casts added 6,113 7,905
Lines where a cast present on main is gone 0 0

Sample-set gate: 67/233 perfect, total distance 7,603 — identical to main on 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), where rax += rax << 1 became rax = (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:

  • Loop conditions + traversal (first two commits): 1,187 functions / 3,155 casts unoptimized, 1,428 / 4,476 O2.
  • Truncation peel (third commit, measured against the build without it): 638 functions / 2,958 casts unoptimized, 992 / 3,431 O2. The dominant shape is a call argument, 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).
  • The iv_recovery fix 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_condition
  • restores_unsigned_cast_in_every_statement_not_only_the_first, restores_unsigned_cast_in_every_call_argument_not_only_the_first
  • substitution_reaches_every_call_argument
  • hir_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 use narrow + 1.

cargo nextest run -p fission-midend-normalize -p fission-pcode: 1,798 passed (debug), 1,797 passed (release). cargo fmt --all --check and CI's cargo clippy --workspace --locked line are clean.

Not fixed

  • Compares on untyped or width-mismatched bindings, and variable-vs-variable compares, are still left uncast (deliberate in the existing pass).
  • a < b || a == b is not fused to <=.

🤖 Generated with Claude Code

sjkim1127 and others added 3 commits November 15, 2026 07:55
`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]>
Copilot AI balanced review requested due to automatic review settings November 15, 2026 01:34

Copilot AI 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.

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

No deployments
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